|
15 | 15 | use Config\Autoload; |
16 | 16 | use Config\Modules; |
17 | 17 | use InvalidArgumentException; |
| 18 | +use RuntimeException; |
18 | 19 |
|
19 | 20 | /** |
20 | 21 | * An autoloader that uses both PSR4 autoloading, and traditional classmaps. |
@@ -306,13 +307,25 @@ public function sanitizeFilename(string $filename): string |
306 | 307 | // Plus the forward slash for directory separators since this might be a path. |
307 | 308 | // http://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap03.html#tag_03_278 |
308 | 309 | // Modified to allow backslash and colons for on Windows machines. |
309 | | - $tmp = preg_replace('/[^0-9\p{L}\s\/\-\_\.\:\\\\]/u', '', $filename); |
| 310 | + $result = preg_match_all('/[^0-9\p{L}\s\/\-_.:\\\\]/u', $filename, $matches); |
| 311 | + |
| 312 | + if ($result > 0) { |
| 313 | + $chars = implode('', $matches[0]); |
| 314 | + |
| 315 | + throw new InvalidArgumentException( |
| 316 | + 'The file path contains special characters "' . $chars |
| 317 | + . '" that are not allowed: "' . $filename . '"' |
| 318 | + ); |
| 319 | + } |
| 320 | + if ($result === false) { |
| 321 | + throw new RuntimeException(preg_last_error_msg() . ' filename: "' . $filename . '"'); |
| 322 | + } |
310 | 323 |
|
311 | 324 | // Clean up our filename edges. |
312 | | - $cleanFilename = trim($tmp, '.-_'); |
| 325 | + $cleanFilename = trim($filename, '.-_'); |
313 | 326 |
|
314 | 327 | if ($filename !== $cleanFilename) { |
315 | | - throw new InvalidArgumentException('The file path contains special character that is not allowed: "' . $filename . '"'); |
| 328 | + throw new InvalidArgumentException('The characters ".-_" are not allowed in filename edges: "' . $filename . '"'); |
316 | 329 | } |
317 | 330 |
|
318 | 331 | return $cleanFilename; |
|
0 commit comments