|
13 | 13 |
|
14 | 14 | namespace CodeIgniter\Validation;
|
15 | 15 |
|
16 |
| -use CodeIgniter\Exceptions\InvalidArgumentException; |
17 |
| -use CodeIgniter\HTTP\CLIRequest; |
18 |
| -use CodeIgniter\HTTP\IncomingRequest; |
19 |
| -use CodeIgniter\HTTP\RequestInterface; |
20 |
| -use Config\Mimes; |
| 16 | +use CodeIgniter\Validation\StrictRules\FileRules as StrictFileRules; |
21 | 17 |
|
22 | 18 | /**
|
23 | 19 | * File validation rules
|
24 | 20 | *
|
25 | 21 | * @see \CodeIgniter\Validation\FileRulesTest
|
26 | 22 | */
|
27 |
| -class FileRules |
| 23 | +class FileRules extends StrictFileRules |
28 | 24 | {
|
29 |
| - /** |
30 |
| - * Request instance. So we can get access to the files. |
31 |
| - * |
32 |
| - * @var IncomingRequest |
33 |
| - */ |
34 |
| - protected $request; |
35 |
| - |
36 |
| - /** |
37 |
| - * Constructor. |
38 |
| - */ |
39 |
| - public function __construct(?RequestInterface $request = null) |
40 |
| - { |
41 |
| - if ($request === null) { |
42 |
| - $request = service('request'); |
43 |
| - } |
44 |
| - |
45 |
| - assert($request instanceof IncomingRequest || $request instanceof CLIRequest); |
46 |
| - |
47 |
| - $this->request = $request; |
48 |
| - } |
49 |
| - |
50 |
| - /** |
51 |
| - * Verifies that $name is the name of a valid uploaded file. |
52 |
| - */ |
53 |
| - public function uploaded(?string $blank, string $name): bool |
54 |
| - { |
55 |
| - if (! ($files = $this->request->getFileMultiple($name))) { |
56 |
| - $files = [$this->request->getFile($name)]; |
57 |
| - } |
58 |
| - |
59 |
| - foreach ($files as $file) { |
60 |
| - if ($file === null) { |
61 |
| - return false; |
62 |
| - } |
63 |
| - |
64 |
| - if (ENVIRONMENT === 'testing') { |
65 |
| - if ($file->getError() !== 0) { |
66 |
| - return false; |
67 |
| - } |
68 |
| - } else { |
69 |
| - // Note: cannot unit test this; no way to over-ride ENVIRONMENT? |
70 |
| - // @codeCoverageIgnoreStart |
71 |
| - if (! $file->isValid()) { |
72 |
| - return false; |
73 |
| - } |
74 |
| - // @codeCoverageIgnoreEnd |
75 |
| - } |
76 |
| - } |
77 |
| - |
78 |
| - return true; |
79 |
| - } |
80 |
| - |
81 |
| - /** |
82 |
| - * Verifies if the file's size in Kilobytes is no larger than the parameter. |
83 |
| - */ |
84 |
| - public function max_size(?string $blank, string $params): bool |
85 |
| - { |
86 |
| - // Grab the file name off the top of the $params |
87 |
| - // after we split it. |
88 |
| - $paramArray = explode(',', $params); |
89 |
| - if (count($paramArray) !== 2) { |
90 |
| - throw new InvalidArgumentException('Invalid max_size parameter: "' . $params . '"'); |
91 |
| - } |
92 |
| - $name = array_shift($paramArray); |
93 |
| - |
94 |
| - if (! ($files = $this->request->getFileMultiple($name))) { |
95 |
| - $files = [$this->request->getFile($name)]; |
96 |
| - } |
97 |
| - |
98 |
| - foreach ($files as $file) { |
99 |
| - if ($file === null) { |
100 |
| - return false; |
101 |
| - } |
102 |
| - |
103 |
| - if ($file->getError() === UPLOAD_ERR_NO_FILE) { |
104 |
| - return true; |
105 |
| - } |
106 |
| - |
107 |
| - if ($file->getError() === UPLOAD_ERR_INI_SIZE) { |
108 |
| - return false; |
109 |
| - } |
110 |
| - |
111 |
| - if ($file->getSize() / 1024 > $paramArray[0]) { |
112 |
| - return false; |
113 |
| - } |
114 |
| - } |
115 |
| - |
116 |
| - return true; |
117 |
| - } |
118 |
| - |
119 |
| - /** |
120 |
| - * Uses the mime config file to determine if a file is considered an "image", |
121 |
| - * which for our purposes basically means that it's a raster image or svg. |
122 |
| - */ |
123 |
| - public function is_image(?string $blank, string $params): bool |
124 |
| - { |
125 |
| - // Grab the file name off the top of the $params |
126 |
| - // after we split it. |
127 |
| - $params = explode(',', $params); |
128 |
| - $name = array_shift($params); |
129 |
| - |
130 |
| - if (! ($files = $this->request->getFileMultiple($name))) { |
131 |
| - $files = [$this->request->getFile($name)]; |
132 |
| - } |
133 |
| - |
134 |
| - foreach ($files as $file) { |
135 |
| - if ($file === null) { |
136 |
| - return false; |
137 |
| - } |
138 |
| - |
139 |
| - if ($file->getError() === UPLOAD_ERR_NO_FILE) { |
140 |
| - return true; |
141 |
| - } |
142 |
| - |
143 |
| - // We know that our mimes list always has the first mime |
144 |
| - // start with `image` even when then are multiple accepted types. |
145 |
| - $type = Mimes::guessTypeFromExtension($file->getExtension()) ?? ''; |
146 |
| - |
147 |
| - if (mb_strpos($type, 'image') !== 0) { |
148 |
| - return false; |
149 |
| - } |
150 |
| - } |
151 |
| - |
152 |
| - return true; |
153 |
| - } |
154 |
| - |
155 |
| - /** |
156 |
| - * Checks to see if an uploaded file's mime type matches one in the parameter. |
157 |
| - */ |
158 |
| - public function mime_in(?string $blank, string $params): bool |
159 |
| - { |
160 |
| - // Grab the file name off the top of the $params |
161 |
| - // after we split it. |
162 |
| - $params = explode(',', $params); |
163 |
| - $name = array_shift($params); |
164 |
| - |
165 |
| - if (! ($files = $this->request->getFileMultiple($name))) { |
166 |
| - $files = [$this->request->getFile($name)]; |
167 |
| - } |
168 |
| - |
169 |
| - foreach ($files as $file) { |
170 |
| - if ($file === null) { |
171 |
| - return false; |
172 |
| - } |
173 |
| - |
174 |
| - if ($file->getError() === UPLOAD_ERR_NO_FILE) { |
175 |
| - return true; |
176 |
| - } |
177 |
| - |
178 |
| - if (! in_array($file->getMimeType(), $params, true)) { |
179 |
| - return false; |
180 |
| - } |
181 |
| - } |
182 |
| - |
183 |
| - return true; |
184 |
| - } |
185 |
| - |
186 |
| - /** |
187 |
| - * Checks to see if an uploaded file's extension matches one in the parameter. |
188 |
| - */ |
189 |
| - public function ext_in(?string $blank, string $params): bool |
190 |
| - { |
191 |
| - // Grab the file name off the top of the $params |
192 |
| - // after we split it. |
193 |
| - $params = explode(',', $params); |
194 |
| - $name = array_shift($params); |
195 |
| - |
196 |
| - if (! ($files = $this->request->getFileMultiple($name))) { |
197 |
| - $files = [$this->request->getFile($name)]; |
198 |
| - } |
199 |
| - |
200 |
| - foreach ($files as $file) { |
201 |
| - if ($file === null) { |
202 |
| - return false; |
203 |
| - } |
204 |
| - |
205 |
| - if ($file->getError() === UPLOAD_ERR_NO_FILE) { |
206 |
| - return true; |
207 |
| - } |
208 |
| - |
209 |
| - if (! in_array($file->guessExtension(), $params, true)) { |
210 |
| - return false; |
211 |
| - } |
212 |
| - } |
213 |
| - |
214 |
| - return true; |
215 |
| - } |
216 |
| - |
217 |
| - /** |
218 |
| - * Checks an uploaded file to verify that the dimensions are within |
219 |
| - * a specified allowable dimension. |
220 |
| - */ |
221 |
| - public function max_dims(?string $blank, string $params): bool |
222 |
| - { |
223 |
| - // Grab the file name off the top of the $params |
224 |
| - // after we split it. |
225 |
| - $params = explode(',', $params); |
226 |
| - $name = array_shift($params); |
227 |
| - |
228 |
| - if (! ($files = $this->request->getFileMultiple($name))) { |
229 |
| - $files = [$this->request->getFile($name)]; |
230 |
| - } |
231 |
| - |
232 |
| - foreach ($files as $file) { |
233 |
| - if ($file === null) { |
234 |
| - return false; |
235 |
| - } |
236 |
| - |
237 |
| - if ($file->getError() === UPLOAD_ERR_NO_FILE) { |
238 |
| - return true; |
239 |
| - } |
240 |
| - |
241 |
| - // Get Parameter sizes |
242 |
| - $allowedWidth = $params[0] ?? 0; |
243 |
| - $allowedHeight = $params[1] ?? 0; |
244 |
| - |
245 |
| - // Get uploaded image size |
246 |
| - $info = getimagesize($file->getTempName()); |
247 |
| - |
248 |
| - if ($info === false) { |
249 |
| - // Cannot get the image size. |
250 |
| - return false; |
251 |
| - } |
252 |
| - |
253 |
| - $fileWidth = $info[0]; |
254 |
| - $fileHeight = $info[1]; |
255 |
| - |
256 |
| - if ($fileWidth > $allowedWidth || $fileHeight > $allowedHeight) { |
257 |
| - return false; |
258 |
| - } |
259 |
| - } |
260 |
| - |
261 |
| - return true; |
262 |
| - } |
263 |
| - |
264 |
| - /** |
265 |
| - * Checks an uploaded file to verify that the dimensions are greater than |
266 |
| - * a specified dimension. |
267 |
| - */ |
268 |
| - public function min_dims(?string $blank, string $params): bool |
269 |
| - { |
270 |
| - // Grab the file name off the top of the $params |
271 |
| - // after we split it. |
272 |
| - $params = explode(',', $params); |
273 |
| - $name = array_shift($params); |
274 |
| - |
275 |
| - $files = $this->request->getFileMultiple($name); |
276 |
| - if ($files === null) { |
277 |
| - $files = [$this->request->getFile($name)]; |
278 |
| - } |
279 |
| - |
280 |
| - foreach ($files as $file) { |
281 |
| - if ($file === null) { |
282 |
| - return false; |
283 |
| - } |
284 |
| - |
285 |
| - if ($file->getError() === UPLOAD_ERR_NO_FILE) { |
286 |
| - return true; |
287 |
| - } |
288 |
| - |
289 |
| - // Get Parameter sizes |
290 |
| - $minimumWidth = $params[0] ?? 0; |
291 |
| - $minimumHeight = $params[1] ?? 0; |
292 |
| - |
293 |
| - // Get uploaded image size |
294 |
| - $info = getimagesize($file->getTempName()); |
295 |
| - |
296 |
| - if ($info === false) { |
297 |
| - // Cannot get the image size. |
298 |
| - return false; |
299 |
| - } |
300 |
| - |
301 |
| - $fileWidth = $info[0]; |
302 |
| - $fileHeight = $info[1]; |
303 |
| - |
304 |
| - if ($fileWidth < $minimumWidth || $fileHeight < $minimumHeight) { |
305 |
| - return false; |
306 |
| - } |
307 |
| - } |
308 |
| - |
309 |
| - return true; |
310 |
| - } |
311 | 25 | }
|
0 commit comments