3 * Deal with importing all those nasssty globals and things
7 # Copyright (C) 2003 Brion Vibber <brion@pobox.com>
8 # http://www.mediawiki.org/
10 # This program is free software; you can redistribute it and/or modify
11 # it under the terms of the GNU General Public License as published by
12 # the Free Software Foundation; either version 2 of the License, or
13 # (at your option) any later version.
15 # This program is distributed in the hope that it will be useful,
16 # but WITHOUT ANY WARRANTY; without even the implied warranty of
17 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 # GNU General Public License for more details.
20 # You should have received a copy of the GNU General Public License along
21 # with this program; if not, write to the Free Software Foundation, Inc.,
22 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
23 # http://www.gnu.org/copyleft/gpl.html
26 * The WebRequest class encapsulates getting at data passed in the
27 * URL or via a POSTed form, handling remove of "magic quotes" slashes,
28 * stripping illegal input characters and normalizing Unicode sequences.
30 * Usually this is used via a global singleton, $wgRequest. You should
31 * not create a second WebRequest object; make a FauxRequest object if
32 * you want to pass arbitrary data to some function in place of the web
39 * Some entry points may use this file without first enabling the
42 if ( !function_exists( '__autoload' ) ) {
43 require_once( dirname(__FILE__
) . '/normal/UtfNormal.php' );
47 function WebRequest() {
48 $this->checkMagicQuotes();
49 global $wgUsePathInfo;
50 if ( $wgUsePathInfo ) {
51 if ( isset( $_SERVER['ORIG_PATH_INFO'] ) && $_SERVER['ORIG_PATH_INFO'] != '' ) {
53 # http://bugs.php.net/bug.php?id=31892
54 # Also reported when ini_get('cgi.fix_pathinfo')==false
55 $_GET['title'] = $_REQUEST['title'] = substr( $_SERVER['ORIG_PATH_INFO'], 1 );
56 } elseif ( isset( $_SERVER['PATH_INFO'] ) && ($_SERVER['PATH_INFO'] != '') && $wgUsePathInfo ) {
57 $_GET['title'] = $_REQUEST['title'] = substr( $_SERVER['PATH_INFO'], 1 );
65 * Recursively strips slashes from the given array;
66 * used for undoing the evil that is magic_quotes_gpc.
67 * @param array &$arr will be modified
68 * @return array the original array
71 function &fix_magic_quotes( &$arr ) {
72 foreach( $arr as $key => $val ) {
73 if( is_array( $val ) ) {
74 $this->fix_magic_quotes( $arr[$key] );
76 $arr[$key] = stripslashes( $val );
83 * If magic_quotes_gpc option is on, run the global arrays
84 * through fix_magic_quotes to strip out the stupid slashes.
85 * WARNING: This should only be done once! Running a second
86 * time could damage the values.
89 function checkMagicQuotes() {
90 if ( get_magic_quotes_gpc() ) {
91 $this->fix_magic_quotes( $_COOKIE );
92 $this->fix_magic_quotes( $_ENV );
93 $this->fix_magic_quotes( $_GET );
94 $this->fix_magic_quotes( $_POST );
95 $this->fix_magic_quotes( $_REQUEST );
96 $this->fix_magic_quotes( $_SERVER );
101 * Recursively normalizes UTF-8 strings in the given array.
102 * @param array $data string or array
103 * @return cleaned-up version of the given
106 function normalizeUnicode( $data ) {
107 if( is_array( $data ) ) {
108 foreach( $data as $key => $val ) {
109 $data[$key] = $this->normalizeUnicode( $val );
112 $data = UtfNormal
::cleanUp( $data );
118 * Fetch a value from the given array or return $default if it's not set.
121 * @param string $name
122 * @param mixed $default
126 function getGPCVal( $arr, $name, $default ) {
127 if( isset( $arr[$name] ) ) {
130 if( isset( $_GET[$name] ) && !is_array( $data ) ) {
131 # Check for alternate/legacy character encoding.
132 if( isset( $wgContLang ) ) {
133 $data = $wgContLang->checkTitleEncoding( $data );
136 $data = $this->normalizeUnicode( $data );
144 * Fetch a scalar from the input or return $default if it's not set.
145 * Returns a string. Arrays are discarded.
147 * @param string $name
148 * @param string $default optional default (or NULL)
151 function getVal( $name, $default = NULL ) {
152 $val = $this->getGPCVal( $_REQUEST, $name, $default );
153 if( is_array( $val ) ) {
156 if( is_null( $val ) ) {
164 * Fetch an array from the input or return $default if it's not set.
165 * If source was scalar, will return an array with a single element.
166 * If no source and no default, returns NULL.
168 * @param string $name
169 * @param array $default optional default (or NULL)
172 function getArray( $name, $default = NULL ) {
173 $val = $this->getGPCVal( $_REQUEST, $name, $default );
174 if( is_null( $val ) ) {
182 * Fetch an array of integers, or return $default if it's not set.
183 * If source was scalar, will return an array with a single element.
184 * If no source and no default, returns NULL.
185 * If an array is returned, contents are guaranteed to be integers.
187 * @param string $name
188 * @param array $default option default (or NULL)
189 * @return array of ints
191 function getIntArray( $name, $default = NULL ) {
192 $val = $this->getArray( $name, $default );
193 if( is_array( $val ) ) {
194 $val = array_map( 'intval', $val );
200 * Fetch an integer value from the input or return $default if not set.
201 * Guaranteed to return an integer; non-numeric input will typically
203 * @param string $name
204 * @param int $default
207 function getInt( $name, $default = 0 ) {
208 return intval( $this->getVal( $name, $default ) );
212 * Fetch an integer value from the input or return null if empty.
213 * Guaranteed to return an integer or null; non-numeric input will
214 * typically return null.
215 * @param string $name
218 function getIntOrNull( $name ) {
219 $val = $this->getVal( $name );
220 return is_numeric( $val )
226 * Fetch a boolean value from the input or return $default if not set.
227 * Guaranteed to return true or false, with normal PHP semantics for
228 * boolean interpretation of strings.
229 * @param string $name
230 * @param bool $default
233 function getBool( $name, $default = false ) {
234 return $this->getVal( $name, $default ) ?
true : false;
238 * Return true if the named value is set in the input, whatever that
239 * value is (even "0"). Return false if the named value is not set.
240 * Example use is checking for the presence of check boxes in forms.
241 * @param string $name
244 function getCheck( $name ) {
245 # Checkboxes and buttons are only present when clicked
246 # Presence connotes truth, abscense false
247 $val = $this->getVal( $name, NULL );
248 return isset( $val );
252 * Fetch a text string from the given array or return $default if it's not
253 * set. \r is stripped from the text, and with some language modules there
254 * is an input transliteration applied. This should generally be used for
255 * form <textarea> and <input> fields.
257 * @param string $name
258 * @param string $default optional
261 function getText( $name, $default = '' ) {
263 $val = $this->getVal( $name, $default );
264 return str_replace( "\r\n", "\n",
265 $wgContLang->recodeInput( $val ) );
269 * Extracts the given named values into an array.
270 * If no arguments are given, returns all input values.
271 * No transformation is performed on the values.
273 function getValues() {
274 $names = func_get_args();
275 if ( count( $names ) == 0 ) {
276 $names = array_keys( $_REQUEST );
280 foreach ( $names as $name ) {
281 $value = $this->getVal( $name );
282 if ( !is_null( $value ) ) {
283 $retVal[$name] = $value;
290 * Returns true if the present request was reached by a POST operation,
291 * false otherwise (GET, HEAD, or command-line).
293 * Note that values retrieved by the object may come from the
294 * GET URL etc even on a POST request.
298 function wasPosted() {
299 return $_SERVER['REQUEST_METHOD'] == 'POST';
303 * Returns true if there is a session cookie set.
304 * This does not necessarily mean that the user is logged in!
308 function checkSessionCookie() {
309 return isset( $_COOKIE[ini_get('session.name')] );
313 * Return the path portion of the request URI.
316 function getRequestURL() {
317 $base = $_SERVER['REQUEST_URI'];
318 if( $base{0} == '/' ) {
321 // We may get paths with a host prepended; strip it.
322 return preg_replace( '!^[^:]+://[^/]+/!', '/', $base );
327 * Return the request URI with the canonical service and hostname.
330 function getFullRequestURL() {
332 return $wgServer . $this->getRequestURL();
336 * Take an arbitrary query and rewrite the present URL to include it
337 * @param $query String: query string fragment; do not include initial '?'
340 function appendQuery( $query ) {
343 foreach( $_GET as $var => $val ) {
344 if ( $var == 'title' )
346 if ( is_array( $val ) )
347 /* This will happen given a request like
348 * http://en.wikipedia.org/w/index.php?title[]=Special:Userlogin&returnto[]=Main_Page
351 $basequery .= '&' . urlencode( $var ) . '=' . urlencode( $val );
353 $basequery .= '&' . $query;
356 $basequery = substr( $basequery, 1 );
357 return $wgTitle->getLocalURL( $basequery );
361 * HTML-safe version of appendQuery().
362 * @param $query String: query string fragment; do not include initial '?'
365 function escapeAppendQuery( $query ) {
366 return htmlspecialchars( $this->appendQuery( $query ) );
370 * Check for limit and offset parameters on the input, and return sensible
371 * defaults if not given. The limit must be positive and is capped at 5000.
372 * Offset must be positive but is not capped.
374 * @param $deflimit Integer: limit to use if no input and the user hasn't set the option.
375 * @param $optionname String: to specify an option other than rclimit to pull from.
376 * @return array first element is limit, second is offset
378 function getLimitOffset( $deflimit = 50, $optionname = 'rclimit' ) {
381 $limit = $this->getInt( 'limit', 0 );
382 if( $limit < 0 ) $limit = 0;
383 if( ( $limit == 0 ) && ( $optionname != '' ) ) {
384 $limit = (int)$wgUser->getOption( $optionname );
386 if( $limit <= 0 ) $limit = $deflimit;
387 if( $limit > 5000 ) $limit = 5000; # We have *some* limits...
389 $offset = $this->getInt( 'offset', 0 );
390 if( $offset < 0 ) $offset = 0;
392 return array( $limit, $offset );
396 * Return the path to the temporary file where PHP has stored the upload.
397 * @param $key String:
398 * @return string or NULL if no such file.
400 function getFileTempname( $key ) {
401 if( !isset( $_FILES[$key] ) ) {
404 return $_FILES[$key]['tmp_name'];
408 * Return the size of the upload, or 0.
409 * @param $key String:
412 function getFileSize( $key ) {
413 if( !isset( $_FILES[$key] ) ) {
416 return $_FILES[$key]['size'];
420 * Return the upload error or 0
421 * @param $key String:
424 function getUploadError( $key ) {
425 if( !isset( $_FILES[$key] ) ||
!isset( $_FILES[$key]['error'] ) ) {
426 return 0/*UPLOAD_ERR_OK*/;
428 return $_FILES[$key]['error'];
432 * Return the original filename of the uploaded file, as reported by
433 * the submitting user agent. HTML-style character entities are
434 * interpreted and normalized to Unicode normalization form C, in part
435 * to deal with weird input from Safari with non-ASCII filenames.
437 * Other than this the name is not verified for being a safe filename.
439 * @param $key String:
440 * @return string or NULL if no such file.
442 function getFileName( $key ) {
443 if( !isset( $_FILES[$key] ) ) {
446 $name = $_FILES[$key]['name'];
448 # Safari sends filenames in HTML-encoded Unicode form D...
449 # Horrid and evil! Let's try to make some kind of sense of it.
450 $name = Sanitizer
::decodeCharReferences( $name );
451 $name = UtfNormal
::cleanUp( $name );
452 wfDebug( "WebRequest::getFileName() '" . $_FILES[$key]['name'] . "' normalized to '$name'\n" );
457 * Return a handle to WebResponse style object, for setting cookies,
458 * headers and other stuff, for Request being worked on.
460 function response() {
461 /* Lazy initialization of response object for this request */
462 if (!is_object($this->_response
)) {
463 $this->_response
= new WebResponse
;
465 return $this->_response
;
471 * WebRequest clone which takes values from a provided array.
475 class FauxRequest
extends WebRequest
{
477 var $wasPosted = false;
479 function FauxRequest( $data, $wasPosted = false ) {
480 if( is_array( $data ) ) {
483 throw new MWException( "FauxRequest() got bogus data" );
485 $this->wasPosted
= $wasPosted;
488 function getVal( $name, $default = NULL ) {
489 return $this->getGPCVal( $this->data
, $name, $default );
492 function getText( $name, $default = '' ) {
493 # Override; don't recode since we're using internal data
494 return $this->getVal( $name, $default );
497 function getValues() {
501 function wasPosted() {
502 return $this->wasPosted
;
505 function checkSessionCookie() {
509 function getRequestURL() {
510 throw new MWException( 'FauxRequest::getRequestURL() not implemented' );
513 function appendQuery( $query ) {
514 throw new MWException( 'FauxRequest::appendQuery() not implemented' );