| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  | namespace Gongo\MercifulPolluter; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  | class Request extends Base | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |     private $magicQuotesGpc = false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |      | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |     public function pollute() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |         $this->injectFileToGlobal(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |         $this->injectEGPCSToGlobal(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |     public function enableMagicQuotesGpc() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |         $this->magicQuotesGpc = true; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |     public function disableMagicQuotesGpc() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |         $this->magicQuotesGpc = false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |      | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |      * Inject $_FILES to global variables. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |      * The naming rule when injected | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |      *     $_FILES['upfile']['tmp_name'] => $upfile | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |      *     $_FILES['upfile']['size']     => $upfile_size | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |      *     $_FILES['upfile']['type']     => $upfile_type | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |     private function injectFileToGlobal() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |         foreach ($_FILES as $field => $info) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |             $values = array(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |             foreach ($info as $key => $value) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |                 if ($key === 'tmp_name') { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |                     $name = $field; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |                 } else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |                     $name = "${field}_${key}"; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |                 } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |                 $values[$name] = $value; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |             $this->injectToGlobal($values); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |      * Inject `EGPCS` to global variables. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |      * `EGPCS` means $_ENV, $_GET, $_POST, $_COOKIE and $_SERVER. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |      * If beforehand call `enableMagicQuotesGpc()`, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |      * applies `addslashes` $_GET, $_POST, $_COOKIE and those inject variables. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |     private function injectEGPCSToGlobal() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |         $injectedFlag = array( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |             'e' => false, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |             'g' => false, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |             'p' => false, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |             'c' => false, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |             's' => false | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |         ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |         foreach ($this->getInjectVariables() as $name) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |             if (!isset($injectedFlag[$name]) || $injectedFlag[$name]) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |                 continue; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |             switch ($name) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |                 case 'e': | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |                     $this->injectToGlobal($_ENV); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |                     break; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |                 case 'g': | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |                     if ($this->magicQuotesGpc) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |                         $this->addSlashesRecursive($_GET); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |                     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |                     $this->injectToGlobal($_GET); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |                     break; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |                 case 'p': | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |                     if ($this->magicQuotesGpc) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |                         $this->addSlashesRecursive($_POST); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |                     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |                     $this->injectToGlobal($_POST); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |                     break; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |                 case 'c': | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |                     if ($this->magicQuotesGpc) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |                         $this->addSlashesRecursive($_COOKIE); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |                     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |                     $this->injectToGlobal($_COOKIE); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |                     break; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |                 case 's': | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |                     $this->injectToGlobal($_SERVER); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |                     break; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |             $injectedFlag[$name] = true; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |      * @return  array | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 108 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 109 |  |  |     protected function getInjectVariables() | 
            
                                                                        
                            
            
                                    
            
            
                | 110 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 111 |  |  |         return str_split(strtolower(ini_get('variables_order'))); | 
            
                                                                        
                            
            
                                    
            
            
                | 112 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |      * Recursively applies `addslashes` to each element of the array recursive. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |      * This method is **bang** . | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |     private function addSlashesRecursive(&$theVariables) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |         array_walk_recursive( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  |             $theVariables, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  |             function (&$value) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  |                 $value = addslashes($value); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  |         ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  |     protected function injectToGlobal(array $theVariables) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  |         foreach ($theVariables as $name => $value) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 |  |  |             if ($this->ignoringVariable($name)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 |  |  |                 continue; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 |  |  |             $GLOBALS[$name] = $value; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 139 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 140 |  |  |  |