getThumbnail() may legitimately return null
[mediawiki.git] / includes / WebRequest.php
blob7e3a583040d4a54cbd0872d0a55ef1ec86e50e79
1 <?php
2 /**
3 * Deal with importing all those nasssty globals and things
4 * @package MediaWiki
5 */
7 # Copyright (C) 2003 Brion Vibber <brion@pobox.com>
8 # http://www.mediawiki.org/
9 #
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 # 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
23 # http://www.gnu.org/copyleft/gpl.html
25 /**
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
33 * input.
35 * @package MediaWiki
37 class WebRequest {
38 function WebRequest() {
39 $this->checkMagicQuotes();
40 global $wgUsePathInfo;
41 if( isset( $_SERVER['PATH_INFO'] ) && ($_SERVER['PATH_INFO'] != '') && $wgUsePathInfo ) {
42 # Stuff it!
43 $_GET['title'] = $_REQUEST['title'] =
44 substr( $_SERVER['PATH_INFO'], 1 );
48 /**
49 * Recursively strips slashes from the given array;
50 * used for undoing the evil that is magic_quotes_gpc.
51 * @param array &$arr will be modified
52 * @return array the original array
53 * @private
55 function &fix_magic_quotes( &$arr ) {
56 foreach( $arr as $key => $val ) {
57 if( is_array( $val ) ) {
58 $this->fix_magic_quotes( $arr[$key] );
59 } else {
60 $arr[$key] = stripslashes( $val );
63 return $arr;
66 /**
67 * If magic_quotes_gpc option is on, run the global arrays
68 * through fix_magic_quotes to strip out the stupid dlashes.
69 * WARNING: This should only be done once! Running a second
70 * time could damage the values.
71 * @private
73 function checkMagicQuotes() {
74 if ( get_magic_quotes_gpc() ) {
75 $this->fix_magic_quotes( $_COOKIE );
76 $this->fix_magic_quotes( $_ENV );
77 $this->fix_magic_quotes( $_GET );
78 $this->fix_magic_quotes( $_POST );
79 $this->fix_magic_quotes( $_REQUEST );
80 $this->fix_magic_quotes( $_SERVER );
84 /**
85 * Recursively normalizes UTF-8 strings in the given array.
86 * @param array $data string or array
87 * @return cleaned-up version of the given
88 * @private
90 function normalizeUnicode( $data ) {
91 if( is_array( $data ) ) {
92 foreach( $data as $key => $val ) {
93 $data[$key] = $this->normalizeUnicode( $val );
95 } else {
96 $data = UtfNormal::cleanUp( $data );
98 return $data;
102 * Fetch a value from the given array or return $default if it's not set.
104 * @param array &$arr
105 * @param string $name
106 * @param mixed $default
107 * @return mixed
108 * @private
110 function getGPCVal( &$arr, $name, $default ) {
111 if( isset( $arr[$name] ) ) {
112 global $wgServer, $wgContLang;
113 $data = $arr[$name];
114 if( isset( $_GET[$name] ) && !is_array( $data ) ) {
115 # Check for alternate/legacy character encoding.
116 if( isset( $wgContLang ) ) {
117 $data = $wgContLang->checkTitleEncoding( $data );
120 require_once( 'normal/UtfNormal.php' );
121 $data = $this->normalizeUnicode( $data );
122 return $data;
123 } else {
124 return $default;
129 * Fetch a scalar from the input or return $default if it's not set.
130 * Returns a string. Arrays are discarded.
132 * @param string $name
133 * @param string $default optional default (or NULL)
134 * @return string
136 function getVal( $name, $default = NULL ) {
137 $val = $this->getGPCVal( $_REQUEST, $name, $default );
138 if( is_array( $val ) ) {
139 $val = $default;
141 if( is_null( $val ) ) {
142 return null;
143 } else {
144 return (string)$val;
149 * Fetch an array from the input or return $default if it's not set.
150 * If source was scalar, will return an array with a single element.
151 * If no source and no default, returns NULL.
153 * @param string $name
154 * @param array $default optional default (or NULL)
155 * @return array
157 function getArray( $name, $default = NULL ) {
158 $val = $this->getGPCVal( $_REQUEST, $name, $default );
159 if( is_null( $val ) ) {
160 return null;
161 } else {
162 return (array)$val;
167 * Fetch an integer value from the input or return $default if not set.
168 * Guaranteed to return an integer; non-numeric input will typically
169 * return 0.
170 * @param string $name
171 * @param int $default
172 * @return int
174 function getInt( $name, $default = 0 ) {
175 return IntVal( $this->getVal( $name, $default ) );
179 * Fetch a boolean value from the input or return $default if not set.
180 * Guaranteed to return true or false, with normal PHP semantics for
181 * boolean interpretation of strings.
182 * @param string $name
183 * @param bool $default
184 * @return bool
186 function getBool( $name, $default = false ) {
187 return $this->getVal( $name, $default ) ? true : false;
191 * Return true if the named value is set in the input, whatever that
192 * value is (even "0"). Return false if the named value is not set.
193 * Example use is checking for the presence of check boxes in forms.
194 * @param string $name
195 * @return bool
197 function getCheck( $name ) {
198 # Checkboxes and buttons are only present when clicked
199 # Presence connotes truth, abscense false
200 $val = $this->getVal( $name, NULL );
201 return isset( $val );
205 * Fetch a text string from the given array or return $default if it's not
206 * set. \r is stripped from the text, and with some language modules there
207 * is an input transliteration applied. This should generally be used for
208 * form <textarea> and <input> fields.
210 * @param string $name
211 * @param string $default optional
212 * @return string
214 function getText( $name, $default = '' ) {
215 global $wgContLang;
216 $val = $this->getVal( $name, $default );
217 return str_replace( "\r\n", "\n",
218 $wgContLang->recodeInput( $val ) );
222 * Extracts the given named values into an array.
223 * If no arguments are given, returns all input values.
224 * No transformation is performed on the values.
226 function getValues() {
227 $names = func_get_args();
228 if ( count( $names ) == 0 ) {
229 $names = array_keys( $_REQUEST );
232 $retVal = array();
233 foreach ( $names as $name ) {
234 $value = $this->getVal( $name );
235 if ( !is_null( $value ) ) {
236 $retVal[$name] = $value;
239 return $retVal;
243 * Returns true if the present request was reached by a POST operation,
244 * false otherwise (GET, HEAD, or command-line).
246 * Note that values retrieved by the object may come from the
247 * GET URL etc even on a POST request.
249 * @return bool
251 function wasPosted() {
252 return $_SERVER['REQUEST_METHOD'] == 'POST';
256 * Returns true if there is a session cookie set.
257 * This does not necessarily mean that the user is logged in!
259 * @return bool
261 function checkSessionCookie() {
262 return isset( $_COOKIE[ini_get('session.name')] );
266 * Return the path portion of the request URI.
267 * @return string
269 function getRequestURL() {
270 return $_SERVER['REQUEST_URI'];
274 * Return the request URI with the canonical service and hostname.
275 * @return string
277 function getFullRequestURL() {
278 global $wgServer;
279 return $wgServer . $this->getRequestURL();
283 * Take an arbitrary query and rewrite the present URL to include it
284 * @param string $query Query string fragment; do not include initial '?'
285 * @return string
287 function appendQuery( $query ) {
288 global $wgTitle;
289 $basequery = '';
290 foreach( $_GET as $var => $val ) {
291 if( $var == 'title' ) continue;
292 $basequery .= '&' . urlencode( $var ) . '=' . urlencode( $val );
294 $basequery .= '&' . $query;
296 # Trim the extra &
297 $basequery = substr( $basequery, 1 );
298 return $wgTitle->getLocalURL( $basequery );
302 * HTML-safe version of appendQuery().
303 * @param string $query Query string fragment; do not include initial '?'
304 * @return string
306 function escapeAppendQuery( $query ) {
307 return htmlspecialchars( $this->appendQuery( $query ) );
311 * Check for limit and offset parameters on the input, and return sensible
312 * defaults if not given. The limit must be positive and is capped at 5000.
313 * Offset must be positive but is not capped.
315 * @param int $deflimit Limit to use if no input and the user hasn't set the option.
316 * @param string $optionname To specify an option other than rclimit to pull from.
317 * @return array first element is limit, second is offset
319 function getLimitOffset( $deflimit = 50, $optionname = 'rclimit' ) {
320 global $wgUser;
322 $limit = $this->getInt( 'limit', 0 );
323 if( $limit < 0 ) $limit = 0;
324 if( ( $limit == 0 ) && ( $optionname != '' ) ) {
325 $limit = (int)$wgUser->getOption( $optionname );
327 if( $limit <= 0 ) $limit = $deflimit;
328 if( $limit > 5000 ) $limit = 5000; # We have *some* limits...
330 $offset = $this->getInt( 'offset', 0 );
331 if( $offset < 0 ) $offset = 0;
333 return array( $limit, $offset );
337 * Return the path to the temporary file where PHP has stored the upload.
338 * @param string $key
339 * @return string or NULL if no such file.
341 function getFileTempname( $key ) {
342 if( !isset( $_FILES[$key] ) ) {
343 return NULL;
345 return $_FILES[$key]['tmp_name'];
349 * Return the size of the upload, or 0.
350 * @param string $key
351 * @return integer
353 function getFileSize( $key ) {
354 if( !isset( $_FILES[$key] ) ) {
355 return 0;
357 return $_FILES[$key]['size'];
361 * Return the original filename of the uploaded file, as reported by
362 * the submitting user agent. HTML-style character entities are
363 * interpreted and normalized to Unicode normalization form C, in part
364 * to deal with weird input from Safari with non-ASCII filenames.
366 * Other than this the name is not verified for being a safe filename.
368 * @param string $key
369 * @return string or NULL if no such file.
371 function getFileName( $key ) {
372 if( !isset( $_FILES[$key] ) ) {
373 return NULL;
375 $name = $_FILES[$key]['name'];
377 # Safari sends filenames in HTML-encoded Unicode form D...
378 # Horrid and evil! Let's try to make some kind of sense of it.
379 $name = Sanitizer::decodeCharReferences( $name );
380 $name = UtfNormal::cleanUp( $name );
381 wfDebug( "WebRequest::getFileName() '" . $_FILES[$key]['name'] . "' normalized to '$name'\n" );
382 return $name;
387 * WebRequest clone which takes values from a provided array.
389 * @package MediaWiki
391 class FauxRequest extends WebRequest {
392 var $data = null;
393 var $wasPosted = false;
395 function FauxRequest( $data, $wasPosted = false ) {
396 if( is_array( $data ) ) {
397 $this->data = $data;
398 } else {
399 wfDebugDieBacktrace( "FauxRequest() got bogus data" );
401 $this->wasPosted = $wasPosted;
404 function getVal( $name, $default = NULL ) {
405 return $this->getGPCVal( $this->data, $name, $default );
408 function getText( $name, $default = '' ) {
409 # Override; don't recode since we're using internal data
410 return $this->getVal( $name, $default );
413 function getValues() {
414 return $this->data;
417 function wasPosted() {
418 return $this->wasPosted;
421 function checkSessionCookie() {
422 return false;
425 function getRequestURL() {
426 wfDebugDieBacktrace( 'FauxRequest::getRequestURL() not implemented' );
429 function appendQuery( $query ) {
430 wfDebugDieBacktrace( 'FauxRequest::appendQuery() not implemented' );