69b3f0acf20e4ecb74c3823c03092b90d6fff548
[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      * Redirect the browser to the cache URL.
47      * If disabled, the file is directly delivered.
48      *
49      * Helpful for debugging since it does not change the browser's URL.
50      *
51      * @var boolean
52      */
53     public $redirect = true;
54
55     /**
56      * How long requests with an old timestamp may be used.
57      * 2 days default.
58      *
59      * @var integer
60      */
61     public $timestampMaxAge = 'P2D';
62
63     /**
64      * Cache time of downloaded screenshots.
65      * When the file is as older than this, it gets re-created.
66      * The user can override that using the "smaxage" parameter.
67      *
68      * Defaults to 1 week.
69      *
70      * @var integer Lifetime in seconds
71      */
72     public $screenshotMaxAge = 'P1W';
73
74     /**
75      * Minimum age of a screeshot.
76      * A user cannot set the max age parameter below it.
77      *
78      * Defaults to 1 hour.
79      *
80      * @var integer Minimum lifetime in seconds
81      */
82     public $screenshotMinAge = 'PT1H';
83
84
85     public function __construct()
86     {
87         $this->cacheDir    = getcwd() . '/imgcache/';
88         $this->cacheDirUrl = $this->getCurrentUrlDir() . '/imgcache/';
89
90         $this->timestampMaxAge  = Options::validateAge($this->timestampMaxAge);
91         $this->screenshotMaxAge = Options::validateAge($this->screenshotMaxAge);
92         $this->screenshotMinAge = Options::validateAge($this->screenshotMinAge);
93
94         $this->loadConfigFilePaths();
95     }
96
97     public function load()
98     {
99         $this->cfgFileExists = false;
100         foreach ($this->cfgFiles as $file) {
101             if (file_exists($file)) {
102                 $this->cfgFileExists = true;
103                 $this->loadFile($file);
104                 break;
105             }
106         }
107
108         $this->setupCheck();
109     }
110
111     /**
112      * Load possible configuration file paths into $this->cfgFiles.
113      *
114      * @return void
115      */
116     protected function loadConfigFilePaths()
117     {
118         $pharFile = \Phar::running();
119         if ($pharFile == '') {
120             $this->cfgFiles[] = __DIR__ . '/../../data/phancap.config.php';
121         } else {
122             //remove phar:// from the path
123             $this->cfgFiles[] = substr($pharFile, 7) . '.config.php';
124         }
125
126         //TODO: add ~/.config/phancap.php
127
128         $this->cfgFiles[] = '/etc/phancap.php';
129     }
130
131     protected function loadFile($filename)
132     {
133         include $filename;
134         $vars = get_defined_vars();
135         foreach ($vars as $k => $value) {
136             $this->$k = $value;
137         }
138     }
139
140     public function setupCheck()
141     {
142         if (!is_dir($this->cacheDir)) {
143             throw new \Exception('Cache directory does not exist: ' . $this->cacheDir);
144         }
145         if (!is_writable($this->cacheDir)) {
146             throw new \Exception('Cache directory is not writable: ' . $this->cacheDir);
147         }
148     }
149
150     protected function getCurrentUrl()
151     {
152         if (!isset($_SERVER['REQUEST_SCHEME'])) {
153             $_SERVER['REQUEST_SCHEME'] = 'http';
154         }
155         return $_SERVER['REQUEST_SCHEME'] . '://'
156             . $_SERVER['HTTP_HOST']
157             . preg_replace('/#.*$/', '', $_SERVER['REQUEST_URI']);
158     }
159
160     /**
161      * @return string Directory of URL without trailing slash,
162      *                and without .phar file
163      */
164     protected function getCurrentUrlDir()
165     {
166         $url = $this->getCurrentUrl();
167         $url = preg_replace('/\?.*$/', '', $url);
168         if (substr($url, -1) != '/') {
169             $url = substr($url, 0, -strlen(basename($url)) - 1);
170         }
171         if (\Phar::running()) {
172             //remove .phar file name
173             $url = substr($url, 0, -strlen(basename($url)) - 1);
174         }
175
176         return $url;
177     }
178 }
179 ?>