|
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
|
|
|
* This stores the headers that will be sent when this page is |
|
108
|
|
|
* generated as an associative array of header=>value. |
|
109
|
|
|
* |
|
110
|
|
|
* @var array |
|
111
|
|
|
*/ |
|
112
|
|
|
protected $_headers = array(); |
|
113
|
|
|
|
|
114
|
|
|
/** |
|
115
|
|
|
* Initialises the Page object by setting the headers to empty |
|
116
|
|
|
*/ |
|
117
|
|
|
public function __construct() |
|
118
|
|
|
{ |
|
119
|
|
|
$this->_headers = array(); |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
/** |
|
123
|
|
|
* |
|
124
|
|
|
* This method returns the string HTTP Status value. |
|
125
|
|
|
* If `$status_code` is null, it returns all the values |
|
126
|
|
|
* currently registered. |
|
127
|
|
|
* |
|
128
|
|
|
* @link http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html |
|
129
|
|
|
* |
|
130
|
|
|
* @since Symphony 2.3.2 |
|
131
|
|
|
* |
|
132
|
|
|
* @param integer $status_code (optional) |
|
133
|
|
|
* The HTTP Status code to get the value for. |
|
134
|
|
|
* @return array|string |
|
135
|
|
|
* Returns string if the $status_code is not null. Array otherwise |
|
136
|
|
|
*/ |
|
137
|
|
|
final public static function getHttpStatusValue($status_code = null) |
|
138
|
|
|
{ |
|
139
|
|
|
if (!$status_code) { |
|
|
|
|
|
|
140
|
|
|
return self::$HTTP_STATUSES; |
|
141
|
|
|
} |
|
142
|
|
|
|
|
143
|
|
|
return self::$HTTP_STATUSES[$status_code]; |
|
144
|
|
|
} |
|
145
|
|
|
|
|
146
|
|
|
/** |
|
147
|
|
|
* This method format the provided `$status_code` to used |
|
148
|
|
|
* php's `header()` function. |
|
149
|
|
|
* |
|
150
|
|
|
* @since Symphony 2.3.2 |
|
151
|
|
|
* |
|
152
|
|
|
* @param integer $status_code |
|
153
|
|
|
* The HTTP Status code to get the value for |
|
154
|
|
|
* @return string |
|
155
|
|
|
* The formatted HTTP Status string |
|
156
|
|
|
*/ |
|
157
|
|
|
final public static function getHeaderStatusString($status_code) |
|
158
|
|
|
{ |
|
159
|
|
|
return sprintf("Status: %d %s", $status_code, self::getHttpStatusValue($status_code)); |
|
|
|
|
|
|
160
|
|
|
} |
|
161
|
|
|
|
|
162
|
|
|
/** |
|
163
|
|
|
* Sets the `$sting_value` for the specified `$status_code`. |
|
164
|
|
|
* If `$sting_value` is null, the `$status_code` is removed from |
|
165
|
|
|
* the array. |
|
166
|
|
|
* |
|
167
|
|
|
* This allow developers to register customs HTTP_STATUS into the |
|
168
|
|
|
* static `Page::$HTTP_STATUSES` array and use `$page->setHttpStatus()`. |
|
169
|
|
|
* |
|
170
|
|
|
* @since Symphony 2.3.2 |
|
171
|
|
|
* |
|
172
|
|
|
* @param integer $status_code |
|
173
|
|
|
* The HTTP Status numeric code. |
|
174
|
|
|
* @param string $string_value |
|
175
|
|
|
* The HTTP Status string value. |
|
176
|
|
|
*/ |
|
177
|
|
|
final public static function setHttpStatusValue($status_code, $string_value) |
|
178
|
|
|
{ |
|
179
|
|
|
if (!$string_value) { |
|
180
|
|
|
unset(self::$HTTP_STATUSES[$status_code]); |
|
181
|
|
|
} elseif (is_int($status_code) && $status_code >= 100 && $status_code < 600) { |
|
182
|
|
|
self::$HTTP_STATUSES[$status_code] = $string_value; |
|
183
|
|
|
} else { |
|
184
|
|
|
// Throw error ? |
|
185
|
|
|
} |
|
186
|
|
|
} |
|
187
|
|
|
|
|
188
|
|
|
/** |
|
189
|
|
|
* Adds a header to the $_headers array using the $name |
|
190
|
|
|
* as the key. |
|
191
|
|
|
* |
|
192
|
|
|
* @param string $name |
|
193
|
|
|
* The header name, eg. Content-Type. |
|
194
|
|
|
* @param string $value (optional) |
|
195
|
|
|
* The value for the header, eg. text/xml. Defaults to null. |
|
196
|
|
|
* @param integer $response_code (optional) |
|
197
|
|
|
* The HTTP response code that should be set by PHP with this header, eg. 200 |
|
198
|
|
|
*/ |
|
199
|
|
|
public function addHeaderToPage($name, $value = null, $response_code = null) |
|
200
|
|
|
{ |
|
201
|
|
|
$this->_headers[strtolower($name)] = array( |
|
202
|
|
|
'header' => $name . (is_null($value) ? null : ":{$value}"), |
|
203
|
|
|
'response_code' => $response_code |
|
204
|
|
|
); |
|
205
|
|
|
} |
|
206
|
|
|
|
|
207
|
|
|
/** |
|
208
|
|
|
* Removes a header from the $_headers array using the $name |
|
209
|
|
|
* as the key. |
|
210
|
|
|
* |
|
211
|
|
|
* @param string $name |
|
212
|
|
|
* The header name, eg. Expires. |
|
213
|
|
|
*/ |
|
214
|
|
|
public function removeHeaderFromPage($name) |
|
215
|
|
|
{ |
|
216
|
|
|
unset($this->_headers[strtolower($name)]); |
|
217
|
|
|
} |
|
218
|
|
|
|
|
219
|
|
|
/** |
|
220
|
|
|
* Shorthand for `addHeaderToPage` in order to set the |
|
221
|
|
|
* HTTP Status header. |
|
222
|
|
|
* |
|
223
|
|
|
* @since Symphony 2.3.2 |
|
224
|
|
|
* |
|
225
|
|
|
* @param integer $status_code |
|
226
|
|
|
* The HTTP Status numeric value. |
|
227
|
|
|
*/ |
|
228
|
|
|
public function setHttpStatus($status_code) |
|
229
|
|
|
{ |
|
230
|
|
|
$this->addHeaderToPage('Status', null, $status_code); |
|
231
|
|
|
} |
|
232
|
|
|
|
|
233
|
|
|
/** |
|
234
|
|
|
* Gets the current HTTP Status. |
|
235
|
|
|
* If none is set, it assumes HTTP_STATUS_OK |
|
236
|
|
|
* |
|
237
|
|
|
* @since Symphony 2.3.2 |
|
238
|
|
|
* |
|
239
|
|
|
* @return integer |
|
240
|
|
|
*/ |
|
241
|
|
|
public function getHttpStatusCode() |
|
242
|
|
|
{ |
|
243
|
|
|
if (isset($this->_headers['status'])) { |
|
244
|
|
|
return $this->_headers['status']['response_code']; |
|
245
|
|
|
} |
|
246
|
|
|
|
|
247
|
|
|
return self::HTTP_STATUS_OK; |
|
248
|
|
|
} |
|
249
|
|
|
|
|
250
|
|
|
/** |
|
251
|
|
|
* Accessor function for `$_headers` |
|
252
|
|
|
* |
|
253
|
|
|
* @return array |
|
254
|
|
|
*/ |
|
255
|
|
|
public function headers() |
|
256
|
|
|
{ |
|
257
|
|
|
return $this->_headers; |
|
258
|
|
|
} |
|
259
|
|
|
|
|
260
|
|
|
/** |
|
261
|
|
|
* This function should generate the content to send to the client. |
|
262
|
|
|
* The base implementation returns an empty body. |
|
263
|
|
|
* This function calls `renderHeaders()`. |
|
264
|
|
|
* |
|
265
|
|
|
* @see renderHeaders() |
|
266
|
|
|
* @return string |
|
267
|
|
|
*/ |
|
268
|
|
|
public function generate($page = null) |
|
|
|
|
|
|
269
|
|
|
{ |
|
270
|
|
|
$this->renderHeaders(); |
|
271
|
|
|
return ''; |
|
272
|
|
|
} |
|
273
|
|
|
|
|
274
|
|
|
/** |
|
275
|
|
|
* This method calls php's `header()` function |
|
276
|
|
|
* in order to set the HTTP status code properly on all platforms. |
|
277
|
|
|
* |
|
278
|
|
|
* @see https://github.com/symphonycms/symphony-2/issues/1558#issuecomment-10663716 |
|
279
|
|
|
* |
|
280
|
|
|
* @param integer $status_code |
|
281
|
|
|
*/ |
|
282
|
|
|
final public static function renderStatusCode($status_code) |
|
283
|
|
|
{ |
|
284
|
|
|
header(self::getHeaderStatusString($status_code), true, $status_code); |
|
285
|
|
|
} |
|
286
|
|
|
|
|
287
|
|
|
/** |
|
288
|
|
|
* Iterates over the `$_headers` for this page |
|
289
|
|
|
* and outputs them using PHP's header() function. |
|
290
|
|
|
*/ |
|
291
|
|
|
protected function renderHeaders() |
|
292
|
|
|
{ |
|
293
|
|
|
if (!is_array($this->_headers) || empty($this->_headers)) { |
|
|
|
|
|
|
294
|
|
|
return; |
|
295
|
|
|
} |
|
296
|
|
|
|
|
297
|
|
|
foreach ($this->_headers as $key => $value) { |
|
298
|
|
|
// If this is the http status |
|
299
|
|
|
if ($key == 'status' && isset($value['response_code'])) { |
|
300
|
|
|
$res_code = intval($value['response_code']); |
|
301
|
|
|
self::renderStatusCode($res_code); |
|
302
|
|
|
} else { |
|
303
|
|
|
header($value['header']); |
|
304
|
|
|
} |
|
305
|
|
|
} |
|
306
|
|
|
} |
|
307
|
|
|
|
|
308
|
|
|
/** |
|
309
|
|
|
* This function will check to ensure that this post request is not larger than |
|
310
|
|
|
* what the server is set to handle. If it is, a notice is shown. |
|
311
|
|
|
* |
|
312
|
|
|
* @link https://github.com/symphonycms/symphony-2/issues/1187 |
|
313
|
|
|
* @since Symphony 2.5.2 |
|
314
|
|
|
*/ |
|
315
|
|
|
public function isRequestValid() |
|
316
|
|
|
{ |
|
317
|
|
|
$max_size = @ini_get('post_max_size'); |
|
318
|
|
|
if (!$max_size) { |
|
319
|
|
|
return true; |
|
320
|
|
|
} |
|
321
|
|
|
|
|
322
|
|
|
if (server_safe('REQUEST_METHOD') === 'POST' && (int)server_safe('CONTENT_LENGTH') > General::convertHumanFileSizeToBytes($max_size)) { |
|
323
|
|
|
return false; |
|
324
|
|
|
} |
|
325
|
|
|
|
|
326
|
|
|
return true; |
|
327
|
|
|
} |
|
328
|
|
|
} |
|
329
|
|
|
|
In PHP, under loose comparison (like
==, or!=, orswitchconditions), values of different types might be equal.For
integervalues, zero is a special case, in particular the following results might be unexpected: