1
0
Fork 0

Fix normalization of relative paths

pull/1773/head
Martin Hasoň 2013-04-05 06:41:14 +02:00
parent 2b385cbe58
commit 97f67c09e4
2 changed files with 17 additions and 5 deletions

View File

@ -312,23 +312,30 @@ class Filesystem
$parts = array(); $parts = array();
$path = strtr($path, '\\', '/'); $path = strtr($path, '\\', '/');
$prefix = ''; $prefix = '';
$absolute = false;
if (preg_match('{^((?:[0-9a-z]+://)?(?:[a-z]:)?/)}i', $path, $match)) { if (preg_match('{^([0-9a-z]+:(?://(?:[a-z]:)?)?)}i', $path, $match)) {
$prefix = $match[1]; $prefix = $match[1];
$path = substr($path, strlen($prefix)); $path = substr($path, strlen($prefix));
} }
$appended = false; if (substr($path, 0, 1) === '/') {
$absolute = true;
$path = substr($path, 1);
}
$up = false;
foreach (explode('/', $path) as $chunk) { foreach (explode('/', $path) as $chunk) {
if ('..' === $chunk && $appended) { if ('..' === $chunk && ($absolute || $up)) {
array_pop($parts); array_pop($parts);
$up = !(empty($parts) || '..' === end($parts));
} elseif ('.' !== $chunk && '' !== $chunk) { } elseif ('.' !== $chunk && '' !== $chunk) {
$appended = true;
$parts[] = $chunk; $parts[] = $chunk;
$up = '..' !== $chunk;
} }
} }
return $prefix.implode('/', $parts); return $prefix.($absolute ? '/' : '').implode('/', $parts);
} }
protected function directorySize($directory) protected function directorySize($directory)

View File

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