1
0
Fork 0

Fix support for UNC paths in normalizePath, refs #9993

pull/9995/head
Jordi Boggiano 2021-07-12 13:36:57 +02:00
parent 078aaa6968
commit cc81f5bac3
No known key found for this signature in database
GPG Key ID: 7BBD42C429EC80BC
2 changed files with 11 additions and 4 deletions

View File

@ -544,7 +544,13 @@ class Filesystem
$parts = array(); $parts = array();
$path = strtr($path, '\\', '/'); $path = strtr($path, '\\', '/');
$prefix = ''; $prefix = '';
$absolute = false; $absolute = '';
// extract windows UNC paths e.g. \\foo\bar
if (strpos($path, '//') === 0 && \strlen($path) > 2) {
$absolute = '//';
$path = substr($path, 2);
}
// extract a prefix being a protocol://, protocol:, protocol://drive: or simply drive: // extract a prefix being a protocol://, protocol:, protocol://drive: or simply drive:
if (preg_match('{^( [0-9a-z]{2,}+: (?: // (?: [a-z]: )? )? | [a-z]: )}ix', $path, $match)) { if (preg_match('{^( [0-9a-z]{2,}+: (?: // (?: [a-z]: )? )? | [a-z]: )}ix', $path, $match)) {
@ -553,13 +559,13 @@ class Filesystem
} }
if (strpos($path, '/') === 0) { if (strpos($path, '/') === 0) {
$absolute = true; $absolute = '/';
$path = substr($path, 1); $path = substr($path, 1);
} }
$up = false; $up = false;
foreach (explode('/', $path) as $chunk) { foreach (explode('/', $path) as $chunk) {
if ('..' === $chunk && ($absolute || $up)) { if ('..' === $chunk && ($absolute !== '' || $up)) {
array_pop($parts); array_pop($parts);
$up = !(empty($parts) || '..' === end($parts)); $up = !(empty($parts) || '..' === end($parts));
} elseif ('.' !== $chunk && '' !== $chunk) { } elseif ('.' !== $chunk && '' !== $chunk) {
@ -568,7 +574,7 @@ class Filesystem
} }
} }
return $prefix.($absolute ? '/' : '').implode('/', $parts); return $prefix.((string) $absolute).implode('/', $parts);
} }
/** /**

View File

@ -220,6 +220,7 @@ class FilesystemTest extends TestCase
array('../src', 'Foo/Bar/../../../src'), array('../src', 'Foo/Bar/../../../src'),
array('c:../b', 'c:.\\..\\a\\..\\b'), array('c:../b', 'c:.\\..\\a\\..\\b'),
array('phar://c:../Foo', 'phar://c:../Foo'), array('phar://c:../Foo', 'phar://c:../Foo'),
array('//foo/bar', '\\\\foo\\bar'),
); );
} }