Explicitly define for old php versions when it's undefined
[squirrelmail.git] / functions / global.php
1 <?php
2
3 /**
4 * globals.php
5 *
6 * Copyright (c) 1999-2003 The SquirrelMail Project Team
7 * Licensed under the GNU GPL. For full terms see the file COPYING.
8 *
9 * This includes code to update < 4.1.0 globals to the newer format
10 * It also has some session register functions that work across various
11 * php versions.
12 *
13 * $Id$
14 */
15
16 require_once(SM_PATH . 'config/config.php');
17
18 /* set the name of the session cookie */
19 if(isset($session_name) && $session_name) {
20 ini_set('session.name' , $session_name);
21 } else {
22 ini_set('session.name' , 'SQMSESSID');
23 }
24
25 /* If magic_quotes_runtime is on, SquirrelMail breaks in new and creative ways.
26 * Force magic_quotes_runtime off.
27 * chilts@birdbrained.org - I put it here in the hopes that all SM code includes this.
28 * If there's a better place, please let me know.
29 */
30 ini_set('magic_quotes_runtime','0');
31
32 /* convert old-style superglobals to current method
33 * this is executed if you are running PHP 4.0.x.
34 * it is run via a require_once directive in validate.php
35 * and redirect.php. Patch submitted by Ray Black.
36 */
37
38 if ( !check_php_version(4,1) ) {
39 global $_COOKIE, $_ENV, $_FILES, $_GET, $_POST, $_SERVER, $_SESSION;
40 global $HTTP_COOKIE_VARS, $HTTP_ENV_VARS, $HTTP_POST_FILES, $HTTP_GET_VARS,
41 $HTTP_POST_VARS, $HTTP_SERVER_VARS, $HTTP_SESSION_VARS, $PHP_SELF;
42 $_COOKIE =& $HTTP_COOKIE_VARS;
43 $_ENV =& $HTTP_ENV_VARS;
44 $_FILES =& $HTTP_POST_FILES;
45 $_GET =& $HTTP_GET_VARS;
46 $_POST =& $HTTP_POST_VARS;
47 $_SERVER =& $HTTP_SERVER_VARS;
48 $_SESSION =& $HTTP_SESSION_VARS;
49 if (!isset($PHP_SELF) || empty($PHP_SELF)) {
50 $PHP_SELF = $HTTP_SERVER_VARS['PHP_SELF'];
51 }
52 }
53
54 /* if running with magic_quotes_gpc then strip the slashes
55 from POST and GET global arrays */
56
57 if (get_magic_quotes_gpc()) {
58 sqstripslashes($_GET);
59 sqstripslashes($_POST);
60 }
61
62 /* strip any tags added to the url from PHP_SELF.
63 This fixes hand crafted url XXS expoits for any
64 page that uses PHP_SELF as the FORM action */
65
66 $_SERVER['PHP_SELF'] = strip_tags($_SERVER['PHP_SELF']);
67
68 /**
69 * returns true if current php version is at mimimum a.b.c
70 *
71 * Called: check_php_version(4,1)
72 */
73 function check_php_version ($a = '0', $b = '0', $c = '0')
74 {
75 global $SQ_PHP_VERSION;
76
77 if(!isset($SQ_PHP_VERSION))
78 $SQ_PHP_VERSION = substr( str_pad( preg_replace('/\D/','', PHP_VERSION), 3, '0'), 0, 3);
79
80 return $SQ_PHP_VERSION >= ($a.$b.$c);
81 }
82
83 /**
84 * returns true if the current internal SM version is at minimum a.b.c
85 * These are plain integer comparisons, as our internal version is
86 * constructed by us, as an array of 3 ints.
87 *
88 * Called: check_sm_version(1,3,3)
89 */
90 function check_sm_version($a = 0, $b = 0, $c = 0)
91 {
92 global $SQM_INTERNAL_VERSION;
93 if ( !isset($SQM_INTERNAL_VERSION) ||
94 $SQM_INTERNAL_VERSION[0] < $a ||
95 $SQM_INTERNAL_VERSION[1] < $b ||
96 ( $SQM_INTERNAL_VERSION[1] == $b &&
97 $SQM_INTERNAL_VERSION[2] < $c ) ) {
98 return FALSE;
99 }
100 return TRUE;
101 }
102
103
104 /* recursively strip slashes from the values of an array */
105 function sqstripslashes(&$array) {
106 if(count($array) > 0) {
107 foreach ($array as $index=>$value) {
108 if (is_array($array[$index])) {
109 sqstripslashes($array[$index]);
110 }
111 else {
112 $array[$index] = stripslashes($value);
113 }
114 }
115 }
116 }
117
118 function sqsession_register ($var, $name) {
119
120 sqsession_is_active();
121
122 if ( !check_php_version(4,1) ) {
123 global $HTTP_SESSION_VARS;
124 $HTTP_SESSION_VARS[$name] = $var;
125 }
126 else {
127 $_SESSION["$name"] = $var;
128 }
129 session_register("$name");
130 }
131
132 function sqsession_unregister ($name) {
133
134 sqsession_is_active();
135
136 if ( !check_php_version(4,1) ) {
137 global $HTTP_SESSION_VARS;
138 unset($HTTP_SESSION_VARS[$name]);
139 }
140 else {
141 unset($_SESSION[$name]);
142 }
143 session_unregister("$name");
144 }
145
146 function sqsession_is_registered ($name) {
147 $test_name = &$name;
148 $result = false;
149 if ( !check_php_version(4,1) ) {
150 global $HTTP_SESSION_VARS;
151 if (isset($HTTP_SESSION_VARS[$test_name])) {
152 $result = true;
153 }
154 }
155 else {
156 if (isset($_SESSION[$test_name])) {
157 $result = true;
158 }
159 }
160 return $result;
161 }
162
163
164 define('SQ_INORDER',0);
165 define('SQ_GET',1);
166 define('SQ_POST',2);
167 define('SQ_SESSION',3);
168 define('SQ_COOKIE',4);
169 define('SQ_SERVER',5);
170
171 /**
172 * Search for the var $name in $_SESSION, $_POST, $_GET,
173 * $_COOKIE, or $_SERVER and set it in provided var.
174 *
175 * If $search is not provided, or == SQ_INORDER, it will search
176 * $_SESSION, then $_POST, then $_GET. Otherwise,
177 * use one of the defined constants to look for
178 * a var in one place specifically.
179 *
180 * Note: $search is an int value equal to one of the
181 * constants defined above.
182 *
183 * example:
184 * sqgetGlobalVar('username',$username,SQ_SESSION);
185 * -- no quotes around last param!
186 *
187 * Returns FALSE if variable is not found.
188 * Returns TRUE if it is.
189 */
190 function sqgetGlobalVar($name, &$value, $search = SQ_INORDER) {
191 if ( !check_php_version(4,1) ) {
192 global $_SESSION, $_GET, $_POST, $_COOKIE, $_SERVER;
193 }
194
195 /* NOTE: DO NOT enclose the constants in the switch
196 statement with quotes. They are constant values,
197 enclosing them in quotes will cause them to evaluate
198 as strings. */
199 switch ($search) {
200 /* we want the default case to be first here,
201 so that if a valid value isn't specified,
202 all three arrays will be searched. */
203 default:
204 case SQ_INORDER:
205 case SQ_SESSION:
206 if( isset($_SESSION[$name]) ) {
207 $value = $_SESSION[$name];
208 return TRUE;
209 } elseif ( $search == SQ_SESSION ) {
210 break;
211 }
212 case SQ_POST:
213 if( isset($_POST[$name]) ) {
214 $value = $_POST[$name];
215 return TRUE;
216 } elseif ( $search == SQ_POST ) {
217 break;
218 }
219 case SQ_GET:
220 if ( isset($_GET[$name]) ) {
221 $value = $_GET[$name];
222 return TRUE;
223 }
224 /* NO IF HERE. FOR SQ_INORDER CASE, EXIT after GET */
225 break;
226 case SQ_COOKIE:
227 if ( isset($_COOKIE[$name]) ) {
228 $value = $_COOKIE[$name];
229 return TRUE;
230 }
231 break;
232 case SQ_SERVER:
233 if ( isset($_SERVER[$name]) ) {
234 $value = $_SERVER[$name];
235 return TRUE;
236 }
237 break;
238 }
239 return FALSE;
240 }
241
242 function sqsession_destroy() {
243
244 /*
245 * php.net says we can kill the cookie by setting just the name:
246 * http://www.php.net/manual/en/function.setcookie.php
247 * maybe this will help fix the session merging again.
248 *
249 * Changed the theory on this to kill the cookies first starting
250 * a new session will provide a new session for all instances of
251 * the browser, we don't want that, as that is what is causing the
252 * merging of sessions.
253 */
254
255 global $base_uri;
256
257 if (isset($_COOKIE[session_name()])) setcookie(session_name(), '', time() - 5, $base_uri);
258 if (isset($_COOKIE['username'])) setcookie('username','',time() - 5,$base_uri);
259 if (isset($_COOKIE['key'])) setcookie('key','',time() - 5,$base_uri);
260
261 $sessid = session_id();
262 if (!empty( $sessid )) {
263 if ( !check_php_version(4,1) ) {
264 global $HTTP_SESSION_VARS;
265 $HTTP_SESSION_VARS = array();
266 } else {
267 $_SESSION = array();
268 }
269 @session_destroy();
270 }
271
272 }
273
274 /*
275 * Function to verify a session has been started. If it hasn't
276 * start a session up. php.net doesn't tell you that $_SESSION
277 * (even though autoglobal), is not created unless a session is
278 * started, unlike $_POST, $_GET and such
279 */
280
281 function sqsession_is_active() {
282
283 $sessid = session_id();
284 if ( empty( $sessid ) ) {
285 session_start();
286 }
287 }
288
289
290 ?>