comment
[lhc/web/wiklou.git] / includes / WebRequest.php
1 <?php
2 # Deal with importing all those nasssty globals and things
3 #
4 # Copyright (C) 2003 Brion Vibber <brion@pobox.com>
5 # http://www.mediawiki.org/
6 #
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.
11 #
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.
16 #
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
21
22 # Hypothetically, we could use a WebRequest object to fake a
23 # self-contained request.
24
25 ## Enable this to debug total elimination of register_globals
26 #define( "DEBUG_GLOBALS", 1 );
27
28 class WebRequest {
29 function WebRequest() {
30 if( defined('DEBUG_GLOBALS') ) error_reporting(E_ALL);
31
32 $this->checkMagicQuotes();
33 $this->checkRegisterGlobals();
34 }
35
36 function &fix_magic_quotes( &$arr ) {
37 foreach( $arr as $key => $val ) {
38 if( is_array( $val ) ) {
39 $this->fix_magic_quotes( $arr[$key] );
40 } else {
41 $arr[$key] = stripslashes( $val );
42 }
43 }
44 return $arr;
45 }
46
47 function checkMagicQuotes() {
48 if ( get_magic_quotes_gpc() ) {
49 $this->fix_magic_quotes( $_COOKIE );
50 $this->fix_magic_quotes( $_ENV );
51 $this->fix_magic_quotes( $_GET );
52 $this->fix_magic_quotes( $_POST );
53 $this->fix_magic_quotes( $_REQUEST );
54 $this->fix_magic_quotes( $_SERVER );
55 } elseif( defined('DEBUG_GLOBALS') ) {
56 die("DEBUG_GLOBALS: turn on magic_quotes_gpc" );
57 }
58 }
59
60 function checkRegisterGlobals() {
61 if( ini_get( "register_globals" ) ) {
62 if( defined( "DEBUG_GLOBALS" ) ) {
63 die( "DEBUG_GLOBALS: Turn register_globals off!" );
64 }
65 } else {
66 if( !defined( "DEBUG_GLOBALS" ) ) {
67 # Insecure, but at least it'll run
68 import_request_variables( "GPC" );
69 }
70 }
71 }
72
73 function getGPCVal( &$arr, $name, $default ) {
74 if( isset( $arr[$name] ) ) {
75 return $arr[$name];
76 } else {
77 return $default;
78 }
79 }
80
81 function getGPCText( &$arr, $name, $default ) {
82 # Text fields may be in an alternate encoding which we should check.
83 # Also, strip CRLF line endings down to LF to achieve consistency.
84 global $wgLang;
85 if( isset( $arr[$name] ) ) {
86 return str_replace( "\r\n", "\n", $wgLang->recodeInput( $arr[$name] ) );
87 } else {
88 return $default;
89 }
90 }
91
92 function getVal( $name, $default = NULL ) {
93 return $this->getGPCVal( $_REQUEST, $name, $default );
94 }
95
96 function getInt( $name, $default = 0 ) {
97 return IntVal( $this->getVal( $name, $default ) );
98 }
99
100 function getBool( $name, $default = false ) {
101 return $this->getVal( $name, $default ) ? true : false;
102 }
103
104 function getCheck( $name ) {
105 # Checkboxes and buttons are only present when clicked
106 # Presence connotes truth, abscense false
107 $val = $this->getVal( $name, NULL );
108 return isset( $val );
109 }
110
111 function getText( $name, $default = "" ) {
112 return $this->getGPCText( $_REQUEST, $name, $default );
113 }
114
115 function getValues() {
116 $names = func_get_args();
117 $retVal = array();
118 foreach ( $names as $name ) {
119 $value = $this->getVal( $name );
120 if ( !is_null( $value ) ) {
121 $retVal[$name] = $value;
122 }
123 }
124 return $retVal;
125 }
126
127 function wasPosted() {
128 return $_SERVER['REQUEST_METHOD'] == 'POST';
129 }
130
131 function checkSessionCookie() {
132 return isset( $_COOKIE[ini_get("session.name")] );
133 }
134
135 function getRequestURL() {
136 return $_SERVER['REQUEST_URI'];
137 }
138
139 function getFullRequestURL() {
140 global $wgServer;
141 return $wgServer . $this->getRequestURL();
142 }
143
144 # Take an arbitrary query and rewrite the present URL to include it
145 function appendQuery( $query ) {
146 global $wgTitle;
147 $basequery = "";
148 foreach( $_GET as $var => $val ) {
149 if( $var == "title" ) continue;
150 $basequery .= "&" . urlencode( $var ) . "=" . urlencode( $val );
151 }
152 $basequery .= "&" . $query;
153
154 # Trim the extra &
155 $basequery = substr( $basequery, 1 );
156 return $wgTitle->getLocalURL( $basequery );
157 }
158
159 function escapeAppendQuery( $query ) {
160 return htmlspecialchars( $this->appendQuery( $query ) );
161 }
162
163 }
164
165 ?>