7 * Full file system path to cache directory
13 * Full URL to cache directory
20 * List of config file paths that were tried to load
23 public $cfgFiles = array();
26 * If a configuration file could be found
29 public $cfgFileExists;
32 * Credentials for access
35 * username => secret key
36 * entries (used for signature).
38 * Boolean true to allow access in every case,
39 * false to completely disable it.
43 public $access = true;
46 * Redirect the browser to the cache URL.
47 * If disabled, the file is directly delivered.
49 * Helpful for debugging since it does not change the browser's URL.
53 public $redirect = true;
56 * How long requests with an old timestamp may be used.
61 public $timestampMaxAge = 'P2D';
64 * Cache time of downloaded screenshots.
65 * When the file is as older than this, it gets re-created.
66 * The user can override that using the "smaxage" parameter.
70 * @var integer Lifetime in seconds
72 public $screenshotMaxAge = 'P1W';
75 * Minimum age of a screeshot.
76 * A user cannot set the max age parameter below it.
80 * @var integer Minimum lifetime in seconds
82 public $screenshotMinAge = 'PT1H';
85 public function __construct()
87 $this->cacheDir = getcwd() . '/imgcache/';
88 $this->cacheDirUrl = $this->getCurrentUrlDir() . '/imgcache/';
90 $this->timestampMaxAge = Options::validateAge($this->timestampMaxAge);
91 $this->screenshotMaxAge = Options::validateAge($this->screenshotMaxAge);
92 $this->screenshotMinAge = Options::validateAge($this->screenshotMinAge);
94 $this->loadConfigFilePaths();
97 public function load()
99 $this->cfgFileExists = false;
100 foreach ($this->cfgFiles as $file) {
101 if (file_exists($file)) {
102 $this->cfgFileExists = true;
103 $this->loadFile($file);
112 * Load possible configuration file paths into $this->cfgFiles.
116 protected function loadConfigFilePaths()
118 $pharFile = \Phar::running();
119 if ($pharFile == '') {
120 $this->cfgFiles[] = __DIR__ . '/../../data/phancap.config.php';
122 //remove phar:// from the path
123 $this->cfgFiles[] = substr($pharFile, 7) . '.config.php';
126 //TODO: add ~/.config/phancap.php
128 $this->cfgFiles[] = '/etc/phancap.php';
131 protected function loadFile($filename)
134 $vars = get_defined_vars();
135 foreach ($vars as $k => $value) {
140 public function setupCheck()
142 if (!is_dir($this->cacheDir)) {
143 throw new \Exception('Cache directory does not exist: ' . $this->cacheDir);
145 if (!is_writable($this->cacheDir)) {
146 throw new \Exception('Cache directory is not writable: ' . $this->cacheDir);
150 protected function getCurrentUrl()
152 if (!isset($_SERVER['REQUEST_SCHEME'])) {
153 $_SERVER['REQUEST_SCHEME'] = 'http';
155 return $_SERVER['REQUEST_SCHEME'] . '://'
156 . $_SERVER['HTTP_HOST']
157 . preg_replace('/#.*$/', '', $_SERVER['REQUEST_URI']);
161 * @return string Directory of URL without trailing slash,
162 * and without .phar file
164 protected function getCurrentUrlDir()
166 $url = $this->getCurrentUrl();
167 $url = preg_replace('/\?.*$/', '', $url);
168 if (substr($url, -1) != '/') {
169 $url = substr($url, 0, -strlen(basename($url)) - 1);
171 if (\Phar::running()) {
172 //remove .phar file name
173 $url = substr($url, 0, -strlen(basename($url)) - 1);