]> scripts.mit.edu Git - autoinstallsdev/mediawiki.git/blob - includes/WebRequest.php
MediaWiki 1.16.3
[autoinstallsdev/mediawiki.git] / includes / WebRequest.php
1 <?php
2 /**
3  * Deal with importing all those nasssty globals and things
4  */
5
6 # Copyright (C) 2003 Brion Vibber <brion@pobox.com>
7 # http://www.mediawiki.org/
8 #
9 # This program is free software; you can redistribute it and/or modify
10 # it under the terms of the GNU General Public License as published by
11 # the Free Software Foundation; either version 2 of the License, or
12 # (at your option) any later version.
13 #
14 # This program is distributed in the hope that it will be useful,
15 # but WITHOUT ANY WARRANTY; without even the implied warranty of
16 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 # GNU General Public License for more details.
18 #
19 # You should have received a copy of the GNU General Public License along
20 # with this program; if not, write to the Free Software Foundation, Inc.,
21 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
22 # http://www.gnu.org/copyleft/gpl.html
23
24
25 /**
26  * Some entry points may use this file without first enabling the
27  * autoloader.
28  */
29 if ( !function_exists( '__autoload' ) ) {
30         require_once( dirname(__FILE__) . '/normal/UtfNormal.php' );
31 }
32
33 /**
34  * The WebRequest class encapsulates getting at data passed in the
35  * URL or via a POSTed form, handling remove of "magic quotes" slashes,
36  * stripping illegal input characters and normalizing Unicode sequences.
37  *
38  * Usually this is used via a global singleton, $wgRequest. You should
39  * not create a second WebRequest object; make a FauxRequest object if
40  * you want to pass arbitrary data to some function in place of the web
41  * input.
42  *
43  * @ingroup HTTP
44  */
45 class WebRequest {
46         protected $data, $headers = array();
47         private $_response;
48
49         public function __construct() {
50                 /// @todo Fixme: this preemptive de-quoting can interfere with other web libraries
51                 ///        and increases our memory footprint. It would be cleaner to do on
52                 ///        demand; but currently we have no wrapper for $_SERVER etc.
53                 $this->checkMagicQuotes();
54
55                 // POST overrides GET data
56                 // We don't use $_REQUEST here to avoid interference from cookies...
57                 $this->data = $_POST + $_GET;
58         }
59
60         /**
61          * Check for title, action, and/or variant data in the URL
62          * and interpolate it into the GET variables.
63          * This should only be run after $wgContLang is available,
64          * as we may need the list of language variants to determine
65          * available variant URLs.
66          */
67         public function interpolateTitle() {
68                 global $wgUsePathInfo;
69
70                 if ( $wgUsePathInfo ) {
71                         // PATH_INFO is mangled due to http://bugs.php.net/bug.php?id=31892
72                         // And also by Apache 2.x, double slashes are converted to single slashes.
73                         // So we will use REQUEST_URI if possible.
74                         $matches = array();
75
76                         if ( !empty( $_SERVER['REQUEST_URI'] ) ) {
77                                 // Slurp out the path portion to examine...
78                                 $url = $_SERVER['REQUEST_URI'];
79                                 if ( !preg_match( '!^https?://!', $url ) ) {
80                                         $url = 'http://unused' . $url;
81                                 }
82                                 $a = parse_url( $url );
83                                 if( $a ) {
84                                         $path = isset( $a['path'] ) ? $a['path'] : '';
85
86                                         global $wgScript;
87                                         if( $path == $wgScript ) {
88                                                 // Script inside a rewrite path?
89                                                 // Abort to keep from breaking...
90                                                 return;
91                                         }
92                                         // Raw PATH_INFO style
93                                         $matches = $this->extractTitle( $path, "$wgScript/$1" );
94
95                                         global $wgArticlePath;
96                                         if( !$matches && $wgArticlePath ) {
97                                                 $matches = $this->extractTitle( $path, $wgArticlePath );
98                                         }
99
100                                         global $wgActionPaths;
101                                         if( !$matches && $wgActionPaths ) {
102                                                 $matches = $this->extractTitle( $path, $wgActionPaths, 'action' );
103                                         }
104
105                                         global $wgVariantArticlePath, $wgContLang;
106                                         if( !$matches && $wgVariantArticlePath ) {
107                                                 $variantPaths = array();
108                                                 foreach( $wgContLang->getVariants() as $variant ) {
109                                                         $variantPaths[$variant] =
110                                                                 str_replace( '$2', $variant, $wgVariantArticlePath );
111                                                 }
112                                                 $matches = $this->extractTitle( $path, $variantPaths, 'variant' );
113                                         }
114                                 }
115                         } elseif ( isset( $_SERVER['ORIG_PATH_INFO'] ) && $_SERVER['ORIG_PATH_INFO'] != '' ) {
116                                 // Mangled PATH_INFO
117                                 // http://bugs.php.net/bug.php?id=31892
118                                 // Also reported when ini_get('cgi.fix_pathinfo')==false
119                                 $matches['title'] = substr( $_SERVER['ORIG_PATH_INFO'], 1 );
120
121                         } elseif ( isset( $_SERVER['PATH_INFO'] ) && ($_SERVER['PATH_INFO'] != '') ) {
122                                 // Regular old PATH_INFO yay
123                                 $matches['title'] = substr( $_SERVER['PATH_INFO'], 1 );
124                         }
125                         foreach( $matches as $key => $val) {
126                                 $this->data[$key] = $_GET[$key] = $_REQUEST[$key] = $val;
127                         }
128                 }
129         }
130
131         /**
132          * Internal URL rewriting function; tries to extract page title and,
133          * optionally, one other fixed parameter value from a URL path.
134          *
135          * @param $path string: the URL path given from the client
136          * @param $bases array: one or more URLs, optionally with $1 at the end
137          * @param $key string: if provided, the matching key in $bases will be
138          *             passed on as the value of this URL parameter
139          * @return array of URL variables to interpolate; empty if no match
140          */
141         private function extractTitle( $path, $bases, $key=false ) {
142                 foreach( (array)$bases as $keyValue => $base ) {
143                         // Find the part after $wgArticlePath
144                         $base = str_replace( '$1', '', $base );
145                         $baseLen = strlen( $base );
146                         if( substr( $path, 0, $baseLen ) == $base ) {
147                                 $raw = substr( $path, $baseLen );
148                                 if( $raw !== '' ) {
149                                         $matches = array( 'title' => rawurldecode( $raw ) );
150                                         if( $key ) {
151                                                 $matches[$key] = $keyValue;
152                                         }
153                                         return $matches;
154                                 }
155                         }
156                 }
157                 return array();
158         }
159
160         /**
161          * Recursively strips slashes from the given array;
162          * used for undoing the evil that is magic_quotes_gpc.
163          * @param $arr array: will be modified
164          * @return array the original array
165          */
166         private function &fix_magic_quotes( &$arr ) {
167                 foreach( $arr as $key => $val ) {
168                         if( is_array( $val ) ) {
169                                 $this->fix_magic_quotes( $arr[$key] );
170                         } else {
171                                 $arr[$key] = stripslashes( $val );
172                         }
173                 }
174                 return $arr;
175         }
176
177         /**
178          * If magic_quotes_gpc option is on, run the global arrays
179          * through fix_magic_quotes to strip out the stupid slashes.
180          * WARNING: This should only be done once! Running a second
181          * time could damage the values.
182          */
183         private function checkMagicQuotes() {
184                 $mustFixQuotes = function_exists( 'get_magic_quotes_gpc' )
185                         && get_magic_quotes_gpc();
186                 if( $mustFixQuotes ) {
187                         $this->fix_magic_quotes( $_COOKIE );
188                         $this->fix_magic_quotes( $_ENV );
189                         $this->fix_magic_quotes( $_GET );
190                         $this->fix_magic_quotes( $_POST );
191                         $this->fix_magic_quotes( $_REQUEST );
192                         $this->fix_magic_quotes( $_SERVER );
193                 }
194         }
195
196         /**
197          * Recursively normalizes UTF-8 strings in the given array.
198          * @param $data string or array
199          * @return cleaned-up version of the given
200          * @private
201          */
202         function normalizeUnicode( $data ) {
203                 if( is_array( $data ) ) {
204                         foreach( $data as $key => $val ) {
205                                 $data[$key] = $this->normalizeUnicode( $val );
206                         }
207                 } else {
208                         global $wgContLang;
209                         $data = $wgContLang->normalize( $data );
210                 }
211                 return $data;
212         }
213
214         /**
215          * Fetch a value from the given array or return $default if it's not set.
216          *
217          * @param $arr array
218          * @param $name string
219          * @param $default mixed
220          * @return mixed
221          */
222         private function getGPCVal( $arr, $name, $default ) {
223                 # PHP is so nice to not touch input data, except sometimes:
224                 # http://us2.php.net/variables.external#language.variables.external.dot-in-names
225                 # Work around PHP *feature* to avoid *bugs* elsewhere.
226                 $name = strtr( $name, '.', '_' );
227                 if( isset( $arr[$name] ) ) {
228                         global $wgContLang;
229                         $data = $arr[$name];
230                         if( isset( $_GET[$name] ) && !is_array( $data ) ) {
231                                 # Check for alternate/legacy character encoding.
232                                 if( isset( $wgContLang ) ) {
233                                         $data = $wgContLang->checkTitleEncoding( $data );
234                                 }
235                         }
236                         $data = $this->normalizeUnicode( $data );
237                         return $data;
238                 } else {
239                         taint( $default );
240                         return $default;
241                 }
242         }
243
244         /**
245          * Fetch a scalar from the input or return $default if it's not set.
246          * Returns a string. Arrays are discarded. Useful for
247          * non-freeform text inputs (e.g. predefined internal text keys
248          * selected by a drop-down menu). For freeform input, see getText().
249          *
250          * @param $name string
251          * @param $default string: optional default (or NULL)
252          * @return string
253          */
254         public function getVal( $name, $default = null ) {
255                 $val = $this->getGPCVal( $this->data, $name, $default );
256                 if( is_array( $val ) ) {
257                         $val = $default;
258                 }
259                 if( is_null( $val ) ) {
260                         return $val;
261                 } else {
262                         return (string)$val;
263                 }
264         }
265
266         /**
267          * Set an aribtrary value into our get/post data.
268          * @param $key string Key name to use
269          * @param $value mixed Value to set
270          * @return mixed old value if one was present, null otherwise
271          */
272         public function setVal( $key, $value ) {
273                 $ret = isset( $this->data[$key] ) ? $this->data[$key] : null;
274                 $this->data[$key] = $value;
275                 return $ret;
276         }
277
278         /**
279          * Fetch an array from the input or return $default if it's not set.
280          * If source was scalar, will return an array with a single element.
281          * If no source and no default, returns NULL.
282          *
283          * @param $name string
284          * @param $default array: optional default (or NULL)
285          * @return array
286          */
287         public function getArray( $name, $default = null ) {
288                 $val = $this->getGPCVal( $this->data, $name, $default );
289                 if( is_null( $val ) ) {
290                         return null;
291                 } else {
292                         return (array)$val;
293                 }
294         }
295
296         /**
297          * Fetch an array of integers, or return $default if it's not set.
298          * If source was scalar, will return an array with a single element.
299          * If no source and no default, returns NULL.
300          * If an array is returned, contents are guaranteed to be integers.
301          *
302          * @param $name string
303          * @param $default array: option default (or NULL)
304          * @return array of ints
305          */
306         public function getIntArray( $name, $default = null ) {
307                 $val = $this->getArray( $name, $default );
308                 if( is_array( $val ) ) {
309                         $val = array_map( 'intval', $val );
310                 }
311                 return $val;
312         }
313
314         /**
315          * Fetch an integer value from the input or return $default if not set.
316          * Guaranteed to return an integer; non-numeric input will typically
317          * return 0.
318          * @param $name string
319          * @param $default int
320          * @return int
321          */
322         public function getInt( $name, $default = 0 ) {
323                 return intval( $this->getVal( $name, $default ) );
324         }
325
326         /**
327          * Fetch an integer value from the input or return null if empty.
328          * Guaranteed to return an integer or null; non-numeric input will
329          * typically return null.
330          * @param $name string
331          * @return int
332          */
333         public function getIntOrNull( $name ) {
334                 $val = $this->getVal( $name );
335                 return is_numeric( $val )
336                         ? intval( $val )
337                         : null;
338         }
339
340         /**
341          * Fetch a boolean value from the input or return $default if not set.
342          * Guaranteed to return true or false, with normal PHP semantics for
343          * boolean interpretation of strings.
344          * @param $name string
345          * @param $default bool
346          * @return bool
347          */
348         public function getBool( $name, $default = false ) {
349                 return $this->getVal( $name, $default ) ? true : false;
350         }
351
352         /**
353          * Return true if the named value is set in the input, whatever that
354          * value is (even "0"). Return false if the named value is not set.
355          * Example use is checking for the presence of check boxes in forms.
356          * @param $name string
357          * @return bool
358          */
359         public function getCheck( $name ) {
360                 # Checkboxes and buttons are only present when clicked
361                 # Presence connotes truth, abscense false
362                 $val = $this->getVal( $name, null );
363                 return isset( $val );
364         }
365
366         /**
367          * Fetch a text string from the given array or return $default if it's not
368          * set. \r is stripped from the text, and with some language modules there
369          * is an input transliteration applied. This should generally be used for
370          * form <textarea> and <input> fields. Used for user-supplied freeform text
371          * input (for which input transformations may be required - e.g. Esperanto
372          * x-coding).
373          *
374          * @param $name string
375          * @param $default string: optional
376          * @return string
377          */
378         public function getText( $name, $default = '' ) {
379                 global $wgContLang;
380                 $val = $this->getVal( $name, $default );
381                 return str_replace( "\r\n", "\n",
382                         $wgContLang->recodeInput( $val ) );
383         }
384
385         /**
386          * Extracts the given named values into an array.
387          * If no arguments are given, returns all input values.
388          * No transformation is performed on the values.
389          */
390         public function getValues() {
391                 $names = func_get_args();
392                 if ( count( $names ) == 0 ) {
393                         $names = array_keys( $this->data );
394                 }
395
396                 $retVal = array();
397                 foreach ( $names as $name ) {
398                         $value = $this->getVal( $name );
399                         if ( !is_null( $value ) ) {
400                                 $retVal[$name] = $value;
401                         }
402                 }
403                 return $retVal;
404         }
405
406         /**
407          * Returns true if the present request was reached by a POST operation,
408          * false otherwise (GET, HEAD, or command-line).
409          *
410          * Note that values retrieved by the object may come from the
411          * GET URL etc even on a POST request.
412          *
413          * @return bool
414          */
415         public function wasPosted() {
416                 return $_SERVER['REQUEST_METHOD'] == 'POST';
417         }
418
419         /**
420          * Returns true if there is a session cookie set.
421          * This does not necessarily mean that the user is logged in!
422          *
423          * If you want to check for an open session, use session_id()
424          * instead; that will also tell you if the session was opened
425          * during the current request (in which case the cookie will
426          * be sent back to the client at the end of the script run).
427          *
428          * @return bool
429          */
430         public function checkSessionCookie() {
431                 return isset( $_COOKIE[session_name()] );
432         }
433
434         /**
435          * Return the path portion of the request URI.
436          * @return string
437          */
438         public function getRequestURL() {
439                 if( isset( $_SERVER['REQUEST_URI']) && strlen($_SERVER['REQUEST_URI']) ) {
440                         $base = $_SERVER['REQUEST_URI'];
441                 } elseif( isset( $_SERVER['SCRIPT_NAME'] ) ) {
442                         // Probably IIS; doesn't set REQUEST_URI
443                         $base = $_SERVER['SCRIPT_NAME'];
444                         if( isset( $_SERVER['QUERY_STRING'] ) && $_SERVER['QUERY_STRING'] != '' ) {
445                                 $base .= '?' . $_SERVER['QUERY_STRING'];
446                         }
447                 } else {
448                         // This shouldn't happen!
449                         throw new MWException( "Web server doesn't provide either " .
450                                 "REQUEST_URI or SCRIPT_NAME. Report details of your " .
451                                 "web server configuration to http://bugzilla.wikimedia.org/" );
452                 }
453                 // User-agents should not send a fragment with the URI, but
454                 // if they do, and the web server passes it on to us, we
455                 // need to strip it or we get false-positive redirect loops
456                 // or weird output URLs
457                 $hash = strpos( $base, '#' );
458                 if( $hash !== false ) {
459                         $base = substr( $base, 0, $hash );
460                 }
461                 if( $base{0} == '/' ) {
462                         return $base;
463                 } else {
464                         // We may get paths with a host prepended; strip it.
465                         return preg_replace( '!^[^:]+://[^/]+/!', '/', $base );
466                 }
467         }
468
469         /**
470          * Return the request URI with the canonical service and hostname.
471          * @return string
472          */
473         public function getFullRequestURL() {
474                 global $wgServer;
475                 return $wgServer . $this->getRequestURL();
476         }
477
478         /**
479          * Take an arbitrary query and rewrite the present URL to include it
480          * @param $query String: query string fragment; do not include initial '?'
481          * @return string
482          */
483         public function appendQuery( $query ) {
484                 global $wgTitle;
485                 $basequery = '';
486                 foreach( $_GET as $var => $val ) {
487                         if ( $var == 'title' )
488                                 continue;
489                         if ( is_array( $val ) )
490                                 /* This will happen given a request like
491                                  * http://en.wikipedia.org/w/index.php?title[]=Special:Userlogin&returnto[]=Main_Page
492                                  */
493                                 continue;
494                         $basequery .= '&' . urlencode( $var ) . '=' . urlencode( $val );
495                 }
496                 $basequery .= '&' . $query;
497
498                 # Trim the extra &
499                 $basequery = substr( $basequery, 1 );
500                 return $wgTitle->getLocalURL( $basequery );
501         }
502
503         /**
504          * HTML-safe version of appendQuery().
505          * @param $query String: query string fragment; do not include initial '?'
506          * @return string
507          */
508         public function escapeAppendQuery( $query ) {
509                 return htmlspecialchars( $this->appendQuery( $query ) );
510         }
511
512         public function appendQueryValue( $key, $value, $onlyquery = false ) {
513                 return $this->appendQueryArray( array( $key => $value ), $onlyquery );
514         }
515
516         /**
517          * Appends or replaces value of query variables.
518          * @param $array Array of values to replace/add to query
519          * @param $onlyquery Bool: whether to only return the query string and not
520          *                   the complete URL
521          * @return string
522          */
523         public function appendQueryArray( $array, $onlyquery = false ) {
524                 global $wgTitle;
525                 $newquery = $_GET;
526                 unset( $newquery['title'] );
527                 $newquery = array_merge( $newquery, $array );
528                 $query = wfArrayToCGI( $newquery );
529                 return $onlyquery ? $query : $wgTitle->getLocalURL( $query );
530         }
531
532         /**
533          * Check for limit and offset parameters on the input, and return sensible
534          * defaults if not given. The limit must be positive and is capped at 5000.
535          * Offset must be positive but is not capped.
536          *
537          * @param $deflimit Integer: limit to use if no input and the user hasn't set the option.
538          * @param $optionname String: to specify an option other than rclimit to pull from.
539          * @return array first element is limit, second is offset
540          */
541         public function getLimitOffset( $deflimit = 50, $optionname = 'rclimit' ) {
542                 global $wgUser;
543
544                 $limit = $this->getInt( 'limit', 0 );
545                 if( $limit < 0 ) $limit = 0;
546                 if( ( $limit == 0 ) && ( $optionname != '' ) ) {
547                         $limit = (int)$wgUser->getOption( $optionname );
548                 }
549                 if( $limit <= 0 ) $limit = $deflimit;
550                 if( $limit > 5000 ) $limit = 5000; # We have *some* limits...
551
552                 $offset = $this->getInt( 'offset', 0 );
553                 if( $offset < 0 ) $offset = 0;
554
555                 return array( $limit, $offset );
556         }
557
558         /**
559          * Return the path to the temporary file where PHP has stored the upload.
560          * @param $key String:
561          * @return string or NULL if no such file.
562          */
563         public function getFileTempname( $key ) {
564                 if( !isset( $_FILES[$key] ) ) {
565                         return null;
566                 }
567                 return $_FILES[$key]['tmp_name'];
568         }
569
570         /**
571          * Return the size of the upload, or 0.
572          * @param $key String:
573          * @return integer
574          */
575         public function getFileSize( $key ) {
576                 if( !isset( $_FILES[$key] ) ) {
577                         return 0;
578                 }
579                 return $_FILES[$key]['size'];
580         }
581
582         /**
583          * Return the upload error or 0
584          * @param $key String:
585          * @return integer
586          */
587         public function getUploadError( $key ) {
588                 if( !isset( $_FILES[$key] ) || !isset( $_FILES[$key]['error'] ) ) {
589                         return 0/*UPLOAD_ERR_OK*/;
590                 }
591                 return $_FILES[$key]['error'];
592         }
593
594         /**
595          * Return the original filename of the uploaded file, as reported by
596          * the submitting user agent. HTML-style character entities are
597          * interpreted and normalized to Unicode normalization form C, in part
598          * to deal with weird input from Safari with non-ASCII filenames.
599          *
600          * Other than this the name is not verified for being a safe filename.
601          *
602          * @param $key String:
603          * @return string or NULL if no such file.
604          */
605         public function getFileName( $key ) {
606                 global $wgContLang;
607                 if( !isset( $_FILES[$key] ) ) {
608                         return null;
609                 }
610                 $name = $_FILES[$key]['name'];
611
612                 # Safari sends filenames in HTML-encoded Unicode form D...
613                 # Horrid and evil! Let's try to make some kind of sense of it.
614                 $name = Sanitizer::decodeCharReferences( $name );
615                 $name = $wgContLang->normalize( $name );
616                 wfDebug( "WebRequest::getFileName() '" . $_FILES[$key]['name'] . "' normalized to '$name'\n" );
617                 return $name;
618         }
619
620         /**
621          * Return a handle to WebResponse style object, for setting cookies,
622          * headers and other stuff, for Request being worked on.
623          */
624         public function response() {
625                 /* Lazy initialization of response object for this request */
626                 if ( !is_object( $this->_response ) ) {
627                         $class = ( $this instanceof FauxRequest ) ? 'FauxResponse' : 'WebResponse';
628                         $this->_response = new $class();
629                 }
630                 return $this->_response;
631         }
632
633         /**
634          * Get a request header, or false if it isn't set
635          * @param $name String: case-insensitive header name
636          */
637         public function getHeader( $name ) {
638                 $name = strtoupper( $name );
639                 if ( function_exists( 'apache_request_headers' ) ) {
640                         if ( !$this->headers ) {
641                                 foreach ( apache_request_headers() as $tempName => $tempValue ) {
642                                         $this->headers[ strtoupper( $tempName ) ] = $tempValue;
643                                 }
644                         }
645                         if ( isset( $this->headers[$name] ) ) {
646                                 return $this->headers[$name];
647                         } else {
648                                 return false;
649                         }
650                 } else {
651                         $name = 'HTTP_' . str_replace( '-', '_', $name );
652                         if ( isset( $_SERVER[$name] ) ) {
653                                 return $_SERVER[$name];
654                         } else {
655                                 return false;
656                         }
657                 }
658         }
659
660         /*
661          * Get data from $_SESSION
662          * @param $key String Name of key in $_SESSION
663          * @return mixed
664          */
665         public function getSessionData( $key ) {
666                 if( !isset( $_SESSION[$key] ) )
667                         return null;
668                 return $_SESSION[$key];
669         }
670
671         /**
672          * Set session data
673          * @param $key String Name of key in $_SESSION
674          * @param $data mixed
675          */
676         public function setSessionData( $key, $data ) {
677                 $_SESSION[$key] = $data;
678         }
679
680         /**
681          * Returns true if the PATH_INFO ends with an extension other than a script
682          * extension. This could confuse IE for scripts that send arbitrary data which
683          * is not HTML but may be detected as such.
684          *
685          * Various past attempts to use the URL to make this check have generally
686          * run up against the fact that CGI does not provide a standard method to
687          * determine the URL. PATH_INFO may be mangled (e.g. if cgi.fix_pathinfo=0),
688          * but only by prefixing it with the script name and maybe some other stuff,
689          * the extension is not mangled. So this should be a reasonably portable
690          * way to perform this security check.
691          *
692          * Also checks for anything that looks like a file extension at the end of
693          * QUERY_STRING, since IE 6 and earlier will use this to get the file type
694          * if there was no dot before the question mark (bug 28235).
695          */
696         public function isPathInfoBad() {
697                 global $wgScriptExtension;
698
699                 if ( isset( $_SERVER['QUERY_STRING'] ) 
700                         && preg_match( '/\.[a-z]{1,4}$/i', $_SERVER['QUERY_STRING'] ) )
701                 {
702                         // Bug 28235
703                         // Block only Internet Explorer, and requests with missing UA 
704                         // headers that could be IE users behind a privacy proxy.
705                         if ( !isset( $_SERVER['HTTP_USER_AGENT'] ) 
706                                 || preg_match( '/; *MSIE/', $_SERVER['HTTP_USER_AGENT'] ) )
707                         {
708                                 return true;
709                         }
710                 }
711
712                 if ( !isset( $_SERVER['PATH_INFO'] ) ) {
713                         return false;
714                 }
715                 $pi = $_SERVER['PATH_INFO'];
716                 $dotPos = strrpos( $pi, '.' );
717                 if ( $dotPos === false ) {
718                         return false;
719                 }
720                 $ext = substr( $pi, $dotPos );
721                 return !in_array( $ext, array( $wgScriptExtension, '.php', '.php5' ) );
722         }
723 }
724
725 /**
726  * WebRequest clone which takes values from a provided array.
727  *
728  * @ingroup HTTP
729  */
730 class FauxRequest extends WebRequest {
731         private $wasPosted = false;
732         private $session = array();
733         private $response;
734
735         /**
736          * @param $data Array of *non*-urlencoded key => value pairs, the
737          *   fake GET/POST values
738          * @param $wasPosted Bool: whether to treat the data as POST
739          */
740         public function __construct( $data, $wasPosted = false, $session = null ) {
741                 if( is_array( $data ) ) {
742                         $this->data = $data;
743                 } else {
744                         throw new MWException( "FauxRequest() got bogus data" );
745                 }
746                 $this->wasPosted = $wasPosted;
747                 if( $session )
748                         $this->session = $session;
749         }
750
751         private function notImplemented( $method ) {
752                 throw new MWException( "{$method}() not implemented" );
753         }
754
755         public function getText( $name, $default = '' ) {
756                 # Override; don't recode since we're using internal data
757                 return (string)$this->getVal( $name, $default );
758         }
759
760         public function getValues() {
761                 return $this->data;
762         }
763
764         public function wasPosted() {
765                 return $this->wasPosted;
766         }
767
768         public function checkSessionCookie() {
769                 return false;
770         }
771
772         public function getRequestURL() {
773                 $this->notImplemented( __METHOD__ );
774         }
775
776         public function appendQuery( $query ) {
777                 $this->notImplemented( __METHOD__ );
778         }
779
780         public function getHeader( $name ) {
781                 return isset( $this->headers[$name] ) ? $this->headers[$name] : false;
782         }
783
784         public function setHeader( $name, $val ) {
785                 $this->headers[$name] = $val;
786         }
787
788         public function getSessionData( $key ) {
789                 if( isset( $this->session[$key] ) )
790                         return $this->session[$key];
791         }
792
793         public function setSessionData( $key, $data ) {
794                 $this->notImplemented( __METHOD__ );
795         }
796
797         public function isPathInfoBad() {
798                 return false;
799         }
800 }