File: UrlUtils.php

package info (click to toggle)
mediawiki 1%3A1.43.3%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 417,464 kB
  • sloc: php: 1,062,949; javascript: 664,290; sql: 9,714; python: 5,458; xml: 3,489; sh: 1,131; makefile: 64
file content (505 lines) | stat: -rw-r--r-- 17,872 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
<?php

namespace MediaWiki\Utils;

use BadMethodCallException;
use InvalidArgumentException;
use MediaWiki\Debug\MWDebug;
use MediaWiki\MainConfigSchema;

/**
 * A service to expand, parse, and otherwise manipulate URLs.
 *
 * @since 1.39
 * @newable
 */
class UrlUtils {
	public const SERVER = 'server';
	public const CANONICAL_SERVER = 'canonicalServer';
	public const INTERNAL_SERVER = 'internalServer';
	public const FALLBACK_PROTOCOL = 'fallbackProtocol';
	public const HTTPS_PORT = 'httpsPort';
	public const VALID_PROTOCOLS = 'validProtocols';

	/** @var ?string */
	private $server = null;

	/** @var ?string */
	private $canonicalServer = null;

	/** @var ?string */
	private $internalServer = null;
	/** @var string */
	private $fallbackProtocol = 'http';

	/** @var int */
	private $httpsPort = 443;

	/** @var array */
	private $validProtocols = MainConfigSchema::UrlProtocols['default'];

	/** @var ?string */
	private $validProtocolsCache = null;

	/** @var ?string */
	private $validAbsoluteProtocolsCache = null;

	/**
	 * @stable to call
	 * @param array $options All keys are optional, but if you omit SERVER then calling expand()
	 *   (and getServer(), expandIRI(), and matchesDomainList()) will throw. Recognized keys:
	 *     * self::SERVER: The protocol and server portion of the URLs to expand, with no other parts
	 *       (port, path, etc.). Example: 'https://example.com'. Protocol-relative URLs are
	 *       allowed.
	 *     * self::CANONICAL_SERVER: If SERVER is protocol-relative, this can be set to a
	 *       fully-qualified version for use when PROTO_CANONICAL is passed to expand(). Defaults
	 *       to SERVER, with 'http:' prepended if SERVER is protocol-relative.
	 *     * self::INTERNAL_SERVER: An alternative to SERVER that's used when PROTO_INTERNAL is
	 *       passed to expand(). It's intended for sites that have a different server name exposed
	 *       to CDNs. Defaults to SERVER.
	 *     * self::FALLBACK_PROTOCOL: Used by expand() when no $defaultProto parameter is provided.
	 *       Defaults to 'http'. The instance created by ServiceWiring sets this to 'https' if the
	 *       current request is detected to be via HTTPS, and 'http' otherwise.
	 *     * self::HTTPS_PORT: Defaults to 443. Used when a protocol-relative URL is expanded to
	 *       https.
	 *     * self::VALID_PROTOCOLS: An array of recognized URL protocols. The default can be found
	 *       in MainConfigSchema::UrlProtocols['default'].
	 */
	public function __construct( array $options = [] ) {
		foreach ( $options as $key => $value ) {
			switch ( $key ) {
				case self::SERVER:
				case self::CANONICAL_SERVER:
				case self::INTERNAL_SERVER:
				case self::FALLBACK_PROTOCOL:
				case self::HTTPS_PORT:
				case self::VALID_PROTOCOLS:
					$this->$key = $value;
					break;

				default:
					throw new InvalidArgumentException( "Unrecognized option \"$key\"" );
			}
		}

		if ( $this->server !== null ) {
			if ( $this->canonicalServer === null || $this->canonicalServer === false ) {
				$this->canonicalServer = $this->expand( $this->server, PROTO_HTTP );
			}
			if ( $this->internalServer === null || $this->internalServer === false ) {
				$this->internalServer = $this->server;
			}
		}
	}

	/**
	 * Expand a potentially local URL to a fully-qualified URL using $wgServer
	 * (or one of its alternatives).
	 *
	 * The meaning of the PROTO_* constants is as follows:
	 * PROTO_HTTP: Output a URL starting with http://
	 * PROTO_HTTPS: Output a URL starting with https://
	 * PROTO_RELATIVE: Output a URL starting with // (protocol-relative URL)
	 * PROTO_FALLBACK: Output a URL starting with the FALLBACK_PROTOCOL option
	 * PROTO_CURRENT: Legacy alias for PROTO_FALLBACK
	 * PROTO_CANONICAL: For URLs without a domain, like /w/index.php, use CANONICAL_SERVER.  For
	 *   protocol-relative URLs, use the protocol of CANONICAL_SERVER
	 * PROTO_INTERNAL: Like PROTO_CANONICAL, but uses INTERNAL_SERVER instead of CANONICAL_SERVER
	 *
	 * If $url specifies a protocol, or $url is domain-relative and $wgServer
	 * specifies a protocol, PROTO_HTTP, PROTO_HTTPS, PROTO_RELATIVE and
	 * PROTO_CURRENT do not change that.
	 *
	 * Parent references (/../) in the path are resolved (as in ::removeDotSegments).
	 *
	 * @todo this won't work with current-path-relative URLs like "subdir/foo.html", etc.
	 *
	 * @throws BadMethodCallException if no server was passed to the constructor
	 * @param string $url An URL; can be absolute (e.g. http://example.com/foo/bar),
	 *    protocol-relative (//example.com/foo/bar) or domain-relative (/foo/bar).
	 * @param string|int|null $defaultProto One of the PROTO_* constants, as described above.
	 * @return ?string Fully-qualified URL, current-path-relative URL or null if
	 *    no valid URL can be constructed
	 */
	public function expand( string $url, $defaultProto = PROTO_FALLBACK ): ?string {
		if ( $defaultProto === PROTO_CANONICAL ) {
			$serverUrl = $this->canonicalServer;
		} elseif ( $defaultProto === PROTO_INTERNAL ) {
			$serverUrl = $this->internalServer;
		} else {
			$serverUrl = $this->server;
			if ( $defaultProto === PROTO_FALLBACK ) {
				$defaultProto = $this->fallbackProtocol . '://';
			}
		}

		if ( str_starts_with( $url, '/' ) ) {
			if ( $serverUrl === null ) {
				throw new BadMethodCallException( 'Cannot call expand() if the appropriate ' .
					'SERVER/CANONICAL_SERVER/INTERNAL_SERVER option was not passed to the ' .
					'constructor' );
			}

			// Analyze $serverUrl to obtain its protocol
			$bits = $this->parse( $serverUrl );
			$serverProto = $bits && $bits['scheme'] != '' ? $bits['scheme'] . '://' : null;

			if ( $defaultProto === PROTO_CANONICAL || $defaultProto === PROTO_INTERNAL ) {
				// Fall back to HTTP in the ridiculous case that CanonicalServer or InternalServer
				// doesn't have a protocol
				$defaultProto = $serverProto ?? PROTO_HTTP;
			}

			// @phan-suppress-next-line PhanTypeMismatchArgumentNullableInternal T308355
			$defaultProtoWithoutSlashes = $defaultProto === PROTO_FALLBACK ? '' : substr( $defaultProto, 0, -2 );

			if ( str_starts_with( $url, '//' ) ) {
				$url = $defaultProtoWithoutSlashes . $url;
			} else {
				// If $serverUrl is protocol-relative, prepend $defaultProtoWithoutSlashes,
				// otherwise leave it alone.
				if ( $serverProto ) {
					$url = $serverUrl . $url;
				} else {
					// If an HTTPS URL is synthesized from a protocol-relative Server, allow the
					// user to override the port number (T67184)
					if ( $defaultProto === PROTO_HTTPS && $this->httpsPort != 443 ) {
						if ( isset( $bits['port'] ) ) {
							throw new InvalidArgumentException(
								'A protocol-relative server may not contain a port number' );
						}
						$url = "$defaultProtoWithoutSlashes$serverUrl:{$this->httpsPort}$url";
					} else {
						$url = "$defaultProtoWithoutSlashes$serverUrl$url";
					}
				}
			}
		}

		$bits = $this->parse( $url );

		if ( $bits && isset( $bits['path'] ) ) {
			$bits['path'] = self::removeDotSegments( $bits['path'] );
			return self::assemble( $bits );
		} elseif ( $bits ) {
			# No path to expand
			return $url;
		} elseif ( !str_starts_with( $url, '/' ) ) {
			# URL is a relative path
			return self::removeDotSegments( $url );
		}

		# Expanded URL is not valid.
		return null;
	}

	/**
	 * Get the wiki's "server", i.e. the protocol and host part of the URL, with a
	 * protocol specified using a PROTO_* constant as in expand()
	 *
	 * @throws BadMethodCallException if no server was passed to the constructor
	 * @param string|int|null $proto One of the PROTO_* constants.
	 * @return ?string The URL, or null on failure
	 */
	public function getServer( $proto ): ?string {
		$url = $this->expand( '/', $proto );
		if ( $url === null ) {
			return null;
		}
		return substr( $url, 0, -1 );
	}

	/**
	 * Get the canonical server, i.e. the canonical protocol and host part of
	 * the wiki's URL.
	 * @return string
	 */
	public function getCanonicalServer(): string {
		// @phan-suppress-next-line PhanTypeMismatchReturnNullable -- throw if unconfigured
		return $this->canonicalServer;
	}

	/**
	 * This function will reassemble a URL parsed with parse().  This is useful if you need to edit
	 * part of a URL and put it back together.
	 *
	 * This is the basic structure used (brackets contain keys for $urlParts):
	 * [scheme][delimiter][user]:[pass]@[host]:[port][path]?[query]#[fragment]
	 *
	 * @since 1.41
	 * @param array $urlParts URL parts, as output from parse()
	 * @return string URL assembled from its component parts
	 */
	public static function assemble( array $urlParts ): string {
		$result = '';

		if ( isset( $urlParts['delimiter'] ) ) {
			if ( isset( $urlParts['scheme'] ) ) {
				$result .= $urlParts['scheme'];
			}

			$result .= $urlParts['delimiter'];
		}

		if ( isset( $urlParts['host'] ) ) {
			if ( isset( $urlParts['user'] ) ) {
				$result .= $urlParts['user'];
				if ( isset( $urlParts['pass'] ) ) {
					$result .= ':' . $urlParts['pass'];
				}
				$result .= '@';
			}

			$result .= $urlParts['host'];

			if ( isset( $urlParts['port'] ) ) {
				$result .= ':' . $urlParts['port'];
			}
		}

		if ( isset( $urlParts['path'] ) ) {
			$result .= $urlParts['path'];
		}

		if ( isset( $urlParts['query'] ) && $urlParts['query'] !== '' ) {
			$result .= '?' . $urlParts['query'];
		}

		if ( isset( $urlParts['fragment'] ) ) {
			$result .= '#' . $urlParts['fragment'];
		}

		return $result;
	}

	/**
	 * Remove all dot-segments in the provided URL path.  For example, '/a/./b/../c/' becomes
	 * '/a/c/'.  For details on the algorithm, please see RFC3986 section 5.2.4.
	 *
	 * @since 1.41
	 * @param string $urlPath URL path, potentially containing dot-segments
	 * @return string URL path with all dot-segments removed
	 */
	public static function removeDotSegments( string $urlPath ): string {
		$output = '';
		$inputOffset = 0;
		$inputLength = strlen( $urlPath );

		while ( $inputOffset < $inputLength ) {
			$trimOutput = false;
			if ( substr_compare( $urlPath, './', $inputOffset, 2 ) === 0 ) {
				# Step A, remove leading "./"
				$inputOffset += 2;
			} elseif ( substr_compare( $urlPath, '../', $inputOffset, 3 ) === 0 ) {
				# Step A, remove leading "../"
				$inputOffset += 3;
			} elseif ( $inputOffset + 2 === $inputLength && str_ends_with( $urlPath, '/.' ) ) {
				# Step B, replace leading "/.$" with "/"
				$inputOffset += 1;
				$urlPath[$inputOffset] = '/';
			} elseif ( substr_compare( $urlPath, '/./', $inputOffset, 3 ) === 0 ) {
				# Step B, replace leading "/./" with "/"
				$inputOffset += 2;
			} elseif ( $inputOffset + 3 === $inputLength && str_ends_with( $urlPath, '/..' ) ) {
				# Step C, replace leading "/..$" with "/" and
				# remove last path component in output
				$inputOffset += 2;
				$urlPath[$inputOffset] = '/';
				$trimOutput = true;
			} elseif ( substr_compare( $urlPath, '/../', $inputOffset, 4 ) === 0 ) {
				# Step C, replace leading "/../" with "/" and
				# remove last path component in output
				$inputOffset += 3;
				$trimOutput = true;
			} elseif ( $inputOffset + 1 === $inputLength && str_ends_with( $urlPath, '.' ) ) {
				# Step D, remove "^.$"
				$inputOffset += 1;
			} elseif ( $inputOffset + 2 === $inputLength && str_ends_with( $urlPath, '..' ) ) {
				# Step D, remove "^..$"
				$inputOffset += 2;
			} else {
				# Step E, move leading path segment to output
				if ( $urlPath[$inputOffset] === '/' ) {
					$slashPos = strpos( $urlPath, '/', $inputOffset + 1 );
				} else {
					$slashPos = strpos( $urlPath, '/', $inputOffset );
				}
				if ( $slashPos === false ) {
					$output .= substr( $urlPath, $inputOffset );
					$inputOffset = $inputLength;
				} else {
					$output .= substr( $urlPath, $inputOffset, $slashPos - $inputOffset );
					$inputOffset += $slashPos - $inputOffset;
				}
			}

			if ( $trimOutput ) {
				$slashPos = strrpos( $output, '/' );
				if ( $slashPos === false ) {
					$output = '';
				} else {
					$output = substr( $output, 0, $slashPos );
				}
			}
		}

		return $output;
	}

	/**
	 * Returns a partial regular expression of recognized URL protocols, e.g. "http:\/\/|https:\/\/"
	 *
	 * @return string
	 */
	public function validProtocols(): string {
		$this->validProtocolsCache ??= $this->validProtocolsInternal( true );
		return $this->validProtocolsCache;
	}

	/**
	 * Like validProtocols(), but excludes '//' from the protocol list. Use this if you need a
	 * regex that matches all URL protocols but does not match protocol-relative URLs
	 *
	 * @return string
	 */
	public function validAbsoluteProtocols(): string {
		$this->validAbsoluteProtocolsCache ??= $this->validProtocolsInternal( false );
		return $this->validAbsoluteProtocolsCache;
	}

	/**
	 * Returns a partial regular expression of URL protocols, e.g. "http:\/\/|https:\/\/"
	 *
	 * @param bool $includeProtocolRelative If false, remove '//' from the returned protocol list.
	 * @return string
	 */
	private function validProtocolsInternal( bool $includeProtocolRelative ): string {
		if ( !is_array( $this->validProtocols ) ) {
			MWDebug::deprecated( '$wgUrlProtocols that is not an array', '1.39' );
			return (string)$this->validProtocols;
		}

		$protocols = [];
		foreach ( $this->validProtocols as $protocol ) {
			// Filter out '//' if !$includeProtocolRelative
			if ( $includeProtocolRelative || $protocol !== '//' ) {
				$protocols[] = preg_quote( $protocol, '/' );
			}
		}

		return implode( '|', $protocols );
	}

	/**
	 * Advanced and configurable version of parse_url().
	 *
	 * 1) Add a "delimiter" element to the array, which helps permits to blindly re-assemble
	 *    any URL regardless of protocol, including those that don't use `://`,
	 *    such as "mailto:" and "news:".
	 * 2) Reject URLs with protocols not in $wgUrlProtocols.
	 * 3) Reject relative or incomplete URLs that parse_url would return a partial array for.
	 *
	 * If all you need is to extract parts of an HTTP or HTTPS URL (i.e. not specific to
	 * site-configurable extra protocols, or user input) then `parse_url()` can be used
	 * directly instead.
	 *
	 * @param string $url A URL to parse
	 * @return ?string[] Bits of the URL in an associative array, or null on failure.
	 *   Possible fields:
	 *   - scheme: URI scheme (protocol), e.g. 'http', 'mailto'. Lowercase, always present, but can
	 *       be an empty string for protocol-relative URLs.
	 *   - delimiter: either '://', ':' or '//'. Always present.
	 *   - host: domain name / IP. Always present, but could be an empty string, e.g. for file: URLs.
	 *   - port: port number. Will be missing when port is not explicitly specified.
	 *   - user: user name, e.g. for HTTP Basic auth URLs such as http://user:pass@example.com/
	 *       Missing when there is no username.
	 *   - pass: password, same as above.
	 *   - path: path including the leading /. Will be missing when empty (e.g. 'http://example.com')
	 *   - query: query string (as a string; see wfCgiToArray() for parsing it), can be missing.
	 *   - fragment: the part after #, can be missing.
	 */
	public function parse( string $url ): ?array {
		// Protocol-relative URLs are handled really badly by parse_url(). It's so bad that the
		// easiest way to handle them is to just prepend 'http:' and strip the protocol out later.
		$wasRelative = str_starts_with( $url, '//' );
		if ( $wasRelative ) {
			$url = "http:$url";
		}
		$bits = parse_url( $url );
		// parse_url() returns an array without scheme for invalid URLs, e.g.
		// parse_url("something bad://example") == [ 'path' => 'something bad://example' ]
		if ( !$bits || !isset( $bits['scheme'] ) ) {
			return null;
		}

		// parse_url() incorrectly handles schemes case-sensitively. Convert it to lowercase.
		$bits['scheme'] = strtolower( $bits['scheme'] );
		$bits['host'] ??= '';

		// most of the protocols are followed by ://, but mailto: and sometimes news: not, check for it
		if ( in_array( $bits['scheme'] . '://', $this->validProtocols ) ) {
			$bits['delimiter'] = '://';
		} elseif ( in_array( $bits['scheme'] . ':', $this->validProtocols ) ) {
			$bits['delimiter'] = ':';
		} else {
			return null;
		}

		/* parse_url loses the third / for file:///c:/ urls */
		if ( $bits['scheme'] === 'file' && isset( $bits['path'] ) && !str_starts_with( $bits['path'], '/' ) ) {
			$bits['path'] = '/' . $bits['path'];
		}

		// If the URL was protocol-relative, fix scheme and delimiter
		if ( $wasRelative ) {
			$bits['scheme'] = '';
			$bits['delimiter'] = '//';
		}
		return $bits;
	}

	/**
	 * Take a URL, make sure it's expanded to fully qualified, and replace any encoded non-ASCII
	 * Unicode characters with their UTF-8 original forms for more compact display and legibility
	 * for local audiences.
	 *
	 * @todo handle punycode domains too
	 *
	 * @throws BadMethodCallException if no server was passed to the constructor
	 * @param string $url
	 * @return ?string
	 */
	public function expandIRI( string $url ): ?string {
		$expanded = $this->expand( $url );
		if ( $expanded === null ) {
			return null;
		}
		return preg_replace_callback(
			'/(?:%[89A-F][0-9A-F])+/i',
			static fn ( $m ) => urldecode( $m[0] ),
			$expanded
		);
	}

	/**
	 * Check whether a given URL has a domain that occurs in a given set of domains
	 *
	 * @throws BadMethodCallException if no server was passed to the constructor
	 * @param string $url
	 * @param array $domains Array of domains (strings)
	 * @return bool True if the host part of $url ends in one of the strings in $domains
	 */
	public function matchesDomainList( string $url, array $domains ): bool {
		$bits = $this->parse( $url );
		if ( is_array( $bits ) && isset( $bits['host'] ) ) {
			$host = '.' . $bits['host'];
			foreach ( $domains as $domain ) {
				$domain = '.' . $domain;
				if ( str_ends_with( $host, $domain ) ) {
					return true;
				}
			}
		}
		return false;
	}
}