Set default value for $wgSharedSchema
[lhc/web/wiklou.git] / includes / WebRequest.php
1 <?php
2 /**
3 * Deal with importing all those nasty globals and things
4 *
5 * Copyright © 2003 Brion Vibber <brion@pobox.com>
6 * https://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 stripping illegal input characters and
29 * 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 /**
48 * Cached client IP address
49 * @var string
50 */
51 private $ip;
52
53 /**
54 * Cached URL protocol
55 * @var string
56 */
57 protected $protocol;
58
59 public function __construct() {
60 if ( function_exists( 'get_magic_quotes_gpc' ) && get_magic_quotes_gpc() ) {
61 throw new MWException( "MediaWiki does not function when magic quotes are enabled." );
62 }
63
64 // POST overrides GET data
65 // We don't use $_REQUEST here to avoid interference from cookies...
66 $this->data = $_POST + $_GET;
67 }
68
69 /**
70 * Extract relevant query arguments from the http request uri's path
71 * to be merged with the normal php provided query arguments.
72 * Tries to use the REQUEST_URI data if available and parses it
73 * according to the wiki's configuration looking for any known pattern.
74 *
75 * If the REQUEST_URI is not provided we'll fall back on the PATH_INFO
76 * provided by the server if any and use that to set a 'title' parameter.
77 *
78 * @param string $want If this is not 'all', then the function
79 * will return an empty array if it determines that the URL is
80 * inside a rewrite path.
81 *
82 * @return array Any query arguments found in path matches.
83 */
84 public static function getPathInfo( $want = 'all' ) {
85 global $wgUsePathInfo;
86 // PATH_INFO is mangled due to http://bugs.php.net/bug.php?id=31892
87 // And also by Apache 2.x, double slashes are converted to single slashes.
88 // So we will use REQUEST_URI if possible.
89 $matches = array();
90 if ( !empty( $_SERVER['REQUEST_URI'] ) ) {
91 // Slurp out the path portion to examine...
92 $url = $_SERVER['REQUEST_URI'];
93 if ( !preg_match( '!^https?://!', $url ) ) {
94 $url = 'http://unused' . $url;
95 }
96 wfSuppressWarnings();
97 $a = parse_url( $url );
98 wfRestoreWarnings();
99 if ( $a ) {
100 $path = isset( $a['path'] ) ? $a['path'] : '';
101
102 global $wgScript;
103 if ( $path == $wgScript && $want !== 'all' ) {
104 // Script inside a rewrite path?
105 // Abort to keep from breaking...
106 return $matches;
107 }
108
109 $router = new PathRouter;
110
111 // Raw PATH_INFO style
112 $router->add( "$wgScript/$1" );
113
114 if ( isset( $_SERVER['SCRIPT_NAME'] )
115 && preg_match( '/\.php5?/', $_SERVER['SCRIPT_NAME'] )
116 ) {
117 # Check for SCRIPT_NAME, we handle index.php explicitly
118 # But we do have some other .php files such as img_auth.php
119 # Don't let root article paths clober the parsing for them
120 $router->add( $_SERVER['SCRIPT_NAME'] . "/$1" );
121 }
122
123 global $wgArticlePath;
124 if ( $wgArticlePath ) {
125 $router->add( $wgArticlePath );
126 }
127
128 global $wgActionPaths;
129 if ( $wgActionPaths ) {
130 $router->add( $wgActionPaths, array( 'action' => '$key' ) );
131 }
132
133 global $wgVariantArticlePath, $wgContLang;
134 if ( $wgVariantArticlePath ) {
135 $router->add( $wgVariantArticlePath,
136 array( 'variant' => '$2' ),
137 array( '$2' => $wgContLang->getVariants() )
138 );
139 }
140
141 wfRunHooks( 'WebRequestPathInfoRouter', array( $router ) );
142
143 $matches = $router->parse( $path );
144 }
145 } elseif ( $wgUsePathInfo ) {
146 if ( isset( $_SERVER['ORIG_PATH_INFO'] ) && $_SERVER['ORIG_PATH_INFO'] != '' ) {
147 // Mangled PATH_INFO
148 // http://bugs.php.net/bug.php?id=31892
149 // Also reported when ini_get('cgi.fix_pathinfo')==false
150 $matches['title'] = substr( $_SERVER['ORIG_PATH_INFO'], 1 );
151
152 } elseif ( isset( $_SERVER['PATH_INFO'] ) && $_SERVER['PATH_INFO'] != '' ) {
153 // Regular old PATH_INFO yay
154 $matches['title'] = substr( $_SERVER['PATH_INFO'], 1 );
155 }
156 }
157
158 return $matches;
159 }
160
161 /**
162 * Work out an appropriate URL prefix containing scheme and host, based on
163 * information detected from $_SERVER
164 *
165 * @return string
166 */
167 public static function detectServer() {
168 $proto = self::detectProtocol();
169 $stdPort = $proto === 'https' ? 443 : 80;
170
171 $varNames = array( 'HTTP_HOST', 'SERVER_NAME', 'HOSTNAME', 'SERVER_ADDR' );
172 $host = 'localhost';
173 $port = $stdPort;
174 foreach ( $varNames as $varName ) {
175 if ( !isset( $_SERVER[$varName] ) ) {
176 continue;
177 }
178 $parts = IP::splitHostAndPort( $_SERVER[$varName] );
179 if ( !$parts ) {
180 // Invalid, do not use
181 continue;
182 }
183 $host = $parts[0];
184 if ( isset( $_SERVER['HTTP_X_FORWARDED_PROTO'] ) ) {
185 // Bug 70021: Assume that upstream proxy is running on the default
186 // port based on the protocol. We have no reliable way to determine
187 // the actual port in use upstream.
188 $port = $stdPort;
189 } elseif ( $parts[1] === false ) {
190 if ( isset( $_SERVER['SERVER_PORT'] ) ) {
191 $port = $_SERVER['SERVER_PORT'];
192 } // else leave it as $stdPort
193 } else {
194 $port = $parts[1];
195 }
196 break;
197 }
198
199 return $proto . '://' . IP::combineHostAndPort( $host, $port, $stdPort );
200 }
201
202 /**
203 * Detect the protocol from $_SERVER.
204 * This is for use prior to Setup.php, when no WebRequest object is available.
205 * At other times, use the non-static function getProtocol().
206 *
207 * @return array
208 */
209 public static function detectProtocol() {
210 if ( ( isset( $_SERVER['HTTPS'] ) && $_SERVER['HTTPS'] == 'on' ) ||
211 ( isset( $_SERVER['HTTP_X_FORWARDED_PROTO'] ) &&
212 $_SERVER['HTTP_X_FORWARDED_PROTO'] == 'https' ) ) {
213 return 'https';
214 } else {
215 return 'http';
216 }
217 }
218
219 /**
220 * Get the current URL protocol (http or https)
221 * @return string
222 */
223 public function getProtocol() {
224 if ( $this->protocol === null ) {
225 $this->protocol = self::detectProtocol();
226 }
227 return $this->protocol;
228 }
229
230 /**
231 * Check for title, action, and/or variant data in the URL
232 * and interpolate it into the GET variables.
233 * This should only be run after $wgContLang is available,
234 * as we may need the list of language variants to determine
235 * available variant URLs.
236 */
237 public function interpolateTitle() {
238 // bug 16019: title interpolation on API queries is useless and sometimes harmful
239 if ( defined( 'MW_API' ) ) {
240 return;
241 }
242
243 $matches = self::getPathInfo( 'title' );
244 foreach ( $matches as $key => $val ) {
245 $this->data[$key] = $_GET[$key] = $_REQUEST[$key] = $val;
246 }
247 }
248
249 /**
250 * URL rewriting function; tries to extract page title and,
251 * optionally, one other fixed parameter value from a URL path.
252 *
253 * @param string $path The URL path given from the client
254 * @param array $bases One or more URLs, optionally with $1 at the end
255 * @param string $key If provided, the matching key in $bases will be
256 * passed on as the value of this URL parameter
257 * @return array Array of URL variables to interpolate; empty if no match
258 */
259 static function extractTitle( $path, $bases, $key = false ) {
260 foreach ( (array)$bases as $keyValue => $base ) {
261 // Find the part after $wgArticlePath
262 $base = str_replace( '$1', '', $base );
263 $baseLen = strlen( $base );
264 if ( substr( $path, 0, $baseLen ) == $base ) {
265 $raw = substr( $path, $baseLen );
266 if ( $raw !== '' ) {
267 $matches = array( 'title' => rawurldecode( $raw ) );
268 if ( $key ) {
269 $matches[$key] = $keyValue;
270 }
271 return $matches;
272 }
273 }
274 }
275 return array();
276 }
277
278 /**
279 * Recursively normalizes UTF-8 strings in the given array.
280 *
281 * @param string|array $data
282 * @return array|string Cleaned-up version of the given
283 * @private
284 */
285 function normalizeUnicode( $data ) {
286 if ( is_array( $data ) ) {
287 foreach ( $data as $key => $val ) {
288 $data[$key] = $this->normalizeUnicode( $val );
289 }
290 } else {
291 global $wgContLang;
292 $data = isset( $wgContLang ) ? $wgContLang->normalize( $data ) : UtfNormal::cleanUp( $data );
293 }
294 return $data;
295 }
296
297 /**
298 * Fetch a value from the given array or return $default if it's not set.
299 *
300 * @param array $arr
301 * @param string $name
302 * @param mixed $default
303 * @return mixed
304 */
305 private function getGPCVal( $arr, $name, $default ) {
306 # PHP is so nice to not touch input data, except sometimes:
307 # http://us2.php.net/variables.external#language.variables.external.dot-in-names
308 # Work around PHP *feature* to avoid *bugs* elsewhere.
309 $name = strtr( $name, '.', '_' );
310 if ( isset( $arr[$name] ) ) {
311 global $wgContLang;
312 $data = $arr[$name];
313 if ( isset( $_GET[$name] ) && !is_array( $data ) ) {
314 # Check for alternate/legacy character encoding.
315 if ( isset( $wgContLang ) ) {
316 $data = $wgContLang->checkTitleEncoding( $data );
317 }
318 }
319 $data = $this->normalizeUnicode( $data );
320 return $data;
321 } else {
322 return $default;
323 }
324 }
325
326 /**
327 * Fetch a scalar from the input or return $default if it's not set.
328 * Returns a string. Arrays are discarded. Useful for
329 * non-freeform text inputs (e.g. predefined internal text keys
330 * selected by a drop-down menu). For freeform input, see getText().
331 *
332 * @param string $name
333 * @param string $default Optional default (or null)
334 * @return string
335 */
336 public function getVal( $name, $default = null ) {
337 $val = $this->getGPCVal( $this->data, $name, $default );
338 if ( is_array( $val ) ) {
339 $val = $default;
340 }
341 if ( is_null( $val ) ) {
342 return $val;
343 } else {
344 return (string)$val;
345 }
346 }
347
348 /**
349 * Set an arbitrary value into our get/post data.
350 *
351 * @param string $key Key name to use
352 * @param mixed $value Value to set
353 * @return mixed Old value if one was present, null otherwise
354 */
355 public function setVal( $key, $value ) {
356 $ret = isset( $this->data[$key] ) ? $this->data[$key] : null;
357 $this->data[$key] = $value;
358 return $ret;
359 }
360
361 /**
362 * Unset an arbitrary value from our get/post data.
363 *
364 * @param string $key Key name to use
365 * @return mixed Old value if one was present, null otherwise
366 */
367 public function unsetVal( $key ) {
368 if ( !isset( $this->data[$key] ) ) {
369 $ret = null;
370 } else {
371 $ret = $this->data[$key];
372 unset( $this->data[$key] );
373 }
374 return $ret;
375 }
376
377 /**
378 * Fetch an array from the input or return $default if it's not set.
379 * If source was scalar, will return an array with a single element.
380 * If no source and no default, returns null.
381 *
382 * @param string $name
383 * @param array $default Optional default (or null)
384 * @return array
385 */
386 public function getArray( $name, $default = null ) {
387 $val = $this->getGPCVal( $this->data, $name, $default );
388 if ( is_null( $val ) ) {
389 return null;
390 } else {
391 return (array)$val;
392 }
393 }
394
395 /**
396 * Fetch an array of integers, or return $default if it's not set.
397 * If source was scalar, will return an array with a single element.
398 * If no source and no default, returns null.
399 * If an array is returned, contents are guaranteed to be integers.
400 *
401 * @param string $name
402 * @param array $default Option default (or null)
403 * @return array Array of ints
404 */
405 public function getIntArray( $name, $default = null ) {
406 $val = $this->getArray( $name, $default );
407 if ( is_array( $val ) ) {
408 $val = array_map( 'intval', $val );
409 }
410 return $val;
411 }
412
413 /**
414 * Fetch an integer value from the input or return $default if not set.
415 * Guaranteed to return an integer; non-numeric input will typically
416 * return 0.
417 *
418 * @param string $name
419 * @param int $default
420 * @return int
421 */
422 public function getInt( $name, $default = 0 ) {
423 return intval( $this->getVal( $name, $default ) );
424 }
425
426 /**
427 * Fetch an integer value from the input or return null if empty.
428 * Guaranteed to return an integer or null; non-numeric input will
429 * typically return null.
430 *
431 * @param string $name
432 * @return int|null
433 */
434 public function getIntOrNull( $name ) {
435 $val = $this->getVal( $name );
436 return is_numeric( $val )
437 ? intval( $val )
438 : null;
439 }
440
441 /**
442 * Fetch a floating point value from the input or return $default if not set.
443 * Guaranteed to return a float; non-numeric input will typically
444 * return 0.
445 *
446 * @since 1.23
447 * @param string $name
448 * @param float $default
449 * @return float
450 */
451 public function getFloat( $name, $default = 0.0 ) {
452 return floatval( $this->getVal( $name, $default ) );
453 }
454
455 /**
456 * Fetch a boolean value from the input or return $default if not set.
457 * Guaranteed to return true or false, with normal PHP semantics for
458 * boolean interpretation of strings.
459 *
460 * @param string $name
461 * @param bool $default
462 * @return bool
463 */
464 public function getBool( $name, $default = false ) {
465 return (bool)$this->getVal( $name, $default );
466 }
467
468 /**
469 * Fetch a boolean value from the input or return $default if not set.
470 * Unlike getBool, the string "false" will result in boolean false, which is
471 * useful when interpreting information sent from JavaScript.
472 *
473 * @param string $name
474 * @param bool $default
475 * @return bool
476 */
477 public function getFuzzyBool( $name, $default = false ) {
478 return $this->getBool( $name, $default ) && strcasecmp( $this->getVal( $name ), 'false' ) !== 0;
479 }
480
481 /**
482 * Return true if the named value is set in the input, whatever that
483 * value is (even "0"). Return false if the named value is not set.
484 * Example use is checking for the presence of check boxes in forms.
485 *
486 * @param string $name
487 * @return bool
488 */
489 public function getCheck( $name ) {
490 # Checkboxes and buttons are only present when clicked
491 # Presence connotes truth, absence false
492 return $this->getVal( $name, null ) !== null;
493 }
494
495 /**
496 * Fetch a text string from the given array or return $default if it's not
497 * set. Carriage returns are stripped from the text, and with some language
498 * modules there is an input transliteration applied. This should generally
499 * be used for form "<textarea>" and "<input>" fields. Used for
500 * user-supplied freeform text input (for which input transformations may
501 * be required - e.g. Esperanto x-coding).
502 *
503 * @param string $name
504 * @param string $default Optional
505 * @return string
506 */
507 public function getText( $name, $default = '' ) {
508 global $wgContLang;
509 $val = $this->getVal( $name, $default );
510 return str_replace( "\r\n", "\n",
511 $wgContLang->recodeInput( $val ) );
512 }
513
514 /**
515 * Extracts the given named values into an array.
516 * If no arguments are given, returns all input values.
517 * No transformation is performed on the values.
518 *
519 * @return array
520 */
521 public function getValues() {
522 $names = func_get_args();
523 if ( count( $names ) == 0 ) {
524 $names = array_keys( $this->data );
525 }
526
527 $retVal = array();
528 foreach ( $names as $name ) {
529 $value = $this->getGPCVal( $this->data, $name, null );
530 if ( !is_null( $value ) ) {
531 $retVal[$name] = $value;
532 }
533 }
534 return $retVal;
535 }
536
537 /**
538 * Returns the names of all input values excluding those in $exclude.
539 *
540 * @param array $exclude
541 * @return array
542 */
543 public function getValueNames( $exclude = array() ) {
544 return array_diff( array_keys( $this->getValues() ), $exclude );
545 }
546
547 /**
548 * Get the values passed in the query string.
549 * No transformation is performed on the values.
550 *
551 * @return array
552 */
553 public function getQueryValues() {
554 return $_GET;
555 }
556
557 /**
558 * Return the contents of the Query with no decoding. Use when you need to
559 * know exactly what was sent, e.g. for an OAuth signature over the elements.
560 *
561 * @return string
562 */
563 public function getRawQueryString() {
564 return $_SERVER['QUERY_STRING'];
565 }
566
567 /**
568 * Return the contents of the POST with no decoding. Use when you need to
569 * know exactly what was sent, e.g. for an OAuth signature over the elements.
570 *
571 * @return string
572 */
573 public function getRawPostString() {
574 if ( !$this->wasPosted() ) {
575 return '';
576 }
577 return $this->getRawInput();
578 }
579
580 /**
581 * Return the raw request body, with no processing. Cached since some methods
582 * disallow reading the stream more than once. As stated in the php docs, this
583 * does not work with enctype="multipart/form-data".
584 *
585 * @return string
586 */
587 public function getRawInput() {
588 static $input = null;
589 if ( $input === null ) {
590 $input = file_get_contents( 'php://input' );
591 }
592 return $input;
593 }
594
595 /**
596 * Get the HTTP method used for this request.
597 *
598 * @return string
599 */
600 public function getMethod() {
601 return isset( $_SERVER['REQUEST_METHOD'] ) ? $_SERVER['REQUEST_METHOD'] : 'GET';
602 }
603
604 /**
605 * Returns true if the present request was reached by a POST operation,
606 * false otherwise (GET, HEAD, or command-line).
607 *
608 * Note that values retrieved by the object may come from the
609 * GET URL etc even on a POST request.
610 *
611 * @return bool
612 */
613 public function wasPosted() {
614 return $this->getMethod() == 'POST';
615 }
616
617 /**
618 * Returns true if there is a session cookie set.
619 * This does not necessarily mean that the user is logged in!
620 *
621 * If you want to check for an open session, use session_id()
622 * instead; that will also tell you if the session was opened
623 * during the current request (in which case the cookie will
624 * be sent back to the client at the end of the script run).
625 *
626 * @return bool
627 */
628 public function checkSessionCookie() {
629 return isset( $_COOKIE[session_name()] );
630 }
631
632 /**
633 * Get a cookie from the $_COOKIE jar
634 *
635 * @param string $key The name of the cookie
636 * @param string $prefix A prefix to use for the cookie name, if not $wgCookiePrefix
637 * @param mixed $default What to return if the value isn't found
638 * @return mixed Cookie value or $default if the cookie not set
639 */
640 public function getCookie( $key, $prefix = null, $default = null ) {
641 if ( $prefix === null ) {
642 global $wgCookiePrefix;
643 $prefix = $wgCookiePrefix;
644 }
645 return $this->getGPCVal( $_COOKIE, $prefix . $key, $default );
646 }
647
648 /**
649 * Return the path and query string portion of the request URI.
650 * This will be suitable for use as a relative link in HTML output.
651 *
652 * @throws MWException
653 * @return string
654 */
655 public function getRequestURL() {
656 if ( isset( $_SERVER['REQUEST_URI'] ) && strlen( $_SERVER['REQUEST_URI'] ) ) {
657 $base = $_SERVER['REQUEST_URI'];
658 } elseif ( isset( $_SERVER['HTTP_X_ORIGINAL_URL'] )
659 && strlen( $_SERVER['HTTP_X_ORIGINAL_URL'] )
660 ) {
661 // Probably IIS; doesn't set REQUEST_URI
662 $base = $_SERVER['HTTP_X_ORIGINAL_URL'];
663 } elseif ( isset( $_SERVER['SCRIPT_NAME'] ) ) {
664 $base = $_SERVER['SCRIPT_NAME'];
665 if ( isset( $_SERVER['QUERY_STRING'] ) && $_SERVER['QUERY_STRING'] != '' ) {
666 $base .= '?' . $_SERVER['QUERY_STRING'];
667 }
668 } else {
669 // This shouldn't happen!
670 throw new MWException( "Web server doesn't provide either " .
671 "REQUEST_URI, HTTP_X_ORIGINAL_URL or SCRIPT_NAME. Report details " .
672 "of your web server configuration to http://bugzilla.wikimedia.org/" );
673 }
674 // User-agents should not send a fragment with the URI, but
675 // if they do, and the web server passes it on to us, we
676 // need to strip it or we get false-positive redirect loops
677 // or weird output URLs
678 $hash = strpos( $base, '#' );
679 if ( $hash !== false ) {
680 $base = substr( $base, 0, $hash );
681 }
682
683 if ( $base[0] == '/' ) {
684 // More than one slash will look like it is protocol relative
685 return preg_replace( '!^/+!', '/', $base );
686 } else {
687 // We may get paths with a host prepended; strip it.
688 return preg_replace( '!^[^:]+://[^/]+/+!', '/', $base );
689 }
690 }
691
692 /**
693 * Return the request URI with the canonical service and hostname, path,
694 * and query string. This will be suitable for use as an absolute link
695 * in HTML or other output.
696 *
697 * If $wgServer is protocol-relative, this will return a fully
698 * qualified URL with the protocol that was used for this request.
699 *
700 * @return string
701 */
702 public function getFullRequestURL() {
703 return wfExpandUrl( $this->getRequestURL(), PROTO_CURRENT );
704 }
705
706 /**
707 * Take an arbitrary query and rewrite the present URL to include it
708 * @deprecated Use appendQueryValue/appendQueryArray instead
709 * @param string $query Query string fragment; do not include initial '?'
710 * @return string
711 */
712 public function appendQuery( $query ) {
713 wfDeprecated( __METHOD__, '1.25' );
714 return $this->appendQueryArray( wfCgiToArray( $query ) );
715 }
716
717 /**
718 * @param string $key
719 * @param string $value
720 * @param bool $onlyquery [deprecated]
721 * @return string
722 */
723 public function appendQueryValue( $key, $value, $onlyquery = true ) {
724 return $this->appendQueryArray( array( $key => $value ), $onlyquery );
725 }
726
727 /**
728 * Appends or replaces value of query variables.
729 *
730 * @param array $array Array of values to replace/add to query
731 * @param bool $onlyquery Whether to only return the query string and not the complete URL [deprecated]
732 * @return string
733 */
734 public function appendQueryArray( $array, $onlyquery = true ) {
735 global $wgTitle;
736 $newquery = $this->getQueryValues();
737 unset( $newquery['title'] );
738 $newquery = array_merge( $newquery, $array );
739 $query = wfArrayToCgi( $newquery );
740 if ( !$onlyquery ) {
741 wfDeprecated( __METHOD__, '1.25' );
742 return $wgTitle->getLocalURL( $query );
743 }
744
745 return $query;
746 }
747
748 /**
749 * Check for limit and offset parameters on the input, and return sensible
750 * defaults if not given. The limit must be positive and is capped at 5000.
751 * Offset must be positive but is not capped.
752 *
753 * @param int $deflimit Limit to use if no input and the user hasn't set the option.
754 * @param string $optionname To specify an option other than rclimit to pull from.
755 * @return array First element is limit, second is offset
756 */
757 public function getLimitOffset( $deflimit = 50, $optionname = 'rclimit' ) {
758 global $wgUser;
759
760 $limit = $this->getInt( 'limit', 0 );
761 if ( $limit < 0 ) {
762 $limit = 0;
763 }
764 if ( ( $limit == 0 ) && ( $optionname != '' ) ) {
765 $limit = $wgUser->getIntOption( $optionname );
766 }
767 if ( $limit <= 0 ) {
768 $limit = $deflimit;
769 }
770 if ( $limit > 5000 ) {
771 $limit = 5000; # We have *some* limits...
772 }
773
774 $offset = $this->getInt( 'offset', 0 );
775 if ( $offset < 0 ) {
776 $offset = 0;
777 }
778
779 return array( $limit, $offset );
780 }
781
782 /**
783 * Return the path to the temporary file where PHP has stored the upload.
784 *
785 * @param string $key
786 * @return string|null String or null if no such file.
787 */
788 public function getFileTempname( $key ) {
789 $file = new WebRequestUpload( $this, $key );
790 return $file->getTempName();
791 }
792
793 /**
794 * Return the upload error or 0
795 *
796 * @param string $key
797 * @return int
798 */
799 public function getUploadError( $key ) {
800 $file = new WebRequestUpload( $this, $key );
801 return $file->getError();
802 }
803
804 /**
805 * Return the original filename of the uploaded file, as reported by
806 * the submitting user agent. HTML-style character entities are
807 * interpreted and normalized to Unicode normalization form C, in part
808 * to deal with weird input from Safari with non-ASCII filenames.
809 *
810 * Other than this the name is not verified for being a safe filename.
811 *
812 * @param string $key
813 * @return string|null String or null if no such file.
814 */
815 public function getFileName( $key ) {
816 $file = new WebRequestUpload( $this, $key );
817 return $file->getName();
818 }
819
820 /**
821 * Return a WebRequestUpload object corresponding to the key
822 *
823 * @param string $key
824 * @return WebRequestUpload
825 */
826 public function getUpload( $key ) {
827 return new WebRequestUpload( $this, $key );
828 }
829
830 /**
831 * Return a handle to WebResponse style object, for setting cookies,
832 * headers and other stuff, for Request being worked on.
833 *
834 * @return WebResponse
835 */
836 public function response() {
837 /* Lazy initialization of response object for this request */
838 if ( !is_object( $this->response ) ) {
839 $class = ( $this instanceof FauxRequest ) ? 'FauxResponse' : 'WebResponse';
840 $this->response = new $class();
841 }
842 return $this->response;
843 }
844
845 /**
846 * Initialise the header list
847 */
848 private function initHeaders() {
849 if ( count( $this->headers ) ) {
850 return;
851 }
852
853 $apacheHeaders = function_exists( 'apache_request_headers' ) ? apache_request_headers() : false;
854 if ( $apacheHeaders ) {
855 foreach ( $apacheHeaders as $tempName => $tempValue ) {
856 $this->headers[strtoupper( $tempName )] = $tempValue;
857 }
858 } else {
859 foreach ( $_SERVER as $name => $value ) {
860 if ( substr( $name, 0, 5 ) === 'HTTP_' ) {
861 $name = str_replace( '_', '-', substr( $name, 5 ) );
862 $this->headers[$name] = $value;
863 } elseif ( $name === 'CONTENT_LENGTH' ) {
864 $this->headers['CONTENT-LENGTH'] = $value;
865 }
866 }
867 }
868 }
869
870 /**
871 * Get an array containing all request headers
872 *
873 * @return array Mapping header name to its value
874 */
875 public function getAllHeaders() {
876 $this->initHeaders();
877 return $this->headers;
878 }
879
880 /**
881 * Get a request header, or false if it isn't set
882 * @param string $name Case-insensitive header name
883 *
884 * @return string|bool False on failure
885 */
886 public function getHeader( $name ) {
887 $this->initHeaders();
888 $name = strtoupper( $name );
889 if ( isset( $this->headers[$name] ) ) {
890 return $this->headers[$name];
891 } else {
892 return false;
893 }
894 }
895
896 /**
897 * Get data from $_SESSION
898 *
899 * @param string $key Name of key in $_SESSION
900 * @return mixed
901 */
902 public function getSessionData( $key ) {
903 if ( !isset( $_SESSION[$key] ) ) {
904 return null;
905 }
906 return $_SESSION[$key];
907 }
908
909 /**
910 * Set session data
911 *
912 * @param string $key Name of key in $_SESSION
913 * @param mixed $data
914 */
915 public function setSessionData( $key, $data ) {
916 $_SESSION[$key] = $data;
917 }
918
919 /**
920 * Check if Internet Explorer will detect an incorrect cache extension in
921 * PATH_INFO or QUERY_STRING. If the request can't be allowed, show an error
922 * message or redirect to a safer URL. Returns true if the URL is OK, and
923 * false if an error message has been shown and the request should be aborted.
924 *
925 * @param array $extWhitelist
926 * @throws HttpError
927 * @return bool
928 */
929 public function checkUrlExtension( $extWhitelist = array() ) {
930 global $wgScriptExtension;
931 $extWhitelist[] = ltrim( $wgScriptExtension, '.' );
932 if ( IEUrlExtension::areServerVarsBad( $_SERVER, $extWhitelist ) ) {
933 if ( !$this->wasPosted() ) {
934 $newUrl = IEUrlExtension::fixUrlForIE6(
935 $this->getFullRequestURL(), $extWhitelist );
936 if ( $newUrl !== false ) {
937 $this->doSecurityRedirect( $newUrl );
938 return false;
939 }
940 }
941 throw new HttpError( 403,
942 'Invalid file extension found in the path info or query string.' );
943 }
944 return true;
945 }
946
947 /**
948 * Attempt to redirect to a URL with a QUERY_STRING that's not dangerous in
949 * IE 6. Returns true if it was successful, false otherwise.
950 *
951 * @param string $url
952 * @return bool
953 */
954 protected function doSecurityRedirect( $url ) {
955 header( 'Location: ' . $url );
956 header( 'Content-Type: text/html' );
957 $encUrl = htmlspecialchars( $url );
958 echo <<<HTML
959 <html>
960 <head>
961 <title>Security redirect</title>
962 </head>
963 <body>
964 <h1>Security redirect</h1>
965 <p>
966 We can't serve non-HTML content from the URL you have requested, because
967 Internet Explorer would interpret it as an incorrect and potentially dangerous
968 content type.</p>
969 <p>Instead, please use <a href="$encUrl">this URL</a>, which is the same as the
970 URL you have requested, except that "&amp;*" is appended. This prevents Internet
971 Explorer from seeing a bogus file extension.
972 </p>
973 </body>
974 </html>
975 HTML;
976 echo "\n";
977 return true;
978 }
979
980 /**
981 * Parse the Accept-Language header sent by the client into an array
982 *
983 * @return array Array( languageCode => q-value ) sorted by q-value in
984 * descending order then appearing time in the header in ascending order.
985 * May contain the "language" '*', which applies to languages other than those explicitly listed.
986 * This is aligned with rfc2616 section 14.4
987 * Preference for earlier languages appears in rfc3282 as an extension to HTTP/1.1.
988 */
989 public function getAcceptLang() {
990 // Modified version of code found at
991 // http://www.thefutureoftheweb.com/blog/use-accept-language-header
992 $acceptLang = $this->getHeader( 'Accept-Language' );
993 if ( !$acceptLang ) {
994 return array();
995 }
996
997 // Return the language codes in lower case
998 $acceptLang = strtolower( $acceptLang );
999
1000 // Break up string into pieces (languages and q factors)
1001 $lang_parse = null;
1002 preg_match_all(
1003 '/([a-z]{1,8}(-[a-z]{1,8})*|\*)\s*(;\s*q\s*=\s*(1(\.0{0,3})?|0(\.[0-9]{0,3})?)?)?/',
1004 $acceptLang,
1005 $lang_parse
1006 );
1007
1008 if ( !count( $lang_parse[1] ) ) {
1009 return array();
1010 }
1011
1012 $langcodes = $lang_parse[1];
1013 $qvalues = $lang_parse[4];
1014 $indices = range( 0, count( $lang_parse[1] ) - 1 );
1015
1016 // Set default q factor to 1
1017 foreach ( $indices as $index ) {
1018 if ( $qvalues[$index] === '' ) {
1019 $qvalues[$index] = 1;
1020 } elseif ( $qvalues[$index] == 0 ) {
1021 unset( $langcodes[$index], $qvalues[$index], $indices[$index] );
1022 }
1023 }
1024
1025 // Sort list. First by $qvalues, then by order. Reorder $langcodes the same way
1026 array_multisort( $qvalues, SORT_DESC, SORT_NUMERIC, $indices, $langcodes );
1027
1028 // Create a list like "en" => 0.8
1029 $langs = array_combine( $langcodes, $qvalues );
1030
1031 return $langs;
1032 }
1033
1034 /**
1035 * Fetch the raw IP from the request
1036 *
1037 * @since 1.19
1038 *
1039 * @throws MWException
1040 * @return string
1041 */
1042 protected function getRawIP() {
1043 if ( !isset( $_SERVER['REMOTE_ADDR'] ) ) {
1044 return null;
1045 }
1046
1047 if ( is_array( $_SERVER['REMOTE_ADDR'] ) || strpos( $_SERVER['REMOTE_ADDR'], ',' ) !== false ) {
1048 throw new MWException( __METHOD__
1049 . " : Could not determine the remote IP address due to multiple values." );
1050 } else {
1051 $ipchain = $_SERVER['REMOTE_ADDR'];
1052 }
1053
1054 return IP::canonicalize( $ipchain );
1055 }
1056
1057 /**
1058 * Work out the IP address based on various globals
1059 * For trusted proxies, use the XFF client IP (first of the chain)
1060 *
1061 * @since 1.19
1062 *
1063 * @throws MWException
1064 * @return string
1065 */
1066 public function getIP() {
1067 global $wgUsePrivateIPs;
1068
1069 # Return cached result
1070 if ( $this->ip !== null ) {
1071 return $this->ip;
1072 }
1073
1074 # collect the originating ips
1075 $ip = $this->getRawIP();
1076 if ( !$ip ) {
1077 throw new MWException( 'Unable to determine IP.' );
1078 }
1079
1080 # Append XFF
1081 $forwardedFor = $this->getHeader( 'X-Forwarded-For' );
1082 if ( $forwardedFor !== false ) {
1083 $isConfigured = IP::isConfiguredProxy( $ip );
1084 $ipchain = array_map( 'trim', explode( ',', $forwardedFor ) );
1085 $ipchain = array_reverse( $ipchain );
1086 array_unshift( $ipchain, $ip );
1087
1088 # Step through XFF list and find the last address in the list which is a
1089 # trusted server. Set $ip to the IP address given by that trusted server,
1090 # unless the address is not sensible (e.g. private). However, prefer private
1091 # IP addresses over proxy servers controlled by this site (more sensible).
1092 # Note that some XFF values might be "unknown" with Squid/Varnish.
1093 foreach ( $ipchain as $i => $curIP ) {
1094 $curIP = IP::sanitizeIP( IP::canonicalize( $curIP ) );
1095 if ( !$curIP || !isset( $ipchain[$i + 1] ) || $ipchain[$i + 1] === 'unknown'
1096 || !IP::isTrustedProxy( $curIP )
1097 ) {
1098 break; // IP is not valid/trusted or does not point to anything
1099 }
1100 if (
1101 IP::isPublic( $ipchain[$i + 1] ) ||
1102 $wgUsePrivateIPs ||
1103 IP::isConfiguredProxy( $curIP ) // bug 48919; treat IP as sane
1104 ) {
1105 // Follow the next IP according to the proxy
1106 $nextIP = IP::canonicalize( $ipchain[$i + 1] );
1107 if ( !$nextIP && $isConfigured ) {
1108 // We have not yet made it past CDN/proxy servers of this site,
1109 // so either they are misconfigured or there is some IP spoofing.
1110 throw new MWException( "Invalid IP given in XFF '$forwardedFor'." );
1111 }
1112 $ip = $nextIP;
1113 // keep traversing the chain
1114 continue;
1115 }
1116 break;
1117 }
1118 }
1119
1120 # Allow extensions to improve our guess
1121 wfRunHooks( 'GetIP', array( &$ip ) );
1122
1123 if ( !$ip ) {
1124 throw new MWException( "Unable to determine IP." );
1125 }
1126
1127 wfDebug( "IP: $ip\n" );
1128 $this->ip = $ip;
1129 return $ip;
1130 }
1131
1132 /**
1133 * @param string $ip
1134 * @return void
1135 * @since 1.21
1136 */
1137 public function setIP( $ip ) {
1138 $this->ip = $ip;
1139 }
1140 }
1141
1142 /**
1143 * Object to access the $_FILES array
1144 */
1145 class WebRequestUpload {
1146 protected $request;
1147 protected $doesExist;
1148 protected $fileInfo;
1149
1150 /**
1151 * Constructor. Should only be called by WebRequest
1152 *
1153 * @param WebRequest $request The associated request
1154 * @param string $key Key in $_FILES array (name of form field)
1155 */
1156 public function __construct( $request, $key ) {
1157 $this->request = $request;
1158 $this->doesExist = isset( $_FILES[$key] );
1159 if ( $this->doesExist ) {
1160 $this->fileInfo = $_FILES[$key];
1161 }
1162 }
1163
1164 /**
1165 * Return whether a file with this name was uploaded.
1166 *
1167 * @return bool
1168 */
1169 public function exists() {
1170 return $this->doesExist;
1171 }
1172
1173 /**
1174 * Return the original filename of the uploaded file
1175 *
1176 * @return string|null Filename or null if non-existent
1177 */
1178 public function getName() {
1179 if ( !$this->exists() ) {
1180 return null;
1181 }
1182
1183 global $wgContLang;
1184 $name = $this->fileInfo['name'];
1185
1186 # Safari sends filenames in HTML-encoded Unicode form D...
1187 # Horrid and evil! Let's try to make some kind of sense of it.
1188 $name = Sanitizer::decodeCharReferences( $name );
1189 $name = $wgContLang->normalize( $name );
1190 wfDebug( __METHOD__ . ": {$this->fileInfo['name']} normalized to '$name'\n" );
1191 return $name;
1192 }
1193
1194 /**
1195 * Return the file size of the uploaded file
1196 *
1197 * @return int File size or zero if non-existent
1198 */
1199 public function getSize() {
1200 if ( !$this->exists() ) {
1201 return 0;
1202 }
1203
1204 return $this->fileInfo['size'];
1205 }
1206
1207 /**
1208 * Return the path to the temporary file
1209 *
1210 * @return string|null Path or null if non-existent
1211 */
1212 public function getTempName() {
1213 if ( !$this->exists() ) {
1214 return null;
1215 }
1216
1217 return $this->fileInfo['tmp_name'];
1218 }
1219
1220 /**
1221 * Return the upload error. See link for explanation
1222 * http://www.php.net/manual/en/features.file-upload.errors.php
1223 *
1224 * @return int One of the UPLOAD_ constants, 0 if non-existent
1225 */
1226 public function getError() {
1227 if ( !$this->exists() ) {
1228 return 0; # UPLOAD_ERR_OK
1229 }
1230
1231 return $this->fileInfo['error'];
1232 }
1233
1234 /**
1235 * Returns whether this upload failed because of overflow of a maximum set
1236 * in php.ini
1237 *
1238 * @return bool
1239 */
1240 public function isIniSizeOverflow() {
1241 if ( $this->getError() == UPLOAD_ERR_INI_SIZE ) {
1242 # PHP indicated that upload_max_filesize is exceeded
1243 return true;
1244 }
1245
1246 $contentLength = $this->request->getHeader( 'CONTENT_LENGTH' );
1247 if ( $contentLength > wfShorthandToInteger( ini_get( 'post_max_size' ) ) ) {
1248 # post_max_size is exceeded
1249 return true;
1250 }
1251
1252 return false;
1253 }
1254 }
1255
1256 /**
1257 * WebRequest clone which takes values from a provided array.
1258 *
1259 * @ingroup HTTP
1260 */
1261 class FauxRequest extends WebRequest {
1262 private $wasPosted = false;
1263 private $session = array();
1264 private $requestUrl;
1265
1266 /**
1267 * @param array $data Array of *non*-urlencoded key => value pairs, the
1268 * fake GET/POST values
1269 * @param bool $wasPosted Whether to treat the data as POST
1270 * @param array|null $session Session array or null
1271 * @param string $protocol 'http' or 'https'
1272 * @throws MWException
1273 */
1274 public function __construct( $data = array(), $wasPosted = false,
1275 $session = null, $protocol = 'http'
1276 ) {
1277 if ( is_array( $data ) ) {
1278 $this->data = $data;
1279 } else {
1280 throw new MWException( "FauxRequest() got bogus data" );
1281 }
1282 $this->wasPosted = $wasPosted;
1283 if ( $session ) {
1284 $this->session = $session;
1285 }
1286 $this->protocol = $protocol;
1287 }
1288
1289 /**
1290 * @param string $method
1291 * @throws MWException
1292 */
1293 private function notImplemented( $method ) {
1294 throw new MWException( "{$method}() not implemented" );
1295 }
1296
1297 /**
1298 * @param string $name
1299 * @param string $default
1300 * @return string
1301 */
1302 public function getText( $name, $default = '' ) {
1303 # Override; don't recode since we're using internal data
1304 return (string)$this->getVal( $name, $default );
1305 }
1306
1307 /**
1308 * @return array
1309 */
1310 public function getValues() {
1311 return $this->data;
1312 }
1313
1314 /**
1315 * @return array
1316 */
1317 public function getQueryValues() {
1318 if ( $this->wasPosted ) {
1319 return array();
1320 } else {
1321 return $this->data;
1322 }
1323 }
1324
1325 public function getMethod() {
1326 return $this->wasPosted ? 'POST' : 'GET';
1327 }
1328
1329 /**
1330 * @return bool
1331 */
1332 public function wasPosted() {
1333 return $this->wasPosted;
1334 }
1335
1336 public function getCookie( $key, $prefix = null, $default = null ) {
1337 return $default;
1338 }
1339
1340 public function checkSessionCookie() {
1341 return false;
1342 }
1343
1344 public function setRequestURL( $url ) {
1345 $this->requestUrl = $url;
1346 }
1347
1348 public function getRequestURL() {
1349 if ( $this->requestUrl === null ) {
1350 throw new MWException( 'Request URL not set' );
1351 }
1352 return $this->requestUrl;
1353 }
1354
1355 public function getProtocol() {
1356 return $this->protocol;
1357 }
1358
1359 /**
1360 * @param string $name The name of the header to get (case insensitive).
1361 * @return bool|string
1362 */
1363 public function getHeader( $name ) {
1364 $name = strtoupper( $name );
1365 return isset( $this->headers[$name] ) ? $this->headers[$name] : false;
1366 }
1367
1368 /**
1369 * @param string $name
1370 * @param string $val
1371 */
1372 public function setHeader( $name, $val ) {
1373 $name = strtoupper( $name );
1374 $this->headers[$name] = $val;
1375 }
1376
1377 /**
1378 * @param string $key
1379 * @return array|null
1380 */
1381 public function getSessionData( $key ) {
1382 if ( isset( $this->session[$key] ) ) {
1383 return $this->session[$key];
1384 }
1385 return null;
1386 }
1387
1388 /**
1389 * @param string $key
1390 * @param array $data
1391 */
1392 public function setSessionData( $key, $data ) {
1393 $this->session[$key] = $data;
1394 }
1395
1396 /**
1397 * @return array|mixed|null
1398 */
1399 public function getSessionArray() {
1400 return $this->session;
1401 }
1402
1403 /**
1404 * FauxRequests shouldn't depend on raw request data (but that could be implemented here)
1405 * @return string
1406 */
1407 public function getRawQueryString() {
1408 return '';
1409 }
1410
1411 /**
1412 * FauxRequests shouldn't depend on raw request data (but that could be implemented here)
1413 * @return string
1414 */
1415 public function getRawPostString() {
1416 return '';
1417 }
1418
1419 /**
1420 * FauxRequests shouldn't depend on raw request data (but that could be implemented here)
1421 * @return string
1422 */
1423 public function getRawInput() {
1424 return '';
1425 }
1426
1427 /**
1428 * @param array $extWhitelist
1429 * @return bool
1430 */
1431 public function checkUrlExtension( $extWhitelist = array() ) {
1432 return true;
1433 }
1434
1435 /**
1436 * @return string
1437 */
1438 protected function getRawIP() {
1439 return '127.0.0.1';
1440 }
1441 }
1442
1443 /**
1444 * Similar to FauxRequest, but only fakes URL parameters and method
1445 * (POST or GET) and use the base request for the remaining stuff
1446 * (cookies, session and headers).
1447 *
1448 * @ingroup HTTP
1449 * @since 1.19
1450 */
1451 class DerivativeRequest extends FauxRequest {
1452 private $base;
1453
1454 /**
1455 * @param WebRequest $base
1456 * @param array $data Array of *non*-urlencoded key => value pairs, the
1457 * fake GET/POST values
1458 * @param bool $wasPosted Whether to treat the data as POST
1459 */
1460 public function __construct( WebRequest $base, $data, $wasPosted = false ) {
1461 $this->base = $base;
1462 parent::__construct( $data, $wasPosted );
1463 }
1464
1465 public function getCookie( $key, $prefix = null, $default = null ) {
1466 return $this->base->getCookie( $key, $prefix, $default );
1467 }
1468
1469 public function checkSessionCookie() {
1470 return $this->base->checkSessionCookie();
1471 }
1472
1473 public function getHeader( $name ) {
1474 return $this->base->getHeader( $name );
1475 }
1476
1477 public function getAllHeaders() {
1478 return $this->base->getAllHeaders();
1479 }
1480
1481 public function getSessionData( $key ) {
1482 return $this->base->getSessionData( $key );
1483 }
1484
1485 public function setSessionData( $key, $data ) {
1486 $this->base->setSessionData( $key, $data );
1487 }
1488
1489 public function getAcceptLang() {
1490 return $this->base->getAcceptLang();
1491 }
1492
1493 public function getIP() {
1494 return $this->base->getIP();
1495 }
1496
1497 public function getProtocol() {
1498 return $this->base->getProtocol();
1499 }
1500 }