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) |
|
|
|
|
149
|
|
|
{ |
150
|
|
|
if (!$status_code) { |
|
|
|
|
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)); |
|
|
|
|
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) |
|
|
|
|
211
|
|
|
{ |
212
|
|
|
$this->_headers[strtolower($name)] = array( |
213
|
|
|
'header' => $name . (is_null($value) ? null : ":{$value}"), |
|
|
|
|
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; |
|
|
|
|
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) { |
|
|
|
|
258
|
|
|
$this->setHttpStatus($this->_status); |
|
|
|
|
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) |
|
|
|
|
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)) { |
|
|
|
|
308
|
|
|
return; |
309
|
|
|
} |
310
|
|
|
|
311
|
|
|
// Legacy check |
312
|
|
|
if ($this->_status != null) { |
|
|
|
|
313
|
|
|
$this->setHttpStatus($this->_status); |
|
|
|
|
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
|
|
|
|