Start cracking down on illegal titles: in UTF-8 mode reject titles which have had...
[lhc/web/wiklou.git] / includes / WebRequest.php
1 <?php
2 /**
3 * Deal with importing all those nasssty globals and things
4 */
5
6 # Copyright (C) 2003 Brion Vibber <brion@pobox.com>
7 # http://www.mediawiki.org/
8 #
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.
13 #
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.
18 #
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
23
24 /**
25 * Hypothetically, we could use a WebRequest object to fake a
26 * self-contained request (FauxRequest).
27 */
28 class WebRequest {
29 function WebRequest() {
30 $this->checkMagicQuotes();
31 global $wgUsePathInfo;
32 if( isset( $_SERVER['PATH_INFO'] ) && $wgUsePathInfo ) {
33 # Stuff it!
34 $_REQUEST['title'] = substr( $_SERVER['PATH_INFO'], 1 );
35 }
36 global $wgUseLatin1;
37 if( !$wgUseLatin1 ) {
38 require_once( 'normal/UtfNormal.php' );
39 wfProfileIn( 'WebRequest:normalizeUnicode-fix' );
40 $this->normalizeUnicode( $_REQUEST );
41 wfProfileOut( 'WebRequest:normalizeUnicode-fix' );
42 }
43 }
44
45 function &fix_magic_quotes( &$arr ) {
46 foreach( $arr as $key => $val ) {
47 if( is_array( $val ) ) {
48 $this->fix_magic_quotes( $arr[$key] );
49 } else {
50 $arr[$key] = stripslashes( $val );
51 }
52 }
53 return $arr;
54 }
55
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 );
64 }
65 }
66
67 function normalizeUnicode( &$arr ) {
68 foreach( $arr as $key => $val ) {
69 if( is_array( $val ) ) {
70 $this->normalizeUnicode( $arr[$key ] );
71 } else {
72 $arr[$key] = UtfNormal::cleanUp( $val );
73 }
74 }
75 }
76
77 function getGPCVal( &$arr, $name, $default ) {
78 if( isset( $arr[$name] ) ) {
79 return $arr[$name];
80 } else {
81 return $default;
82 }
83 }
84
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.
88 global $wgLang;
89 if( isset( $arr[$name] ) ) {
90 return str_replace( "\r\n", "\n", $wgLang->recodeInput( $arr[$name] ) );
91 } else {
92 return $default;
93 }
94 }
95
96 function getVal( $name, $default = NULL ) {
97 return $this->getGPCVal( $_REQUEST, $name, $default );
98 }
99
100 function getInt( $name, $default = 0 ) {
101 return IntVal( $this->getVal( $name, $default ) );
102 }
103
104 function getBool( $name, $default = false ) {
105 return $this->getVal( $name, $default ) ? true : false;
106 }
107
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 );
113 }
114
115 function getText( $name, $default = '' ) {
116 return $this->getGPCText( $_REQUEST, $name, $default );
117 }
118
119 function getValues() {
120 $names = func_get_args();
121 if ( count( $names ) == 0 ) {
122 $names = array_keys( $_REQUEST );
123 }
124
125 $retVal = array();
126 foreach ( $names as $name ) {
127 $value = $this->getVal( $name );
128 if ( !is_null( $value ) ) {
129 $retVal[$name] = $value;
130 }
131 }
132 return $retVal;
133 }
134
135 function wasPosted() {
136 return $_SERVER['REQUEST_METHOD'] == 'POST';
137 }
138
139 function checkSessionCookie() {
140 return isset( $_COOKIE[ini_get('session.name')] );
141 }
142
143 function getRequestURL() {
144 return $_SERVER['REQUEST_URI'];
145 }
146
147 function getFullRequestURL() {
148 global $wgServer;
149 return $wgServer . $this->getRequestURL();
150 }
151
152 /**
153 * Take an arbitrary query and rewrite the present URL to include it
154 */
155 function appendQuery( $query ) {
156 global $wgTitle;
157 $basequery = '';
158 foreach( $_GET as $var => $val ) {
159 if( $var == 'title' ) continue;
160 $basequery .= '&' . urlencode( $var ) . '=' . urlencode( $val );
161 }
162 $basequery .= '&' . $query;
163
164 # Trim the extra &
165 $basequery = substr( $basequery, 1 );
166 return $wgTitle->getLocalURL( $basequery );
167 }
168
169 function escapeAppendQuery( $query ) {
170 return htmlspecialchars( $this->appendQuery( $query ) );
171 }
172
173 function getLimitOffset( $deflimit = 50, $optionname = 'rclimit' ) {
174 global $wgUser;
175
176 $limit = $this->getInt( 'limit', 0 );
177 if( $limit < 0 ) $limit = 0;
178 if( ( $limit == 0 ) && ( $optionname != '' ) ) {
179 $limit = (int)$wgUser->getOption( $optionname );
180 }
181 if( $limit <= 0 ) $limit = $deflimit;
182 if( $limit > 5000 ) $limit = 5000; # We have *some* limits...
183
184 $offset = $this->getInt( 'offset', 0 );
185 if( $offset < 0 ) $offset = 0;
186
187 return array( $limit, $offset );
188 }
189 }
190
191 /**
192 *
193 */
194 class FauxRequest extends WebRequest {
195 var $data = null;
196 var $wasPosted = false;
197
198 function WebRequest( $data, $wasPosted = false ) {
199 if( is_array( $data ) ) {
200 $this->data = $data;
201 } else {
202 wfDebugDieBacktrace( "FauxReqeust() got bogus data" );
203 }
204 $this->wasPosted = $wasPosted;
205 }
206
207 function getVal( $name, $default = NULL ) {
208 return $this->getGPCVal( $this->data, $name, $default );
209 }
210
211 function getText( $name, $default = '' ) {
212 # Override; don't recode since we're using internal data
213 return $this->getVal( $name, $default );
214 }
215
216 function getValues() {
217 return $this->data;
218 }
219
220 function wasPosted() {
221 return $this->wasPosted;
222 }
223
224 function checkSessionCookie() {
225 return false;
226 }
227
228 function getRequestURL() {
229 wfDebugDieBacktrace( 'FauxRequest::getRequestURL() not implemented' );
230 }
231
232 function appendQuery( $query ) {
233 wfDebugDieBacktrace( 'FauxRequest::appendQuery() not implemented' );
234 }
235
236 }
237
238 ?>