Do not require phar extension
[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         $phar = false;
162         if (class_exists('\\Phar')) {
163             $pharFile = \Phar::running();
164             if ($pharFile != '') {
165                 $phar = true;
166             }
167         }
168         if ($phar) {
169             //remove phar:// from the path
170             $this->cfgFiles[] = substr($pharFile, 7) . '.config.php';
171         } else {
172             $this->cfgFiles[] = __DIR__ . '/../../data/phancap.config.php';
173         }
174
175         //TODO: add ~/.config/phancap.php
176
177         $this->cfgFiles[] = '/etc/phancap.php';
178     }
179
180     /**
181      * Load values of a configuration file into this class
182      *
183      * @param string $filename Configuration file (.php)
184      *
185      * @return void
186      */
187     protected function loadFile($filename)
188     {
189         include $filename;
190         $vars = get_defined_vars();
191         foreach ($vars as $k => $value) {
192             $this->$k = $value;
193         }
194     }
195
196     /**
197      * Check if the cache directory exists and is writable
198      *
199      * @return void
200      * @throws \Exception When something is not ok
201      */
202     public function setupCheck()
203     {
204         if (!is_dir($this->cacheDir)) {
205             throw new \Exception(
206                 'Cache directory does not exist: ' . $this->cacheDir
207             );
208         }
209         if (!is_writable($this->cacheDir)) {
210             throw new \Exception(
211                 'Cache directory is not writable: ' . $this->cacheDir
212             );
213         }
214     }
215
216     /**
217      * Returns the current URL, without fragmet.
218      *
219      * @return string Full URL
220      */
221     protected function getCurrentUrl()
222     {
223         if (!isset($_SERVER['REQUEST_SCHEME'])) {
224             $_SERVER['REQUEST_SCHEME'] = 'http';
225         }
226         return $_SERVER['REQUEST_SCHEME'] . '://'
227             . $_SERVER['HTTP_HOST']
228             . preg_replace('/#.*$/', '', $_SERVER['REQUEST_URI']);
229     }
230
231     /**
232      * Returns the current URL without the file name in the path
233      *
234      * @return string Directory of URL without trailing slash,
235      *                and without .phar file
236      */
237     protected function getCurrentUrlDir()
238     {
239         $url = $this->getCurrentUrl();
240         $url = preg_replace('/\?.*$/', '', $url);
241         if (substr($url, -1) != '/') {
242             $url = substr($url, 0, -strlen(basename($url)) - 1);
243         }
244         if (class_exists('\\Phar') && \Phar::running()) {
245             //remove .phar file name
246             $url = substr($url, 0, -strlen(basename($url)) - 1);
247         }
248
249         return $url;
250     }
251 }
252 ?>