Normalize Unicode input to normalization form C. Most of the time input
[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 $this->normalizeUnicode();
31 }
32 }
33
34 function &fix_magic_quotes( &$arr ) {
35 foreach( $arr as $key => $val ) {
36 if( is_array( $val ) ) {
37 $this->fix_magic_quotes( $arr[$key] );
38 } else {
39 $arr[$key] = stripslashes( $val );
40 }
41 }
42 return $arr;
43 }
44
45 function checkMagicQuotes() {
46 if ( get_magic_quotes_gpc() ) {
47 $this->fix_magic_quotes( $_COOKIE );
48 $this->fix_magic_quotes( $_ENV );
49 $this->fix_magic_quotes( $_GET );
50 $this->fix_magic_quotes( $_POST );
51 $this->fix_magic_quotes( $_REQUEST );
52 $this->fix_magic_quotes( $_SERVER );
53 }
54 }
55
56 function normalizeUnicode() {
57 wfProfileIn( 'WebRequest:normalizeUnicode-include' );
58 require_once( 'normal/UtfNormal.php' );
59 wfProfileOut( 'WebRequest:normalizeUnicode-include' );
60 wfProfileIn( 'WebRequest:normalizeUnicode-fix' );
61 foreach( $_REQUEST as $key => $val ) {
62 $_REQUEST[$key] = UtfNormal::toNFC( $val );
63 }
64 wfProfileOut( 'WebRequest:normalizeUnicode-fix' );
65 }
66
67 function getGPCVal( &$arr, $name, $default ) {
68 if( isset( $arr[$name] ) ) {
69 return $arr[$name];
70 } else {
71 return $default;
72 }
73 }
74
75 function getGPCText( &$arr, $name, $default ) {
76 # Text fields may be in an alternate encoding which we should check.
77 # Also, strip CRLF line endings down to LF to achieve consistency.
78 global $wgLang;
79 if( isset( $arr[$name] ) ) {
80 return str_replace( "\r\n", "\n", $wgLang->recodeInput( $arr[$name] ) );
81 } else {
82 return $default;
83 }
84 }
85
86 function getVal( $name, $default = NULL ) {
87 return $this->getGPCVal( $_REQUEST, $name, $default );
88 }
89
90 function getInt( $name, $default = 0 ) {
91 return IntVal( $this->getVal( $name, $default ) );
92 }
93
94 function getBool( $name, $default = false ) {
95 return $this->getVal( $name, $default ) ? true : false;
96 }
97
98 function getCheck( $name ) {
99 # Checkboxes and buttons are only present when clicked
100 # Presence connotes truth, abscense false
101 $val = $this->getVal( $name, NULL );
102 return isset( $val );
103 }
104
105 function getText( $name, $default = '' ) {
106 return $this->getGPCText( $_REQUEST, $name, $default );
107 }
108
109 function getValues() {
110 $names = func_get_args();
111 if ( count( $names ) == 0 ) {
112 $names = array_keys( $_REQUEST );
113 }
114
115 $retVal = array();
116 foreach ( $names as $name ) {
117 $value = $this->getVal( $name );
118 if ( !is_null( $value ) ) {
119 $retVal[$name] = $value;
120 }
121 }
122 return $retVal;
123 }
124
125 function wasPosted() {
126 return $_SERVER['REQUEST_METHOD'] == 'POST';
127 }
128
129 function checkSessionCookie() {
130 return isset( $_COOKIE[ini_get('session.name')] );
131 }
132
133 function getRequestURL() {
134 return $_SERVER['REQUEST_URI'];
135 }
136
137 function getFullRequestURL() {
138 global $wgServer;
139 return $wgServer . $this->getRequestURL();
140 }
141
142 # Take an arbitrary query and rewrite the present URL to include it
143 function appendQuery( $query ) {
144 global $wgTitle;
145 $basequery = '';
146 foreach( $_GET as $var => $val ) {
147 if( $var == 'title' ) continue;
148 $basequery .= '&' . urlencode( $var ) . '=' . urlencode( $val );
149 }
150 $basequery .= '&' . $query;
151
152 # Trim the extra &
153 $basequery = substr( $basequery, 1 );
154 return $wgTitle->getLocalURL( $basequery );
155 }
156
157 function escapeAppendQuery( $query ) {
158 return htmlspecialchars( $this->appendQuery( $query ) );
159 }
160
161 function getLimitOffset( $deflimit = 50, $optionname = 'rclimit' ) {
162 global $wgUser;
163
164 $limit = $this->getInt( 'limit', 0 );
165 if( $limit < 0 ) $limit = 0;
166 if( ( $limit == 0 ) && ( $optionname != '' ) ) {
167 $limit = (int)$wgUser->getOption( $optionname );
168 }
169 if( $limit <= 0 ) $limit = $deflimit;
170 if( $limit > 5000 ) $limit = 5000; # We have *some* limits...
171
172 $offset = $this->getInt( 'offset', 0 );
173 if( $offset < 0 ) $offset = 0;
174
175 return array( $limit, $offset );
176 }
177 }
178
179 class FauxRequest extends WebRequest {
180 var $data = null;
181 var $wasPosted = false;
182
183 function WebRequest( $data, $wasPosted = false ) {
184 if( is_array( $data ) ) {
185 $this->data = $data;
186 } else {
187 wfDebugDieBacktrace( "FauxReqeust() got bogus data" );
188 }
189 $this->wasPosted = $wasPosted;
190 }
191
192 function getVal( $name, $default = NULL ) {
193 return $this->getGPCVal( $this->data, $name, $default );
194 }
195
196 function getText( $name, $default = '' ) {
197 # Override; don't recode since we're using internal data
198 return $this->getVal( $name, $default );
199 }
200
201 function getValues() {
202 return $this->data;
203 }
204
205 function wasPosted() {
206 return $this->wasPosted;
207 }
208
209 function checkSessionCookie() {
210 return false;
211 }
212
213 function getRequestURL() {
214 wfDebugDieBacktrace( 'FauxRequest::getRequestURL() not implemented' );
215 }
216
217 function appendQuery( $query ) {
218 wfDebugDieBacktrace( 'FauxRequest::appendQuery() not implemented' );
219 }
220
221 }
222
223 ?>