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