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