-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathRequest.php
executable file
·405 lines (365 loc) · 12.2 KB
/
Request.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
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
<?php
/**
* Slim - a micro PHP 5 framework
*
* @author Josh Lockhart <info@joshlockhart.com>
* @copyright 2011 Josh Lockhart
* @link http://www.slimframework.com
* @license http://www.slimframework.com/license
* @version 1.5.0
*
* MIT LICENSE
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
/**
* Request
*
* Object-oriented representation of an HTTP request. This class
* is responsible for parsing the raw HTTP request into a format
* usable by the Slim application.
*
* This class will automatically remove slashes from GET, POST, PUT,
* and Cookie data if magic quotes are enabled.
*
* @package Slim
* @author Josh Lockhart <info@joshlockhart.com>
* @author Kris Jordan <http://www.github.com/KrisJordan>
* @since Version 1.0
*/
class Slim_Http_Request {
const METHOD_HEAD = 'HEAD';
const METHOD_GET = 'GET';
const METHOD_POST = 'POST';
const METHOD_PUT = 'PUT';
const METHOD_DELETE = 'DELETE';
const METHOD_OPTIONS = 'OPTIONS';
const METHOD_OVERRIDE = '_METHOD';
/**
* @var string Request method (ie. "GET", "POST", "PUT", "DELETE", "HEAD")
*/
protected $method;
/**
* @var array Key-value array of HTTP request headers
*/
protected $headers;
/**
* @var array Names of additional headers to parse from the current
* HTTP request that are not prefixed with "HTTP_"
*/
protected $additionalHeaders = array('content-type', 'content-length', 'php-auth-user', 'php-auth-pw', 'auth-type', 'x-requested-with');
/**
* @var array Key-value array of cookies sent with the
* current HTTP request
*/
protected $cookies;
/**
* @var array Key-value array of HTTP GET parameters
*/
protected $get;
/**
* @var array Key-value array of HTTP POST parameters
*/
protected $post;
/**
* @var array Key-value array of HTTP PUT parameters
*/
protected $put;
/**
* @var string Raw body of HTTP request
*/
protected $body;
/**
* @var string Content type of HTTP request
*/
protected $contentType;
/**
* @var string Resource URI (ie. "/person/1")
*/
protected $resource;
/**
* @var string The root URI of the Slim application without trailing slash.
* This will be "" if the app is installed at the web
* document root. If the app is installed in a
* sub-directory "/foo", this will be "/foo".
*/
protected $root;
/**
* Constructor
*/
public function __construct() {
$this->method = isset($_SERVER['REQUEST_METHOD']) ? $_SERVER['REQUEST_METHOD'] : false;
$this->headers = $this->loadHttpHeaders();
$this->body = @file_get_contents('php://input');
$this->get = self::stripSlashesIfMagicQuotes($_GET);
$this->post = self::stripSlashesIfMagicQuotes($_POST);
$this->put = self::stripSlashesIfMagicQuotes($this->loadPutParameters());
$this->cookies = self::stripSlashesIfMagicQuotes($_COOKIE);
$this->root = Slim_Http_Uri::getBaseUri(true);
$this->resource = Slim_Http_Uri::getUri(true);
$this->checkForHttpMethodOverride();
}
/**
* Is this a GET request?
* @return bool
*/
public function isGet() {
return $this->method === self::METHOD_GET;
}
/**
* Is this a POST request?
* @return bool
*/
public function isPost() {
return $this->method === self::METHOD_POST;
}
/**
* Is this a PUT request?
* @return bool
*/
public function isPut() {
return $this->method === self::METHOD_PUT;
}
/**
* Is this a DELETE request?
* @return bool
*/
public function isDelete() {
return $this->method === self::METHOD_DELETE;
}
/**
* Is this a HEAD request?
* @return bool
*/
public function isHead() {
return $this->method === self::METHOD_HEAD;
}
/**
* Is this a OPTIONS request?
* @return bool
*/
public function isOptions() {
return $this->method === self::METHOD_OPTIONS;
}
/**
* Is this a XHR request?
* @return bool
*/
public function isAjax() {
return ( $this->params('isajax') || $this->headers('X_REQUESTED_WITH') === 'XMLHttpRequest' );
}
/**
* Fetch a PUT|POST|GET parameter value
*
* The preferred method to fetch the value of a
* PUT, POST, or GET parameter (searched in that order).
*
* @param string $key The paramter name
* @return string|null The value of parameter, or NULL if parameter not found
*/
public function params( $key ) {
foreach ( array('put', 'post', 'get') as $dataSource ) {
$source = $this->$dataSource;
if ( isset($source[(string)$key]) ) {
return $source[(string)$key];
}
}
return null;
}
/**
* Fetch GET parameter(s)
* @param string $key Name of parameter
* @return array|string|null All parameters, parameter value if $key
* and parameter exists, or NULL if $key
* and parameter does not exist.
*/
public function get( $key = null ) {
return $this->arrayOrArrayValue($this->get, $key);
}
/**
* Fetch POST parameter(s)
* @param string $key Name of parameter
* @return array|string|null All parameters, parameter value if $key
* and parameter exists, or NULL if $key
* and parameter does not exist.
*/
public function post( $key = null ) {
return $this->arrayOrArrayValue($this->post, $key);
}
/**
* Fetch PUT parameter(s)
* @param string $key Name of parameter
* @return array|string|null All parameters, parameter value if $key
* and parameter exists, or NULL if $key
* and parameter does not exist.
*/
public function put( $key = null ) {
return $this->arrayOrArrayValue($this->put, $key);
}
/**
* Fetch COOKIE value(s)
* @param string $key The cookie name
* @return array|string|null All parameters, parameter value if $key
* and parameter exists, or NULL if $key
* and parameter does not exist.
*/
public function cookies( $key = null ) {
return $this->arrayOrArrayValue($this->cookies, $key);
}
/**
* Get HTTP request header
* @param string $key The header name
* @return array|string|null All parameters, parameter value if $key
* and parameter exists, or NULL if $key
* and parameter does not exist.
*/
public function headers( $key = null ) {
return is_null($key) ? $this->headers : $this->arrayOrArrayValue($this->headers, $this->convertHttpHeaderName($key));
}
/**
* Get HTTP request body
* @return string|false String, or FALSE if body could not be read
*/
public function getBody() {
return $this->body;
}
/**
* Get HTTP method
* @return string
*/
public function getMethod() {
return $this->method;
}
/**
* Get HTTP request content type
* @return string
*/
public function getContentType() {
if ( !isset($this->contentType) ) {
$contentType = 'application/x-www-form-urlencoded';
$header = $this->headers('CONTENT_TYPE');
if ( !is_null($header) ) {
$headerParts = preg_split('/\s*;\s*/', $header);
$contentType = $headerParts[0];
}
$this->contentType = $contentType;
}
return $this->contentType;
}
/**
* Get HTTP request resource URI
* @return string
*/
public function getResourceUri() {
return $this->resource;
}
/**
* Get HTTP request root URI
* @return string
*/
public function getRootUri() {
return $this->root;
}
/**
* Fetch array or array value
* @param array $array
* @param string $key
* @return array|mixed Array if key is null, else array value
*/
protected function arrayOrArrayValue( array &$array, $key = null ) {
return is_null($key) ? $array : $this->arrayValueForKey($array, $key);
}
/**
* Fetch value from array
* @return mixed|null
*/
protected function arrayValueForKey( array &$array, $key ) {
return isset($array[(string)$key]) ? $array[(string)$key] : null;
}
/**
* Strip slashes from string or array of strings
* @param array|string $rawData
* @return array|string
*/
public static function stripSlashesIfMagicQuotes( $rawData ) {
if ( get_magic_quotes_gpc() ) {
return is_array($rawData) ? array_map(array('self', 'stripSlashesIfMagicQuotes'), $rawData) : stripslashes($rawData);
} else {
return $rawData;
}
}
/**
* Get PUT parameters
* @return array Key-value array of HTTP request PUT parameters
*/
protected function loadPutParameters() {
if ( $this->getContentType() === 'application/x-www-form-urlencoded' ) {
$input = is_string($this->body) ? $this->body : '';
if ( function_exists('mb_parse_str') ) {
mb_parse_str($input, $output);
} else {
parse_str($input, $output);
}
return $output;
} else {
return array();
}
}
/**
* Get HTTP request headers
* @return array Key-value array of HTTP request headers
*/
protected function loadHttpHeaders() {
$headers = array();
foreach ( $_SERVER as $key => $value ) {
$key = $this->convertHttpHeaderName($key);
if ( strpos($key, 'http-') === 0 || in_array($key, $this->additionalHeaders) ) {
$name = str_replace('http-', '', $key);
$headers[$name] = $value;
}
}
return $headers;
}
/**
* Convert HTTP header name
* @return string
*/
protected function convertHttpHeaderName( $name ) {
return str_replace('_', '-', strtolower($name));
}
/**
* Check for HTTP request method override
*
* Because traditional web browsers do not support PUT and DELETE
* HTTP methods, we use a hidden form input field to
* mimic PUT and DELETE requests. We check for this override here.
*
* @return void
*/
protected function checkForHttpMethodOverride() {
if ( isset($this->post[self::METHOD_OVERRIDE]) ) {
$this->method = $this->post[self::METHOD_OVERRIDE];
unset($this->post[self::METHOD_OVERRIDE]);
if ( $this->isPut() ) {
$this->put = $this->post;
}
}
}
}