Make these h4
[lhc/web/wiklou.git] / includes / WebRequest.php
index a5a550b..398fce0 100644 (file)
@@ -42,8 +42,19 @@ if ( !function_exists( '__autoload' ) ) {
  *
  */
 class WebRequest {
+       var $data = array();
+       var $headers;
+       private $_response;
+       
        function __construct() {
+               /// @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();
+               
+               // POST overrides GET data
+               // We don't use $_REQUEST here to avoid interference from cookies...
+               $this->data = wfArrayMerge( $_GET, $_POST );
        }
        
        /**
@@ -68,13 +79,24 @@ class WebRequest {
                                }
                                $a = parse_url( $url );
                                if( $a ) {
-                                       $path = $a['path'];
+                                       $path = isset( $a['path'] ) ? $a['path'] : '';
+                                       
+                                       global $wgScript;
+                                       if( $path == $wgScript ) {
+                                               // Script inside a rewrite path?
+                                               // Abort to keep from breaking...
+                                               return;
+                                       }
+                                       // Raw PATH_INFO style
+                                       $matches = $this->extractTitle( $path, "$wgScript/$1" );
                                        
                                        global $wgArticlePath;
-                                       $matches = $this->extractTitle( $path, $wgArticlePath );
+                                       if( !$matches && $wgArticlePath ) {
+                                               $matches = $this->extractTitle( $path, $wgArticlePath );
+                                       }
                                        
                                        global $wgActionPaths;
-                                       if( !$matches && $wgActionPaths) {
+                                       if( !$matches && $wgActionPaths ) {
                                                $matches = $this->extractTitle( $path, $wgActionPaths, 'action' );
                                        }
                                        
@@ -99,7 +121,7 @@ class WebRequest {
                                $matches['title'] = substr( $_SERVER['PATH_INFO'], 1 );
                        }
                        foreach( $matches as $key => $val) {
-                               $_GET[$key] = $_REQUEST[$key] = $val;
+                               $this->data[$key] = $_GET[$key] = $_REQUEST[$key] = $val;
                        }
                }
        }
@@ -132,8 +154,6 @@ class WebRequest {
                }
                return array();
        }
-       
-       private $_response;
 
        /**
         * Recursively strips slashes from the given array;
@@ -161,7 +181,7 @@ class WebRequest {
         * @private
         */
        function checkMagicQuotes() {
-               if ( get_magic_quotes_gpc() ) {
+               if ( function_exists( 'get_magic_quotes_gpc' ) && get_magic_quotes_gpc() ) {
                        $this->fix_magic_quotes( $_COOKIE );
                        $this->fix_magic_quotes( $_ENV );
                        $this->fix_magic_quotes( $_GET );
@@ -225,7 +245,7 @@ class WebRequest {
         * @return string
         */
        function getVal( $name, $default = NULL ) {
-               $val = $this->getGPCVal( $_REQUEST, $name, $default );
+               $val = $this->getGPCVal( $this->data, $name, $default );
                if( is_array( $val ) ) {
                        $val = $default;
                }
@@ -246,7 +266,7 @@ class WebRequest {
         * @return array
         */
        function getArray( $name, $default = NULL ) {
-               $val = $this->getGPCVal( $_REQUEST, $name, $default );
+               $val = $this->getGPCVal( $this->data, $name, $default );
                if( is_null( $val ) ) {
                        return null;
                } else {
@@ -351,7 +371,7 @@ class WebRequest {
        function getValues() {
                $names = func_get_args();
                if ( count( $names ) == 0 ) {
-                       $names = array_keys( $_REQUEST );
+                       $names = array_keys( $this->data );
                }
 
                $retVal = array();
@@ -470,6 +490,24 @@ class WebRequest {
                return htmlspecialchars( $this->appendQuery( $query ) );
        }
 
+       function appendQueryValue( $key, $value, $onlyquery = false ) {
+               return $this->appendQueryArray( array( $key => $value ), $onlyquery );
+       }
+
+       /**
+        * Appends or replaces value of query variables.
+        * @param $array Array of values to replace/add to query
+        * @return string
+        */
+       function appendQueryArray( $array, $onlyquery = false ) {
+               global $wgTitle;
+               $newquery = $_GET;
+               unset( $newquery['title'] );
+               $newquery = array_merge( $newquery, $array );
+               $query = wfArrayToCGI( $newquery );
+               return $onlyquery ? $query : $wgTitle->getLocalURL( $basequery );
+       }
+
        /**
         * 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.
@@ -568,7 +606,34 @@ class WebRequest {
                } 
                return $this->_response;
        }
-       
+
+       /**
+        * Get a request header, or false if it isn't set
+        * @param string $name Case-insensitive header name
+        */
+       function getHeader( $name ) {
+               $name = strtoupper( $name );
+               if ( function_exists( 'apache_request_headers' ) ) {
+                       if ( !isset( $this->headers ) ) {
+                               $this->headers = array();
+                               foreach ( apache_request_headers() as $tempName => $tempValue ) {
+                                       $this->headers[ strtoupper( $tempName ) ] = $tempValue;
+                               }
+                       }
+                       if ( isset( $this->headers[$name] ) ) {
+                               return $this->headers[$name];
+                       } else {
+                               return false;
+                       }
+               } else {
+                       $name = 'HTTP_' . str_replace( '-', '_', $name );
+                       if ( isset( $_SERVER[$name] ) ) {
+                               return $_SERVER[$name];
+                       } else {
+                               return false;
+                       }
+               }
+       }
 }
 
 /**
@@ -576,9 +641,13 @@ class WebRequest {
  *
  */
 class FauxRequest extends WebRequest {
-       var $data = null;
        var $wasPosted = false;
 
+       /**
+        * @param array $data Array of *non*-urlencoded key => value pairs, the
+        *   fake GET/POST values
+        * @param bool $wasPosted Whether to treat the data as POST
+        */
        function FauxRequest( $data, $wasPosted = false ) {
                if( is_array( $data ) ) {
                        $this->data = $data;
@@ -586,15 +655,12 @@ class FauxRequest extends WebRequest {
                        throw new MWException( "FauxRequest() got bogus data" );
                }
                $this->wasPosted = $wasPosted;
-       }
-
-       function getVal( $name, $default = NULL ) {
-               return $this->getGPCVal( $this->data, $name, $default );
+               $this->headers = array();
        }
 
        function getText( $name, $default = '' ) {
                # Override; don't recode since we're using internal data
-               return $this->getVal( $name, $default );
+               return (string)$this->getVal( $name, $default );
        }
 
        function getValues() {
@@ -617,6 +683,10 @@ class FauxRequest extends WebRequest {
                throw new MWException( 'FauxRequest::appendQuery() not implemented' );
        }
 
+       function getHeader( $name ) {
+               return isset( $this->headers[$name] ) ? $this->headers[$name] : false;
+       }
+
 }
 
-?>
+