Diff: STRATO-apps/wordpress_03/app/wp-content/plugins/google-site-kit/includes/Core/Util/URL.php
Keine Baseline-Datei – Diff nur gegen leer.
1
-
1
+
<?php
2
+
/**
3
+
* Class Google\Site_Kit\Core\Util\URL
4
+
*
5
+
* @package Google\Site_Kit\Core\Util
6
+
* @copyright 2022 Google LLC
7
+
* @license https://www.apache.org/licenses/LICENSE-2.0 Apache License 2.0
8
+
* @link https://sitekit.withgoogle.com
9
+
*/
10
+
11
+
namespace Google\Site_Kit\Core\Util;
12
+
13
+
/**
14
+
* Class for custom URL parsing methods.
15
+
*
16
+
* @since 1.84.0
17
+
* @access private
18
+
* @ignore
19
+
*/
20
+
class URL {
21
+
22
+
/**
23
+
* Prefix for Punycode-encoded hostnames.
24
+
*/
25
+
const PUNYCODE_PREFIX = 'xn--';
26
+
27
+
/**
28
+
* Parses URLs with UTF-8 multi-byte characters,
29
+
* otherwise similar to `wp_parse_url()`.
30
+
*
31
+
* @since 1.84.0
32
+
*
33
+
* @param string $url The URL to parse.
34
+
* @param int $component The specific component to retrieve. Use one of the PHP
35
+
* predefined constants to specify which one.
36
+
* Defaults to -1 (= return all parts as an array).
37
+
* @return mixed False on parse failure; Array of URL components on success;
38
+
* When a specific component has been requested: null if the component
39
+
* doesn't exist in the given URL; a string or - in the case of
40
+
* PHP_URL_PORT - integer when it does. See parse_url()'s return values.
41
+
*/
42
+
public static function parse( $url, $component = -1 ) {
43
+
$url = (string) $url;
44
+
45
+
if ( mb_strlen( $url, 'UTF-8' ) === strlen( $url ) ) {
46
+
return wp_parse_url( $url, $component );
47
+
}
48
+
49
+
$to_unset = array();
50
+
if ( '//' === mb_substr( $url, 0, 2 ) ) {
51
+
$to_unset[] = 'scheme';
52
+
$url = 'placeholder:' . $url;
53
+
} elseif ( '/' === mb_substr( $url, 0, 1 ) ) {
54
+
$to_unset[] = 'scheme';
55
+
$to_unset[] = 'host';
56
+
$url = 'placeholder://placeholder' . $url;
57
+
}
58
+
59
+
$parts = self::mb_parse_url( $url );
60
+
61
+
if ( false === $parts ) {
62
+
// Parsing failure.
63
+
return $parts;
64
+
}
65
+
66
+
// Remove the placeholder values.
67
+
foreach ( $to_unset as $key ) {
68
+
unset( $parts[ $key ] );
69
+
}
70
+
71
+
return _get_component_from_parsed_url_array( $parts, $component );
72
+
}
73
+
74
+
/**
75
+
* Replacement for parse_url which is UTF-8 multi-byte character aware.
76
+
*
77
+
* @since 1.84.0
78
+
*
79
+
* @param string $url The URL to parse.
80
+
* @return mixed False on parse failure; Array of URL components on success
81
+
*/
82
+
private static function mb_parse_url( $url ) {
83
+
$enc_url = preg_replace_callback(
84
+
'%[^:/@?&=#]+%usD',
85
+
function ( $matches ) {
86
+
return rawurlencode( $matches[0] );
87
+
},
88
+
$url
89
+
);
90
+
91
+
$parts = parse_url( $enc_url ); // phpcs:ignore WordPress.WP.AlternativeFunctions.parse_url_parse_url
92
+
93
+
if ( false === $parts ) {
94
+
return $parts;
95
+
}
96
+
97
+
foreach ( $parts as $name => $value ) {
98
+
$parts[ $name ] = urldecode( $value );
99
+
}
100
+
101
+
return $parts;
102
+
}
103
+
104
+
/**
105
+
* Permutes site URL to cover all different variants of it (not considering the path).
106
+
*
107
+
* @since 1.99.0
108
+
*
109
+
* @param string $site_url Site URL to get permutations for.
110
+
* @return array List of permutations.
111
+
*/
112
+
public static function permute_site_url( $site_url ) {
113
+
$hostname = self::parse( $site_url, PHP_URL_HOST );
114
+
$path = self::parse( $site_url, PHP_URL_PATH );
115
+
116
+
return array_reduce(
117
+
self::permute_site_hosts( $hostname ),
118
+
function ( $urls, $host ) use ( $path ) {
119
+
$host_with_path = $host . $path;
120
+
array_push( $urls, "https://$host_with_path", "http://$host_with_path" );
121
+
return $urls;
122
+
},
123
+
array()
124
+
);
125
+
}
126
+
127
+
/**
128
+
* Generates common variations of the given hostname.
129
+
*
130
+
* Returns a list of hostnames that includes:
131
+
* - (if IDN) in Punycode encoding
132
+
* - (if IDN) in Unicode encoding
133
+
* - with and without www. subdomain (including IDNs)
134
+
*
135
+
* @since 1.99.0
136
+
*
137
+
* @param string $hostname Hostname to generate variations of.
138
+
* @return string[] Hostname variations.
139
+
*/
140
+
public static function permute_site_hosts( $hostname ) {
141
+
if ( ! $hostname || ! is_string( $hostname ) ) {
142
+
return array();
143
+
}
144
+
145
+
// See \Requests_IDNAEncoder::is_ascii.
146
+
$is_ascii = preg_match( '/(?:[^\x00-\x7F])/', $hostname ) !== 1;
147
+
$is_www = 0 === strpos( $hostname, 'www.' );
148
+
// Normalize hostname without www.
149
+
$hostname = $is_www ? substr( $hostname, strlen( 'www.' ) ) : $hostname;
150
+
$hosts = array( $hostname, "www.$hostname" );
151
+
152
+
try {
153
+
// An ASCII hostname can only be non-IDN or punycode-encoded.
154
+
if ( $is_ascii ) {
155
+
// If the hostname is in punycode encoding, add the decoded version to the list of hosts.
156
+
if ( 0 === strpos( $hostname, self::PUNYCODE_PREFIX ) || false !== strpos( $hostname, '.' . self::PUNYCODE_PREFIX ) ) {
157
+
// Ignoring phpcs here, and not passing the variant so that the correct default can be selected by PHP based on the
158
+
// version. INTL_IDNA_VARIANT_UTS46 for PHP>=7.4, INTL_IDNA_VARIANT_2003 for PHP<7.4.
159
+
// phpcs:ignore PHPCompatibility.ParameterValues.NewIDNVariantDefault.NotSet
160
+
$host_decoded = idn_to_utf8( $hostname );
161
+
array_push( $hosts, $host_decoded, "www.$host_decoded" );
162
+
}
163
+
} else {
164
+
// If it's not ASCII, then add the punycode encoded version.
165
+
// Ignoring phpcs here, and not passing the variant so that the correct default can be selected by PHP based on the
166
+
// version. INTL_IDNA_VARIANT_UTS46 for PHP>=7.4, INTL_IDNA_VARIANT_2003 for PHP<7.4.
167
+
// phpcs:ignore PHPCompatibility.ParameterValues.NewIDNVariantDefault.NotSet
168
+
$host_encoded = idn_to_ascii( $hostname );
169
+
array_push( $hosts, $host_encoded, "www.$host_encoded" );
170
+
}
171
+
} catch ( Exception $exception ) { // phpcs:ignore Generic.CodeAnalysis.EmptyStatement.DetectedCatch
172
+
// Do nothing.
173
+
}
174
+
175
+
return $hosts;
176
+
}
177
+
}
178
+