9 * @author Christian Weiske <cweiske@cweiske.de>
10 * @copyright 2014 Christian Weiske
11 * @license http://www.gnu.org/licenses/agpl.html GNU AGPL v3
12 * @link http://cweiske.de/phancap.htm
17 * Phancap configuration
21 * @author Christian Weiske <cweiske@cweiske.de>
22 * @copyright 2014 Christian Weiske
23 * @license http://www.gnu.org/licenses/agpl.html GNU AGPL v3
24 * @version Release: @package_version@
25 * @link http://cweiske.de/phancap.htm
30 * Full file system path to cache directory
36 * Full URL to cache directory
43 * List of config file paths that were tried to load
46 public $cfgFiles = array();
49 * If a configuration file could be found
52 public $cfgFileExists;
55 * Credentials for access
58 * username => secret key
59 * entries (used for signature).
61 * Boolean true to allow access in every case,
62 * false to completely disable it.
66 public $access = true;
69 * Disable the setup check tool
72 public $disableSetup = false;
75 * Redirect the browser to the cache URL.
76 * If disabled, the file is directly delivered.
78 * Helpful for debugging since it does not change the browser's URL.
82 public $redirect = true;
85 * How long requests with an old timestamp may be used.
90 public $timestampMaxAge = 'P2D';
93 * Cache time of downloaded screenshots.
94 * When the file is as older than this, it gets re-created.
95 * The user can override that using the "smaxage" parameter.
99 * @var integer Lifetime in seconds
101 public $screenshotMaxAge = 'P1W';
104 * Minimum age of a screeshot.
105 * A user cannot set the max age parameter below it.
107 * Defaults to 1 hour.
109 * @var integer Minimum lifetime in seconds
111 public $screenshotMinAge = 'PT1H';
115 * Initialize default values and loads configuration file paths
117 public function __construct()
119 $this->cacheDir = getcwd() . '/imgcache/';
120 $this->cacheDirUrl = $this->getCurrentUrlDir() . '/imgcache/';
122 $this->timestampMaxAge = Options::validateAge($this->timestampMaxAge);
123 $this->screenshotMaxAge = Options::validateAge($this->screenshotMaxAge);
124 $this->screenshotMinAge = Options::validateAge($this->screenshotMinAge);
126 $this->loadConfigFilePaths();
130 * Load the first configuration file that exists
133 * @uses $cfgFileExists
135 public function load()
137 $this->cfgFileExists = false;
138 foreach ($this->cfgFiles as $file) {
139 if (file_exists($file)) {
140 $this->cfgFileExists = true;
141 $this->loadFile($file);
150 * Load possible configuration file paths into $this->cfgFiles.
154 protected function loadConfigFilePaths()
156 $pharFile = \Phar::running();
157 if ($pharFile == '') {
158 $this->cfgFiles[] = __DIR__ . '/../../data/phancap.config.php';
160 //remove phar:// from the path
161 $this->cfgFiles[] = substr($pharFile, 7) . '.config.php';
164 //TODO: add ~/.config/phancap.php
166 $this->cfgFiles[] = '/etc/phancap.php';
170 * Load values of a configuration file into this class
172 * @param string $filename Configuration file (.php)
176 protected function loadFile($filename)
179 $vars = get_defined_vars();
180 foreach ($vars as $k => $value) {
186 * Check if the cache directory exists and is writable
189 * @throws \Exception When something is not ok
191 public function setupCheck()
193 if (!is_dir($this->cacheDir)) {
194 throw new \Exception(
195 'Cache directory does not exist: ' . $this->cacheDir
198 if (!is_writable($this->cacheDir)) {
199 throw new \Exception(
200 'Cache directory is not writable: ' . $this->cacheDir
206 * Returns the current URL, without fragmet.
208 * @return string Full URL
210 protected function getCurrentUrl()
212 if (!isset($_SERVER['REQUEST_SCHEME'])) {
213 $_SERVER['REQUEST_SCHEME'] = 'http';
215 return $_SERVER['REQUEST_SCHEME'] . '://'
216 . $_SERVER['HTTP_HOST']
217 . preg_replace('/#.*$/', '', $_SERVER['REQUEST_URI']);
221 * Returns the current URL without the file name in the path
223 * @return string Directory of URL without trailing slash,
224 * and without .phar file
226 protected function getCurrentUrlDir()
228 $url = $this->getCurrentUrl();
229 $url = preg_replace('/\?.*$/', '', $url);
230 if (substr($url, -1) != '/') {
231 $url = substr($url, 0, -strlen(basename($url)) - 1);
233 if (\Phar::running()) {
234 //remove .phar file name
235 $url = substr($url, 0, -strlen(basename($url)) - 1);