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