add option to disable setup check
[phancap.git] / src / phancap / Config.php
1 <?php
2 namespace phancap;
3
4 class Config
5 {
6     /**
7      * Full file system path to cache directory
8      * @var string
9      */
10     public $cacheDir;
11
12     /**
13      * Full URL to cache directory
14      * @var string
15      */
16     public $cacheDirUrl;
17
18
19     /**
20      * List of config file paths that were tried to load
21      * @var array
22      */
23     public $cfgFiles = array();
24
25     /**
26      * If a configuration file could be found
27      * @var boolean
28      */
29     public $cfgFileExists;
30
31     /**
32      * Credentials for access
33      *
34      * Array of
35      *     username => secret key
36      * entries (used for signature).
37      *
38      * Boolean true to allow access in every case,
39      * false to completely disable it.
40      *
41      * @var array|boolean
42      */
43     public $access = true;
44
45     /**
46      * Disable the setup check tool
47      * @var boolean
48      */
49     public $disableSetup = false;
50
51     /**
52      * Redirect the browser to the cache URL.
53      * If disabled, the file is directly delivered.
54      *
55      * Helpful for debugging since it does not change the browser's URL.
56      *
57      * @var boolean
58      */
59     public $redirect = true;
60
61     /**
62      * How long requests with an old timestamp may be used.
63      * 2 days default.
64      *
65      * @var integer
66      */
67     public $timestampMaxAge = 'P2D';
68
69     /**
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.
73      *
74      * Defaults to 1 week.
75      *
76      * @var integer Lifetime in seconds
77      */
78     public $screenshotMaxAge = 'P1W';
79
80     /**
81      * Minimum age of a screeshot.
82      * A user cannot set the max age parameter below it.
83      *
84      * Defaults to 1 hour.
85      *
86      * @var integer Minimum lifetime in seconds
87      */
88     public $screenshotMinAge = 'PT1H';
89
90
91     public function __construct()
92     {
93         $this->cacheDir    = getcwd() . '/imgcache/';
94         $this->cacheDirUrl = $this->getCurrentUrlDir() . '/imgcache/';
95
96         $this->timestampMaxAge  = Options::validateAge($this->timestampMaxAge);
97         $this->screenshotMaxAge = Options::validateAge($this->screenshotMaxAge);
98         $this->screenshotMinAge = Options::validateAge($this->screenshotMinAge);
99
100         $this->loadConfigFilePaths();
101     }
102
103     public function load()
104     {
105         $this->cfgFileExists = false;
106         foreach ($this->cfgFiles as $file) {
107             if (file_exists($file)) {
108                 $this->cfgFileExists = true;
109                 $this->loadFile($file);
110                 break;
111             }
112         }
113
114         $this->setupCheck();
115     }
116
117     /**
118      * Load possible configuration file paths into $this->cfgFiles.
119      *
120      * @return void
121      */
122     protected function loadConfigFilePaths()
123     {
124         $pharFile = \Phar::running();
125         if ($pharFile == '') {
126             $this->cfgFiles[] = __DIR__ . '/../../data/phancap.config.php';
127         } else {
128             //remove phar:// from the path
129             $this->cfgFiles[] = substr($pharFile, 7) . '.config.php';
130         }
131
132         //TODO: add ~/.config/phancap.php
133
134         $this->cfgFiles[] = '/etc/phancap.php';
135     }
136
137     protected function loadFile($filename)
138     {
139         include $filename;
140         $vars = get_defined_vars();
141         foreach ($vars as $k => $value) {
142             $this->$k = $value;
143         }
144     }
145
146     public function setupCheck()
147     {
148         if (!is_dir($this->cacheDir)) {
149             throw new \Exception('Cache directory does not exist: ' . $this->cacheDir);
150         }
151         if (!is_writable($this->cacheDir)) {
152             throw new \Exception('Cache directory is not writable: ' . $this->cacheDir);
153         }
154     }
155
156     protected function getCurrentUrl()
157     {
158         if (!isset($_SERVER['REQUEST_SCHEME'])) {
159             $_SERVER['REQUEST_SCHEME'] = 'http';
160         }
161         return $_SERVER['REQUEST_SCHEME'] . '://'
162             . $_SERVER['HTTP_HOST']
163             . preg_replace('/#.*$/', '', $_SERVER['REQUEST_URI']);
164     }
165
166     /**
167      * @return string Directory of URL without trailing slash,
168      *                and without .phar file
169      */
170     protected function getCurrentUrlDir()
171     {
172         $url = $this->getCurrentUrl();
173         $url = preg_replace('/\?.*$/', '', $url);
174         if (substr($url, -1) != '/') {
175             $url = substr($url, 0, -strlen(basename($url)) - 1);
176         }
177         if (\Phar::running()) {
178             //remove .phar file name
179             $url = substr($url, 0, -strlen(basename($url)) - 1);
180         }
181
182         return $url;
183     }
184 }
185 ?>