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