| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  | /* | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  |  * Ntentan Framework | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  * Copyright (c) 2008-2015 James Ekow Abaka Ainooson | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  *  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  * Permission is hereby granted, free of charge, to any person obtaining | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  * a copy of this software and associated documentation files (the | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  * "Software"), to deal in the Software without restriction, including | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  * without limitation the rights to use, copy, modify, merge, publish, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  * distribute, sublicense, and/or sell copies of the Software, and to | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  * permit persons to whom the Software is furnished to do so, subject to | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |  * the following conditions: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  *  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |  * The above copyright notice and this permission notice shall be | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |  * included in all copies or substantial portions of the Software. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |  *  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |  * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |  * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |  * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |  *  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  | namespace ntentan\utils; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |  * An input filter class which is wraped around PHP's `filter_input` and | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |  * `filter_input_array` classes. This class provides methods which allow safe | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |  * and secure access to data passed to an application.  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |  * @author James Ainooson | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  | class Input | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  | {        | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |     const POST = INPUT_POST; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |     const GET = INPUT_GET; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |     const REQUEST = INPUT_REQUEST; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |      | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |     private static $arrays = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |      | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |      * Does the actual work of calling either the filter_input of  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |      * filter_input_array. Calls the filter_input when a data key is provided | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |      * and callse the filte_input_array when a data key is absent. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |      *  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |      * @param string $input Input type | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |      * @param string $key The data key | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |      * @return string|array The value. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |     private static function getVariable($input, $key) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |         if($key === null) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |         { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |             if(!isset(self::$arrays[$input])) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |             { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |                 self::$arrays[$input] = filter_input_array($input); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |             $return = self::$arrays[$input]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |         else | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |         { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |             $return = filter_input($input, $key); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |          | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |         if($return === null && $key === null) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |         { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |             $return = array(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |          | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |         return $return; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |      | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |      * Retrieves GET request variables. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |      *  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |      * @param string $key | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |      * @return string|array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |     public static function get($key = null) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |         return self::getVariable(INPUT_GET, $key); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |      | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |      * Retrieves post request variables. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |      *  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |      * @param string $key | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |      * @return string|array | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 92 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 93 |  |  |     public static function post($key = null) | 
            
                                                                        
                            
            
                                    
            
            
                | 94 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 95 |  |  |         return self::getVariable(INPUT_POST, $key); | 
            
                                                                        
                            
            
                                    
            
            
                | 96 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |      | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |      * Retrieves server variables. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |      *  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |      * @param string $key | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |      * @return string|array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |     public static function server($key = null) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |         return self::getVariable(INPUT_SERVER, $key); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |      | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |      * Retrieves cookie variables. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |      *  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |      * @param string $key | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |      * @return string|array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |     public static function cookie($key = null) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |         return self::getVariable(INPUT_COOKIE, $key); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |      | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |     public static function exists($input, $key) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  |         return isset(self::getVariable($input, null)[$key]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  |      | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  |     public static function files($key = null) | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  |         $files = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  |         if(!isset($_FILES[$key])) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  |             return null; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  |         if(is_array($_FILES[$key]['name'])) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 |  |  |             for($i = 0; $i < count($_FILES[$key]['name']); $i++) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 |  |  |                 $files[] = new filesystem\UploadedFile([ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 |  |  |                     'name' => $_FILES[$key]['name'][$i], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 |  |  |                     'type' => $_FILES[$key]['type'][$i], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 |  |  |                     'tmp_name' => $_FILES[$key]['tmp_name'][$i], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 |  |  |                     'error' => $_FILES[$key]['error'][$i], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 |  |  |                     'size' => $_FILES[$key]['size'][$i], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 |  |  |                 ]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 140 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 141 |  |  |             return $files; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 142 |  |  |         } else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 143 |  |  |             return new filesystem\UploadedFile($_FILES); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 144 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 145 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 146 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 147 |  |  |  |