PageRenderTime 36ms CodeModel.GetById 12ms RepoModel.GetById 1ms app.codeStats 0ms

/htdocs/wp-content/plugins/wordpress-seo/vendor_prefixed/guzzlehttp/psr7/src/UriResolver.php

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