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