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

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