2004-03-08 09:09:35 +00:00
|
|
|
<?php
|
2004-09-02 23:28:24 +00:00
|
|
|
/**
|
2013-03-13 07:42:41 +00:00
|
|
|
* Deal with importing all those nasty globals and things
|
2010-08-14 17:42:40 +00:00
|
|
|
*
|
|
|
|
|
* Copyright © 2003 Brion Vibber <brion@pobox.com>
|
2014-03-12 22:30:35 +00:00
|
|
|
* https://www.mediawiki.org/
|
2010-08-14 17:42:40 +00:00
|
|
|
*
|
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
|
* (at your option) any later version.
|
|
|
|
|
*
|
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
|
*
|
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
|
* with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
|
* http://www.gnu.org/copyleft/gpl.html
|
|
|
|
|
*
|
|
|
|
|
* @file
|
2004-09-02 23:28:24 +00:00
|
|
|
*/
|
|
|
|
|
|
2016-09-22 02:52:06 +00:00
|
|
|
use MediaWiki\MediaWikiServices;
|
2016-04-06 22:22:33 +00:00
|
|
|
use MediaWiki\Session\Session;
|
|
|
|
|
use MediaWiki\Session\SessionId;
|
2016-02-01 20:44:03 +00:00
|
|
|
use MediaWiki\Session\SessionManager;
|
2021-07-20 21:17:10 +00:00
|
|
|
use MediaWiki\User\UserIdentity;
|
2019-06-25 18:53:15 +00:00
|
|
|
use Wikimedia\IPUtils;
|
2016-02-01 20:44:03 +00:00
|
|
|
|
2018-05-19 21:41:41 +00:00
|
|
|
// The point of this class is to be a wrapper around super globals
|
|
|
|
|
// phpcs:disable MediaWiki.Usage.SuperGlobalsUsage.SuperGlobals
|
|
|
|
|
|
2004-09-02 23:28:24 +00:00
|
|
|
/**
|
2004-09-04 00:12:08 +00:00
|
|
|
* The WebRequest class encapsulates getting at data passed in the
|
2014-07-09 15:36:55 +00:00
|
|
|
* URL or via a POSTed form stripping illegal input characters and
|
|
|
|
|
* normalizing Unicode sequences.
|
2004-09-04 00:12:08 +00:00
|
|
|
*
|
2008-09-03 17:30:20 +00:00
|
|
|
* @ingroup HTTP
|
2004-09-02 23:28:24 +00:00
|
|
|
*/
|
2004-03-08 09:09:35 +00:00
|
|
|
class WebRequest {
|
2019-09-03 00:25:19 +00:00
|
|
|
/**
|
|
|
|
|
* The parameters from $_GET, $_POST and the path router
|
|
|
|
|
* @var array
|
|
|
|
|
*/
|
2019-08-30 13:09:51 +00:00
|
|
|
protected $data;
|
2019-09-03 00:25:19 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* The parameters from $_GET. The parameters from the path router are
|
|
|
|
|
* added by interpolateTitle() during Setup.php.
|
2020-11-28 14:18:24 +00:00
|
|
|
* @var string[]
|
2019-09-03 00:25:19 +00:00
|
|
|
*/
|
|
|
|
|
protected $queryAndPathParams;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* The parameters from $_GET only.
|
2020-11-28 14:18:24 +00:00
|
|
|
* @var string[]
|
2019-09-03 00:25:19 +00:00
|
|
|
*/
|
|
|
|
|
protected $queryParams;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Lazy-initialized request headers indexed by upper-case header name
|
2020-11-28 14:18:24 +00:00
|
|
|
* @var string[]
|
2019-09-03 00:25:19 +00:00
|
|
|
*/
|
2019-08-30 13:09:51 +00:00
|
|
|
protected $headers = [];
|
2010-11-05 12:59:37 +00:00
|
|
|
|
2015-05-09 00:42:15 +00:00
|
|
|
/**
|
|
|
|
|
* Flag to make WebRequest::getHeader return an array of values.
|
|
|
|
|
* @since 1.26
|
|
|
|
|
*/
|
2020-05-11 00:48:27 +00:00
|
|
|
public const GETHEADER_LIST = 1;
|
2015-05-09 00:42:15 +00:00
|
|
|
|
2016-04-11 21:00:43 +00:00
|
|
|
/**
|
|
|
|
|
* The unique request ID.
|
|
|
|
|
* @var string
|
|
|
|
|
*/
|
|
|
|
|
private static $reqId;
|
|
|
|
|
|
2010-11-05 12:59:37 +00:00
|
|
|
/**
|
|
|
|
|
* Lazy-init response object
|
|
|
|
|
* @var WebResponse
|
|
|
|
|
*/
|
|
|
|
|
private $response;
|
2008-04-14 07:45:50 +00:00
|
|
|
|
2011-08-18 20:03:30 +00:00
|
|
|
/**
|
|
|
|
|
* Cached client IP address
|
2014-04-23 09:37:59 +00:00
|
|
|
* @var string
|
2011-08-18 20:03:30 +00:00
|
|
|
*/
|
|
|
|
|
private $ip;
|
|
|
|
|
|
2015-04-01 23:16:09 +00:00
|
|
|
/**
|
|
|
|
|
* The timestamp of the start of the request, with microsecond precision.
|
|
|
|
|
* @var float
|
|
|
|
|
*/
|
|
|
|
|
protected $requestTime;
|
|
|
|
|
|
2013-08-21 05:35:40 +00:00
|
|
|
/**
|
|
|
|
|
* Cached URL protocol
|
|
|
|
|
* @var string
|
|
|
|
|
*/
|
2013-11-23 01:08:44 +00:00
|
|
|
protected $protocol;
|
2013-08-21 05:35:40 +00:00
|
|
|
|
2016-02-01 20:44:03 +00:00
|
|
|
/**
|
2016-04-06 22:22:33 +00:00
|
|
|
* @var SessionId|null Session ID to use for this
|
2016-02-01 20:44:03 +00:00
|
|
|
* request. We can't save the session directly due to reference cycles not
|
2019-10-10 19:56:42 +00:00
|
|
|
* working too well (slow GC).
|
|
|
|
|
*
|
|
|
|
|
* TODO: Investigate whether this GC slowness concern (added in a73c5b7395 with regard to
|
|
|
|
|
* PHP 5.6) still applies in PHP 7.2+.
|
2016-02-01 20:44:03 +00:00
|
|
|
*/
|
|
|
|
|
protected $sessionId = null;
|
|
|
|
|
|
2016-05-06 22:25:36 +00:00
|
|
|
/** @var bool Whether this HTTP request is "safe" (even if it is an HTTP post) */
|
|
|
|
|
protected $markedAsSafe = false;
|
|
|
|
|
|
2016-09-09 04:58:09 +00:00
|
|
|
/**
|
|
|
|
|
* @codeCoverageIgnore
|
|
|
|
|
*/
|
2009-07-25 00:14:34 +00:00
|
|
|
public function __construct() {
|
2016-08-12 10:33:37 +00:00
|
|
|
$this->requestTime = $_SERVER['REQUEST_TIME_FLOAT'];
|
2008-09-08 13:48:07 +00:00
|
|
|
|
2008-02-12 22:07:16 +00:00
|
|
|
// POST overrides GET data
|
|
|
|
|
// We don't use $_REQUEST here to avoid interference from cookies...
|
2008-11-01 23:20:25 +00:00
|
|
|
$this->data = $_POST + $_GET;
|
2019-09-03 00:25:19 +00:00
|
|
|
|
|
|
|
|
$this->queryAndPathParams = $this->queryParams = $_GET;
|
2007-05-17 20:02:59 +00:00
|
|
|
}
|
2008-04-14 07:45:50 +00:00
|
|
|
|
2011-02-02 15:44:37 +00:00
|
|
|
/**
|
2011-12-09 00:28:34 +00:00
|
|
|
* Extract relevant query arguments from the http request uri's path
|
|
|
|
|
* to be merged with the normal php provided query arguments.
|
|
|
|
|
* Tries to use the REQUEST_URI data if available and parses it
|
|
|
|
|
* according to the wiki's configuration looking for any known pattern.
|
|
|
|
|
*
|
|
|
|
|
* If the REQUEST_URI is not provided we'll fall back on the PATH_INFO
|
|
|
|
|
* provided by the server if any and use that to set a 'title' parameter.
|
2011-02-02 15:44:37 +00:00
|
|
|
*
|
2021-02-03 01:28:39 +00:00
|
|
|
* This internal method handles many odd cases and is tailored specifically for
|
|
|
|
|
* used by WebRequest::interpolateTitle, for index.php requests.
|
|
|
|
|
* Consider using WebRequest::getRequestPathSuffix for other path-related use cases.
|
2020-03-31 05:33:41 +00:00
|
|
|
*
|
2013-03-11 17:15:01 +00:00
|
|
|
* @param string $want If this is not 'all', then the function
|
2011-02-02 15:44:37 +00:00
|
|
|
* will return an empty array if it determines that the URL is
|
|
|
|
|
* inside a rewrite path.
|
|
|
|
|
*
|
2020-11-28 14:18:24 +00:00
|
|
|
* @return string[] Any query arguments found in path matches.
|
2019-09-21 00:08:10 +00:00
|
|
|
* @throws FatalError If invalid routes are configured (T48998)
|
2011-02-02 15:44:37 +00:00
|
|
|
*/
|
2021-02-03 01:28:39 +00:00
|
|
|
protected static function getPathInfo( $want = 'all' ) {
|
2016-10-13 05:34:26 +00:00
|
|
|
// PATH_INFO is mangled due to https://bugs.php.net/bug.php?id=31892
|
2011-02-02 15:44:37 +00:00
|
|
|
// And also by Apache 2.x, double slashes are converted to single slashes.
|
|
|
|
|
// So we will use REQUEST_URI if possible.
|
2019-09-02 01:03:02 +00:00
|
|
|
if ( isset( $_SERVER['REQUEST_URI'] ) ) {
|
2011-02-02 01:08:15 +00:00
|
|
|
// Slurp out the path portion to examine...
|
|
|
|
|
$url = $_SERVER['REQUEST_URI'];
|
|
|
|
|
if ( !preg_match( '!^https?://!', $url ) ) {
|
|
|
|
|
$url = 'http://unused' . $url;
|
|
|
|
|
}
|
|
|
|
|
$a = parse_url( $url );
|
2019-09-02 01:03:02 +00:00
|
|
|
if ( !$a ) {
|
|
|
|
|
return [];
|
|
|
|
|
}
|
|
|
|
|
$path = $a['path'] ?? '';
|
2012-02-07 13:05:31 +00:00
|
|
|
|
2019-09-02 01:03:02 +00:00
|
|
|
global $wgScript;
|
|
|
|
|
if ( $path == $wgScript && $want !== 'all' ) {
|
|
|
|
|
// Script inside a rewrite path?
|
|
|
|
|
// Abort to keep from breaking...
|
|
|
|
|
return [];
|
|
|
|
|
}
|
2011-11-30 15:12:19 +00:00
|
|
|
|
2019-09-02 01:03:02 +00:00
|
|
|
$router = new PathRouter;
|
2011-02-02 01:08:15 +00:00
|
|
|
|
2019-09-02 01:03:02 +00:00
|
|
|
// Raw PATH_INFO style
|
|
|
|
|
$router->add( "$wgScript/$1" );
|
2011-11-24 09:55:33 +00:00
|
|
|
|
2019-09-02 01:03:02 +00:00
|
|
|
global $wgArticlePath;
|
|
|
|
|
if ( $wgArticlePath ) {
|
2019-09-21 00:08:10 +00:00
|
|
|
$router->validateRoute( $wgArticlePath, 'wgArticlePath' );
|
2019-09-02 01:03:02 +00:00
|
|
|
$router->add( $wgArticlePath );
|
|
|
|
|
}
|
2011-08-12 19:23:43 +00:00
|
|
|
|
2019-09-02 01:03:02 +00:00
|
|
|
global $wgActionPaths;
|
2019-08-31 22:05:50 +00:00
|
|
|
$articlePaths = PathRouter::getActionPaths( $wgActionPaths, $wgArticlePath );
|
|
|
|
|
if ( $articlePaths ) {
|
|
|
|
|
$router->add( $articlePaths, [ 'action' => '$key' ] );
|
2019-09-02 01:03:02 +00:00
|
|
|
}
|
2011-11-30 15:12:19 +00:00
|
|
|
|
2019-09-02 01:03:02 +00:00
|
|
|
global $wgVariantArticlePath;
|
|
|
|
|
if ( $wgVariantArticlePath ) {
|
2021-08-03 22:50:15 +00:00
|
|
|
$services = MediaWikiServices::getInstance();
|
2019-09-21 00:08:10 +00:00
|
|
|
$router->validateRoute( $wgVariantArticlePath, 'wgVariantArticlePath' );
|
2019-09-02 01:03:02 +00:00
|
|
|
$router->add( $wgVariantArticlePath,
|
|
|
|
|
[ 'variant' => '$2' ],
|
2021-08-03 22:50:15 +00:00
|
|
|
[ '$2' => $services->getLanguageConverterFactory()
|
|
|
|
|
->getLanguageConverter( $services->getContentLanguage() )
|
|
|
|
|
->getVariants() ]
|
2019-09-02 01:03:02 +00:00
|
|
|
);
|
2011-02-02 01:08:15 +00:00
|
|
|
}
|
2019-09-02 01:03:02 +00:00
|
|
|
|
Hooks::run() call site migration
Migrate all callers of Hooks::run() to use the new
HookContainer/HookRunner system.
General principles:
* Use DI if it is already used. We're not changing the way state is
managed in this patch.
* HookContainer is always injected, not HookRunner. HookContainer
is a service, it's a more generic interface, it is the only
thing that provides isRegistered() which is needed in some cases,
and a HookRunner can be efficiently constructed from it
(confirmed by benchmark). Because HookContainer is needed
for object construction, it is also needed by all factories.
* "Ask your friendly local base class". Big hierarchies like
SpecialPage and ApiBase have getHookContainer() and getHookRunner()
methods in the base class, and classes that extend that base class
are not expected to know or care where the base class gets its
HookContainer from.
* ProtectedHookAccessorTrait provides protected getHookContainer() and
getHookRunner() methods, getting them from the global service
container. The point of this is to ease migration to DI by ensuring
that call sites ask their local friendly base class rather than
getting a HookRunner from the service container directly.
* Private $this->hookRunner. In some smaller classes where accessor
methods did not seem warranted, there is a private HookRunner property
which is accessed directly. Very rarely (two cases), there is a
protected property, for consistency with code that conventionally
assumes protected=private, but in cases where the class might actually
be overridden, a protected accessor is preferred over a protected
property.
* The last resort: Hooks::runner(). Mostly for static, file-scope and
global code. In a few cases it was used for objects with broken
construction schemes, out of horror or laziness.
Constructors with new required arguments:
* AuthManager
* BadFileLookup
* BlockManager
* ClassicInterwikiLookup
* ContentHandlerFactory
* ContentSecurityPolicy
* DefaultOptionsManager
* DerivedPageDataUpdater
* FullSearchResultWidget
* HtmlCacheUpdater
* LanguageFactory
* LanguageNameUtils
* LinkRenderer
* LinkRendererFactory
* LocalisationCache
* MagicWordFactory
* MessageCache
* NamespaceInfo
* PageEditStash
* PageHandlerFactory
* PageUpdater
* ParserFactory
* PermissionManager
* RevisionStore
* RevisionStoreFactory
* SearchEngineConfig
* SearchEngineFactory
* SearchFormWidget
* SearchNearMatcher
* SessionBackend
* SpecialPageFactory
* UserNameUtils
* UserOptionsManager
* WatchedItemQueryService
* WatchedItemStore
Constructors with new optional arguments:
* DefaultPreferencesFactory
* Language
* LinkHolderArray
* MovePage
* Parser
* ParserCache
* PasswordReset
* Router
setHookContainer() now required after construction:
* AuthenticationProvider
* ResourceLoaderModule
* SearchEngine
Change-Id: Id442b0dbe43aba84bd5cf801d86dedc768b082c7
2020-03-19 02:42:09 +00:00
|
|
|
Hooks::runner()->onWebRequestPathInfoRouter( $router );
|
2019-09-02 01:03:02 +00:00
|
|
|
|
|
|
|
|
$matches = $router->parse( $path );
|
|
|
|
|
} else {
|
|
|
|
|
global $wgUsePathInfo;
|
|
|
|
|
$matches = [];
|
|
|
|
|
if ( $wgUsePathInfo ) {
|
|
|
|
|
if ( !empty( $_SERVER['ORIG_PATH_INFO'] ) ) {
|
|
|
|
|
// Mangled PATH_INFO
|
|
|
|
|
// https://bugs.php.net/bug.php?id=31892
|
|
|
|
|
// Also reported when ini_get('cgi.fix_pathinfo')==false
|
|
|
|
|
$matches['title'] = substr( $_SERVER['ORIG_PATH_INFO'], 1 );
|
|
|
|
|
} elseif ( !empty( $_SERVER['PATH_INFO'] ) ) {
|
|
|
|
|
// Regular old PATH_INFO yay
|
|
|
|
|
$matches['title'] = substr( $_SERVER['PATH_INFO'], 1 );
|
|
|
|
|
}
|
2012-03-24 08:25:01 +00:00
|
|
|
}
|
2011-02-02 01:08:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $matches;
|
|
|
|
|
}
|
|
|
|
|
|
2020-03-31 05:33:41 +00:00
|
|
|
/**
|
|
|
|
|
* If the request URL matches a given base path, extract the path part of
|
|
|
|
|
* the request URL after that base, and decode escape sequences in it.
|
|
|
|
|
*
|
|
|
|
|
* If the request URL does not match, false is returned.
|
|
|
|
|
*
|
|
|
|
|
* @since 1.35
|
|
|
|
|
* @param string $basePath The base URL path. Trailing slashes will be
|
|
|
|
|
* stripped.
|
|
|
|
|
* @return string|false
|
|
|
|
|
*/
|
|
|
|
|
public static function getRequestPathSuffix( $basePath ) {
|
|
|
|
|
$basePath = rtrim( $basePath, '/' ) . '/';
|
|
|
|
|
$requestUrl = self::getGlobalRequestURL();
|
|
|
|
|
$qpos = strpos( $requestUrl, '?' );
|
|
|
|
|
if ( $qpos !== false ) {
|
|
|
|
|
$requestPath = substr( $requestUrl, 0, $qpos );
|
|
|
|
|
} else {
|
|
|
|
|
$requestPath = $requestUrl;
|
|
|
|
|
}
|
|
|
|
|
if ( substr( $requestPath, 0, strlen( $basePath ) ) !== $basePath ) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
return rawurldecode( substr( $requestPath, strlen( $basePath ) ) );
|
|
|
|
|
}
|
|
|
|
|
|
2011-06-16 05:52:16 +00:00
|
|
|
/**
|
|
|
|
|
* Work out an appropriate URL prefix containing scheme and host, based on
|
|
|
|
|
* information detected from $_SERVER
|
2011-06-30 02:59:43 +00:00
|
|
|
*
|
|
|
|
|
* @return string
|
2011-06-16 05:52:16 +00:00
|
|
|
*/
|
|
|
|
|
public static function detectServer() {
|
2015-06-22 18:59:05 +00:00
|
|
|
global $wgAssumeProxiesUseDefaultProtocolPorts;
|
|
|
|
|
|
2013-08-21 05:35:40 +00:00
|
|
|
$proto = self::detectProtocol();
|
|
|
|
|
$stdPort = $proto === 'https' ? 443 : 80;
|
2011-06-16 05:52:16 +00:00
|
|
|
|
2016-02-17 09:09:32 +00:00
|
|
|
$varNames = [ 'HTTP_HOST', 'SERVER_NAME', 'HOSTNAME', 'SERVER_ADDR' ];
|
2011-06-16 05:52:16 +00:00
|
|
|
$host = 'localhost';
|
|
|
|
|
$port = $stdPort;
|
|
|
|
|
foreach ( $varNames as $varName ) {
|
|
|
|
|
if ( !isset( $_SERVER[$varName] ) ) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
2015-06-22 18:59:05 +00:00
|
|
|
|
2019-06-25 18:53:15 +00:00
|
|
|
$parts = IPUtils::splitHostAndPort( $_SERVER[$varName] );
|
2011-06-16 05:52:16 +00:00
|
|
|
if ( !$parts ) {
|
|
|
|
|
// Invalid, do not use
|
|
|
|
|
continue;
|
|
|
|
|
}
|
2015-06-22 18:59:05 +00:00
|
|
|
|
2011-06-16 05:52:16 +00:00
|
|
|
$host = $parts[0];
|
2015-06-22 18:59:05 +00:00
|
|
|
if ( $wgAssumeProxiesUseDefaultProtocolPorts && isset( $_SERVER['HTTP_X_FORWARDED_PROTO'] ) ) {
|
2017-02-20 22:44:19 +00:00
|
|
|
// T72021: Assume that upstream proxy is running on the default
|
2014-08-26 03:44:22 +00:00
|
|
|
// port based on the protocol. We have no reliable way to determine
|
|
|
|
|
// the actual port in use upstream.
|
|
|
|
|
$port = $stdPort;
|
|
|
|
|
} elseif ( $parts[1] === false ) {
|
2011-06-16 05:52:16 +00:00
|
|
|
if ( isset( $_SERVER['SERVER_PORT'] ) ) {
|
|
|
|
|
$port = $_SERVER['SERVER_PORT'];
|
|
|
|
|
} // else leave it as $stdPort
|
|
|
|
|
} else {
|
|
|
|
|
$port = $parts[1];
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
2019-06-25 18:53:15 +00:00
|
|
|
return $proto . '://' . IPUtils::combineHostAndPort( $host, $port, $stdPort );
|
2011-06-16 05:52:16 +00:00
|
|
|
}
|
2011-08-18 00:54:06 +00:00
|
|
|
|
2011-08-31 14:47:08 +00:00
|
|
|
/**
|
2013-08-21 05:35:40 +00:00
|
|
|
* Detect the protocol from $_SERVER.
|
|
|
|
|
* This is for use prior to Setup.php, when no WebRequest object is available.
|
|
|
|
|
* At other times, use the non-static function getProtocol().
|
|
|
|
|
*
|
2016-12-08 05:04:53 +00:00
|
|
|
* @return string
|
2011-08-31 14:47:08 +00:00
|
|
|
*/
|
2013-08-21 05:35:40 +00:00
|
|
|
public static function detectProtocol() {
|
2014-03-05 11:03:17 +00:00
|
|
|
if ( ( !empty( $_SERVER['HTTPS'] ) && $_SERVER['HTTPS'] !== 'off' ) ||
|
2013-01-03 23:45:44 +00:00
|
|
|
( isset( $_SERVER['HTTP_X_FORWARDED_PROTO'] ) &&
|
2014-03-05 11:03:17 +00:00
|
|
|
$_SERVER['HTTP_X_FORWARDED_PROTO'] === 'https' ) ) {
|
2013-08-21 05:35:40 +00:00
|
|
|
return 'https';
|
2013-01-03 23:45:44 +00:00
|
|
|
} else {
|
2013-08-21 05:35:40 +00:00
|
|
|
return 'http';
|
2013-01-03 23:45:44 +00:00
|
|
|
}
|
2011-07-27 08:21:40 +00:00
|
|
|
}
|
2011-08-18 00:54:06 +00:00
|
|
|
|
2015-04-01 23:16:09 +00:00
|
|
|
/**
|
|
|
|
|
* Get the number of seconds to have elapsed since request start,
|
|
|
|
|
* in fractional seconds, with microsecond resolution.
|
|
|
|
|
*
|
|
|
|
|
* @return float
|
|
|
|
|
* @since 1.25
|
|
|
|
|
*/
|
|
|
|
|
public function getElapsedTime() {
|
|
|
|
|
return microtime( true ) - $this->requestTime;
|
|
|
|
|
}
|
|
|
|
|
|
Provide a unique request identifier
When MediaWiki encounters an unhandled exception, the error message it produces
includes a randomly-generated token, which allows the exception details to be
looked up in the error logs. This is useful but narrow: would it not be useful
to have the ability to retrieve all log records associated with a particular
request, rather than just exception details? (Hint: yes.)
So: introduce the notion of a request-global unique ID, retrievable via
WebRequest::getRequestId(). When MediaWiki is behind Apache + mod_unique_id
(which provides the same facility) or some other software which sets a
UNIQUE_ID envvar, the value of that envvar is used as the request ID.
Otherwise, it is a randomly-generated 24-character string.
The request ID supplants exception-specific IDs; MWExceptionHandler::getLogId()
is deprecated, accordingly. The request ID is also added as an annotation to
all Monolog-processed log records, and is exposed client-side as 'wgRequestId'.
This allows developers to associate a page view with log records even when the
page view does not result in an unhandled exception. (For the WMF, I also
intend to add it as an annotation to profiling data).
The request ID is not a tracking token; it does not persist, and it is
associated with a backend request, not with a particular user or a particular
session. Like the data in the NewPP report, the request ID is designed to be
cacheable, so that if, for example, a developer notices something weird in the
HTML, s/he can associate the output with a backend request regardless of
whether the response was served from the cache or directly from the backend.
Some prior art:
* https://httpd.apache.org/docs/2.4/mod/mod_unique_id.html
* http://api.rubyonrails.org/classes/ActionDispatch/RequestId.html
* https://github.com/dabapps/django-log-request-id
* https://packagist.org/packages/php-middleware/request-id
* https://github.com/rhyselsmore/flask-request-id
Change-Id: Iaf90c20c330e0470b9b98627a0228cadefd301d1
2016-03-25 01:43:23 +00:00
|
|
|
/**
|
2021-01-30 00:55:36 +00:00
|
|
|
* Get the current request ID.
|
|
|
|
|
*
|
|
|
|
|
* This is usually based on the `X-Request-Id` header, or the `UNIQUE_ID`
|
|
|
|
|
* environment variable, falling back to (process cached) randomly-generated string.
|
Provide a unique request identifier
When MediaWiki encounters an unhandled exception, the error message it produces
includes a randomly-generated token, which allows the exception details to be
looked up in the error logs. This is useful but narrow: would it not be useful
to have the ability to retrieve all log records associated with a particular
request, rather than just exception details? (Hint: yes.)
So: introduce the notion of a request-global unique ID, retrievable via
WebRequest::getRequestId(). When MediaWiki is behind Apache + mod_unique_id
(which provides the same facility) or some other software which sets a
UNIQUE_ID envvar, the value of that envvar is used as the request ID.
Otherwise, it is a randomly-generated 24-character string.
The request ID supplants exception-specific IDs; MWExceptionHandler::getLogId()
is deprecated, accordingly. The request ID is also added as an annotation to
all Monolog-processed log records, and is exposed client-side as 'wgRequestId'.
This allows developers to associate a page view with log records even when the
page view does not result in an unhandled exception. (For the WMF, I also
intend to add it as an annotation to profiling data).
The request ID is not a tracking token; it does not persist, and it is
associated with a backend request, not with a particular user or a particular
session. Like the data in the NewPP report, the request ID is designed to be
cacheable, so that if, for example, a developer notices something weird in the
HTML, s/he can associate the output with a backend request regardless of
whether the response was served from the cache or directly from the backend.
Some prior art:
* https://httpd.apache.org/docs/2.4/mod/mod_unique_id.html
* http://api.rubyonrails.org/classes/ActionDispatch/RequestId.html
* https://github.com/dabapps/django-log-request-id
* https://packagist.org/packages/php-middleware/request-id
* https://github.com/rhyselsmore/flask-request-id
Change-Id: Iaf90c20c330e0470b9b98627a0228cadefd301d1
2016-03-25 01:43:23 +00:00
|
|
|
*
|
|
|
|
|
* @return string
|
|
|
|
|
* @since 1.27
|
|
|
|
|
*/
|
|
|
|
|
public static function getRequestId() {
|
2021-01-30 00:55:36 +00:00
|
|
|
// This method is called from various error handlers and MUST be kept simple and stateless.
|
2020-03-24 01:07:53 +00:00
|
|
|
if ( !self::$reqId ) {
|
|
|
|
|
global $wgAllowExternalReqID;
|
2021-01-30 00:55:36 +00:00
|
|
|
if ( $wgAllowExternalReqID ) {
|
|
|
|
|
$id = $_SERVER['HTTP_X_REQUEST_ID'] ?? $_SERVER['UNIQUE_ID'] ?? wfRandomString( 24 );
|
|
|
|
|
} else {
|
2020-03-24 01:07:53 +00:00
|
|
|
$id = $_SERVER['UNIQUE_ID'] ?? wfRandomString( 24 );
|
2019-04-22 20:28:54 +00:00
|
|
|
}
|
2020-03-24 01:07:53 +00:00
|
|
|
self::$reqId = $id;
|
Provide a unique request identifier
When MediaWiki encounters an unhandled exception, the error message it produces
includes a randomly-generated token, which allows the exception details to be
looked up in the error logs. This is useful but narrow: would it not be useful
to have the ability to retrieve all log records associated with a particular
request, rather than just exception details? (Hint: yes.)
So: introduce the notion of a request-global unique ID, retrievable via
WebRequest::getRequestId(). When MediaWiki is behind Apache + mod_unique_id
(which provides the same facility) or some other software which sets a
UNIQUE_ID envvar, the value of that envvar is used as the request ID.
Otherwise, it is a randomly-generated 24-character string.
The request ID supplants exception-specific IDs; MWExceptionHandler::getLogId()
is deprecated, accordingly. The request ID is also added as an annotation to
all Monolog-processed log records, and is exposed client-side as 'wgRequestId'.
This allows developers to associate a page view with log records even when the
page view does not result in an unhandled exception. (For the WMF, I also
intend to add it as an annotation to profiling data).
The request ID is not a tracking token; it does not persist, and it is
associated with a backend request, not with a particular user or a particular
session. Like the data in the NewPP report, the request ID is designed to be
cacheable, so that if, for example, a developer notices something weird in the
HTML, s/he can associate the output with a backend request regardless of
whether the response was served from the cache or directly from the backend.
Some prior art:
* https://httpd.apache.org/docs/2.4/mod/mod_unique_id.html
* http://api.rubyonrails.org/classes/ActionDispatch/RequestId.html
* https://github.com/dabapps/django-log-request-id
* https://packagist.org/packages/php-middleware/request-id
* https://github.com/rhyselsmore/flask-request-id
Change-Id: Iaf90c20c330e0470b9b98627a0228cadefd301d1
2016-03-25 01:43:23 +00:00
|
|
|
}
|
|
|
|
|
|
2016-04-11 21:00:43 +00:00
|
|
|
return self::$reqId;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Override the unique request ID. This is for sub-requests, such as jobs,
|
|
|
|
|
* that wish to use the same id but are not part of the same execution context.
|
|
|
|
|
*
|
|
|
|
|
* @param string $id
|
|
|
|
|
* @since 1.27
|
|
|
|
|
*/
|
|
|
|
|
public static function overrideRequestId( $id ) {
|
|
|
|
|
self::$reqId = $id;
|
Provide a unique request identifier
When MediaWiki encounters an unhandled exception, the error message it produces
includes a randomly-generated token, which allows the exception details to be
looked up in the error logs. This is useful but narrow: would it not be useful
to have the ability to retrieve all log records associated with a particular
request, rather than just exception details? (Hint: yes.)
So: introduce the notion of a request-global unique ID, retrievable via
WebRequest::getRequestId(). When MediaWiki is behind Apache + mod_unique_id
(which provides the same facility) or some other software which sets a
UNIQUE_ID envvar, the value of that envvar is used as the request ID.
Otherwise, it is a randomly-generated 24-character string.
The request ID supplants exception-specific IDs; MWExceptionHandler::getLogId()
is deprecated, accordingly. The request ID is also added as an annotation to
all Monolog-processed log records, and is exposed client-side as 'wgRequestId'.
This allows developers to associate a page view with log records even when the
page view does not result in an unhandled exception. (For the WMF, I also
intend to add it as an annotation to profiling data).
The request ID is not a tracking token; it does not persist, and it is
associated with a backend request, not with a particular user or a particular
session. Like the data in the NewPP report, the request ID is designed to be
cacheable, so that if, for example, a developer notices something weird in the
HTML, s/he can associate the output with a backend request regardless of
whether the response was served from the cache or directly from the backend.
Some prior art:
* https://httpd.apache.org/docs/2.4/mod/mod_unique_id.html
* http://api.rubyonrails.org/classes/ActionDispatch/RequestId.html
* https://github.com/dabapps/django-log-request-id
* https://packagist.org/packages/php-middleware/request-id
* https://github.com/rhyselsmore/flask-request-id
Change-Id: Iaf90c20c330e0470b9b98627a0228cadefd301d1
2016-03-25 01:43:23 +00:00
|
|
|
}
|
|
|
|
|
|
2011-08-31 14:47:08 +00:00
|
|
|
/**
|
2013-08-21 05:35:40 +00:00
|
|
|
* Get the current URL protocol (http or https)
|
2011-08-31 14:47:08 +00:00
|
|
|
* @return string
|
|
|
|
|
*/
|
2013-08-21 05:35:40 +00:00
|
|
|
public function getProtocol() {
|
|
|
|
|
if ( $this->protocol === null ) {
|
|
|
|
|
$this->protocol = self::detectProtocol();
|
|
|
|
|
}
|
|
|
|
|
return $this->protocol;
|
2011-07-27 08:21:40 +00:00
|
|
|
}
|
2011-06-16 05:52:16 +00:00
|
|
|
|
2007-05-17 20:02:59 +00:00
|
|
|
/**
|
|
|
|
|
* Check for title, action, and/or variant data in the URL
|
|
|
|
|
* and interpolate it into the GET variables.
|
2018-07-29 12:24:54 +00:00
|
|
|
* This should only be run after the content language is available,
|
2007-05-17 20:02:59 +00:00
|
|
|
* as we may need the list of language variants to determine
|
|
|
|
|
* available variant URLs.
|
|
|
|
|
*/
|
2009-07-25 00:14:34 +00:00
|
|
|
public function interpolateTitle() {
|
2012-03-24 08:25:01 +00:00
|
|
|
$matches = self::getPathInfo( 'title' );
|
2013-04-20 22:49:30 +00:00
|
|
|
foreach ( $matches as $key => $val ) {
|
2019-09-03 00:25:19 +00:00
|
|
|
$this->data[$key] = $this->queryAndPathParams[$key] = $val;
|
2004-09-02 07:50:04 +00:00
|
|
|
}
|
2004-03-08 09:09:35 +00:00
|
|
|
}
|
2008-04-14 07:45:50 +00:00
|
|
|
|
2007-05-17 20:02:59 +00:00
|
|
|
/**
|
2011-08-12 19:23:43 +00:00
|
|
|
* URL rewriting function; tries to extract page title and,
|
2007-05-17 20:02:59 +00:00
|
|
|
* optionally, one other fixed parameter value from a URL path.
|
|
|
|
|
*
|
2014-04-23 09:37:59 +00:00
|
|
|
* @param string $path The URL path given from the client
|
|
|
|
|
* @param array $bases One or more URLs, optionally with $1 at the end
|
2020-11-28 14:18:24 +00:00
|
|
|
* @param string|false $key If provided, the matching key in $bases will be
|
2014-04-23 09:37:59 +00:00
|
|
|
* passed on as the value of this URL parameter
|
|
|
|
|
* @return array Array of URL variables to interpolate; empty if no match
|
2007-05-17 20:02:59 +00:00
|
|
|
*/
|
2019-11-30 22:32:44 +00:00
|
|
|
public static function extractTitle( $path, $bases, $key = false ) {
|
2013-04-20 22:49:30 +00:00
|
|
|
foreach ( (array)$bases as $keyValue => $base ) {
|
2007-05-14 13:56:26 +00:00
|
|
|
// Find the part after $wgArticlePath
|
2007-05-17 20:02:59 +00:00
|
|
|
$base = str_replace( '$1', '', $base );
|
|
|
|
|
$baseLen = strlen( $base );
|
2013-04-20 22:49:30 +00:00
|
|
|
if ( substr( $path, 0, $baseLen ) == $base ) {
|
2007-05-17 20:02:59 +00:00
|
|
|
$raw = substr( $path, $baseLen );
|
2013-04-20 22:49:30 +00:00
|
|
|
if ( $raw !== '' ) {
|
2016-02-17 09:09:32 +00:00
|
|
|
$matches = [ 'title' => rawurldecode( $raw ) ];
|
2013-04-20 22:49:30 +00:00
|
|
|
if ( $key ) {
|
2007-05-17 20:02:59 +00:00
|
|
|
$matches[$key] = $keyValue;
|
|
|
|
|
}
|
|
|
|
|
return $matches;
|
|
|
|
|
}
|
2007-05-14 13:56:26 +00:00
|
|
|
}
|
|
|
|
|
}
|
2016-02-17 09:09:32 +00:00
|
|
|
return [];
|
2007-05-14 13:56:26 +00:00
|
|
|
}
|
2004-03-08 09:09:35 +00:00
|
|
|
|
2004-09-04 00:12:08 +00:00
|
|
|
/**
|
|
|
|
|
* Recursively normalizes UTF-8 strings in the given array.
|
2010-05-28 20:20:00 +00:00
|
|
|
*
|
2014-04-23 09:37:59 +00:00
|
|
|
* @param string|array $data
|
|
|
|
|
* @return array|string Cleaned-up version of the given
|
2020-06-26 12:14:23 +00:00
|
|
|
* @internal
|
2004-09-04 00:12:08 +00:00
|
|
|
*/
|
2016-09-09 04:58:09 +00:00
|
|
|
public function normalizeUnicode( $data ) {
|
2013-04-20 22:49:30 +00:00
|
|
|
if ( is_array( $data ) ) {
|
|
|
|
|
foreach ( $data as $key => $val ) {
|
2004-09-06 03:01:33 +00:00
|
|
|
$data[$key] = $this->normalizeUnicode( $val );
|
2004-09-02 08:01:13 +00:00
|
|
|
}
|
2004-09-06 03:01:33 +00:00
|
|
|
} else {
|
2018-07-29 12:24:54 +00:00
|
|
|
$contLang = MediaWikiServices::getInstance()->getContentLanguage();
|
2020-06-16 18:11:25 +00:00
|
|
|
$data = $contLang->normalize( $data );
|
2004-09-02 07:50:04 +00:00
|
|
|
}
|
2004-09-06 03:01:33 +00:00
|
|
|
return $data;
|
2004-09-02 07:50:04 +00:00
|
|
|
}
|
2006-01-07 13:31:29 +00:00
|
|
|
|
2004-09-04 00:12:08 +00:00
|
|
|
/**
|
|
|
|
|
* Fetch a value from the given array or return $default if it's not set.
|
2005-04-07 14:13:06 +00:00
|
|
|
*
|
2014-04-23 09:37:59 +00:00
|
|
|
* @param array $arr
|
|
|
|
|
* @param string $name
|
|
|
|
|
* @param mixed $default
|
2004-09-04 08:34:51 +00:00
|
|
|
* @return mixed
|
2004-09-04 00:12:08 +00:00
|
|
|
*/
|
2009-07-25 00:14:34 +00:00
|
|
|
private function getGPCVal( $arr, $name, $default ) {
|
2009-07-24 13:35:24 +00:00
|
|
|
# PHP is so nice to not touch input data, except sometimes:
|
2019-04-12 04:41:14 +00:00
|
|
|
# https://www.php.net/variables.external#language.variables.external.dot-in-names
|
2009-07-24 13:35:24 +00:00
|
|
|
# Work around PHP *feature* to avoid *bugs* elsewhere.
|
|
|
|
|
$name = strtr( $name, '.', '_' );
|
2019-07-31 22:48:45 +00:00
|
|
|
|
|
|
|
|
if ( !isset( $arr[$name] ) ) {
|
|
|
|
|
return $default;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$data = $arr[$name];
|
|
|
|
|
# Optimisation: Skip UTF-8 normalization and legacy transcoding for simple ASCII strings.
|
|
|
|
|
$isAsciiStr = ( is_string( $data ) && preg_match( '/[^\x20-\x7E]/', $data ) === 0 );
|
|
|
|
|
if ( !$isAsciiStr ) {
|
2019-03-21 19:36:52 +00:00
|
|
|
if ( isset( $_GET[$name] ) && is_string( $data ) ) {
|
2005-06-27 02:04:44 +00:00
|
|
|
# Check for alternate/legacy character encoding.
|
2019-07-31 22:48:45 +00:00
|
|
|
$data = MediaWikiServices::getInstance()
|
|
|
|
|
->getContentLanguage()
|
|
|
|
|
->checkTitleEncoding( $data );
|
2004-09-06 03:01:33 +00:00
|
|
|
}
|
2005-03-26 22:23:48 +00:00
|
|
|
$data = $this->normalizeUnicode( $data );
|
2004-03-08 09:09:35 +00:00
|
|
|
}
|
2019-07-31 22:48:45 +00:00
|
|
|
|
|
|
|
|
return $data;
|
2004-03-08 09:09:35 +00:00
|
|
|
}
|
2004-12-18 11:18:56 +00:00
|
|
|
|
2016-08-16 20:36:27 +00:00
|
|
|
/**
|
2021-08-10 21:36:25 +00:00
|
|
|
* Fetch a string WITHOUT any Unicode or line break normalization. This is a fast alternative
|
|
|
|
|
* for values that are known to be simple, e.g. pure ASCII. When reading user input, use
|
|
|
|
|
* {@see getText} instead.
|
2016-08-16 20:36:27 +00:00
|
|
|
*
|
2021-08-10 21:36:25 +00:00
|
|
|
* Array values are discarded for security reasons. Use {@see getArray} or {@see getIntArray}.
|
2016-08-16 20:36:27 +00:00
|
|
|
*
|
|
|
|
|
* @since 1.28
|
|
|
|
|
* @param string $name
|
2017-12-28 15:06:10 +00:00
|
|
|
* @param string|null $default
|
2021-08-10 21:36:25 +00:00
|
|
|
* @return string|null The value, or $default if none set
|
2016-08-16 20:36:27 +00:00
|
|
|
*/
|
|
|
|
|
public function getRawVal( $name, $default = null ) {
|
|
|
|
|
$name = strtr( $name, '.', '_' ); // See comment in self::getGPCVal()
|
|
|
|
|
if ( isset( $this->data[$name] ) && !is_array( $this->data[$name] ) ) {
|
|
|
|
|
$val = $this->data[$name];
|
|
|
|
|
} else {
|
|
|
|
|
$val = $default;
|
|
|
|
|
}
|
2021-08-10 21:36:25 +00:00
|
|
|
|
|
|
|
|
return $val === null ? null : (string)$val;
|
2016-08-16 20:36:27 +00:00
|
|
|
}
|
|
|
|
|
|
2004-09-04 00:12:08 +00:00
|
|
|
/**
|
2021-08-10 21:36:25 +00:00
|
|
|
* Fetch a text string and partially normalized it.
|
|
|
|
|
*
|
|
|
|
|
* Use of this method is discouraged. It doesn't normalize line breaks and defaults to null
|
|
|
|
|
* instead of the empty string. Instead:
|
|
|
|
|
* - Use {@see getText} when reading user input or form fields that are expected to contain
|
|
|
|
|
* non-ASCII characters.
|
|
|
|
|
* - Use {@see getRawVal} when reading ASCII strings, such as parameters used to select
|
|
|
|
|
* predefined behaviour in the software.
|
|
|
|
|
*
|
|
|
|
|
* Array values are discarded for security reasons. Use {@see getArray} or {@see getIntArray}.
|
2004-12-18 11:18:56 +00:00
|
|
|
*
|
2014-04-23 09:37:59 +00:00
|
|
|
* @param string $name
|
2021-08-10 21:36:25 +00:00
|
|
|
* @param string|null $default
|
|
|
|
|
* @return string|null The input value, or $default if none set
|
2004-09-04 00:12:08 +00:00
|
|
|
*/
|
2009-12-11 21:07:27 +00:00
|
|
|
public function getVal( $name, $default = null ) {
|
2008-02-12 22:07:16 +00:00
|
|
|
$val = $this->getGPCVal( $this->data, $name, $default );
|
2013-04-20 22:49:30 +00:00
|
|
|
if ( is_array( $val ) ) {
|
2004-12-18 11:18:56 +00:00
|
|
|
$val = $default;
|
|
|
|
|
}
|
2021-08-10 21:36:25 +00:00
|
|
|
|
|
|
|
|
return $val === null ? null : (string)$val;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Fetch a text string and return it in normalized form.
|
|
|
|
|
*
|
|
|
|
|
* This normalizes Unicode sequences (via {@see getGPCVal}) and line breaks.
|
|
|
|
|
*
|
|
|
|
|
* This should be used for all user input and form fields that are expected to contain non-ASCII
|
|
|
|
|
* characters, especially if the value will be stored or compared against stored values. Without
|
|
|
|
|
* normalization, logically identically values might not match when they are typed on different
|
|
|
|
|
* OS' or keyboards.
|
|
|
|
|
*
|
|
|
|
|
* Array values are discarded for security reasons. Use {@see getArray} or {@see getIntArray}.
|
|
|
|
|
*
|
|
|
|
|
* @param string $name
|
|
|
|
|
* @param string $default
|
|
|
|
|
* @return string The normalized input value, or $default if none set
|
|
|
|
|
*/
|
|
|
|
|
public function getText( $name, $default = '' ) {
|
|
|
|
|
$val = $this->getVal( $name, $default );
|
|
|
|
|
return str_replace( "\r\n", "\n", $val );
|
2004-03-08 09:09:35 +00:00
|
|
|
}
|
2010-01-06 03:42:30 +00:00
|
|
|
|
2008-09-30 15:13:13 +00:00
|
|
|
/**
|
2011-06-10 11:32:28 +00:00
|
|
|
* Set an arbitrary value into our get/post data.
|
2010-05-28 20:20:00 +00:00
|
|
|
*
|
2014-04-23 09:37:59 +00:00
|
|
|
* @param string $key Key name to use
|
|
|
|
|
* @param mixed $value Value to set
|
|
|
|
|
* @return mixed Old value if one was present, null otherwise
|
2008-09-30 15:13:13 +00:00
|
|
|
*/
|
2009-07-25 00:14:34 +00:00
|
|
|
public function setVal( $key, $value ) {
|
2017-10-06 22:17:58 +00:00
|
|
|
$ret = $this->data[$key] ?? null;
|
2008-09-30 15:13:13 +00:00
|
|
|
$this->data[$key] = $value;
|
|
|
|
|
return $ret;
|
|
|
|
|
}
|
2006-01-07 13:31:29 +00:00
|
|
|
|
2012-02-29 21:56:05 +00:00
|
|
|
/**
|
|
|
|
|
* Unset an arbitrary value from our get/post data.
|
2012-10-26 15:42:13 +00:00
|
|
|
*
|
2014-04-23 09:37:59 +00:00
|
|
|
* @param string $key Key name to use
|
|
|
|
|
* @return mixed Old value if one was present, null otherwise
|
2012-02-29 21:56:05 +00:00
|
|
|
*/
|
|
|
|
|
public function unsetVal( $key ) {
|
|
|
|
|
if ( !isset( $this->data[$key] ) ) {
|
|
|
|
|
$ret = null;
|
|
|
|
|
} else {
|
|
|
|
|
$ret = $this->data[$key];
|
|
|
|
|
unset( $this->data[$key] );
|
|
|
|
|
}
|
|
|
|
|
return $ret;
|
|
|
|
|
}
|
|
|
|
|
|
2004-09-04 00:12:08 +00:00
|
|
|
/**
|
2004-12-18 11:18:56 +00:00
|
|
|
* Fetch an array from the input or return $default if it's not set.
|
|
|
|
|
* If source was scalar, will return an array with a single element.
|
2014-04-23 11:39:49 +00:00
|
|
|
* If no source and no default, returns null.
|
2004-12-18 11:18:56 +00:00
|
|
|
*
|
2014-04-23 09:37:59 +00:00
|
|
|
* @param string $name
|
2018-06-26 21:14:43 +00:00
|
|
|
* @param array|null $default Optional default (or null)
|
2017-02-20 21:19:47 +00:00
|
|
|
* @return array|null
|
2004-09-04 00:12:08 +00:00
|
|
|
*/
|
2009-12-11 21:07:27 +00:00
|
|
|
public function getArray( $name, $default = null ) {
|
2008-02-12 22:07:16 +00:00
|
|
|
$val = $this->getGPCVal( $this->data, $name, $default );
|
2020-01-09 23:48:34 +00:00
|
|
|
if ( $val === null ) {
|
2004-12-18 11:18:56 +00:00
|
|
|
return null;
|
|
|
|
|
} else {
|
|
|
|
|
return (array)$val;
|
|
|
|
|
}
|
2004-03-08 09:09:35 +00:00
|
|
|
}
|
2008-04-14 07:45:50 +00:00
|
|
|
|
2006-03-16 19:04:25 +00:00
|
|
|
/**
|
|
|
|
|
* Fetch an array of integers, or return $default if it's not set.
|
|
|
|
|
* If source was scalar, will return an array with a single element.
|
2014-04-23 11:39:49 +00:00
|
|
|
* If no source and no default, returns null.
|
2006-03-16 19:04:25 +00:00
|
|
|
* If an array is returned, contents are guaranteed to be integers.
|
|
|
|
|
*
|
2014-04-23 09:37:59 +00:00
|
|
|
* @param string $name
|
2018-06-26 21:14:43 +00:00
|
|
|
* @param array|null $default Option default (or null)
|
2019-09-15 13:22:08 +00:00
|
|
|
* @return int[]|null
|
2006-03-16 19:04:25 +00:00
|
|
|
*/
|
2009-12-11 21:07:27 +00:00
|
|
|
public function getIntArray( $name, $default = null ) {
|
2006-03-16 19:04:25 +00:00
|
|
|
$val = $this->getArray( $name, $default );
|
2013-04-20 22:49:30 +00:00
|
|
|
if ( is_array( $val ) ) {
|
2006-03-16 19:04:25 +00:00
|
|
|
$val = array_map( 'intval', $val );
|
|
|
|
|
}
|
|
|
|
|
return $val;
|
|
|
|
|
}
|
2004-12-18 11:18:56 +00:00
|
|
|
|
2004-09-04 00:12:08 +00:00
|
|
|
/**
|
|
|
|
|
* Fetch an integer value from the input or return $default if not set.
|
2004-09-04 08:34:51 +00:00
|
|
|
* Guaranteed to return an integer; non-numeric input will typically
|
2004-09-04 00:12:08 +00:00
|
|
|
* return 0.
|
2010-05-28 20:20:00 +00:00
|
|
|
*
|
2014-04-23 09:37:59 +00:00
|
|
|
* @param string $name
|
|
|
|
|
* @param int $default
|
|
|
|
|
* @return int
|
2004-09-04 00:12:08 +00:00
|
|
|
*/
|
2009-07-25 00:14:34 +00:00
|
|
|
public function getInt( $name, $default = 0 ) {
|
2016-09-09 22:46:08 +00:00
|
|
|
return intval( $this->getRawVal( $name, $default ) );
|
2004-03-08 09:09:35 +00:00
|
|
|
}
|
2006-01-07 13:31:29 +00:00
|
|
|
|
2005-06-29 06:16:03 +00:00
|
|
|
/**
|
|
|
|
|
* Fetch an integer value from the input or return null if empty.
|
|
|
|
|
* Guaranteed to return an integer or null; non-numeric input will
|
|
|
|
|
* typically return null.
|
2010-05-28 20:20:00 +00:00
|
|
|
*
|
2014-04-23 09:37:59 +00:00
|
|
|
* @param string $name
|
|
|
|
|
* @return int|null
|
2005-06-29 06:16:03 +00:00
|
|
|
*/
|
2009-07-25 00:14:34 +00:00
|
|
|
public function getIntOrNull( $name ) {
|
2016-09-09 22:46:08 +00:00
|
|
|
$val = $this->getRawVal( $name );
|
2005-06-29 06:16:03 +00:00
|
|
|
return is_numeric( $val )
|
2005-08-16 23:36:16 +00:00
|
|
|
? intval( $val )
|
2005-06-29 06:16:03 +00:00
|
|
|
: null;
|
|
|
|
|
}
|
2006-01-07 13:31:29 +00:00
|
|
|
|
2013-10-12 15:34:15 +00:00
|
|
|
/**
|
|
|
|
|
* Fetch a floating point value from the input or return $default if not set.
|
|
|
|
|
* Guaranteed to return a float; non-numeric input will typically
|
|
|
|
|
* return 0.
|
|
|
|
|
*
|
|
|
|
|
* @since 1.23
|
2014-04-23 09:37:59 +00:00
|
|
|
* @param string $name
|
|
|
|
|
* @param float $default
|
|
|
|
|
* @return float
|
2013-10-12 15:34:15 +00:00
|
|
|
*/
|
2014-05-24 20:06:01 +00:00
|
|
|
public function getFloat( $name, $default = 0.0 ) {
|
2016-09-09 22:46:08 +00:00
|
|
|
return floatval( $this->getRawVal( $name, $default ) );
|
2013-10-12 15:34:15 +00:00
|
|
|
}
|
|
|
|
|
|
2004-09-04 00:12:08 +00:00
|
|
|
/**
|
|
|
|
|
* Fetch a boolean value from the input or return $default if not set.
|
|
|
|
|
* Guaranteed to return true or false, with normal PHP semantics for
|
|
|
|
|
* boolean interpretation of strings.
|
2010-05-28 20:20:00 +00:00
|
|
|
*
|
2014-04-23 09:37:59 +00:00
|
|
|
* @param string $name
|
|
|
|
|
* @param bool $default
|
|
|
|
|
* @return bool
|
2004-09-04 00:12:08 +00:00
|
|
|
*/
|
2009-07-25 00:14:34 +00:00
|
|
|
public function getBool( $name, $default = false ) {
|
2016-09-09 22:46:08 +00:00
|
|
|
return (bool)$this->getRawVal( $name, $default );
|
2004-03-08 09:09:35 +00:00
|
|
|
}
|
2011-02-01 23:08:10 +00:00
|
|
|
|
2010-09-22 20:15:31 +00:00
|
|
|
/**
|
|
|
|
|
* Fetch a boolean value from the input or return $default if not set.
|
|
|
|
|
* Unlike getBool, the string "false" will result in boolean false, which is
|
|
|
|
|
* useful when interpreting information sent from JavaScript.
|
|
|
|
|
*
|
2014-04-23 09:37:59 +00:00
|
|
|
* @param string $name
|
|
|
|
|
* @param bool $default
|
|
|
|
|
* @return bool
|
2010-09-22 20:15:31 +00:00
|
|
|
*/
|
|
|
|
|
public function getFuzzyBool( $name, $default = false ) {
|
2016-09-09 22:46:08 +00:00
|
|
|
return $this->getBool( $name, $default )
|
|
|
|
|
&& strcasecmp( $this->getRawVal( $name ), 'false' ) !== 0;
|
2010-09-22 20:15:31 +00:00
|
|
|
}
|
2006-01-07 13:31:29 +00:00
|
|
|
|
2004-09-04 00:12:08 +00:00
|
|
|
/**
|
|
|
|
|
* Return true if the named value is set in the input, whatever that
|
|
|
|
|
* value is (even "0"). Return false if the named value is not set.
|
|
|
|
|
* Example use is checking for the presence of check boxes in forms.
|
2010-05-28 20:20:00 +00:00
|
|
|
*
|
2014-04-23 09:37:59 +00:00
|
|
|
* @param string $name
|
|
|
|
|
* @return bool
|
2004-09-04 00:12:08 +00:00
|
|
|
*/
|
2009-07-25 00:14:34 +00:00
|
|
|
public function getCheck( $name ) {
|
2004-03-08 09:09:35 +00:00
|
|
|
# Checkboxes and buttons are only present when clicked
|
2013-03-13 07:42:41 +00:00
|
|
|
# Presence connotes truth, absence false
|
2016-09-09 22:46:08 +00:00
|
|
|
return $this->getRawVal( $name, null ) !== null;
|
2004-03-08 09:09:35 +00:00
|
|
|
}
|
2006-01-07 13:31:29 +00:00
|
|
|
|
2004-09-04 00:12:08 +00:00
|
|
|
/**
|
2021-01-05 01:24:11 +00:00
|
|
|
* Extracts the (given) named values into an array.
|
2004-09-04 00:12:08 +00:00
|
|
|
* No transformation is performed on the values.
|
2011-06-30 02:59:43 +00:00
|
|
|
*
|
2021-01-05 01:24:11 +00:00
|
|
|
* @param string ...$names If no arguments are given, returns all input values
|
2011-06-30 02:59:43 +00:00
|
|
|
* @return array
|
2004-09-04 00:12:08 +00:00
|
|
|
*/
|
2021-01-05 01:24:11 +00:00
|
|
|
public function getValues( ...$names ) {
|
|
|
|
|
if ( $names === [] ) {
|
2008-02-12 22:07:16 +00:00
|
|
|
$names = array_keys( $this->data );
|
2004-07-25 11:49:05 +00:00
|
|
|
}
|
|
|
|
|
|
2016-02-17 09:09:32 +00:00
|
|
|
$retVal = [];
|
2006-01-07 13:09:30 +00:00
|
|
|
foreach ( $names as $name ) {
|
2012-03-10 00:31:54 +00:00
|
|
|
$value = $this->getGPCVal( $this->data, $name, null );
|
2020-01-09 23:48:34 +00:00
|
|
|
if ( $value !== null ) {
|
2004-03-29 14:48:07 +00:00
|
|
|
$retVal[$name] = $value;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return $retVal;
|
|
|
|
|
}
|
|
|
|
|
|
2011-07-13 18:05:44 +00:00
|
|
|
/**
|
|
|
|
|
* Returns the names of all input values excluding those in $exclude.
|
|
|
|
|
*
|
2014-04-23 09:37:59 +00:00
|
|
|
* @param array $exclude
|
2011-07-13 18:05:44 +00:00
|
|
|
* @return array
|
|
|
|
|
*/
|
2016-02-17 09:09:32 +00:00
|
|
|
public function getValueNames( $exclude = [] ) {
|
2011-07-13 18:05:44 +00:00
|
|
|
return array_diff( array_keys( $this->getValues() ), $exclude );
|
|
|
|
|
}
|
|
|
|
|
|
2011-02-23 20:23:35 +00:00
|
|
|
/**
|
2019-09-03 00:25:19 +00:00
|
|
|
* Get the values passed in the query string and the path router parameters.
|
2011-02-23 20:23:35 +00:00
|
|
|
* No transformation is performed on the values.
|
|
|
|
|
*
|
2016-09-09 04:58:09 +00:00
|
|
|
* @codeCoverageIgnore
|
2020-11-28 14:18:24 +00:00
|
|
|
* @return string[]
|
2011-02-23 20:23:35 +00:00
|
|
|
*/
|
2013-02-03 20:05:24 +00:00
|
|
|
public function getQueryValues() {
|
2019-09-03 00:25:19 +00:00
|
|
|
return $this->queryAndPathParams;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get the values passed in the query string only, not including the path
|
|
|
|
|
* router parameters. This is less suitable for self-links to index.php but
|
|
|
|
|
* useful for other entry points. No transformation is performed on the
|
|
|
|
|
* values.
|
|
|
|
|
*
|
|
|
|
|
* @since 1.34
|
2020-11-28 14:18:24 +00:00
|
|
|
* @return string[]
|
2019-09-03 00:25:19 +00:00
|
|
|
*/
|
|
|
|
|
public function getQueryValuesOnly() {
|
|
|
|
|
return $this->queryParams;
|
2013-02-03 20:05:24 +00:00
|
|
|
}
|
2011-02-23 20:23:35 +00:00
|
|
|
|
2018-05-19 21:41:41 +00:00
|
|
|
/**
|
|
|
|
|
* Get the values passed via POST.
|
|
|
|
|
* No transformation is performed on the values.
|
|
|
|
|
*
|
|
|
|
|
* @since 1.32
|
|
|
|
|
* @codeCoverageIgnore
|
2020-11-28 14:18:24 +00:00
|
|
|
* @return string[]
|
2018-05-19 21:41:41 +00:00
|
|
|
*/
|
|
|
|
|
public function getPostValues() {
|
|
|
|
|
return $_POST;
|
|
|
|
|
}
|
|
|
|
|
|
2013-06-26 21:10:52 +00:00
|
|
|
/**
|
|
|
|
|
* Return the contents of the Query with no decoding. Use when you need to
|
|
|
|
|
* know exactly what was sent, e.g. for an OAuth signature over the elements.
|
|
|
|
|
*
|
2016-09-09 04:58:09 +00:00
|
|
|
* @codeCoverageIgnore
|
2014-04-23 09:37:59 +00:00
|
|
|
* @return string
|
2013-06-26 21:10:52 +00:00
|
|
|
*/
|
|
|
|
|
public function getRawQueryString() {
|
|
|
|
|
return $_SERVER['QUERY_STRING'];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Return the contents of the POST with no decoding. Use when you need to
|
|
|
|
|
* know exactly what was sent, e.g. for an OAuth signature over the elements.
|
|
|
|
|
*
|
2014-04-23 09:37:59 +00:00
|
|
|
* @return string
|
2013-06-26 21:10:52 +00:00
|
|
|
*/
|
|
|
|
|
public function getRawPostString() {
|
|
|
|
|
if ( !$this->wasPosted() ) {
|
|
|
|
|
return '';
|
|
|
|
|
}
|
|
|
|
|
return $this->getRawInput();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Return the raw request body, with no processing. Cached since some methods
|
|
|
|
|
* disallow reading the stream more than once. As stated in the php docs, this
|
|
|
|
|
* does not work with enctype="multipart/form-data".
|
|
|
|
|
*
|
2014-04-23 09:37:59 +00:00
|
|
|
* @return string
|
2013-06-26 21:10:52 +00:00
|
|
|
*/
|
|
|
|
|
public function getRawInput() {
|
2014-05-01 19:58:46 +00:00
|
|
|
static $input = null;
|
|
|
|
|
if ( $input === null ) {
|
2013-06-26 21:10:52 +00:00
|
|
|
$input = file_get_contents( 'php://input' );
|
|
|
|
|
}
|
|
|
|
|
return $input;
|
|
|
|
|
}
|
|
|
|
|
|
2012-08-07 06:33:41 +00:00
|
|
|
/**
|
|
|
|
|
* Get the HTTP method used for this request.
|
|
|
|
|
*
|
2014-04-23 09:37:59 +00:00
|
|
|
* @return string
|
2012-08-07 06:33:41 +00:00
|
|
|
*/
|
|
|
|
|
public function getMethod() {
|
2017-10-06 22:17:58 +00:00
|
|
|
return $_SERVER['REQUEST_METHOD'] ?? 'GET';
|
2012-08-07 06:33:41 +00:00
|
|
|
}
|
|
|
|
|
|
2004-09-04 00:12:08 +00:00
|
|
|
/**
|
|
|
|
|
* Returns true if the present request was reached by a POST operation,
|
|
|
|
|
* false otherwise (GET, HEAD, or command-line).
|
|
|
|
|
*
|
|
|
|
|
* Note that values retrieved by the object may come from the
|
|
|
|
|
* GET URL etc even on a POST request.
|
2004-09-04 08:34:51 +00:00
|
|
|
*
|
2014-04-23 09:37:59 +00:00
|
|
|
* @return bool
|
2004-09-04 00:12:08 +00:00
|
|
|
*/
|
2009-07-25 00:14:34 +00:00
|
|
|
public function wasPosted() {
|
2012-08-07 06:33:41 +00:00
|
|
|
return $this->getMethod() == 'POST';
|
2004-03-08 09:09:35 +00:00
|
|
|
}
|
2006-01-07 13:31:29 +00:00
|
|
|
|
2004-09-04 00:12:08 +00:00
|
|
|
/**
|
2016-02-01 20:44:03 +00:00
|
|
|
* Return the session for this request
|
2016-08-27 05:40:37 +00:00
|
|
|
*
|
|
|
|
|
* This might unpersist an existing session if it was invalid.
|
|
|
|
|
*
|
2016-02-01 20:44:03 +00:00
|
|
|
* @since 1.27
|
|
|
|
|
* @note For performance, keep the session locally if you will be making
|
|
|
|
|
* much use of it instead of calling this method repeatedly.
|
2016-04-06 22:22:33 +00:00
|
|
|
* @return Session
|
2016-02-01 20:44:03 +00:00
|
|
|
*/
|
|
|
|
|
public function getSession() {
|
|
|
|
|
if ( $this->sessionId !== null ) {
|
|
|
|
|
$session = SessionManager::singleton()->getSessionById( (string)$this->sessionId, true, $this );
|
|
|
|
|
if ( $session ) {
|
|
|
|
|
return $session;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$session = SessionManager::singleton()->getSessionForRequest( $this );
|
|
|
|
|
$this->sessionId = $session->getSessionId();
|
|
|
|
|
return $session;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Set the session for this request
|
|
|
|
|
* @since 1.27
|
2020-06-26 12:14:23 +00:00
|
|
|
* @internal For use by MediaWiki\Session classes only
|
2016-04-06 22:22:33 +00:00
|
|
|
* @param SessionId $sessionId
|
2016-02-01 20:44:03 +00:00
|
|
|
*/
|
2016-04-06 22:22:33 +00:00
|
|
|
public function setSessionId( SessionId $sessionId ) {
|
2016-02-01 20:44:03 +00:00
|
|
|
$this->sessionId = $sessionId;
|
|
|
|
|
}
|
|
|
|
|
|
2016-02-26 21:17:37 +00:00
|
|
|
/**
|
|
|
|
|
* Get the session id for this request, if any
|
|
|
|
|
* @since 1.27
|
2020-06-26 12:14:23 +00:00
|
|
|
* @internal For use by MediaWiki\Session classes only
|
2016-04-06 22:22:33 +00:00
|
|
|
* @return SessionId|null
|
2016-02-26 21:17:37 +00:00
|
|
|
*/
|
|
|
|
|
public function getSessionId() {
|
|
|
|
|
return $this->sessionId;
|
|
|
|
|
}
|
|
|
|
|
|
2010-05-26 00:50:46 +00:00
|
|
|
/**
|
|
|
|
|
* Get a cookie from the $_COOKIE jar
|
2010-05-28 20:20:00 +00:00
|
|
|
*
|
2014-04-23 09:37:59 +00:00
|
|
|
* @param string $key The name of the cookie
|
2018-06-26 21:14:43 +00:00
|
|
|
* @param string|null $prefix A prefix to use for the cookie name, if not $wgCookiePrefix
|
|
|
|
|
* @param mixed|null $default What to return if the value isn't found
|
2014-04-23 09:37:59 +00:00
|
|
|
* @return mixed Cookie value or $default if the cookie not set
|
2010-05-26 00:50:46 +00:00
|
|
|
*/
|
2010-08-06 15:00:43 +00:00
|
|
|
public function getCookie( $key, $prefix = null, $default = null ) {
|
2013-04-20 22:49:30 +00:00
|
|
|
if ( $prefix === null ) {
|
2010-05-26 00:50:46 +00:00
|
|
|
global $wgCookiePrefix;
|
|
|
|
|
$prefix = $wgCookiePrefix;
|
|
|
|
|
}
|
2020-07-01 00:01:00 +00:00
|
|
|
$name = $prefix . $key;
|
|
|
|
|
// Work around mangling of $_COOKIE
|
|
|
|
|
$name = strtr( $name, '.', '_' );
|
|
|
|
|
if ( isset( $_COOKIE[$name] ) ) {
|
|
|
|
|
return $_COOKIE[$name];
|
|
|
|
|
} else {
|
|
|
|
|
return $default;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get a cookie set with SameSite=None possibly with a legacy fallback cookie.
|
|
|
|
|
*
|
|
|
|
|
* @param string $key The name of the cookie
|
|
|
|
|
* @param string $prefix A prefix to use, empty by default
|
|
|
|
|
* @param mixed|null $default What to return if the value isn't found
|
|
|
|
|
* @return mixed Cookie value or $default if the cookie is not set
|
|
|
|
|
*/
|
|
|
|
|
public function getCrossSiteCookie( $key, $prefix = '', $default = null ) {
|
|
|
|
|
global $wgUseSameSiteLegacyCookies;
|
|
|
|
|
$name = $prefix . $key;
|
|
|
|
|
// Work around mangling of $_COOKIE
|
|
|
|
|
$name = strtr( $name, '.', '_' );
|
|
|
|
|
if ( isset( $_COOKIE[$name] ) ) {
|
|
|
|
|
return $_COOKIE[$name];
|
|
|
|
|
}
|
|
|
|
|
if ( $wgUseSameSiteLegacyCookies ) {
|
|
|
|
|
$legacyName = $prefix . "ss0-" . $key;
|
|
|
|
|
$legacyName = strtr( $legacyName, '.', '_' );
|
|
|
|
|
if ( isset( $_COOKIE[$legacyName] ) ) {
|
|
|
|
|
return $_COOKIE[$legacyName];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return $default;
|
2010-05-26 00:50:46 +00:00
|
|
|
}
|
|
|
|
|
|
2004-09-04 00:12:08 +00:00
|
|
|
/**
|
2016-03-08 17:46:05 +00:00
|
|
|
* Return the path and query string portion of the main request URI.
|
2011-06-29 23:35:05 +00:00
|
|
|
* This will be suitable for use as a relative link in HTML output.
|
2010-05-28 20:20:00 +00:00
|
|
|
*
|
2012-10-07 23:35:26 +00:00
|
|
|
* @throws MWException
|
2014-04-23 09:37:59 +00:00
|
|
|
* @return string
|
2004-09-04 00:12:08 +00:00
|
|
|
*/
|
2016-03-08 17:46:05 +00:00
|
|
|
public static function getGlobalRequestURL() {
|
2018-03-16 02:40:07 +00:00
|
|
|
// This method is called on fatal errors; it should not depend on anything complex.
|
|
|
|
|
|
2013-04-20 22:49:30 +00:00
|
|
|
if ( isset( $_SERVER['REQUEST_URI'] ) && strlen( $_SERVER['REQUEST_URI'] ) ) {
|
2007-01-16 01:45:51 +00:00
|
|
|
$base = $_SERVER['REQUEST_URI'];
|
2014-05-11 15:34:55 +00:00
|
|
|
} elseif ( isset( $_SERVER['HTTP_X_ORIGINAL_URL'] )
|
|
|
|
|
&& strlen( $_SERVER['HTTP_X_ORIGINAL_URL'] )
|
|
|
|
|
) {
|
2007-01-16 01:45:51 +00:00
|
|
|
// Probably IIS; doesn't set REQUEST_URI
|
2011-02-19 13:09:17 +00:00
|
|
|
$base = $_SERVER['HTTP_X_ORIGINAL_URL'];
|
2013-04-20 22:49:30 +00:00
|
|
|
} elseif ( isset( $_SERVER['SCRIPT_NAME'] ) ) {
|
2007-01-16 01:45:51 +00:00
|
|
|
$base = $_SERVER['SCRIPT_NAME'];
|
2013-04-20 22:49:30 +00:00
|
|
|
if ( isset( $_SERVER['QUERY_STRING'] ) && $_SERVER['QUERY_STRING'] != '' ) {
|
2007-01-16 01:45:51 +00:00
|
|
|
$base .= '?' . $_SERVER['QUERY_STRING'];
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
// This shouldn't happen!
|
|
|
|
|
throw new MWException( "Web server doesn't provide either " .
|
2011-02-19 13:09:17 +00:00
|
|
|
"REQUEST_URI, HTTP_X_ORIGINAL_URL or SCRIPT_NAME. Report details " .
|
2015-06-13 17:29:15 +00:00
|
|
|
"of your web server configuration to https://phabricator.wikimedia.org/" );
|
2007-01-16 01:45:51 +00:00
|
|
|
}
|
2007-03-05 18:54:27 +00:00
|
|
|
// User-agents should not send a fragment with the URI, but
|
|
|
|
|
// if they do, and the web server passes it on to us, we
|
|
|
|
|
// need to strip it or we get false-positive redirect loops
|
|
|
|
|
// or weird output URLs
|
|
|
|
|
$hash = strpos( $base, '#' );
|
2013-04-20 22:49:30 +00:00
|
|
|
if ( $hash !== false ) {
|
2007-03-05 18:54:27 +00:00
|
|
|
$base = substr( $base, 0, $hash );
|
|
|
|
|
}
|
2013-03-28 08:37:37 +00:00
|
|
|
|
2013-04-20 22:49:30 +00:00
|
|
|
if ( $base[0] == '/' ) {
|
2013-03-27 23:44:48 +00:00
|
|
|
// More than one slash will look like it is protocol relative
|
|
|
|
|
return preg_replace( '!^/+!', '/', $base );
|
2005-11-05 10:53:21 +00:00
|
|
|
} else {
|
|
|
|
|
// We may get paths with a host prepended; strip it.
|
2013-03-27 23:44:48 +00:00
|
|
|
return preg_replace( '!^[^:]+://[^/]+/+!', '/', $base );
|
2005-11-05 10:53:21 +00:00
|
|
|
}
|
2004-03-19 08:05:36 +00:00
|
|
|
}
|
2006-01-07 13:31:29 +00:00
|
|
|
|
2016-03-08 17:46:05 +00:00
|
|
|
/**
|
|
|
|
|
* Return the path and query string portion of the request URI.
|
|
|
|
|
* This will be suitable for use as a relative link in HTML output.
|
|
|
|
|
*
|
|
|
|
|
* @throws MWException
|
|
|
|
|
* @return string
|
|
|
|
|
*/
|
|
|
|
|
public function getRequestURL() {
|
|
|
|
|
return self::getGlobalRequestURL();
|
|
|
|
|
}
|
|
|
|
|
|
2004-09-04 00:12:08 +00:00
|
|
|
/**
|
2011-06-29 23:35:05 +00:00
|
|
|
* Return the request URI with the canonical service and hostname, path,
|
|
|
|
|
* and query string. This will be suitable for use as an absolute link
|
|
|
|
|
* in HTML or other output.
|
2011-08-31 14:47:08 +00:00
|
|
|
*
|
2011-08-19 13:25:43 +00:00
|
|
|
* If $wgServer is protocol-relative, this will return a fully
|
2019-04-19 23:41:56 +00:00
|
|
|
* qualified URL with the protocol of this request object.
|
2010-05-28 20:20:00 +00:00
|
|
|
*
|
2014-04-23 09:37:59 +00:00
|
|
|
* @return string
|
2004-09-04 00:12:08 +00:00
|
|
|
*/
|
2009-07-25 00:14:34 +00:00
|
|
|
public function getFullRequestURL() {
|
2019-04-19 23:41:56 +00:00
|
|
|
// Pass an explicit PROTO constant instead of PROTO_CURRENT so that we
|
|
|
|
|
// do not rely on state from the global $wgRequest object (which it would,
|
|
|
|
|
// via wfGetServerUrl/wfExpandUrl/$wgRequest->protocol).
|
|
|
|
|
if ( $this->getProtocol() === 'http' ) {
|
|
|
|
|
return wfGetServerUrl( PROTO_HTTP ) . $this->getRequestURL();
|
|
|
|
|
} else {
|
|
|
|
|
return wfGetServerUrl( PROTO_HTTPS ) . $this->getRequestURL();
|
|
|
|
|
}
|
2004-03-19 08:05:36 +00:00
|
|
|
}
|
2006-01-07 13:31:29 +00:00
|
|
|
|
2011-06-30 02:59:43 +00:00
|
|
|
/**
|
2014-04-23 09:37:59 +00:00
|
|
|
* @param string $key
|
|
|
|
|
* @param string $value
|
|
|
|
|
* @return string
|
2011-06-30 02:59:43 +00:00
|
|
|
*/
|
2016-01-06 23:13:31 +00:00
|
|
|
public function appendQueryValue( $key, $value ) {
|
2016-02-17 09:09:32 +00:00
|
|
|
return $this->appendQueryArray( [ $key => $value ] );
|
2008-04-12 07:25:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Appends or replaces value of query variables.
|
2010-05-28 20:20:00 +00:00
|
|
|
*
|
2014-04-23 09:37:59 +00:00
|
|
|
* @param array $array Array of values to replace/add to query
|
|
|
|
|
* @return string
|
2008-04-12 07:25:20 +00:00
|
|
|
*/
|
2016-01-06 23:13:31 +00:00
|
|
|
public function appendQueryArray( $array ) {
|
2011-02-23 20:23:35 +00:00
|
|
|
$newquery = $this->getQueryValues();
|
2008-04-12 07:25:20 +00:00
|
|
|
unset( $newquery['title'] );
|
|
|
|
|
$newquery = array_merge( $newquery, $array );
|
2014-09-24 20:38:17 +00:00
|
|
|
|
2016-01-06 23:13:31 +00:00
|
|
|
return wfArrayToCgi( $newquery );
|
2008-04-12 07:25:20 +00:00
|
|
|
}
|
|
|
|
|
|
2020-01-28 00:29:08 +00:00
|
|
|
/**
|
|
|
|
|
* Check for limit and offset parameters on the input, and return sensible
|
|
|
|
|
* defaults if not given. The limit must be positive and is capped at 5000.
|
|
|
|
|
* Offset must be positive but is not capped.
|
|
|
|
|
*
|
2021-07-20 21:17:10 +00:00
|
|
|
* @param UserIdentity $user UserIdentity to get option for
|
2020-01-28 00:29:08 +00:00
|
|
|
* @param int $deflimit Limit to use if no input and the user hasn't set the option.
|
|
|
|
|
* @param string $optionname To specify an option other than rclimit to pull from.
|
|
|
|
|
* @return int[] First element is limit, second is offset
|
|
|
|
|
*/
|
2021-07-20 21:17:10 +00:00
|
|
|
public function getLimitOffsetForUser( UserIdentity $user, $deflimit = 50, $optionname = 'rclimit' ) {
|
2004-08-23 02:19:02 +00:00
|
|
|
$limit = $this->getInt( 'limit', 0 );
|
2013-04-20 22:49:30 +00:00
|
|
|
if ( $limit < 0 ) {
|
2010-09-04 03:43:33 +00:00
|
|
|
$limit = 0;
|
|
|
|
|
}
|
2013-04-20 22:49:30 +00:00
|
|
|
if ( ( $limit == 0 ) && ( $optionname != '' ) ) {
|
2021-07-20 21:17:10 +00:00
|
|
|
$limit = MediaWikiServices::getInstance()
|
|
|
|
|
->getUserOptionsLookup()
|
|
|
|
|
->getIntOption( $user, $optionname );
|
2004-08-23 02:19:02 +00:00
|
|
|
}
|
2013-04-20 22:49:30 +00:00
|
|
|
if ( $limit <= 0 ) {
|
2010-09-04 03:43:33 +00:00
|
|
|
$limit = $deflimit;
|
|
|
|
|
}
|
2014-02-11 01:03:45 +00:00
|
|
|
if ( $limit > 5000 ) {
|
|
|
|
|
$limit = 5000; # We have *some* limits...
|
2010-09-04 03:43:33 +00:00
|
|
|
}
|
2006-01-07 13:31:29 +00:00
|
|
|
|
2004-08-23 02:19:02 +00:00
|
|
|
$offset = $this->getInt( 'offset', 0 );
|
2013-04-20 22:49:30 +00:00
|
|
|
if ( $offset < 0 ) {
|
2010-09-04 03:43:33 +00:00
|
|
|
$offset = 0;
|
|
|
|
|
}
|
2006-01-07 13:31:29 +00:00
|
|
|
|
2016-02-17 09:09:32 +00:00
|
|
|
return [ $limit, $offset ];
|
2004-08-23 02:19:02 +00:00
|
|
|
}
|
2006-01-07 13:31:29 +00:00
|
|
|
|
2004-09-03 07:12:46 +00:00
|
|
|
/**
|
2004-09-04 00:12:08 +00:00
|
|
|
* Return the path to the temporary file where PHP has stored the upload.
|
2010-05-28 20:20:00 +00:00
|
|
|
*
|
2014-04-23 09:37:59 +00:00
|
|
|
* @param string $key
|
2014-07-24 17:42:24 +00:00
|
|
|
* @return string|null String or null if no such file.
|
2004-09-03 07:12:46 +00:00
|
|
|
*/
|
2009-07-25 00:14:34 +00:00
|
|
|
public function getFileTempname( $key ) {
|
2021-01-04 23:53:01 +00:00
|
|
|
return $this->getUpload( $key )->getTempName();
|
2004-09-03 07:12:46 +00:00
|
|
|
}
|
2006-01-07 13:31:29 +00:00
|
|
|
|
2005-08-26 15:31:05 +00:00
|
|
|
/**
|
|
|
|
|
* Return the upload error or 0
|
2010-05-28 20:20:00 +00:00
|
|
|
*
|
2014-04-23 09:37:59 +00:00
|
|
|
* @param string $key
|
|
|
|
|
* @return int
|
2005-08-26 15:31:05 +00:00
|
|
|
*/
|
2009-07-25 00:14:34 +00:00
|
|
|
public function getUploadError( $key ) {
|
2021-01-04 23:53:01 +00:00
|
|
|
return $this->getUpload( $key )->getError();
|
2005-08-26 15:31:05 +00:00
|
|
|
}
|
2006-01-07 13:31:29 +00:00
|
|
|
|
2004-09-04 00:12:08 +00:00
|
|
|
/**
|
|
|
|
|
* Return the original filename of the uploaded file, as reported by
|
|
|
|
|
* the submitting user agent. HTML-style character entities are
|
|
|
|
|
* interpreted and normalized to Unicode normalization form C, in part
|
|
|
|
|
* to deal with weird input from Safari with non-ASCII filenames.
|
|
|
|
|
*
|
|
|
|
|
* Other than this the name is not verified for being a safe filename.
|
2004-09-04 08:34:51 +00:00
|
|
|
*
|
2014-04-23 09:37:59 +00:00
|
|
|
* @param string $key
|
2014-07-24 17:42:24 +00:00
|
|
|
* @return string|null String or null if no such file.
|
2004-09-04 00:12:08 +00:00
|
|
|
*/
|
2009-07-25 00:14:34 +00:00
|
|
|
public function getFileName( $key ) {
|
2021-01-04 23:53:01 +00:00
|
|
|
return $this->getUpload( $key )->getName();
|
2010-07-27 20:38:36 +00:00
|
|
|
}
|
2010-09-01 16:58:44 +00:00
|
|
|
|
2010-07-27 20:38:36 +00:00
|
|
|
/**
|
|
|
|
|
* Return a WebRequestUpload object corresponding to the key
|
2010-09-01 16:58:44 +00:00
|
|
|
*
|
2014-04-23 09:37:59 +00:00
|
|
|
* @param string $key
|
2010-07-27 20:38:36 +00:00
|
|
|
* @return WebRequestUpload
|
|
|
|
|
*/
|
|
|
|
|
public function getUpload( $key ) {
|
2010-07-27 20:54:34 +00:00
|
|
|
return new WebRequestUpload( $this, $key );
|
2004-09-03 07:12:46 +00:00
|
|
|
}
|
2008-04-14 07:45:50 +00:00
|
|
|
|
2006-08-12 23:03:53 +00:00
|
|
|
/**
|
2008-04-14 07:45:50 +00:00
|
|
|
* Return a handle to WebResponse style object, for setting cookies,
|
2006-08-12 23:03:53 +00:00
|
|
|
* headers and other stuff, for Request being worked on.
|
2010-11-05 12:54:58 +00:00
|
|
|
*
|
|
|
|
|
* @return WebResponse
|
2006-08-12 23:03:53 +00:00
|
|
|
*/
|
2009-07-25 00:14:34 +00:00
|
|
|
public function response() {
|
2006-08-12 23:03:53 +00:00
|
|
|
/* Lazy initialization of response object for this request */
|
2010-11-05 12:59:37 +00:00
|
|
|
if ( !is_object( $this->response ) ) {
|
2018-01-13 00:02:09 +00:00
|
|
|
$class = ( $this instanceof FauxRequest ) ? FauxResponse::class : WebResponse::class;
|
2010-11-05 12:59:37 +00:00
|
|
|
$this->response = new $class();
|
2008-04-14 07:45:50 +00:00
|
|
|
}
|
2010-11-05 12:59:37 +00:00
|
|
|
return $this->response;
|
2006-08-12 23:03:53 +00:00
|
|
|
}
|
2008-02-20 04:07:26 +00:00
|
|
|
|
2011-02-19 13:09:17 +00:00
|
|
|
/**
|
|
|
|
|
* Initialise the header list
|
|
|
|
|
*/
|
2015-09-16 16:35:37 +00:00
|
|
|
protected function initHeaders() {
|
2011-02-19 13:09:17 +00:00
|
|
|
if ( count( $this->headers ) ) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2020-03-18 08:49:15 +00:00
|
|
|
$this->headers = array_change_key_case( getallheaders(), CASE_UPPER );
|
2011-02-19 13:09:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get an array containing all request headers
|
|
|
|
|
*
|
2020-11-28 14:18:24 +00:00
|
|
|
* @return string[] Mapping header name to its value
|
2011-02-19 13:09:17 +00:00
|
|
|
*/
|
|
|
|
|
public function getAllHeaders() {
|
|
|
|
|
$this->initHeaders();
|
|
|
|
|
return $this->headers;
|
|
|
|
|
}
|
|
|
|
|
|
2008-02-20 04:07:26 +00:00
|
|
|
/**
|
2015-05-09 00:42:15 +00:00
|
|
|
* Get a request header, or false if it isn't set.
|
2011-06-30 02:59:43 +00:00
|
|
|
*
|
2015-05-09 00:42:15 +00:00
|
|
|
* @param string $name Case-insensitive header name
|
|
|
|
|
* @param int $flags Bitwise combination of:
|
|
|
|
|
* WebRequest::GETHEADER_LIST Treat the header as a comma-separated list
|
|
|
|
|
* of values, as described in RFC 2616 § 4.2.
|
|
|
|
|
* (since 1.26).
|
2020-11-28 14:18:24 +00:00
|
|
|
* @return string|string[]|false False if header is unset; otherwise the
|
2015-05-09 00:42:15 +00:00
|
|
|
* header value(s) as either a string (the default) or an array, if
|
|
|
|
|
* WebRequest::GETHEADER_LIST flag was set.
|
|
|
|
|
*/
|
|
|
|
|
public function getHeader( $name, $flags = 0 ) {
|
2011-04-14 11:58:52 +00:00
|
|
|
$this->initHeaders();
|
2008-02-20 04:07:26 +00:00
|
|
|
$name = strtoupper( $name );
|
2015-05-09 00:42:15 +00:00
|
|
|
if ( !isset( $this->headers[$name] ) ) {
|
2011-02-19 13:09:17 +00:00
|
|
|
return false;
|
2008-02-20 04:07:26 +00:00
|
|
|
}
|
2015-05-09 00:42:15 +00:00
|
|
|
$value = $this->headers[$name];
|
|
|
|
|
if ( $flags & self::GETHEADER_LIST ) {
|
|
|
|
|
$value = array_map( 'trim', explode( ',', $value ) );
|
|
|
|
|
}
|
|
|
|
|
return $value;
|
2008-02-20 04:07:26 +00:00
|
|
|
}
|
2010-01-06 03:42:30 +00:00
|
|
|
|
2010-05-28 20:20:00 +00:00
|
|
|
/**
|
2016-02-01 20:44:03 +00:00
|
|
|
* Get data from the session
|
2010-05-28 20:20:00 +00:00
|
|
|
*
|
2016-02-01 20:44:03 +00:00
|
|
|
* @note Prefer $this->getSession() instead if making multiple calls.
|
|
|
|
|
* @param string $key Name of key in the session
|
2014-04-23 09:37:59 +00:00
|
|
|
* @return mixed
|
2008-09-06 08:58:24 +00:00
|
|
|
*/
|
2009-07-25 00:14:34 +00:00
|
|
|
public function getSessionData( $key ) {
|
2016-02-01 20:44:03 +00:00
|
|
|
return $this->getSession()->get( $key );
|
2008-09-06 08:58:24 +00:00
|
|
|
}
|
2010-01-06 03:42:30 +00:00
|
|
|
|
2009-07-25 00:14:34 +00:00
|
|
|
/**
|
2016-02-01 20:44:03 +00:00
|
|
|
* @note Prefer $this->getSession() instead if making multiple calls.
|
|
|
|
|
* @param string $key Name of key in the session
|
2014-04-23 09:37:59 +00:00
|
|
|
* @param mixed $data
|
2009-07-25 00:14:34 +00:00
|
|
|
*/
|
|
|
|
|
public function setSessionData( $key, $data ) {
|
2016-05-06 22:25:36 +00:00
|
|
|
$this->getSession()->set( $key, $data );
|
2008-11-21 09:55:13 +00:00
|
|
|
}
|
2009-08-17 13:23:45 +00:00
|
|
|
|
2011-06-03 05:32:51 +00:00
|
|
|
/**
|
2019-11-25 02:56:17 +00:00
|
|
|
* This function formerly did a security check to prevent an XSS
|
|
|
|
|
* vulnerability in IE6, as documented in T30235. Since IE6 support has
|
|
|
|
|
* been dropped, this function now returns true unconditionally.
|
2011-06-30 02:59:43 +00:00
|
|
|
*
|
2019-11-25 02:56:17 +00:00
|
|
|
* @deprecated since 1.35
|
2021-04-18 15:49:35 +00:00
|
|
|
* @param array $extList
|
2011-06-30 02:59:43 +00:00
|
|
|
* @return bool
|
2011-06-03 05:32:51 +00:00
|
|
|
*/
|
2021-04-18 15:49:35 +00:00
|
|
|
public function checkUrlExtension( $extList = [] ) {
|
2019-11-25 02:56:17 +00:00
|
|
|
wfDeprecated( __METHOD__, '1.35' );
|
2011-06-03 05:32:51 +00:00
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2010-06-18 18:00:54 +00:00
|
|
|
/**
|
|
|
|
|
* Parse the Accept-Language header sent by the client into an array
|
2014-05-11 15:34:55 +00:00
|
|
|
*
|
2017-01-16 15:42:53 +00:00
|
|
|
* @return array [ languageCode => q-value ] sorted by q-value in
|
2014-05-11 15:34:55 +00:00
|
|
|
* descending order then appearing time in the header in ascending order.
|
2010-08-03 13:23:31 +00:00
|
|
|
* May contain the "language" '*', which applies to languages other than those explicitly listed.
|
2020-06-19 17:59:51 +00:00
|
|
|
*
|
|
|
|
|
* This logic is aligned with RFC 7231 section 5 (previously RFC 2616 section 14),
|
|
|
|
|
* at <https://tools.ietf.org/html/rfc7231#section-5.3.5>.
|
|
|
|
|
*
|
|
|
|
|
* Earlier languages in the list are preferred as per the RFC 23282 extension to HTTP/1.1,
|
|
|
|
|
* at <https://tools.ietf.org/html/rfc3282>.
|
2010-06-18 18:00:54 +00:00
|
|
|
*/
|
|
|
|
|
public function getAcceptLang() {
|
2014-05-11 15:34:55 +00:00
|
|
|
// Modified version of code found at
|
|
|
|
|
// http://www.thefutureoftheweb.com/blog/use-accept-language-header
|
2010-08-02 20:16:36 +00:00
|
|
|
$acceptLang = $this->getHeader( 'Accept-Language' );
|
|
|
|
|
if ( !$acceptLang ) {
|
2016-02-17 09:09:32 +00:00
|
|
|
return [];
|
2010-06-18 18:00:54 +00:00
|
|
|
}
|
2010-09-01 16:58:44 +00:00
|
|
|
|
2010-08-03 13:23:31 +00:00
|
|
|
// Return the language codes in lower case
|
|
|
|
|
$acceptLang = strtolower( $acceptLang );
|
2010-09-01 16:58:44 +00:00
|
|
|
|
2010-06-18 18:00:54 +00:00
|
|
|
// Break up string into pieces (languages and q factors)
|
2020-06-19 17:59:51 +00:00
|
|
|
if ( !preg_match_all(
|
2020-05-14 17:44:30 +00:00
|
|
|
'/
|
|
|
|
|
# a language code or a star is required
|
|
|
|
|
([a-z]{1,8}(?:-[a-z]{1,8})*|\*)
|
|
|
|
|
# from here everything is optional
|
|
|
|
|
\s*
|
|
|
|
|
(?:
|
|
|
|
|
# this accepts only numbers in the range ;q=0.000 to ;q=1.000
|
|
|
|
|
;\s*q\s*=\s*
|
|
|
|
|
(1(?:\.0{0,3})?|0(?:\.\d{0,3})?)?
|
|
|
|
|
)?
|
|
|
|
|
/x',
|
2014-05-11 15:34:55 +00:00
|
|
|
$acceptLang,
|
2020-06-19 17:59:51 +00:00
|
|
|
$matches,
|
|
|
|
|
PREG_SET_ORDER
|
|
|
|
|
) ) {
|
2016-02-17 09:09:32 +00:00
|
|
|
return [];
|
2010-06-18 18:00:54 +00:00
|
|
|
}
|
2010-08-03 13:23:31 +00:00
|
|
|
|
2020-06-19 17:59:51 +00:00
|
|
|
// Create a list like "en" => 0.8
|
|
|
|
|
$langs = [];
|
|
|
|
|
foreach ( $matches as $match ) {
|
|
|
|
|
$languageCode = $match[1];
|
|
|
|
|
// When not present, the default value is 1
|
2020-05-14 17:44:30 +00:00
|
|
|
$qValue = (float)( $match[2] ?? 1.0 );
|
|
|
|
|
if ( $qValue ) {
|
2020-06-19 17:59:51 +00:00
|
|
|
$langs[$languageCode] = $qValue;
|
2010-06-18 18:00:54 +00:00
|
|
|
}
|
|
|
|
|
}
|
2010-08-03 13:23:31 +00:00
|
|
|
|
2020-06-19 17:59:51 +00:00
|
|
|
// Sort list by qValue
|
|
|
|
|
arsort( $langs, SORT_NUMERIC );
|
2010-06-18 18:00:54 +00:00
|
|
|
return $langs;
|
|
|
|
|
}
|
2011-08-18 20:03:30 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Fetch the raw IP from the request
|
|
|
|
|
*
|
2012-02-06 20:20:43 +00:00
|
|
|
* @since 1.19
|
|
|
|
|
*
|
2012-12-09 03:09:48 +00:00
|
|
|
* @throws MWException
|
2020-03-21 20:55:25 +00:00
|
|
|
* @return string|null
|
2011-08-18 20:03:30 +00:00
|
|
|
*/
|
|
|
|
|
protected function getRawIP() {
|
2012-08-22 20:38:25 +00:00
|
|
|
if ( !isset( $_SERVER['REMOTE_ADDR'] ) ) {
|
2011-08-18 20:03:30 +00:00
|
|
|
return null;
|
|
|
|
|
}
|
2012-08-22 20:38:25 +00:00
|
|
|
|
|
|
|
|
if ( is_array( $_SERVER['REMOTE_ADDR'] ) || strpos( $_SERVER['REMOTE_ADDR'], ',' ) !== false ) {
|
2014-05-11 15:34:55 +00:00
|
|
|
throw new MWException( __METHOD__
|
|
|
|
|
. " : Could not determine the remote IP address due to multiple values." );
|
2012-08-22 20:38:25 +00:00
|
|
|
} else {
|
|
|
|
|
$ipchain = $_SERVER['REMOTE_ADDR'];
|
|
|
|
|
}
|
|
|
|
|
|
2019-06-25 18:53:15 +00:00
|
|
|
return IPUtils::canonicalize( $ipchain );
|
2011-08-18 20:03:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Work out the IP address based on various globals
|
|
|
|
|
* For trusted proxies, use the XFF client IP (first of the chain)
|
2012-10-07 23:35:26 +00:00
|
|
|
*
|
2012-02-06 20:20:43 +00:00
|
|
|
* @since 1.19
|
|
|
|
|
*
|
2012-10-07 23:35:26 +00:00
|
|
|
* @throws MWException
|
2011-08-18 20:03:30 +00:00
|
|
|
* @return string
|
|
|
|
|
*/
|
|
|
|
|
public function getIP() {
|
|
|
|
|
global $wgUsePrivateIPs;
|
|
|
|
|
|
|
|
|
|
# Return cached result
|
|
|
|
|
if ( $this->ip !== null ) {
|
|
|
|
|
return $this->ip;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
# collect the originating ips
|
|
|
|
|
$ip = $this->getRawIP();
|
2014-06-07 00:20:47 +00:00
|
|
|
if ( !$ip ) {
|
|
|
|
|
throw new MWException( 'Unable to determine IP.' );
|
|
|
|
|
}
|
2011-08-18 20:03:30 +00:00
|
|
|
|
|
|
|
|
# Append XFF
|
|
|
|
|
$forwardedFor = $this->getHeader( 'X-Forwarded-For' );
|
|
|
|
|
if ( $forwardedFor !== false ) {
|
2016-09-22 02:52:06 +00:00
|
|
|
$proxyLookup = MediaWikiServices::getInstance()->getProxyLookup();
|
|
|
|
|
$isConfigured = $proxyLookup->isConfiguredProxy( $ip );
|
2011-08-18 20:03:30 +00:00
|
|
|
$ipchain = array_map( 'trim', explode( ',', $forwardedFor ) );
|
|
|
|
|
$ipchain = array_reverse( $ipchain );
|
2014-06-07 00:20:47 +00:00
|
|
|
array_unshift( $ipchain, $ip );
|
2011-08-18 20:03:30 +00:00
|
|
|
|
2013-05-30 17:30:55 +00:00
|
|
|
# Step through XFF list and find the last address in the list which is a
|
|
|
|
|
# trusted server. Set $ip to the IP address given by that trusted server,
|
|
|
|
|
# unless the address is not sensible (e.g. private). However, prefer private
|
|
|
|
|
# IP addresses over proxy servers controlled by this site (more sensible).
|
2014-06-07 00:20:47 +00:00
|
|
|
# Note that some XFF values might be "unknown" with Squid/Varnish.
|
2011-08-18 20:03:30 +00:00
|
|
|
foreach ( $ipchain as $i => $curIP ) {
|
2020-11-10 12:06:29 +00:00
|
|
|
$curIP = IPUtils::sanitizeIP(
|
|
|
|
|
IPUtils::canonicalize(
|
|
|
|
|
self::canonicalizeIPv6LoopbackAddress( $curIP )
|
|
|
|
|
)
|
|
|
|
|
);
|
2014-06-07 00:20:47 +00:00
|
|
|
if ( !$curIP || !isset( $ipchain[$i + 1] ) || $ipchain[$i + 1] === 'unknown'
|
2016-09-22 02:52:06 +00:00
|
|
|
|| !$proxyLookup->isTrustedProxy( $curIP )
|
2014-06-07 00:20:47 +00:00
|
|
|
) {
|
|
|
|
|
break; // IP is not valid/trusted or does not point to anything
|
|
|
|
|
}
|
|
|
|
|
if (
|
2019-06-25 18:53:15 +00:00
|
|
|
IPUtils::isPublic( $ipchain[$i + 1] ) ||
|
2014-06-07 00:20:47 +00:00
|
|
|
$wgUsePrivateIPs ||
|
2017-02-20 22:44:19 +00:00
|
|
|
$proxyLookup->isConfiguredProxy( $curIP ) // T50919; treat IP as sane
|
2014-06-07 00:20:47 +00:00
|
|
|
) {
|
2020-11-10 12:06:29 +00:00
|
|
|
$nextIP = $ipchain[$i + 1];
|
|
|
|
|
|
2014-06-07 00:20:47 +00:00
|
|
|
// Follow the next IP according to the proxy
|
2020-11-10 12:06:29 +00:00
|
|
|
$nextIP = IPUtils::canonicalize(
|
|
|
|
|
self::canonicalizeIPv6LoopbackAddress( $nextIP )
|
|
|
|
|
);
|
2014-06-07 00:20:47 +00:00
|
|
|
if ( !$nextIP && $isConfigured ) {
|
|
|
|
|
// We have not yet made it past CDN/proxy servers of this site,
|
|
|
|
|
// so either they are misconfigured or there is some IP spoofing.
|
|
|
|
|
throw new MWException( "Invalid IP given in XFF '$forwardedFor'." );
|
2011-08-18 20:03:30 +00:00
|
|
|
}
|
2014-06-07 00:20:47 +00:00
|
|
|
$ip = $nextIP;
|
2020-11-10 12:06:29 +00:00
|
|
|
|
2014-06-07 00:20:47 +00:00
|
|
|
// keep traversing the chain
|
|
|
|
|
continue;
|
2011-08-18 20:03:30 +00:00
|
|
|
}
|
2013-05-30 17:30:55 +00:00
|
|
|
break;
|
2011-08-18 20:03:30 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
# Allow extensions to improve our guess
|
Hooks::run() call site migration
Migrate all callers of Hooks::run() to use the new
HookContainer/HookRunner system.
General principles:
* Use DI if it is already used. We're not changing the way state is
managed in this patch.
* HookContainer is always injected, not HookRunner. HookContainer
is a service, it's a more generic interface, it is the only
thing that provides isRegistered() which is needed in some cases,
and a HookRunner can be efficiently constructed from it
(confirmed by benchmark). Because HookContainer is needed
for object construction, it is also needed by all factories.
* "Ask your friendly local base class". Big hierarchies like
SpecialPage and ApiBase have getHookContainer() and getHookRunner()
methods in the base class, and classes that extend that base class
are not expected to know or care where the base class gets its
HookContainer from.
* ProtectedHookAccessorTrait provides protected getHookContainer() and
getHookRunner() methods, getting them from the global service
container. The point of this is to ease migration to DI by ensuring
that call sites ask their local friendly base class rather than
getting a HookRunner from the service container directly.
* Private $this->hookRunner. In some smaller classes where accessor
methods did not seem warranted, there is a private HookRunner property
which is accessed directly. Very rarely (two cases), there is a
protected property, for consistency with code that conventionally
assumes protected=private, but in cases where the class might actually
be overridden, a protected accessor is preferred over a protected
property.
* The last resort: Hooks::runner(). Mostly for static, file-scope and
global code. In a few cases it was used for objects with broken
construction schemes, out of horror or laziness.
Constructors with new required arguments:
* AuthManager
* BadFileLookup
* BlockManager
* ClassicInterwikiLookup
* ContentHandlerFactory
* ContentSecurityPolicy
* DefaultOptionsManager
* DerivedPageDataUpdater
* FullSearchResultWidget
* HtmlCacheUpdater
* LanguageFactory
* LanguageNameUtils
* LinkRenderer
* LinkRendererFactory
* LocalisationCache
* MagicWordFactory
* MessageCache
* NamespaceInfo
* PageEditStash
* PageHandlerFactory
* PageUpdater
* ParserFactory
* PermissionManager
* RevisionStore
* RevisionStoreFactory
* SearchEngineConfig
* SearchEngineFactory
* SearchFormWidget
* SearchNearMatcher
* SessionBackend
* SpecialPageFactory
* UserNameUtils
* UserOptionsManager
* WatchedItemQueryService
* WatchedItemStore
Constructors with new optional arguments:
* DefaultPreferencesFactory
* Language
* LinkHolderArray
* MovePage
* Parser
* ParserCache
* PasswordReset
* Router
setHookContainer() now required after construction:
* AuthenticationProvider
* ResourceLoaderModule
* SearchEngine
Change-Id: Id442b0dbe43aba84bd5cf801d86dedc768b082c7
2020-03-19 02:42:09 +00:00
|
|
|
Hooks::runner()->onGetIP( $ip );
|
2011-08-18 20:03:30 +00:00
|
|
|
|
|
|
|
|
if ( !$ip ) {
|
2013-09-18 21:25:17 +00:00
|
|
|
throw new MWException( "Unable to determine IP." );
|
2011-08-18 20:03:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$this->ip = $ip;
|
|
|
|
|
return $ip;
|
|
|
|
|
}
|
2013-02-13 21:25:37 +00:00
|
|
|
|
2020-11-10 12:06:29 +00:00
|
|
|
/**
|
|
|
|
|
* Converts ::1 (IPv6 loopback address) to 127.0.0.1 (IPv4 loopback address);
|
|
|
|
|
* assists in matching trusted proxies.
|
|
|
|
|
*
|
|
|
|
|
* @param string $ip
|
|
|
|
|
* @return string either '127.0.0.1' or $ip
|
|
|
|
|
* @since 1.36
|
|
|
|
|
*/
|
|
|
|
|
public static function canonicalizeIPv6LoopbackAddress( $ip ) {
|
|
|
|
|
// Code moved from IPUtils library. See T248237#6614927
|
|
|
|
|
$m = [];
|
|
|
|
|
if ( preg_match( '/^0*' . IPUtils::RE_IPV6_GAP . '1$/', $ip, $m ) ) {
|
|
|
|
|
return '127.0.0.1';
|
|
|
|
|
}
|
|
|
|
|
return $ip;
|
|
|
|
|
}
|
|
|
|
|
|
2013-02-13 21:25:37 +00:00
|
|
|
/**
|
|
|
|
|
* @param string $ip
|
|
|
|
|
* @return void
|
|
|
|
|
* @since 1.21
|
|
|
|
|
*/
|
|
|
|
|
public function setIP( $ip ) {
|
|
|
|
|
$this->ip = $ip;
|
|
|
|
|
}
|
2016-05-06 22:25:36 +00:00
|
|
|
|
2016-05-20 03:31:19 +00:00
|
|
|
/**
|
|
|
|
|
* Check if this request uses a "safe" HTTP method
|
|
|
|
|
*
|
|
|
|
|
* Safe methods are verbs (e.g. GET/HEAD/OPTIONS) used for obtaining content. Such requests
|
|
|
|
|
* are not expected to mutate content, especially in ways attributable to the client. Verbs
|
|
|
|
|
* like POST and PUT are typical of non-safe requests which often change content.
|
|
|
|
|
*
|
|
|
|
|
* @return bool
|
|
|
|
|
* @see https://tools.ietf.org/html/rfc7231#section-4.2.1
|
|
|
|
|
* @see https://www.w3.org/Protocols/rfc2616/rfc2616-sec9.html
|
|
|
|
|
* @since 1.28
|
|
|
|
|
*/
|
|
|
|
|
public function hasSafeMethod() {
|
|
|
|
|
if ( !isset( $_SERVER['REQUEST_METHOD'] ) ) {
|
|
|
|
|
return false; // CLI mode
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return in_array( $_SERVER['REQUEST_METHOD'], [ 'GET', 'HEAD', 'OPTIONS', 'TRACE' ] );
|
|
|
|
|
}
|
|
|
|
|
|
2016-05-06 22:25:36 +00:00
|
|
|
/**
|
|
|
|
|
* Whether this request should be identified as being "safe"
|
|
|
|
|
*
|
|
|
|
|
* This means that the client is not requesting any state changes and that database writes
|
|
|
|
|
* are not inherently required. Ideally, no visible updates would happen at all. If they
|
2019-01-27 09:15:42 +00:00
|
|
|
* must, then they should not be publicly attributed to the end user.
|
2016-05-06 22:25:36 +00:00
|
|
|
*
|
|
|
|
|
* In more detail:
|
|
|
|
|
* - Cache populations and refreshes MAY occur.
|
|
|
|
|
* - Private user session updates and private server logging MAY occur.
|
|
|
|
|
* - Updates to private viewing activity data MAY occur via DeferredUpdates.
|
|
|
|
|
* - Other updates SHOULD NOT occur (e.g. modifying content assets).
|
|
|
|
|
*
|
|
|
|
|
* @return bool
|
|
|
|
|
* @see https://tools.ietf.org/html/rfc7231#section-4.2.1
|
|
|
|
|
* @see https://www.w3.org/Protocols/rfc2616/rfc2616-sec9.html
|
|
|
|
|
* @since 1.28
|
|
|
|
|
*/
|
|
|
|
|
public function isSafeRequest() {
|
2016-05-20 03:31:19 +00:00
|
|
|
if ( $this->markedAsSafe && $this->wasPosted() ) {
|
|
|
|
|
return true; // marked as a "safe" POST
|
2016-05-06 22:25:36 +00:00
|
|
|
}
|
|
|
|
|
|
2016-05-20 03:31:19 +00:00
|
|
|
return $this->hasSafeMethod();
|
2016-05-06 22:25:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2016-05-20 03:31:19 +00:00
|
|
|
* Mark this request as identified as being nullipotent even if it is a POST request
|
2016-05-06 22:25:36 +00:00
|
|
|
*
|
|
|
|
|
* POST requests are often used due to the need for a client payload, even if the request
|
|
|
|
|
* is otherwise equivalent to a "safe method" request.
|
|
|
|
|
*
|
|
|
|
|
* @see https://tools.ietf.org/html/rfc7231#section-4.2.1
|
|
|
|
|
* @see https://www.w3.org/Protocols/rfc2616/rfc2616-sec9.html
|
|
|
|
|
* @since 1.28
|
|
|
|
|
*/
|
|
|
|
|
public function markAsSafeRequest() {
|
|
|
|
|
$this->markedAsSafe = true;
|
|
|
|
|
}
|
2004-03-08 09:09:35 +00:00
|
|
|
}
|