Localisation updates from https://translatewiki.net.
[mediawiki.git] / includes / Request / WebRequest.php
blob07d1f4885601c286ea506dc41d419a9a5b277dc9
1 <?php
2 /**
3 * Deal with importing all those nasty globals and things
5 * Copyright © 2003 Brooke Vibber <bvibber@wikimedia.org>
6 * https://www.mediawiki.org/
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
21 * http://www.gnu.org/copyleft/gpl.html
23 * @file
26 namespace MediaWiki\Request;
28 use FatalError;
29 use MediaWiki\HookContainer\HookRunner;
30 use MediaWiki\Http\Telemetry;
31 use MediaWiki\MainConfigNames;
32 use MediaWiki\MediaWikiServices;
33 use MediaWiki\Session\Session;
34 use MediaWiki\Session\SessionId;
35 use MediaWiki\Session\SessionManager;
36 use MediaWiki\User\UserIdentity;
37 use MWException;
38 use Wikimedia\IPUtils;
40 // The point of this class is to be a wrapper around super globals
41 // phpcs:disable MediaWiki.Usage.SuperGlobalsUsage.SuperGlobals
43 /**
44 * The WebRequest class encapsulates getting at data passed in the
45 * URL or via a POSTed form, stripping illegal input characters, and
46 * normalizing Unicode sequences. This class should be used instead
47 * of accessing globals such as $_GET, $_POST, and $_COOKIE.
49 * @ingroup HTTP
51 class WebRequest {
52 /**
53 * The parameters from $_GET, $_POST and the path router
54 * @var array
56 protected $data;
58 /**
59 * The parameters from $_GET. The parameters from the path router are
60 * added by interpolateTitle() during Setup.php.
61 * @var (string|string[])[]
63 protected $queryAndPathParams;
65 /**
66 * The parameters from $_GET only.
67 * @var (string|string[])[]
69 protected $queryParams;
71 /**
72 * Lazy-initialized request headers indexed by upper-case header name
73 * @var string[]
75 protected $headers = [];
77 /**
78 * Flag to make WebRequest::getHeader return an array of values.
79 * @since 1.26
81 public const GETHEADER_LIST = 1;
83 /**
84 * Lazy-init response object
85 * @var WebResponse|null
87 protected ?WebResponse $response = null;
89 /**
90 * Cached client IP address
91 * @var string
93 private $ip;
95 /**
96 * The timestamp of the start of the request, with microsecond precision.
97 * @var float
99 protected $requestTime;
102 * Cached URL protocol
103 * @var string
105 protected $protocol;
108 * @var SessionId|null Session ID to use for this
109 * request. We can't save the session directly due to reference cycles not
110 * working too well (slow GC).
112 * TODO: Investigate whether this GC slowness concern (added in a73c5b7395 with regard to
113 * PHP 5.6) still applies in PHP 7.2+.
115 protected $sessionId = null;
117 /** @var bool Whether this HTTP request is "safe" (even if it is an HTTP post) */
118 protected $markedAsSafe = false;
121 * @codeCoverageIgnore
123 public function __construct() {
124 $this->requestTime = $_SERVER['REQUEST_TIME_FLOAT'];
126 // POST overrides GET data
127 // We don't use $_REQUEST here to avoid interference from cookies...
128 $this->data = $_POST + $_GET;
130 $this->queryAndPathParams = $this->queryParams = $_GET;
134 * Returns an entry from the $_SERVER array.
135 * This exists mainly to allow us to inject fake values for testing.
137 * @param string $name A well known key for $_SERVER,
138 * see <https://www.php.net/manual/en/reserved.variables.server.php>.
139 * Only fields that contain string values are supported,
140 * so 'argv' and 'argc' are not safe to use.
141 * @param ?string $default The value to return if no value is known for the
142 * key $name.
144 * @return ?string
146 protected function getServerInfo( string $name, ?string $default = null ): ?string {
147 return isset( $_SERVER[$name] ) ? (string)$_SERVER[$name] : $default;
151 * Extract relevant query arguments from the http request uri's path
152 * to be merged with the normal php provided query arguments.
153 * Tries to use the REQUEST_URI data if available and parses it
154 * according to the wiki's configuration looking for any known pattern.
156 * If the REQUEST_URI is not provided we'll fall back on the PATH_INFO
157 * provided by the server if any and use that to set a 'title' parameter.
159 * This internal method handles many odd cases and is tailored specifically for
160 * used by WebRequest::interpolateTitle, for index.php requests.
161 * Consider using WebRequest::getRequestPathSuffix for other path-related use cases.
163 * @param string $want If this is not 'all', then the function
164 * will return an empty array if it determines that the URL is
165 * inside a rewrite path.
167 * @return string[] Any query arguments found in path matches.
168 * @throws FatalError If invalid routes are configured (T48998)
170 protected function getPathInfo( $want = 'all' ) {
171 // PATH_INFO is mangled due to https://bugs.php.net/bug.php?id=31892
172 // And also by Apache 2.x, double slashes are converted to single slashes.
173 // So we will use REQUEST_URI if possible.
174 $url = $this->getServerInfo( 'REQUEST_URI' );
175 if ( $url !== null ) {
176 // Slurp out the path portion to examine...
177 if ( !preg_match( '!^https?://!', $url ) ) {
178 $url = 'http://unused' . $url;
180 $a = parse_url( $url );
181 if ( !$a ) {
182 return [];
184 $path = $a['path'] ?? '';
186 global $wgScript;
187 if ( $path == $wgScript && $want !== 'all' ) {
188 // Script inside a rewrite path?
189 // Abort to keep from breaking...
190 return [];
193 $router = new PathRouter;
195 // Raw PATH_INFO style
196 $router->add( "$wgScript/$1" );
198 global $wgArticlePath;
199 if ( $wgArticlePath ) {
200 $router->validateRoute( $wgArticlePath, 'wgArticlePath' );
201 $router->add( $wgArticlePath );
204 global $wgActionPaths;
205 $articlePaths = PathRouter::getActionPaths( $wgActionPaths, $wgArticlePath );
206 if ( $articlePaths ) {
207 $router->add( $articlePaths, [ 'action' => '$key' ] );
210 $services = MediaWikiServices::getInstance();
211 global $wgVariantArticlePath;
212 if ( $wgVariantArticlePath ) {
213 $router->validateRoute( $wgVariantArticlePath, 'wgVariantArticlePath' );
214 $router->add( $wgVariantArticlePath,
215 [ 'variant' => '$2' ],
216 [ '$2' => $services->getLanguageConverterFactory()
217 ->getLanguageConverter( $services->getContentLanguage() )
218 ->getVariants() ]
222 ( new HookRunner( $services->getHookContainer() ) )->onWebRequestPathInfoRouter( $router );
224 $matches = $router->parse( $path );
225 } else {
226 global $wgUsePathInfo;
227 $matches = [];
228 if ( $wgUsePathInfo ) {
229 $origPathInfo = $this->getServerInfo( 'ORIG_PATH_INFO' ) ?? '';
230 $pathInfo = $this->getServerInfo( 'PATH_INFO' ) ?? '';
231 if ( $origPathInfo !== '' ) {
232 // Mangled PATH_INFO
233 // https://bugs.php.net/bug.php?id=31892
234 // Also reported when ini_get('cgi.fix_pathinfo')==false
235 $matches['title'] = substr( $origPathInfo, 1 );
236 } elseif ( $pathInfo !== '' ) {
237 // Regular old PATH_INFO yay
238 $matches['title'] = substr( $pathInfo, 1 );
243 return $matches;
247 * If the request URL matches a given base path, extract the path part of
248 * the request URL after that base, and decode escape sequences in it.
250 * If the request URL does not match, false is returned.
252 * @since 1.35
253 * @param string $basePath The base URL path. Trailing slashes will be
254 * stripped.
255 * @param ?string $requestUrl The request URL to examine. If not given, the
256 * URL returned by getGlobalRequestURL() will be used.
257 * @return string|false
259 public static function getRequestPathSuffix( string $basePath, ?string $requestUrl = null ) {
260 $basePath = rtrim( $basePath, '/' ) . '/';
261 $requestUrl ??= self::getGlobalRequestURL();
262 $qpos = strpos( $requestUrl, '?' );
263 if ( $qpos !== false ) {
264 $requestPath = substr( $requestUrl, 0, $qpos );
265 } else {
266 $requestPath = $requestUrl;
268 if ( !str_starts_with( $requestPath, $basePath ) ) {
269 return false;
271 return rawurldecode( substr( $requestPath, strlen( $basePath ) ) );
275 * Work out an appropriate URL prefix containing scheme and host, based on
276 * information detected from $_SERVER
278 * @param bool|null $assumeProxiesUseDefaultProtocolPorts When the wiki is running behind a proxy
279 * and this is set to true, assumes that the proxy exposes the wiki on the standard ports
280 * (443 for https and 80 for http). Added in 1.38. Calls without this argument are
281 * supported for backwards compatibility but deprecated.
283 * @return string
285 public static function detectServer( $assumeProxiesUseDefaultProtocolPorts = null ) {
286 $assumeProxiesUseDefaultProtocolPorts ??= $GLOBALS['wgAssumeProxiesUseDefaultProtocolPorts'];
288 $proto = self::detectProtocol();
289 $stdPort = $proto === 'https' ? 443 : 80;
291 $varNames = [ 'HTTP_HOST', 'SERVER_NAME', 'HOSTNAME', 'SERVER_ADDR' ];
292 $host = 'localhost';
293 $port = $stdPort;
294 foreach ( $varNames as $varName ) {
295 if ( !isset( $_SERVER[$varName] ) ) {
296 continue;
299 $parts = IPUtils::splitHostAndPort( $_SERVER[$varName] );
300 if ( !$parts ) {
301 // Invalid, do not use
302 continue;
305 $host = $parts[0];
306 if ( $assumeProxiesUseDefaultProtocolPorts && isset( $_SERVER['HTTP_X_FORWARDED_PROTO'] ) ) {
307 // T72021: Assume that upstream proxy is running on the default
308 // port based on the protocol. We have no reliable way to determine
309 // the actual port in use upstream.
310 $port = $stdPort;
311 } elseif ( $parts[1] === false ) {
312 if ( isset( $_SERVER['SERVER_PORT'] ) ) {
313 $port = intval( $_SERVER['SERVER_PORT'] );
314 } // else leave it as $stdPort
315 } else {
316 $port = $parts[1];
318 break;
321 return $proto . '://' . IPUtils::combineHostAndPort( $host, $port, $stdPort );
325 * Detect the protocol from $_SERVER.
326 * This is for use prior to Setup.php, when no WebRequest object is available.
327 * At other times, use the non-static function getProtocol().
329 * @return string
331 public static function detectProtocol() {
332 if ( ( !empty( $_SERVER['HTTPS'] ) && $_SERVER['HTTPS'] !== 'off' ) ||
333 ( isset( $_SERVER['HTTP_X_FORWARDED_PROTO'] ) &&
334 $_SERVER['HTTP_X_FORWARDED_PROTO'] === 'https' ) ) {
335 return 'https';
336 } else {
337 return 'http';
342 * Get the number of seconds to have elapsed since request start,
343 * in fractional seconds, with microsecond resolution.
345 * @return float
346 * @since 1.25
348 public function getElapsedTime() {
349 return microtime( true ) - $this->requestTime;
353 * Get the current request ID.
355 * This is usually based on the `X-Request-Id` header, or the `UNIQUE_ID`
356 * environment variable, falling back to (process cached) randomly-generated string.
358 * @return string
359 * @since 1.27
361 public static function getRequestId() {
362 return Telemetry::getInstance()->getRequestId();
366 * Override the unique request ID. This is for sub-requests, such as jobs,
367 * that wish to use the same id but are not part of the same execution context.
369 * @param string|null $newId
370 * @since 1.27
372 public static function overrideRequestId( $newId ) {
373 $telemetry = Telemetry::getInstance();
374 if ( $newId === null ) {
375 $telemetry->regenerateRequestId();
376 } else {
377 $telemetry->overrideRequestId( $newId );
382 * Get the current URL protocol (http or https)
383 * @return string
385 public function getProtocol() {
386 $this->protocol ??= self::detectProtocol();
387 return $this->protocol;
391 * Check for title, action, and/or variant data in the URL
392 * and interpolate it into the GET variables.
393 * This should only be run after the content language is available,
394 * as we may need the list of language variants to determine
395 * available variant URLs.
397 public function interpolateTitle() {
398 $matches = $this->getPathInfo( 'title' );
399 foreach ( $matches as $key => $val ) {
400 $this->data[$key] = $this->queryAndPathParams[$key] = $val;
405 * URL rewriting function; tries to extract page title and,
406 * optionally, one other fixed parameter value from a URL path.
408 * @param string $path The URL path given from the client
409 * @param array $bases One or more URLs, optionally with $1 at the end
410 * @param string|false $key If provided, the matching key in $bases will be
411 * passed on as the value of this URL parameter
412 * @return array Array of URL variables to interpolate; empty if no match
414 public static function extractTitle( $path, $bases, $key = false ) {
415 foreach ( (array)$bases as $keyValue => $base ) {
416 // Find the part after $wgArticlePath
417 $base = str_replace( '$1', '', $base );
418 if ( str_starts_with( $path, $base ) ) {
419 $raw = substr( $path, strlen( $base ) );
420 if ( $raw !== '' ) {
421 $matches = [ 'title' => rawurldecode( $raw ) ];
422 if ( $key ) {
423 $matches[$key] = $keyValue;
425 return $matches;
429 return [];
433 * Recursively normalizes UTF-8 strings in the given array.
435 * @param string|array $data
436 * @return array|string Cleaned-up version of the given
437 * @internal
439 public function normalizeUnicode( $data ) {
440 if ( is_array( $data ) ) {
441 foreach ( $data as $key => $val ) {
442 $data[$key] = $this->normalizeUnicode( $val );
444 } else {
445 $contLang = MediaWikiServices::getInstance()->getContentLanguage();
446 $data = $contLang->normalize( $data );
448 return $data;
452 * Fetch a value from the given array or return $default if it's not set.
454 * @param array $arr
455 * @param string $name
456 * @param mixed $default
457 * @return mixed
458 * @return-taint tainted
460 private function getGPCVal( $arr, $name, $default ) {
461 # PHP is so nice to not touch input data, except sometimes:
462 # https://www.php.net/variables.external#language.variables.external.dot-in-names
463 # Work around PHP *feature* to avoid *bugs* elsewhere.
464 $name = strtr( $name, '.', '_' );
466 if ( !isset( $arr[$name] ) ) {
467 return $default;
470 $data = $arr[$name];
471 # Optimisation: Skip UTF-8 normalization and legacy transcoding for simple ASCII strings.
472 $isAsciiStr = ( is_string( $data ) && preg_match( '/[^\x20-\x7E]/', $data ) === 0 );
473 if ( !$isAsciiStr ) {
474 if ( isset( $_GET[$name] ) && is_string( $data ) ) {
475 # Check for alternate/legacy character encoding.
476 $data = MediaWikiServices::getInstance()
477 ->getContentLanguage()
478 ->checkTitleEncoding( $data );
480 $data = $this->normalizeUnicode( $data );
483 return $data;
487 * Fetch a string from this web request's $_GET, $_POST or path router vars WITHOUT any
488 * Unicode or line break normalization. This is a fast alternative for values that are known
489 * to be simple, e.g. pure ASCII. When reading user input, use {@see getText} instead.
491 * Array values are discarded for security reasons. Use {@see getArray} or {@see getIntArray}.
493 * @since 1.28
494 * @param string $name
495 * @param string|null $default Deprecated since 1.43. Use ?? $default instead.
496 * @return string|null The value, or $default if none set
497 * @return-taint tainted
499 public function getRawVal( $name, $default = null ): ?string {
500 if ( $default !== null ) {
501 wfDeprecated( __METHOD__ . ' with parameter $default', '1.43' );
503 $name = strtr( $name, '.', '_' ); // See comment in self::getGPCVal()
504 if ( isset( $this->data[$name] ) && !is_array( $this->data[$name] ) ) {
505 $val = $this->data[$name];
506 } else {
507 $val = $default;
510 return $val === null ? null : (string)$val;
514 * Fetch a text string from this web request's $_GET, $_POST or path router vars and partially
515 * normalize it.
517 * Use of this method is discouraged. It doesn't normalize line breaks and defaults to null
518 * instead of the empty string. Instead:
519 * - Use {@see getText} when reading user input or form fields that are expected to contain
520 * non-ASCII characters.
521 * - Use {@see getRawVal} when reading ASCII strings, such as parameters used to select
522 * predefined behaviour in the software.
524 * Array values are discarded for security reasons. Use {@see getArray} or {@see getIntArray}.
526 * @param string $name
527 * @param string|null $default
528 * @return string|null The input value, or $default if none set
529 * @return-taint tainted
531 public function getVal( $name, $default = null ) {
532 $val = $this->getGPCVal( $this->data, $name, $default );
533 if ( is_array( $val ) ) {
534 $val = $default;
537 return $val === null ? null : (string)$val;
541 * Fetch a text string from this web request's $_GET, $_POST or path router vars and return it
542 * in normalized form.
544 * This normalizes Unicode sequences (via {@see getGPCVal}) and line breaks.
546 * This should be used for all user input and form fields that are expected to contain non-ASCII
547 * characters, especially if the value will be stored or compared against stored values. Without
548 * normalization, logically identically values might not match when they are typed on different
549 * OS' or keyboards.
551 * Array values are discarded for security reasons. Use {@see getArray} or {@see getIntArray}.
553 * @param string $name
554 * @param string $default
555 * @return string The normalized input value, or $default if none set
556 * @return-taint tainted
558 public function getText( $name, $default = '' ) {
559 $val = $this->getVal( $name, $default );
560 return str_replace( "\r\n", "\n", $val );
564 * Set an arbitrary value into our get/post data.
566 * @param string $key Key name to use
567 * @param mixed $value Value to set
568 * @return mixed Old value if one was present, null otherwise
570 public function setVal( $key, $value ) {
571 $ret = $this->data[$key] ?? null;
572 $this->data[$key] = $value;
573 return $ret;
577 * Unset an arbitrary value from our get/post data.
579 * @param string $key Key name to use
580 * @return mixed Old value if one was present, null otherwise
582 public function unsetVal( $key ) {
583 if ( !isset( $this->data[$key] ) ) {
584 $ret = null;
585 } else {
586 $ret = $this->data[$key];
587 unset( $this->data[$key] );
589 return $ret;
593 * Fetch an array from this web request's $_GET, $_POST or path router vars,
594 * or return $default if it's not set. If source was scalar, will return an
595 * array with a single element. If no source and no default, returns null.
597 * @param string $name
598 * @param array|null $default Optional default (or null)
599 * @return array|null
600 * @return-taint tainted
602 public function getArray( $name, $default = null ) {
603 $val = $this->getGPCVal( $this->data, $name, $default );
604 if ( $val === null ) {
605 return null;
606 } else {
607 return (array)$val;
612 * Fetch an array of integers from this web request's $_GET, $_POST or
613 * path router vars, or return $default if it's not set. If source was
614 * scalar, will return an array with a single element. If no source and
615 * no default, returns null. If an array is returned, contents are
616 * guaranteed to be integers.
618 * @param string $name
619 * @param array|null $default Option default (or null)
620 * @return int[]|null
621 * @return-taint none
623 public function getIntArray( $name, $default = null ) {
624 $val = $this->getArray( $name, $default );
625 if ( is_array( $val ) ) {
626 $val = array_map( 'intval', $val );
628 return $val;
632 * Fetch an integer value from this web request's $_GET, $_POST or
633 * path router vars, or return $default if not set. Guaranteed to return
634 * an integer; non-numeric input will typically return 0.
636 * @param string $name
637 * @param int $default
638 * @return int
640 public function getInt( $name, $default = 0 ): int {
641 return intval( $this->getRawVal( $name ) ?? $default );
645 * Fetch an integer value from this web request's $_GET, $_POST or
646 * path router vars, or return null if empty. Guaranteed to return an
647 * integer or null; non-numeric input will typically return null.
649 * @param string $name
650 * @return int|null
652 public function getIntOrNull( $name ): ?int {
653 $val = $this->getRawVal( $name );
654 return is_numeric( $val ) ? intval( $val ) : null;
658 * Fetch a floating point value from this web request's $_GET, $_POST
659 * or path router vars, or return $default if not set. Guaranteed to
660 * return a float; non-numeric input will typically return 0.
662 * @since 1.23
663 * @param string $name
664 * @param float $default
665 * @return float
667 public function getFloat( $name, $default = 0.0 ): float {
668 return floatval( $this->getRawVal( $name ) ?? $default );
672 * Fetch a boolean value from this web request's $_GET, $_POST or path
673 * router vars or return $default if not set. Guaranteed to return true
674 * or false, with normal PHP semantics for boolean interpretation of strings.
676 * @param string $name
677 * @param bool $default
678 * @return bool
680 public function getBool( $name, $default = false ): bool {
681 return (bool)( $this->getRawVal( $name ) ?? $default );
685 * Fetch a boolean value from this web request's $_GET, $_POST or path router
686 * vars or return $default if not set. Unlike getBool, the string "false" will
687 * result in boolean false, which is useful when interpreting information sent
688 * from JavaScript.
690 * @param string $name
691 * @param bool $default
692 * @return bool
694 public function getFuzzyBool( $name, $default = false ): bool {
695 $value = $this->getRawVal( $name );
696 if ( $value === null ) {
697 return (bool)$default;
700 return $value && strcasecmp( $value, 'false' ) !== 0;
704 * Return true if the named value is set in this web request's $_GET,
705 * $_POST or path router vars, whatever that value is (even "0").
706 * Return false if the named value is not set. Example use is checking
707 * for the presence of check boxes in forms.
709 * @param string $name
710 * @return bool
712 public function getCheck( $name ): bool {
713 # Checkboxes and buttons are only present when clicked
714 # Presence connotes truth, absence false
715 return $this->getRawVal( $name ) !== null;
719 * Extracts the (given) named values from this web request's $_GET, $_POST or path
720 * router vars into an array. No transformation is performed on the values.
722 * @param string ...$names If no arguments are given, returns all input values
723 * @return array
724 * @return-taint tainted
726 public function getValues( ...$names ) {
727 if ( $names === [] ) {
728 $names = array_keys( $this->data );
731 $retVal = [];
732 foreach ( $names as $name ) {
733 $value = $this->getGPCVal( $this->data, $name, null );
734 if ( $value !== null ) {
735 $retVal[$name] = $value;
738 return $retVal;
742 * Returns the names of this web request's $_GET, $_POST or path router vars,
743 * excluding those in $exclude.
745 * @param array $exclude
746 * @return array
747 * @return-taint tainted
749 public function getValueNames( $exclude = [] ) {
750 return array_diff( array_keys( $this->getValues() ), $exclude );
754 * Get the values passed in $_GET and the path router parameters.
755 * No transformation is performed on the values.
757 * @codeCoverageIgnore
758 * @return (string|string[])[] Might contain arrays in case there was a `&param[]=…` parameter
759 * @return-taint tainted
761 public function getQueryValues() {
762 return $this->queryAndPathParams;
766 * Get the values passed in $_GET only, not including the path
767 * router parameters. This is less suitable for self-links to index.php but
768 * useful for other entry points. No transformation is performed on the
769 * values.
771 * @since 1.34
772 * @return (string|string[])[] Might contain arrays in case there was a `&param[]=…` parameter
774 public function getQueryValuesOnly() {
775 return $this->queryParams;
779 * Get the values passed via POST.
780 * No transformation is performed on the values.
782 * @since 1.32
783 * @codeCoverageIgnore
784 * @return (string|string[])[] Might contain arrays in case there was a `&param[]=…` parameter
786 public function getPostValues() {
787 return $_POST;
791 * Return the contents of the URL query string with no decoding. Use when you need to
792 * know exactly what was sent, e.g. for an OAuth signature over the elements.
794 * @codeCoverageIgnore
795 * @return string
796 * @return-taint tainted
798 public function getRawQueryString() {
799 return $this->getServerInfo( 'QUERY_STRING' ) ?? '';
803 * Return the contents of the POST with no decoding. Use when you need to
804 * know exactly what was sent, e.g. for an OAuth signature over the elements.
806 * @return string
807 * @return-taint tainted
809 public function getRawPostString() {
810 if ( !$this->wasPosted() ) {
811 return '';
813 return $this->getRawInput();
817 * Return the raw request body, with no processing. Cached since some methods
818 * disallow reading the stream more than once. As stated in the php docs, this
819 * does not work with enctype="multipart/form-data".
821 * @return string
822 * @return-taint tainted
824 public function getRawInput() {
825 static $input = null;
826 $input ??= file_get_contents( 'php://input' );
827 return $input;
831 * Get the HTTP method used for this request.
833 * @return string
835 public function getMethod() {
836 return $this->getServerInfo( 'REQUEST_METHOD' ) ?: 'GET';
840 * Returns true if the present request was reached by a POST operation,
841 * false otherwise (GET, HEAD, or command-line).
843 * Note that values retrieved by the object may come from the
844 * GET URL etc even on a POST request.
846 * @return bool
848 public function wasPosted() {
849 return $this->getMethod() == 'POST';
853 * Return the session for this request
855 * This might unpersist an existing session if it was invalid.
857 * @since 1.27
858 * @note For performance, keep the session locally if you will be making
859 * much use of it instead of calling this method repeatedly.
860 * @return Session
862 public function getSession(): Session {
863 if ( $this->sessionId !== null ) {
864 $session = SessionManager::singleton()->getSessionById( (string)$this->sessionId, true, $this );
865 if ( $session ) {
866 return $session;
870 $session = SessionManager::singleton()->getSessionForRequest( $this );
871 $this->sessionId = $session->getSessionId();
872 return $session;
876 * Set the session for this request
877 * @since 1.27
878 * @internal For use by MediaWiki\Session classes only
879 * @param SessionId $sessionId
881 public function setSessionId( SessionId $sessionId ) {
882 $this->sessionId = $sessionId;
886 * Get the session id for this request, if any
887 * @since 1.27
888 * @internal For use by MediaWiki\Session classes only
889 * @return SessionId|null
891 public function getSessionId() {
892 return $this->sessionId;
896 * Get a cookie from the $_COOKIE jar
898 * @param string $key The name of the cookie
899 * @param string|null $prefix A prefix to use for the cookie name, if not $wgCookiePrefix
900 * @param mixed|null $default What to return if the value isn't found
901 * @return mixed Cookie value or $default if the cookie not set
902 * @return-taint tainted
904 public function getCookie( $key, $prefix = null, $default = null ) {
905 if ( $prefix === null ) {
906 global $wgCookiePrefix;
907 $prefix = $wgCookiePrefix;
909 $name = $prefix . $key;
910 // Work around mangling of $_COOKIE
911 $name = strtr( $name, '.', '_' );
912 if ( isset( $_COOKIE[$name] ) ) {
913 // For duplicate cookies in the format of name[]=value;name[]=value2,
914 // PHP will assign an array value for the 'name' cookie in $_COOKIE.
915 // Neither RFC 6265 nor its preceding RFCs define such behavior,
916 // and MediaWiki does not rely on it either, so treat the cookie as absent if so (T363980).
917 if ( is_array( $_COOKIE[$name] ) ) {
918 return $default;
920 return $_COOKIE[$name];
921 } else {
922 return $default;
927 * Get a cookie set with SameSite=None.
929 * @deprecated since 1.42 use getCookie(), but note the different $prefix default
931 * @param string $key The name of the cookie
932 * @param string $prefix A prefix to use, empty by default
933 * @param mixed|null $default What to return if the value isn't found
934 * @return mixed Cookie value or $default if the cookie is not set
936 public function getCrossSiteCookie( $key, $prefix = '', $default = null ) {
937 wfDeprecated( __METHOD__, '1.42' );
938 return $this->getCookie( $key, $prefix, $default );
942 * Return the path and query string portion of the main request URI.
943 * This will be suitable for use as a relative link in HTML output.
945 * @throws MWException
946 * @return string
947 * @return-taint tainted
949 public static function getGlobalRequestURL() {
950 // This method is called on fatal errors; it should not depend on anything complex.
952 if ( isset( $_SERVER['REQUEST_URI'] ) && strlen( $_SERVER['REQUEST_URI'] ) ) {
953 $base = $_SERVER['REQUEST_URI'];
954 } elseif ( isset( $_SERVER['HTTP_X_ORIGINAL_URL'] )
955 && strlen( $_SERVER['HTTP_X_ORIGINAL_URL'] )
957 // Probably IIS; doesn't set REQUEST_URI
958 $base = $_SERVER['HTTP_X_ORIGINAL_URL'];
959 } elseif ( isset( $_SERVER['SCRIPT_NAME'] ) ) {
960 $base = $_SERVER['SCRIPT_NAME'];
961 if ( isset( $_SERVER['QUERY_STRING'] ) && $_SERVER['QUERY_STRING'] != '' ) {
962 $base .= '?' . $_SERVER['QUERY_STRING'];
964 } else {
965 // This shouldn't happen!
966 throw new MWException(
967 "Web server doesn't provide either " .
968 "REQUEST_URI, HTTP_X_ORIGINAL_URL or SCRIPT_NAME. Report details " .
969 "of your web server configuration to https://phabricator.wikimedia.org/"
972 // User-agents should not send a fragment with the URI, but
973 // if they do, and the web server passes it on to us, we
974 // need to strip it or we get false-positive redirect loops
975 // or weird output URLs
976 $hash = strpos( $base, '#' );
977 if ( $hash !== false ) {
978 $base = substr( $base, 0, $hash );
981 if ( $base[0] == '/' ) {
982 // More than one slash will look like it is protocol relative
983 return preg_replace( '!^/+!', '/', $base );
984 } else {
985 // We may get paths with a host prepended; strip it.
986 return preg_replace( '!^[^:]+://[^/]+/+!', '/', $base );
991 * Return the path and query string portion of the request URI.
992 * This will be suitable for use as a relative link in HTML output.
994 * @throws MWException
995 * @return string
996 * @return-taint tainted
998 public function getRequestURL() {
999 return self::getGlobalRequestURL();
1003 * Return the request URI with the canonical service and hostname, path,
1004 * and query string. This will be suitable for use as an absolute link
1005 * in HTML or other output.
1007 * If $wgServer is protocol-relative, this will return a fully
1008 * qualified URL with the protocol of this request object.
1010 * @return string
1011 * @return-taint tainted
1013 public function getFullRequestURL() {
1014 $urlUtils = MediaWikiServices::getInstance()->getUrlUtils();
1015 // Pass an explicit PROTO constant instead of PROTO_CURRENT so that we
1016 // do not rely on state from the global $wgRequest object (which it would,
1017 // via UrlUtils::getServer()/UrlUtils::expand()/$wgRequest->protocol).
1018 if ( $this->getProtocol() === 'http' ) {
1019 return ( $urlUtils->getServer( PROTO_HTTP ) ?? '' ) . $this->getRequestURL();
1020 } else {
1021 return ( $urlUtils->getServer( PROTO_HTTPS ) ?? '' ) . $this->getRequestURL();
1026 * @param string $key
1027 * @param string $value
1028 * @return string
1030 public function appendQueryValue( $key, $value ) {
1031 return $this->appendQueryArray( [ $key => $value ] );
1035 * Appends or replaces value of query variables.
1037 * @param array $array Array of values to replace/add to query
1038 * @return string
1040 public function appendQueryArray( $array ) {
1041 $newquery = $this->getQueryValues();
1042 unset( $newquery['title'] );
1043 $newquery = array_merge( $newquery, $array );
1045 return wfArrayToCgi( $newquery );
1049 * Check for limit and offset parameters on the input, and return sensible
1050 * defaults if not given. The limit must be positive and is capped at 5000.
1051 * Offset must be positive but is not capped.
1053 * @param UserIdentity $user UserIdentity to get option for
1054 * @param int $deflimit Limit to use if no input and the user hasn't set the option.
1055 * @param string $optionname To specify an option other than rclimit to pull from.
1056 * @return int[] First element is limit, second is offset
1058 public function getLimitOffsetForUser( UserIdentity $user, $deflimit = 50, $optionname = 'rclimit' ) {
1059 $limit = $this->getInt( 'limit', 0 );
1060 if ( $limit < 0 ) {
1061 $limit = 0;
1063 if ( ( $limit == 0 ) && ( $optionname != '' ) ) {
1064 $limit = MediaWikiServices::getInstance()
1065 ->getUserOptionsLookup()
1066 ->getIntOption( $user, $optionname );
1068 if ( $limit <= 0 ) {
1069 $limit = $deflimit;
1071 if ( $limit > 5000 ) {
1072 $limit = 5000; # We have *some* limits...
1075 $offset = $this->getInt( 'offset', 0 );
1076 if ( $offset < 0 ) {
1077 $offset = 0;
1080 return [ $limit, $offset ];
1084 * Return the path to the temporary file where PHP has stored the upload.
1086 * @param string $key
1087 * @return string|null String or null if no such file.
1089 public function getFileTempname( $key ) {
1090 return $this->getUpload( $key )->getTempName();
1094 * Return the upload error or 0
1096 * @param string $key
1097 * @return int
1099 public function getUploadError( $key ) {
1100 return $this->getUpload( $key )->getError();
1104 * Return the original filename of the uploaded file, as reported by
1105 * the submitting user agent. HTML-style character entities are
1106 * interpreted and normalized to Unicode normalization form C, in part
1107 * to deal with weird input from Safari with non-ASCII filenames.
1109 * Other than this the name is not verified for being a safe filename.
1111 * @param string $key
1112 * @return string|null String or null if no such file.
1114 public function getFileName( $key ) {
1115 return $this->getUpload( $key )->getName();
1119 * Return a MediaWiki\Request\WebRequestUpload object corresponding to the key
1121 * @param string $key
1122 * @return WebRequestUpload
1124 public function getUpload( $key ) {
1125 return new WebRequestUpload( $this, $key );
1129 * Return a handle to WebResponse style object, for setting cookies,
1130 * headers and other stuff, for Request being worked on.
1132 public function response(): WebResponse {
1133 /* Lazy initialization of response object for this request */
1134 if ( !$this->response ) {
1135 $this->response = new WebResponse();
1137 return $this->response;
1141 * Initialise the header list
1143 protected function initHeaders() {
1144 if ( count( $this->headers ) ) {
1145 return;
1148 $this->headers = array_change_key_case( getallheaders(), CASE_UPPER );
1152 * Get an array containing all request headers
1154 * @return string[] Mapping header name to its value
1155 * @return-taint tainted
1157 public function getAllHeaders() {
1158 $this->initHeaders();
1159 return $this->headers;
1163 * Get a request header, or false if it isn't set.
1165 * @param string $name Case-insensitive header name
1166 * @param int $flags Bitwise combination of:
1167 * WebRequest::GETHEADER_LIST Treat the header as a comma-separated list
1168 * of values, as described in RFC 2616 § 4.2.
1169 * (since 1.26).
1170 * @return string|string[]|false False if header is unset; otherwise the
1171 * header value(s) as either a string (the default) or an array, if
1172 * WebRequest::GETHEADER_LIST flag was set.
1173 * @return-taint tainted
1175 public function getHeader( $name, $flags = 0 ) {
1176 $this->initHeaders();
1177 $name = strtoupper( $name );
1178 if ( !isset( $this->headers[$name] ) ) {
1179 return false;
1181 $value = $this->headers[$name];
1182 if ( $flags & self::GETHEADER_LIST ) {
1183 $value = array_map( 'trim', explode( ',', $value ) );
1185 return $value;
1189 * Get data from the session
1191 * @note Prefer $this->getSession() instead if making multiple calls.
1192 * @param string $key Name of key in the session
1193 * @return mixed
1195 public function getSessionData( $key ) {
1196 return $this->getSession()->get( $key );
1200 * @note Prefer $this->getSession() instead if making multiple calls.
1201 * @param string $key Name of key in the session
1202 * @param mixed $data
1204 public function setSessionData( $key, $data ) {
1205 $this->getSession()->set( $key, $data );
1209 * Parse the Accept-Language header sent by the client into an array
1211 * @return array [ languageCode => q-value ] sorted by q-value in
1212 * descending order then appearing time in the header in ascending order.
1213 * May contain the "language" '*', which applies to languages other than those explicitly listed.
1215 * This logic is aligned with RFC 7231 section 5 (previously RFC 2616 section 14),
1216 * at <https://tools.ietf.org/html/rfc7231#section-5.3.5>.
1218 * Earlier languages in the list are preferred as per the RFC 23282 extension to HTTP/1.1,
1219 * at <https://tools.ietf.org/html/rfc3282>.
1220 * @return-taint tainted
1222 public function getAcceptLang() {
1223 // Modified version of code found at
1224 // http://www.thefutureoftheweb.com/blog/use-accept-language-header
1225 $acceptLang = $this->getHeader( 'Accept-Language' );
1226 if ( !$acceptLang ) {
1227 return [];
1230 // Return the language codes in lower case
1231 $acceptLang = strtolower( $acceptLang );
1233 // Break up string into pieces (languages and q factors)
1234 if ( !preg_match_all(
1236 # a language code or a star is required
1237 ([a-z]{1,8}(?:-[a-z]{1,8})*|\*)
1238 # from here everything is optional
1241 # this accepts only numbers in the range ;q=0.000 to ;q=1.000
1242 ;\s*q\s*=\s*
1243 (1(?:\.0{0,3})?|0(?:\.\d{0,3})?)?
1245 /x',
1246 $acceptLang,
1247 $matches,
1248 PREG_SET_ORDER
1249 ) ) {
1250 return [];
1253 // Create a list like "en" => 0.8
1254 $langs = [];
1255 foreach ( $matches as $match ) {
1256 $languageCode = $match[1];
1257 // When not present, the default value is 1
1258 $qValue = (float)( $match[2] ?? 1.0 );
1259 if ( $qValue ) {
1260 $langs[$languageCode] = $qValue;
1264 // Sort list by qValue
1265 arsort( $langs, SORT_NUMERIC );
1266 return $langs;
1270 * Fetch the raw IP from the request
1272 * @since 1.19
1273 * @return string|null
1275 protected function getRawIP() {
1276 $remoteAddr = $this->getServerInfo( 'REMOTE_ADDR' );
1277 if ( !$remoteAddr ) {
1278 return null;
1280 if ( str_contains( $remoteAddr, ',' ) ) {
1281 throw new MWException( 'Remote IP must not contain multiple values' );
1284 return IPUtils::canonicalize( $remoteAddr );
1288 * Work out the IP address based on various globals
1289 * For trusted proxies, use the XFF client IP (first of the chain)
1291 * @since 1.19
1292 * @return string
1294 public function getIP(): string {
1295 global $wgUsePrivateIPs;
1297 # Return cached result
1298 if ( $this->ip !== null ) {
1299 return $this->ip;
1302 # collect the originating IPs
1303 $ip = $this->getRawIP();
1304 if ( !$ip ) {
1305 throw new MWException( 'Unable to determine IP.' );
1308 $services = MediaWikiServices::getInstance();
1309 # Append XFF
1310 $forwardedFor = $this->getHeader( 'X-Forwarded-For' );
1311 if ( $forwardedFor !== false ) {
1312 $proxyLookup = $services->getProxyLookup();
1313 $isConfigured = $proxyLookup->isConfiguredProxy( $ip );
1314 $ipchain = array_map( 'trim', explode( ',', $forwardedFor ) );
1315 $ipchain = array_reverse( $ipchain );
1316 array_unshift( $ipchain, $ip );
1318 # Step through XFF list and find the last address in the list which is a
1319 # trusted server. Set $ip to the IP address given by that trusted server,
1320 # unless the address is not sensible (e.g. private). However, prefer private
1321 # IP addresses over proxy servers controlled by this site (more sensible).
1322 # Note that some XFF values might be "unknown" with Squid/Varnish.
1323 foreach ( $ipchain as $i => $curIP ) {
1324 $curIP = IPUtils::sanitizeIP(
1325 IPUtils::canonicalize(
1326 self::canonicalizeIPv6LoopbackAddress( $curIP )
1329 if ( !$curIP || !isset( $ipchain[$i + 1] ) || $ipchain[$i + 1] === 'unknown'
1330 || !$proxyLookup->isTrustedProxy( $curIP )
1332 break; // IP is not valid/trusted or does not point to anything
1334 if (
1335 IPUtils::isPublic( $ipchain[$i + 1] ) ||
1336 $wgUsePrivateIPs ||
1337 // T50919; treat IP as valid
1338 $proxyLookup->isConfiguredProxy( $curIP )
1340 $nextIP = $ipchain[$i + 1];
1342 // Follow the next IP according to the proxy
1343 $nextIP = IPUtils::canonicalize(
1344 self::canonicalizeIPv6LoopbackAddress( $nextIP )
1346 if ( !$nextIP && $isConfigured ) {
1347 // We have not yet made it past CDN/proxy servers of this site,
1348 // so either they are misconfigured or there is some IP spoofing.
1349 throw new MWException( "Invalid IP given in XFF '$forwardedFor'." );
1351 $ip = $nextIP;
1353 // keep traversing the chain
1354 continue;
1356 break;
1360 // Allow extensions to modify the result
1361 $hookContainer = $services->getHookContainer();
1362 // Optimisation: Hot code called on most requests (T85805).
1363 if ( $hookContainer->isRegistered( 'GetIP' ) ) {
1364 // @phan-suppress-next-line PhanTypeMismatchArgument Type mismatch on pass-by-ref args
1365 ( new HookRunner( $hookContainer ) )->onGetIP( $ip );
1368 if ( !$ip ) {
1369 throw new MWException( 'Unable to determine IP.' );
1372 $this->ip = $ip;
1373 return $ip;
1377 * Converts ::1 (IPv6 loopback address) to 127.0.0.1 (IPv4 loopback address);
1378 * assists in matching trusted proxies.
1380 * @param string $ip
1381 * @return string either '127.0.0.1' or $ip
1382 * @since 1.36
1384 public static function canonicalizeIPv6LoopbackAddress( $ip ) {
1385 // Code moved from IPUtils library. See T248237#6614927
1386 if ( preg_match( '/^0*' . IPUtils::RE_IPV6_GAP . '1$/', $ip ) ) {
1387 return '127.0.0.1';
1389 return $ip;
1393 * @param string $ip
1394 * @return void
1395 * @since 1.21
1397 public function setIP( $ip ) {
1398 $this->ip = $ip;
1402 * Check if this request uses a "safe" HTTP method
1404 * Safe methods are verbs (e.g. GET/HEAD/OPTIONS) used for obtaining content. Such requests
1405 * are not expected to mutate content, especially in ways attributable to the client. Verbs
1406 * like POST and PUT are typical of non-safe requests which often change content.
1408 * @return bool
1409 * @see https://tools.ietf.org/html/rfc7231#section-4.2.1
1410 * @see https://www.w3.org/Protocols/rfc2616/rfc2616-sec9.html
1411 * @since 1.28
1413 public function hasSafeMethod() {
1414 if ( $this->getServerInfo( 'REQUEST_METHOD' ) === null ) {
1415 return false; // CLI mode
1418 return in_array( $this->getServerInfo( 'REQUEST_METHOD' ), [ 'GET', 'HEAD', 'OPTIONS', 'TRACE' ] );
1422 * Whether this request should be identified as being "safe"
1424 * This means that the client is not requesting any state changes and that database writes
1425 * are not inherently required. Ideally, no visible updates would happen at all. If they
1426 * must, then they should not be publicly attributed to the end user.
1428 * In more detail:
1429 * - Cache populations and refreshes MAY occur.
1430 * - Private user session updates and private server logging MAY occur.
1431 * - Updates to private viewing activity data MAY occur via DeferredUpdates.
1432 * - Other updates SHOULD NOT occur (e.g. modifying content assets).
1434 * @deprecated since 1.41, use hasSafeMethod() instead.
1436 * @return bool
1437 * @see https://tools.ietf.org/html/rfc7231#section-4.2.1
1438 * @see https://www.w3.org/Protocols/rfc2616/rfc2616-sec9.html
1439 * @since 1.28
1441 public function isSafeRequest() {
1442 wfDeprecated( __METHOD__, '1.41' );
1443 if ( $this->markedAsSafe && $this->wasPosted() ) {
1444 return true; // marked as a "safe" POST
1447 return $this->hasSafeMethod();
1451 * Mark this request as identified as being nullipotent even if it is a POST request
1453 * POST requests are often used due to the need for a client payload, even if the request
1454 * is otherwise equivalent to a "safe method" request.
1456 * @deprecated since 1.41
1458 * @see https://tools.ietf.org/html/rfc7231#section-4.2.1
1459 * @see https://www.w3.org/Protocols/rfc2616/rfc2616-sec9.html
1460 * @since 1.28
1462 public function markAsSafeRequest() {
1463 wfDeprecated( __METHOD__, '1.41' );
1464 $this->markedAsSafe = true;
1468 * Determine if the request URL matches one of a given set of canonical CDN URLs.
1470 * MediaWiki uses this to determine whether to set a long 'Cache-Control: s-maxage='
1471 * header on the response. {@see MainConfigNames::CdnMatchParameterOrder} controls whether
1472 * the matching is sensitive to the order of query parameters.
1474 * @param string[] $cdnUrls URLs to match against
1475 * @return bool
1476 * @since 1.39
1478 public function matchURLForCDN( array $cdnUrls ) {
1479 $services = MediaWikiServices::getInstance();
1480 $reqUrl = (string)$services->getUrlUtils()->expand( $this->getRequestURL(), PROTO_INTERNAL );
1481 $config = $services->getMainConfig();
1482 if ( $config->get( MainConfigNames::CdnMatchParameterOrder ) ) {
1483 // Strict matching
1484 return in_array( $reqUrl, $cdnUrls, true );
1487 // Loose matching (order of query parameters is ignored)
1488 $reqUrlParts = explode( '?', $reqUrl, 2 );
1489 $reqUrlBase = $reqUrlParts[0];
1490 $reqUrlParams = count( $reqUrlParts ) === 2 ? explode( '&', $reqUrlParts[1] ) : [];
1491 // The order of parameters after the sort() call below does not match
1492 // the order set by the CDN, and does not need to. The CDN needs to
1493 // take special care to preserve the relative order of duplicate keys
1494 // and array-like parameters.
1495 sort( $reqUrlParams );
1496 foreach ( $cdnUrls as $cdnUrl ) {
1497 if ( strlen( $reqUrl ) !== strlen( $cdnUrl ) ) {
1498 continue;
1500 $cdnUrlParts = explode( '?', $cdnUrl, 2 );
1501 $cdnUrlBase = $cdnUrlParts[0];
1502 if ( $reqUrlBase !== $cdnUrlBase ) {
1503 continue;
1505 $cdnUrlParams = count( $cdnUrlParts ) === 2 ? explode( '&', $cdnUrlParts[1] ) : [];
1506 sort( $cdnUrlParams );
1507 if ( $reqUrlParams === $cdnUrlParams ) {
1508 return true;
1511 return false;
1515 /** @deprecated class alias since 1.41 */
1516 class_alias( WebRequest::class, 'WebRequest' );