PluginProbe
Yoast SEO – Advanced SEO with real-time guidance and built-in AI / 28.2
Yoast SEO – Advanced SEO with real-time guidance and built-in AI v28.2
28.5 28.4 28.3 28.2 28.1 28.0 27.9 27.8 27.7 27.6 27.5 trunk 18.0 18.1 18.2 18.3 18.4 18.4.1 18.5 18.5.1 18.6 18.7 18.8 18.9 19.0 All 129 releases
wordpress-seo / vendor_prefixed / guzzlehttp / psr7 / src / UriResolver.php

UriResolver.php in Yoast SEO – Advanced SEO with real-time guidance and built-in AI 28.2, at vendor_prefixed/guzzlehttp/psr7/src/UriResolver.php

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