| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849 |
- <?php
- declare(strict_types=1);
- namespace League\Flysystem;
- class WhitespacePathNormalizer implements PathNormalizer
- {
- public function normalizePath(string $path): string
- {
- $path = str_replace('\\', '/', $path);
- $this->rejectFunkyWhiteSpace($path);
- return $this->normalizeRelativePath($path);
- }
- private function rejectFunkyWhiteSpace(string $path): void
- {
- if (preg_match('#\p{C}+#u', $path)) {
- throw CorruptedPathDetected::forPath($path);
- }
- }
- private function normalizeRelativePath(string $path): string
- {
- $parts = [];
- foreach (explode('/', $path) as $part) {
- switch ($part) {
- case '':
- case '.':
- break;
- case '..':
- if (empty($parts)) {
- throw PathTraversalDetected::forPath($path);
- }
- array_pop($parts);
- break;
- default:
- $parts[] = $part;
- break;
- }
- }
- return implode('/', $parts);
- }
- }
|