酒店预订平台
25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.
 
 
 
 
 
 

223 satır
8.6 KiB

  1. <?php
  2. namespace GuzzleHttp\Psr7;
  3. use 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(UriInterface $base, 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 Uri(Uri::composeComponents(
  95. $base->getScheme(),
  96. $targetAuthority,
  97. $targetPath,
  98. $targetQuery,
  99. $rel->getFragment()
  100. ));
  101. }
  102. /**
  103. * Returns the target URI as a relative reference from the base URI.
  104. *
  105. * This method is the counterpart to resolve():
  106. *
  107. * (string) $target === (string) UriResolver::resolve($base, UriResolver::relativize($base, $target))
  108. *
  109. * One use-case is to use the current request URI as base URI and then generate relative links in your documents
  110. * to reduce the document size or offer self-contained downloadable document archives.
  111. *
  112. * $base = new Uri('http://example.com/a/b/');
  113. * echo UriResolver::relativize($base, new Uri('http://example.com/a/b/c')); // prints 'c'.
  114. * echo UriResolver::relativize($base, new Uri('http://example.com/a/x/y')); // prints '../x/y'.
  115. * echo UriResolver::relativize($base, new Uri('http://example.com/a/b/?q')); // prints '?q'.
  116. * echo UriResolver::relativize($base, new Uri('http://example.org/a/b/')); // prints '//example.org/a/b/'.
  117. *
  118. * This method also accepts a target that is already relative and will try to relativize it further. Only a
  119. * relative-path reference will be returned as-is.
  120. *
  121. * echo UriResolver::relativize($base, new Uri('/a/b/c')); // prints 'c' as well
  122. *
  123. * @param UriInterface $base Base URI
  124. * @param UriInterface $target Target URI
  125. *
  126. * @return UriInterface The relative URI reference
  127. */
  128. public static function relativize(UriInterface $base, UriInterface $target)
  129. {
  130. if ($target->getScheme() !== '' &&
  131. ($base->getScheme() !== $target->getScheme() || $target->getAuthority() === '' && $base->getAuthority() !== '')
  132. ) {
  133. return $target;
  134. }
  135. if (Uri::isRelativePathReference($target)) {
  136. // As the target is already highly relative we return it as-is. It would be possible to resolve
  137. // the target with `$target = self::resolve($base, $target);` and then try make it more relative
  138. // by removing a duplicate query. But let's not do that automatically.
  139. return $target;
  140. }
  141. if ($target->getAuthority() !== '' && $base->getAuthority() !== $target->getAuthority()) {
  142. return $target->withScheme('');
  143. }
  144. // We must remove the path before removing the authority because if the path starts with two slashes, the URI
  145. // would turn invalid. And we also cannot set a relative path before removing the authority, as that is also
  146. // invalid.
  147. $emptyPathUri = $target->withScheme('')->withPath('')->withUserInfo('')->withPort(null)->withHost('');
  148. if ($base->getPath() !== $target->getPath()) {
  149. return $emptyPathUri->withPath(self::getRelativePath($base, $target));
  150. }
  151. if ($base->getQuery() === $target->getQuery()) {
  152. // Only the target fragment is left. And it must be returned even if base and target fragment are the same.
  153. return $emptyPathUri->withQuery('');
  154. }
  155. // If the base URI has a query but the target has none, we cannot return an empty path reference as it would
  156. // inherit the base query component when resolving.
  157. if ($target->getQuery() === '') {
  158. $segments = explode('/', $target->getPath());
  159. $lastSegment = end($segments);
  160. return $emptyPathUri->withPath($lastSegment === '' ? './' : $lastSegment);
  161. }
  162. return $emptyPathUri;
  163. }
  164. private static function getRelativePath(UriInterface $base, UriInterface $target)
  165. {
  166. $sourceSegments = explode('/', $base->getPath());
  167. $targetSegments = explode('/', $target->getPath());
  168. array_pop($sourceSegments);
  169. $targetLastSegment = array_pop($targetSegments);
  170. foreach ($sourceSegments as $i => $segment) {
  171. if (isset($targetSegments[$i]) && $segment === $targetSegments[$i]) {
  172. unset($sourceSegments[$i], $targetSegments[$i]);
  173. } else {
  174. break;
  175. }
  176. }
  177. $targetSegments[] = $targetLastSegment;
  178. $relativePath = str_repeat('../', count($sourceSegments)) . implode('/', $targetSegments);
  179. // A reference to am empty last segment or an empty first sub-segment must be prefixed with "./".
  180. // This also applies to a segment with a colon character (e.g., "file:colon") that cannot be used
  181. // as the first segment of a relative-path reference, as it would be mistaken for a scheme name.
  182. if ('' === $relativePath || false !== strpos(explode('/', $relativePath, 2)[0], ':')) {
  183. $relativePath = "./$relativePath";
  184. } elseif ('/' === $relativePath[0]) {
  185. if ($base->getAuthority() != '' && $base->getPath() === '') {
  186. // In this case an extra slash is added by resolve() automatically. So we must not add one here.
  187. $relativePath = ".$relativePath";
  188. } else {
  189. $relativePath = "./$relativePath";
  190. }
  191. }
  192. return $relativePath;
  193. }
  194. private function __construct()
  195. {
  196. // cannot be instantiated
  197. }
  198. }