Tweak doc comments on WebRequest::getRequestURL() & WebRequest::getFullRequestURL...
[lhc/web/wiklou.git] / includes / WebRequest.php
1 <?php
2 /**
3 * Deal with importing all those nasssty globals and things
4 *
5 * Copyright © 2003 Brion Vibber <brion@pobox.com>
6 * http://www.mediawiki.org/
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
21 * http://www.gnu.org/copyleft/gpl.html
22 *
23 * @file
24 */
25
26 /**
27 * The WebRequest class encapsulates getting at data passed in the
28 * URL or via a POSTed form, handling remove of "magic quotes" slashes,
29 * stripping illegal input characters and normalizing Unicode sequences.
30 *
31 * Usually this is used via a global singleton, $wgRequest. You should
32 * not create a second WebRequest object; make a FauxRequest object if
33 * you want to pass arbitrary data to some function in place of the web
34 * input.
35 *
36 * @ingroup HTTP
37 */
38 class WebRequest {
39 protected $data, $headers = array();
40
41 /**
42 * Lazy-init response object
43 * @var WebResponse
44 */
45 private $response;
46
47 public function __construct() {
48 /// @todo FIXME: This preemptive de-quoting can interfere with other web libraries
49 /// and increases our memory footprint. It would be cleaner to do on
50 /// demand; but currently we have no wrapper for $_SERVER etc.
51 $this->checkMagicQuotes();
52
53 // POST overrides GET data
54 // We don't use $_REQUEST here to avoid interference from cookies...
55 $this->data = $_POST + $_GET;
56 }
57
58 /**
59 * Extract the PATH_INFO variable even when it isn't a reasonable
60 * value. On some large webhosts, PATH_INFO includes the script
61 * path as well as everything after it.
62 *
63 * @param $want string: If this is not 'all', then the function
64 * will return an empty array if it determines that the URL is
65 * inside a rewrite path.
66 *
67 * @return Array: 'title' key is the title of the article.
68 */
69 static public function getPathInfo( $want = 'all' ) {
70 // PATH_INFO is mangled due to http://bugs.php.net/bug.php?id=31892
71 // And also by Apache 2.x, double slashes are converted to single slashes.
72 // So we will use REQUEST_URI if possible.
73 $matches = array();
74 if ( !empty( $_SERVER['REQUEST_URI'] ) ) {
75 // Slurp out the path portion to examine...
76 $url = $_SERVER['REQUEST_URI'];
77 if ( !preg_match( '!^https?://!', $url ) ) {
78 $url = 'http://unused' . $url;
79 }
80 $a = parse_url( $url );
81 if( $a ) {
82 $path = isset( $a['path'] ) ? $a['path'] : '';
83
84 global $wgScript;
85 if( $path == $wgScript && $want !== 'all' ) {
86 // Script inside a rewrite path?
87 // Abort to keep from breaking...
88 return $matches;
89 }
90 // Raw PATH_INFO style
91 $matches = self::extractTitle( $path, "$wgScript/$1" );
92
93 global $wgArticlePath;
94 if( !$matches && $wgArticlePath ) {
95 $matches = self::extractTitle( $path, $wgArticlePath );
96 }
97
98 global $wgActionPaths;
99 if( !$matches && $wgActionPaths ) {
100 $matches = self::extractTitle( $path, $wgActionPaths, 'action' );
101 }
102
103 global $wgVariantArticlePath, $wgContLang;
104 if( !$matches && $wgVariantArticlePath ) {
105 $variantPaths = array();
106 foreach( $wgContLang->getVariants() as $variant ) {
107 $variantPaths[$variant] =
108 str_replace( '$2', $variant, $wgVariantArticlePath );
109 }
110 $matches = self::extractTitle( $path, $variantPaths, 'variant' );
111 }
112 }
113 } elseif ( isset( $_SERVER['ORIG_PATH_INFO'] ) && $_SERVER['ORIG_PATH_INFO'] != '' ) {
114 // Mangled PATH_INFO
115 // http://bugs.php.net/bug.php?id=31892
116 // Also reported when ini_get('cgi.fix_pathinfo')==false
117 $matches['title'] = substr( $_SERVER['ORIG_PATH_INFO'], 1 );
118
119 } elseif ( isset( $_SERVER['PATH_INFO'] ) && ($_SERVER['PATH_INFO'] != '') ) {
120 // Regular old PATH_INFO yay
121 $matches['title'] = substr( $_SERVER['PATH_INFO'], 1 );
122 }
123
124 return $matches;
125 }
126
127 /**
128 * Work out an appropriate URL prefix containing scheme and host, based on
129 * information detected from $_SERVER
130 */
131 public static function detectServer() {
132 if ( isset( $_SERVER['HTTPS'] ) && $_SERVER['HTTPS'] == 'on') {
133 $proto = 'https';
134 $stdPort = 443;
135 } else {
136 $proto = 'http';
137 $stdPort = 80;
138 }
139
140 $varNames = array( 'HTTP_HOST', 'SERVER_NAME', 'HOSTNAME', 'SERVER_ADDR' );
141 $host = 'localhost';
142 $port = $stdPort;
143 foreach ( $varNames as $varName ) {
144 if ( !isset( $_SERVER[$varName] ) ) {
145 continue;
146 }
147 $parts = IP::splitHostAndPort( $_SERVER[$varName] );
148 if ( !$parts ) {
149 // Invalid, do not use
150 continue;
151 }
152 $host = $parts[0];
153 if ( $parts[1] === false ) {
154 if ( isset( $_SERVER['SERVER_PORT'] ) ) {
155 $port = $_SERVER['SERVER_PORT'];
156 } // else leave it as $stdPort
157 } else {
158 $port = $parts[1];
159 }
160 break;
161 }
162
163 return $proto . '://' . IP::combineHostAndPort( $host, $port, $stdPort );
164 }
165
166 /**
167 * Check for title, action, and/or variant data in the URL
168 * and interpolate it into the GET variables.
169 * This should only be run after $wgContLang is available,
170 * as we may need the list of language variants to determine
171 * available variant URLs.
172 */
173 public function interpolateTitle() {
174 global $wgUsePathInfo;
175
176 // bug 16019: title interpolation on API queries is useless and sometimes harmful
177 if ( defined( 'MW_API' ) ) {
178 return;
179 }
180
181 if ( $wgUsePathInfo ) {
182 $matches = self::getPathInfo( 'title' );
183 foreach( $matches as $key => $val) {
184 $this->data[$key] = $_GET[$key] = $_REQUEST[$key] = $val;
185 }
186 }
187 }
188
189 /**
190 * Internal URL rewriting function; tries to extract page title and,
191 * optionally, one other fixed parameter value from a URL path.
192 *
193 * @param $path string: the URL path given from the client
194 * @param $bases array: one or more URLs, optionally with $1 at the end
195 * @param $key string: if provided, the matching key in $bases will be
196 * passed on as the value of this URL parameter
197 * @return array of URL variables to interpolate; empty if no match
198 */
199 private static function extractTitle( $path, $bases, $key=false ) {
200 foreach( (array)$bases as $keyValue => $base ) {
201 // Find the part after $wgArticlePath
202 $base = str_replace( '$1', '', $base );
203 $baseLen = strlen( $base );
204 if( substr( $path, 0, $baseLen ) == $base ) {
205 $raw = substr( $path, $baseLen );
206 if( $raw !== '' ) {
207 $matches = array( 'title' => rawurldecode( $raw ) );
208 if( $key ) {
209 $matches[$key] = $keyValue;
210 }
211 return $matches;
212 }
213 }
214 }
215 return array();
216 }
217
218 /**
219 * Recursively strips slashes from the given array;
220 * used for undoing the evil that is magic_quotes_gpc.
221 *
222 * @param $arr array: will be modified
223 * @return array the original array
224 */
225 private function &fix_magic_quotes( &$arr ) {
226 foreach( $arr as $key => $val ) {
227 if( is_array( $val ) ) {
228 $this->fix_magic_quotes( $arr[$key] );
229 } else {
230 $arr[$key] = stripslashes( $val );
231 }
232 }
233 return $arr;
234 }
235
236 /**
237 * If magic_quotes_gpc option is on, run the global arrays
238 * through fix_magic_quotes to strip out the stupid slashes.
239 * WARNING: This should only be done once! Running a second
240 * time could damage the values.
241 */
242 private function checkMagicQuotes() {
243 $mustFixQuotes = function_exists( 'get_magic_quotes_gpc' )
244 && get_magic_quotes_gpc();
245 if( $mustFixQuotes ) {
246 $this->fix_magic_quotes( $_COOKIE );
247 $this->fix_magic_quotes( $_ENV );
248 $this->fix_magic_quotes( $_GET );
249 $this->fix_magic_quotes( $_POST );
250 $this->fix_magic_quotes( $_REQUEST );
251 $this->fix_magic_quotes( $_SERVER );
252 }
253 }
254
255 /**
256 * Recursively normalizes UTF-8 strings in the given array.
257 *
258 * @param $data string or array
259 * @return cleaned-up version of the given
260 * @private
261 */
262 function normalizeUnicode( $data ) {
263 if( is_array( $data ) ) {
264 foreach( $data as $key => $val ) {
265 $data[$key] = $this->normalizeUnicode( $val );
266 }
267 } else {
268 global $wgContLang;
269 $data = isset( $wgContLang ) ? $wgContLang->normalize( $data ) : UtfNormal::cleanUp( $data );
270 }
271 return $data;
272 }
273
274 /**
275 * Fetch a value from the given array or return $default if it's not set.
276 *
277 * @param $arr Array
278 * @param $name String
279 * @param $default Mixed
280 * @return mixed
281 */
282 private function getGPCVal( $arr, $name, $default ) {
283 # PHP is so nice to not touch input data, except sometimes:
284 # http://us2.php.net/variables.external#language.variables.external.dot-in-names
285 # Work around PHP *feature* to avoid *bugs* elsewhere.
286 $name = strtr( $name, '.', '_' );
287 if( isset( $arr[$name] ) ) {
288 global $wgContLang;
289 $data = $arr[$name];
290 if( isset( $_GET[$name] ) && !is_array( $data ) ) {
291 # Check for alternate/legacy character encoding.
292 if( isset( $wgContLang ) ) {
293 $data = $wgContLang->checkTitleEncoding( $data );
294 }
295 }
296 $data = $this->normalizeUnicode( $data );
297 return $data;
298 } else {
299 taint( $default );
300 return $default;
301 }
302 }
303
304 /**
305 * Fetch a scalar from the input or return $default if it's not set.
306 * Returns a string. Arrays are discarded. Useful for
307 * non-freeform text inputs (e.g. predefined internal text keys
308 * selected by a drop-down menu). For freeform input, see getText().
309 *
310 * @param $name String
311 * @param $default String: optional default (or NULL)
312 * @return String
313 */
314 public function getVal( $name, $default = null ) {
315 $val = $this->getGPCVal( $this->data, $name, $default );
316 if( is_array( $val ) ) {
317 $val = $default;
318 }
319 if( is_null( $val ) ) {
320 return $val;
321 } else {
322 return (string)$val;
323 }
324 }
325
326 /**
327 * Set an arbitrary value into our get/post data.
328 *
329 * @param $key String: key name to use
330 * @param $value Mixed: value to set
331 * @return Mixed: old value if one was present, null otherwise
332 */
333 public function setVal( $key, $value ) {
334 $ret = isset( $this->data[$key] ) ? $this->data[$key] : null;
335 $this->data[$key] = $value;
336 return $ret;
337 }
338
339 /**
340 * Fetch an array from the input or return $default if it's not set.
341 * If source was scalar, will return an array with a single element.
342 * If no source and no default, returns NULL.
343 *
344 * @param $name String
345 * @param $default Array: optional default (or NULL)
346 * @return Array
347 */
348 public function getArray( $name, $default = null ) {
349 $val = $this->getGPCVal( $this->data, $name, $default );
350 if( is_null( $val ) ) {
351 return null;
352 } else {
353 return (array)$val;
354 }
355 }
356
357 /**
358 * Fetch an array of integers, or return $default if it's not set.
359 * If source was scalar, will return an array with a single element.
360 * If no source and no default, returns NULL.
361 * If an array is returned, contents are guaranteed to be integers.
362 *
363 * @param $name String
364 * @param $default Array: option default (or NULL)
365 * @return Array of ints
366 */
367 public function getIntArray( $name, $default = null ) {
368 $val = $this->getArray( $name, $default );
369 if( is_array( $val ) ) {
370 $val = array_map( 'intval', $val );
371 }
372 return $val;
373 }
374
375 /**
376 * Fetch an integer value from the input or return $default if not set.
377 * Guaranteed to return an integer; non-numeric input will typically
378 * return 0.
379 *
380 * @param $name String
381 * @param $default Integer
382 * @return Integer
383 */
384 public function getInt( $name, $default = 0 ) {
385 return intval( $this->getVal( $name, $default ) );
386 }
387
388 /**
389 * Fetch an integer value from the input or return null if empty.
390 * Guaranteed to return an integer or null; non-numeric input will
391 * typically return null.
392 *
393 * @param $name String
394 * @return Integer
395 */
396 public function getIntOrNull( $name ) {
397 $val = $this->getVal( $name );
398 return is_numeric( $val )
399 ? intval( $val )
400 : null;
401 }
402
403 /**
404 * Fetch a boolean value from the input or return $default if not set.
405 * Guaranteed to return true or false, with normal PHP semantics for
406 * boolean interpretation of strings.
407 *
408 * @param $name String
409 * @param $default Boolean
410 * @return Boolean
411 */
412 public function getBool( $name, $default = false ) {
413 return (bool)$this->getVal( $name, $default );
414 }
415
416 /**
417 * Fetch a boolean value from the input or return $default if not set.
418 * Unlike getBool, the string "false" will result in boolean false, which is
419 * useful when interpreting information sent from JavaScript.
420 *
421 * @param $name String
422 * @param $default Boolean
423 * @return Boolean
424 */
425 public function getFuzzyBool( $name, $default = false ) {
426 return $this->getBool( $name, $default ) && strcasecmp( $this->getVal( $name ), 'false' ) !== 0;
427 }
428
429 /**
430 * Return true if the named value is set in the input, whatever that
431 * value is (even "0"). Return false if the named value is not set.
432 * Example use is checking for the presence of check boxes in forms.
433 *
434 * @param $name String
435 * @return Boolean
436 */
437 public function getCheck( $name ) {
438 # Checkboxes and buttons are only present when clicked
439 # Presence connotes truth, abscense false
440 $val = $this->getVal( $name, null );
441 return isset( $val );
442 }
443
444 /**
445 * Fetch a text string from the given array or return $default if it's not
446 * set. Carriage returns are stripped from the text, and with some language
447 * modules there is an input transliteration applied. This should generally
448 * be used for form <textarea> and <input> fields. Used for user-supplied
449 * freeform text input (for which input transformations may be required - e.g.
450 * Esperanto x-coding).
451 *
452 * @param $name String
453 * @param $default String: optional
454 * @return String
455 */
456 public function getText( $name, $default = '' ) {
457 global $wgContLang;
458 $val = $this->getVal( $name, $default );
459 return str_replace( "\r\n", "\n",
460 $wgContLang->recodeInput( $val ) );
461 }
462
463 /**
464 * Extracts the given named values into an array.
465 * If no arguments are given, returns all input values.
466 * No transformation is performed on the values.
467 */
468 public function getValues() {
469 $names = func_get_args();
470 if ( count( $names ) == 0 ) {
471 $names = array_keys( $this->data );
472 }
473
474 $retVal = array();
475 foreach ( $names as $name ) {
476 $value = $this->getVal( $name );
477 if ( !is_null( $value ) ) {
478 $retVal[$name] = $value;
479 }
480 }
481 return $retVal;
482 }
483
484 /**
485 * Get the values passed in the query string.
486 * No transformation is performed on the values.
487 *
488 * @return Array
489 */
490 public function getQueryValues() {
491 return $_GET;
492 }
493
494 /**
495 * Returns true if the present request was reached by a POST operation,
496 * false otherwise (GET, HEAD, or command-line).
497 *
498 * Note that values retrieved by the object may come from the
499 * GET URL etc even on a POST request.
500 *
501 * @return Boolean
502 */
503 public function wasPosted() {
504 return $_SERVER['REQUEST_METHOD'] == 'POST';
505 }
506
507 /**
508 * Returns true if there is a session cookie set.
509 * This does not necessarily mean that the user is logged in!
510 *
511 * If you want to check for an open session, use session_id()
512 * instead; that will also tell you if the session was opened
513 * during the current request (in which case the cookie will
514 * be sent back to the client at the end of the script run).
515 *
516 * @return Boolean
517 */
518 public function checkSessionCookie() {
519 return isset( $_COOKIE[ session_name() ] );
520 }
521
522 /**
523 * Get a cookie from the $_COOKIE jar
524 *
525 * @param $key String: the name of the cookie
526 * @param $prefix String: a prefix to use for the cookie name, if not $wgCookiePrefix
527 * @param $default Mixed: what to return if the value isn't found
528 * @return Mixed: cookie value or $default if the cookie not set
529 */
530 public function getCookie( $key, $prefix = null, $default = null ) {
531 if( $prefix === null ) {
532 global $wgCookiePrefix;
533 $prefix = $wgCookiePrefix;
534 }
535 return $this->getGPCVal( $_COOKIE, $prefix . $key , $default );
536 }
537
538 /**
539 * Return the path and query string portion of the request URI.
540 * This will be suitable for use as a relative link in HTML output.
541 *
542 * @return String
543 */
544 public function getRequestURL() {
545 if( isset( $_SERVER['REQUEST_URI'] ) && strlen( $_SERVER['REQUEST_URI'] ) ) {
546 $base = $_SERVER['REQUEST_URI'];
547 } elseif ( isset( $_SERVER['HTTP_X_ORIGINAL_URL'] ) && strlen( $_SERVER['HTTP_X_ORIGINAL_URL'] ) ) {
548 // Probably IIS; doesn't set REQUEST_URI
549 $base = $_SERVER['HTTP_X_ORIGINAL_URL'];
550 } elseif( isset( $_SERVER['SCRIPT_NAME'] ) ) {
551 $base = $_SERVER['SCRIPT_NAME'];
552 if( isset( $_SERVER['QUERY_STRING'] ) && $_SERVER['QUERY_STRING'] != '' ) {
553 $base .= '?' . $_SERVER['QUERY_STRING'];
554 }
555 } else {
556 // This shouldn't happen!
557 throw new MWException( "Web server doesn't provide either " .
558 "REQUEST_URI, HTTP_X_ORIGINAL_URL or SCRIPT_NAME. Report details " .
559 "of your web server configuration to http://bugzilla.wikimedia.org/" );
560 }
561 // User-agents should not send a fragment with the URI, but
562 // if they do, and the web server passes it on to us, we
563 // need to strip it or we get false-positive redirect loops
564 // or weird output URLs
565 $hash = strpos( $base, '#' );
566 if( $hash !== false ) {
567 $base = substr( $base, 0, $hash );
568 }
569 if( $base[0] == '/' ) {
570 return $base;
571 } else {
572 // We may get paths with a host prepended; strip it.
573 return preg_replace( '!^[^:]+://[^/]+/!', '/', $base );
574 }
575 }
576
577 /**
578 * Return the request URI with the canonical service and hostname, path,
579 * and query string. This will be suitable for use as an absolute link
580 * in HTML or other output.
581 *
582 * @return String
583 */
584 public function getFullRequestURL() {
585 global $wgServer;
586 return $wgServer . $this->getRequestURL();
587 }
588
589 /**
590 * Take an arbitrary query and rewrite the present URL to include it
591 * @param $query String: query string fragment; do not include initial '?'
592 *
593 * @return String
594 */
595 public function appendQuery( $query ) {
596 return $this->appendQueryArray( wfCgiToArray( $query ) );
597 }
598
599 /**
600 * HTML-safe version of appendQuery().
601 *
602 * @param $query String: query string fragment; do not include initial '?'
603 * @return String
604 */
605 public function escapeAppendQuery( $query ) {
606 return htmlspecialchars( $this->appendQuery( $query ) );
607 }
608
609 public function appendQueryValue( $key, $value, $onlyquery = false ) {
610 return $this->appendQueryArray( array( $key => $value ), $onlyquery );
611 }
612
613 /**
614 * Appends or replaces value of query variables.
615 *
616 * @param $array Array of values to replace/add to query
617 * @param $onlyquery Bool: whether to only return the query string and not
618 * the complete URL
619 * @return String
620 */
621 public function appendQueryArray( $array, $onlyquery = false ) {
622 global $wgTitle;
623 $newquery = $this->getQueryValues();
624 unset( $newquery['title'] );
625 $newquery = array_merge( $newquery, $array );
626 $query = wfArrayToCGI( $newquery );
627 return $onlyquery ? $query : $wgTitle->getLocalURL( $query );
628 }
629
630 /**
631 * Check for limit and offset parameters on the input, and return sensible
632 * defaults if not given. The limit must be positive and is capped at 5000.
633 * Offset must be positive but is not capped.
634 *
635 * @param $deflimit Integer: limit to use if no input and the user hasn't set the option.
636 * @param $optionname String: to specify an option other than rclimit to pull from.
637 * @return array first element is limit, second is offset
638 */
639 public function getLimitOffset( $deflimit = 50, $optionname = 'rclimit' ) {
640 global $wgUser;
641
642 $limit = $this->getInt( 'limit', 0 );
643 if( $limit < 0 ) {
644 $limit = 0;
645 }
646 if( ( $limit == 0 ) && ( $optionname != '' ) ) {
647 $limit = (int)$wgUser->getOption( $optionname );
648 }
649 if( $limit <= 0 ) {
650 $limit = $deflimit;
651 }
652 if( $limit > 5000 ) {
653 $limit = 5000; # We have *some* limits...
654 }
655
656 $offset = $this->getInt( 'offset', 0 );
657 if( $offset < 0 ) {
658 $offset = 0;
659 }
660
661 return array( $limit, $offset );
662 }
663
664 /**
665 * Return the path to the temporary file where PHP has stored the upload.
666 *
667 * @param $key String:
668 * @return string or NULL if no such file.
669 */
670 public function getFileTempname( $key ) {
671 $file = new WebRequestUpload( $this, $key );
672 return $file->getTempName();
673 }
674
675 /**
676 * Return the size of the upload, or 0.
677 *
678 * @deprecated since 1.17
679 * @param $key String:
680 * @return integer
681 */
682 public function getFileSize( $key ) {
683 $file = new WebRequestUpload( $this, $key );
684 return $file->getSize();
685 }
686
687 /**
688 * Return the upload error or 0
689 *
690 * @param $key String:
691 * @return integer
692 */
693 public function getUploadError( $key ) {
694 $file = new WebRequestUpload( $this, $key );
695 return $file->getError();
696 }
697
698 /**
699 * Return the original filename of the uploaded file, as reported by
700 * the submitting user agent. HTML-style character entities are
701 * interpreted and normalized to Unicode normalization form C, in part
702 * to deal with weird input from Safari with non-ASCII filenames.
703 *
704 * Other than this the name is not verified for being a safe filename.
705 *
706 * @param $key String:
707 * @return string or NULL if no such file.
708 */
709 public function getFileName( $key ) {
710 $file = new WebRequestUpload( $this, $key );
711 return $file->getName();
712 }
713
714 /**
715 * Return a WebRequestUpload object corresponding to the key
716 *
717 * @param @key string
718 * @return WebRequestUpload
719 */
720 public function getUpload( $key ) {
721 return new WebRequestUpload( $this, $key );
722 }
723
724 /**
725 * Return a handle to WebResponse style object, for setting cookies,
726 * headers and other stuff, for Request being worked on.
727 *
728 * @return WebResponse
729 */
730 public function response() {
731 /* Lazy initialization of response object for this request */
732 if ( !is_object( $this->response ) ) {
733 $class = ( $this instanceof FauxRequest ) ? 'FauxResponse' : 'WebResponse';
734 $this->response = new $class();
735 }
736 return $this->response;
737 }
738
739 /**
740 * Initialise the header list
741 */
742 private function initHeaders() {
743 if ( count( $this->headers ) ) {
744 return;
745 }
746
747 if ( function_exists( 'apache_request_headers' ) ) {
748 foreach ( apache_request_headers() as $tempName => $tempValue ) {
749 $this->headers[ strtoupper( $tempName ) ] = $tempValue;
750 }
751 } else {
752 $headers = $_SERVER;
753 foreach ( $_SERVER as $name => $value ) {
754 if ( substr( $name, 0, 5 ) === 'HTTP_' ) {
755 $name = str_replace( '_', '-', substr( $name, 5 ) );
756 $this->headers[$name] = $value;
757 } elseif ( $name === 'CONTENT_LENGTH' ) {
758 $this->headers['CONTENT-LENGTH'] = $value;
759 }
760 }
761 }
762 }
763
764 /**
765 * Get an array containing all request headers
766 *
767 * @return Array mapping header name to its value
768 */
769 public function getAllHeaders() {
770 $this->initHeaders();
771 return $this->headers;
772 }
773
774 /**
775 * Get a request header, or false if it isn't set
776 * @param $name String: case-insensitive header name
777 */
778 public function getHeader( $name ) {
779 $this->initHeaders();
780 $name = strtoupper( $name );
781 if ( isset( $this->headers[$name] ) ) {
782 return $this->headers[$name];
783 } else {
784 return false;
785 }
786 }
787
788 /**
789 * Get data from $_SESSION
790 *
791 * @param $key String: name of key in $_SESSION
792 * @return Mixed
793 */
794 public function getSessionData( $key ) {
795 if( !isset( $_SESSION[$key] ) ) {
796 return null;
797 }
798 return $_SESSION[$key];
799 }
800
801 /**
802 * Set session data
803 *
804 * @param $key String: name of key in $_SESSION
805 * @param $data Mixed
806 */
807 public function setSessionData( $key, $data ) {
808 $_SESSION[$key] = $data;
809 }
810
811 /**
812 * Check if Internet Explorer will detect an incorrect cache extension in
813 * PATH_INFO or QUERY_STRING. If the request can't be allowed, show an error
814 * message or redirect to a safer URL. Returns true if the URL is OK, and
815 * false if an error message has been shown and the request should be aborted.
816 */
817 public function checkUrlExtension( $extWhitelist = array() ) {
818 global $wgScriptExtension;
819 $extWhitelist[] = ltrim( $wgScriptExtension, '.' );
820 if ( IEUrlExtension::areServerVarsBad( $_SERVER, $extWhitelist ) ) {
821 if ( !$this->wasPosted() ) {
822 $newUrl = IEUrlExtension::fixUrlForIE6(
823 $this->getFullRequestURL(), $extWhitelist );
824 if ( $newUrl !== false ) {
825 $this->doSecurityRedirect( $newUrl );
826 return false;
827 }
828 }
829 wfHttpError( 403, 'Forbidden',
830 'Invalid file extension found in the path info or query string.' );
831
832 return false;
833 }
834 return true;
835 }
836
837 /**
838 * Attempt to redirect to a URL with a QUERY_STRING that's not dangerous in
839 * IE 6. Returns true if it was successful, false otherwise.
840 */
841 protected function doSecurityRedirect( $url ) {
842 header( 'Location: ' . $url );
843 header( 'Content-Type: text/html' );
844 $encUrl = htmlspecialchars( $url );
845 echo <<<HTML
846 <html>
847 <head>
848 <title>Security redirect</title>
849 </head>
850 <body>
851 <h1>Security redirect</h1>
852 <p>
853 We can't serve non-HTML content from the URL you have requested, because
854 Internet Explorer would interpret it as an incorrect and potentially dangerous
855 content type.</p>
856 <p>Instead, please use <a href="$encUrl">this URL</a>, which is the same as the URL you have requested, except that
857 "&amp;*" is appended. This prevents Internet Explorer from seeing a bogus file
858 extension.
859 </p>
860 </body>
861 </html>
862 HTML;
863 echo "\n";
864 return true;
865 }
866
867 /**
868 * Returns true if the PATH_INFO ends with an extension other than a script
869 * extension. This could confuse IE for scripts that send arbitrary data which
870 * is not HTML but may be detected as such.
871 *
872 * Various past attempts to use the URL to make this check have generally
873 * run up against the fact that CGI does not provide a standard method to
874 * determine the URL. PATH_INFO may be mangled (e.g. if cgi.fix_pathinfo=0),
875 * but only by prefixing it with the script name and maybe some other stuff,
876 * the extension is not mangled. So this should be a reasonably portable
877 * way to perform this security check.
878 *
879 * Also checks for anything that looks like a file extension at the end of
880 * QUERY_STRING, since IE 6 and earlier will use this to get the file type
881 * if there was no dot before the question mark (bug 28235).
882 *
883 * @deprecated Use checkUrlExtension().
884 */
885 public function isPathInfoBad( $extWhitelist = array() ) {
886 global $wgScriptExtension;
887 $extWhitelist[] = ltrim( $wgScriptExtension, '.' );
888 return IEUrlExtension::areServerVarsBad( $_SERVER, $extWhitelist );
889 }
890
891 /**
892 * Parse the Accept-Language header sent by the client into an array
893 * @return array( languageCode => q-value ) sorted by q-value in descending order
894 * May contain the "language" '*', which applies to languages other than those explicitly listed.
895 * This is aligned with rfc2616 section 14.4
896 */
897 public function getAcceptLang() {
898 // Modified version of code found at http://www.thefutureoftheweb.com/blog/use-accept-language-header
899 $acceptLang = $this->getHeader( 'Accept-Language' );
900 if ( !$acceptLang ) {
901 return array();
902 }
903
904 // Return the language codes in lower case
905 $acceptLang = strtolower( $acceptLang );
906
907 // Break up string into pieces (languages and q factors)
908 $lang_parse = null;
909 preg_match_all( '/([a-z]{1,8}(-[a-z]{1,8})?|\*)\s*(;\s*q\s*=\s*(1|0(\.[0-9]+)?)?)?/',
910 $acceptLang, $lang_parse );
911
912 if ( !count( $lang_parse[1] ) ) {
913 return array();
914 }
915
916 // Create a list like "en" => 0.8
917 $langs = array_combine( $lang_parse[1], $lang_parse[4] );
918 // Set default q factor to 1
919 foreach ( $langs as $lang => $val ) {
920 if ( $val === '' ) {
921 $langs[$lang] = 1;
922 } elseif ( $val == 0 ) {
923 unset($langs[$lang]);
924 }
925 }
926
927 // Sort list
928 arsort( $langs, SORT_NUMERIC );
929 return $langs;
930 }
931 }
932
933 /**
934 * Object to access the $_FILES array
935 */
936 class WebRequestUpload {
937 protected $request;
938 protected $doesExist;
939 protected $fileInfo;
940
941 /**
942 * Constructor. Should only be called by WebRequest
943 *
944 * @param $request WebRequest The associated request
945 * @param $key string Key in $_FILES array (name of form field)
946 */
947 public function __construct( $request, $key ) {
948 $this->request = $request;
949 $this->doesExist = isset( $_FILES[$key] );
950 if ( $this->doesExist ) {
951 $this->fileInfo = $_FILES[$key];
952 }
953 }
954
955 /**
956 * Return whether a file with this name was uploaded.
957 *
958 * @return bool
959 */
960 public function exists() {
961 return $this->doesExist;
962 }
963
964 /**
965 * Return the original filename of the uploaded file
966 *
967 * @return mixed Filename or null if non-existent
968 */
969 public function getName() {
970 if ( !$this->exists() ) {
971 return null;
972 }
973
974 global $wgContLang;
975 $name = $this->fileInfo['name'];
976
977 # Safari sends filenames in HTML-encoded Unicode form D...
978 # Horrid and evil! Let's try to make some kind of sense of it.
979 $name = Sanitizer::decodeCharReferences( $name );
980 $name = $wgContLang->normalize( $name );
981 wfDebug( __METHOD__ . ": {$this->fileInfo['name']} normalized to '$name'\n" );
982 return $name;
983 }
984
985 /**
986 * Return the file size of the uploaded file
987 *
988 * @return int File size or zero if non-existent
989 */
990 public function getSize() {
991 if ( !$this->exists() ) {
992 return 0;
993 }
994
995 return $this->fileInfo['size'];
996 }
997
998 /**
999 * Return the path to the temporary file
1000 *
1001 * @return mixed Path or null if non-existent
1002 */
1003 public function getTempName() {
1004 if ( !$this->exists() ) {
1005 return null;
1006 }
1007
1008 return $this->fileInfo['tmp_name'];
1009 }
1010
1011 /**
1012 * Return the upload error. See link for explanation
1013 * http://www.php.net/manual/en/features.file-upload.errors.php
1014 *
1015 * @return int One of the UPLOAD_ constants, 0 if non-existent
1016 */
1017 public function getError() {
1018 if ( !$this->exists() ) {
1019 return 0; # UPLOAD_ERR_OK
1020 }
1021
1022 return $this->fileInfo['error'];
1023 }
1024
1025 /**
1026 * Returns whether this upload failed because of overflow of a maximum set
1027 * in php.ini
1028 *
1029 * @return bool
1030 */
1031 public function isIniSizeOverflow() {
1032 if ( $this->getError() == UPLOAD_ERR_INI_SIZE ) {
1033 # PHP indicated that upload_max_filesize is exceeded
1034 return true;
1035 }
1036
1037 $contentLength = $this->request->getHeader( 'CONTENT_LENGTH' );
1038 if ( $contentLength > wfShorthandToInteger( ini_get( 'post_max_size' ) ) ) {
1039 # post_max_size is exceeded
1040 return true;
1041 }
1042
1043 return false;
1044 }
1045 }
1046
1047 /**
1048 * WebRequest clone which takes values from a provided array.
1049 *
1050 * @ingroup HTTP
1051 */
1052 class FauxRequest extends WebRequest {
1053 private $wasPosted = false;
1054 private $session = array();
1055
1056 /**
1057 * @param $data Array of *non*-urlencoded key => value pairs, the
1058 * fake GET/POST values
1059 * @param $wasPosted Bool: whether to treat the data as POST
1060 * @param $session Mixed: session array or null
1061 */
1062 public function __construct( $data, $wasPosted = false, $session = null ) {
1063 if( is_array( $data ) ) {
1064 $this->data = $data;
1065 } else {
1066 throw new MWException( "FauxRequest() got bogus data" );
1067 }
1068 $this->wasPosted = $wasPosted;
1069 if( $session )
1070 $this->session = $session;
1071 }
1072
1073 private function notImplemented( $method ) {
1074 throw new MWException( "{$method}() not implemented" );
1075 }
1076
1077 public function getText( $name, $default = '' ) {
1078 # Override; don't recode since we're using internal data
1079 return (string)$this->getVal( $name, $default );
1080 }
1081
1082 public function getValues() {
1083 return $this->data;
1084 }
1085
1086 public function getQueryValues() {
1087 if ( $this->wasPosted ) {
1088 return array();
1089 } else {
1090 return $this->data;
1091 }
1092 }
1093
1094 public function wasPosted() {
1095 return $this->wasPosted;
1096 }
1097
1098 public function checkSessionCookie() {
1099 return false;
1100 }
1101
1102 public function getRequestURL() {
1103 $this->notImplemented( __METHOD__ );
1104 }
1105
1106 public function getHeader( $name ) {
1107 return isset( $this->headers[$name] ) ? $this->headers[$name] : false;
1108 }
1109
1110 public function setHeader( $name, $val ) {
1111 $this->headers[$name] = $val;
1112 }
1113
1114 public function getSessionData( $key ) {
1115 if( isset( $this->session[$key] ) )
1116 return $this->session[$key];
1117 }
1118
1119 public function setSessionData( $key, $data ) {
1120 $this->session[$key] = $data;
1121 }
1122
1123 public function getSessionArray() {
1124 return $this->session;
1125 }
1126
1127 public function isPathInfoBad( $extWhitelist = array() ) {
1128 return false;
1129 }
1130
1131 public function checkUrlExtension( $extWhitelist = array() ) {
1132 return true;
1133 }
1134 }