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