More globals and uninitialized variables fixes. Added WebRequest ($wgRequest)
[lhc/web/wiklou.git] / includes / WebRequest.php
1 <?php
2
3 # Hypothetically, we could use a WebRequest object to fake a
4 # self-contained request.
5
6 ## Enable this to debug total elimination of register_globals
7 #define( "DEBUG_GLOBALS", 1 );
8
9 # Deal with importing all those nasssty globals and things
10 class WebRequest {
11 function WebRequest() {
12 if( defined('DEBUG_GLOBALS') ) error_reporting(E_ALL);
13
14 $this->checkMagicQuotes();
15 $this->checkRegisterGlobals();
16 }
17
18 function &fix_magic_quotes( &$arr ) {
19 foreach( $arr as $key => $val ) {
20 if( is_array( $val ) ) {
21 $this->fix_magic_quotes( $arr[$key] );
22 } else {
23 $arr[$key] = stripslashes( $val );
24 }
25 }
26 return $arr;
27 }
28
29 function checkMagicQuotes() {
30 if ( get_magic_quotes_gpc() ) {
31 $this->fix_magic_quotes( $_COOKIE );
32 $this->fix_magic_quotes( $_ENV );
33 $this->fix_magic_quotes( $_GET );
34 $this->fix_magic_quotes( $_POST );
35 $this->fix_magic_quotes( $_REQUEST );
36 $this->fix_magic_quotes( $_SERVER );
37 } elseif( defined('DEBUG_GLOBALS') ) {
38 die("DEBUG_GLOBALS: turn on magic_quotes_gpc" );
39 }
40 }
41
42 function checkRegisterGlobals() {
43 if( ini_get( "register_globals" ) ) {
44 if( defined( "DEBUG_GLOBALS" ) ) {
45 die( "DEBUG_GLOBALS: Turn register_globals off!" );
46 }
47 } else {
48 if( !defined( "DEBUG_GLOBALS" ) ) {
49 # Insecure, but at least it'll run
50 import_request_variables( "GPC" );
51 }
52 }
53 }
54
55 function getGPCVal( &$arr, $name, $default ) {
56 if( isset( $arr[$name] ) ) {
57 return $arr[$name];
58 } else {
59 return $default;
60 }
61 }
62
63 function getGPCText( &$arr, $name, $default ) {
64 # Text fields may be in an alternate encoding which we should check.
65 # Also, strip CRLF line endings down to LF to achieve consistency.
66 global $wgLang;
67 if( isset( $arr[$name] ) ) {
68 return str_replace( "\r\n", "\n", $wgLang->recodeInput( $arr[$name] ) );
69 } else {
70 return $default;
71 }
72 }
73
74 function getVal( $name, $default = NULL ) {
75 return $this->getGPCVal( $_REQUEST, $name, $default );
76 }
77
78 function getInt( $name, $default = 0 ) {
79 return IntVal( $this->getVal( $name, $default ) );
80 }
81
82 function getBool( $name, $default = false ) {
83 return $this->getVal( $name, $default ) ? true : false;
84 }
85
86 function getCheck( $name ) {
87 # Checkboxes and buttons are only present when clicked
88 # Presence connotes truth, abscense false
89 $val = $this->getVal( $name, NULL );
90 return isset( $val );
91 }
92
93 function getText( $name, $default = "" ) {
94 return $this->getGPCText( $_REQUEST, $name, $default );
95 }
96
97 function wasPosted() {
98 return $_SERVER['REQUEST_METHOD'] == 'POST';
99 }
100
101 function checkSessionCookie() {
102 return isset( $_COOKIE[ini_get("session.name")] );
103 }
104 }
105
106 ?>