00001 <?php
00002
# Deal with importing all those nasssty globals and things
00003
#
00004
# Copyright (C) 2003 Brion Vibber <brion@pobox.com>
00005
# http://www.mediawiki.org/
00006
#
00007
# This program is free software; you can redistribute it and/or modify
00008
# it under the terms of the GNU General Public License as published by
00009
# the Free Software Foundation; either version 2 of the License, or
00010
# (at your option) any later version.
00011
#
00012
# This program is distributed in the hope that it will be useful,
00013
# but WITHOUT ANY WARRANTY; without even the implied warranty of
00014
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
00015
# GNU General Public License for more details.
00016
#
00017
# You should have received a copy of the GNU General Public License along
00018
# with this program; if not, write to the Free Software Foundation, Inc.,
00019
# 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
00020
# http://www.gnu.org/copyleft/gpl.html
00021
00022
# Hypothetically, we could use a WebRequest object to fake a
00023
# self-contained request.
00024
00025
## Enable this to debug total elimination of register_globals
00026
#define( "DEBUG_GLOBALS", 1 );
00027
00028 class WebRequest {
00029 function
WebRequest() {
00030
if( defined('DEBUG_GLOBALS') ) error_reporting(E_ALL);
00031
00032 $this->
checkMagicQuotes();
00033 $this->
checkRegisterGlobals();
00034 }
00035
00036 function &
fix_magic_quotes( &$arr ) {
00037 foreach( $arr as $key => $val ) {
00038
if( is_array( $val ) ) {
00039 $this->fix_magic_quotes( $arr[$key] );
00040 }
else {
00041 $arr[$key] = stripslashes( $val );
00042 }
00043 }
00044
return $arr;
00045 }
00046
00047 function
checkMagicQuotes() {
00048
if ( get_magic_quotes_gpc() ) {
00049 $this->fix_magic_quotes( $_COOKIE );
00050 $this->fix_magic_quotes( $_ENV );
00051 $this->fix_magic_quotes( $_GET );
00052 $this->fix_magic_quotes( $_POST );
00053 $this->fix_magic_quotes( $_REQUEST );
00054 $this->fix_magic_quotes( $_SERVER );
00055 } elseif( defined('DEBUG_GLOBALS') ) {
00056 die(
"DEBUG_GLOBALS: turn on magic_quotes_gpc" );
00057 }
00058 }
00059
00060 function
checkRegisterGlobals() {
00061
if( ini_get(
"register_globals" ) ) {
00062
if( defined(
"DEBUG_GLOBALS" ) ) {
00063 die(
"DEBUG_GLOBALS: Turn register_globals off!" );
00064 }
00065 }
00066
00067
00068
00069
00070
00071
00072
00073
00074 }
00075
00076 function
getGPCVal( &$arr, $name, $
default ) {
00077
if( isset( $arr[$
name] ) ) {
00078
return $arr[$name];
00079 }
else {
00080
return $default;
00081 }
00082 }
00083
00084 function
getGPCText( &$arr, $name, $
default ) {
00085
# Text fields may be in an alternate encoding which we should check.
00086
# Also, strip CRLF line endings down to LF to achieve consistency.
00087
global
$wgLang;
00088
if( isset( $arr[$
name] ) ) {
00089
return str_replace(
"\r\n",
"\n", $wgLang->recodeInput( $arr[$
name] ) );
00090 }
else {
00091
return $default;
00092 }
00093 }
00094
00095 function
getVal( $name, $
default = NULL ) {
00096
return $this->getGPCVal( $_REQUEST, $
name, $
default );
00097 }
00098
00099 function
getInt( $name, $
default = 0 ) {
00100
return IntVal( $this->getVal( $
name, $
default ) );
00101 }
00102
00103 function
getBool( $name, $
default =
false ) {
00104
return $this->getVal( $
name, $
default ) ?
true :
false;
00105 }
00106
00107 function
getCheck( $name ) {
00108
# Checkboxes and buttons are only present when clicked
00109
# Presence connotes truth, abscense false
00110
$val = $this->getVal( $
name, NULL );
00111
return isset( $val );
00112 }
00113
00114 function
getText( $name, $
default =
"" ) {
00115
return $this->getGPCText( $_REQUEST, $
name, $
default );
00116 }
00117
00118 function
getValues() {
00119 $names = func_get_args();
00120 $retVal = array();
00121 foreach ( $names as $
name ) {
00122 $value = $this->getVal( $
name );
00123
if ( !is_null( $value ) ) {
00124 $retVal[$name] = $value;
00125 }
00126 }
00127
return $retVal;
00128 }
00129
00130 function
wasPosted() {
00131
return $_SERVER['REQUEST_METHOD'] == 'POST';
00132 }
00133
00134 function
checkSessionCookie() {
00135
return isset( $_COOKIE[ini_get(
"session.name")] );
00136 }
00137
00138 function
getRequestURL() {
00139
return $_SERVER['REQUEST_URI'];
00140 }
00141
00142 function
getFullRequestURL() {
00143 global $wgServer;
00144
return $wgServer . $this->
getRequestURL();
00145 }
00146
00147
# Take an arbitrary query and rewrite the present URL to include it
00148 function
appendQuery( $query ) {
00149 global
$wgTitle;
00150 $basequery =
"";
00151 foreach( $_GET as $var => $val ) {
00152
if( $var ==
"title" )
continue;
00153 $basequery .=
"&" . urlencode( $var ) .
"=" . urlencode( $val );
00154 }
00155 $basequery .=
"&" . $query;
00156
00157
# Trim the extra &
00158
$basequery = substr( $basequery, 1 );
00159
return $wgTitle->getLocalURL( $basequery );
00160 }
00161
00162 function
escapeAppendQuery( $query ) {
00163
return htmlspecialchars( $this->appendQuery( $query ) );
00164 }
00165
00166 }
00167
00168 ?>