efae012faa79e1c6fb8ad8e1e9398b9313cb975b
[lhc/web/wiklou.git] / includes / WebRequest.php
1 <?php
2 /**
3 * Deal with importing all those nasssty globals and things
4 * @package MediaWiki
5 */
6
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.
14 #
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.
19 #
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
24
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.
29 *
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.
34 *
35 * @package MediaWiki
36 */
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 );
45 }
46 }
47
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
54 */
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 );
61 }
62 }
63 return $arr;
64 }
65
66 /**
67 * If magic_quotes_gpc option is on, run the global arrays
68 * through fix_magic_quotes to strip out the stupid slashes.
69 * WARNING: This should only be done once! Running a second
70 * time could damage the values.
71 * @private
72 */
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 );
81 }
82 }
83
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
89 */
90 function normalizeUnicode( $data ) {
91 if( is_array( $data ) ) {
92 foreach( $data as $key => $val ) {
93 $data[$key] = $this->normalizeUnicode( $val );
94 }
95 } else {
96 $data = UtfNormal::cleanUp( $data );
97 }
98 return $data;
99 }
100
101 /**
102 * Fetch a value from the given array or return $default if it's not set.
103 *
104 * @param array $arr
105 * @param string $name
106 * @param mixed $default
107 * @return mixed
108 * @private
109 */
110 function getGPCVal( $arr, $name, $default ) {
111 if( isset( $arr[$name] ) ) {
112 global $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 );
118 }
119 }
120 $data = $this->normalizeUnicode( $data );
121 return $data;
122 } else {
123 return $default;
124 }
125 }
126
127 /**
128 * Fetch a scalar from the input or return $default if it's not set.
129 * Returns a string. Arrays are discarded.
130 *
131 * @param string $name
132 * @param string $default optional default (or NULL)
133 * @return string
134 */
135 function getVal( $name, $default = NULL ) {
136 $val = $this->getGPCVal( $_REQUEST, $name, $default );
137 if( is_array( $val ) ) {
138 $val = $default;
139 }
140 if( is_null( $val ) ) {
141 return null;
142 } else {
143 return (string)$val;
144 }
145 }
146
147 /**
148 * Fetch an array from the input or return $default if it's not set.
149 * If source was scalar, will return an array with a single element.
150 * If no source and no default, returns NULL.
151 *
152 * @param string $name
153 * @param array $default optional default (or NULL)
154 * @return array
155 */
156 function getArray( $name, $default = NULL ) {
157 $val = $this->getGPCVal( $_REQUEST, $name, $default );
158 if( is_null( $val ) ) {
159 return null;
160 } else {
161 return (array)$val;
162 }
163 }
164
165 /**
166 * Fetch an array of integers, or return $default if it's not set.
167 * If source was scalar, will return an array with a single element.
168 * If no source and no default, returns NULL.
169 * If an array is returned, contents are guaranteed to be integers.
170 *
171 * @param string $name
172 * @param array $default option default (or NULL)
173 * @return array of ints
174 */
175 function getIntArray( $name, $default = NULL ) {
176 $val = $this->getArray( $name, $default );
177 if( is_array( $val ) ) {
178 $val = array_map( 'intval', $val );
179 }
180 return $val;
181 }
182
183 /**
184 * Fetch an integer value from the input or return $default if not set.
185 * Guaranteed to return an integer; non-numeric input will typically
186 * return 0.
187 * @param string $name
188 * @param int $default
189 * @return int
190 */
191 function getInt( $name, $default = 0 ) {
192 return intval( $this->getVal( $name, $default ) );
193 }
194
195 /**
196 * Fetch an integer value from the input or return null if empty.
197 * Guaranteed to return an integer or null; non-numeric input will
198 * typically return null.
199 * @param string $name
200 * @return int
201 */
202 function getIntOrNull( $name ) {
203 $val = $this->getVal( $name );
204 return is_numeric( $val )
205 ? intval( $val )
206 : null;
207 }
208
209 /**
210 * Fetch a boolean value from the input or return $default if not set.
211 * Guaranteed to return true or false, with normal PHP semantics for
212 * boolean interpretation of strings.
213 * @param string $name
214 * @param bool $default
215 * @return bool
216 */
217 function getBool( $name, $default = false ) {
218 return $this->getVal( $name, $default ) ? true : false;
219 }
220
221 /**
222 * Return true if the named value is set in the input, whatever that
223 * value is (even "0"). Return false if the named value is not set.
224 * Example use is checking for the presence of check boxes in forms.
225 * @param string $name
226 * @return bool
227 */
228 function getCheck( $name ) {
229 # Checkboxes and buttons are only present when clicked
230 # Presence connotes truth, abscense false
231 $val = $this->getVal( $name, NULL );
232 return isset( $val );
233 }
234
235 /**
236 * Fetch a text string from the given array or return $default if it's not
237 * set. \r is stripped from the text, and with some language modules there
238 * is an input transliteration applied. This should generally be used for
239 * form <textarea> and <input> fields.
240 *
241 * @param string $name
242 * @param string $default optional
243 * @return string
244 */
245 function getText( $name, $default = '' ) {
246 global $wgContLang;
247 $val = $this->getVal( $name, $default );
248 return str_replace( "\r\n", "\n",
249 $wgContLang->recodeInput( $val ) );
250 }
251
252 /**
253 * Extracts the given named values into an array.
254 * If no arguments are given, returns all input values.
255 * No transformation is performed on the values.
256 */
257 function getValues() {
258 $names = func_get_args();
259 if ( count( $names ) == 0 ) {
260 $names = array_keys( $_REQUEST );
261 }
262
263 $retVal = array();
264 foreach ( $names as $name ) {
265 $value = $this->getVal( $name );
266 if ( !is_null( $value ) ) {
267 $retVal[$name] = $value;
268 }
269 }
270 return $retVal;
271 }
272
273 /**
274 * Returns true if the present request was reached by a POST operation,
275 * false otherwise (GET, HEAD, or command-line).
276 *
277 * Note that values retrieved by the object may come from the
278 * GET URL etc even on a POST request.
279 *
280 * @return bool
281 */
282 function wasPosted() {
283 return $_SERVER['REQUEST_METHOD'] == 'POST';
284 }
285
286 /**
287 * Returns true if there is a session cookie set.
288 * This does not necessarily mean that the user is logged in!
289 *
290 * @return bool
291 */
292 function checkSessionCookie() {
293 return isset( $_COOKIE[ini_get('session.name')] );
294 }
295
296 /**
297 * Return the path portion of the request URI.
298 * @return string
299 */
300 function getRequestURL() {
301 $base = $_SERVER['REQUEST_URI'];
302 if( $base{0} == '/' ) {
303 return $base;
304 } else {
305 // We may get paths with a host prepended; strip it.
306 return preg_replace( '!^[^:]+://[^/]+/!', '/', $base );
307 }
308 }
309
310 /**
311 * Return the request URI with the canonical service and hostname.
312 * @return string
313 */
314 function getFullRequestURL() {
315 global $wgServer;
316 return $wgServer . $this->getRequestURL();
317 }
318
319 /**
320 * Take an arbitrary query and rewrite the present URL to include it
321 * @param $query String: query string fragment; do not include initial '?'
322 * @return string
323 */
324 function appendQuery( $query ) {
325 global $wgTitle;
326 $basequery = '';
327 foreach( $_GET as $var => $val ) {
328 if ( $var == 'title' )
329 continue;
330 if ( is_array( $val ) )
331 /* This will happen given a request like
332 * http://en.wikipedia.org/w/index.php?title[]=Special:Userlogin&returnto[]=Main_Page
333 */
334 continue;
335 $basequery .= '&' . urlencode( $var ) . '=' . urlencode( $val );
336 }
337 $basequery .= '&' . $query;
338
339 # Trim the extra &
340 $basequery = substr( $basequery, 1 );
341 return $wgTitle->getLocalURL( $basequery );
342 }
343
344 /**
345 * HTML-safe version of appendQuery().
346 * @param $query String: query string fragment; do not include initial '?'
347 * @return string
348 */
349 function escapeAppendQuery( $query ) {
350 return htmlspecialchars( $this->appendQuery( $query ) );
351 }
352
353 /**
354 * Check for limit and offset parameters on the input, and return sensible
355 * defaults if not given. The limit must be positive and is capped at 5000.
356 * Offset must be positive but is not capped.
357 *
358 * @param $deflimit Integer: limit to use if no input and the user hasn't set the option.
359 * @param $optionname String: to specify an option other than rclimit to pull from.
360 * @return array first element is limit, second is offset
361 */
362 function getLimitOffset( $deflimit = 50, $optionname = 'rclimit' ) {
363 global $wgUser;
364
365 $limit = $this->getInt( 'limit', 0 );
366 if( $limit < 0 ) $limit = 0;
367 if( ( $limit == 0 ) && ( $optionname != '' ) ) {
368 $limit = (int)$wgUser->getOption( $optionname );
369 }
370 if( $limit <= 0 ) $limit = $deflimit;
371 if( $limit > 5000 ) $limit = 5000; # We have *some* limits...
372
373 $offset = $this->getInt( 'offset', 0 );
374 if( $offset < 0 ) $offset = 0;
375
376 return array( $limit, $offset );
377 }
378
379 /**
380 * Return the path to the temporary file where PHP has stored the upload.
381 * @param $key String:
382 * @return string or NULL if no such file.
383 */
384 function getFileTempname( $key ) {
385 if( !isset( $_FILES[$key] ) ) {
386 return NULL;
387 }
388 return $_FILES[$key]['tmp_name'];
389 }
390
391 /**
392 * Return the size of the upload, or 0.
393 * @param $key String:
394 * @return integer
395 */
396 function getFileSize( $key ) {
397 if( !isset( $_FILES[$key] ) ) {
398 return 0;
399 }
400 return $_FILES[$key]['size'];
401 }
402
403 /**
404 * Return the upload error or 0
405 * @param $key String:
406 * @return integer
407 */
408 function getUploadError( $key ) {
409 if( !isset( $_FILES[$key] ) || !isset( $_FILES[$key]['error'] ) ) {
410 return 0/*UPLOAD_ERR_OK*/;
411 }
412 return $_FILES[$key]['error'];
413 }
414
415 /**
416 * Return the original filename of the uploaded file, as reported by
417 * the submitting user agent. HTML-style character entities are
418 * interpreted and normalized to Unicode normalization form C, in part
419 * to deal with weird input from Safari with non-ASCII filenames.
420 *
421 * Other than this the name is not verified for being a safe filename.
422 *
423 * @param $key String:
424 * @return string or NULL if no such file.
425 */
426 function getFileName( $key ) {
427 if( !isset( $_FILES[$key] ) ) {
428 return NULL;
429 }
430 $name = $_FILES[$key]['name'];
431
432 # Safari sends filenames in HTML-encoded Unicode form D...
433 # Horrid and evil! Let's try to make some kind of sense of it.
434 $name = Sanitizer::decodeCharReferences( $name );
435 $name = UtfNormal::cleanUp( $name );
436 wfDebug( "WebRequest::getFileName() '" . $_FILES[$key]['name'] . "' normalized to '$name'\n" );
437 return $name;
438 }
439 }
440
441 /**
442 * WebRequest clone which takes values from a provided array.
443 *
444 * @package MediaWiki
445 */
446 class FauxRequest extends WebRequest {
447 var $data = null;
448 var $wasPosted = false;
449
450 function FauxRequest( $data, $wasPosted = false ) {
451 if( is_array( $data ) ) {
452 $this->data = $data;
453 } else {
454 wfDebugDieBacktrace( "FauxRequest() got bogus data" );
455 }
456 $this->wasPosted = $wasPosted;
457 }
458
459 function getVal( $name, $default = NULL ) {
460 return $this->getGPCVal( $this->data, $name, $default );
461 }
462
463 function getText( $name, $default = '' ) {
464 # Override; don't recode since we're using internal data
465 return $this->getVal( $name, $default );
466 }
467
468 function getValues() {
469 return $this->data;
470 }
471
472 function wasPosted() {
473 return $this->wasPosted;
474 }
475
476 function checkSessionCookie() {
477 return false;
478 }
479
480 function getRequestURL() {
481 wfDebugDieBacktrace( 'FauxRequest::getRequestURL() not implemented' );
482 }
483
484 function appendQuery( $query ) {
485 wfDebugDieBacktrace( 'FauxRequest::appendQuery() not implemented' );
486 }
487
488 }
489
490 ?>