Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

[Filesystem] normalize paths before making them relative #22133

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 24, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
normalize paths before making them relative
  • Loading branch information
xabbuh committed Mar 24, 2017
commit d50ffa1de7e0474024147b68b69db9b036d5b70d
25 changes: 25 additions & 0 deletions 25 src/Symfony/Component/Filesystem/Filesystem.php
Original file line number Diff line number Diff line change
Expand Up @@ -362,6 +362,31 @@ public function makePathRelative($endPath, $startPath)
$startPathArr = explode('/', trim($startPath, '/'));
$endPathArr = explode('/', trim($endPath, '/'));

if ('/' !== $startPath[0]) {
array_shift($startPathArr);
}

if ('/' !== $endPath[0]) {
array_shift($endPathArr);
}

$normalizePathArray = function ($pathSegments) {
$result = array();

foreach ($pathSegments as $segment) {
if ('..' === $segment) {
array_pop($result);
} else {
$result[] = $segment;
}
}

return $result;
};

$startPathArr = $normalizePathArray($startPathArr);
$endPathArr = $normalizePathArray($endPathArr);

// Find for which directory the common path stops
$index = 0;
while (isset($startPathArr[$index]) && isset($endPathArr[$index]) && $startPathArr[$index] === $endPathArr[$index]) {
Expand Down
10 changes: 10 additions & 0 deletions 10 src/Symfony/Component/Filesystem/Tests/FilesystemTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -868,6 +868,16 @@ public function providePathsForMakePathRelative()
array('/a/aab/bb/', '/b/aab', '../../a/aab/bb/'),
array('/aab/bb', '/aa', '../aab/bb/'),
array('/aab', '/aa', '../aab/'),
array('/aa/bb/cc', '/aa/dd/..', 'bb/cc/'),
array('/aa/../bb/cc', '/aa/dd/..', '../bb/cc/'),
array('/aa/bb/../../cc', '/aa/../dd/..', 'cc/'),
array('/../aa/bb/cc', '/aa/dd/..', 'bb/cc/'),
array('/../../aa/../bb/cc', '/aa/dd/..', '../bb/cc/'),
array('C:/aa/bb/cc', 'C:/aa/dd/..', 'bb/cc/'),
array('c:/aa/../bb/cc', 'c:/aa/dd/..', '../bb/cc/'),
array('C:/aa/bb/../../cc', 'C:/aa/../dd/..', 'cc/'),
array('C:/../aa/bb/cc', 'C:/aa/dd/..', 'bb/cc/'),
array('C:/../../aa/../bb/cc', 'C:/aa/dd/..', '../bb/cc/'),
);

if ('\\' === DIRECTORY_SEPARATOR) {
Expand Down
Morty Proxy This is a proxified and sanitized view of the page, visit original site.