Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 11b5775

Browse files
committed
normalize paths before making them relative
1 parent ab1d938 commit 11b5775

File tree

2 files changed

+35
-0
lines changed

2 files changed

+35
-0
lines changed

src/Symfony/Component/Filesystem/Filesystem.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -362,6 +362,31 @@ public function makePathRelative($endPath, $startPath)
362362
$startPathArr = explode('/', trim($startPath, '/'));
363363
$endPathArr = explode('/', trim($endPath, '/'));
364364

365+
if ('/' !== $startPath[0]) {
366+
array_shift($startPathArr);
367+
}
368+
369+
if ('/' !== $endPath[0]) {
370+
array_shift($endPathArr);
371+
}
372+
373+
$normalizePathArray = function ($pathSegments) {
374+
$result = array();
375+
376+
foreach ($pathSegments as $segment) {
377+
if ('..' === $segment) {
378+
array_pop($result);
379+
} else {
380+
$result[] = $segment;
381+
}
382+
}
383+
384+
return $result;
385+
};
386+
387+
$startPathArr = $normalizePathArray($startPathArr);
388+
$endPathArr = $normalizePathArray($endPathArr);
389+
365390
// Find for which directory the common path stops
366391
$index = 0;
367392
while (isset($startPathArr[$index]) && isset($endPathArr[$index]) && $startPathArr[$index] === $endPathArr[$index]) {

src/Symfony/Component/Filesystem/Tests/FilesystemTest.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -842,6 +842,16 @@ public function providePathsForMakePathRelative()
842842
array('/a/aab/bb/', '/b/aab', '../../a/aab/bb/'),
843843
array('/aab/bb', '/aa', '../aab/bb/'),
844844
array('/aab', '/aa', '../aab/'),
845+
array('/aa/bb/cc', '/aa/dd/..', 'bb/cc/'),
846+
array('/aa/../bb/cc', '/aa/dd/..', '../bb/cc/'),
847+
array('/aa/bb/../../cc', '/aa/../dd/..', 'cc/'),
848+
array('../aa/bb/cc', '/aa/dd/..', 'bb/cc/'),
849+
array('../../aa/../bb/cc', '/aa/dd/..', '../bb/cc/'),
850+
array('C:/aa/bb/cc', 'C:/aa/dd/..', 'bb/cc/'),
851+
array('c:/aa/../bb/cc', 'c:/aa/dd/..', '../bb/cc/'),
852+
array('C:/aa/bb/../../cc', 'C:/aa/../dd/..', 'cc/'),
853+
array('C:/../aa/bb/cc', 'C:/aa/dd/..', 'bb/cc/'),
854+
array('C:/../../aa/../bb/cc', 'C:/aa/dd/..', '../bb/cc/'),
845855
);
846856

847857
if ('\\' === DIRECTORY_SEPARATOR) {

0 commit comments

Comments
 (0)