nice debug msg for Title::getRestrictionTypes
[mediawiki.git] / includes / WebRequest.php
blob9f6d277f8dad157acfc23951b09ac53eb46c00a8
1 <?php
2 /**
3 * Deal with importing all those nasssty globals and things
5 * Copyright © 2003 Brion Vibber <brion@pobox.com>
6 * http://www.mediawiki.org/
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.
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.
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
23 * @file
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.
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.
36 * @ingroup HTTP
38 class WebRequest {
39 protected $data, $headers = array();
41 /**
42 * Lazy-init response object
43 * @var WebResponse
45 private $response;
47 /**
48 * Cached client IP address
49 * @var String
51 private $ip;
53 public function __construct() {
54 /// @todo FIXME: This preemptive de-quoting can interfere with other web libraries
55 /// and increases our memory footprint. It would be cleaner to do on
56 /// demand; but currently we have no wrapper for $_SERVER etc.
57 $this->checkMagicQuotes();
59 // POST overrides GET data
60 // We don't use $_REQUEST here to avoid interference from cookies...
61 $this->data = $_POST + $_GET;
64 /**
65 * Extract the PATH_INFO variable even when it isn't a reasonable
66 * value. On some large webhosts, PATH_INFO includes the script
67 * path as well as everything after it.
69 * @param $want string: If this is not 'all', then the function
70 * will return an empty array if it determines that the URL is
71 * inside a rewrite path.
73 * @return Array: 'title' key is the title of the article.
75 static public function getPathInfo( $want = 'all' ) {
76 // PATH_INFO is mangled due to http://bugs.php.net/bug.php?id=31892
77 // And also by Apache 2.x, double slashes are converted to single slashes.
78 // So we will use REQUEST_URI if possible.
79 $matches = array();
80 if ( !empty( $_SERVER['REQUEST_URI'] ) ) {
81 // Slurp out the path portion to examine...
82 $url = $_SERVER['REQUEST_URI'];
83 if ( !preg_match( '!^https?://!', $url ) ) {
84 $url = 'http://unused' . $url;
86 $a = parse_url( $url );
87 if( $a ) {
88 $path = isset( $a['path'] ) ? $a['path'] : '';
90 global $wgScript;
91 if( $path == $wgScript && $want !== 'all' ) {
92 // Script inside a rewrite path?
93 // Abort to keep from breaking...
94 return $matches;
96 // Raw PATH_INFO style
97 $matches = self::extractTitle( $path, "$wgScript/$1" );
99 if( !$matches
100 && isset( $_SERVER['SCRIPT_NAME'] )
101 && preg_match( '/\.php5?/', $_SERVER['SCRIPT_NAME'] ) )
103 # Check for SCRIPT_NAME, we handle index.php explicitly
104 # But we do have some other .php files such as img_auth.php
105 # Don't let root article paths clober the parsing for them
106 $matches = self::extractTitle( $path, $_SERVER['SCRIPT_NAME'] . "/$1" );
109 global $wgArticlePath;
110 if( !$matches && $wgArticlePath ) {
111 $matches = self::extractTitle( $path, $wgArticlePath );
114 global $wgActionPaths;
115 if( !$matches && $wgActionPaths ) {
116 $matches = self::extractTitle( $path, $wgActionPaths, 'action' );
119 global $wgVariantArticlePath, $wgContLang;
120 if( !$matches && $wgVariantArticlePath ) {
121 $variantPaths = array();
122 foreach( $wgContLang->getVariants() as $variant ) {
123 $variantPaths[$variant] =
124 str_replace( '$2', $variant, $wgVariantArticlePath );
126 $matches = self::extractTitle( $path, $variantPaths, 'variant' );
129 wfRunHooks( 'WebRequestGetPathInfoRequestURI', array( $path, &$matches ) );
131 } elseif ( isset( $_SERVER['ORIG_PATH_INFO'] ) && $_SERVER['ORIG_PATH_INFO'] != '' ) {
132 // Mangled PATH_INFO
133 // http://bugs.php.net/bug.php?id=31892
134 // Also reported when ini_get('cgi.fix_pathinfo')==false
135 $matches['title'] = substr( $_SERVER['ORIG_PATH_INFO'], 1 );
137 } elseif ( isset( $_SERVER['PATH_INFO'] ) && ($_SERVER['PATH_INFO'] != '') ) {
138 // Regular old PATH_INFO yay
139 $matches['title'] = substr( $_SERVER['PATH_INFO'], 1 );
142 return $matches;
146 * Work out an appropriate URL prefix containing scheme and host, based on
147 * information detected from $_SERVER
149 * @return string
151 public static function detectServer() {
152 list( $proto, $stdPort ) = self::detectProtocolAndStdPort();
154 $varNames = array( 'HTTP_HOST', 'SERVER_NAME', 'HOSTNAME', 'SERVER_ADDR' );
155 $host = 'localhost';
156 $port = $stdPort;
157 foreach ( $varNames as $varName ) {
158 if ( !isset( $_SERVER[$varName] ) ) {
159 continue;
161 $parts = IP::splitHostAndPort( $_SERVER[$varName] );
162 if ( !$parts ) {
163 // Invalid, do not use
164 continue;
166 $host = $parts[0];
167 if ( $parts[1] === false ) {
168 if ( isset( $_SERVER['SERVER_PORT'] ) ) {
169 $port = $_SERVER['SERVER_PORT'];
170 } // else leave it as $stdPort
171 } else {
172 $port = $parts[1];
174 break;
177 return $proto . '://' . IP::combineHostAndPort( $host, $port, $stdPort );
181 * @return array
183 public static function detectProtocolAndStdPort() {
184 return ( isset( $_SERVER['HTTPS'] ) && $_SERVER['HTTPS'] == 'on' ) ? array( 'https', 443 ) : array( 'http', 80 );
188 * @return string
190 public static function detectProtocol() {
191 list( $proto, $stdPort ) = self::detectProtocolAndStdPort();
192 return $proto;
196 * Check for title, action, and/or variant data in the URL
197 * and interpolate it into the GET variables.
198 * This should only be run after $wgContLang is available,
199 * as we may need the list of language variants to determine
200 * available variant URLs.
202 public function interpolateTitle() {
203 global $wgUsePathInfo;
205 // bug 16019: title interpolation on API queries is useless and sometimes harmful
206 if ( defined( 'MW_API' ) ) {
207 return;
210 if ( $wgUsePathInfo ) {
211 $matches = self::getPathInfo( 'title' );
212 foreach( $matches as $key => $val) {
213 $this->data[$key] = $_GET[$key] = $_REQUEST[$key] = $val;
219 * URL rewriting function; tries to extract page title and,
220 * optionally, one other fixed parameter value from a URL path.
222 * @param $path string: the URL path given from the client
223 * @param $bases array: one or more URLs, optionally with $1 at the end
224 * @param $key string: if provided, the matching key in $bases will be
225 * passed on as the value of this URL parameter
226 * @return array of URL variables to interpolate; empty if no match
228 static function extractTitle( $path, $bases, $key = false ) {
229 foreach( (array)$bases as $keyValue => $base ) {
230 // Find the part after $wgArticlePath
231 $base = str_replace( '$1', '', $base );
232 $baseLen = strlen( $base );
233 if( substr( $path, 0, $baseLen ) == $base ) {
234 $raw = substr( $path, $baseLen );
235 if( $raw !== '' ) {
236 $matches = array( 'title' => rawurldecode( $raw ) );
237 if( $key ) {
238 $matches[$key] = $keyValue;
240 return $matches;
244 return array();
248 * Recursively strips slashes from the given array;
249 * used for undoing the evil that is magic_quotes_gpc.
251 * @param $arr array: will be modified
252 * @param $topLevel bool Specifies if the array passed is from the top
253 * level of the source. In PHP5 magic_quotes only escapes the first level
254 * of keys that belong to an array.
255 * @return array the original array
256 * @see http://www.php.net/manual/en/function.get-magic-quotes-gpc.php#49612
258 private function &fix_magic_quotes( &$arr, $topLevel = true ) {
259 $clean = array();
260 foreach( $arr as $key => $val ) {
261 if( is_array( $val ) ) {
262 $cleanKey = $topLevel ? stripslashes( $key ) : $key;
263 $clean[$cleanKey] = $this->fix_magic_quotes( $arr[$key], false );
264 } else {
265 $cleanKey = stripslashes( $key );
266 $clean[$cleanKey] = stripslashes( $val );
269 $arr = $clean;
270 return $arr;
274 * If magic_quotes_gpc option is on, run the global arrays
275 * through fix_magic_quotes to strip out the stupid slashes.
276 * WARNING: This should only be done once! Running a second
277 * time could damage the values.
279 private function checkMagicQuotes() {
280 $mustFixQuotes = function_exists( 'get_magic_quotes_gpc' )
281 && get_magic_quotes_gpc();
282 if( $mustFixQuotes ) {
283 $this->fix_magic_quotes( $_COOKIE );
284 $this->fix_magic_quotes( $_ENV );
285 $this->fix_magic_quotes( $_GET );
286 $this->fix_magic_quotes( $_POST );
287 $this->fix_magic_quotes( $_REQUEST );
288 $this->fix_magic_quotes( $_SERVER );
293 * Recursively normalizes UTF-8 strings in the given array.
295 * @param $data string or array
296 * @return cleaned-up version of the given
297 * @private
299 function normalizeUnicode( $data ) {
300 if( is_array( $data ) ) {
301 foreach( $data as $key => $val ) {
302 $data[$key] = $this->normalizeUnicode( $val );
304 } else {
305 global $wgContLang;
306 $data = isset( $wgContLang ) ? $wgContLang->normalize( $data ) : UtfNormal::cleanUp( $data );
308 return $data;
312 * Fetch a value from the given array or return $default if it's not set.
314 * @param $arr Array
315 * @param $name String
316 * @param $default Mixed
317 * @return mixed
319 private function getGPCVal( $arr, $name, $default ) {
320 # PHP is so nice to not touch input data, except sometimes:
321 # http://us2.php.net/variables.external#language.variables.external.dot-in-names
322 # Work around PHP *feature* to avoid *bugs* elsewhere.
323 $name = strtr( $name, '.', '_' );
324 if( isset( $arr[$name] ) ) {
325 global $wgContLang;
326 $data = $arr[$name];
327 if( isset( $_GET[$name] ) && !is_array( $data ) ) {
328 # Check for alternate/legacy character encoding.
329 if( isset( $wgContLang ) ) {
330 $data = $wgContLang->checkTitleEncoding( $data );
333 $data = $this->normalizeUnicode( $data );
334 return $data;
335 } else {
336 taint( $default );
337 return $default;
342 * Fetch a scalar from the input or return $default if it's not set.
343 * Returns a string. Arrays are discarded. Useful for
344 * non-freeform text inputs (e.g. predefined internal text keys
345 * selected by a drop-down menu). For freeform input, see getText().
347 * @param $name String
348 * @param $default String: optional default (or NULL)
349 * @return String
351 public function getVal( $name, $default = null ) {
352 $val = $this->getGPCVal( $this->data, $name, $default );
353 if( is_array( $val ) ) {
354 $val = $default;
356 if( is_null( $val ) ) {
357 return $val;
358 } else {
359 return (string)$val;
364 * Set an arbitrary value into our get/post data.
366 * @param $key String: key name to use
367 * @param $value Mixed: value to set
368 * @return Mixed: old value if one was present, null otherwise
370 public function setVal( $key, $value ) {
371 $ret = isset( $this->data[$key] ) ? $this->data[$key] : null;
372 $this->data[$key] = $value;
373 return $ret;
377 * Fetch an array from the input or return $default if it's not set.
378 * If source was scalar, will return an array with a single element.
379 * If no source and no default, returns NULL.
381 * @param $name String
382 * @param $default Array: optional default (or NULL)
383 * @return Array
385 public function getArray( $name, $default = null ) {
386 $val = $this->getGPCVal( $this->data, $name, $default );
387 if( is_null( $val ) ) {
388 return null;
389 } else {
390 return (array)$val;
395 * Fetch an array of integers, or return $default if it's not set.
396 * If source was scalar, will return an array with a single element.
397 * If no source and no default, returns NULL.
398 * If an array is returned, contents are guaranteed to be integers.
400 * @param $name String
401 * @param $default Array: option default (or NULL)
402 * @return Array of ints
404 public function getIntArray( $name, $default = null ) {
405 $val = $this->getArray( $name, $default );
406 if( is_array( $val ) ) {
407 $val = array_map( 'intval', $val );
409 return $val;
413 * Fetch an integer value from the input or return $default if not set.
414 * Guaranteed to return an integer; non-numeric input will typically
415 * return 0.
417 * @param $name String
418 * @param $default Integer
419 * @return Integer
421 public function getInt( $name, $default = 0 ) {
422 return intval( $this->getVal( $name, $default ) );
426 * Fetch an integer value from the input or return null if empty.
427 * Guaranteed to return an integer or null; non-numeric input will
428 * typically return null.
430 * @param $name String
431 * @return Integer
433 public function getIntOrNull( $name ) {
434 $val = $this->getVal( $name );
435 return is_numeric( $val )
436 ? intval( $val )
437 : null;
441 * Fetch a boolean value from the input or return $default if not set.
442 * Guaranteed to return true or false, with normal PHP semantics for
443 * boolean interpretation of strings.
445 * @param $name String
446 * @param $default Boolean
447 * @return Boolean
449 public function getBool( $name, $default = false ) {
450 return (bool)$this->getVal( $name, $default );
454 * Fetch a boolean value from the input or return $default if not set.
455 * Unlike getBool, the string "false" will result in boolean false, which is
456 * useful when interpreting information sent from JavaScript.
458 * @param $name String
459 * @param $default Boolean
460 * @return Boolean
462 public function getFuzzyBool( $name, $default = false ) {
463 return $this->getBool( $name, $default ) && strcasecmp( $this->getVal( $name ), 'false' ) !== 0;
467 * Return true if the named value is set in the input, whatever that
468 * value is (even "0"). Return false if the named value is not set.
469 * Example use is checking for the presence of check boxes in forms.
471 * @param $name String
472 * @return Boolean
474 public function getCheck( $name ) {
475 # Checkboxes and buttons are only present when clicked
476 # Presence connotes truth, abscense false
477 $val = $this->getVal( $name, null );
478 return isset( $val );
482 * Fetch a text string from the given array or return $default if it's not
483 * set. Carriage returns are stripped from the text, and with some language
484 * modules there is an input transliteration applied. This should generally
485 * be used for form <textarea> and <input> fields. Used for user-supplied
486 * freeform text input (for which input transformations may be required - e.g.
487 * Esperanto x-coding).
489 * @param $name String
490 * @param $default String: optional
491 * @return String
493 public function getText( $name, $default = '' ) {
494 global $wgContLang;
495 $val = $this->getVal( $name, $default );
496 return str_replace( "\r\n", "\n",
497 $wgContLang->recodeInput( $val ) );
501 * Extracts the given named values into an array.
502 * If no arguments are given, returns all input values.
503 * No transformation is performed on the values.
505 * @return array
507 public function getValues() {
508 $names = func_get_args();
509 if ( count( $names ) == 0 ) {
510 $names = array_keys( $this->data );
513 $retVal = array();
514 foreach ( $names as $name ) {
515 $value = $this->getVal( $name );
516 if ( !is_null( $value ) ) {
517 $retVal[$name] = $value;
520 return $retVal;
524 * Returns the names of all input values excluding those in $exclude.
526 * @param $exclude Array
527 * @return array
529 public function getValueNames( $exclude = array() ) {
530 return array_diff( array_keys( $this->getValues() ), $exclude );
534 * Get the values passed in the query string.
535 * No transformation is performed on the values.
537 * @return Array
539 public function getQueryValues() {
540 return $_GET;
544 * Returns true if the present request was reached by a POST operation,
545 * false otherwise (GET, HEAD, or command-line).
547 * Note that values retrieved by the object may come from the
548 * GET URL etc even on a POST request.
550 * @return Boolean
552 public function wasPosted() {
553 return isset( $_SERVER['REQUEST_METHOD'] ) && $_SERVER['REQUEST_METHOD'] == 'POST';
557 * Returns true if there is a session cookie set.
558 * This does not necessarily mean that the user is logged in!
560 * If you want to check for an open session, use session_id()
561 * instead; that will also tell you if the session was opened
562 * during the current request (in which case the cookie will
563 * be sent back to the client at the end of the script run).
565 * @return Boolean
567 public function checkSessionCookie() {
568 return isset( $_COOKIE[ session_name() ] );
572 * Get a cookie from the $_COOKIE jar
574 * @param $key String: the name of the cookie
575 * @param $prefix String: a prefix to use for the cookie name, if not $wgCookiePrefix
576 * @param $default Mixed: what to return if the value isn't found
577 * @return Mixed: cookie value or $default if the cookie not set
579 public function getCookie( $key, $prefix = null, $default = null ) {
580 if( $prefix === null ) {
581 global $wgCookiePrefix;
582 $prefix = $wgCookiePrefix;
584 return $this->getGPCVal( $_COOKIE, $prefix . $key , $default );
588 * Return the path and query string portion of the request URI.
589 * This will be suitable for use as a relative link in HTML output.
591 * @return String
593 public function getRequestURL() {
594 if( isset( $_SERVER['REQUEST_URI'] ) && strlen( $_SERVER['REQUEST_URI'] ) ) {
595 $base = $_SERVER['REQUEST_URI'];
596 } elseif ( isset( $_SERVER['HTTP_X_ORIGINAL_URL'] ) && strlen( $_SERVER['HTTP_X_ORIGINAL_URL'] ) ) {
597 // Probably IIS; doesn't set REQUEST_URI
598 $base = $_SERVER['HTTP_X_ORIGINAL_URL'];
599 } elseif( isset( $_SERVER['SCRIPT_NAME'] ) ) {
600 $base = $_SERVER['SCRIPT_NAME'];
601 if( isset( $_SERVER['QUERY_STRING'] ) && $_SERVER['QUERY_STRING'] != '' ) {
602 $base .= '?' . $_SERVER['QUERY_STRING'];
604 } else {
605 // This shouldn't happen!
606 throw new MWException( "Web server doesn't provide either " .
607 "REQUEST_URI, HTTP_X_ORIGINAL_URL or SCRIPT_NAME. Report details " .
608 "of your web server configuration to http://bugzilla.wikimedia.org/" );
610 // User-agents should not send a fragment with the URI, but
611 // if they do, and the web server passes it on to us, we
612 // need to strip it or we get false-positive redirect loops
613 // or weird output URLs
614 $hash = strpos( $base, '#' );
615 if( $hash !== false ) {
616 $base = substr( $base, 0, $hash );
618 if( $base[0] == '/' ) {
619 return $base;
620 } else {
621 // We may get paths with a host prepended; strip it.
622 return preg_replace( '!^[^:]+://[^/]+/!', '/', $base );
627 * Return the request URI with the canonical service and hostname, path,
628 * and query string. This will be suitable for use as an absolute link
629 * in HTML or other output.
631 * If $wgServer is protocol-relative, this will return a fully
632 * qualified URL with the protocol that was used for this request.
634 * @return String
636 public function getFullRequestURL() {
637 return wfExpandUrl( $this->getRequestURL(), PROTO_CURRENT );
641 * Take an arbitrary query and rewrite the present URL to include it
642 * @param $query String: query string fragment; do not include initial '?'
644 * @return String
646 public function appendQuery( $query ) {
647 return $this->appendQueryArray( wfCgiToArray( $query ) );
651 * HTML-safe version of appendQuery().
653 * @param $query String: query string fragment; do not include initial '?'
654 * @return String
656 public function escapeAppendQuery( $query ) {
657 return htmlspecialchars( $this->appendQuery( $query ) );
661 * @param $key
662 * @param $value
663 * @param $onlyquery bool
664 * @return String
666 public function appendQueryValue( $key, $value, $onlyquery = false ) {
667 return $this->appendQueryArray( array( $key => $value ), $onlyquery );
671 * Appends or replaces value of query variables.
673 * @param $array Array of values to replace/add to query
674 * @param $onlyquery Bool: whether to only return the query string and not
675 * the complete URL
676 * @return String
678 public function appendQueryArray( $array, $onlyquery = false ) {
679 global $wgTitle;
680 $newquery = $this->getQueryValues();
681 unset( $newquery['title'] );
682 $newquery = array_merge( $newquery, $array );
683 $query = wfArrayToCGI( $newquery );
684 return $onlyquery ? $query : $wgTitle->getLocalURL( $query );
688 * Check for limit and offset parameters on the input, and return sensible
689 * defaults if not given. The limit must be positive and is capped at 5000.
690 * Offset must be positive but is not capped.
692 * @param $deflimit Integer: limit to use if no input and the user hasn't set the option.
693 * @param $optionname String: to specify an option other than rclimit to pull from.
694 * @return array first element is limit, second is offset
696 public function getLimitOffset( $deflimit = 50, $optionname = 'rclimit' ) {
697 global $wgUser;
699 $limit = $this->getInt( 'limit', 0 );
700 if( $limit < 0 ) {
701 $limit = 0;
703 if( ( $limit == 0 ) && ( $optionname != '' ) ) {
704 $limit = (int)$wgUser->getOption( $optionname );
706 if( $limit <= 0 ) {
707 $limit = $deflimit;
709 if( $limit > 5000 ) {
710 $limit = 5000; # We have *some* limits...
713 $offset = $this->getInt( 'offset', 0 );
714 if( $offset < 0 ) {
715 $offset = 0;
718 return array( $limit, $offset );
722 * Return the path to the temporary file where PHP has stored the upload.
724 * @param $key String:
725 * @return string or NULL if no such file.
727 public function getFileTempname( $key ) {
728 $file = new WebRequestUpload( $this, $key );
729 return $file->getTempName();
733 * Return the size of the upload, or 0.
735 * @deprecated since 1.17
736 * @param $key String:
737 * @return integer
739 public function getFileSize( $key ) {
740 $file = new WebRequestUpload( $this, $key );
741 return $file->getSize();
745 * Return the upload error or 0
747 * @param $key String:
748 * @return integer
750 public function getUploadError( $key ) {
751 $file = new WebRequestUpload( $this, $key );
752 return $file->getError();
756 * Return the original filename of the uploaded file, as reported by
757 * the submitting user agent. HTML-style character entities are
758 * interpreted and normalized to Unicode normalization form C, in part
759 * to deal with weird input from Safari with non-ASCII filenames.
761 * Other than this the name is not verified for being a safe filename.
763 * @param $key String:
764 * @return string or NULL if no such file.
766 public function getFileName( $key ) {
767 $file = new WebRequestUpload( $this, $key );
768 return $file->getName();
772 * Return a WebRequestUpload object corresponding to the key
774 * @param $key string
775 * @return WebRequestUpload
777 public function getUpload( $key ) {
778 return new WebRequestUpload( $this, $key );
782 * Return a handle to WebResponse style object, for setting cookies,
783 * headers and other stuff, for Request being worked on.
785 * @return WebResponse
787 public function response() {
788 /* Lazy initialization of response object for this request */
789 if ( !is_object( $this->response ) ) {
790 $class = ( $this instanceof FauxRequest ) ? 'FauxResponse' : 'WebResponse';
791 $this->response = new $class();
793 return $this->response;
797 * Initialise the header list
799 private function initHeaders() {
800 if ( count( $this->headers ) ) {
801 return;
804 if ( function_exists( 'apache_request_headers' ) ) {
805 foreach ( apache_request_headers() as $tempName => $tempValue ) {
806 $this->headers[ strtoupper( $tempName ) ] = $tempValue;
808 } else {
809 foreach ( $_SERVER as $name => $value ) {
810 if ( substr( $name, 0, 5 ) === 'HTTP_' ) {
811 $name = str_replace( '_', '-', substr( $name, 5 ) );
812 $this->headers[$name] = $value;
813 } elseif ( $name === 'CONTENT_LENGTH' ) {
814 $this->headers['CONTENT-LENGTH'] = $value;
821 * Get an array containing all request headers
823 * @return Array mapping header name to its value
825 public function getAllHeaders() {
826 $this->initHeaders();
827 return $this->headers;
831 * Get a request header, or false if it isn't set
832 * @param $name String: case-insensitive header name
834 * @return string|false
836 public function getHeader( $name ) {
837 $this->initHeaders();
838 $name = strtoupper( $name );
839 if ( isset( $this->headers[$name] ) ) {
840 return $this->headers[$name];
841 } else {
842 return false;
847 * Get data from $_SESSION
849 * @param $key String: name of key in $_SESSION
850 * @return Mixed
852 public function getSessionData( $key ) {
853 if( !isset( $_SESSION[$key] ) ) {
854 return null;
856 return $_SESSION[$key];
860 * Set session data
862 * @param $key String: name of key in $_SESSION
863 * @param $data Mixed
865 public function setSessionData( $key, $data ) {
866 $_SESSION[$key] = $data;
870 * Check if Internet Explorer will detect an incorrect cache extension in
871 * PATH_INFO or QUERY_STRING. If the request can't be allowed, show an error
872 * message or redirect to a safer URL. Returns true if the URL is OK, and
873 * false if an error message has been shown and the request should be aborted.
875 * @param $extWhitelist array
876 * @return bool
878 public function checkUrlExtension( $extWhitelist = array() ) {
879 global $wgScriptExtension;
880 $extWhitelist[] = ltrim( $wgScriptExtension, '.' );
881 if ( IEUrlExtension::areServerVarsBad( $_SERVER, $extWhitelist ) ) {
882 if ( !$this->wasPosted() ) {
883 $newUrl = IEUrlExtension::fixUrlForIE6(
884 $this->getFullRequestURL(), $extWhitelist );
885 if ( $newUrl !== false ) {
886 $this->doSecurityRedirect( $newUrl );
887 return false;
890 throw new HttpError( 403,
891 'Invalid file extension found in the path info or query string.' );
893 return true;
897 * Attempt to redirect to a URL with a QUERY_STRING that's not dangerous in
898 * IE 6. Returns true if it was successful, false otherwise.
900 * @param $url string
901 * @return bool
903 protected function doSecurityRedirect( $url ) {
904 header( 'Location: ' . $url );
905 header( 'Content-Type: text/html' );
906 $encUrl = htmlspecialchars( $url );
907 echo <<<HTML
908 <html>
909 <head>
910 <title>Security redirect</title>
911 </head>
912 <body>
913 <h1>Security redirect</h1>
915 We can't serve non-HTML content from the URL you have requested, because
916 Internet Explorer would interpret it as an incorrect and potentially dangerous
917 content type.</p>
918 <p>Instead, please use <a href="$encUrl">this URL</a>, which is the same as the URL you have requested, except that
919 "&amp;*" is appended. This prevents Internet Explorer from seeing a bogus file
920 extension.
921 </p>
922 </body>
923 </html>
924 HTML;
925 echo "\n";
926 return true;
930 * Returns true if the PATH_INFO ends with an extension other than a script
931 * extension. This could confuse IE for scripts that send arbitrary data which
932 * is not HTML but may be detected as such.
934 * Various past attempts to use the URL to make this check have generally
935 * run up against the fact that CGI does not provide a standard method to
936 * determine the URL. PATH_INFO may be mangled (e.g. if cgi.fix_pathinfo=0),
937 * but only by prefixing it with the script name and maybe some other stuff,
938 * the extension is not mangled. So this should be a reasonably portable
939 * way to perform this security check.
941 * Also checks for anything that looks like a file extension at the end of
942 * QUERY_STRING, since IE 6 and earlier will use this to get the file type
943 * if there was no dot before the question mark (bug 28235).
945 * @deprecated Use checkUrlExtension().
947 * @param $extWhitelist array
949 * @return bool
951 public function isPathInfoBad( $extWhitelist = array() ) {
952 global $wgScriptExtension;
953 $extWhitelist[] = ltrim( $wgScriptExtension, '.' );
954 return IEUrlExtension::areServerVarsBad( $_SERVER, $extWhitelist );
958 * Parse the Accept-Language header sent by the client into an array
959 * @return array array( languageCode => q-value ) sorted by q-value in descending order
960 * May contain the "language" '*', which applies to languages other than those explicitly listed.
961 * This is aligned with rfc2616 section 14.4
963 public function getAcceptLang() {
964 // Modified version of code found at http://www.thefutureoftheweb.com/blog/use-accept-language-header
965 $acceptLang = $this->getHeader( 'Accept-Language' );
966 if ( !$acceptLang ) {
967 return array();
970 // Return the language codes in lower case
971 $acceptLang = strtolower( $acceptLang );
973 // Break up string into pieces (languages and q factors)
974 $lang_parse = null;
975 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})?)?)?/',
976 $acceptLang, $lang_parse );
978 if ( !count( $lang_parse[1] ) ) {
979 return array();
982 // Create a list like "en" => 0.8
983 $langs = array_combine( $lang_parse[1], $lang_parse[4] );
984 // Set default q factor to 1
985 foreach ( $langs as $lang => $val ) {
986 if ( $val === '' ) {
987 $langs[$lang] = 1;
988 } elseif ( $val == 0 ) {
989 unset($langs[$lang]);
993 // Sort list
994 arsort( $langs, SORT_NUMERIC );
995 return $langs;
999 * Fetch the raw IP from the request
1001 * @return String
1003 protected function getRawIP() {
1004 if ( isset( $_SERVER['REMOTE_ADDR'] ) ) {
1005 return IP::canonicalize( $_SERVER['REMOTE_ADDR'] );
1006 } else {
1007 return null;
1012 * Work out the IP address based on various globals
1013 * For trusted proxies, use the XFF client IP (first of the chain)
1014 * @return string
1016 public function getIP() {
1017 global $wgUsePrivateIPs;
1019 # Return cached result
1020 if ( $this->ip !== null ) {
1021 return $this->ip;
1024 # collect the originating ips
1025 $ip = $this->getRawIP();
1027 # Append XFF
1028 $forwardedFor = $this->getHeader( 'X-Forwarded-For' );
1029 if ( $forwardedFor !== false ) {
1030 $ipchain = array_map( 'trim', explode( ',', $forwardedFor ) );
1031 $ipchain = array_reverse( $ipchain );
1032 if ( $ip ) {
1033 array_unshift( $ipchain, $ip );
1036 # Step through XFF list and find the last address in the list which is a trusted server
1037 # Set $ip to the IP address given by that trusted server, unless the address is not sensible (e.g. private)
1038 foreach ( $ipchain as $i => $curIP ) {
1039 $curIP = IP::canonicalize( $curIP );
1040 if ( wfIsTrustedProxy( $curIP ) ) {
1041 if ( isset( $ipchain[$i + 1] ) ) {
1042 if ( $wgUsePrivateIPs || IP::isPublic( $ipchain[$i + 1 ] ) ) {
1043 $ip = $ipchain[$i + 1];
1046 } else {
1047 break;
1052 # Allow extensions to improve our guess
1053 wfRunHooks( 'GetIP', array( &$ip ) );
1055 if ( !$ip ) {
1056 throw new MWException( "Unable to determine IP" );
1059 wfDebug( "IP: $ip\n" );
1060 $this->ip = $ip;
1061 return $ip;
1066 * Object to access the $_FILES array
1068 class WebRequestUpload {
1069 protected $request;
1070 protected $doesExist;
1071 protected $fileInfo;
1074 * Constructor. Should only be called by WebRequest
1076 * @param $request WebRequest The associated request
1077 * @param $key string Key in $_FILES array (name of form field)
1079 public function __construct( $request, $key ) {
1080 $this->request = $request;
1081 $this->doesExist = isset( $_FILES[$key] );
1082 if ( $this->doesExist ) {
1083 $this->fileInfo = $_FILES[$key];
1088 * Return whether a file with this name was uploaded.
1090 * @return bool
1092 public function exists() {
1093 return $this->doesExist;
1097 * Return the original filename of the uploaded file
1099 * @return mixed Filename or null if non-existent
1101 public function getName() {
1102 if ( !$this->exists() ) {
1103 return null;
1106 global $wgContLang;
1107 $name = $this->fileInfo['name'];
1109 # Safari sends filenames in HTML-encoded Unicode form D...
1110 # Horrid and evil! Let's try to make some kind of sense of it.
1111 $name = Sanitizer::decodeCharReferences( $name );
1112 $name = $wgContLang->normalize( $name );
1113 wfDebug( __METHOD__ . ": {$this->fileInfo['name']} normalized to '$name'\n" );
1114 return $name;
1118 * Return the file size of the uploaded file
1120 * @return int File size or zero if non-existent
1122 public function getSize() {
1123 if ( !$this->exists() ) {
1124 return 0;
1127 return $this->fileInfo['size'];
1131 * Return the path to the temporary file
1133 * @return mixed Path or null if non-existent
1135 public function getTempName() {
1136 if ( !$this->exists() ) {
1137 return null;
1140 return $this->fileInfo['tmp_name'];
1144 * Return the upload error. See link for explanation
1145 * http://www.php.net/manual/en/features.file-upload.errors.php
1147 * @return int One of the UPLOAD_ constants, 0 if non-existent
1149 public function getError() {
1150 if ( !$this->exists() ) {
1151 return 0; # UPLOAD_ERR_OK
1154 return $this->fileInfo['error'];
1158 * Returns whether this upload failed because of overflow of a maximum set
1159 * in php.ini
1161 * @return bool
1163 public function isIniSizeOverflow() {
1164 if ( $this->getError() == UPLOAD_ERR_INI_SIZE ) {
1165 # PHP indicated that upload_max_filesize is exceeded
1166 return true;
1169 $contentLength = $this->request->getHeader( 'CONTENT_LENGTH' );
1170 if ( $contentLength > wfShorthandToInteger( ini_get( 'post_max_size' ) ) ) {
1171 # post_max_size is exceeded
1172 return true;
1175 return false;
1180 * WebRequest clone which takes values from a provided array.
1182 * @ingroup HTTP
1184 class FauxRequest extends WebRequest {
1185 private $wasPosted = false;
1186 private $session = array();
1189 * @param $data Array of *non*-urlencoded key => value pairs, the
1190 * fake GET/POST values
1191 * @param $wasPosted Bool: whether to treat the data as POST
1192 * @param $session Mixed: session array or null
1194 public function __construct( $data, $wasPosted = false, $session = null ) {
1195 if( is_array( $data ) ) {
1196 $this->data = $data;
1197 } else {
1198 throw new MWException( "FauxRequest() got bogus data" );
1200 $this->wasPosted = $wasPosted;
1201 if( $session )
1202 $this->session = $session;
1206 * @param $method string
1207 * @throws MWException
1209 private function notImplemented( $method ) {
1210 throw new MWException( "{$method}() not implemented" );
1214 * @param $name string
1215 * @param $default string
1216 * @return string
1218 public function getText( $name, $default = '' ) {
1219 # Override; don't recode since we're using internal data
1220 return (string)$this->getVal( $name, $default );
1224 * @return Array
1226 public function getValues() {
1227 return $this->data;
1231 * @return array
1233 public function getQueryValues() {
1234 if ( $this->wasPosted ) {
1235 return array();
1236 } else {
1237 return $this->data;
1242 * @return bool
1244 public function wasPosted() {
1245 return $this->wasPosted;
1248 public function checkSessionCookie() {
1249 return false;
1252 public function getRequestURL() {
1253 $this->notImplemented( __METHOD__ );
1257 * @param $name
1258 * @return bool|string
1260 public function getHeader( $name ) {
1261 return isset( $this->headers[$name] ) ? $this->headers[$name] : false;
1265 * @param $name string
1266 * @param $val string
1268 public function setHeader( $name, $val ) {
1269 $this->headers[$name] = $val;
1273 * @param $key
1274 * @return mixed
1276 public function getSessionData( $key ) {
1277 if( isset( $this->session[$key] ) )
1278 return $this->session[$key];
1282 * @param $key
1283 * @param $data
1285 public function setSessionData( $key, $data ) {
1286 $this->session[$key] = $data;
1290 * @return array|Mixed|null
1292 public function getSessionArray() {
1293 return $this->session;
1297 * @param array $extWhitelist
1298 * @return bool
1300 public function isPathInfoBad( $extWhitelist = array() ) {
1301 return false;
1305 * @param array $extWhitelist
1306 * @return bool
1308 public function checkUrlExtension( $extWhitelist = array() ) {
1309 return true;
1313 * @return string
1315 protected function getRawIP() {
1316 return '127.0.0.1';
1321 * Similar to FauxRequest, but only fakes URL parameters and method
1322 * (POST or GET) and use the base request for the remaining stuff
1323 * (cookies, session and headers).
1325 * @ingroup HTTP
1327 class DerivativeRequest extends FauxRequest {
1328 private $base;
1330 public function __construct( WebRequest $base, $data, $wasPosted = false ) {
1331 $this->base = $base;
1332 parent::__construct( $data, $wasPosted );
1335 public function getCookie( $key, $prefix = null, $default = null ) {
1336 return $this->base->getCookie( $key, $prefix, $default );
1339 public function checkSessionCookie() {
1340 return $this->base->checkSessionCookie();
1343 public function getHeader( $name ) {
1344 return $this->base->getHeader( $name );
1347 public function getAllHeaders() {
1348 return $this->base->getAllHeaders();
1351 public function getSessionData( $key ) {
1352 return $this->base->getSessionData( $key );
1355 public function setSessionData( $key, $data ) {
1356 return $this->base->setSessionData( $key, $data );
1359 public function getAcceptLang() {
1360 return $this->base->getAcceptLang();
1363 public function getIP() {
1364 return $this->base->getIP();