first working version
[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     public function __construct()
20     {
21         $this->cacheDir    = getcwd() . '/imgcache/';
22         $this->cacheDirUrl = $this->getCurrentUrlDir() . '/imgcache/';
23     }
24
25     public function setupCheck()
26     {
27         if (!is_dir($this->cacheDir)) {
28             throw new \Exception('Cache directory does not exist: ' . $this->cacheDir);
29         }
30         if (!is_writable($this->cacheDir)) {
31             throw new \Exception('Cache directory is not writable: ' . $this->cacheDir);
32         }
33     }
34
35     protected function getCurrentUrl()
36     {
37         if (!isset($_SERVER['REQUEST_SCHEME'])) {
38             $_SERVER['REQUEST_SCHEME'] = 'http';
39         }
40         return $_SERVER['REQUEST_SCHEME'] . '://'
41             . $_SERVER['HTTP_HOST']
42             . preg_replace('/#.*$/', '', $_SERVER['REQUEST_URI']);
43     }
44
45     protected function getCurrentUrlDir()
46     {
47         $url = $this->getCurrentUrl();
48         $url = preg_replace('/\?.*$/', '', $url);
49         if (substr($url, -1) == '/') {
50             return $url;
51         }
52
53         return substr($url, 0, -strlen(basename($url)) - 1);
54     }
55 }
56 ?>