@@ -64,6 +64,13 @@ class Request extends Message implements RequestInterface
6464 */
6565 protected $ method ;
6666
67+ /**
68+ * Stores values we've retrieved from
69+ * PHP globals.
70+ * @var array
71+ */
72+ protected $ globals = [];
73+
6774 //--------------------------------------------------------------------
6875
6976 /**
@@ -302,6 +309,23 @@ public function getEnv($index = null, $filter = null, $flags = null)
302309
303310 //--------------------------------------------------------------------
304311
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+
305329 /**
306330 * Fetches one or more items from a global, like cookies, get, post, etc.
307331 * 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)
312336 *
313337 * http://php.net/manual/en/filter.filters.sanitize.php
314338 *
315- * @param int $type Input filter constant
339+ * @param int $method Input filter constant
316340 * @param string|array $index
317341 * @param int $filter Filter constant
318342 * @param null $flags
319343 *
320344 * @return mixed
321345 */
322- protected function fetchGlobal ($ type , $ index = null , $ filter = null , $ flags = null )
346+ public function fetchGlobal ($ method , $ index = null , $ filter = null , $ flags = null )
323347 {
324- // Null filters cause null values to return.
325- if (is_null ($ filter ))
348+ $ method = strtolower ($ method );
349+
350+ if (! isset ($ this ->globals [$ method ]))
326351 {
327- $ filter = FILTER_DEFAULT ;
352+ $ this -> populateGlobals ( $ method ) ;
328353 }
329354
330- $ loopThrough = [];
331- switch ( $ type )
355+ // Null filters cause null values to return.
356+ if ( is_null ( $ filter ) )
332357 {
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 ;
345359 }
346360
347- // If $index is null, it means that the whole input type array is requested
361+ // Return all values when $index is null
348362 if (is_null ($ index ))
349363 {
350364 $ values = [];
351- foreach ($ loopThrough as $ key => $ value )
365+ foreach ($ this -> globals [ $ method ] as $ key => $ value )
352366 {
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 );
354370 }
355371
356372 return $ values ;
@@ -363,7 +379,7 @@ protected function fetchGlobal($type, $index = null, $filter = null, $flags = nu
363379
364380 foreach ($ index as $ key )
365381 {
366- $ output [$ key ] = $ this ->fetchGlobal ($ type , $ key , $ filter , $ flags );
382+ $ output [$ key ] = $ this ->fetchGlobal ($ method , $ key , $ filter , $ flags );
367383 }
368384
369385 return $ output ;
@@ -372,7 +388,7 @@ protected function fetchGlobal($type, $index = null, $filter = null, $flags = nu
372388 // Does the index contain array notation?
373389 if (($ count = preg_match_all ('/(?:^[^\[]+)|\[[^]]*\]/ ' , $ index , $ matches )) > 1 )
374390 {
375- $ value = $ loopThrough ;
391+ $ value = $ this -> globals [ $ method ] ;
376392 for ($ i = 0 ; $ i < $ count ; $ i ++)
377393 {
378394 $ key = trim ($ matches [0 ][$ i ], '[] ' );
@@ -393,14 +409,12 @@ protected function fetchGlobal($type, $index = null, $filter = null, $flags = nu
393409 }
394410 }
395411
396- // Due to issues with FastCGI and testing,
397- // we need to do these all manually instead
398- // of the simpler filter_input();
399412 if (empty ($ value ))
400413 {
401- $ value = $ loopThrough [$ index ] ?? null ;
414+ $ value = $ this -> globals [ $ method ] [$ index ] ?? null ;
402415 }
403416
417+ // Cannot filter these types of data automatically...
404418 if (is_array ($ value ) || is_object ($ value ) || is_null ($ value ))
405419 {
406420 return $ value ;
@@ -410,4 +424,39 @@ protected function fetchGlobal($type, $index = null, $filter = null, $flags = nu
410424 }
411425
412426 //--------------------------------------------------------------------
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+ }
413462}
0 commit comments