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