/
class.page.php
executable file
·312 lines (285 loc) · 7.5 KB
/
class.page.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
<?php
/**
* @package toolkit
*/
/**
* Page is an abstract class that holds an object representation
* of a page's headers.
*/
Abstract Class Page {
/**
* Refers to the HTTP status code, 200 OK
*
* @since Symphony 2.3.2
* @var integer
*/
const HTTP_STATUS_OK = 200;
/**
* Refers to the HTTP status code, 301 Moved Permanently
*
* @since Symphony 2.3.2
* @var integer
*/
const HTTP_STATUS_MOVED_PERMANENT = 301;
/**
* Refers to the HTTP status code, 302 Found
* This is used as a temporary redirect
*
* @since Symphony 2.3.2
* @var integer
*/
const HTTP_STATUS_FOUND = 302;
/**
* Refers to the HTTP status code, 304 Not Modified
*
* @since Symphony 2.3.2
* @var integer
*/
const HTTP_NOT_MODIFIED = 304;
/**
* Refers to the HTTP status code, 400 Bad Request
*
* @since Symphony 2.3.2
* @var integer
*/
const HTTP_STATUS_BAD_REQUEST = 400;
/**
* Refers to the HTTP status code, 401 Unauthorized
*
* @since Symphony 2.3.2
* @var integer
*/
const HTTP_STATUS_UNAUTHORIZED = 401;
/**
* Refers to the HTTP status code, 403 Forbidden
*
* @since Symphony 2.3.2
* @var integer
*/
const HTTP_STATUS_FORBIDDEN = 403;
/**
* Refers to the HTTP status code, 404 Not Found
*
* @since Symphony 2.3.2
* @var integer
*/
const HTTP_STATUS_NOT_FOUND = 404;
/**
* Refers to the HTTP status code, 500 Internal Server Error
*
* @since Symphony 2.3.2
* @var integer
*/
const HTTP_STATUS_ERROR = 500;
/**
* Keyed array of all the string
*
* @since Symphony 2.3.2
* @var array
*/
public static $HTTP_STATUSES = array(
// 200
self::HTTP_STATUS_OK => 'OK',
// 300
self::HTTP_STATUS_MOVED_PERMANENT => 'Moved Permanently',
self::HTTP_STATUS_FOUND => 'Found',
self::HTTP_NOT_MODIFIED => 'Not Modified',
// 400
self::HTTP_STATUS_BAD_REQUEST => 'Bad Request',
self::HTTP_STATUS_UNAUTHORIZED => 'Unauthorized',
self::HTTP_STATUS_FORBIDDEN => 'Forbidden',
self::HTTP_STATUS_NOT_FOUND => 'Not Found',
// 500
self::HTTP_STATUS_ERROR => 'Internal Server Error',
);
/**
* The HTTP status code of the page using the `HTTP_STATUSES` constants
*
* @deprecated @since Symphony 2.3.2
* @see $this->setHttpStatus and self::$HTTP_STATUSES
*
* @var integer
*/
protected $_status = NULL;
/**
* This stores the headers that will be sent when this page is
* generated as an associative array of header=>value.
*
* @var array
*/
protected $_headers = array();
/**
* Initialises the Page object by setting the headers to empty
*/
public function __construct(){
$this->_headers = array();
}
/**
*
* This method returns the string HTTP Status value.
* If `$status_code` is null, it returns all the values
* currently registered.
*
* @link http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html
*
* @since Symphony 2.3.2
*
* @param integer $status_code (optional)
* The HTTP Status code to get the value for.
* @return array|string
* Returns string if the $status_code is not null. Array otherwise
*/
public static final function getHttpStatusValue($status_code=NULL) {
if (!$status_code) {
return self::$HTTP_STATUSES;
}
return self::$HTTP_STATUSES[$status_code];
}
/**
* This method format the provided `$status_code` to used
* php's `header()` function.
*
* @since Symphony 2.3.2
*
* @param integer $res_code
* The HTTP Status code to get the value for
* @return string
* The formatted HTTP Status string
*/
public static final function getHeaderStatusString($status_code) {
return sprintf("Status: %d %s", $status_code, self::getHttpStatusValue($status_code));
}
/**
* Sets the `$sting_value` for the specified `$status_code`.
* If `$sting_value` is null, the `$status_code` is removed from
* the array.
*
* This allow developers to register customs HTTP_STATUS into the
* static `Page::$HTTP_STATUSES` array and use `$page->setHttpStatus()`.
*
* @since Symphony 2.3.2
*
* @param integer $status_code
* The HTTP Status numeric code.
* @param string $string_value
* The HTTP Status string value.
*/
public static final function setHttpStatusValue($status_code, $string_value) {
if (!$string_value) {
unset(self::$HTTP_STATUSES[$status_code]);
}
else if (is_int($status_code) && $status_code >= 100 && $status_code < 600) {
self::$HTTP_STATUSES[$status_code] = $string_value;
}
else {
// Throw error ?
}
}
/**
* Adds a header to the $_headers array using the $name
* as the key.
*
* @param string $name
* The header name, eg. Content-Type.
* @param string $value (optional)
* The value for the header, eg. text/xml. Defaults to null.
* @param integer $response_code (optional)
* The HTTP response code that should be set by PHP with this header, eg. 200
*/
public function addHeaderToPage($name, $value = null, $response_code = null) {
$this->_headers[strtolower($name)] = array(
'header' => $name . (is_null($value) ? null : ":{$value}"),
'response_code' => $response_code
);
}
/**
* Removes a header from the $_headers array using the $name
* as the key.
*
* @param string $name
* The header name, eg. Expires.
*/
public function removeHeaderFromPage($name) {
unset($this->_headers[strtolower($name)]);
}
/**
* Shorthand for `addHeaderToPage` in order to set the
* HTTP Status header.
*
* @since Symphony 2.3.2
*
* @param integer $status_code
* The HTTP Status numeric value.
*/
public function setHttpStatus($status_code) {
$this->addHeaderToPage('Status', null, $status_code);
// Assure we clear the legacy value
$this->_status = null;
}
/**
* Gets the current HTTP Status.
* If none is set, it assumes HTTP_STATUS_OK
*
* @since Symphony 2.3.2
*
* @return integer
*/
public function getHttpStatusCode() {
// Legacy check
if ($this->_status != null) {
$this->setHttpStatus($this->_status);
}
if (isset($this->_headers['status'])) {
return $this->_headers['status']['response_code'];
}
return self::HTTP_STATUS_OK;
}
/**
* Accessor function for `$_headers`
*
* @return array
*/
public function headers(){
return $this->_headers;
}
/**
* This function calls `__renderHeaders()`.
*
* @see __renderHeaders()
*/
public function generate($page = null) {
$this->__renderHeaders();
}
/**
* This method calls php's `header()` function
* in order to set the HTTP status code properly on all platforms.
*
* @see https://github.com/symphonycms/symphony-2/issues/1558#issuecomment-10663716
*
* @param integer $status_code
*/
public static final function renderStatusCode($status_code) {
header(self::getHeaderStatusString($status_code), true, $status_code);
}
/**
* Iterates over the `$_headers` for this page
* and outputs them using PHP's header() function.
*/
protected function __renderHeaders(){
if(!is_array($this->_headers) || empty($this->_headers)) return;
// Legacy check
if ($this->_status != null) {
$this->setHttpStatus($this->_status);
}
foreach($this->_headers as $key => $value){
// If this is the http status
if($key == 'status' && isset($value['response_code'])) {
$res_code = intval($value['response_code']);
self::renderStatusCode($res_code);
}
else {
header($value['header']);
}
}
}
}