b5194545e5ff9ccef0f91c906ec2c48db9b81e9e
[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     /**
115      * Initialize default values and loads configuration file paths
116      */
117     public function __construct()
118     {
119         $this->cacheDir    = getcwd() . '/imgcache/';
120         $this->cacheDirUrl = $this->getCurrentUrlDir() . '/imgcache/';
121
122         $this->timestampMaxAge  = Options::validateAge($this->timestampMaxAge);
123         $this->screenshotMaxAge = Options::validateAge($this->screenshotMaxAge);
124         $this->screenshotMinAge = Options::validateAge($this->screenshotMinAge);
125
126         $this->loadConfigFilePaths();
127     }
128
129     /**
130      * Load the first configuration file that exists
131      *
132      * @return void
133      * @uses   $cfgFileExists
134      */
135     public function load()
136     {
137         $this->cfgFileExists = false;
138         foreach ($this->cfgFiles as $file) {
139             if (file_exists($file)) {
140                 $this->cfgFileExists = true;
141                 $this->loadFile($file);
142                 break;
143             }
144         }
145
146         $this->setupCheck();
147     }
148
149     /**
150      * Load possible configuration file paths into $this->cfgFiles.
151      *
152      * @return void
153      */
154     protected function loadConfigFilePaths()
155     {
156         $pharFile = \Phar::running();
157         if ($pharFile == '') {
158             $this->cfgFiles[] = __DIR__ . '/../../data/phancap.config.php';
159         } else {
160             //remove phar:// from the path
161             $this->cfgFiles[] = substr($pharFile, 7) . '.config.php';
162         }
163
164         //TODO: add ~/.config/phancap.php
165
166         $this->cfgFiles[] = '/etc/phancap.php';
167     }
168
169     /**
170      * Load values of a configuration file into this class
171      *
172      * @param string $filename Configuration file (.php)
173      *
174      * @return void
175      */
176     protected function loadFile($filename)
177     {
178         include $filename;
179         $vars = get_defined_vars();
180         foreach ($vars as $k => $value) {
181             $this->$k = $value;
182         }
183     }
184
185     /**
186      * Check if the cache directory exists and is writable
187      *
188      * @return void
189      * @throws \Exception When something is not ok
190      */
191     public function setupCheck()
192     {
193         if (!is_dir($this->cacheDir)) {
194             throw new \Exception(
195                 'Cache directory does not exist: ' . $this->cacheDir
196             );
197         }
198         if (!is_writable($this->cacheDir)) {
199             throw new \Exception(
200                 'Cache directory is not writable: ' . $this->cacheDir
201             );
202         }
203     }
204
205     /**
206      * Returns the current URL, without fragmet.
207      *
208      * @return string Full URL
209      */
210     protected function getCurrentUrl()
211     {
212         if (!isset($_SERVER['REQUEST_SCHEME'])) {
213             $_SERVER['REQUEST_SCHEME'] = 'http';
214         }
215         return $_SERVER['REQUEST_SCHEME'] . '://'
216             . $_SERVER['HTTP_HOST']
217             . preg_replace('/#.*$/', '', $_SERVER['REQUEST_URI']);
218     }
219
220     /**
221      * Returns the current URL without the file name in the path
222      *
223      * @return string Directory of URL without trailing slash,
224      *                and without .phar file
225      */
226     protected function getCurrentUrlDir()
227     {
228         $url = $this->getCurrentUrl();
229         $url = preg_replace('/\?.*$/', '', $url);
230         if (substr($url, -1) != '/') {
231             $url = substr($url, 0, -strlen(basename($url)) - 1);
232         }
233         if (\Phar::running()) {
234             //remove .phar file name
235             $url = substr($url, 0, -strlen(basename($url)) - 1);
236         }
237
238         return $url;
239     }
240 }
241 ?>