make it a bit cleared: users should not modify the file
[lhc/web/wiklou.git] / includes / WebRequest.php
index 985697a..cd21edc 100644 (file)
@@ -38,7 +38,7 @@ class WebRequest {
        function WebRequest() {
                $this->checkMagicQuotes();
                global $wgUsePathInfo;
-               if( isset( $_SERVER['PATH_INFO'] ) && $wgUsePathInfo ) {
+               if( isset( $_SERVER['PATH_INFO'] ) && ($_SERVER['PATH_INFO'] != '') && $wgUsePathInfo ) {
                        # Stuff it!
                        $_GET['title'] = $_REQUEST['title'] =
                                substr( $_SERVER['PATH_INFO'], 1 );
@@ -65,7 +65,7 @@ class WebRequest {
        
        /**
         * If magic_quotes_gpc option is on, run the global arrays
-        * through fix_magic_quotes to strip out the stupid dlashes.
+        * through fix_magic_quotes to strip out the stupid slashes.
         * WARNING: This should only be done once! Running a second
         * time could damage the values.
         * @private
@@ -100,30 +100,25 @@ class WebRequest {
        
        /**
         * Fetch a value from the given array or return $default if it's not set.
-        * @param array &$arr
+        *
+        * @param array $arr
         * @param string $name
         * @param mixed $default
         * @return mixed
         * @private
         */
-       function getGPCVal( &$arr, $name, $default ) {
+       function getGPCVal( $arr, $name, $default ) {
                if( isset( $arr[$name] ) ) {
-                       global $wgUseLatin1, $wgServer, $wgContLang;
+                       global $wgContLang;
                        $data = $arr[$name];
-                       if( isset( $_GET[$name] ) &&
-                               !is_array( $data ) &&
-                               ( empty( $_SERVER['HTTP_REFERER'] ) ||
-                               strncmp($wgServer, $_SERVER['HTTP_REFERER'], strlen( $wgServer ) ) ) ) {
-                               # For links that came from outside, check for alternate/legacy
-                               # character encoding.
+                       if( isset( $_GET[$name] ) && !is_array( $data ) ) {
+                               # Check for alternate/legacy character encoding.
                                if( isset( $wgContLang ) ) {
                                        $data = $wgContLang->checkTitleEncoding( $data );
                                }
                        }
-                       if( !$wgUseLatin1 ) {
-                               require_once( 'normal/UtfNormal.php' );
-                               $data = $this->normalizeUnicode( $data );
-                       }
+                       require_once( 'normal/UtfNormal.php' );
+                       $data = $this->normalizeUnicode( $data );
                        return $data;
                } else {
                        return $default;
@@ -177,7 +172,21 @@ class WebRequest {
         * @return int
         */
        function getInt( $name, $default = 0 ) {
-               return IntVal( $this->getVal( $name, $default ) );
+               return intval( $this->getVal( $name, $default ) );
+       }
+       
+       /**
+        * 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.
+        * @param string $name
+        * @return int
+        */
+       function getIntOrNull( $name ) {
+               $val = $this->getVal( $name );
+               return is_numeric( $val )
+                       ? intval( $val )
+                       : null;
        }
        
        /**
@@ -272,7 +281,13 @@ class WebRequest {
         * @return string
         */
        function getRequestURL() {
-               return $_SERVER['REQUEST_URI'];
+               $base = $_SERVER['REQUEST_URI'];
+               if( $base{0} == '/' ) {
+                       return $base;
+               } else {
+                       // We may get paths with a host prepended; strip it.
+                       return preg_replace( '!^[^:]+://[^/]+/!', '/', $base );
+               }
        }
        
        /**
@@ -362,6 +377,18 @@ class WebRequest {
                return $_FILES[$key]['size'];
        }
        
+       /**
+        * Return the upload error or 0
+        * @param string $key
+        * @return integer
+        */
+       function getUploadError( $key ) {
+               if( !isset( $_FILES[$key] ) || !isset( $_FILES[$key]['error'] ) ) {
+                       return 0/*UPLOAD_ERR_OK*/;
+               }
+               return $_FILES[$key]['error'];
+       }
+       
        /**
         * Return the original filename of the uploaded file, as reported by
         * the submitting user agent. HTML-style character entities are
@@ -381,15 +408,8 @@ class WebRequest {
                
                # Safari sends filenames in HTML-encoded Unicode form D...
                # Horrid and evil! Let's try to make some kind of sense of it.
-               global $wgUseLatin1;
-               if( $wgUseLatin1 ) {
-                       $name = utf8_encode( $name );
-               }
-               $name = wfMungeToUtf8( $name );
+               $name = Sanitizer::decodeCharReferences( $name );
                $name = UtfNormal::cleanUp( $name );
-               if( $wgUseLatin1 ) {
-                       $name = utf8_decode( $name );
-               }
                wfDebug( "WebRequest::getFileName() '" . $_FILES[$key]['name'] . "' normalized to '$name'\n" );
                return $name;
        }