implement authentication
[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      * Credentials for access
20      * username => secret key (used for signature)
21      * @var array
22      */
23     public $access = false;
24
25     /**
26      * How long requests with an old timestamp may be used.
27      * 2 days default.
28      *
29      * @var integer
30      */
31     public $timestampLifetime = 172800;
32
33
34     public function __construct()
35     {
36         $this->cacheDir    = getcwd() . '/imgcache/';
37         $this->cacheDirUrl = $this->getCurrentUrlDir() . '/imgcache/';
38     }
39
40     public function load()
41     {
42         $cfgFile = __DIR__ . '/../../data/phancap.config.php';
43         if (file_exists($cfgFile)) {
44             $this->loadFile($cfgFile);
45         }
46
47         $this->setupCheck();
48     }
49
50     protected function loadFile($filename)
51     {
52         include $filename;
53         $vars = get_defined_vars();
54         foreach ($vars as $k => $value) {
55             $this->$k = $value;
56         }
57     }
58
59     public function setupCheck()
60     {
61         if (!is_dir($this->cacheDir)) {
62             throw new \Exception('Cache directory does not exist: ' . $this->cacheDir);
63         }
64         if (!is_writable($this->cacheDir)) {
65             throw new \Exception('Cache directory is not writable: ' . $this->cacheDir);
66         }
67     }
68
69     protected function getCurrentUrl()
70     {
71         if (!isset($_SERVER['REQUEST_SCHEME'])) {
72             $_SERVER['REQUEST_SCHEME'] = 'http';
73         }
74         return $_SERVER['REQUEST_SCHEME'] . '://'
75             . $_SERVER['HTTP_HOST']
76             . preg_replace('/#.*$/', '', $_SERVER['REQUEST_URI']);
77     }
78
79     protected function getCurrentUrlDir()
80     {
81         $url = $this->getCurrentUrl();
82         $url = preg_replace('/\?.*$/', '', $url);
83         if (substr($url, -1) == '/') {
84             return $url;
85         }
86
87         return substr($url, 0, -strlen(basename($url)) - 1);
88     }
89 }
90 ?>