Merge "Add note that IP::isInRange() can return unexpected results for invalid args"
[lhc/web/wiklou.git] / includes / WebRequest.php
index 4c4ca97..b18d59c 100644 (file)
@@ -23,6 +23,8 @@
  * @file
  */
 
+use MediaWiki\Session\Session;
+use MediaWiki\Session\SessionId;
 use MediaWiki\Session\SessionManager;
 
 /**
@@ -33,7 +35,7 @@ use MediaWiki\Session\SessionManager;
  * @ingroup HTTP
  */
 class WebRequest {
-       protected $data, $headers = array();
+       protected $data, $headers = [];
 
        /**
         * Flag to make WebRequest::getHeader return an array of values.
@@ -66,7 +68,7 @@ class WebRequest {
        protected $protocol;
 
        /**
-        * @var \\MediaWiki\\Session\\SessionId|null Session ID to use for this
+        * @var SessionId|null Session ID to use for this
         *  request. We can't save the session directly due to reference cycles not
         *  working too well (slow GC in Zend and never collected in HHVM).
         */
@@ -101,7 +103,7 @@ class WebRequest {
                // PATH_INFO is mangled due to http://bugs.php.net/bug.php?id=31892
                // And also by Apache 2.x, double slashes are converted to single slashes.
                // So we will use REQUEST_URI if possible.
-               $matches = array();
+               $matches = [];
                if ( !empty( $_SERVER['REQUEST_URI'] ) ) {
                        // Slurp out the path portion to examine...
                        $url = $_SERVER['REQUEST_URI'];
@@ -142,18 +144,18 @@ class WebRequest {
 
                                global $wgActionPaths;
                                if ( $wgActionPaths ) {
-                                       $router->add( $wgActionPaths, array( 'action' => '$key' ) );
+                                       $router->add( $wgActionPaths, [ 'action' => '$key' ] );
                                }
 
                                global $wgVariantArticlePath, $wgContLang;
                                if ( $wgVariantArticlePath ) {
                                        $router->add( $wgVariantArticlePath,
-                                               array( 'variant' => '$2' ),
-                                               array( '$2' => $wgContLang->getVariants() )
+                                               [ 'variant' => '$2' ],
+                                               [ '$2' => $wgContLang->getVariants() ]
                                        );
                                }
 
-                               Hooks::run( 'WebRequestPathInfoRouter', array( $router ) );
+                               Hooks::run( 'WebRequestPathInfoRouter', [ $router ] );
 
                                $matches = $router->parse( $path );
                        }
@@ -185,7 +187,7 @@ class WebRequest {
                $proto = self::detectProtocol();
                $stdPort = $proto === 'https' ? 443 : 80;
 
-               $varNames = array( 'HTTP_HOST', 'SERVER_NAME', 'HOSTNAME', 'SERVER_ADDR' );
+               $varNames = [ 'HTTP_HOST', 'SERVER_NAME', 'HOSTNAME', 'SERVER_ADDR' ];
                $host = 'localhost';
                $port = $stdPort;
                foreach ( $varNames as $varName ) {
@@ -246,6 +248,25 @@ class WebRequest {
                return microtime( true ) - $this->requestTime;
        }
 
+       /**
+        * Get the unique request ID.
+        * This is either the value of the UNIQUE_ID envvar (if present) or a
+        * randomly-generated 24-character string.
+        *
+        * @return string
+        * @since 1.27
+        */
+       public static function getRequestId() {
+               static $reqId;
+
+               if ( !$reqId ) {
+                       $reqId = isset( $_SERVER['UNIQUE_ID'] )
+                               ? $_SERVER['UNIQUE_ID'] : wfRandomString( 24 );
+               }
+
+               return $reqId;
+       }
+
        /**
         * Get the current URL protocol (http or https)
         * @return string
@@ -294,7 +315,7 @@ class WebRequest {
                        if ( substr( $path, 0, $baseLen ) == $base ) {
                                $raw = substr( $path, $baseLen );
                                if ( $raw !== '' ) {
-                                       $matches = array( 'title' => rawurldecode( $raw ) );
+                                       $matches = [ 'title' => rawurldecode( $raw ) ];
                                        if ( $key ) {
                                                $matches[$key] = $keyValue;
                                        }
@@ -302,7 +323,7 @@ class WebRequest {
                                }
                        }
                }
-               return array();
+               return [];
        }
 
        /**
@@ -556,7 +577,7 @@ class WebRequest {
                        $names = array_keys( $this->data );
                }
 
-               $retVal = array();
+               $retVal = [];
                foreach ( $names as $name ) {
                        $value = $this->getGPCVal( $this->data, $name, null );
                        if ( !is_null( $value ) ) {
@@ -572,7 +593,7 @@ class WebRequest {
         * @param array $exclude
         * @return array
         */
-       public function getValueNames( $exclude = array() ) {
+       public function getValueNames( $exclude = [] ) {
                return array_diff( array_keys( $this->getValues() ), $exclude );
        }
 
@@ -651,7 +672,7 @@ class WebRequest {
         * @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.
-        * @return MediaWiki\\Session\\Session
+        * @return Session
         */
        public function getSession() {
                if ( $this->sessionId !== null ) {
@@ -669,19 +690,29 @@ class WebRequest {
        /**
         * Set the session for this request
         * @since 1.27
-        * @private For use by MediaWiki\\Session classes only
-        * @param MediaWiki\\Session\\SessionId $sessionId
+        * @private For use by MediaWiki\Session classes only
+        * @param SessionId $sessionId
         */
-       public function setSessionId( MediaWiki\Session\SessionId $sessionId ) {
+       public function setSessionId( SessionId $sessionId ) {
                $this->sessionId = $sessionId;
        }
 
+       /**
+        * Get the session id for this request, if any
+        * @since 1.27
+        * @private For use by MediaWiki\Session classes only
+        * @return SessionId|null
+        */
+       public function getSessionId() {
+               return $this->sessionId;
+       }
+
        /**
         * Returns true if the request has a persistent session.
         * This does not necessarily mean that the user is logged in!
         *
         * @deprecated since 1.27, use
-        *  \\MediaWiki\\Session\\SessionManager::singleton()->getPersistedSessionId()
+        *  \MediaWiki\Session\SessionManager::singleton()->getPersistedSessionId()
         *  instead.
         * @return bool
         */
@@ -709,13 +740,13 @@ class WebRequest {
        }
 
        /**
-        * Return the path and query string portion of the request URI.
+        * Return the path and query string portion of the main request URI.
         * This will be suitable for use as a relative link in HTML output.
         *
         * @throws MWException
         * @return string
         */
-       public function getRequestURL() {
+       public static function getGlobalRequestURL() {
                if ( isset( $_SERVER['REQUEST_URI'] ) && strlen( $_SERVER['REQUEST_URI'] ) ) {
                        $base = $_SERVER['REQUEST_URI'];
                } elseif ( isset( $_SERVER['HTTP_X_ORIGINAL_URL'] )
@@ -752,6 +783,17 @@ class WebRequest {
                }
        }
 
+       /**
+        * 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();
+       }
+
        /**
         * Return the request URI with the canonical service and hostname, path,
         * and query string. This will be suitable for use as an absolute link
@@ -772,7 +814,7 @@ class WebRequest {
         * @return string
         */
        public function appendQueryValue( $key, $value ) {
-               return $this->appendQueryArray( array( $key => $value ) );
+               return $this->appendQueryArray( [ $key => $value ] );
        }
 
        /**
@@ -820,7 +862,7 @@ class WebRequest {
                        $offset = 0;
                }
 
-               return array( $limit, $offset );
+               return [ $limit, $offset ];
        }
 
        /**
@@ -978,7 +1020,7 @@ class WebRequest {
         * @throws HttpError
         * @return bool
         */
-       public function checkUrlExtension( $extWhitelist = array() ) {
+       public function checkUrlExtension( $extWhitelist = [] ) {
                $extWhitelist[] = 'php';
                if ( IEUrlExtension::areServerVarsBad( $_SERVER, $extWhitelist ) ) {
                        if ( !$this->wasPosted() ) {
@@ -1042,7 +1084,7 @@ HTML;
                // http://www.thefutureoftheweb.com/blog/use-accept-language-header
                $acceptLang = $this->getHeader( 'Accept-Language' );
                if ( !$acceptLang ) {
-                       return array();
+                       return [];
                }
 
                // Return the language codes in lower case
@@ -1057,7 +1099,7 @@ HTML;
                );
 
                if ( !count( $lang_parse[1] ) ) {
-                       return array();
+                       return [];
                }
 
                $langcodes = $lang_parse[1];
@@ -1169,7 +1211,7 @@ HTML;
                }
 
                # Allow extensions to improve our guess
-               Hooks::run( 'GetIP', array( &$ip ) );
+               Hooks::run( 'GetIP', [ &$ip ] );
 
                if ( !$ip ) {
                        throw new MWException( "Unable to determine IP." );