2 # Deal with importing all those nasssty globals and things
4 # Copyright (C) 2003 Brion Vibber <brion@pobox.com>
5 # http://www.mediawiki.org/
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.
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.
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
22 # Hypothetically, we could use a WebRequest object to fake a
23 # self-contained request (FauxRequest).
26 function WebRequest() {
27 $this->checkMagicQuotes();
30 $this->normalizeUnicode();
34 function &fix_magic_quotes( &$arr ) {
35 foreach( $arr as $key => $val ) {
36 if( is_array( $val ) ) {
37 $this->fix_magic_quotes( $arr[$key] );
39 $arr[$key] = stripslashes( $val );
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 );
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 );
64 wfProfileOut( 'WebRequest:normalizeUnicode-fix' );
67 function getGPCVal( &$arr, $name, $default ) {
68 if( isset( $arr[$name] ) ) {
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.
79 if( isset( $arr[$name] ) ) {
80 return str_replace( "\r\n", "\n", $wgLang->recodeInput( $arr[$name] ) );
86 function getVal( $name, $default = NULL ) {
87 return $this->getGPCVal( $_REQUEST, $name, $default );
90 function getInt( $name, $default = 0 ) {
91 return IntVal( $this->getVal( $name, $default ) );
94 function getBool( $name, $default = false ) {
95 return $this->getVal( $name, $default ) ?
true : false;
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 );
105 function getText( $name, $default = '' ) {
106 return $this->getGPCText( $_REQUEST, $name, $default );
109 function getValues() {
110 $names = func_get_args();
111 if ( count( $names ) == 0 ) {
112 $names = array_keys( $_REQUEST );
116 foreach ( $names as $name ) {
117 $value = $this->getVal( $name );
118 if ( !is_null( $value ) ) {
119 $retVal[$name] = $value;
125 function wasPosted() {
126 return $_SERVER['REQUEST_METHOD'] == 'POST';
129 function checkSessionCookie() {
130 return isset( $_COOKIE[ini_get('session.name')] );
133 function getRequestURL() {
134 return $_SERVER['REQUEST_URI'];
137 function getFullRequestURL() {
139 return $wgServer . $this->getRequestURL();
142 # Take an arbitrary query and rewrite the present URL to include it
143 function appendQuery( $query ) {
146 foreach( $_GET as $var => $val ) {
147 if( $var == 'title' ) continue;
148 $basequery .= '&' . urlencode( $var ) . '=' . urlencode( $val );
150 $basequery .= '&' . $query;
153 $basequery = substr( $basequery, 1 );
154 return $wgTitle->getLocalURL( $basequery );
157 function escapeAppendQuery( $query ) {
158 return htmlspecialchars( $this->appendQuery( $query ) );
161 function getLimitOffset( $deflimit = 50, $optionname = 'rclimit' ) {
164 $limit = $this->getInt( 'limit', 0 );
165 if( $limit < 0 ) $limit = 0;
166 if( ( $limit == 0 ) && ( $optionname != '' ) ) {
167 $limit = (int)$wgUser->getOption( $optionname );
169 if( $limit <= 0 ) $limit = $deflimit;
170 if( $limit > 5000 ) $limit = 5000; # We have *some* limits...
172 $offset = $this->getInt( 'offset', 0 );
173 if( $offset < 0 ) $offset = 0;
175 return array( $limit, $offset );
179 class FauxRequest
extends WebRequest
{
181 var $wasPosted = false;
183 function WebRequest( $data, $wasPosted = false ) {
184 if( is_array( $data ) ) {
187 wfDebugDieBacktrace( "FauxReqeust() got bogus data" );
189 $this->wasPosted
= $wasPosted;
192 function getVal( $name, $default = NULL ) {
193 return $this->getGPCVal( $this->data
, $name, $default );
196 function getText( $name, $default = '' ) {
197 # Override; don't recode since we're using internal data
198 return $this->getVal( $name, $default );
201 function getValues() {
205 function wasPosted() {
206 return $this->wasPosted
;
209 function checkSessionCookie() {
213 function getRequestURL() {
214 wfDebugDieBacktrace( 'FauxRequest::getRequestURL() not implemented' );
217 function appendQuery( $query ) {
218 wfDebugDieBacktrace( 'FauxRequest::appendQuery() not implemented' );