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 * Disable the setup check tool
49 public $disableSetup = false;
52 * Redirect the browser to the cache URL.
53 * If disabled, the file is directly delivered.
55 * Helpful for debugging since it does not change the browser's URL.
59 public $redirect = true;
62 * How long requests with an old timestamp may be used.
67 public $timestampMaxAge = 'P2D';
70 * Cache time of downloaded screenshots.
71 * When the file is as older than this, it gets re-created.
72 * The user can override that using the "smaxage" parameter.
76 * @var integer Lifetime in seconds
78 public $screenshotMaxAge = 'P1W';
81 * Minimum age of a screeshot.
82 * A user cannot set the max age parameter below it.
86 * @var integer Minimum lifetime in seconds
88 public $screenshotMinAge = 'PT1H';
91 public function __construct()
93 $this->cacheDir = getcwd() . '/imgcache/';
94 $this->cacheDirUrl = $this->getCurrentUrlDir() . '/imgcache/';
96 $this->timestampMaxAge = Options::validateAge($this->timestampMaxAge);
97 $this->screenshotMaxAge = Options::validateAge($this->screenshotMaxAge);
98 $this->screenshotMinAge = Options::validateAge($this->screenshotMinAge);
100 $this->loadConfigFilePaths();
103 public function load()
105 $this->cfgFileExists = false;
106 foreach ($this->cfgFiles as $file) {
107 if (file_exists($file)) {
108 $this->cfgFileExists = true;
109 $this->loadFile($file);
118 * Load possible configuration file paths into $this->cfgFiles.
122 protected function loadConfigFilePaths()
124 $pharFile = \Phar::running();
125 if ($pharFile == '') {
126 $this->cfgFiles[] = __DIR__ . '/../../data/phancap.config.php';
128 //remove phar:// from the path
129 $this->cfgFiles[] = substr($pharFile, 7) . '.config.php';
132 //TODO: add ~/.config/phancap.php
134 $this->cfgFiles[] = '/etc/phancap.php';
137 protected function loadFile($filename)
140 $vars = get_defined_vars();
141 foreach ($vars as $k => $value) {
146 public function setupCheck()
148 if (!is_dir($this->cacheDir)) {
149 throw new \Exception('Cache directory does not exist: ' . $this->cacheDir);
151 if (!is_writable($this->cacheDir)) {
152 throw new \Exception('Cache directory is not writable: ' . $this->cacheDir);
156 protected function getCurrentUrl()
158 if (!isset($_SERVER['REQUEST_SCHEME'])) {
159 $_SERVER['REQUEST_SCHEME'] = 'http';
161 return $_SERVER['REQUEST_SCHEME'] . '://'
162 . $_SERVER['HTTP_HOST']
163 . preg_replace('/#.*$/', '', $_SERVER['REQUEST_URI']);
167 * @return string Directory of URL without trailing slash,
168 * and without .phar file
170 protected function getCurrentUrlDir()
172 $url = $this->getCurrentUrl();
173 $url = preg_replace('/\?.*$/', '', $url);
174 if (substr($url, -1) != '/') {
175 $url = substr($url, 0, -strlen(basename($url)) - 1);
177 if (\Phar::running()) {
178 //remove .phar file name
179 $url = substr($url, 0, -strlen(basename($url)) - 1);