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