| 1 |
<?php |
| 2 |
|
| 3 |
namespace MediaWiki\Utils; |
| 4 |
|
| 5 |
use BadMethodCallException; |
| 6 |
use InvalidArgumentException; |
| 7 |
use MediaWiki\Debug\MWDebug; |
| 8 |
use MediaWiki\MainConfigSchema; |
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
class UrlUtils { |
| 17 |
public const SERVER = 'server'; |
| 18 |
public const CANONICAL_SERVER = 'canonicalServer'; |
| 19 |
public const INTERNAL_SERVER = 'internalServer'; |
| 20 |
public const FALLBACK_PROTOCOL = 'fallbackProtocol'; |
| 21 |
public const HTTPS_PORT = 'httpsPort'; |
| 22 |
public const VALID_PROTOCOLS = 'validProtocols'; |
| 23 |
|
| 24 |
|
| 25 |
private $server = null; |
| 26 |
|
| 27 |
|
| 28 |
private $canonicalServer = null; |
| 29 |
|
| 30 |
|
| 31 |
private $internalServer = null; |
| 32 |
|
| 33 |
private $fallbackProtocol = 'http'; |
| 34 |
|
| 35 |
|
| 36 |
private $httpsPort = 443; |
| 37 |
|
| 38 |
|
| 39 |
private $validProtocols = MainConfigSchema::UrlProtocols['default']; |
| 40 |
|
| 41 |
|
| 42 |
private $validProtocolsCache = null; |
| 43 |
|
| 44 |
|
| 45 |
private $validAbsoluteProtocolsCache = null; |
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
public function __construct( array $options = [] ) { |
| 69 |
foreach ( $options as $key => $value ) { |
| 70 |
switch ( $key ) { |
| 71 |
case self::SERVER: |
| 72 |
case self::CANONICAL_SERVER: |
| 73 |
case self::INTERNAL_SERVER: |
| 74 |
case self::FALLBACK_PROTOCOL: |
| 75 |
case self::HTTPS_PORT: |
| 76 |
case self::VALID_PROTOCOLS: |
| 77 |
$this->$key = $value; |
| 78 |
break; |
| 79 |
|
| 80 |
default: |
| 81 |
throw new InvalidArgumentException( "Unrecognized option \"$key\"" ); |
| 82 |
} |
| 83 |
} |
| 84 |
|
| 85 |
if ( $this->server !== null ) { |
| 86 |
if ( $this->canonicalServer === null || $this->canonicalServer === false ) { |
| 87 |
$this->canonicalServer = $this->expand( $this->server, PROTO_HTTP ); |
| 88 |
} |
| 89 |
if ( $this->internalServer === null || $this->internalServer === false ) { |
| 90 |
$this->internalServer = $this->server; |
| 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 |
public function expand( string $url, $defaultProto = PROTO_FALLBACK ): ?string { |
| 125 |
if ( $defaultProto === PROTO_CANONICAL ) { |
| 126 |
$serverUrl = $this->canonicalServer; |
| 127 |
} elseif ( $defaultProto === PROTO_INTERNAL ) { |
| 128 |
$serverUrl = $this->internalServer; |
| 129 |
} else { |
| 130 |
$serverUrl = $this->server; |
| 131 |
if ( $defaultProto === PROTO_FALLBACK ) { |
| 132 |
$defaultProto = $this->fallbackProtocol . '://'; |
| 133 |
} |
| 134 |
} |
| 135 |
|
| 136 |
if ( str_starts_with( $url, '/' ) ) { |
| 137 |
if ( $serverUrl === null ) { |
| 138 |
throw new BadMethodCallException( 'Cannot call expand() if the appropriate ' . |
| 139 |
'SERVER/CANONICAL_SERVER/INTERNAL_SERVER option was not passed to the ' . |
| 140 |
'constructor' ); |
| 141 |
} |
| 142 |
|
| 143 |
|
| 144 |
$bits = $this->parse( $serverUrl ); |
| 145 |
$serverProto = $bits && $bits['scheme'] != '' ? $bits['scheme'] . '://' : null; |
| 146 |
|
| 147 |
if ( $defaultProto === PROTO_CANONICAL || $defaultProto === PROTO_INTERNAL ) { |
| 148 |
|
| 149 |
|
| 150 |
$defaultProto = $serverProto ?? PROTO_HTTP; |
| 151 |
} |
| 152 |
|
| 153 |
|
| 154 |
$defaultProtoWithoutSlashes = $defaultProto === PROTO_FALLBACK ? '' : substr( $defaultProto, 0, -2 ); |
| 155 |
|
| 156 |
if ( str_starts_with( $url, '//' ) ) { |
| 157 |
$url = $defaultProtoWithoutSlashes . $url; |
| 158 |
} else { |
| 159 |
|
| 160 |
|
| 161 |
if ( $serverProto ) { |
| 162 |
$url = $serverUrl . $url; |
| 163 |
} else { |
| 164 |
|
| 165 |
|
| 166 |
if ( $defaultProto === PROTO_HTTPS && $this->httpsPort != 443 ) { |
| 167 |
if ( isset( $bits['port'] ) ) { |
| 168 |
throw new InvalidArgumentException( |
| 169 |
'A protocol-relative server may not contain a port number' ); |
| 170 |
} |
| 171 |
$url = "$defaultProtoWithoutSlashes$serverUrl:{$this->httpsPort}$url"; |
| 172 |
} else { |
| 173 |
$url = "$defaultProtoWithoutSlashes$serverUrl$url"; |
| 174 |
} |
| 175 |
} |
| 176 |
} |
| 177 |
} |
| 178 |
|
| 179 |
$bits = $this->parse( $url ); |
| 180 |
|
| 181 |
if ( $bits && isset( $bits['path'] ) ) { |
| 182 |
$bits['path'] = self::removeDotSegments( $bits['path'] ); |
| 183 |
return self::assemble( $bits ); |
| 184 |
} elseif ( $bits ) { |
| 185 |
|
| 186 |
return $url; |
| 187 |
} elseif ( !str_starts_with( $url, '/' ) ) { |
| 188 |
|
| 189 |
return self::removeDotSegments( $url ); |
| 190 |
} |
| 191 |
|
| 192 |
|
| 193 |
return null; |
| 194 |
} |
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
public function getServer( $proto ): ?string { |
| 205 |
$url = $this->expand( '/', $proto ); |
| 206 |
if ( $url === null ) { |
| 207 |
return null; |
| 208 |
} |
| 209 |
return substr( $url, 0, -1 ); |
| 210 |
} |
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
public function getCanonicalServer(): string { |
| 218 |
|
| 219 |
return $this->canonicalServer; |
| 220 |
} |
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
public static function assemble( array $urlParts ): string { |
| 234 |
$result = ''; |
| 235 |
|
| 236 |
if ( isset( $urlParts['delimiter'] ) ) { |
| 237 |
if ( isset( $urlParts['scheme'] ) ) { |
| 238 |
$result .= $urlParts['scheme']; |
| 239 |
} |
| 240 |
|
| 241 |
$result .= $urlParts['delimiter']; |
| 242 |
} |
| 243 |
|
| 244 |
if ( isset( $urlParts['host'] ) ) { |
| 245 |
if ( isset( $urlParts['user'] ) ) { |
| 246 |
$result .= $urlParts['user']; |
| 247 |
if ( isset( $urlParts['pass'] ) ) { |
| 248 |
$result .= ':' . $urlParts['pass']; |
| 249 |
} |
| 250 |
$result .= '@'; |
| 251 |
} |
| 252 |
|
| 253 |
$result .= $urlParts['host']; |
| 254 |
|
| 255 |
if ( isset( $urlParts['port'] ) ) { |
| 256 |
$result .= ':' . $urlParts['port']; |
| 257 |
} |
| 258 |
} |
| 259 |
|
| 260 |
if ( isset( $urlParts['path'] ) ) { |
| 261 |
$result .= $urlParts['path']; |
| 262 |
} |
| 263 |
|
| 264 |
if ( isset( $urlParts['query'] ) && $urlParts['query'] !== '' ) { |
| 265 |
$result .= '?' . $urlParts['query']; |
| 266 |
} |
| 267 |
|
| 268 |
if ( isset( $urlParts['fragment'] ) ) { |
| 269 |
$result .= '#' . $urlParts['fragment']; |
| 270 |
} |
| 271 |
|
| 272 |
return $result; |
| 273 |
} |
| 274 |
|
| 275 |
|
| 276 |
|
| 277 |
|
| 278 |
|
| 279 |
|
| 280 |
|
| 281 |
|
| 282 |
|
| 283 |
public static function removeDotSegments( string $urlPath ): string { |
| 284 |
$output = ''; |
| 285 |
$inputOffset = 0; |
| 286 |
$inputLength = strlen( $urlPath ); |
| 287 |
|
| 288 |
while ( $inputOffset < $inputLength ) { |
| 289 |
$trimOutput = false; |
| 290 |
if ( substr_compare( $urlPath, './', $inputOffset, 2 ) === 0 ) { |
| 291 |
|
| 292 |
$inputOffset += 2; |
| 293 |
} elseif ( substr_compare( $urlPath, '../', $inputOffset, 3 ) === 0 ) { |
| 294 |
|
| 295 |
$inputOffset += 3; |
| 296 |
} elseif ( $inputOffset + 2 === $inputLength && str_ends_with( $urlPath, '/.' ) ) { |
| 297 |
|
| 298 |
$inputOffset += 1; |
| 299 |
$urlPath[$inputOffset] = '/'; |
| 300 |
} elseif ( substr_compare( $urlPath, '/./', $inputOffset, 3 ) === 0 ) { |
| 301 |
|
| 302 |
$inputOffset += 2; |
| 303 |
} elseif ( $inputOffset + 3 === $inputLength && str_ends_with( $urlPath, '/..' ) ) { |
| 304 |
|
| 305 |
|
| 306 |
$inputOffset += 2; |
| 307 |
$urlPath[$inputOffset] = '/'; |
| 308 |
$trimOutput = true; |
| 309 |
} elseif ( substr_compare( $urlPath, '/../', $inputOffset, 4 ) === 0 ) { |
| 310 |
|
| 311 |
|
| 312 |
$inputOffset += 3; |
| 313 |
$trimOutput = true; |
| 314 |
} elseif ( $inputOffset + 1 === $inputLength && str_ends_with( $urlPath, '.' ) ) { |
| 315 |
|
| 316 |
$inputOffset += 1; |
| 317 |
} elseif ( $inputOffset + 2 === $inputLength && str_ends_with( $urlPath, '..' ) ) { |
| 318 |
|
| 319 |
$inputOffset += 2; |
| 320 |
} else { |
| 321 |
|
| 322 |
if ( $urlPath[$inputOffset] === '/' ) { |
| 323 |
$slashPos = strpos( $urlPath, '/', $inputOffset + 1 ); |
| 324 |
} else { |
| 325 |
$slashPos = strpos( $urlPath, '/', $inputOffset ); |
| 326 |
} |
| 327 |
if ( $slashPos === false ) { |
| 328 |
$output .= substr( $urlPath, $inputOffset ); |
| 329 |
$inputOffset = $inputLength; |
| 330 |
} else { |
| 331 |
$output .= substr( $urlPath, $inputOffset, $slashPos - $inputOffset ); |
| 332 |
$inputOffset += $slashPos - $inputOffset; |
| 333 |
} |
| 334 |
} |
| 335 |
|
| 336 |
if ( $trimOutput ) { |
| 337 |
$slashPos = strrpos( $output, '/' ); |
| 338 |
if ( $slashPos === false ) { |
| 339 |
$output = ''; |
| 340 |
} else { |
| 341 |
$output = substr( $output, 0, $slashPos ); |
| 342 |
} |
| 343 |
} |
| 344 |
} |
| 345 |
|
| 346 |
return $output; |
| 347 |
} |
| 348 |
|
| 349 |
|
| 350 |
|
| 351 |
|
| 352 |
|
| 353 |
|
| 354 |
public function validProtocols(): string { |
| 355 |
$this->validProtocolsCache ??= $this->validProtocolsInternal( true ); |
| 356 |
return $this->validProtocolsCache; |
| 357 |
} |
| 358 |
|
| 359 |
|
| 360 |
|
| 361 |
|
| 362 |
|
| 363 |
|
| 364 |
|
| 365 |
public function validAbsoluteProtocols(): string { |
| 366 |
$this->validAbsoluteProtocolsCache ??= $this->validProtocolsInternal( false ); |
| 367 |
return $this->validAbsoluteProtocolsCache; |
| 368 |
} |
| 369 |
|
| 370 |
|
| 371 |
|
| 372 |
|
| 373 |
|
| 374 |
|
| 375 |
|
| 376 |
private function validProtocolsInternal( bool $includeProtocolRelative ): string { |
| 377 |
if ( !is_array( $this->validProtocols ) ) { |
| 378 |
MWDebug::deprecated( '$wgUrlProtocols that is not an array', '1.39' ); |
| 379 |
return (string)$this->validProtocols; |
| 380 |
} |
| 381 |
|
| 382 |
$protocols = []; |
| 383 |
foreach ( $this->validProtocols as $protocol ) { |
| 384 |
|
| 385 |
if ( $includeProtocolRelative || $protocol !== '//' ) { |
| 386 |
$protocols[] = preg_quote( $protocol, '/' ); |
| 387 |
} |
| 388 |
} |
| 389 |
|
| 390 |
return implode( '|', $protocols ); |
| 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 |
public function parse( string $url ): ?array { |
| 422 |
|
| 423 |
|
| 424 |
$wasRelative = str_starts_with( $url, '//' ); |
| 425 |
if ( $wasRelative ) { |
| 426 |
$url = "http:$url"; |
| 427 |
} |
| 428 |
$bits = parse_url( $url ); |
| 429 |
|
| 430 |
|
| 431 |
if ( !$bits || !isset( $bits['scheme'] ) ) { |
| 432 |
return null; |
| 433 |
} |
| 434 |
|
| 435 |
|
| 436 |
$bits['scheme'] = strtolower( $bits['scheme'] ); |
| 437 |
$bits['host'] ??= ''; |
| 438 |
|
| 439 |
|
| 440 |
if ( in_array( $bits['scheme'] . '://', $this->validProtocols ) ) { |
| 441 |
$bits['delimiter'] = '://'; |
| 442 |
} elseif ( in_array( $bits['scheme'] . ':', $this->validProtocols ) ) { |
| 443 |
$bits['delimiter'] = ':'; |
| 444 |
} else { |
| 445 |
return null; |
| 446 |
} |
| 447 |
|
| 448 |
|
| 449 |
if ( $bits['scheme'] === 'file' && isset( $bits['path'] ) && !str_starts_with( $bits['path'], '/' ) ) { |
| 450 |
$bits['path'] = '/' . $bits['path']; |
| 451 |
} |
| 452 |
|
| 453 |
|
| 454 |
if ( $wasRelative ) { |
| 455 |
$bits['scheme'] = ''; |
| 456 |
$bits['delimiter'] = '//'; |
| 457 |
} |
| 458 |
return $bits; |
| 459 |
} |
| 460 |
|
| 461 |
|
| 462 |
|
| 463 |
|
| 464 |
|
| 465 |
|
| 466 |
|
| 467 |
|
| 468 |
|
| 469 |
|
| 470 |
|
| 471 |
|
| 472 |
public function expandIRI( string $url ): ?string { |
| 473 |
$expanded = $this->expand( $url ); |
| 474 |
if ( $expanded === null ) { |
| 475 |
return null; |
| 476 |
} |
| 477 |
return preg_replace_callback( |
| 478 |
'/(?:%[89A-F][0-9A-F])+/i', |
| 479 |
static fn ( $m ) => urldecode( $m[0] ), |
| 480 |
$expanded |
| 481 |
); |
| 482 |
} |
| 483 |
|
| 484 |
|
| 485 |
|
| 486 |
|
| 487 |
|
| 488 |
|
| 489 |
|
| 490 |
|
| 491 |
|
| 492 |
public function matchesDomainList( string $url, array $domains ): bool { |
| 493 |
$bits = $this->parse( $url ); |
| 494 |
if ( is_array( $bits ) && isset( $bits['host'] ) ) { |
| 495 |
$host = '.' . $bits['host']; |
| 496 |
foreach ( $domains as $domain ) { |
| 497 |
$domain = '.' . $domain; |
| 498 |
if ( str_ends_with( $host, $domain ) ) { |
| 499 |
return true; |
| 500 |
} |
| 501 |
} |
| 502 |
} |
| 503 |
return false; |
| 504 |
} |
| 505 |
} |