We're living in 2004 now... perl is your friend for these kinds of things :)
[squirrelmail.git] / functions / global.php
1 <?php
2
3 /**
4 * global.php
5 *
6 * Copyright (c) 1999-2004 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 * @package squirrelmail
15 */
16
17 /** Bring in the config file. */
18 require_once(SM_PATH . 'config/config.php');
19
20 /** set the name of the session cookie */
21 if(isset($session_name) && $session_name) {
22 ini_set('session.name' , $session_name);
23 } else {
24 ini_set('session.name' , 'SQMSESSID');
25 }
26
27 /** If magic_quotes_runtime is on, SquirrelMail breaks in new and creative ways.
28 * Force magic_quotes_runtime off.
29 * tassium@squirrelmail.org - I put it here in the hopes that all SM code includes this.
30 * If there's a better place, please let me know.
31 */
32 ini_set('magic_quotes_runtime','0');
33
34 /* Since we decided all IMAP servers must implement the UID command as defined in
35 * the IMAP RFC, we force $uid_support to be on.
36 */
37
38 global $uid_support;
39 $uid_support = true;
40
41 sqsession_is_active();
42
43 /* convert old-style superglobals to current method
44 * this is executed if you are running PHP 4.0.x.
45 * it is run via a require_once directive in validate.php
46 * and redirect.php. Patch submitted by Ray Black.
47 */
48
49 if ( !check_php_version(4,1) ) {
50 global $_COOKIE, $_ENV, $_FILES, $_GET, $_POST, $_SERVER, $_SESSION;
51 global $HTTP_COOKIE_VARS, $HTTP_ENV_VARS, $HTTP_POST_FILES, $HTTP_GET_VARS,
52 $HTTP_POST_VARS, $HTTP_SERVER_VARS, $HTTP_SESSION_VARS, $PHP_SELF;
53 $_COOKIE =& $HTTP_COOKIE_VARS;
54 $_ENV =& $HTTP_ENV_VARS;
55 $_FILES =& $HTTP_POST_FILES;
56 $_GET =& $HTTP_GET_VARS;
57 $_POST =& $HTTP_POST_VARS;
58 $_SERVER =& $HTTP_SERVER_VARS;
59 $_SESSION =& $HTTP_SESSION_VARS;
60 if (!isset($PHP_SELF) || empty($PHP_SELF)) {
61 $PHP_SELF = $HTTP_SERVER_VARS['PHP_SELF'];
62 }
63 }
64
65 /* if running with magic_quotes_gpc then strip the slashes
66 from POST and GET global arrays */
67
68 if (get_magic_quotes_gpc()) {
69 sqstripslashes($_GET);
70 sqstripslashes($_POST);
71 }
72
73 /* strip any tags added to the url from PHP_SELF.
74 This fixes hand crafted url XXS expoits for any
75 page that uses PHP_SELF as the FORM action */
76
77 $_SERVER['PHP_SELF'] = strip_tags($_SERVER['PHP_SELF']);
78
79 /**
80 * returns true if current php version is at mimimum a.b.c
81 *
82 * Called: check_php_version(4,1)
83 * @param int a major version number
84 * @param int b minor version number
85 * @param int c release number
86 * @return bool
87 */
88 function check_php_version ($a = '0', $b = '0', $c = '0')
89 {
90 global $SQ_PHP_VERSION;
91
92 if(!isset($SQ_PHP_VERSION))
93 $SQ_PHP_VERSION = substr( str_pad( preg_replace('/\D/','', PHP_VERSION), 3, '0'), 0, 3);
94
95 return $SQ_PHP_VERSION >= ($a.$b.$c);
96 }
97
98 /**
99 * returns true if the current internal SM version is at minimum a.b.c
100 * These are plain integer comparisons, as our internal version is
101 * constructed by us, as an array of 3 ints.
102 *
103 * Called: check_sm_version(1,3,3)
104 * @param int a major version number
105 * @param int b minor version number
106 * @param int c release number
107 * @return bool
108 */
109 function check_sm_version($a = 0, $b = 0, $c = 0)
110 {
111 global $SQM_INTERNAL_VERSION;
112 if ( !isset($SQM_INTERNAL_VERSION) ||
113 $SQM_INTERNAL_VERSION[0] < $a ||
114 $SQM_INTERNAL_VERSION[1] < $b ||
115 ( $SQM_INTERNAL_VERSION[1] == $b &&
116 $SQM_INTERNAL_VERSION[2] < $c ) ) {
117 return FALSE;
118 }
119 return TRUE;
120 }
121
122
123 /**
124 * Recursively strip slashes from the values of an array.
125 * @param array array the array to strip, passed by reference
126 * @return void
127 */
128 function sqstripslashes(&$array) {
129 if(count($array) > 0) {
130 foreach ($array as $index=>$value) {
131 if (is_array($array[$index])) {
132 sqstripslashes($array[$index]);
133 }
134 else {
135 $array[$index] = stripslashes($value);
136 }
137 }
138 }
139 }
140
141 /**
142 * Add a variable to the session.
143 * @param mixed $var the variable to register
144 * @param string $name the name to refer to this variable
145 * @return void
146 */
147 function sqsession_register ($var, $name) {
148
149 sqsession_is_active();
150
151 if ( !check_php_version(4,1) ) {
152 global $HTTP_SESSION_VARS;
153 $HTTP_SESSION_VARS[$name] = $var;
154 }
155 else {
156 $_SESSION["$name"] = $var;
157 }
158 session_register("$name");
159 }
160
161 /**
162 * Delete a variable from the session.
163 * @param string $name the name of the var to delete
164 * @return void
165 */
166 function sqsession_unregister ($name) {
167
168 sqsession_is_active();
169
170 if ( !check_php_version(4,1) ) {
171 global $HTTP_SESSION_VARS;
172 unset($HTTP_SESSION_VARS[$name]);
173 }
174 else {
175 unset($_SESSION[$name]);
176 }
177 session_unregister("$name");
178 }
179
180 /**
181 * Checks to see if a variable has already been registered
182 * in the session.
183 * @param string $name the name of the var to check
184 * @return bool whether the var has been registered
185 */
186 function sqsession_is_registered ($name) {
187 $test_name = &$name;
188 $result = false;
189 if ( !check_php_version(4,1) ) {
190 global $HTTP_SESSION_VARS;
191 if (isset($HTTP_SESSION_VARS[$test_name])) {
192 $result = true;
193 }
194 }
195 else {
196 if (isset($_SESSION[$test_name])) {
197 $result = true;
198 }
199 }
200 return $result;
201 }
202
203
204 define('SQ_INORDER',0);
205 define('SQ_GET',1);
206 define('SQ_POST',2);
207 define('SQ_SESSION',3);
208 define('SQ_COOKIE',4);
209 define('SQ_SERVER',5);
210 define('SQ_FORM',6);
211
212 /**
213 * Search for the var $name in $_SESSION, $_POST, $_GET,
214 * $_COOKIE, or $_SERVER and set it in provided var.
215 *
216 * If $search is not provided, or == SQ_INORDER, it will search
217 * $_SESSION, then $_POST, then $_GET. Otherwise,
218 * use one of the defined constants to look for
219 * a var in one place specifically.
220 *
221 * Note: $search is an int value equal to one of the
222 * constants defined above.
223 *
224 * example:
225 * sqgetGlobalVar('username',$username,SQ_SESSION);
226 * -- no quotes around last param!
227 *
228 * @param string name the name of the var to search
229 * @param mixed value the variable to return
230 * @param int search constant defining where to look
231 * @return bool whether variable is found.
232 */
233 function sqgetGlobalVar($name, &$value, $search = SQ_INORDER) {
234
235 if ( !check_php_version(4,1) ) {
236 global $HTTP_COOKIE_VARS, $HTTP_GET_VARS, $HTTP_POST_VARS,
237 $HTTP_SERVER_VARS, $HTTP_SESSION_VARS;
238
239 $_COOKIE =& $HTTP_COOKIE_VARS;
240 $_GET =& $HTTP_GET_VARS;
241 $_POST =& $HTTP_POST_VARS;
242 $_SERVER =& $HTTP_SERVER_VARS;
243 $_SESSION =& $HTTP_SESSION_VARS;
244 }
245
246 /* NOTE: DO NOT enclose the constants in the switch
247 statement with quotes. They are constant values,
248 enclosing them in quotes will cause them to evaluate
249 as strings. */
250 switch ($search) {
251 /* we want the default case to be first here,
252 so that if a valid value isn't specified,
253 all three arrays will be searched. */
254 default:
255 case SQ_INORDER: // check session, post, get
256 case SQ_SESSION:
257 if( isset($_SESSION[$name]) ) {
258 $value = $_SESSION[$name];
259 return TRUE;
260 } elseif ( $search == SQ_SESSION ) {
261 break;
262 }
263 case SQ_FORM: // check post, get
264 case SQ_POST:
265 if( isset($_POST[$name]) ) {
266 $value = $_POST[$name];
267 return TRUE;
268 } elseif ( $search == SQ_POST ) {
269 break;
270 }
271 case SQ_GET:
272 if ( isset($_GET[$name]) ) {
273 $value = $_GET[$name];
274 return TRUE;
275 }
276 /* NO IF HERE. FOR SQ_INORDER CASE, EXIT after GET */
277 break;
278 case SQ_COOKIE:
279 if ( isset($_COOKIE[$name]) ) {
280 $value = $_COOKIE[$name];
281 return TRUE;
282 }
283 break;
284 case SQ_SERVER:
285 if ( isset($_SERVER[$name]) ) {
286 $value = $_SERVER[$name];
287 return TRUE;
288 }
289 break;
290 }
291 return FALSE;
292 }
293
294 /**
295 * Deletes an existing session, more advanced than the standard PHP
296 * session_destroy(), it explicitly deletes the cookies and global vars.
297 */
298 function sqsession_destroy() {
299
300 /*
301 * php.net says we can kill the cookie by setting just the name:
302 * http://www.php.net/manual/en/function.setcookie.php
303 * maybe this will help fix the session merging again.
304 *
305 * Changed the theory on this to kill the cookies first starting
306 * a new session will provide a new session for all instances of
307 * the browser, we don't want that, as that is what is causing the
308 * merging of sessions.
309 */
310
311 global $base_uri;
312
313 if (isset($_COOKIE[session_name()])) setcookie(session_name(), '', time() - 5, $base_uri);
314 if (isset($_COOKIE['username'])) setcookie('username','',time() - 5,$base_uri);
315 if (isset($_COOKIE['key'])) setcookie('key','',time() - 5,$base_uri);
316
317 $sessid = session_id();
318 if (!empty( $sessid )) {
319 if ( !check_php_version(4,1) ) {
320 global $HTTP_SESSION_VARS;
321 $HTTP_SESSION_VARS = array();
322 } else {
323 $_SESSION = array();
324 }
325 @session_destroy();
326 }
327
328 }
329
330 /**
331 * Function to verify a session has been started. If it hasn't
332 * start a session up. php.net doesn't tell you that $_SESSION
333 * (even though autoglobal), is not created unless a session is
334 * started, unlike $_POST, $_GET and such
335 */
336
337 function sqsession_is_active() {
338
339 $sessid = session_id();
340 if ( empty( $sessid ) ) {
341 session_start();
342 }
343 }
344
345
346 ?>