PluginProbe ʕ •ᴥ•ʔ
SureCart – Ecommerce Made Easy For Selling Physical Products, Digital Downloads, Subscriptions, Donations, & Payments / 3.0.0-beta2
SureCart – Ecommerce Made Easy For Selling Physical Products, Digital Downloads, Subscriptions, Donations, & Payments v3.0.0-beta2
4.7.0 4.6.6 4.6.5 4.6.4 4.6.3 4.6.2 4.6.1 4.6.0 4.5.1 4.5.0 4.4.2 4.4.1 4.4.0 4.3.3 4.3.2 4.3.1 4.3.0 4.2.3 4.2.2 4.2.1 1.0.3 1.0.4 1.0.5 1.0.6 1.1.0 1.1.1 1.1.10 1.1.11 1.1.12 1.1.13 1.1.14 1.1.15 1.1.16 1.1.17 1.1.18 1.1.19 1.1.2 1.1.3 1.1.4 1.1.5 1.1.6 1.1.7 1.1.8 1.1.9 1.10.0 1.10.1 1.10.2 1.10.3 1.10.4 1.11.0 1.11.1 1.11.2 1.2.0 1.2.1 1.2.2 1.2.3 1.2.4 1.2.5 1.3.0 1.3.1 1.3.2 1.3.3 1.3.4 1.4.0 1.4.1 1.4.2 1.5.0 1.5.1 1.5.2 1.5.3 1.5.4 1.5.5 1.5.6 1.5.7 1.5.8 1.6.0 1.6.1 1.6.2 1.6.3 1.6.4 1.7.0 1.7.1 1.7.2 1.8.0 1.8.1 1.8.2 1.8.3 1.8.4 1.8.5 1.9.0 1.9.1 1.9.2 1.9.3 1.9.4 1.9.5 2.0.0 2.0.1 2.1.0 2.1.1 2.1.2 2.1.3 2.1.4 2.10.0 2.10.1 2.11.0 2.11.1 2.11.2 2.11.3 2.11.4 2.12.0 2.13.0 2.14.0 2.14.1 2.15.0 2.15.1 2.16.0 2.16.1 2.16.2 2.16.3 2.17.0 2.17.1 2.17.2 2.18.0 2.19.0 2.19.2 2.19.3 2.19.4 2.2.0 2.2.1 2.20.0 2.20.1 2.20.2 2.20.3 2.20.4 2.20.5 2.20.6 2.21.0 2.22.0 2.22.1 2.23.0 2.24.0 2.25.0 2.25.1 2.25.2 2.26.0 2.27.0 2.27.1 2.28.0 2.29.0 2.29.1 2.29.2 2.29.3 2.29.4 2.3.0 2.3.1 2.30.0 2.31.0 2.31.1 2.31.2 2.31.3 2.4.0 2.4.1 2.4.2 2.4.3 2.4.4 2.40.0 2.40.1 2.5.0 2.5.1 2.5.2 2.6.0 2.6.1 2.6.2 2.7.0 2.7.1 2.7.2 2.7.3 2.7.4 2.7.5 2.8.0 2.8.1 2.8.2 2.8.3 2.8.4 2.9.0 3.0.0 3.0.0-RC1 3.0.0-RC2 3.0.0-beta1 3.0.0-beta2 3.0.1 3.0.2 3.0.3 3.0.4 3.0.5 3.1.0 3.1.1 3.1.2 3.1.3 3.1.4 3.1.5 3.1.6 3.10.0 3.10.1 3.11.0 3.12.0 3.13.0 3.13.1 3.13.2 3.13.3 3.13.4 3.14.0 3.15.0 3.15.1 3.15.2 3.15.3 3.15.4 3.15.5 3.16.0 3.16.1 3.16.2 3.16.3 3.16.4 3.16.5 3.16.6 3.16.7 3.16.8 3.17.0 3.17.1 3.17.2 3.17.3 3.17.4 3.17.5 3.17.6 3.18.0 3.19.0 3.19.1 3.19.2 3.2.0 3.2.1 3.2.2 3.20.0 3.20.1 3.3.0 3.3.1 3.4.0 3.4.1 3.4.2 3.4.3 3.5.0 3.5.1 3.5.2 3.5.3 3.6.0 3.6.1 3.6.2 3.7.0 3.7.1 3.7.2 3.7.3 3.8.0 3.8.1 3.8.2 3.8.3 3.8.4 3.8.5 3.9.0 4.0.0 4.0.1 4.0.2 4.0.3 trunk 4.1.0 0.2.19.1 4.1.1 1.0.0 4.2.0 1.0.1 1.0.2
surecart / vendor / guzzlehttp / psr7 / src / UriResolver.php
surecart / vendor / guzzlehttp / psr7 / src Last commit date
AppendStream.php 4 years ago BufferStream.php 4 years ago CachingStream.php 4 years ago DroppingStream.php 4 years ago FnStream.php 4 years ago Header.php 4 years ago InflateStream.php 4 years ago LazyOpenStream.php 4 years ago LimitStream.php 4 years ago Message.php 4 years ago MessageTrait.php 3 years ago MimeType.php 4 years ago MultipartStream.php 4 years ago NoSeekStream.php 4 years ago PumpStream.php 4 years ago Query.php 4 years ago Request.php 4 years ago Response.php 4 years ago Rfc7230.php 4 years ago ServerRequest.php 4 years ago Stream.php 4 years ago StreamDecoratorTrait.php 4 years ago StreamWrapper.php 4 years ago UploadedFile.php 4 years ago Uri.php 4 years ago UriComparator.php 3 years ago UriNormalizer.php 4 years ago UriResolver.php 4 years ago Utils.php 4 years ago functions.php 4 years ago functions_include.php 4 years ago
UriResolver.php
223 lines
1 <?php
2
3 namespace SureCartVendors\GuzzleHttp\Psr7;
4
5 use SureCartVendors\Psr\Http\Message\UriInterface;
6
7 /**
8 * Resolves a URI reference in the context of a base URI and the opposite way.
9 *
10 * @author Tobias Schultze
11 *
12 * @link https://tools.ietf.org/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 * @param string $path
20 *
21 * @return string
22 *
23 * @link http://tools.ietf.org/html/rfc3986#section-5.2.4
24 */
25 public static function removeDotSegments($path)
26 {
27 if ($path === '' || $path === '/') {
28 return $path;
29 }
30
31 $results = [];
32 $segments = explode('/', $path);
33 foreach ($segments as $segment) {
34 if ($segment === '..') {
35 array_pop($results);
36 } elseif ($segment !== '.') {
37 $results[] = $segment;
38 }
39 }
40
41 $newPath = implode('/', $results);
42
43 if ($path[0] === '/' && (!isset($newPath[0]) || $newPath[0] !== '/')) {
44 // Re-add the leading slash if necessary for cases like "/.."
45 $newPath = '/' . $newPath;
46 } elseif ($newPath !== '' && ($segment === '.' || $segment === '..')) {
47 // Add the trailing slash if necessary
48 // If newPath is not empty, then $segment must be set and is the last segment from the foreach
49 $newPath .= '/';
50 }
51
52 return $newPath;
53 }
54
55 /**
56 * Converts the relative URI into a new URI that is resolved against the base URI.
57 *
58 * @param UriInterface $base Base URI
59 * @param UriInterface $rel Relative URI
60 *
61 * @return UriInterface
62 *
63 * @link http://tools.ietf.org/html/rfc3986#section-5.2
64 */
65 public static function resolve(UriInterface $base, UriInterface $rel)
66 {
67 if ((string) $rel === '') {
68 // we can simply return the same base URI instance for this same-document reference
69 return $base;
70 }
71
72 if ($rel->getScheme() != '') {
73 return $rel->withPath(self::removeDotSegments($rel->getPath()));
74 }
75
76 if ($rel->getAuthority() != '') {
77 $targetAuthority = $rel->getAuthority();
78 $targetPath = self::removeDotSegments($rel->getPath());
79 $targetQuery = $rel->getQuery();
80 } else {
81 $targetAuthority = $base->getAuthority();
82 if ($rel->getPath() === '') {
83 $targetPath = $base->getPath();
84 $targetQuery = $rel->getQuery() != '' ? $rel->getQuery() : $base->getQuery();
85 } else {
86 if ($rel->getPath()[0] === '/') {
87 $targetPath = $rel->getPath();
88 } else {
89 if ($targetAuthority != '' && $base->getPath() === '') {
90 $targetPath = '/' . $rel->getPath();
91 } else {
92 $lastSlashPos = strrpos($base->getPath(), '/');
93 if ($lastSlashPos === false) {
94 $targetPath = $rel->getPath();
95 } else {
96 $targetPath = substr($base->getPath(), 0, $lastSlashPos + 1) . $rel->getPath();
97 }
98 }
99 }
100 $targetPath = self::removeDotSegments($targetPath);
101 $targetQuery = $rel->getQuery();
102 }
103 }
104
105 return new Uri(Uri::composeComponents(
106 $base->getScheme(),
107 $targetAuthority,
108 $targetPath,
109 $targetQuery,
110 $rel->getFragment()
111 ));
112 }
113
114 /**
115 * Returns the target URI as a relative reference from the base URI.
116 *
117 * This method is the counterpart to resolve():
118 *
119 * (string) $target === (string) UriResolver::resolve($base, UriResolver::relativize($base, $target))
120 *
121 * One use-case is to use the current request URI as base URI and then generate relative links in your documents
122 * to reduce the document size or offer self-contained downloadable document archives.
123 *
124 * $base = new Uri('http://example.com/a/b/');
125 * echo UriResolver::relativize($base, new Uri('http://example.com/a/b/c')); // prints 'c'.
126 * echo UriResolver::relativize($base, new Uri('http://example.com/a/x/y')); // prints '../x/y'.
127 * echo UriResolver::relativize($base, new Uri('http://example.com/a/b/?q')); // prints '?q'.
128 * echo UriResolver::relativize($base, new Uri('http://example.org/a/b/')); // prints '//example.org/a/b/'.
129 *
130 * This method also accepts a target that is already relative and will try to relativize it further. Only a
131 * relative-path reference will be returned as-is.
132 *
133 * echo UriResolver::relativize($base, new Uri('/a/b/c')); // prints 'c' as well
134 *
135 * @param UriInterface $base Base URI
136 * @param UriInterface $target Target URI
137 *
138 * @return UriInterface The relative URI reference
139 */
140 public static function relativize(UriInterface $base, UriInterface $target)
141 {
142 if ($target->getScheme() !== '' &&
143 ($base->getScheme() !== $target->getScheme() || $target->getAuthority() === '' && $base->getAuthority() !== '')
144 ) {
145 return $target;
146 }
147
148 if (Uri::isRelativePathReference($target)) {
149 // As the target is already highly relative we return it as-is. It would be possible to resolve
150 // the target with `$target = self::resolve($base, $target);` and then try make it more relative
151 // by removing a duplicate query. But let's not do that automatically.
152 return $target;
153 }
154
155 if ($target->getAuthority() !== '' && $base->getAuthority() !== $target->getAuthority()) {
156 return $target->withScheme('');
157 }
158
159 // We must remove the path before removing the authority because if the path starts with two slashes, the URI
160 // would turn invalid. And we also cannot set a relative path before removing the authority, as that is also
161 // invalid.
162 $emptyPathUri = $target->withScheme('')->withPath('')->withUserInfo('')->withPort(null)->withHost('');
163
164 if ($base->getPath() !== $target->getPath()) {
165 return $emptyPathUri->withPath(self::getRelativePath($base, $target));
166 }
167
168 if ($base->getQuery() === $target->getQuery()) {
169 // Only the target fragment is left. And it must be returned even if base and target fragment are the same.
170 return $emptyPathUri->withQuery('');
171 }
172
173 // If the base URI has a query but the target has none, we cannot return an empty path reference as it would
174 // inherit the base query component when resolving.
175 if ($target->getQuery() === '') {
176 $segments = explode('/', $target->getPath());
177 $lastSegment = end($segments);
178
179 return $emptyPathUri->withPath($lastSegment === '' ? './' : $lastSegment);
180 }
181
182 return $emptyPathUri;
183 }
184
185 private static function getRelativePath(UriInterface $base, UriInterface $target)
186 {
187 $sourceSegments = explode('/', $base->getPath());
188 $targetSegments = explode('/', $target->getPath());
189 array_pop($sourceSegments);
190 $targetLastSegment = array_pop($targetSegments);
191 foreach ($sourceSegments as $i => $segment) {
192 if (isset($targetSegments[$i]) && $segment === $targetSegments[$i]) {
193 unset($sourceSegments[$i], $targetSegments[$i]);
194 } else {
195 break;
196 }
197 }
198 $targetSegments[] = $targetLastSegment;
199 $relativePath = str_repeat('../', count($sourceSegments)) . implode('/', $targetSegments);
200
201 // A reference to am empty last segment or an empty first sub-segment must be prefixed with "./".
202 // This also applies to a segment with a colon character (e.g., "file:colon") that cannot be used
203 // as the first segment of a relative-path reference, as it would be mistaken for a scheme name.
204 if ('' === $relativePath || false !== strpos(explode('/', $relativePath, 2)[0], ':')) {
205 $relativePath = "./$relativePath";
206 } elseif ('/' === $relativePath[0]) {
207 if ($base->getAuthority() != '' && $base->getPath() === '') {
208 // In this case an extra slash is added by resolve() automatically. So we must not add one here.
209 $relativePath = ".$relativePath";
210 } else {
211 $relativePath = "./$relativePath";
212 }
213 }
214
215 return $relativePath;
216 }
217
218 private function __construct()
219 {
220 // cannot be instantiated
221 }
222 }
223