04cc51d0f5b985353c78a61eccafb801ecd75ba1
[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 * Hypothetically, we could use a WebRequest object to fake a
27 * self-contained request (FauxRequest).
28 * @package MediaWiki
29 */
30 class WebRequest {
31 function WebRequest() {
32 $this->checkMagicQuotes();
33 global $wgUsePathInfo;
34 if( isset( $_SERVER['PATH_INFO'] ) && $wgUsePathInfo ) {
35 # Stuff it!
36 $_REQUEST['title'] = substr( $_SERVER['PATH_INFO'], 1 );
37 }
38 global $wgUseLatin1;
39 if( !$wgUseLatin1 ) {
40 require_once( 'normal/UtfNormal.php' );
41 wfProfileIn( 'WebRequest:normalizeUnicode-fix' );
42 $this->normalizeUnicode( $_REQUEST );
43 wfProfileOut( 'WebRequest:normalizeUnicode-fix' );
44 }
45 }
46
47 function &fix_magic_quotes( &$arr ) {
48 foreach( $arr as $key => $val ) {
49 if( is_array( $val ) ) {
50 $this->fix_magic_quotes( $arr[$key] );
51 } else {
52 $arr[$key] = stripslashes( $val );
53 }
54 }
55 return $arr;
56 }
57
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 );
66 }
67 }
68
69 function normalizeUnicode( &$arr ) {
70 foreach( $arr as $key => $val ) {
71 if( is_array( $val ) ) {
72 $this->normalizeUnicode( $arr[$key ] );
73 } else {
74 $arr[$key] = UtfNormal::cleanUp( $val );
75 }
76 }
77 }
78
79 function getGPCVal( &$arr, $name, $default ) {
80 if( isset( $arr[$name] ) ) {
81 return $arr[$name];
82 } else {
83 return $default;
84 }
85 }
86
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.
90 global $wgLang;
91 if( isset( $arr[$name] ) ) {
92 return str_replace( "\r\n", "\n", $wgLang->recodeInput( $arr[$name] ) );
93 } else {
94 return $default;
95 }
96 }
97
98 function getVal( $name, $default = NULL ) {
99 return $this->getGPCVal( $_REQUEST, $name, $default );
100 }
101
102 function getInt( $name, $default = 0 ) {
103 return IntVal( $this->getVal( $name, $default ) );
104 }
105
106 function getBool( $name, $default = false ) {
107 return $this->getVal( $name, $default ) ? true : false;
108 }
109
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 );
115 }
116
117 function getText( $name, $default = '' ) {
118 return $this->getGPCText( $_REQUEST, $name, $default );
119 }
120
121 function getValues() {
122 $names = func_get_args();
123 if ( count( $names ) == 0 ) {
124 $names = array_keys( $_REQUEST );
125 }
126
127 $retVal = array();
128 foreach ( $names as $name ) {
129 $value = $this->getVal( $name );
130 if ( !is_null( $value ) ) {
131 $retVal[$name] = $value;
132 }
133 }
134 return $retVal;
135 }
136
137 function wasPosted() {
138 return $_SERVER['REQUEST_METHOD'] == 'POST';
139 }
140
141 function checkSessionCookie() {
142 return isset( $_COOKIE[ini_get('session.name')] );
143 }
144
145 function getRequestURL() {
146 return $_SERVER['REQUEST_URI'];
147 }
148
149 function getFullRequestURL() {
150 global $wgServer;
151 return $wgServer . $this->getRequestURL();
152 }
153
154 /**
155 * Take an arbitrary query and rewrite the present URL to include it
156 */
157 function appendQuery( $query ) {
158 global $wgTitle;
159 $basequery = '';
160 foreach( $_GET as $var => $val ) {
161 if( $var == 'title' ) continue;
162 $basequery .= '&' . urlencode( $var ) . '=' . urlencode( $val );
163 }
164 $basequery .= '&' . $query;
165
166 # Trim the extra &
167 $basequery = substr( $basequery, 1 );
168 return $wgTitle->getLocalURL( $basequery );
169 }
170
171 function escapeAppendQuery( $query ) {
172 return htmlspecialchars( $this->appendQuery( $query ) );
173 }
174
175 function getLimitOffset( $deflimit = 50, $optionname = 'rclimit' ) {
176 global $wgUser;
177
178 $limit = $this->getInt( 'limit', 0 );
179 if( $limit < 0 ) $limit = 0;
180 if( ( $limit == 0 ) && ( $optionname != '' ) ) {
181 $limit = (int)$wgUser->getOption( $optionname );
182 }
183 if( $limit <= 0 ) $limit = $deflimit;
184 if( $limit > 5000 ) $limit = 5000; # We have *some* limits...
185
186 $offset = $this->getInt( 'offset', 0 );
187 if( $offset < 0 ) $offset = 0;
188
189 return array( $limit, $offset );
190 }
191
192 /**
193 * Get information on uploaded files
194 */
195 function getFileTempname( $key ) {
196 if( !isset( $_FILES[$key] ) ) {
197 return NULL;
198 }
199 return $_FILES[$key]['tmp_name'];
200 }
201
202 function getFileSize( $key ) {
203 if( !isset( $_FILES[$key] ) ) {
204 return 0;
205 }
206 return $_FILES[$key]['size'];
207 }
208
209 function getFileName( $key ) {
210 if( !isset( $_FILES[$key] ) ) {
211 return NULL;
212 }
213 $name = $_FILES[$key]['name'];
214
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.
217 global $wgUseLatin1;
218 if( $wgUseLatin1 ) {
219 $name = utf8_encode( $name );
220 }
221 $name = wfMungeToUtf8( $name );
222 $name = UtfNormal::cleanUp( $name );
223 if( $wgUseLatin1 ) {
224 $name = utf8_decode( $name );
225 }
226 wfDebug( "WebRequest::getFileName() '" . $_FILES[$key]['name'] . "' normalized to '$name'\n" );
227 return $name;
228 }
229 }
230
231 /**
232 *
233 * @package MediaWiki
234 */
235 class FauxRequest extends WebRequest {
236 var $data = null;
237 var $wasPosted = false;
238
239 function WebRequest( $data, $wasPosted = false ) {
240 if( is_array( $data ) ) {
241 $this->data = $data;
242 } else {
243 wfDebugDieBacktrace( "FauxReqeust() got bogus data" );
244 }
245 $this->wasPosted = $wasPosted;
246 }
247
248 function getVal( $name, $default = NULL ) {
249 return $this->getGPCVal( $this->data, $name, $default );
250 }
251
252 function getText( $name, $default = '' ) {
253 # Override; don't recode since we're using internal data
254 return $this->getVal( $name, $default );
255 }
256
257 function getValues() {
258 return $this->data;
259 }
260
261 function wasPosted() {
262 return $this->wasPosted;
263 }
264
265 function checkSessionCookie() {
266 return false;
267 }
268
269 function getRequestURL() {
270 wfDebugDieBacktrace( 'FauxRequest::getRequestURL() not implemented' );
271 }
272
273 function appendQuery( $query ) {
274 wfDebugDieBacktrace( 'FauxRequest::appendQuery() not implemented' );
275 }
276
277 }
278
279 ?>