1ebc2438d7a8836bb7366342627f67f0549b0ead
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 require_once( 'normal/UtfNormal.php' );
31 wfProfileIn( 'WebRequest:normalizeUnicode-fix' );
32 $this->normalizeUnicode( $_REQUEST );
33 wfProfileOut( 'WebRequest:normalizeUnicode-fix' );
37 function &fix_magic_quotes( &$arr ) {
38 foreach( $arr as $key => $val ) {
39 if( is_array( $val ) ) {
40 $this->fix_magic_quotes( $arr[$key] );
42 $arr[$key] = stripslashes( $val );
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 );
59 function normalizeUnicode( &$arr ) {
60 foreach( $arr as $key => $val ) {
61 if( is_array( $val ) ) {
62 $this->normalizeUnicode( $arr[$key ] );
64 $arr[$key] = UtfNormal
::toNFC( $val );
69 function getGPCVal( &$arr, $name, $default ) {
70 if( isset( $arr[$name] ) ) {
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.
81 if( isset( $arr[$name] ) ) {
82 return str_replace( "\r\n", "\n", $wgLang->recodeInput( $arr[$name] ) );
88 function getVal( $name, $default = NULL ) {
89 return $this->getGPCVal( $_REQUEST, $name, $default );
92 function getInt( $name, $default = 0 ) {
93 return IntVal( $this->getVal( $name, $default ) );
96 function getBool( $name, $default = false ) {
97 return $this->getVal( $name, $default ) ?
true : false;
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 );
107 function getText( $name, $default = '' ) {
108 return $this->getGPCText( $_REQUEST, $name, $default );
111 function getValues() {
112 $names = func_get_args();
113 if ( count( $names ) == 0 ) {
114 $names = array_keys( $_REQUEST );
118 foreach ( $names as $name ) {
119 $value = $this->getVal( $name );
120 if ( !is_null( $value ) ) {
121 $retVal[$name] = $value;
127 function wasPosted() {
128 return $_SERVER['REQUEST_METHOD'] == 'POST';
131 function checkSessionCookie() {
132 return isset( $_COOKIE[ini_get('session.name')] );
135 function getRequestURL() {
136 return $_SERVER['REQUEST_URI'];
139 function getFullRequestURL() {
141 return $wgServer . $this->getRequestURL();
144 # Take an arbitrary query and rewrite the present URL to include it
145 function appendQuery( $query ) {
148 foreach( $_GET as $var => $val ) {
149 if( $var == 'title' ) continue;
150 $basequery .= '&' . urlencode( $var ) . '=' . urlencode( $val );
152 $basequery .= '&' . $query;
155 $basequery = substr( $basequery, 1 );
156 return $wgTitle->getLocalURL( $basequery );
159 function escapeAppendQuery( $query ) {
160 return htmlspecialchars( $this->appendQuery( $query ) );
163 function getLimitOffset( $deflimit = 50, $optionname = 'rclimit' ) {
166 $limit = $this->getInt( 'limit', 0 );
167 if( $limit < 0 ) $limit = 0;
168 if( ( $limit == 0 ) && ( $optionname != '' ) ) {
169 $limit = (int)$wgUser->getOption( $optionname );
171 if( $limit <= 0 ) $limit = $deflimit;
172 if( $limit > 5000 ) $limit = 5000; # We have *some* limits...
174 $offset = $this->getInt( 'offset', 0 );
175 if( $offset < 0 ) $offset = 0;
177 return array( $limit, $offset );
181 class FauxRequest
extends WebRequest
{
183 var $wasPosted = false;
185 function WebRequest( $data, $wasPosted = false ) {
186 if( is_array( $data ) ) {
189 wfDebugDieBacktrace( "FauxReqeust() got bogus data" );
191 $this->wasPosted
= $wasPosted;
194 function getVal( $name, $default = NULL ) {
195 return $this->getGPCVal( $this->data
, $name, $default );
198 function getText( $name, $default = '' ) {
199 # Override; don't recode since we're using internal data
200 return $this->getVal( $name, $default );
203 function getValues() {
207 function wasPosted() {
208 return $this->wasPosted
;
211 function checkSessionCookie() {
215 function getRequestURL() {
216 wfDebugDieBacktrace( 'FauxRequest::getRequestURL() not implemented' );
219 function appendQuery( $query ) {
220 wfDebugDieBacktrace( 'FauxRequest::appendQuery() not implemented' );