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