PluginProbe
Yoast SEO – Advanced SEO with real-time guidance and built-in AI / 18.2
Yoast SEO – Advanced SEO with real-time guidance and built-in AI v18.2
28.5 28.4 28.3 28.2 28.1 28.0 27.9 27.8 27.7 27.6 27.5 trunk 18.0 18.1 18.2 18.3 18.4 18.4.1 18.5 18.5.1 18.6 18.7 18.8 18.9 19.0 All 129 releases
wordpress-seo / vendor_prefixed / guzzlehttp / psr7 / src / UriNormalizer.php

UriNormalizer.php in Yoast SEO – Advanced SEO with real-time guidance and built-in AI 18.2, at vendor_prefixed/guzzlehttp/psr7/src/UriNormalizer.php

182 lines 8.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace YoastSEO_Vendor\GuzzleHttp\Psr7;
4
5 use YoastSEO_Vendor\Psr\Http\Message\UriInterface;
6 /**
7 * Provides methods to normalize and compare URIs.
8 *
9 * @author Tobias Schultze
10 *
11 * @link https://tools.ietf.org/html/rfc3986#section-6
12 */
13 final class UriNormalizer
14 {
15 /**
16 * Default normalizations which only include the ones that preserve semantics.
17 *
18 * self::CAPITALIZE_PERCENT_ENCODING | self::DECODE_UNRESERVED_CHARACTERS | self::CONVERT_EMPTY_PATH |
19 * self::REMOVE_DEFAULT_HOST | self::REMOVE_DEFAULT_PORT | self::REMOVE_DOT_SEGMENTS
20 */
21 const PRESERVING_NORMALIZATIONS = 63;
22 /**
23 * All letters within a percent-encoding triplet (e.g., "%3A") are case-insensitive, and should be capitalized.
24 *
25 * Example: http://example.org/a%c2%b1b → http://example.org/a%C2%B1b
26 */
27 const CAPITALIZE_PERCENT_ENCODING = 1;
28 /**
29 * Decodes percent-encoded octets of unreserved characters.
30 *
31 * For consistency, percent-encoded octets in the ranges of ALPHA (%41–%5A and %61–%7A), DIGIT (%30–%39),
32 * hyphen (%2D), period (%2E), underscore (%5F), or tilde (%7E) should not be created by URI producers and,
33 * when found in a URI, should be decoded to their corresponding unreserved characters by URI normalizers.
34 *
35 * Example: http://example.org/%7Eusern%61me/ → http://example.org/~username/
36 */
37 const DECODE_UNRESERVED_CHARACTERS = 2;
38 /**
39 * Converts the empty path to "/" for http and https URIs.
40 *
41 * Example: http://example.org → http://example.org/
42 */
43 const CONVERT_EMPTY_PATH = 4;
44 /**
45 * Removes the default host of the given URI scheme from the URI.
46 *
47 * Only the "file" scheme defines the default host "localhost".
48 * All of `file:/myfile`, `file:///myfile`, and `file://localhost/myfile`
49 * are equivalent according to RFC 3986. The first format is not accepted
50 * by PHPs stream functions and thus already normalized implicitly to the
51 * second format in the Uri class. See `GuzzleHttp\Psr7\Uri::composeComponents`.
52 *
53 * Example: file://localhost/myfile → file:///myfile
54 */
55 const REMOVE_DEFAULT_HOST = 8;
56 /**
57 * Removes the default port of the given URI scheme from the URI.
58 *
59 * Example: http://example.org:80/ → http://example.org/
60 */
61 const REMOVE_DEFAULT_PORT = 16;
62 /**
63 * Removes unnecessary dot-segments.
64 *
65 * Dot-segments in relative-path references are not removed as it would
66 * change the semantics of the URI reference.
67 *
68 * Example: http://example.org/../a/b/../c/./d.html → http://example.org/a/c/d.html
69 */
70 const REMOVE_DOT_SEGMENTS = 32;
71 /**
72 * Paths which include two or more adjacent slashes are converted to one.
73 *
74 * Webservers usually ignore duplicate slashes and treat those URIs equivalent.
75 * But in theory those URIs do not need to be equivalent. So this normalization
76 * may change the semantics. Encoded slashes (%2F) are not removed.
77 *
78 * Example: http://example.org//foo///bar.html → http://example.org/foo/bar.html
79 */
80 const REMOVE_DUPLICATE_SLASHES = 64;
81 /**
82 * Sort query parameters with their values in alphabetical order.
83 *
84 * However, the order of parameters in a URI may be significant (this is not defined by the standard).
85 * So this normalization is not safe and may change the semantics of the URI.
86 *
87 * Example: ?lang=en&article=fred → ?article=fred&lang=en
88 *
89 * Note: The sorting is neither locale nor Unicode aware (the URI query does not get decoded at all) as the
90 * purpose is to be able to compare URIs in a reproducible way, not to have the params sorted perfectly.
91 */
92 const SORT_QUERY_PARAMETERS = 128;
93 /**
94 * Returns a normalized URI.
95 *
96 * The scheme and host component are already normalized to lowercase per PSR-7 UriInterface.
97 * This methods adds additional normalizations that can be configured with the $flags parameter.
98 *
99 * PSR-7 UriInterface cannot distinguish between an empty component and a missing component as
100 * getQuery(), getFragment() etc. always return a string. This means the URIs "/?#" and "/" are
101 * treated equivalent which is not necessarily true according to RFC 3986. But that difference
102 * is highly uncommon in reality. So this potential normalization is implied in PSR-7 as well.
103 *
104 * @param UriInterface $uri The URI to normalize
105 * @param int $flags A bitmask of normalizations to apply, see constants
106 *
107 * @return UriInterface The normalized URI
108 *
109 * @link https://tools.ietf.org/html/rfc3986#section-6.2
110 */
111 public static function normalize(\YoastSEO_Vendor\Psr\Http\Message\UriInterface $uri, $flags = self::PRESERVING_NORMALIZATIONS)
112 {
113 if ($flags & self::CAPITALIZE_PERCENT_ENCODING) {
114 $uri = self::capitalizePercentEncoding($uri);
115 }
116 if ($flags & self::DECODE_UNRESERVED_CHARACTERS) {
117 $uri = self::decodeUnreservedCharacters($uri);
118 }
119 if ($flags & self::CONVERT_EMPTY_PATH && $uri->getPath() === '' && ($uri->getScheme() === 'http' || $uri->getScheme() === 'https')) {
120 $uri = $uri->withPath('/');
121 }
122 if ($flags & self::REMOVE_DEFAULT_HOST && $uri->getScheme() === 'file' && $uri->getHost() === 'localhost') {
123 $uri = $uri->withHost('');
124 }
125 if ($flags & self::REMOVE_DEFAULT_PORT && $uri->getPort() !== null && \YoastSEO_Vendor\GuzzleHttp\Psr7\Uri::isDefaultPort($uri)) {
126 $uri = $uri->withPort(null);
127 }
128 if ($flags & self::REMOVE_DOT_SEGMENTS && !\YoastSEO_Vendor\GuzzleHttp\Psr7\Uri::isRelativePathReference($uri)) {
129 $uri = $uri->withPath(\YoastSEO_Vendor\GuzzleHttp\Psr7\UriResolver::removeDotSegments($uri->getPath()));
130 }
131 if ($flags & self::REMOVE_DUPLICATE_SLASHES) {
132 $uri = $uri->withPath(\preg_replace('#//++#', '/', $uri->getPath()));
133 }
134 if ($flags & self::SORT_QUERY_PARAMETERS && $uri->getQuery() !== '') {
135 $queryKeyValues = \explode('&', $uri->getQuery());
136 \sort($queryKeyValues);
137 $uri = $uri->withQuery(\implode('&', $queryKeyValues));
138 }
139 return $uri;
140 }
141 /**
142 * Whether two URIs can be considered equivalent.
143 *
144 * Both URIs are normalized automatically before comparison with the given $normalizations bitmask. The method also
145 * accepts relative URI references and returns true when they are equivalent. This of course assumes they will be
146 * resolved against the same base URI. If this is not the case, determination of equivalence or difference of
147 * relative references does not mean anything.
148 *
149 * @param UriInterface $uri1 An URI to compare
150 * @param UriInterface $uri2 An URI to compare
151 * @param int $normalizations A bitmask of normalizations to apply, see constants
152 *
153 * @return bool
154 *
155 * @link https://tools.ietf.org/html/rfc3986#section-6.1
156 */
157 public static function isEquivalent(\YoastSEO_Vendor\Psr\Http\Message\UriInterface $uri1, \YoastSEO_Vendor\Psr\Http\Message\UriInterface $uri2, $normalizations = self::PRESERVING_NORMALIZATIONS)
158 {
159 return (string) self::normalize($uri1, $normalizations) === (string) self::normalize($uri2, $normalizations);
160 }
161 private static function capitalizePercentEncoding(\YoastSEO_Vendor\Psr\Http\Message\UriInterface $uri)
162 {
163 $regex = '/(?:%[A-Fa-f0-9]{2})++/';
164 $callback = function (array $match) {
165 return \strtoupper($match[0]);
166 };
167 return $uri->withPath(\preg_replace_callback($regex, $callback, $uri->getPath()))->withQuery(\preg_replace_callback($regex, $callback, $uri->getQuery()));
168 }
169 private static function decodeUnreservedCharacters(\YoastSEO_Vendor\Psr\Http\Message\UriInterface $uri)
170 {
171 $regex = '/%(?:2D|2E|5F|7E|3[0-9]|[46][1-9A-F]|[57][0-9A])/i';
172 $callback = function (array $match) {
173 return \rawurldecode($match[0]);
174 };
175 return $uri->withPath(\preg_replace_callback($regex, $callback, $uri->getPath()))->withQuery(\preg_replace_callback($regex, $callback, $uri->getQuery()));
176 }
177 private function __construct()
178 {
179 // cannot be instantiated
180 }
181 }
182