04cc51d0f5b985353c78a61eccafb801ecd75ba1
3 * Deal with importing all those nasssty globals and things
7 # Copyright (C) 2003 Brion Vibber <brion@pobox.com>
8 # http://www.mediawiki.org/
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.
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.
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
26 * Hypothetically, we could use a WebRequest object to fake a
27 * self-contained request (FauxRequest).
31 function WebRequest() {
32 $this->checkMagicQuotes();
33 global $wgUsePathInfo;
34 if( isset( $_SERVER['PATH_INFO'] ) && $wgUsePathInfo ) {
36 $_REQUEST['title'] = substr( $_SERVER['PATH_INFO'], 1 );
40 require_once( 'normal/UtfNormal.php' );
41 wfProfileIn( 'WebRequest:normalizeUnicode-fix' );
42 $this->normalizeUnicode( $_REQUEST );
43 wfProfileOut( 'WebRequest:normalizeUnicode-fix' );
47 function &fix_magic_quotes( &$arr ) {
48 foreach( $arr as $key => $val ) {
49 if( is_array( $val ) ) {
50 $this->fix_magic_quotes( $arr[$key] );
52 $arr[$key] = stripslashes( $val );
58 function checkMagicQuotes() {
59 if ( get_magic_quotes_gpc() ) {
60 $this->fix_magic_quotes( $_COOKIE );
61 $this->fix_magic_quotes( $_ENV );
62 $this->fix_magic_quotes( $_GET );
63 $this->fix_magic_quotes( $_POST );
64 $this->fix_magic_quotes( $_REQUEST );
65 $this->fix_magic_quotes( $_SERVER );
69 function normalizeUnicode( &$arr ) {
70 foreach( $arr as $key => $val ) {
71 if( is_array( $val ) ) {
72 $this->normalizeUnicode( $arr[$key ] );
74 $arr[$key] = UtfNormal
::cleanUp( $val );
79 function getGPCVal( &$arr, $name, $default ) {
80 if( isset( $arr[$name] ) ) {
87 function getGPCText( &$arr, $name, $default ) {
88 # Text fields may be in an alternate encoding which we should check.
89 # Also, strip CRLF line endings down to LF to achieve consistency.
91 if( isset( $arr[$name] ) ) {
92 return str_replace( "\r\n", "\n", $wgLang->recodeInput( $arr[$name] ) );
98 function getVal( $name, $default = NULL ) {
99 return $this->getGPCVal( $_REQUEST, $name, $default );
102 function getInt( $name, $default = 0 ) {
103 return IntVal( $this->getVal( $name, $default ) );
106 function getBool( $name, $default = false ) {
107 return $this->getVal( $name, $default ) ?
true : false;
110 function getCheck( $name ) {
111 # Checkboxes and buttons are only present when clicked
112 # Presence connotes truth, abscense false
113 $val = $this->getVal( $name, NULL );
114 return isset( $val );
117 function getText( $name, $default = '' ) {
118 return $this->getGPCText( $_REQUEST, $name, $default );
121 function getValues() {
122 $names = func_get_args();
123 if ( count( $names ) == 0 ) {
124 $names = array_keys( $_REQUEST );
128 foreach ( $names as $name ) {
129 $value = $this->getVal( $name );
130 if ( !is_null( $value ) ) {
131 $retVal[$name] = $value;
137 function wasPosted() {
138 return $_SERVER['REQUEST_METHOD'] == 'POST';
141 function checkSessionCookie() {
142 return isset( $_COOKIE[ini_get('session.name')] );
145 function getRequestURL() {
146 return $_SERVER['REQUEST_URI'];
149 function getFullRequestURL() {
151 return $wgServer . $this->getRequestURL();
155 * Take an arbitrary query and rewrite the present URL to include it
157 function appendQuery( $query ) {
160 foreach( $_GET as $var => $val ) {
161 if( $var == 'title' ) continue;
162 $basequery .= '&' . urlencode( $var ) . '=' . urlencode( $val );
164 $basequery .= '&' . $query;
167 $basequery = substr( $basequery, 1 );
168 return $wgTitle->getLocalURL( $basequery );
171 function escapeAppendQuery( $query ) {
172 return htmlspecialchars( $this->appendQuery( $query ) );
175 function getLimitOffset( $deflimit = 50, $optionname = 'rclimit' ) {
178 $limit = $this->getInt( 'limit', 0 );
179 if( $limit < 0 ) $limit = 0;
180 if( ( $limit == 0 ) && ( $optionname != '' ) ) {
181 $limit = (int)$wgUser->getOption( $optionname );
183 if( $limit <= 0 ) $limit = $deflimit;
184 if( $limit > 5000 ) $limit = 5000; # We have *some* limits...
186 $offset = $this->getInt( 'offset', 0 );
187 if( $offset < 0 ) $offset = 0;
189 return array( $limit, $offset );
193 * Get information on uploaded files
195 function getFileTempname( $key ) {
196 if( !isset( $_FILES[$key] ) ) {
199 return $_FILES[$key]['tmp_name'];
202 function getFileSize( $key ) {
203 if( !isset( $_FILES[$key] ) ) {
206 return $_FILES[$key]['size'];
209 function getFileName( $key ) {
210 if( !isset( $_FILES[$key] ) ) {
213 $name = $_FILES[$key]['name'];
215 # Safari sends filenames in HTML-encoded Unicode form D...
216 # Horrid and evil! Let's try to make some kind of sense of it.
219 $name = utf8_encode( $name );
221 $name = wfMungeToUtf8( $name );
222 $name = UtfNormal
::cleanUp( $name );
224 $name = utf8_decode( $name );
226 wfDebug( "WebRequest::getFileName() '" . $_FILES[$key]['name'] . "' normalized to '$name'\n" );
235 class FauxRequest
extends WebRequest
{
237 var $wasPosted = false;
239 function WebRequest( $data, $wasPosted = false ) {
240 if( is_array( $data ) ) {
243 wfDebugDieBacktrace( "FauxReqeust() got bogus data" );
245 $this->wasPosted
= $wasPosted;
248 function getVal( $name, $default = NULL ) {
249 return $this->getGPCVal( $this->data
, $name, $default );
252 function getText( $name, $default = '' ) {
253 # Override; don't recode since we're using internal data
254 return $this->getVal( $name, $default );
257 function getValues() {
261 function wasPosted() {
262 return $this->wasPosted
;
265 function checkSessionCookie() {
269 function getRequestURL() {
270 wfDebugDieBacktrace( 'FauxRequest::getRequestURL() not implemented' );
273 function appendQuery( $query ) {
274 wfDebugDieBacktrace( 'FauxRequest::appendQuery() not implemented' );