1 | <?php |
||||
2 | /** |
||||
3 | * @package toolkit |
||||
4 | */ |
||||
5 | /** |
||||
6 | * Page is an abstract class that holds an object representation |
||||
7 | * of a page's headers. |
||||
8 | */ |
||||
9 | abstract class Page |
||||
10 | { |
||||
11 | /** |
||||
12 | * Refers to the HTTP status code, 200 OK |
||||
13 | * |
||||
14 | * @since Symphony 2.3.2 |
||||
15 | * @var integer |
||||
16 | */ |
||||
17 | const HTTP_STATUS_OK = 200; |
||||
18 | |||||
19 | /** |
||||
20 | * Refers to the HTTP status code, 301 Moved Permanently |
||||
21 | * |
||||
22 | * @since Symphony 2.3.2 |
||||
23 | * @var integer |
||||
24 | */ |
||||
25 | const HTTP_STATUS_MOVED_PERMANENT = 301; |
||||
26 | |||||
27 | /** |
||||
28 | * Refers to the HTTP status code, 302 Found |
||||
29 | * This is used as a temporary redirect |
||||
30 | * |
||||
31 | * @since Symphony 2.3.2 |
||||
32 | * @var integer |
||||
33 | */ |
||||
34 | const HTTP_STATUS_FOUND = 302; |
||||
35 | |||||
36 | /** |
||||
37 | * Refers to the HTTP status code, 304 Not Modified |
||||
38 | * |
||||
39 | * @since Symphony 2.3.2 |
||||
40 | * @var integer |
||||
41 | */ |
||||
42 | const HTTP_NOT_MODIFIED = 304; |
||||
43 | |||||
44 | /** |
||||
45 | * Refers to the HTTP status code, 400 Bad Request |
||||
46 | * |
||||
47 | * @since Symphony 2.3.2 |
||||
48 | * @var integer |
||||
49 | */ |
||||
50 | const HTTP_STATUS_BAD_REQUEST = 400; |
||||
51 | |||||
52 | /** |
||||
53 | * Refers to the HTTP status code, 401 Unauthorized |
||||
54 | * |
||||
55 | * @since Symphony 2.3.2 |
||||
56 | * @var integer |
||||
57 | */ |
||||
58 | const HTTP_STATUS_UNAUTHORIZED = 401; |
||||
59 | |||||
60 | /** |
||||
61 | * Refers to the HTTP status code, 403 Forbidden |
||||
62 | * |
||||
63 | * @since Symphony 2.3.2 |
||||
64 | * @var integer |
||||
65 | */ |
||||
66 | const HTTP_STATUS_FORBIDDEN = 403; |
||||
67 | |||||
68 | /** |
||||
69 | * Refers to the HTTP status code, 404 Not Found |
||||
70 | * |
||||
71 | * @since Symphony 2.3.2 |
||||
72 | * @var integer |
||||
73 | */ |
||||
74 | const HTTP_STATUS_NOT_FOUND = 404; |
||||
75 | |||||
76 | /** |
||||
77 | * Refers to the HTTP status code, 500 Internal Server Error |
||||
78 | * |
||||
79 | * @since Symphony 2.3.2 |
||||
80 | * @var integer |
||||
81 | */ |
||||
82 | const HTTP_STATUS_ERROR = 500; |
||||
83 | |||||
84 | /** |
||||
85 | * Keyed array of all the string |
||||
86 | * |
||||
87 | * @since Symphony 2.3.2 |
||||
88 | * @var array |
||||
89 | */ |
||||
90 | public static $HTTP_STATUSES = array( |
||||
91 | // 200 |
||||
92 | self::HTTP_STATUS_OK => 'OK', |
||||
93 | // 300 |
||||
94 | self::HTTP_STATUS_MOVED_PERMANENT => 'Moved Permanently', |
||||
95 | self::HTTP_STATUS_FOUND => 'Found', |
||||
96 | self::HTTP_NOT_MODIFIED => 'Not Modified', |
||||
97 | // 400 |
||||
98 | self::HTTP_STATUS_BAD_REQUEST => 'Bad Request', |
||||
99 | self::HTTP_STATUS_UNAUTHORIZED => 'Unauthorized', |
||||
100 | self::HTTP_STATUS_FORBIDDEN => 'Forbidden', |
||||
101 | self::HTTP_STATUS_NOT_FOUND => 'Not Found', |
||||
102 | // 500 |
||||
103 | self::HTTP_STATUS_ERROR => 'Internal Server Error', |
||||
104 | ); |
||||
105 | |||||
106 | /** |
||||
107 | * The HTTP status code of the page using the `HTTP_STATUSES` constants |
||||
108 | * |
||||
109 | * @deprecated Since Symphony 2.3.2, this has been deprecated. It will be |
||||
110 | * removed in Symphony 3.0 |
||||
111 | * @see $this->setHttpStatus and self::$HTTP_STATUSES |
||||
112 | * |
||||
113 | * @var integer |
||||
114 | */ |
||||
115 | protected $_status = null; |
||||
116 | |||||
117 | /** |
||||
118 | * This stores the headers that will be sent when this page is |
||||
119 | * generated as an associative array of header=>value. |
||||
120 | * |
||||
121 | * @var array |
||||
122 | */ |
||||
123 | protected $_headers = array(); |
||||
124 | |||||
125 | /** |
||||
126 | * Initialises the Page object by setting the headers to empty |
||||
127 | */ |
||||
128 | public function __construct() |
||||
129 | { |
||||
130 | $this->_headers = array(); |
||||
131 | } |
||||
132 | |||||
133 | /** |
||||
134 | * |
||||
135 | * This method returns the string HTTP Status value. |
||||
136 | * If `$status_code` is null, it returns all the values |
||||
137 | * currently registered. |
||||
138 | * |
||||
139 | * @link http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html |
||||
140 | * |
||||
141 | * @since Symphony 2.3.2 |
||||
142 | * |
||||
143 | * @param integer $status_code (optional) |
||||
144 | * The HTTP Status code to get the value for. |
||||
145 | * @return array|string |
||||
146 | * Returns string if the $status_code is not null. Array otherwise |
||||
147 | */ |
||||
148 | final public static function getHttpStatusValue($status_code = null) |
||||
0 ignored issues
–
show
Coding Style
introduced
by
![]() |
|||||
149 | { |
||||
150 | if (!$status_code) { |
||||
0 ignored issues
–
show
The expression
$status_code of type integer|null is loosely compared to false ; this is ambiguous if the integer can be 0. You might want to explicitly use === null instead.
In PHP, under loose comparison (like For 0 == false // true
0 == null // true
123 == false // false
123 == null // false
// It is often better to use strict comparison
0 === false // false
0 === null // false
![]() |
|||||
151 | return self::$HTTP_STATUSES; |
||||
152 | } |
||||
153 | |||||
154 | return self::$HTTP_STATUSES[$status_code]; |
||||
155 | } |
||||
156 | |||||
157 | /** |
||||
158 | * This method format the provided `$status_code` to used |
||||
159 | * php's `header()` function. |
||||
160 | * |
||||
161 | * @since Symphony 2.3.2 |
||||
162 | * |
||||
163 | * @param integer $status_code |
||||
164 | * The HTTP Status code to get the value for |
||||
165 | * @return string |
||||
166 | * The formatted HTTP Status string |
||||
167 | */ |
||||
168 | final public static function getHeaderStatusString($status_code) |
||||
169 | { |
||||
170 | return sprintf("Status: %d %s", $status_code, self::getHttpStatusValue($status_code)); |
||||
0 ignored issues
–
show
Coding Style
Comprehensibility
introduced
by
The string literal
Status: %d %s does not require double quotes, as per coding-style, please use single quotes.
PHP provides two ways to mark string literals. Either with single quotes String literals in single quotes on the other hand are evaluated very literally and the only two
characters that needs escaping in the literal are the single quote itself ( Double quoted string literals may contain other variables or more complex escape sequences. <?php
$singleQuoted = 'Value';
$doubleQuoted = "\tSingle is $singleQuoted";
print $doubleQuoted;
will print an indented: If your string literal does not contain variables or escape sequences, it should be defined using single quotes to make that fact clear. For more information on PHP string literals and available escape sequences see the PHP core documentation. ![]() It seems like
self::getHttpStatusValue($status_code) can also be of type array ; however, parameter $args of sprintf() does only seem to accept string , maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||
171 | } |
||||
172 | |||||
173 | /** |
||||
174 | * Sets the `$sting_value` for the specified `$status_code`. |
||||
175 | * If `$sting_value` is null, the `$status_code` is removed from |
||||
176 | * the array. |
||||
177 | * |
||||
178 | * This allow developers to register customs HTTP_STATUS into the |
||||
179 | * static `Page::$HTTP_STATUSES` array and use `$page->setHttpStatus()`. |
||||
180 | * |
||||
181 | * @since Symphony 2.3.2 |
||||
182 | * |
||||
183 | * @param integer $status_code |
||||
184 | * The HTTP Status numeric code. |
||||
185 | * @param string $string_value |
||||
186 | * The HTTP Status string value. |
||||
187 | */ |
||||
188 | final public static function setHttpStatusValue($status_code, $string_value) |
||||
189 | { |
||||
190 | if (!$string_value) { |
||||
191 | unset(self::$HTTP_STATUSES[$status_code]); |
||||
192 | } elseif (is_int($status_code) && $status_code >= 100 && $status_code < 600) { |
||||
193 | self::$HTTP_STATUSES[$status_code] = $string_value; |
||||
194 | } else { |
||||
195 | // Throw error ? |
||||
196 | } |
||||
197 | } |
||||
198 | |||||
199 | /** |
||||
200 | * Adds a header to the $_headers array using the $name |
||||
201 | * as the key. |
||||
202 | * |
||||
203 | * @param string $name |
||||
204 | * The header name, eg. Content-Type. |
||||
205 | * @param string $value (optional) |
||||
206 | * The value for the header, eg. text/xml. Defaults to null. |
||||
207 | * @param integer $response_code (optional) |
||||
208 | * The HTTP response code that should be set by PHP with this header, eg. 200 |
||||
209 | */ |
||||
210 | public function addHeaderToPage($name, $value = null, $response_code = null) |
||||
0 ignored issues
–
show
|
|||||
211 | { |
||||
212 | $this->_headers[strtolower($name)] = array( |
||||
213 | 'header' => $name . (is_null($value) ? null : ":{$value}"), |
||||
0 ignored issues
–
show
As per coding-style, please use concatenation or
sprintf for the variable $value instead of interpolation.
It is generally a best practice as it is often more readable to use concatenation instead of interpolation for variables inside strings. // Instead of
$x = "foo $bar $baz";
// Better use either
$x = "foo " . $bar . " " . $baz;
$x = sprintf("foo %s %s", $bar, $baz);
![]() |
|||||
214 | 'response_code' => $response_code |
||||
215 | ); |
||||
216 | } |
||||
217 | |||||
218 | /** |
||||
219 | * Removes a header from the $_headers array using the $name |
||||
220 | * as the key. |
||||
221 | * |
||||
222 | * @param string $name |
||||
223 | * The header name, eg. Expires. |
||||
224 | */ |
||||
225 | public function removeHeaderFromPage($name) |
||||
226 | { |
||||
227 | unset($this->_headers[strtolower($name)]); |
||||
228 | } |
||||
229 | |||||
230 | /** |
||||
231 | * Shorthand for `addHeaderToPage` in order to set the |
||||
232 | * HTTP Status header. |
||||
233 | * |
||||
234 | * @since Symphony 2.3.2 |
||||
235 | * |
||||
236 | * @param integer $status_code |
||||
237 | * The HTTP Status numeric value. |
||||
238 | */ |
||||
239 | public function setHttpStatus($status_code) |
||||
240 | { |
||||
241 | $this->addHeaderToPage('Status', null, $status_code); |
||||
242 | // Assure we clear the legacy value |
||||
243 | $this->_status = null; |
||||
0 ignored issues
–
show
The property
Page::$_status has been deprecated: Since Symphony 2.3.2, this has been deprecated. It will be removed in Symphony 3.0
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
This property has been deprecated. The supplier of the class has supplied an explanatory message. The explanatory message should give you some clue as to whether and when the property will be removed from the class and what other property to use instead. ![]() |
|||||
244 | } |
||||
245 | |||||
246 | /** |
||||
247 | * Gets the current HTTP Status. |
||||
248 | * If none is set, it assumes HTTP_STATUS_OK |
||||
249 | * |
||||
250 | * @since Symphony 2.3.2 |
||||
251 | * |
||||
252 | * @return integer |
||||
253 | */ |
||||
254 | public function getHttpStatusCode() |
||||
255 | { |
||||
256 | // Legacy check |
||||
257 | if ($this->_status != null) { |
||||
0 ignored issues
–
show
The property
Page::$_status has been deprecated: Since Symphony 2.3.2, this has been deprecated. It will be removed in Symphony 3.0
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
This property has been deprecated. The supplier of the class has supplied an explanatory message. The explanatory message should give you some clue as to whether and when the property will be removed from the class and what other property to use instead. ![]() |
|||||
258 | $this->setHttpStatus($this->_status); |
||||
0 ignored issues
–
show
The property
Page::$_status has been deprecated: Since Symphony 2.3.2, this has been deprecated. It will be removed in Symphony 3.0
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
This property has been deprecated. The supplier of the class has supplied an explanatory message. The explanatory message should give you some clue as to whether and when the property will be removed from the class and what other property to use instead. ![]() |
|||||
259 | } |
||||
260 | |||||
261 | if (isset($this->_headers['status'])) { |
||||
262 | return $this->_headers['status']['response_code']; |
||||
263 | } |
||||
264 | |||||
265 | return self::HTTP_STATUS_OK; |
||||
266 | } |
||||
267 | |||||
268 | /** |
||||
269 | * Accessor function for `$_headers` |
||||
270 | * |
||||
271 | * @return array |
||||
272 | */ |
||||
273 | public function headers() |
||||
274 | { |
||||
275 | return $this->_headers; |
||||
276 | } |
||||
277 | |||||
278 | /** |
||||
279 | * This function calls `__renderHeaders()`. |
||||
280 | * |
||||
281 | * @see __renderHeaders() |
||||
282 | */ |
||||
283 | public function generate($page = null) |
||||
0 ignored issues
–
show
The parameter
$page is not used and could be removed.
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
This check looks for parameters that have been defined for a function or method, but which are not used in the method body. ![]() |
|||||
284 | { |
||||
285 | $this->__renderHeaders(); |
||||
286 | } |
||||
287 | |||||
288 | /** |
||||
289 | * This method calls php's `header()` function |
||||
290 | * in order to set the HTTP status code properly on all platforms. |
||||
291 | * |
||||
292 | * @see https://github.com/symphonycms/symphony-2/issues/1558#issuecomment-10663716 |
||||
293 | * |
||||
294 | * @param integer $status_code |
||||
295 | */ |
||||
296 | final public static function renderStatusCode($status_code) |
||||
297 | { |
||||
298 | header(self::getHeaderStatusString($status_code), true, $status_code); |
||||
299 | } |
||||
300 | |||||
301 | /** |
||||
302 | * Iterates over the `$_headers` for this page |
||||
303 | * and outputs them using PHP's header() function. |
||||
304 | */ |
||||
305 | protected function __renderHeaders() |
||||
306 | { |
||||
307 | if (!is_array($this->_headers) || empty($this->_headers)) { |
||||
0 ignored issues
–
show
|
|||||
308 | return; |
||||
309 | } |
||||
310 | |||||
311 | // Legacy check |
||||
312 | if ($this->_status != null) { |
||||
0 ignored issues
–
show
The property
Page::$_status has been deprecated: Since Symphony 2.3.2, this has been deprecated. It will be removed in Symphony 3.0
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
This property has been deprecated. The supplier of the class has supplied an explanatory message. The explanatory message should give you some clue as to whether and when the property will be removed from the class and what other property to use instead. ![]() |
|||||
313 | $this->setHttpStatus($this->_status); |
||||
0 ignored issues
–
show
The property
Page::$_status has been deprecated: Since Symphony 2.3.2, this has been deprecated. It will be removed in Symphony 3.0
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
This property has been deprecated. The supplier of the class has supplied an explanatory message. The explanatory message should give you some clue as to whether and when the property will be removed from the class and what other property to use instead. ![]() |
|||||
314 | } |
||||
315 | |||||
316 | foreach ($this->_headers as $key => $value) { |
||||
317 | // If this is the http status |
||||
318 | if ($key == 'status' && isset($value['response_code'])) { |
||||
319 | $res_code = intval($value['response_code']); |
||||
320 | self::renderStatusCode($res_code); |
||||
321 | } else { |
||||
322 | header($value['header']); |
||||
323 | } |
||||
324 | } |
||||
325 | } |
||||
326 | |||||
327 | /** |
||||
328 | * This function will check to ensure that this post request is not larger than |
||||
329 | * what the server is set to handle. If it is, a notice is shown. |
||||
330 | * |
||||
331 | * @link https://github.com/symphonycms/symphony-2/issues/1187 |
||||
332 | * @since Symphony 2.5.2 |
||||
333 | */ |
||||
334 | public function isRequestValid() |
||||
335 | { |
||||
336 | $max_size = @ini_get('post_max_size'); |
||||
337 | if (!$max_size) { |
||||
338 | return true; |
||||
339 | } |
||||
340 | |||||
341 | if (server_safe('REQUEST_METHOD') === 'POST' && (int)server_safe('CONTENT_LENGTH') > General::convertHumanFileSizeToBytes($max_size)) { |
||||
342 | return false; |
||||
343 | } |
||||
344 | |||||
345 | return true; |
||||
346 | } |
||||
347 | } |
||||
348 |