One more unicode normalization fix: don't die horribly on arrays, and get the PATH_IN...
[lhc/web/wiklou.git] / includes / WebRequest.php
1 <?php
2 # Deal with importing all those nasssty globals and things
3 #
4 # Copyright (C) 2003 Brion Vibber <brion@pobox.com>
5 # http://www.mediawiki.org/
6 #
7 # This program is free software; you can redistribute it and/or modify
8 # it under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 2 of the License, or
10 # (at your option) any later version.
11 #
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
16 #
17 # You should have received a copy of the GNU General Public License along
18 # with this program; if not, write to the Free Software Foundation, Inc.,
19 # 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20 # http://www.gnu.org/copyleft/gpl.html
21
22 # Hypothetically, we could use a WebRequest object to fake a
23 # self-contained request (FauxRequest).
24
25 class WebRequest {
26 function WebRequest() {
27 $this->checkMagicQuotes();
28 global $wgUseLatin1;
29 if( !$wgUseLatin1 ) {
30 require_once( 'normal/UtfNormal.php' );
31 wfProfileIn( 'WebRequest:normalizeUnicode-fix' );
32 $this->normalizeUnicode( $_REQUEST );
33 wfProfileOut( 'WebRequest:normalizeUnicode-fix' );
34 }
35 }
36
37 function &fix_magic_quotes( &$arr ) {
38 foreach( $arr as $key => $val ) {
39 if( is_array( $val ) ) {
40 $this->fix_magic_quotes( $arr[$key] );
41 } else {
42 $arr[$key] = stripslashes( $val );
43 }
44 }
45 return $arr;
46 }
47
48 function checkMagicQuotes() {
49 if ( get_magic_quotes_gpc() ) {
50 $this->fix_magic_quotes( $_COOKIE );
51 $this->fix_magic_quotes( $_ENV );
52 $this->fix_magic_quotes( $_GET );
53 $this->fix_magic_quotes( $_POST );
54 $this->fix_magic_quotes( $_REQUEST );
55 $this->fix_magic_quotes( $_SERVER );
56 }
57 }
58
59 function normalizeUnicode( &$arr ) {
60 foreach( $arr as $key => $val ) {
61 if( is_array( $val ) ) {
62 $this->normalizeUnicode( $arr[$key ] );
63 } else {
64 $arr[$key] = UtfNormal::toNFC( $val );
65 }
66 }
67 }
68
69 function getGPCVal( &$arr, $name, $default ) {
70 if( isset( $arr[$name] ) ) {
71 return $arr[$name];
72 } else {
73 return $default;
74 }
75 }
76
77 function getGPCText( &$arr, $name, $default ) {
78 # Text fields may be in an alternate encoding which we should check.
79 # Also, strip CRLF line endings down to LF to achieve consistency.
80 global $wgLang;
81 if( isset( $arr[$name] ) ) {
82 return str_replace( "\r\n", "\n", $wgLang->recodeInput( $arr[$name] ) );
83 } else {
84 return $default;
85 }
86 }
87
88 function getVal( $name, $default = NULL ) {
89 return $this->getGPCVal( $_REQUEST, $name, $default );
90 }
91
92 function getInt( $name, $default = 0 ) {
93 return IntVal( $this->getVal( $name, $default ) );
94 }
95
96 function getBool( $name, $default = false ) {
97 return $this->getVal( $name, $default ) ? true : false;
98 }
99
100 function getCheck( $name ) {
101 # Checkboxes and buttons are only present when clicked
102 # Presence connotes truth, abscense false
103 $val = $this->getVal( $name, NULL );
104 return isset( $val );
105 }
106
107 function getText( $name, $default = '' ) {
108 return $this->getGPCText( $_REQUEST, $name, $default );
109 }
110
111 function getValues() {
112 $names = func_get_args();
113 if ( count( $names ) == 0 ) {
114 $names = array_keys( $_REQUEST );
115 }
116
117 $retVal = array();
118 foreach ( $names as $name ) {
119 $value = $this->getVal( $name );
120 if ( !is_null( $value ) ) {
121 $retVal[$name] = $value;
122 }
123 }
124 return $retVal;
125 }
126
127 function wasPosted() {
128 return $_SERVER['REQUEST_METHOD'] == 'POST';
129 }
130
131 function checkSessionCookie() {
132 return isset( $_COOKIE[ini_get('session.name')] );
133 }
134
135 function getRequestURL() {
136 return $_SERVER['REQUEST_URI'];
137 }
138
139 function getFullRequestURL() {
140 global $wgServer;
141 return $wgServer . $this->getRequestURL();
142 }
143
144 # Take an arbitrary query and rewrite the present URL to include it
145 function appendQuery( $query ) {
146 global $wgTitle;
147 $basequery = '';
148 foreach( $_GET as $var => $val ) {
149 if( $var == 'title' ) continue;
150 $basequery .= '&' . urlencode( $var ) . '=' . urlencode( $val );
151 }
152 $basequery .= '&' . $query;
153
154 # Trim the extra &
155 $basequery = substr( $basequery, 1 );
156 return $wgTitle->getLocalURL( $basequery );
157 }
158
159 function escapeAppendQuery( $query ) {
160 return htmlspecialchars( $this->appendQuery( $query ) );
161 }
162
163 function getLimitOffset( $deflimit = 50, $optionname = 'rclimit' ) {
164 global $wgUser;
165
166 $limit = $this->getInt( 'limit', 0 );
167 if( $limit < 0 ) $limit = 0;
168 if( ( $limit == 0 ) && ( $optionname != '' ) ) {
169 $limit = (int)$wgUser->getOption( $optionname );
170 }
171 if( $limit <= 0 ) $limit = $deflimit;
172 if( $limit > 5000 ) $limit = 5000; # We have *some* limits...
173
174 $offset = $this->getInt( 'offset', 0 );
175 if( $offset < 0 ) $offset = 0;
176
177 return array( $limit, $offset );
178 }
179 }
180
181 class FauxRequest extends WebRequest {
182 var $data = null;
183 var $wasPosted = false;
184
185 function WebRequest( $data, $wasPosted = false ) {
186 if( is_array( $data ) ) {
187 $this->data = $data;
188 } else {
189 wfDebugDieBacktrace( "FauxReqeust() got bogus data" );
190 }
191 $this->wasPosted = $wasPosted;
192 }
193
194 function getVal( $name, $default = NULL ) {
195 return $this->getGPCVal( $this->data, $name, $default );
196 }
197
198 function getText( $name, $default = '' ) {
199 # Override; don't recode since we're using internal data
200 return $this->getVal( $name, $default );
201 }
202
203 function getValues() {
204 return $this->data;
205 }
206
207 function wasPosted() {
208 return $this->wasPosted;
209 }
210
211 function checkSessionCookie() {
212 return false;
213 }
214
215 function getRequestURL() {
216 wfDebugDieBacktrace( 'FauxRequest::getRequestURL() not implemented' );
217 }
218
219 function appendQuery( $query ) {
220 wfDebugDieBacktrace( 'FauxRequest::appendQuery() not implemented' );
221 }
222
223 }
224
225 ?>