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