MediaWiki  REL1_19
WebRequest.php
Go to the documentation of this file.
00001 <?php
00038 class WebRequest {
00039         protected $data, $headers = array();
00040 
00045         private $response;
00046 
00051         private $ip;
00052 
00053         public function __construct() {
00057                 $this->checkMagicQuotes();
00058 
00059                 // POST overrides GET data
00060                 // We don't use $_REQUEST here to avoid interference from cookies...
00061                 $this->data = $_POST + $_GET;
00062         }
00063 
00079         static public function getPathInfo( $want = 'all' ) {
00080                 // PATH_INFO is mangled due to http://bugs.php.net/bug.php?id=31892
00081                 // And also by Apache 2.x, double slashes are converted to single slashes.
00082                 // So we will use REQUEST_URI if possible.
00083                 $matches = array();
00084                 if ( !empty( $_SERVER['REQUEST_URI'] ) ) {
00085                         // Slurp out the path portion to examine...
00086                         $url = $_SERVER['REQUEST_URI'];
00087                         if ( !preg_match( '!^https?://!', $url ) ) {
00088                                 $url = 'http://unused' . $url;
00089                         }
00090                         $a = parse_url( $url );
00091                         if( $a ) {
00092                                 $path = isset( $a['path'] ) ? $a['path'] : '';
00093 
00094                                 global $wgScript;
00095                                 if( $path == $wgScript && $want !== 'all' ) {
00096                                         // Script inside a rewrite path?
00097                                         // Abort to keep from breaking...
00098                                         return $matches;
00099                                 }
00100 
00101                                 $router = new PathRouter;
00102 
00103                                 // Raw PATH_INFO style
00104                                 $router->add( "$wgScript/$1" );
00105 
00106                                 if( isset( $_SERVER['SCRIPT_NAME'] )
00107                                         && preg_match( '/\.php5?/', $_SERVER['SCRIPT_NAME'] ) )
00108                                 {
00109                                         # Check for SCRIPT_NAME, we handle index.php explicitly
00110                                         # But we do have some other .php files such as img_auth.php
00111                                         # Don't let root article paths clober the parsing for them
00112                                         $router->add( $_SERVER['SCRIPT_NAME'] . "/$1" );
00113                                 }
00114 
00115                                 global $wgArticlePath;
00116                                 if( $wgArticlePath ) {
00117                                         $router->add( $wgArticlePath );
00118                                 }
00119 
00120                                 global $wgActionPaths;
00121                                 if( $wgActionPaths ) {
00122                                         $router->add( $wgActionPaths, array( 'action' => '$key' ) );
00123                                 }
00124 
00125                                 global $wgVariantArticlePath, $wgContLang;
00126                                 if( $wgVariantArticlePath ) {
00127                                         $router->add( $wgVariantArticlePath,
00128                                                 array( 'variant' => '$2'),
00129                                                 array( '$2' => $wgContLang->getVariants() )
00130                                         );
00131                                 }
00132 
00133                                 wfRunHooks( 'WebRequestPathInfoRouter', array( $router ) );
00134 
00135                                 $matches = $router->parse( $path );
00136                         }
00137                 } elseif ( isset( $_SERVER['ORIG_PATH_INFO'] ) && $_SERVER['ORIG_PATH_INFO'] != '' ) {
00138                         // Mangled PATH_INFO
00139                         // http://bugs.php.net/bug.php?id=31892
00140                         // Also reported when ini_get('cgi.fix_pathinfo')==false
00141                         $matches['title'] = substr( $_SERVER['ORIG_PATH_INFO'], 1 );
00142 
00143                 } elseif ( isset( $_SERVER['PATH_INFO'] ) && ($_SERVER['PATH_INFO'] != '') ) {
00144                         // Regular old PATH_INFO yay
00145                         $matches['title'] = substr( $_SERVER['PATH_INFO'], 1 );
00146                 }
00147 
00148                 return $matches;
00149         }
00150 
00157         public static function detectServer() {
00158                 list( $proto, $stdPort ) = self::detectProtocolAndStdPort();
00159 
00160                 $varNames = array( 'HTTP_HOST', 'SERVER_NAME', 'HOSTNAME', 'SERVER_ADDR' );
00161                 $host = 'localhost';
00162                 $port = $stdPort;
00163                 foreach ( $varNames as $varName ) {
00164                         if ( !isset( $_SERVER[$varName] ) ) {
00165                                 continue;
00166                         }
00167                         $parts = IP::splitHostAndPort( $_SERVER[$varName] );
00168                         if ( !$parts ) {
00169                                 // Invalid, do not use
00170                                 continue;
00171                         }
00172                         $host = $parts[0];
00173                         if ( $parts[1] === false ) {
00174                                 if ( isset( $_SERVER['SERVER_PORT'] ) ) {
00175                                         $port = $_SERVER['SERVER_PORT'];
00176                                 } // else leave it as $stdPort
00177                         } else {
00178                                 $port = $parts[1];
00179                         }
00180                         break;
00181                 }
00182 
00183                 return $proto . '://' . IP::combineHostAndPort( $host, $port, $stdPort );
00184         }
00185 
00189         public static function detectProtocolAndStdPort() {
00190                 return ( isset( $_SERVER['HTTPS'] ) && $_SERVER['HTTPS'] == 'on' ) ? array( 'https', 443 ) : array( 'http', 80 );
00191         }
00192 
00196         public static function detectProtocol() {
00197                 list( $proto, $stdPort ) = self::detectProtocolAndStdPort();
00198                 return $proto;
00199         }
00200 
00208         public function interpolateTitle() {
00209                 global $wgUsePathInfo;
00210 
00211                 // bug 16019: title interpolation on API queries is useless and sometimes harmful
00212                 if ( defined( 'MW_API' ) ) {
00213                         return;
00214                 }
00215 
00216                 if ( $wgUsePathInfo ) {
00217                         $matches = self::getPathInfo( 'title' );
00218                         foreach( $matches as $key => $val) {
00219                                 $this->data[$key] = $_GET[$key] = $_REQUEST[$key] = $val;
00220                         }
00221                 }
00222         }
00223 
00234         static function extractTitle( $path, $bases, $key = false ) {
00235                 foreach( (array)$bases as $keyValue => $base ) {
00236                         // Find the part after $wgArticlePath
00237                         $base = str_replace( '$1', '', $base );
00238                         $baseLen = strlen( $base );
00239                         if( substr( $path, 0, $baseLen ) == $base ) {
00240                                 $raw = substr( $path, $baseLen );
00241                                 if( $raw !== '' ) {
00242                                         $matches = array( 'title' => rawurldecode( $raw ) );
00243                                         if( $key ) {
00244                                                 $matches[$key] = $keyValue;
00245                                         }
00246                                         return $matches;
00247                                 }
00248                         }
00249                 }
00250                 return array();
00251         }
00252 
00264         private function &fix_magic_quotes( &$arr, $topLevel = true ) {
00265                 $clean = array();
00266                 foreach( $arr as $key => $val ) {
00267                         if( is_array( $val ) ) {
00268                                 $cleanKey = $topLevel ? stripslashes( $key ) : $key;
00269                                 $clean[$cleanKey] = $this->fix_magic_quotes( $arr[$key], false );
00270                         } else {
00271                                 $cleanKey = stripslashes( $key );
00272                                 $clean[$cleanKey] = stripslashes( $val );
00273                         }
00274                 }
00275                 $arr = $clean;
00276                 return $arr;
00277         }
00278 
00285         private function checkMagicQuotes() {
00286                 $mustFixQuotes = function_exists( 'get_magic_quotes_gpc' )
00287                         && get_magic_quotes_gpc();
00288                 if( $mustFixQuotes ) {
00289                         $this->fix_magic_quotes( $_COOKIE );
00290                         $this->fix_magic_quotes( $_ENV );
00291                         $this->fix_magic_quotes( $_GET );
00292                         $this->fix_magic_quotes( $_POST );
00293                         $this->fix_magic_quotes( $_REQUEST );
00294                         $this->fix_magic_quotes( $_SERVER );
00295                 }
00296         }
00297 
00305         function normalizeUnicode( $data ) {
00306                 if( is_array( $data ) ) {
00307                         foreach( $data as $key => $val ) {
00308                                 $data[$key] = $this->normalizeUnicode( $val );
00309                         }
00310                 } else {
00311                         global $wgContLang;
00312                         $data = isset( $wgContLang ) ? $wgContLang->normalize( $data ) : UtfNormal::cleanUp( $data );
00313                 }
00314                 return $data;
00315         }
00316 
00325         private function getGPCVal( $arr, $name, $default ) {
00326                 # PHP is so nice to not touch input data, except sometimes:
00327                 # http://us2.php.net/variables.external#language.variables.external.dot-in-names
00328                 # Work around PHP *feature* to avoid *bugs* elsewhere.
00329                 $name = strtr( $name, '.', '_' );
00330                 if( isset( $arr[$name] ) ) {
00331                         global $wgContLang;
00332                         $data = $arr[$name];
00333                         if( isset( $_GET[$name] ) && !is_array( $data ) ) {
00334                                 # Check for alternate/legacy character encoding.
00335                                 if( isset( $wgContLang ) ) {
00336                                         $data = $wgContLang->checkTitleEncoding( $data );
00337                                 }
00338                         }
00339                         $data = $this->normalizeUnicode( $data );
00340                         return $data;
00341                 } else {
00342                         taint( $default );
00343                         return $default;
00344                 }
00345         }
00346 
00357         public function getVal( $name, $default = null ) {
00358                 $val = $this->getGPCVal( $this->data, $name, $default );
00359                 if( is_array( $val ) ) {
00360                         $val = $default;
00361                 }
00362                 if( is_null( $val ) ) {
00363                         return $val;
00364                 } else {
00365                         return (string)$val;
00366                 }
00367         }
00368 
00376         public function setVal( $key, $value ) {
00377                 $ret = isset( $this->data[$key] ) ? $this->data[$key] : null;
00378                 $this->data[$key] = $value;
00379                 return $ret;
00380         }
00381 
00391         public function getArray( $name, $default = null ) {
00392                 $val = $this->getGPCVal( $this->data, $name, $default );
00393                 if( is_null( $val ) ) {
00394                         return null;
00395                 } else {
00396                         return (array)$val;
00397                 }
00398         }
00399 
00410         public function getIntArray( $name, $default = null ) {
00411                 $val = $this->getArray( $name, $default );
00412                 if( is_array( $val ) ) {
00413                         $val = array_map( 'intval', $val );
00414                 }
00415                 return $val;
00416         }
00417 
00427         public function getInt( $name, $default = 0 ) {
00428                 return intval( $this->getVal( $name, $default ) );
00429         }
00430 
00439         public function getIntOrNull( $name ) {
00440                 $val = $this->getVal( $name );
00441                 return is_numeric( $val )
00442                         ? intval( $val )
00443                         : null;
00444         }
00445 
00455         public function getBool( $name, $default = false ) {
00456                 return (bool)$this->getVal( $name, $default );
00457         }
00458 
00468         public function getFuzzyBool( $name, $default = false ) {
00469                 return $this->getBool( $name, $default ) && strcasecmp( $this->getVal( $name ), 'false' ) !== 0;
00470         }
00471 
00480         public function getCheck( $name ) {
00481                 # Checkboxes and buttons are only present when clicked
00482                 # Presence connotes truth, abscense false
00483                 $val = $this->getVal( $name, null );
00484                 return isset( $val );
00485         }
00486 
00499         public function getText( $name, $default = '' ) {
00500                 global $wgContLang;
00501                 $val = $this->getVal( $name, $default );
00502                 return str_replace( "\r\n", "\n",
00503                         $wgContLang->recodeInput( $val ) );
00504         }
00505 
00513         public function getValues() {
00514                 $names = func_get_args();
00515                 if ( count( $names ) == 0 ) {
00516                         $names = array_keys( $this->data );
00517                 }
00518 
00519                 $retVal = array();
00520                 foreach ( $names as $name ) {
00521                         $value = $this->getVal( $name );
00522                         if ( !is_null( $value ) ) {
00523                                 $retVal[$name] = $value;
00524                         }
00525                 }
00526                 return $retVal;
00527         }
00528 
00535         public function getValueNames( $exclude = array() ) {
00536                 return array_diff( array_keys( $this->getValues() ), $exclude );
00537         }
00538 
00545          public function getQueryValues() {
00546                 return $_GET;
00547          }
00548 
00558         public function wasPosted() {
00559                 return isset( $_SERVER['REQUEST_METHOD'] ) && $_SERVER['REQUEST_METHOD'] == 'POST';
00560         }
00561 
00573         public function checkSessionCookie() {
00574                 return isset( $_COOKIE[ session_name() ] );
00575         }
00576 
00585         public function getCookie( $key, $prefix = null, $default = null ) {
00586                 if( $prefix === null ) {
00587                         global $wgCookiePrefix;
00588                         $prefix = $wgCookiePrefix;
00589                 }
00590                 return $this->getGPCVal( $_COOKIE, $prefix . $key , $default );
00591         }
00592 
00599         public function getRequestURL() {
00600                 if( isset( $_SERVER['REQUEST_URI'] ) && strlen( $_SERVER['REQUEST_URI'] ) ) {
00601                         $base = $_SERVER['REQUEST_URI'];
00602                 } elseif ( isset( $_SERVER['HTTP_X_ORIGINAL_URL'] ) && strlen( $_SERVER['HTTP_X_ORIGINAL_URL'] ) ) {
00603                         // Probably IIS; doesn't set REQUEST_URI
00604                         $base = $_SERVER['HTTP_X_ORIGINAL_URL'];
00605                 } elseif( isset( $_SERVER['SCRIPT_NAME'] ) ) {
00606                         $base = $_SERVER['SCRIPT_NAME'];
00607                         if( isset( $_SERVER['QUERY_STRING'] ) && $_SERVER['QUERY_STRING'] != '' ) {
00608                                 $base .= '?' . $_SERVER['QUERY_STRING'];
00609                         }
00610                 } else {
00611                         // This shouldn't happen!
00612                         throw new MWException( "Web server doesn't provide either " .
00613                                 "REQUEST_URI, HTTP_X_ORIGINAL_URL or SCRIPT_NAME. Report details " .
00614                                 "of your web server configuration to http://bugzilla.wikimedia.org/" );
00615                 }
00616                 // User-agents should not send a fragment with the URI, but
00617                 // if they do, and the web server passes it on to us, we
00618                 // need to strip it or we get false-positive redirect loops
00619                 // or weird output URLs
00620                 $hash = strpos( $base, '#' );
00621                 if( $hash !== false ) {
00622                         $base = substr( $base, 0, $hash );
00623                 }
00624                 if( $base[0] == '/' ) {
00625                         return $base;
00626                 } else {
00627                         // We may get paths with a host prepended; strip it.
00628                         return preg_replace( '!^[^:]+://[^/]+/!', '/', $base );
00629                 }
00630         }
00631 
00642         public function getFullRequestURL() {
00643                 return wfExpandUrl( $this->getRequestURL(), PROTO_CURRENT );
00644         }
00645 
00652         public function appendQuery( $query ) {
00653                 return $this->appendQueryArray( wfCgiToArray( $query ) );
00654         }
00655 
00662         public function escapeAppendQuery( $query ) {
00663                 return htmlspecialchars( $this->appendQuery( $query ) );
00664         }
00665 
00672         public function appendQueryValue( $key, $value, $onlyquery = false ) {
00673                 return $this->appendQueryArray( array( $key => $value ), $onlyquery );
00674         }
00675 
00684         public function appendQueryArray( $array, $onlyquery = false ) {
00685                 global $wgTitle;
00686                 $newquery = $this->getQueryValues();
00687                 unset( $newquery['title'] );
00688                 $newquery = array_merge( $newquery, $array );
00689                 $query = wfArrayToCGI( $newquery );
00690                 return $onlyquery ? $query : $wgTitle->getLocalURL( $query );
00691         }
00692 
00702         public function getLimitOffset( $deflimit = 50, $optionname = 'rclimit' ) {
00703                 global $wgUser;
00704 
00705                 $limit = $this->getInt( 'limit', 0 );
00706                 if( $limit < 0 ) {
00707                         $limit = 0;
00708                 }
00709                 if( ( $limit == 0 ) && ( $optionname != '' ) ) {
00710                         $limit = (int)$wgUser->getOption( $optionname );
00711                 }
00712                 if( $limit <= 0 ) {
00713                         $limit = $deflimit;
00714                 }
00715                 if( $limit > 5000 ) {
00716                         $limit = 5000; # We have *some* limits...
00717                 }
00718 
00719                 $offset = $this->getInt( 'offset', 0 );
00720                 if( $offset < 0 ) {
00721                         $offset = 0;
00722                 }
00723 
00724                 return array( $limit, $offset );
00725         }
00726 
00733         public function getFileTempname( $key ) {
00734                 $file = new WebRequestUpload( $this, $key );
00735                 return $file->getTempName();
00736         }
00737 
00745         public function getFileSize( $key ) {
00746                 wfDeprecated( __METHOD__, '1.17' );
00747                 $file = new WebRequestUpload( $this, $key );
00748                 return $file->getSize();
00749         }
00750 
00757         public function getUploadError( $key ) {
00758                 $file = new WebRequestUpload( $this, $key );
00759                 return $file->getError();
00760         }
00761 
00773         public function getFileName( $key ) {
00774                 $file = new WebRequestUpload( $this, $key );
00775                 return $file->getName();
00776         }
00777 
00784         public function getUpload( $key ) {
00785                 return new WebRequestUpload( $this, $key );
00786         }
00787 
00794         public function response() {
00795                 /* Lazy initialization of response object for this request */
00796                 if ( !is_object( $this->response ) ) {
00797                         $class = ( $this instanceof FauxRequest ) ? 'FauxResponse' : 'WebResponse';
00798                         $this->response = new $class();
00799                 }
00800                 return $this->response;
00801         }
00802 
00806         private function initHeaders() {
00807                 if ( count( $this->headers ) ) {
00808                         return;
00809                 }
00810 
00811                 if ( function_exists( 'apache_request_headers' ) ) {
00812                         foreach ( apache_request_headers() as $tempName => $tempValue ) {
00813                                 $this->headers[ strtoupper( $tempName ) ] = $tempValue;
00814                         }
00815                 } else {
00816                         foreach ( $_SERVER as $name => $value ) {
00817                                 if ( substr( $name, 0, 5 ) === 'HTTP_' ) {
00818                                         $name = str_replace( '_', '-',  substr( $name, 5 ) );
00819                                         $this->headers[$name] = $value;
00820                                 } elseif ( $name === 'CONTENT_LENGTH' ) {
00821                                         $this->headers['CONTENT-LENGTH'] = $value;
00822                                 }
00823                         }
00824                 }
00825         }
00826 
00832         public function getAllHeaders() {
00833                 $this->initHeaders();
00834                 return $this->headers;
00835         }
00836 
00843         public function getHeader( $name ) {
00844                 $this->initHeaders();
00845                 $name = strtoupper( $name );
00846                 if ( isset( $this->headers[$name] ) ) {
00847                         return $this->headers[$name];
00848                 } else {
00849                         return false;
00850                 }
00851         }
00852 
00859         public function getSessionData( $key ) {
00860                 if( !isset( $_SESSION[$key] ) ) {
00861                         return null;
00862                 }
00863                 return $_SESSION[$key];
00864         }
00865 
00872         public function setSessionData( $key, $data ) {
00873                 $_SESSION[$key] = $data;
00874         }
00875 
00885         public function checkUrlExtension( $extWhitelist = array() ) {
00886                 global $wgScriptExtension;
00887                 $extWhitelist[] = ltrim( $wgScriptExtension, '.' );
00888                 if ( IEUrlExtension::areServerVarsBad( $_SERVER, $extWhitelist ) ) {
00889                         if ( !$this->wasPosted() ) {
00890                                 $newUrl = IEUrlExtension::fixUrlForIE6(
00891                                         $this->getFullRequestURL(), $extWhitelist );
00892                                 if ( $newUrl !== false ) {
00893                                         $this->doSecurityRedirect( $newUrl );
00894                                         return false;
00895                                 }
00896                         }
00897                         throw new HttpError( 403,
00898                                 'Invalid file extension found in the path info or query string.' );
00899                 }
00900                 return true;
00901         }
00902 
00910         protected function doSecurityRedirect( $url ) {
00911                 header( 'Location: ' . $url );
00912                 header( 'Content-Type: text/html' );
00913                 $encUrl = htmlspecialchars( $url );
00914                 echo <<<HTML
00915 <html>
00916 <head>
00917 <title>Security redirect</title>
00918 </head>
00919 <body>
00920 <h1>Security redirect</h1>
00921 <p>
00922 We can't serve non-HTML content from the URL you have requested, because
00923 Internet Explorer would interpret it as an incorrect and potentially dangerous
00924 content type.</p>
00925 <p>Instead, please use <a href="$encUrl">this URL</a>, which is the same as the URL you have requested, except that
00926 "&amp;*" is appended. This prevents Internet Explorer from seeing a bogus file
00927 extension.
00928 </p>
00929 </body>
00930 </html>
00931 HTML;
00932                 echo "\n";
00933                 return true;
00934         }
00935 
00958         public function isPathInfoBad( $extWhitelist = array() ) {
00959                 wfDeprecated( __METHOD__, '1.17' );
00960                 global $wgScriptExtension;
00961                 $extWhitelist[] = ltrim( $wgScriptExtension, '.' );
00962                 return IEUrlExtension::areServerVarsBad( $_SERVER, $extWhitelist );
00963         }
00964 
00971         public function getAcceptLang() {
00972                 // Modified version of code found at http://www.thefutureoftheweb.com/blog/use-accept-language-header
00973                 $acceptLang = $this->getHeader( 'Accept-Language' );
00974                 if ( !$acceptLang ) {
00975                         return array();
00976                 }
00977 
00978                 // Return the language codes in lower case
00979                 $acceptLang = strtolower( $acceptLang );
00980 
00981                 // Break up string into pieces (languages and q factors)
00982                 $lang_parse = null;
00983                 preg_match_all( '/([a-z]{1,8}(-[a-z]{1,8})*|\*)\s*(;\s*q\s*=\s*(1(\.0{0,3})?|0(\.[0-9]{0,3})?)?)?/',
00984                         $acceptLang, $lang_parse );
00985 
00986                 if ( !count( $lang_parse[1] ) ) {
00987                         return array();
00988                 }
00989 
00990                 // Create a list like "en" => 0.8
00991                 $langs = array_combine( $lang_parse[1], $lang_parse[4] );
00992                 // Set default q factor to 1
00993                 foreach ( $langs as $lang => $val ) {
00994                         if ( $val === '' ) {
00995                                 $langs[$lang] = 1;
00996                         } elseif ( $val == 0 ) {
00997                                 unset($langs[$lang]);
00998                         }
00999                 }
01000 
01001                 // Sort list
01002                 arsort( $langs, SORT_NUMERIC );
01003                 return $langs;
01004         }
01005 
01013         protected function getRawIP() {
01014                 if ( isset( $_SERVER['REMOTE_ADDR'] ) ) {
01015                         return IP::canonicalize( $_SERVER['REMOTE_ADDR'] );
01016                 } else {
01017                         return null;
01018                 }
01019         }
01020 
01029         public function getIP() {
01030                 global $wgUsePrivateIPs;
01031 
01032                 # Return cached result
01033                 if ( $this->ip !== null ) {
01034                         return $this->ip;
01035                 }
01036 
01037                 # collect the originating ips
01038                 $ip = $this->getRawIP();
01039 
01040                 # Append XFF
01041                 $forwardedFor = $this->getHeader( 'X-Forwarded-For' );
01042                 if ( $forwardedFor !== false ) {
01043                         $ipchain = array_map( 'trim', explode( ',', $forwardedFor ) );
01044                         $ipchain = array_reverse( $ipchain );
01045                         if ( $ip ) {
01046                                 array_unshift( $ipchain, $ip );
01047                         }
01048 
01049                         # Step through XFF list and find the last address in the list which is a trusted server
01050                         # Set $ip to the IP address given by that trusted server, unless the address is not sensible (e.g. private)
01051                         foreach ( $ipchain as $i => $curIP ) {
01052                                 $curIP = IP::canonicalize( $curIP );
01053                                 if ( wfIsTrustedProxy( $curIP ) ) {
01054                                         if ( isset( $ipchain[$i + 1] ) ) {
01055                                                 if ( $wgUsePrivateIPs || IP::isPublic( $ipchain[$i + 1 ] ) ) {
01056                                                         $ip = $ipchain[$i + 1];
01057                                                 }
01058                                         }
01059                                 } else {
01060                                         break;
01061                                 }
01062                         }
01063                 }
01064 
01065                 # Allow extensions to improve our guess
01066                 wfRunHooks( 'GetIP', array( &$ip ) );
01067 
01068                 if ( !$ip ) {
01069                         throw new MWException( "Unable to determine IP" );
01070                 }
01071 
01072                 wfDebug( "IP: $ip\n" );
01073                 $this->ip = $ip;
01074                 return $ip;
01075         }
01076 }
01077 
01081 class WebRequestUpload {
01082         protected $request;
01083         protected $doesExist;
01084         protected $fileInfo;
01085 
01092         public function __construct( $request, $key ) {
01093                 $this->request = $request;
01094                 $this->doesExist = isset( $_FILES[$key] );
01095                 if ( $this->doesExist ) {
01096                         $this->fileInfo = $_FILES[$key];
01097                 }
01098         }
01099 
01105         public function exists() {
01106                 return $this->doesExist;
01107         }
01108 
01114         public function getName() {
01115                 if ( !$this->exists() ) {
01116                         return null;
01117                 }
01118 
01119                 global $wgContLang;
01120                 $name = $this->fileInfo['name'];
01121 
01122                 # Safari sends filenames in HTML-encoded Unicode form D...
01123                 # Horrid and evil! Let's try to make some kind of sense of it.
01124                 $name = Sanitizer::decodeCharReferences( $name );
01125                 $name = $wgContLang->normalize( $name );
01126                 wfDebug( __METHOD__ . ": {$this->fileInfo['name']} normalized to '$name'\n" );
01127                 return $name;
01128         }
01129 
01135         public function getSize() {
01136                 if ( !$this->exists() ) {
01137                         return 0;
01138                 }
01139 
01140                 return $this->fileInfo['size'];
01141         }
01142 
01148         public function getTempName() {
01149                 if ( !$this->exists() ) {
01150                         return null;
01151                 }
01152 
01153                 return $this->fileInfo['tmp_name'];
01154         }
01155 
01162         public function getError() {
01163                 if ( !$this->exists() ) {
01164                         return 0; # UPLOAD_ERR_OK
01165                 }
01166 
01167                 return $this->fileInfo['error'];
01168         }
01169 
01176         public function isIniSizeOverflow() {
01177                 if ( $this->getError() == UPLOAD_ERR_INI_SIZE ) {
01178                         # PHP indicated that upload_max_filesize is exceeded
01179                         return true;
01180                 }
01181 
01182                 $contentLength = $this->request->getHeader( 'CONTENT_LENGTH' );
01183                 if ( $contentLength > wfShorthandToInteger( ini_get( 'post_max_size' ) ) ) {
01184                         # post_max_size is exceeded
01185                         return true;
01186                 }
01187 
01188                 return false;
01189         }
01190 }
01191 
01197 class FauxRequest extends WebRequest {
01198         private $wasPosted = false;
01199         private $session = array();
01200 
01207         public function __construct( $data = array(), $wasPosted = false, $session = null ) {
01208                 if( is_array( $data ) ) {
01209                         $this->data = $data;
01210                 } else {
01211                         throw new MWException( "FauxRequest() got bogus data" );
01212                 }
01213                 $this->wasPosted = $wasPosted;
01214                 if( $session )
01215                         $this->session = $session;
01216         }
01217 
01222         private function notImplemented( $method ) {
01223                 throw new MWException( "{$method}() not implemented" );
01224         }
01225 
01231         public function getText( $name, $default = '' ) {
01232                 # Override; don't recode since we're using internal data
01233                 return (string)$this->getVal( $name, $default );
01234         }
01235 
01239         public function getValues() {
01240                 return $this->data;
01241         }
01242 
01246         public function getQueryValues() {
01247                 if ( $this->wasPosted ) {
01248                         return array();
01249                 } else {
01250                         return $this->data;
01251                 }
01252         }
01253 
01257         public function wasPosted() {
01258                 return $this->wasPosted;
01259         }
01260 
01261         public function getCookie( $key, $prefix = null, $default = null ) {
01262                 return $default;
01263         }
01264 
01265         public function checkSessionCookie() {
01266                 return false;
01267         }
01268 
01269         public function getRequestURL() {
01270                 $this->notImplemented( __METHOD__ );
01271         }
01272 
01277         public function getHeader( $name ) {
01278                 return isset( $this->headers[$name] ) ? $this->headers[$name] : false;
01279         }
01280 
01285         public function setHeader( $name, $val ) {
01286                 $this->headers[$name] = $val;
01287         }
01288 
01293         public function getSessionData( $key ) {
01294                 if( isset( $this->session[$key] ) )
01295                         return $this->session[$key];
01296         }
01297 
01302         public function setSessionData( $key, $data ) {
01303                 $this->session[$key] = $data;
01304         }
01305 
01309         public function getSessionArray() {
01310                 return $this->session;
01311         }
01312 
01317         public function isPathInfoBad( $extWhitelist = array() ) {
01318                 return false;
01319         }
01320 
01325         public function checkUrlExtension( $extWhitelist = array() ) {
01326                 return true;
01327         }
01328 
01332         protected function getRawIP() {
01333                 return '127.0.0.1';
01334         }
01335 }
01336 
01344 class DerivativeRequest extends FauxRequest {
01345         private $base;
01346 
01347         public function __construct( WebRequest $base, $data, $wasPosted = false ) {
01348                 $this->base = $base;
01349                 parent::__construct( $data, $wasPosted );
01350         }
01351 
01352         public function getCookie( $key, $prefix = null, $default = null ) {
01353                 return $this->base->getCookie( $key, $prefix, $default );
01354         }
01355 
01356         public function checkSessionCookie() {
01357                 return $this->base->checkSessionCookie();
01358         }
01359 
01360         public function getHeader( $name ) {
01361                 return $this->base->getHeader( $name );
01362         }
01363 
01364         public function getAllHeaders() {
01365                 return $this->base->getAllHeaders();
01366         }
01367 
01368         public function getSessionData( $key ) {
01369                 return $this->base->getSessionData( $key );
01370         }
01371 
01372         public function setSessionData( $key, $data ) {
01373                 return $this->base->setSessionData( $key, $data );
01374         }
01375 
01376         public function getAcceptLang() {
01377                 return $this->base->getAcceptLang();
01378         }
01379 
01380         public function getIP() {
01381                 return $this->base->getIP();
01382         }
01383 }