restore section folding
[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 }
66 /*
67 else {
68 if( !defined( "DEBUG_GLOBALS" ) ) {
69 # Insecure, but at least it'll run
70 import_request_variables( "GPC" );
71 }
72 }
73 */
74 }
75
76 function getGPCVal( &$arr, $name, $default ) {
77 if( isset( $arr[$name] ) ) {
78 return $arr[$name];
79 } else {
80 return $default;
81 }
82 }
83
84 function getGPCText( &$arr, $name, $default ) {
85 # Text fields may be in an alternate encoding which we should check.
86 # Also, strip CRLF line endings down to LF to achieve consistency.
87 global $wgLang;
88 if( isset( $arr[$name] ) ) {
89 return str_replace( "\r\n", "\n", $wgLang->recodeInput( $arr[$name] ) );
90 } else {
91 return $default;
92 }
93 }
94
95 function getVal( $name, $default = NULL ) {
96 return $this->getGPCVal( $_REQUEST, $name, $default );
97 }
98
99 function getInt( $name, $default = 0 ) {
100 return IntVal( $this->getVal( $name, $default ) );
101 }
102
103 function getBool( $name, $default = false ) {
104 $val=$this->getVal( $name, $default );
105 if($val=="false") { $val=false; }
106 return $val ? true : false;
107 }
108
109 function getCheck( $name ) {
110 # Checkboxes and buttons are only present when clicked
111 # Presence connotes truth, abscense false
112 $val = $this->getVal( $name, NULL );
113 return isset( $val );
114 }
115
116 function getText( $name, $default = "" ) {
117 return $this->getGPCText( $_REQUEST, $name, $default );
118 }
119
120 function getValues() {
121 $names = func_get_args();
122 $retVal = array();
123 foreach ( $names as $name ) {
124 $value = $this->getVal( $name );
125 if ( !is_null( $value ) ) {
126 $retVal[$name] = $value;
127 }
128 }
129 return $retVal;
130 }
131
132 function wasPosted() {
133 return $_SERVER['REQUEST_METHOD'] == 'POST';
134 }
135
136 function checkSessionCookie() {
137 return isset( $_COOKIE[ini_get("session.name")] );
138 }
139
140 function getRequestURL() {
141 return $_SERVER['REQUEST_URI'];
142 }
143
144 function getFullRequestURL() {
145 global $wgServer;
146 return $wgServer . $this->getRequestURL();
147 }
148
149 # Take an arbitrary query and rewrite the present URL to include it
150 function appendQuery( $query ) {
151 global $wgTitle;
152 $basequery = "";
153 foreach( $_GET as $var => $val ) {
154 if( $var == "title" ) continue;
155 $basequery .= "&" . urlencode( $var ) . "=" . urlencode( $val );
156 }
157 $basequery .= "&" . $query;
158
159 # Trim the extra &
160 $basequery = substr( $basequery, 1 );
161 return $wgTitle->getLocalURL( $basequery );
162 }
163
164 function escapeAppendQuery( $query ) {
165 return htmlspecialchars( $this->appendQuery( $query ) );
166 }
167
168 }
169
170 ?>