Edit File by line
/home/barbar84/www/wp-conte.../plugins/updraftp.../vendor/guzzleht.../psr7/src
File: UriResolver.php
<?php
[0] Fix | Delete
[1] Fix | Delete
namespace GuzzleHttp\Psr7;
[2] Fix | Delete
[3] Fix | Delete
use Psr\Http\Message\UriInterface;
[4] Fix | Delete
[5] Fix | Delete
/**
[6] Fix | Delete
* Resolves a URI reference in the context of a base URI and the opposite way.
[7] Fix | Delete
*
[8] Fix | Delete
* @author Tobias Schultze
[9] Fix | Delete
*
[10] Fix | Delete
* @link https://tools.ietf.org/html/rfc3986#section-5
[11] Fix | Delete
*/
[12] Fix | Delete
final class UriResolver
[13] Fix | Delete
{
[14] Fix | Delete
/**
[15] Fix | Delete
* Removes dot segments from a path and returns the new path.
[16] Fix | Delete
*
[17] Fix | Delete
* @param string $path
[18] Fix | Delete
*
[19] Fix | Delete
* @return string
[20] Fix | Delete
*
[21] Fix | Delete
* @link http://tools.ietf.org/html/rfc3986#section-5.2.4
[22] Fix | Delete
*/
[23] Fix | Delete
public static function removeDotSegments($path)
[24] Fix | Delete
{
[25] Fix | Delete
if ($path === '' || $path === '/') {
[26] Fix | Delete
return $path;
[27] Fix | Delete
}
[28] Fix | Delete
[29] Fix | Delete
$results = [];
[30] Fix | Delete
$segments = explode('/', $path);
[31] Fix | Delete
foreach ($segments as $segment) {
[32] Fix | Delete
if ($segment === '..') {
[33] Fix | Delete
array_pop($results);
[34] Fix | Delete
} elseif ($segment !== '.') {
[35] Fix | Delete
$results[] = $segment;
[36] Fix | Delete
}
[37] Fix | Delete
}
[38] Fix | Delete
[39] Fix | Delete
$newPath = implode('/', $results);
[40] Fix | Delete
[41] Fix | Delete
if ($path[0] === '/' && (!isset($newPath[0]) || $newPath[0] !== '/')) {
[42] Fix | Delete
// Re-add the leading slash if necessary for cases like "/.."
[43] Fix | Delete
$newPath = '/' . $newPath;
[44] Fix | Delete
} elseif ($newPath !== '' && ($segment === '.' || $segment === '..')) {
[45] Fix | Delete
// Add the trailing slash if necessary
[46] Fix | Delete
// If newPath is not empty, then $segment must be set and is the last segment from the foreach
[47] Fix | Delete
$newPath .= '/';
[48] Fix | Delete
}
[49] Fix | Delete
[50] Fix | Delete
return $newPath;
[51] Fix | Delete
}
[52] Fix | Delete
[53] Fix | Delete
/**
[54] Fix | Delete
* Converts the relative URI into a new URI that is resolved against the base URI.
[55] Fix | Delete
*
[56] Fix | Delete
* @param UriInterface $base Base URI
[57] Fix | Delete
* @param UriInterface $rel Relative URI
[58] Fix | Delete
*
[59] Fix | Delete
* @return UriInterface
[60] Fix | Delete
*
[61] Fix | Delete
* @link http://tools.ietf.org/html/rfc3986#section-5.2
[62] Fix | Delete
*/
[63] Fix | Delete
public static function resolve(UriInterface $base, UriInterface $rel)
[64] Fix | Delete
{
[65] Fix | Delete
if ((string) $rel === '') {
[66] Fix | Delete
// we can simply return the same base URI instance for this same-document reference
[67] Fix | Delete
return $base;
[68] Fix | Delete
}
[69] Fix | Delete
[70] Fix | Delete
if ($rel->getScheme() != '') {
[71] Fix | Delete
return $rel->withPath(self::removeDotSegments($rel->getPath()));
[72] Fix | Delete
}
[73] Fix | Delete
[74] Fix | Delete
if ($rel->getAuthority() != '') {
[75] Fix | Delete
$targetAuthority = $rel->getAuthority();
[76] Fix | Delete
$targetPath = self::removeDotSegments($rel->getPath());
[77] Fix | Delete
$targetQuery = $rel->getQuery();
[78] Fix | Delete
} else {
[79] Fix | Delete
$targetAuthority = $base->getAuthority();
[80] Fix | Delete
if ($rel->getPath() === '') {
[81] Fix | Delete
$targetPath = $base->getPath();
[82] Fix | Delete
$targetQuery = $rel->getQuery() != '' ? $rel->getQuery() : $base->getQuery();
[83] Fix | Delete
} else {
[84] Fix | Delete
if ($rel->getPath()[0] === '/') {
[85] Fix | Delete
$targetPath = $rel->getPath();
[86] Fix | Delete
} else {
[87] Fix | Delete
if ($targetAuthority != '' && $base->getPath() === '') {
[88] Fix | Delete
$targetPath = '/' . $rel->getPath();
[89] Fix | Delete
} else {
[90] Fix | Delete
$lastSlashPos = strrpos($base->getPath(), '/');
[91] Fix | Delete
if ($lastSlashPos === false) {
[92] Fix | Delete
$targetPath = $rel->getPath();
[93] Fix | Delete
} else {
[94] Fix | Delete
$targetPath = substr($base->getPath(), 0, $lastSlashPos + 1) . $rel->getPath();
[95] Fix | Delete
}
[96] Fix | Delete
}
[97] Fix | Delete
}
[98] Fix | Delete
$targetPath = self::removeDotSegments($targetPath);
[99] Fix | Delete
$targetQuery = $rel->getQuery();
[100] Fix | Delete
}
[101] Fix | Delete
}
[102] Fix | Delete
[103] Fix | Delete
return new Uri(Uri::composeComponents(
[104] Fix | Delete
$base->getScheme(),
[105] Fix | Delete
$targetAuthority,
[106] Fix | Delete
$targetPath,
[107] Fix | Delete
$targetQuery,
[108] Fix | Delete
$rel->getFragment()
[109] Fix | Delete
));
[110] Fix | Delete
}
[111] Fix | Delete
[112] Fix | Delete
/**
[113] Fix | Delete
* Returns the target URI as a relative reference from the base URI.
[114] Fix | Delete
*
[115] Fix | Delete
* This method is the counterpart to resolve():
[116] Fix | Delete
*
[117] Fix | Delete
* (string) $target === (string) UriResolver::resolve($base, UriResolver::relativize($base, $target))
[118] Fix | Delete
*
[119] Fix | Delete
* One use-case is to use the current request URI as base URI and then generate relative links in your documents
[120] Fix | Delete
* to reduce the document size or offer self-contained downloadable document archives.
[121] Fix | Delete
*
[122] Fix | Delete
* $base = new Uri('http://example.com/a/b/');
[123] Fix | Delete
* echo UriResolver::relativize($base, new Uri('http://example.com/a/b/c')); // prints 'c'.
[124] Fix | Delete
* echo UriResolver::relativize($base, new Uri('http://example.com/a/x/y')); // prints '../x/y'.
[125] Fix | Delete
* echo UriResolver::relativize($base, new Uri('http://example.com/a/b/?q')); // prints '?q'.
[126] Fix | Delete
* echo UriResolver::relativize($base, new Uri('http://example.org/a/b/')); // prints '//example.org/a/b/'.
[127] Fix | Delete
*
[128] Fix | Delete
* This method also accepts a target that is already relative and will try to relativize it further. Only a
[129] Fix | Delete
* relative-path reference will be returned as-is.
[130] Fix | Delete
*
[131] Fix | Delete
* echo UriResolver::relativize($base, new Uri('/a/b/c')); // prints 'c' as well
[132] Fix | Delete
*
[133] Fix | Delete
* @param UriInterface $base Base URI
[134] Fix | Delete
* @param UriInterface $target Target URI
[135] Fix | Delete
*
[136] Fix | Delete
* @return UriInterface The relative URI reference
[137] Fix | Delete
*/
[138] Fix | Delete
public static function relativize(UriInterface $base, UriInterface $target)
[139] Fix | Delete
{
[140] Fix | Delete
if ($target->getScheme() !== '' &&
[141] Fix | Delete
($base->getScheme() !== $target->getScheme() || $target->getAuthority() === '' && $base->getAuthority() !== '')
[142] Fix | Delete
) {
[143] Fix | Delete
return $target;
[144] Fix | Delete
}
[145] Fix | Delete
[146] Fix | Delete
if (Uri::isRelativePathReference($target)) {
[147] Fix | Delete
// As the target is already highly relative we return it as-is. It would be possible to resolve
[148] Fix | Delete
// the target with `$target = self::resolve($base, $target);` and then try make it more relative
[149] Fix | Delete
// by removing a duplicate query. But let's not do that automatically.
[150] Fix | Delete
return $target;
[151] Fix | Delete
}
[152] Fix | Delete
[153] Fix | Delete
if ($target->getAuthority() !== '' && $base->getAuthority() !== $target->getAuthority()) {
[154] Fix | Delete
return $target->withScheme('');
[155] Fix | Delete
}
[156] Fix | Delete
[157] Fix | Delete
// We must remove the path before removing the authority because if the path starts with two slashes, the URI
[158] Fix | Delete
// would turn invalid. And we also cannot set a relative path before removing the authority, as that is also
[159] Fix | Delete
// invalid.
[160] Fix | Delete
$emptyPathUri = $target->withScheme('')->withPath('')->withUserInfo('')->withPort(null)->withHost('');
[161] Fix | Delete
[162] Fix | Delete
if ($base->getPath() !== $target->getPath()) {
[163] Fix | Delete
return $emptyPathUri->withPath(self::getRelativePath($base, $target));
[164] Fix | Delete
}
[165] Fix | Delete
[166] Fix | Delete
if ($base->getQuery() === $target->getQuery()) {
[167] Fix | Delete
// Only the target fragment is left. And it must be returned even if base and target fragment are the same.
[168] Fix | Delete
return $emptyPathUri->withQuery('');
[169] Fix | Delete
}
[170] Fix | Delete
[171] Fix | Delete
// If the base URI has a query but the target has none, we cannot return an empty path reference as it would
[172] Fix | Delete
// inherit the base query component when resolving.
[173] Fix | Delete
if ($target->getQuery() === '') {
[174] Fix | Delete
$segments = explode('/', $target->getPath());
[175] Fix | Delete
$lastSegment = end($segments);
[176] Fix | Delete
[177] Fix | Delete
return $emptyPathUri->withPath($lastSegment === '' ? './' : $lastSegment);
[178] Fix | Delete
}
[179] Fix | Delete
[180] Fix | Delete
return $emptyPathUri;
[181] Fix | Delete
}
[182] Fix | Delete
[183] Fix | Delete
private static function getRelativePath(UriInterface $base, UriInterface $target)
[184] Fix | Delete
{
[185] Fix | Delete
$sourceSegments = explode('/', $base->getPath());
[186] Fix | Delete
$targetSegments = explode('/', $target->getPath());
[187] Fix | Delete
array_pop($sourceSegments);
[188] Fix | Delete
$targetLastSegment = array_pop($targetSegments);
[189] Fix | Delete
foreach ($sourceSegments as $i => $segment) {
[190] Fix | Delete
if (isset($targetSegments[$i]) && $segment === $targetSegments[$i]) {
[191] Fix | Delete
unset($sourceSegments[$i], $targetSegments[$i]);
[192] Fix | Delete
} else {
[193] Fix | Delete
break;
[194] Fix | Delete
}
[195] Fix | Delete
}
[196] Fix | Delete
$targetSegments[] = $targetLastSegment;
[197] Fix | Delete
$relativePath = str_repeat('../', count($sourceSegments)) . implode('/', $targetSegments);
[198] Fix | Delete
[199] Fix | Delete
// A reference to am empty last segment or an empty first sub-segment must be prefixed with "./".
[200] Fix | Delete
// This also applies to a segment with a colon character (e.g., "file:colon") that cannot be used
[201] Fix | Delete
// as the first segment of a relative-path reference, as it would be mistaken for a scheme name.
[202] Fix | Delete
if ('' === $relativePath || false !== strpos(explode('/', $relativePath, 2)[0], ':')) {
[203] Fix | Delete
$relativePath = "./$relativePath";
[204] Fix | Delete
} elseif ('/' === $relativePath[0]) {
[205] Fix | Delete
if ($base->getAuthority() != '' && $base->getPath() === '') {
[206] Fix | Delete
// In this case an extra slash is added by resolve() automatically. So we must not add one here.
[207] Fix | Delete
$relativePath = ".$relativePath";
[208] Fix | Delete
} else {
[209] Fix | Delete
$relativePath = "./$relativePath";
[210] Fix | Delete
}
[211] Fix | Delete
}
[212] Fix | Delete
[213] Fix | Delete
return $relativePath;
[214] Fix | Delete
}
[215] Fix | Delete
[216] Fix | Delete
private function __construct()
[217] Fix | Delete
{
[218] Fix | Delete
// cannot be instantiated
[219] Fix | Delete
}
[220] Fix | Delete
}
[221] Fix | Delete
[222] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function