| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* League.Uri (https://uri.thephpleague.com) |
| 5 |
* |
| 6 |
* (c) Ignace Nyamagana Butera <[email protected]> |
| 7 |
* |
| 8 |
* For the full copyright and license information, please view the LICENSE |
| 9 |
* file that was distributed with this source code. |
| 10 |
*/ |
| 11 |
|
| 12 |
declare(strict_types=1); |
| 13 |
|
| 14 |
namespace League\Uri; |
| 15 |
|
| 16 |
use League\Uri\Contracts\UriInterface; |
| 17 |
use Psr\Http\Message\UriInterface as Psr7UriInterface; |
| 18 |
use TypeError; |
| 19 |
use function explode; |
| 20 |
use function implode; |
| 21 |
use function preg_replace_callback; |
| 22 |
use function rawurldecode; |
| 23 |
use function sprintf; |
| 24 |
|
| 25 |
final class UriInfo |
| 26 |
{ |
| 27 |
private const REGEXP_ENCODED_CHARS = ',%(2[D|E]|3[0-9]|4[1-9|A-F]|5[0-9|AF]|6[1-9|A-F]|7[0-9|E]),i'; |
| 28 |
|
| 29 |
private const WHATWG_SPECIAL_SCHEMES = ['ftp' => 21, 'http' => 80, 'https' => 443, 'ws' => 80, 'wss' => 443]; |
| 30 |
|
| 31 |
/** |
| 32 |
* @codeCoverageIgnore |
| 33 |
*/ |
| 34 |
private function __construct() |
| 35 |
{ |
| 36 |
} |
| 37 |
|
| 38 |
/** |
| 39 |
* @param Psr7UriInterface|UriInterface $uri |
| 40 |
*/ |
| 41 |
private static function emptyComponentValue($uri): ?string |
| 42 |
{ |
| 43 |
return $uri instanceof Psr7UriInterface ? '' : null; |
| 44 |
} |
| 45 |
|
| 46 |
/** |
| 47 |
* Filter the URI object. |
| 48 |
* |
| 49 |
* To be valid an URI MUST implement at least one of the following interface: |
| 50 |
* - League\Uri\UriInterface |
| 51 |
* - Psr\Http\Message\UriInterface |
| 52 |
* |
| 53 |
* @param mixed $uri the URI to validate |
| 54 |
* |
| 55 |
* @throws TypeError if the URI object does not implements the supported interfaces. |
| 56 |
* |
| 57 |
* @return Psr7UriInterface|UriInterface |
| 58 |
*/ |
| 59 |
private static function filterUri($uri) |
| 60 |
{ |
| 61 |
if ($uri instanceof Psr7UriInterface || $uri instanceof UriInterface) { |
| 62 |
return $uri; |
| 63 |
} |
| 64 |
|
| 65 |
throw new TypeError(sprintf('The uri must be a valid URI object received `%s`', is_object($uri) ? get_class($uri) : gettype($uri))); |
| 66 |
} |
| 67 |
|
| 68 |
/** |
| 69 |
* Normalize an URI for comparison. |
| 70 |
* |
| 71 |
* @param Psr7UriInterface|UriInterface $uri |
| 72 |
* |
| 73 |
* @return Psr7UriInterface|UriInterface |
| 74 |
*/ |
| 75 |
private static function normalize($uri) |
| 76 |
{ |
| 77 |
$uri = self::filterUri($uri); |
| 78 |
$null = self::emptyComponentValue($uri); |
| 79 |
|
| 80 |
$path = $uri->getPath(); |
| 81 |
if ('/' === ($path[0] ?? '') || '' !== $uri->getScheme().$uri->getAuthority()) { |
| 82 |
$path = UriResolver::resolve($uri, $uri->withPath('')->withQuery($null))->getPath(); |
| 83 |
} |
| 84 |
|
| 85 |
$query = $uri->getQuery(); |
| 86 |
$fragment = $uri->getFragment(); |
| 87 |
$fragmentOrig = $fragment; |
| 88 |
$pairs = null === $query ? [] : explode('&', $query); |
| 89 |
sort($pairs, SORT_REGULAR); |
| 90 |
|
| 91 |
$replace = static fn (array $matches): string => rawurldecode($matches[0]); |
| 92 |
|
| 93 |
$retval = preg_replace_callback(self::REGEXP_ENCODED_CHARS, $replace, [$path, implode('&', $pairs), $fragment]); |
| 94 |
if (null !== $retval) { |
| 95 |
[$path, $query, $fragment] = $retval + ['', $null, $null]; |
| 96 |
} |
| 97 |
|
| 98 |
if ($null !== $uri->getAuthority() && '' === $path) { |
| 99 |
$path = '/'; |
| 100 |
} |
| 101 |
|
| 102 |
return $uri |
| 103 |
->withHost(Uri::createFromComponents(['host' => $uri->getHost()])->getHost()) |
| 104 |
->withPath($path) |
| 105 |
->withQuery([] === $pairs ? $null : $query) |
| 106 |
->withFragment($null === $fragmentOrig ? $fragmentOrig : $fragment); |
| 107 |
} |
| 108 |
|
| 109 |
/** |
| 110 |
* Tell whether the URI represents an absolute URI. |
| 111 |
* |
| 112 |
* @param Psr7UriInterface|UriInterface $uri |
| 113 |
*/ |
| 114 |
public static function isAbsolute($uri): bool |
| 115 |
{ |
| 116 |
return self::emptyComponentValue($uri) !== self::filterUri($uri)->getScheme(); |
| 117 |
} |
| 118 |
|
| 119 |
/** |
| 120 |
* Tell whether the URI represents a network path. |
| 121 |
* |
| 122 |
* @param Psr7UriInterface|UriInterface $uri |
| 123 |
*/ |
| 124 |
public static function isNetworkPath($uri): bool |
| 125 |
{ |
| 126 |
$uri = self::filterUri($uri); |
| 127 |
$null = self::emptyComponentValue($uri); |
| 128 |
|
| 129 |
return $null === $uri->getScheme() && $null !== $uri->getAuthority(); |
| 130 |
} |
| 131 |
|
| 132 |
/** |
| 133 |
* Tell whether the URI represents an absolute path. |
| 134 |
* |
| 135 |
* @param Psr7UriInterface|UriInterface $uri |
| 136 |
*/ |
| 137 |
public static function isAbsolutePath($uri): bool |
| 138 |
{ |
| 139 |
$uri = self::filterUri($uri); |
| 140 |
$null = self::emptyComponentValue($uri); |
| 141 |
|
| 142 |
return $null === $uri->getScheme() |
| 143 |
&& $null === $uri->getAuthority() |
| 144 |
&& '/' === ($uri->getPath()[0] ?? ''); |
| 145 |
} |
| 146 |
|
| 147 |
/** |
| 148 |
* Tell whether the URI represents a relative path. |
| 149 |
* |
| 150 |
* @param Psr7UriInterface|UriInterface $uri |
| 151 |
*/ |
| 152 |
public static function isRelativePath($uri): bool |
| 153 |
{ |
| 154 |
$uri = self::filterUri($uri); |
| 155 |
$null = self::emptyComponentValue($uri); |
| 156 |
|
| 157 |
return $null === $uri->getScheme() |
| 158 |
&& $null === $uri->getAuthority() |
| 159 |
&& '/' !== ($uri->getPath()[0] ?? ''); |
| 160 |
} |
| 161 |
|
| 162 |
/** |
| 163 |
* Tell whether both URI refers to the same document. |
| 164 |
* |
| 165 |
* @param Psr7UriInterface|UriInterface $uri |
| 166 |
* @param Psr7UriInterface|UriInterface $base_uri |
| 167 |
*/ |
| 168 |
public static function isSameDocument($uri, $base_uri): bool |
| 169 |
{ |
| 170 |
$uri = self::normalize($uri); |
| 171 |
$base_uri = self::normalize($base_uri); |
| 172 |
|
| 173 |
return (string) $uri->withFragment($uri instanceof Psr7UriInterface ? '' : null) |
| 174 |
=== (string) $base_uri->withFragment($base_uri instanceof Psr7UriInterface ? '' : null); |
| 175 |
} |
| 176 |
|
| 177 |
/** |
| 178 |
* Returns the URI origin property as defined by WHATWG URL living standard. |
| 179 |
* |
| 180 |
* {@see https://url.spec.whatwg.org/#origin} |
| 181 |
* |
| 182 |
* For URI without a special scheme the method returns null |
| 183 |
* For URI with the file scheme the method will return null (as this is left to the implementation decision) |
| 184 |
* For URI with a special scheme the method returns the scheme followed by its authority (without the userinfo part) |
| 185 |
* |
| 186 |
* @param Psr7UriInterface|UriInterface $uri |
| 187 |
*/ |
| 188 |
public static function getOrigin($uri): ?string |
| 189 |
{ |
| 190 |
$scheme = self::filterUri($uri)->getScheme(); |
| 191 |
if ('blob' === $scheme) { |
| 192 |
$uri = Uri::createFromString($uri->getPath()); |
| 193 |
$scheme = $uri->getScheme(); |
| 194 |
} |
| 195 |
|
| 196 |
if (null === $scheme || !array_key_exists($scheme, self::WHATWG_SPECIAL_SCHEMES)) { |
| 197 |
return null; |
| 198 |
} |
| 199 |
|
| 200 |
$null = self::emptyComponentValue($uri); |
| 201 |
|
| 202 |
return (string) $uri->withFragment($null)->withQuery($null)->withPath('')->withUserInfo($null); |
| 203 |
} |
| 204 |
|
| 205 |
/** |
| 206 |
* @param Psr7UriInterface|UriInterface $uri |
| 207 |
* @param Psr7UriInterface|UriInterface $base_uri |
| 208 |
*/ |
| 209 |
public static function isCrossOrigin($uri, $base_uri): bool |
| 210 |
{ |
| 211 |
return null === ($uriString = self::getOrigin(Uri::createFromUri($uri))) |
| 212 |
|| null === ($baseUriString = self::getOrigin(Uri::createFromUri($base_uri))) |
| 213 |
|| $uriString !== $baseUriString; |
| 214 |
} |
| 215 |
} |
| 216 |
|