Minor followup to r64197
[lhc/web/wiklou.git] / includes / WebRequest.php
index 3ae1a20..903ea28 100644 (file)
@@ -39,16 +39,15 @@ if ( !function_exists( '__autoload' ) ) {
  * not create a second WebRequest object; make a FauxRequest object if
  * you want to pass arbitrary data to some function in place of the web
  * input.
- * 
+ *
  * @ingroup HTTP
  */
 class WebRequest {
-       protected $data = array();
-       var $headers;
-       private $_response, $mFixMagicQuotes;
+       protected $data, $headers = array();
+       private $_response;
 
        public function __construct() {
-               /// @fixme This preemptive de-quoting can interfere with other web libraries
+               /// @todo Fixme: this preemptive de-quoting can interfere with other web libraries
                ///        and increases our memory footprint. It would be cleaner to do on
                ///        demand; but currently we have no wrapper for $_SERVER etc.
                $this->checkMagicQuotes();
@@ -67,11 +66,13 @@ class WebRequest {
         */
        public function interpolateTitle() {
                global $wgUsePathInfo;
+
                if ( $wgUsePathInfo ) {
                        // 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();
+
                        if ( !empty( $_SERVER['REQUEST_URI'] ) ) {
                                // Slurp out the path portion to examine...
                                $url = $_SERVER['REQUEST_URI'];
@@ -180,8 +181,9 @@ class WebRequest {
         * time could damage the values.
         */
        private function checkMagicQuotes() {
-               $this->mFixMagicQuotes = function_exists( 'get_magic_quotes_gpc' ) && get_magic_quotes_gpc();           
-               if( $this->mFixMagicQuotes ) {
+               $mustFixQuotes = function_exists( 'get_magic_quotes_gpc' )
+                       && get_magic_quotes_gpc();
+               if( $mustFixQuotes ) {
                        $this->fix_magic_quotes( $_COOKIE );
                        $this->fix_magic_quotes( $_ENV );
                        $this->fix_magic_quotes( $_GET );
@@ -203,7 +205,8 @@ class WebRequest {
                                $data[$key] = $this->normalizeUnicode( $val );
                        }
                } else {
-                       $data = UtfNormal::cleanUp( $data );
+                       global $wgContLang;
+                       $data = $wgContLang->normalize( $data );
                }
                return $data;
        }
@@ -248,7 +251,7 @@ class WebRequest {
         * @param $default string: optional default (or NULL)
         * @return string
         */
-       public function getVal( $name, $default = NULL ) {
+       public function getVal( $name, $default = null ) {
                $val = $this->getGPCVal( $this->data, $name, $default );
                if( is_array( $val ) ) {
                        $val = $default;
@@ -259,7 +262,7 @@ class WebRequest {
                        return (string)$val;
                }
        }
-       
+
        /**
         * Set an aribtrary value into our get/post data.
         * @param $key string Key name to use
@@ -281,7 +284,7 @@ class WebRequest {
         * @param $default array: optional default (or NULL)
         * @return array
         */
-       public function getArray( $name, $default = NULL ) {
+       public function getArray( $name, $default = null ) {
                $val = $this->getGPCVal( $this->data, $name, $default );
                if( is_null( $val ) ) {
                        return null;
@@ -300,7 +303,7 @@ class WebRequest {
         * @param $default array: option default (or NULL)
         * @return array of ints
         */
-       public function getIntArray( $name, $default = NULL ) {
+       public function getIntArray( $name, $default = null ) {
                $val = $this->getArray( $name, $default );
                if( is_array( $val ) ) {
                        $val = array_map( 'intval', $val );
@@ -356,7 +359,7 @@ class WebRequest {
        public function getCheck( $name ) {
                # Checkboxes and buttons are only present when clicked
                # Presence connotes truth, abscense false
-               $val = $this->getVal( $name, NULL );
+               $val = $this->getVal( $name, null );
                return isset( $val );
        }
 
@@ -559,7 +562,7 @@ class WebRequest {
         */
        public function getFileTempname( $key ) {
                if( !isset( $_FILES[$key] ) ) {
-                       return NULL;
+                       return null;
                }
                return $_FILES[$key]['tmp_name'];
        }
@@ -600,15 +603,16 @@ class WebRequest {
         * @return string or NULL if no such file.
         */
        public function getFileName( $key ) {
+               global $wgContLang;
                if( !isset( $_FILES[$key] ) ) {
-                       return NULL;
+                       return null;
                }
                $name = $_FILES[$key]['name'];
 
                # Safari sends filenames in HTML-encoded Unicode form D...
                # Horrid and evil! Let's try to make some kind of sense of it.
                $name = Sanitizer::decodeCharReferences( $name );
-               $name = UtfNormal::cleanUp( $name );
+               $name = $wgContLang->normalize( $name );
                wfDebug( "WebRequest::getFileName() '" . $_FILES[$key]['name'] . "' normalized to '$name'\n" );
                return $name;
        }
@@ -620,7 +624,8 @@ class WebRequest {
        public function response() {
                /* Lazy initialization of response object for this request */
                if ( !is_object( $this->_response ) ) {
-                       $this->_response = new WebResponse;
+                       $class = ( $this instanceof FauxRequest ) ? 'FauxResponse' : 'WebResponse';
+                       $this->_response = new $class();
                }
                return $this->_response;
        }
@@ -632,8 +637,7 @@ class WebRequest {
        public function getHeader( $name ) {
                $name = strtoupper( $name );
                if ( function_exists( 'apache_request_headers' ) ) {
-                       if ( !isset( $this->headers ) ) {
-                               $this->headers = array();
+                       if ( !$this->headers ) {
                                foreach ( apache_request_headers() as $tempName => $tempValue ) {
                                        $this->headers[ strtoupper( $tempName ) ] = $tempValue;
                                }
@@ -652,7 +656,7 @@ class WebRequest {
                        }
                }
        }
-       
+
        /*
         * Get data from $_SESSION
         * @param $key String Name of key in $_SESSION
@@ -663,7 +667,7 @@ class WebRequest {
                        return null;
                return $_SESSION[$key];
        }
-       
+
        /**
         * Set session data
         * @param $key String Name of key in $_SESSION
@@ -672,6 +676,33 @@ class WebRequest {
        public function setSessionData( $key, $data ) {
                $_SESSION[$key] = $data;
        }
+
+       /**
+        * Returns true if the PATH_INFO ends with an extension other than a script
+        * extension. This could confuse IE for scripts that send arbitrary data which
+        * is not HTML but may be detected as such.
+        *
+        * Various past attempts to use the URL to make this check have generally
+        * run up against the fact that CGI does not provide a standard method to
+        * determine the URL. PATH_INFO may be mangled (e.g. if cgi.fix_pathinfo=0),
+        * but only by prefixing it with the script name and maybe some other stuff,
+        * the extension is not mangled. So this should be a reasonably portable
+        * way to perform this security check.
+        */
+       public function isPathInfoBad() {
+               global $wgScriptExtension;
+
+               if ( !isset( $_SERVER['PATH_INFO'] ) ) {
+                       return false;
+               }
+               $pi = $_SERVER['PATH_INFO'];
+               $dotPos = strrpos( $pi, '.' );
+               if ( $dotPos === false ) {
+                       return false;
+               }
+               $ext = substr( $pi, $dotPos );
+               return !in_array( $ext, array( $wgScriptExtension, '.php', '.php5' ) );
+       }
 }
 
 /**
@@ -681,7 +712,8 @@ class WebRequest {
  */
 class FauxRequest extends WebRequest {
        private $wasPosted = false;
-       private $session, $headers = array();
+       private $session = array();
+       private $response;
 
        /**
         * @param $data Array of *non*-urlencoded key => value pairs, the
@@ -732,14 +764,20 @@ class FauxRequest extends WebRequest {
                return isset( $this->headers[$name] ) ? $this->headers[$name] : false;
        }
 
+       public function setHeader( $name, $val ) {
+               $this->headers[$name] = $val;
+       }
+
        public function getSessionData( $key ) {
-               if( !isset( $this->session[$key] ) )
-                       return null;
-               return $this->session[$key];
+               if( isset( $this->session[$key] ) )
+                       return $this->session[$key];
        }
 
        public function setSessionData( $key, $data ) {
                $this->notImplemented( __METHOD__ );
        }
 
+       public function isPathInfoBad() {
+               return false;
+       }
 }