7 * Full file system path to cache directory
13 * Full URL to cache directory
19 * Credentials for access
22 * username => secret key
23 * entries (used for signature).
25 * Boolean true to allow access in every case,
26 * false to completely disable it.
30 public $access = true;
33 * Redirect the browser to the cache URL.
34 * If disabled, the file is directly delivered.
36 * Helpful for debugging since it does not change the browser's URL.
40 public $redirect = true;
43 * How long requests with an old timestamp may be used.
48 public $timestampMaxAge = 'P2D';
51 * Cache time of downloaded screenshots.
52 * When the file is as older than this, it gets re-created.
53 * The user can override that using the "smaxage" parameter.
57 * @var integer Lifetime in seconds
59 public $screenshotMaxAge = 'P1W';
62 * Minimum age of a screeshot.
63 * A user cannot set the max age parameter below it.
67 * @var integer Minimum lifetime in seconds
69 public $screenshotMinAge = 'PT1H';
72 public function __construct()
74 $this->cacheDir = getcwd() . '/imgcache/';
75 $this->cacheDirUrl = $this->getCurrentUrlDir() . '/imgcache/';
77 $this->timestampMaxAge = Options::validateAge($this->timestampMaxAge);
78 $this->screenshotMaxAge = Options::validateAge($this->screenshotMaxAge);
79 $this->screenshotMinAge = Options::validateAge($this->screenshotMinAge);
82 public function load()
84 $cfgFile = __DIR__ . '/../../data/phancap.config.php';
85 if (file_exists($cfgFile)) {
86 $this->loadFile($cfgFile);
92 protected function loadFile($filename)
95 $vars = get_defined_vars();
96 foreach ($vars as $k => $value) {
101 public function setupCheck()
103 if (!is_dir($this->cacheDir)) {
104 throw new \Exception('Cache directory does not exist: ' . $this->cacheDir);
106 if (!is_writable($this->cacheDir)) {
107 throw new \Exception('Cache directory is not writable: ' . $this->cacheDir);
111 protected function getCurrentUrl()
113 if (!isset($_SERVER['REQUEST_SCHEME'])) {
114 $_SERVER['REQUEST_SCHEME'] = 'http';
116 return $_SERVER['REQUEST_SCHEME'] . '://'
117 . $_SERVER['HTTP_HOST']
118 . preg_replace('/#.*$/', '', $_SERVER['REQUEST_URI']);
122 * @return string Directory of URL without trailing slash,
123 * and without .phar file
125 protected function getCurrentUrlDir()
127 $url = $this->getCurrentUrl();
128 $url = preg_replace('/\?.*$/', '', $url);
129 if (substr($url, -1) != '/') {
130 $url = substr($url, 0, -strlen(basename($url)) - 1);
132 if (\Phar::running()) {
133 //remove .phar file name
134 $url = substr($url, 0, -strlen(basename($url)) - 1);