3 * Deal with importing all those nasssty globals and things
6 # Copyright (C) 2003 Brion Vibber <brion@pobox.com>
7 # http://www.mediawiki.org/
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.
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.
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
25 * Hypothetically, we could use a WebRequest object to fake a
26 * self-contained request (FauxRequest).
29 function WebRequest() {
30 $this->checkMagicQuotes();
33 require_once( 'normal/UtfNormal.php' );
34 wfProfileIn( 'WebRequest:normalizeUnicode-fix' );
35 $this->normalizeUnicode( $_REQUEST );
36 wfProfileOut( 'WebRequest:normalizeUnicode-fix' );
40 function &fix_magic_quotes( &$arr ) {
41 foreach( $arr as $key => $val ) {
42 if( is_array( $val ) ) {
43 $this->fix_magic_quotes( $arr[$key] );
45 $arr[$key] = stripslashes( $val );
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 );
62 function normalizeUnicode( &$arr ) {
63 foreach( $arr as $key => $val ) {
64 if( is_array( $val ) ) {
65 $this->normalizeUnicode( $arr[$key ] );
67 $arr[$key] = UtfNormal
::toNFC( $val );
72 function getGPCVal( &$arr, $name, $default ) {
73 if( isset( $arr[$name] ) ) {
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.
84 if( isset( $arr[$name] ) ) {
85 return str_replace( "\r\n", "\n", $wgLang->recodeInput( $arr[$name] ) );
91 function getVal( $name, $default = NULL ) {
92 return $this->getGPCVal( $_REQUEST, $name, $default );
95 function getInt( $name, $default = 0 ) {
96 return IntVal( $this->getVal( $name, $default ) );
99 function getBool( $name, $default = false ) {
100 return $this->getVal( $name, $default ) ?
true : false;
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 );
110 function getText( $name, $default = '' ) {
111 return $this->getGPCText( $_REQUEST, $name, $default );
114 function getValues() {
115 $names = func_get_args();
116 if ( count( $names ) == 0 ) {
117 $names = array_keys( $_REQUEST );
121 foreach ( $names as $name ) {
122 $value = $this->getVal( $name );
123 if ( !is_null( $value ) ) {
124 $retVal[$name] = $value;
130 function wasPosted() {
131 return $_SERVER['REQUEST_METHOD'] == 'POST';
134 function checkSessionCookie() {
135 return isset( $_COOKIE[ini_get('session.name')] );
138 function getRequestURL() {
139 return $_SERVER['REQUEST_URI'];
142 function getFullRequestURL() {
144 return $wgServer . $this->getRequestURL();
148 * Take an arbitrary query and rewrite the present URL to include it
150 function appendQuery( $query ) {
153 foreach( $_GET as $var => $val ) {
154 if( $var == 'title' ) continue;
155 $basequery .= '&' . urlencode( $var ) . '=' . urlencode( $val );
157 $basequery .= '&' . $query;
160 $basequery = substr( $basequery, 1 );
161 return $wgTitle->getLocalURL( $basequery );
164 function escapeAppendQuery( $query ) {
165 return htmlspecialchars( $this->appendQuery( $query ) );
168 function getLimitOffset( $deflimit = 50, $optionname = 'rclimit' ) {
171 $limit = $this->getInt( 'limit', 0 );
172 if( $limit < 0 ) $limit = 0;
173 if( ( $limit == 0 ) && ( $optionname != '' ) ) {
174 $limit = (int)$wgUser->getOption( $optionname );
176 if( $limit <= 0 ) $limit = $deflimit;
177 if( $limit > 5000 ) $limit = 5000; # We have *some* limits...
179 $offset = $this->getInt( 'offset', 0 );
180 if( $offset < 0 ) $offset = 0;
182 return array( $limit, $offset );
189 class FauxRequest
extends WebRequest
{
191 var $wasPosted = false;
193 function WebRequest( $data, $wasPosted = false ) {
194 if( is_array( $data ) ) {
197 wfDebugDieBacktrace( "FauxReqeust() got bogus data" );
199 $this->wasPosted
= $wasPosted;
202 function getVal( $name, $default = NULL ) {
203 return $this->getGPCVal( $this->data
, $name, $default );
206 function getText( $name, $default = '' ) {
207 # Override; don't recode since we're using internal data
208 return $this->getVal( $name, $default );
211 function getValues() {
215 function wasPosted() {
216 return $this->wasPosted
;
219 function checkSessionCookie() {
223 function getRequestURL() {
224 wfDebugDieBacktrace( 'FauxRequest::getRequestURL() not implemented' );
227 function appendQuery( $query ) {
228 wfDebugDieBacktrace( 'FauxRequest::appendQuery() not implemented' );