b2d42b1dfefde141bfde8be217fd56002a68e296
[phancap.git] / src / phancap / Config.php
1 <?php
2 /**
3  * Part of phancap
4  *
5  * PHP version 5
6  *
7  * @category  Tools
8  * @package   Config
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
13  */
14 namespace phancap;
15
16 /**
17  * Phancap configuration
18  *
19  * @category  Tools
20  * @package   Config
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
26  */
27 class Config
28 {
29     /**
30      * Full file system path to cache directory
31      * @var string
32      */
33     public $cacheDir;
34
35     /**
36      * Full URL to cache directory
37      * @var string
38      */
39     public $cacheDirUrl;
40
41
42     /**
43      * List of config file paths that were tried to load
44      * @var array
45      */
46     public $cfgFiles = array();
47
48     /**
49      * If a configuration file could be found
50      * @var boolean
51      */
52     public $cfgFileExists;
53
54     /**
55      * Credentials for access
56      *
57      * Array of
58      *     username => secret key
59      * entries (used for signature).
60      *
61      * Boolean true to allow access in every case,
62      * false to completely disable it.
63      *
64      * @var array|boolean
65      */
66     public $access = true;
67
68     /**
69      * Disable the setup check tool
70      * @var boolean
71      */
72     public $disableSetup = false;
73
74     /**
75      * Redirect the browser to the cache URL.
76      * If disabled, the file is directly delivered.
77      *
78      * Helpful for debugging since it does not change the browser's URL.
79      *
80      * @var boolean
81      */
82     public $redirect = true;
83
84     /**
85      * How long requests with an old timestamp may be used.
86      * 2 days default.
87      *
88      * @var integer
89      */
90     public $timestampMaxAge = 'P2D';
91
92     /**
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.
96      *
97      * Defaults to 1 week.
98      *
99      * @var integer Lifetime in seconds
100      */
101     public $screenshotMaxAge = 'P1W';
102
103     /**
104      * Minimum age of a screeshot.
105      * A user cannot set the max age parameter below it.
106      *
107      * Defaults to 1 hour.
108      *
109      * @var integer Minimum lifetime in seconds
110      */
111     public $screenshotMinAge = 'PT1H';
112
113     /**
114      * Cutycapt adapter options
115      */
116     public $cutycapt = array();
117
118
119     /**
120      * Initialize default values and loads configuration file paths
121      */
122     public function __construct()
123     {
124         $this->cacheDir    = getcwd() . '/imgcache/';
125         $this->cacheDirUrl = $this->getCurrentUrlDir() . '/imgcache/';
126
127         $this->timestampMaxAge  = Options::validateAge($this->timestampMaxAge);
128         $this->screenshotMaxAge = Options::validateAge($this->screenshotMaxAge);
129         $this->screenshotMinAge = Options::validateAge($this->screenshotMinAge);
130
131         $this->loadConfigFilePaths();
132     }
133
134     /**
135      * Load the first configuration file that exists
136      *
137      * @return void
138      * @uses   $cfgFileExists
139      */
140     public function load()
141     {
142         $this->cfgFileExists = false;
143         foreach ($this->cfgFiles as $file) {
144             if (file_exists($file)) {
145                 $this->cfgFileExists = true;
146                 $this->loadFile($file);
147                 break;
148             }
149         }
150
151         $this->setupCheck();
152     }
153
154     /**
155      * Load possible configuration file paths into $this->cfgFiles.
156      *
157      * @return void
158      */
159     protected function loadConfigFilePaths()
160     {
161         $pharFile = \Phar::running();
162         if ($pharFile == '') {
163             $this->cfgFiles[] = __DIR__ . '/../../data/phancap.config.php';
164         } else {
165             //remove phar:// from the path
166             $this->cfgFiles[] = substr($pharFile, 7) . '.config.php';
167         }
168
169         //TODO: add ~/.config/phancap.php
170
171         $this->cfgFiles[] = '/etc/phancap.php';
172     }
173
174     /**
175      * Load values of a configuration file into this class
176      *
177      * @param string $filename Configuration file (.php)
178      *
179      * @return void
180      */
181     protected function loadFile($filename)
182     {
183         include $filename;
184         $vars = get_defined_vars();
185         foreach ($vars as $k => $value) {
186             $this->$k = $value;
187         }
188     }
189
190     /**
191      * Check if the cache directory exists and is writable
192      *
193      * @return void
194      * @throws \Exception When something is not ok
195      */
196     public function setupCheck()
197     {
198         if (!is_dir($this->cacheDir)) {
199             throw new \Exception(
200                 'Cache directory does not exist: ' . $this->cacheDir
201             );
202         }
203         if (!is_writable($this->cacheDir)) {
204             throw new \Exception(
205                 'Cache directory is not writable: ' . $this->cacheDir
206             );
207         }
208     }
209
210     /**
211      * Returns the current URL, without fragmet.
212      *
213      * @return string Full URL
214      */
215     protected function getCurrentUrl()
216     {
217         if (!isset($_SERVER['REQUEST_SCHEME'])) {
218             $_SERVER['REQUEST_SCHEME'] = 'http';
219         }
220         return $_SERVER['REQUEST_SCHEME'] . '://'
221             . $_SERVER['HTTP_HOST']
222             . preg_replace('/#.*$/', '', $_SERVER['REQUEST_URI']);
223     }
224
225     /**
226      * Returns the current URL without the file name in the path
227      *
228      * @return string Directory of URL without trailing slash,
229      *                and without .phar file
230      */
231     protected function getCurrentUrlDir()
232     {
233         $url = $this->getCurrentUrl();
234         $url = preg_replace('/\?.*$/', '', $url);
235         if (substr($url, -1) != '/') {
236             $url = substr($url, 0, -strlen(basename($url)) - 1);
237         }
238         if (\Phar::running()) {
239             //remove .phar file name
240             $url = substr($url, 0, -strlen(basename($url)) - 1);
241         }
242
243         return $url;
244     }
245 }
246 ?>