Do not remove relative '../' with empty base.

This commit is contained in:
Dave Longley 2013-02-28 16:50:43 -05:00
parent 24fa461705
commit baa3cdf2f6

View file

@ -394,38 +394,41 @@ function jsonld_prepend_base($base, $iri) {
$segments = explode('/', $path); $segments = explode('/', $path);
// remove '.' and '' (do not remove trailing empty path) // remove '.' and '' (do not remove trailing empty path)
$idx = -1; $idx = -1;
$end = count($segments) - 1; $end = count($segments) - 1;
$filter = function($e) use (&$idx, $end) { $filter = function($e) use (&$idx, $end) {
$idx += 1; $idx += 1;
return $e !== '.' && ($e !== '' || $idx === $end); return $e !== '.' && ($e !== '' || $idx === $end);
}; };
$segments = array_values(array_filter($segments, $filter)); $segments = array_values(array_filter($segments, $filter));
// remove as many '..' as possible // do not remove '..' for empty base
for($i = 0; $i < count($segments);) { if($base['scheme'] !== '') {
$segment = $segments[$i]; // remove as many '..' as possible
if($segment === '..') { for($i = 0; $i < count($segments);) {
// too many reverse dots $segment = $segments[$i];
if($i === 0) { if($segment === '..') {
$last = $segments[count($segments) - 1]; // too many reverse dots
if($last !== '..') { if($i === 0) {
$segments = array($last); $last = $segments[count($segments) - 1];
if($last !== '..') {
$segments = array($last);
}
else {
$segments = array();
}
break;
} }
else {
$segments = array(); // remove '..' and previous segment
} array_splice($segments, $i - 1, 2);
break; $segments = array_values($segments);
$i -= 1;
}
else {
$i += 1;
} }
// remove '..' and previous segment
array_splice($segments, $i - 1, 2);
$segments = array_values($segments);
$i -= 1;
}
else {
$i += 1;
} }
} }