-
Notifications
You must be signed in to change notification settings - Fork 287
Expand file tree
/
Copy pathRequest.php
More file actions
529 lines (472 loc) · 12.5 KB
/
Request.php
File metadata and controls
529 lines (472 loc) · 12.5 KB
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
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
<?php
/**
* Klein (klein.php) - A lightning fast router for PHP
*
* @author Chris O'Hara <cohara87@gmail.com>
* @author Trevor Suarez (Rican7) (contributor and v2 refactorer)
* @copyright (c) Chris O'Hara
* @link https://github.com/chriso/klein.php
* @license MIT
*/
namespace Klein;
use \Klein\DataCollection\DataCollection;
use \Klein\DataCollection\ServerDataCollection;
use \Klein\DataCollection\HeaderDataCollection;
/**
* Request
*
* @package Klein
*/
class Request
{
/**
* Class properties
*/
/**
* Unique identifier for the request
*
* @var string
* @access protected
*/
protected $id;
/**
* GET (query) parameters
*
* @var \Klein\DataCollection\DataCollection
* @access protected
*/
protected $params_get;
/**
* POST parameters
*
* @var \Klein\DataCollection\DataCollection
* @access protected
*/
protected $params_post;
/**
* Named parameters
*
* @var \Klein\DataCollection\DataCollection
* @access protected
*/
protected $params_named;
/**
* Client cookie data
*
* @var \Klein\DataCollection\DataCollection
* @access protected
*/
protected $cookies;
/**
* Server created attributes
*
* @var \Klein\DataCollection\ServerDataCollection
* @access protected
*/
protected $server;
/**
* HTTP request headers
*
* @var \Klein\DataCollection\HeaderDataCollection
* @access protected
*/
protected $headers;
/**
* Uploaded temporary files
*
* @var \Klein\DataCollection\DataCollection
* @access protected
*/
protected $files;
/**
* The request body
*
* @var string
* @access protected
*/
protected $body;
/**
* Methods
*/
/**
* Constructor
*
* Create a new Request object and define all of its request data
*
* @param array $params_get
* @param array $params_post
* @param array $cookies
* @param array $server
* @param array $files
* @param string $body
* @access public
*/
public function __construct(
array $params_get = array(),
array $params_post = array(),
array $cookies = array(),
array $server = array(),
array $files = array(),
$body = null
) {
// Assignment city...
$this->params_get = new DataCollection($params_get);
$this->params_post = new DataCollection($params_post);
$this->cookies = new DataCollection($cookies);
$this->server = new ServerDataCollection($server);
$this->headers = new HeaderDataCollection($this->server->getHeaders());
$this->files = new DataCollection($files);
$this->body = $body ? (string) $body : null;
// Non-injected assignments
$this->params_named = new DataCollection();
}
/**
* Create a new request object using the built-in "superglobals"
*
* @link http://php.net/manual/en/language.variables.superglobals.php
* @static
* @access public
* @return Request
*/
public static function createFromGlobals()
{
// Create and return a new instance of this
return new static(
$_GET,
$_POST,
$_COOKIE,
$_SERVER,
$_FILES,
null // Let our content getter take care of the "body"
);
}
/**
* Gets a unique ID for the request
*
* Generates one on the first call
*
* @param boolean $hash Whether or not to hash the ID on creation
* @access public
* @return string
*/
public function id($hash = true)
{
if (null === $this->id) {
$this->id = uniqid();
if ($hash) {
$this->id = sha1($this->id);
}
}
return $this->id;
}
/**
* Returns the GET parameters collection
*
* @access public
* @return \Klein\DataCollection\DataCollection
*/
public function paramsGet()
{
return $this->params_get;
}
/**
* Returns the POST parameters collection
*
* @access public
* @return \Klein\DataCollection\DataCollection
*/
public function paramsPost()
{
return $this->params_post;
}
/**
* Returns the named parameters collection
*
* @access public
* @return \Klein\DataCollection\DataCollection
*/
public function paramsNamed()
{
return $this->params_named;
}
/**
* Returns the cookies collection
*
* @access public
* @return \Klein\DataCollection\DataCollection
*/
public function cookies()
{
return $this->cookies;
}
/**
* Returns the server collection
*
* @access public
* @return \Klein\DataCollection\DataCollection
*/
public function server()
{
return $this->server;
}
/**
* Returns the headers collection
*
* @access public
* @return \Klein\DataCollection\HeaderDataCollection
*/
public function headers()
{
return $this->headers;
}
/**
* Returns the files collection
*
* @access public
* @return \Klein\DataCollection\DataCollection
*/
public function files()
{
return $this->files;
}
/**
* Gets the request body
*
* @access public
* @return string
*/
public function body()
{
// Only get it once
if (null === $this->body) {
$this->body = @file_get_contents('php://input');
}
return $this->body;
}
/**
* Returns all parameters (GET, POST, named, and cookies) that match the mask
*
* Takes an optional mask param that contains the names of any params
* you'd like this method to exclude in the returned array
*
* @see \Klein\DataCollection\DataCollection::all()
* @param array $mask The parameter mask array
* @param boolean $fill_with_nulls Whether or not to fill the returned array
* with null values to match the given mask
* @access public
* @return array
*/
public function params($mask = null, $fill_with_nulls = true)
{
/*
* Make sure that each key in the mask has at least a
* null value, since the user will expect the key to exist
*/
if (null !== $mask && $fill_with_nulls) {
$attributes = array_fill_keys($mask, null);
} else {
$attributes = array();
}
// Merge our params in the get, post, cookies, named order
return array_merge(
$attributes,
$this->params_get->all($mask, false),
$this->params_post->all($mask, false),
$this->cookies->all($mask, false),
$this->params_named->all($mask, false) // Add our named params last
);
}
/**
* Return a request parameter, or $default if it doesn't exist
*
* @param string $key The name of the parameter to return
* @param mixed $default The default value of the parameter if it contains no value
* @access public
* @return string
*/
public function param($key, $default = null)
{
// Get all of our request params
$params = $this->params();
return isset($params[$key]) ? $params[$key] : $default;
}
/**
* Magic "__isset" method
*
* Allows the ability to arbitrarily check the existence of a parameter
* from this instance while treating it as an instance property
*
* @param string $param The name of the parameter
* @access public
* @return boolean
*/
public function __isset($param)
{
// Get all of our request params
$params = $this->params();
return isset($params[$param]);
}
/**
* Magic "__get" method
*
* Allows the ability to arbitrarily request a parameter from this instance
* while treating it as an instance property
*
* @param string $param The name of the parameter
* @access public
* @return string
*/
public function __get($param)
{
return $this->param($param);
}
/**
* Magic "__set" method
*
* Allows the ability to arbitrarily set a parameter from this instance
* while treating it as an instance property
*
* NOTE: This currently sets the "named" parameters, since that's the
* one collection that we have the most sane control over
*
* @param string $param The name of the parameter
* @param mixed $value The value of the parameter
* @access public
* @return void
*/
public function __set($param, $value)
{
$this->params_named->set($param, $value);
}
/**
* Magic "__unset" method
*
* Allows the ability to arbitrarily remove a parameter from this instance
* while treating it as an instance property
*
* @param string $param The name of the parameter
* @access public
* @return void
*/
public function __unset($param)
{
$this->params_named->remove($param);
}
/**
* Is the request secure?
*
* @access public
* @return boolean
*/
public function isSecure()
{
return ($this->server->get('HTTPS') == true);
}
/**
* Gets the request IP address
*
* @access public
* @return string
*/
public function ip()
{
return $this->server->get('REMOTE_ADDR');
}
/**
* Gets the request user agent
*
* @access public
* @return string
*/
public function userAgent()
{
return $this->headers->get('USER_AGENT');
}
/**
* Gets the request URI
*
* @access public
* @return string
*/
public function uri()
{
$p = $this->server->get('PATH_INFO');
if (!$p) {
$p = $this->server->get('REQUEST_URI', '/');
}
return $p;
}
/**
* Get the request's pathname
*
* @access public
* @return string
*/
public function pathname()
{
$uri = $this->uri();
// Strip the query string from the URI
$uri = strstr($uri, '?', true) ?: $uri;
return $uri;
}
/**
* Gets the request method, or checks it against $is
*
* <code>
* // POST request example
* $request->method() // returns 'POST'
* $request->method('post') // returns true
* $request->method('get') // returns false
* </code>
*
* @param string $is The method to check the current request method against
* @param boolean $allow_override Whether or not to allow HTTP method overriding via header or params
* @access public
* @return string | boolean
*/
public function method($is = null, $allow_override = true)
{
$method = $this->server->get('REQUEST_METHOD', 'GET');
// Override
if ($allow_override && $method === 'POST') {
// For legacy servers, override the HTTP method with the X-HTTP-Method-Override header or _method parameter
if ($this->server->exists('X_HTTP_METHOD_OVERRIDE')) {
$method = $this->server->get('X_HTTP_METHOD_OVERRIDE', $method);
} else {
$method = $this->param('_method', $method);
}
$method = strtoupper($method);
}
// We're doing a check
if (null !== $is) {
return strcasecmp($method, $is) === 0;
}
return $method;
}
/**
* Adds to or modifies the current query string
*
* @param string $key The name of the query param
* @param mixed $value The value of the query param
* @access public
* @return string
*/
public function query($key, $value = null)
{
$query = array();
parse_str(
$this->server()->get('QUERY_STRING'),
$query
);
if (is_array($key)) {
$query = array_merge($query, $key);
} else {
$query[$key] = $value;
}
$request_uri = $this->uri();
if (strpos($request_uri, '?') !== false) {
$request_uri = strstr($request_uri, '?', true);
}
return $request_uri . (!empty($query) ? '?' . http_build_query($query) : null);
}
}