3 * Deal with importing all those nasssty globals and things
6 # Copyright (C) 2003 Brion Vibber <brion@pobox.com>
7 # http://www.mediawiki.org/
9 # This program is free software; you can redistribute it and/or modify
10 # it under the terms of the GNU General Public License as published by
11 # the Free Software Foundation; either version 2 of the License, or
12 # (at your option) any later version.
14 # This program is distributed in the hope that it will be useful,
15 # but WITHOUT ANY WARRANTY; without even the implied warranty of
16 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 # GNU General Public License for more details.
19 # You should have received a copy of the GNU General Public License along
20 # with this program; if not, write to the Free Software Foundation, Inc.,
21 # 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
22 # http://www.gnu.org/copyleft/gpl.html
25 * Hypothetically, we could use a WebRequest object to fake a
26 * self-contained request (FauxRequest).
29 function WebRequest() {
30 $this->checkMagicQuotes();
31 global $wgUsePathInfo;
32 if( isset( $_SERVER['PATH_INFO'] ) && $wgUsePathInfo ) {
34 $_REQUEST['title'] = substr( $_SERVER['PATH_INFO'], 1 );
38 require_once( 'normal/UtfNormal.php' );
39 wfProfileIn( 'WebRequest:normalizeUnicode-fix' );
40 $this->normalizeUnicode( $_REQUEST );
41 wfProfileOut( 'WebRequest:normalizeUnicode-fix' );
45 function &fix_magic_quotes( &$arr ) {
46 foreach( $arr as $key => $val ) {
47 if( is_array( $val ) ) {
48 $this->fix_magic_quotes( $arr[$key] );
50 $arr[$key] = stripslashes( $val );
56 function checkMagicQuotes() {
57 if ( get_magic_quotes_gpc() ) {
58 $this->fix_magic_quotes( $_COOKIE );
59 $this->fix_magic_quotes( $_ENV );
60 $this->fix_magic_quotes( $_GET );
61 $this->fix_magic_quotes( $_POST );
62 $this->fix_magic_quotes( $_REQUEST );
63 $this->fix_magic_quotes( $_SERVER );
67 function normalizeUnicode( &$arr ) {
68 foreach( $arr as $key => $val ) {
69 if( is_array( $val ) ) {
70 $this->normalizeUnicode( $arr[$key ] );
72 $arr[$key] = UtfNormal
::cleanUp( $val );
77 function getGPCVal( &$arr, $name, $default ) {
78 if( isset( $arr[$name] ) ) {
85 function getGPCText( &$arr, $name, $default ) {
86 # Text fields may be in an alternate encoding which we should check.
87 # Also, strip CRLF line endings down to LF to achieve consistency.
89 if( isset( $arr[$name] ) ) {
90 return str_replace( "\r\n", "\n", $wgLang->recodeInput( $arr[$name] ) );
96 function getVal( $name, $default = NULL ) {
97 return $this->getGPCVal( $_REQUEST, $name, $default );
100 function getInt( $name, $default = 0 ) {
101 return IntVal( $this->getVal( $name, $default ) );
104 function getBool( $name, $default = false ) {
105 return $this->getVal( $name, $default ) ?
true : false;
108 function getCheck( $name ) {
109 # Checkboxes and buttons are only present when clicked
110 # Presence connotes truth, abscense false
111 $val = $this->getVal( $name, NULL );
112 return isset( $val );
115 function getText( $name, $default = '' ) {
116 return $this->getGPCText( $_REQUEST, $name, $default );
119 function getValues() {
120 $names = func_get_args();
121 if ( count( $names ) == 0 ) {
122 $names = array_keys( $_REQUEST );
126 foreach ( $names as $name ) {
127 $value = $this->getVal( $name );
128 if ( !is_null( $value ) ) {
129 $retVal[$name] = $value;
135 function wasPosted() {
136 return $_SERVER['REQUEST_METHOD'] == 'POST';
139 function checkSessionCookie() {
140 return isset( $_COOKIE[ini_get('session.name')] );
143 function getRequestURL() {
144 return $_SERVER['REQUEST_URI'];
147 function getFullRequestURL() {
149 return $wgServer . $this->getRequestURL();
153 * Take an arbitrary query and rewrite the present URL to include it
155 function appendQuery( $query ) {
158 foreach( $_GET as $var => $val ) {
159 if( $var == 'title' ) continue;
160 $basequery .= '&' . urlencode( $var ) . '=' . urlencode( $val );
162 $basequery .= '&' . $query;
165 $basequery = substr( $basequery, 1 );
166 return $wgTitle->getLocalURL( $basequery );
169 function escapeAppendQuery( $query ) {
170 return htmlspecialchars( $this->appendQuery( $query ) );
173 function getLimitOffset( $deflimit = 50, $optionname = 'rclimit' ) {
176 $limit = $this->getInt( 'limit', 0 );
177 if( $limit < 0 ) $limit = 0;
178 if( ( $limit == 0 ) && ( $optionname != '' ) ) {
179 $limit = (int)$wgUser->getOption( $optionname );
181 if( $limit <= 0 ) $limit = $deflimit;
182 if( $limit > 5000 ) $limit = 5000; # We have *some* limits...
184 $offset = $this->getInt( 'offset', 0 );
185 if( $offset < 0 ) $offset = 0;
187 return array( $limit, $offset );
191 * Get information on uploaded files
193 function getFileTempname( $key ) {
194 if( !isset( $_FILES[$key] ) ) {
197 return $_FILES[$key]['tmp_name'];
200 function getFileSize( $key ) {
201 if( !isset( $_FILES[$key] ) ) {
204 return $_FILES[$key]['size'];
207 function getFileName( $key ) {
208 if( !isset( $_FILES[$key] ) ) {
211 $name = $_FILES[$key]['name'];
213 # Safari sends filenames in HTML-encoded Unicode form D...
214 # Horrid and evil! Let's try to make some kind of sense of it.
217 $name = utf8_encode( $name );
219 $name = wfMungeToUtf8( $name );
220 $name = UtfNormal
::cleanUp( $name );
222 $name = utf8_decode( $name );
224 wfDebug( "WebRequest::getFileName() '" . $_FILES[$key]['name'] . "' normalized to '$name'\n" );
232 class FauxRequest
extends WebRequest
{
234 var $wasPosted = false;
236 function WebRequest( $data, $wasPosted = false ) {
237 if( is_array( $data ) ) {
240 wfDebugDieBacktrace( "FauxReqeust() got bogus data" );
242 $this->wasPosted
= $wasPosted;
245 function getVal( $name, $default = NULL ) {
246 return $this->getGPCVal( $this->data
, $name, $default );
249 function getText( $name, $default = '' ) {
250 # Override; don't recode since we're using internal data
251 return $this->getVal( $name, $default );
254 function getValues() {
258 function wasPosted() {
259 return $this->wasPosted
;
262 function checkSessionCookie() {
266 function getRequestURL() {
267 wfDebugDieBacktrace( 'FauxRequest::getRequestURL() not implemented' );
270 function appendQuery( $query ) {
271 wfDebugDieBacktrace( 'FauxRequest::appendQuery() not implemented' );