massive double to single quotes conversion. I have not noticed any bug after a lot...
[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
27 class WebRequest {
28 function WebRequest() {
29 $this->checkMagicQuotes();
30 }
31
32 function &fix_magic_quotes( &$arr ) {
33 foreach( $arr as $key => $val ) {
34 if( is_array( $val ) ) {
35 $this->fix_magic_quotes( $arr[$key] );
36 } else {
37 $arr[$key] = stripslashes( $val );
38 }
39 }
40 return $arr;
41 }
42
43 function checkMagicQuotes() {
44 if ( get_magic_quotes_gpc() ) {
45 $this->fix_magic_quotes( $_COOKIE );
46 $this->fix_magic_quotes( $_ENV );
47 $this->fix_magic_quotes( $_GET );
48 $this->fix_magic_quotes( $_POST );
49 $this->fix_magic_quotes( $_REQUEST );
50 $this->fix_magic_quotes( $_SERVER );
51 }
52 }
53
54 function getGPCVal( &$arr, $name, $default ) {
55 if( isset( $arr[$name] ) ) {
56 return $arr[$name];
57 } else {
58 return $default;
59 }
60 }
61
62 function getGPCText( &$arr, $name, $default ) {
63 # Text fields may be in an alternate encoding which we should check.
64 # Also, strip CRLF line endings down to LF to achieve consistency.
65 global $wgLang;
66 if( isset( $arr[$name] ) ) {
67 return str_replace( "\r\n", "\n", $wgLang->recodeInput( $arr[$name] ) );
68 } else {
69 return $default;
70 }
71 }
72
73 function getVal( $name, $default = NULL ) {
74 return $this->getGPCVal( $_REQUEST, $name, $default );
75 }
76
77 function getInt( $name, $default = 0 ) {
78 return IntVal( $this->getVal( $name, $default ) );
79 }
80
81 function getBool( $name, $default = false ) {
82 return $this->getVal( $name, $default ) ? true : false;
83 }
84
85 function getCheck( $name ) {
86 # Checkboxes and buttons are only present when clicked
87 # Presence connotes truth, abscense false
88 $val = $this->getVal( $name, NULL );
89 return isset( $val );
90 }
91
92 function getText( $name, $default = '' ) {
93 return $this->getGPCText( $_REQUEST, $name, $default );
94 }
95
96 function getValues() {
97 $names = func_get_args();
98 if ( count( $names ) == 0 ) {
99 $names = array_keys( $_REQUEST );
100 }
101
102 $retVal = array();
103 foreach ( $names as $name ) {
104 $value = $this->getVal( $name );
105 if ( !is_null( $value ) ) {
106 $retVal[$name] = $value;
107 }
108 }
109 return $retVal;
110 }
111
112 function wasPosted() {
113 return $_SERVER['REQUEST_METHOD'] == 'POST';
114 }
115
116 function checkSessionCookie() {
117 return isset( $_COOKIE[ini_get('session.name')] );
118 }
119
120 function getRequestURL() {
121 return $_SERVER['REQUEST_URI'];
122 }
123
124 function getFullRequestURL() {
125 global $wgServer;
126 return $wgServer . $this->getRequestURL();
127 }
128
129 # Take an arbitrary query and rewrite the present URL to include it
130 function appendQuery( $query ) {
131 global $wgTitle;
132 $basequery = '';
133 foreach( $_GET as $var => $val ) {
134 if( $var == 'title' ) continue;
135 $basequery .= '&' . urlencode( $var ) . '=' . urlencode( $val );
136 }
137 $basequery .= '&' . $query;
138
139 # Trim the extra &
140 $basequery = substr( $basequery, 1 );
141 return $wgTitle->getLocalURL( $basequery );
142 }
143
144 function escapeAppendQuery( $query ) {
145 return htmlspecialchars( $this->appendQuery( $query ) );
146 }
147
148 }
149
150 ?>