@@ -64,6 +64,13 @@ class Request extends Message implements RequestInterface
64
64
*/
65
65
protected $ method ;
66
66
67
+ /**
68
+ * Stores values we've retrieved from
69
+ * PHP globals.
70
+ * @var array
71
+ */
72
+ protected $ globals = [];
73
+
67
74
//--------------------------------------------------------------------
68
75
69
76
/**
@@ -302,6 +309,23 @@ public function getEnv($index = null, $filter = null, $flags = null)
302
309
303
310
//--------------------------------------------------------------------
304
311
312
+ /**
313
+ * Allows manually setting the value of PHP global, like $_GET, $_POST, etc.
314
+ *
315
+ * @param string $method
316
+ * @param $value
317
+ *
318
+ * @return $this
319
+ */
320
+ public function setGlobal (string $ method , $ value )
321
+ {
322
+ $ this ->globals [$ method ] = $ value ;
323
+
324
+ return $ this ;
325
+ }
326
+
327
+ //--------------------------------------------------------------------
328
+
305
329
/**
306
330
* Fetches one or more items from a global, like cookies, get, post, etc.
307
331
* Can optionally filter the input when you retrieve it by passing in
@@ -312,45 +336,37 @@ public function getEnv($index = null, $filter = null, $flags = null)
312
336
*
313
337
* http://php.net/manual/en/filter.filters.sanitize.php
314
338
*
315
- * @param int $type Input filter constant
339
+ * @param int $method Input filter constant
316
340
* @param string|array $index
317
341
* @param int $filter Filter constant
318
342
* @param null $flags
319
343
*
320
344
* @return mixed
321
345
*/
322
- protected function fetchGlobal ($ type , $ index = null , $ filter = null , $ flags = null )
346
+ public function fetchGlobal ($ method , $ index = null , $ filter = null , $ flags = null )
323
347
{
324
- // Null filters cause null values to return.
325
- if (is_null ($ filter ))
348
+ $ method = strtolower ($ method );
349
+
350
+ if (! isset ($ this ->globals [$ method ]))
326
351
{
327
- $ filter = FILTER_DEFAULT ;
352
+ $ this -> populateGlobals ( $ method ) ;
328
353
}
329
354
330
- $ loopThrough = [];
331
- switch ( $ type )
355
+ // Null filters cause null values to return.
356
+ if ( is_null ( $ filter ) )
332
357
{
333
- case INPUT_GET : $ loopThrough = $ _GET ;
334
- break ;
335
- case INPUT_POST : $ loopThrough = $ _POST ;
336
- break ;
337
- case INPUT_COOKIE : $ loopThrough = $ _COOKIE ;
338
- break ;
339
- case INPUT_SERVER : $ loopThrough = $ _SERVER ;
340
- break ;
341
- case INPUT_ENV : $ loopThrough = $ _ENV ;
342
- break ;
343
- case INPUT_REQUEST : $ loopThrough = $ _REQUEST ;
344
- break ;
358
+ $ filter = FILTER_DEFAULT ;
345
359
}
346
360
347
- // If $index is null, it means that the whole input type array is requested
361
+ // Return all values when $index is null
348
362
if (is_null ($ index ))
349
363
{
350
364
$ values = [];
351
- foreach ($ loopThrough as $ key => $ value )
365
+ foreach ($ this -> globals [ $ method ] as $ key => $ value )
352
366
{
353
- $ values [$ key ] = is_array ($ value ) ? $ this ->fetchGlobal ($ type , $ key , $ filter , $ flags ) : filter_var ($ value , $ filter , $ flags );
367
+ $ values [$ key ] = is_array ($ value )
368
+ ? $ this ->fetchGlobal ($ method , $ key , $ filter , $ flags )
369
+ : filter_var ($ value , $ filter , $ flags );
354
370
}
355
371
356
372
return $ values ;
@@ -363,7 +379,7 @@ protected function fetchGlobal($type, $index = null, $filter = null, $flags = nu
363
379
364
380
foreach ($ index as $ key )
365
381
{
366
- $ output [$ key ] = $ this ->fetchGlobal ($ type , $ key , $ filter , $ flags );
382
+ $ output [$ key ] = $ this ->fetchGlobal ($ method , $ key , $ filter , $ flags );
367
383
}
368
384
369
385
return $ output ;
@@ -372,7 +388,7 @@ protected function fetchGlobal($type, $index = null, $filter = null, $flags = nu
372
388
// Does the index contain array notation?
373
389
if (($ count = preg_match_all ('/(?:^[^\[]+)|\[[^]]*\]/ ' , $ index , $ matches )) > 1 )
374
390
{
375
- $ value = $ loopThrough ;
391
+ $ value = $ this -> globals [ $ method ] ;
376
392
for ($ i = 0 ; $ i < $ count ; $ i ++)
377
393
{
378
394
$ key = trim ($ matches [0 ][$ i ], '[] ' );
@@ -393,14 +409,12 @@ protected function fetchGlobal($type, $index = null, $filter = null, $flags = nu
393
409
}
394
410
}
395
411
396
- // Due to issues with FastCGI and testing,
397
- // we need to do these all manually instead
398
- // of the simpler filter_input();
399
412
if (empty ($ value ))
400
413
{
401
- $ value = $ loopThrough [$ index ] ?? null ;
414
+ $ value = $ this -> globals [ $ method ] [$ index ] ?? null ;
402
415
}
403
416
417
+ // Cannot filter these types of data automatically...
404
418
if (is_array ($ value ) || is_object ($ value ) || is_null ($ value ))
405
419
{
406
420
return $ value ;
@@ -410,4 +424,39 @@ protected function fetchGlobal($type, $index = null, $filter = null, $flags = nu
410
424
}
411
425
412
426
//--------------------------------------------------------------------
427
+
428
+ /**
429
+ * Saves a copy of the current state of one of several PHP globals
430
+ * so we can retrieve them later.
431
+ *
432
+ * @param string $method
433
+ */
434
+ protected function populateGlobals (string $ method )
435
+ {
436
+ if (! isset ($ this ->globals [$ method ]))
437
+ {
438
+ $ this ->globals [$ method ] = [];
439
+ }
440
+
441
+ // Don't populate ENV as it might contain
442
+ // sensitive data that we don't want to get logged.
443
+ switch ($ method )
444
+ {
445
+ case 'get ' :
446
+ $ this ->globals ['get ' ] = $ _GET ;
447
+ break ;
448
+ case 'post ' :
449
+ $ this ->globals ['post ' ] = $ _POST ;
450
+ break ;
451
+ case 'request ' :
452
+ $ this ->globals ['request ' ] = $ _REQUEST ;
453
+ break ;
454
+ case 'cookie ' :
455
+ $ this ->globals ['cookie ' ] = $ _COOKIE ;
456
+ break ;
457
+ case 'server ' :
458
+ $ this ->globals ['server ' ] = $ _SERVER ;
459
+ break ;
460
+ }
461
+ }
413
462
}
0 commit comments