These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php |
||
2 | /** |
||
3 | * Util |
||
4 | * |
||
5 | * @copyright Copyright (c) Gjero Krsteski (http://krsteski.de) |
||
6 | * @license http://opensource.org/licenses/MIT MIT License |
||
7 | */ |
||
8 | |||
9 | namespace Pimf\Util; |
||
10 | |||
11 | use Pimf\Config; |
||
12 | use Pimf\Sapi; |
||
13 | |||
14 | /** |
||
15 | * Manages a raw HTTP header sending. |
||
16 | * |
||
17 | * @package Util |
||
18 | * @author Gjero Krsteski <[email protected]> |
||
19 | */ |
||
20 | class Header extends Header\ContentType |
||
21 | { |
||
22 | /** |
||
23 | * @var string |
||
24 | */ |
||
25 | private static $userAgent; |
||
26 | |||
27 | /** |
||
28 | * @var string |
||
29 | */ |
||
30 | private static $IfModifiedSince; |
||
31 | |||
32 | /** |
||
33 | * @var string |
||
34 | */ |
||
35 | private static $IfNoneMatch; |
||
36 | |||
37 | /** |
||
38 | * @param string $userAgent |
||
39 | * @param string $IfModifiedSince |
||
40 | * @param string $IfNoneMatch |
||
41 | */ |
||
42 | public static function edge($userAgent, $IfModifiedSince, $IfNoneMatch) |
||
43 | { |
||
44 | self::$userAgent = $userAgent; |
||
45 | self::$IfModifiedSince = $IfModifiedSince; |
||
46 | self::$IfNoneMatch = $IfNoneMatch; |
||
47 | } |
||
48 | |||
49 | /** |
||
50 | * Removes previously set headers. |
||
51 | */ |
||
52 | public static function clear() |
||
53 | { |
||
54 | if (headers_sent() && !error_get_last()) { |
||
55 | foreach ((array)headers_list() as $header) { |
||
56 | header_remove($header); |
||
57 | } |
||
58 | } |
||
59 | } |
||
60 | |||
61 | /** |
||
62 | * @param string $url |
||
63 | * @param boolean $exit |
||
64 | */ |
||
65 | public static function toLocation($url, $exit = true) |
||
66 | { |
||
67 | header('Location: ' . $url); |
||
68 | if ($exit) { |
||
69 | exit(1); |
||
70 | } |
||
71 | } |
||
72 | |||
73 | /** |
||
74 | * @param int $code |
||
75 | * @param string $status |
||
76 | * @param boolean $exit |
||
77 | */ |
||
78 | protected static function view($code, $status, $exit = true) |
||
79 | { |
||
80 | if (Sapi::isCli()) { |
||
81 | echo $status . PHP_EOL; |
||
82 | if ($exit) { |
||
83 | exit; |
||
84 | } |
||
85 | } |
||
86 | |||
87 | self::send($code, $status); |
||
88 | |||
89 | $appTpl = str_replace('/', DS, BASE_PATH . 'app/' . Config::get('app.name') . '/_error/' . $code . '.php'); |
||
90 | $coreTpl = str_replace('/', DS, BASE_PATH . 'pimf-framework/core/Pimf/_error/' . $code . '.php'); |
||
91 | $coreTpl = str_replace(DS . 'pimf-framework' . DS . 'pimf-framework' . DS, DS . 'pimf-framework' . DS, |
||
92 | $coreTpl); |
||
93 | |||
94 | if (file_exists($appTpl) && is_readable($appTpl)) { |
||
95 | include $appTpl; |
||
96 | if ($exit) { |
||
97 | exit(1); |
||
98 | } |
||
99 | } |
||
100 | |||
101 | include $coreTpl; |
||
102 | if ($exit) { |
||
103 | exit(1); |
||
104 | } |
||
105 | } |
||
106 | |||
107 | /** |
||
108 | * @param string $msg |
||
109 | * @param boolean $exit |
||
110 | */ |
||
111 | public static function sendInternalServerError($msg = '', $exit = true) |
||
112 | { |
||
113 | self::view(500, $msg, $exit); |
||
114 | } |
||
115 | |||
116 | /** |
||
117 | * @param string $msg |
||
118 | * @param boolean $exit |
||
119 | */ |
||
120 | public static function sendNotFound($msg = '', $exit = true) |
||
121 | { |
||
122 | self::view(404, $msg, $exit); |
||
123 | } |
||
124 | |||
125 | /** |
||
126 | * Handles setting pages that are always to be revalidated for freshness by any cache. |
||
127 | * |
||
128 | * @param int $last_modified Timestamp in seconds |
||
129 | */ |
||
130 | public static function exitIfNotModifiedSince($last_modified) |
||
131 | { |
||
132 | if (self::isModified($last_modified)) { |
||
133 | self::sendNotModified(); |
||
134 | exit(0); |
||
135 | } |
||
136 | |||
137 | $last_modified = gmdate('D, d M Y H:i:s', $last_modified) . ' GMT'; |
||
138 | header("Cache-Control: must-revalidate"); |
||
139 | header("Last Modified: $last_modified"); |
||
140 | } |
||
141 | |||
142 | /** |
||
143 | * Actual HTTP caching validation. |
||
144 | * |
||
145 | * @param int $mtime In seconds |
||
146 | * @param string $etag |
||
147 | * |
||
148 | * @return bool |
||
149 | */ |
||
150 | public static function isModified($mtime, $etag = '') |
||
151 | { |
||
152 | $modifiedSince = strtotime(preg_replace('/;.*$/', '', self::$IfModifiedSince)); |
||
153 | |||
154 | return !($modifiedSince >= $mtime || self::$IfNoneMatch == $etag); |
||
155 | } |
||
156 | |||
157 | /** |
||
158 | * If you want to allow a page to be cached by shared proxies for one minute. |
||
159 | * |
||
160 | * @param int $seconds Interval in seconds |
||
161 | */ |
||
162 | View Code Duplication | public static function cacheNoValidate($seconds = 60) |
|
0 ignored issues
–
show
|
|||
163 | { |
||
164 | $now = time(); |
||
165 | $lmtime = gmdate('D, d M Y H:i:s', $now) . ' GMT'; |
||
166 | $extime = gmdate('D, d M Y H:i:s', $now + $seconds) . 'GMT'; |
||
167 | // backwards compatibility for HTTP/1.0 clients |
||
168 | header("Last Modified: $lmtime"); |
||
169 | header("Expires: $extime"); |
||
170 | // HTTP/1.1 support |
||
171 | header("Cache-Control: public,max-age=$seconds"); |
||
172 | } |
||
173 | |||
174 | /** |
||
175 | * If instead you have a page that has personalization on it |
||
176 | * (say, for example, the splash page contains local news as well), |
||
177 | * you can set a copy to be cached only by the browser. |
||
178 | * |
||
179 | * @param int $seconds Interval in seconds |
||
180 | */ |
||
181 | View Code Duplication | public static function cacheBrowser($seconds = 60) |
|
0 ignored issues
–
show
This method seems to be duplicated in your project.
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation. You can also find more detailed suggestions in the “Code” section of your repository.
Loading history...
|
|||
182 | { |
||
183 | $now = time(); |
||
184 | $lmtime = gmdate('D, d M Y H:i:s', $now) . ' GMT'; |
||
185 | $extime = gmdate('D, d M Y H:i:s', $now + $seconds) . ' GMT'; |
||
186 | // backwards compatibility for HTTP/1.0 clients |
||
187 | header("Last Modified: $lmtime"); |
||
188 | header("Expires: $extime"); |
||
189 | // HTTP/1.1 support |
||
190 | header("Cache-Control: private,max-age=$seconds,s-maxage=0"); |
||
191 | } |
||
192 | |||
193 | |||
194 | /** |
||
195 | * If you want to try as hard as possible to keep a page from being cached anywhere. |
||
196 | */ |
||
197 | public static function cacheNone() |
||
198 | { |
||
199 | // backwards compatibility for HTTP/1.0 clients |
||
200 | header("Expires: 0"); |
||
201 | header("Pragma: no-cache"); |
||
202 | // HTTP/1.1 support |
||
203 | header("Cache-Control: no-cache,no-store,max-age=0,s-maxage=0,must-revalidate"); |
||
204 | } |
||
205 | |||
206 | /** |
||
207 | * Sends file as download-header through any firewall to the browsers like >=IE6 >=FF3.6, Safari, Chrome, Opera. |
||
208 | * |
||
209 | * @link http://reeg.junetz.de/DSP/node16.html |
||
210 | * @link http://www.php.net/manual/de/function.header.php#88038 |
||
211 | * |
||
212 | * @param string $fileOrString |
||
213 | * @param string $fileName |
||
214 | * @param boolean $exit Optional for testing |
||
215 | */ |
||
216 | public static function sendDownloadDialog($fileOrString, $fileName, $exit = true) |
||
217 | { |
||
218 | $disposition = (false !== strpos(self::$userAgent, 'MSIE 5.5')) ? '' : 'attachment; '; |
||
219 | |||
220 | header("Pragma: public"); |
||
221 | header("Expires: 0"); |
||
222 | header("Cache-Control: must-revalidate, post-check=0, pre-check=0"); |
||
223 | header("Cache-Control: private", false); |
||
224 | header("Content-Disposition: " . $disposition . "filename=" . $fileName . ";"); |
||
225 | |||
226 | if (is_file($fileOrString)) { |
||
227 | readfile($fileOrString); |
||
228 | } else { |
||
229 | echo $fileOrString; |
||
230 | } |
||
231 | |||
232 | if ($exit) { |
||
233 | exit(0); |
||
234 | } |
||
235 | } |
||
236 | } |
||
237 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.