fix redirects when running as phar
[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      *
21      * Array of
22      *     username => secret key
23      * entries (used for signature).
24      *
25      * Boolean true to allow access in every case,
26      * false to completely disable it.
27      *
28      * @var array|boolean
29      */
30     public $access = true;
31
32     /**
33      * Redirect the browser to the cache URL.
34      * If disabled, the file is directly delivered.
35      *
36      * Helpful for debugging since it does not change the browser's URL.
37      *
38      * @var boolean
39      */
40     public $redirect = true;
41
42     /**
43      * How long requests with an old timestamp may be used.
44      * 2 days default.
45      *
46      * @var integer
47      */
48     public $timestampMaxAge = 'P2D';
49
50     /**
51      * Cache time of downloaded screenshots.
52      * When the file is as older than this, it gets re-created.
53      * The user can override that using the "smaxage" parameter.
54      *
55      * Defaults to 1 week.
56      *
57      * @var integer Lifetime in seconds
58      */
59     public $screenshotMaxAge = 'P1W';
60
61     /**
62      * Minimum age of a screeshot.
63      * A user cannot set the max age parameter below it.
64      *
65      * Defaults to 1 hour.
66      *
67      * @var integer Minimum lifetime in seconds
68      */
69     public $screenshotMinAge = 'PT1H';
70
71
72     public function __construct()
73     {
74         $this->cacheDir    = getcwd() . '/imgcache/';
75         $this->cacheDirUrl = $this->getCurrentUrlDir() . '/imgcache/';
76
77         $this->timestampMaxAge  = Options::validateAge($this->timestampMaxAge);
78         $this->screenshotMaxAge = Options::validateAge($this->screenshotMaxAge);
79         $this->screenshotMinAge = Options::validateAge($this->screenshotMinAge);
80     }
81
82     public function load()
83     {
84         $cfgFile = __DIR__ . '/../../data/phancap.config.php';
85         if (file_exists($cfgFile)) {
86             $this->loadFile($cfgFile);
87         }
88
89         $this->setupCheck();
90     }
91
92     protected function loadFile($filename)
93     {
94         include $filename;
95         $vars = get_defined_vars();
96         foreach ($vars as $k => $value) {
97             $this->$k = $value;
98         }
99     }
100
101     public function setupCheck()
102     {
103         if (!is_dir($this->cacheDir)) {
104             throw new \Exception('Cache directory does not exist: ' . $this->cacheDir);
105         }
106         if (!is_writable($this->cacheDir)) {
107             throw new \Exception('Cache directory is not writable: ' . $this->cacheDir);
108         }
109     }
110
111     protected function getCurrentUrl()
112     {
113         if (!isset($_SERVER['REQUEST_SCHEME'])) {
114             $_SERVER['REQUEST_SCHEME'] = 'http';
115         }
116         return $_SERVER['REQUEST_SCHEME'] . '://'
117             . $_SERVER['HTTP_HOST']
118             . preg_replace('/#.*$/', '', $_SERVER['REQUEST_URI']);
119     }
120
121     /**
122      * @return string Directory of URL without trailing slash,
123      *                and without .phar file
124      */
125     protected function getCurrentUrlDir()
126     {
127         $url = $this->getCurrentUrl();
128         $url = preg_replace('/\?.*$/', '', $url);
129         if (substr($url, -1) != '/') {
130             $url = substr($url, 0, -strlen(basename($url)) - 1);
131         }
132         if (\Phar::running()) {
133             //remove .phar file name
134             $url = substr($url, 0, -strlen(basename($url)) - 1);
135         }
136
137         return $url;
138     }
139 }
140 ?>