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';
114 * Cutycapt adapter options
116 public $cutycapt = array();
120 * Initialize default values and loads configuration file paths
122 public function __construct()
124 $this->cacheDir = getcwd() . '/imgcache/';
125 $this->cacheDirUrl = $this->getCurrentUrlDir() . '/imgcache/';
127 $this->timestampMaxAge = Options::validateAge($this->timestampMaxAge);
128 $this->screenshotMaxAge = Options::validateAge($this->screenshotMaxAge);
129 $this->screenshotMinAge = Options::validateAge($this->screenshotMinAge);
131 $this->loadConfigFilePaths();
135 * Load the first configuration file that exists
138 * @uses $cfgFileExists
140 public function load()
142 $this->cfgFileExists = false;
143 foreach ($this->cfgFiles as $file) {
144 if (file_exists($file)) {
145 $this->cfgFileExists = true;
146 $this->loadFile($file);
155 * Load possible configuration file paths into $this->cfgFiles.
159 protected function loadConfigFilePaths()
162 if (class_exists('\\Phar')) {
163 $pharFile = \Phar::running();
164 if ($pharFile != '') {
169 //remove phar:// from the path
170 $this->cfgFiles[] = substr($pharFile, 7) . '.config.php';
172 $this->cfgFiles[] = __DIR__ . '/../../data/phancap.config.php';
175 //TODO: add ~/.config/phancap.php
177 $this->cfgFiles[] = '/etc/phancap.php';
181 * Load values of a configuration file into this class
183 * @param string $filename Configuration file (.php)
187 protected function loadFile($filename)
190 $vars = get_defined_vars();
191 foreach ($vars as $k => $value) {
197 * Check if the cache directory exists and is writable
200 * @throws \Exception When something is not ok
202 public function setupCheck()
204 if (!is_dir($this->cacheDir)) {
205 throw new \Exception(
206 'Cache directory does not exist: ' . $this->cacheDir
209 if (!is_writable($this->cacheDir)) {
210 throw new \Exception(
211 'Cache directory is not writable: ' . $this->cacheDir
217 * Returns the current URL, without fragmet.
219 * @return string Full URL
221 protected function getCurrentUrl()
223 if (!isset($_SERVER['REQUEST_SCHEME'])) {
224 $_SERVER['REQUEST_SCHEME'] = 'http';
226 return $_SERVER['REQUEST_SCHEME'] . '://'
227 . $_SERVER['HTTP_HOST']
228 . preg_replace('/#.*$/', '', $_SERVER['REQUEST_URI']);
232 * Returns the current URL without the file name in the path
234 * @return string Directory of URL without trailing slash,
235 * and without .phar file
237 public function getCurrentUrlDir()
239 $url = $this->getCurrentUrl();
240 $url = preg_replace('/\?.*$/', '', $url);
241 if (substr($url, -1) != '/') {
242 $url = substr($url, 0, -strlen(basename($url)) - 1);
244 if (class_exists('\\Phar') && \Phar::running()) {
245 //remove .phar file name
246 $url = substr($url, 0, -strlen(basename($url)) - 1);