| 1 |
<?php |
| 2 |
|
| 3 |
declare(strict_types=1); |
| 4 |
|
| 5 |
namespace League\Flysystem; |
| 6 |
|
| 7 |
class WhitespacePathNormalizer implements PathNormalizer |
| 8 |
{ |
| 9 |
public function normalizePath(string $path): string |
| 10 |
{ |
| 11 |
$path = str_replace('\\', '/', $path); |
| 12 |
$this->rejectFunkyWhiteSpace($path); |
| 13 |
|
| 14 |
return $this->normalizeRelativePath($path); |
| 15 |
} |
| 16 |
|
| 17 |
private function rejectFunkyWhiteSpace(string $path): void |
| 18 |
{ |
| 19 |
if (preg_match('#\p{C}+#u', $path)) { |
| 20 |
throw CorruptedPathDetected::forPath($path); |
| 21 |
} |
| 22 |
} |
| 23 |
|
| 24 |
private function normalizeRelativePath(string $path): string |
| 25 |
{ |
| 26 |
$parts = []; |
| 27 |
|
| 28 |
foreach (explode('/', $path) as $part) { |
| 29 |
switch ($part) { |
| 30 |
case '': |
| 31 |
case '.': |
| 32 |
break; |
| 33 |
|
| 34 |
case '..': |
| 35 |
if (empty($parts)) { |
| 36 |
throw PathTraversalDetected::forPath($path); |
| 37 |
} |
| 38 |
array_pop($parts); |
| 39 |
break; |
| 40 |
|
| 41 |
default: |
| 42 |
$parts[] = $part; |
| 43 |
break; |
| 44 |
} |
| 45 |
} |
| 46 |
|
| 47 |
return implode('/', $parts); |
| 48 |
} |
| 49 |
} |
| 50 |
|