After a longer phone call Erik and me agreed on working on an equivalent feature...
[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 return $this->getVal( $name, $default ) ? true : false;
105 }
106
107 function getCheck( $name ) {
108 # Checkboxes and buttons are only present when clicked
109 # Presence connotes truth, abscense false
110 $val = $this->getVal( $name, NULL );
111 return isset( $val );
112 }
113
114 function getText( $name, $default = "" ) {
115 return $this->getGPCText( $_REQUEST, $name, $default );
116 }
117
118 function getValues() {
119 $names = func_get_args();
120 $retVal = array();
121 foreach ( $names as $name ) {
122 $value = $this->getVal( $name );
123 if ( !is_null( $value ) ) {
124 $retVal[$name] = $value;
125 }
126 }
127 return $retVal;
128 }
129
130 function wasPosted() {
131 return $_SERVER['REQUEST_METHOD'] == 'POST';
132 }
133
134 function checkSessionCookie() {
135 return isset( $_COOKIE[ini_get("session.name")] );
136 }
137
138 function getRequestURL() {
139 return $_SERVER['REQUEST_URI'];
140 }
141
142 function getFullRequestURL() {
143 global $wgServer;
144 return $wgServer . $this->getRequestURL();
145 }
146
147 # Take an arbitrary query and rewrite the present URL to include it
148 function appendQuery( $query ) {
149 global $wgTitle;
150 $basequery = "";
151 foreach( $_GET as $var => $val ) {
152 if( $var == "title" ) continue;
153 $basequery .= "&" . urlencode( $var ) . "=" . urlencode( $val );
154 }
155 $basequery .= "&" . $query;
156
157 # Trim the extra &
158 $basequery = substr( $basequery, 1 );
159 return $wgTitle->getLocalURL( $basequery );
160 }
161
162 function escapeAppendQuery( $query ) {
163 return htmlspecialchars( $this->appendQuery( $query ) );
164 }
165
166 }
167
168 ?>