81ccfafe5330986ef333e46c1e0c34829aecc9a9
[squirrelmail.git] / functions / global.php
1 <?php
2
3 /**
4 * global.php
5 *
6 * This includes code to update < 4.1.0 globals to the newer format
7 * It also has some session register functions that work across various
8 * php versions.
9 *
10 * @copyright &copy; 1999-2006 The SquirrelMail Project Team
11 * @license http://opensource.org/licenses/gpl-license.php GNU Public License
12 * @version $Id$
13 * @package squirrelmail
14 */
15
16 /**
17 */
18 define('SQ_INORDER',0);
19 define('SQ_GET',1);
20 define('SQ_POST',2);
21 define('SQ_SESSION',3);
22 define('SQ_COOKIE',4);
23 define('SQ_SERVER',5);
24 define('SQ_FORM',6);
25
26
27 /**
28 * returns true if current php version is at mimimum a.b.c
29 *
30 * Called: check_php_version(4,1)
31 * @param int a major version number
32 * @param int b minor version number
33 * @param int c release number
34 * @return bool
35 */
36 function check_php_version ($a = '0', $b = '0', $c = '0')
37 {
38 return version_compare ( PHP_VERSION, "$a.$b.$c", 'ge' );
39 }
40
41 /**
42 * returns true if the current internal SM version is at minimum a.b.c
43 * These are plain integer comparisons, as our internal version is
44 * constructed by us, as an array of 3 ints.
45 *
46 * Called: check_sm_version(1,3,3)
47 * @param int a major version number
48 * @param int b minor version number
49 * @param int c release number
50 * @return bool
51 */
52 function check_sm_version($a = 0, $b = 0, $c = 0)
53 {
54 global $SQM_INTERNAL_VERSION;
55 if ( !isset($SQM_INTERNAL_VERSION) ||
56 $SQM_INTERNAL_VERSION[0] < $a ||
57 ( $SQM_INTERNAL_VERSION[0] == $a &&
58 $SQM_INTERNAL_VERSION[1] < $b) ||
59 ( $SQM_INTERNAL_VERSION[0] == $a &&
60 $SQM_INTERNAL_VERSION[1] == $b &&
61 $SQM_INTERNAL_VERSION[2] < $c ) ) {
62 return FALSE;
63 }
64 return TRUE;
65 }
66
67
68 /**
69 * Recursively strip slashes from the values of an array.
70 * @param array array the array to strip, passed by reference
71 * @return void
72 */
73 function sqstripslashes(&$array) {
74 if(count($array) > 0) {
75 foreach ($array as $index=>$value) {
76 if (is_array($array[$index])) {
77 sqstripslashes($array[$index]);
78 }
79 else {
80 $array[$index] = stripslashes($value);
81 }
82 }
83 }
84 }
85
86 /**
87 * Merges two variables into a single array
88 *
89 * Similar to PHP array_merge function, but provides same
90 * functionality as array_merge without losing array values
91 * with same key names. If the values under identical array
92 * keys are both strings and $concat_strings is TRUE, those
93 * values are concatenated together, otherwise they are placed
94 * in a sub-array and are merged (recursively) in the same manner.
95 *
96 * If either of the elements being merged is not an array,
97 * it will simply be added to the returned array.
98 *
99 * If both values are strings and $concat_strings is TRUE,
100 * a concatenated string is returned instead of an array.
101 *
102 * @param mixed $a First element to be merged
103 * @param mixed $b Second element to be merged
104 * @param boolean $concat_strings Whether or not string values
105 * should be concatenated instead
106 * of added to different array
107 * keys (default TRUE)
108 *
109 * @return array The merged $a and $b in one array
110 *
111 */
112 function sq_array_merge($a, $b, $concat_strings=true) {
113
114 $ret = array();
115
116 if (is_array($a)) {
117 $ret = $a;
118 } else {
119 if (is_string($a) && is_string($b) && $concat_strings) {
120 return $a . $b;
121 }
122 $ret[] = $a;
123 }
124
125
126 if (is_array($b)) {
127 foreach ($b as $key => $value) {
128 if (isset($ret[$key])) {
129 $ret[$key] = sq_array_merge($ret[$key], $value, $concat_strings);
130 } else {
131 $ret[$key] = $value;
132 }
133 }
134 } else {
135 $ret[] = $b;
136 }
137
138 return $ret;
139
140 }
141
142 /**
143 * Add a variable to the session.
144 * @param mixed $var the variable to register
145 * @param string $name the name to refer to this variable
146 * @return void
147 */
148 function sqsession_register ($var, $name) {
149
150 sqsession_is_active();
151
152 $_SESSION["$name"] = $var;
153
154 session_register("$name");
155 }
156
157 /**
158 * Delete a variable from the session.
159 * @param string $name the name of the var to delete
160 * @return void
161 */
162 function sqsession_unregister ($name) {
163
164 sqsession_is_active();
165
166 unset($_SESSION[$name]);
167
168 session_unregister("$name");
169 }
170
171 /**
172 * Checks to see if a variable has already been registered
173 * in the session.
174 * @param string $name the name of the var to check
175 * @return bool whether the var has been registered
176 */
177 function sqsession_is_registered ($name) {
178 $test_name = &$name;
179 $result = false;
180
181 if (isset($_SESSION[$test_name])) {
182 $result = true;
183 }
184
185 return $result;
186 }
187
188 /**
189 * Search for the var $name in $_SESSION, $_POST, $_GET, $_COOKIE, or $_SERVER
190 * and set it in provided var.
191 *
192 * If $search is not provided, or if it is SQ_INORDER, it will search $_SESSION,
193 * then $_POST, then $_GET. If $search is SQ_FORM it will search $_POST and
194 * $_GET. Otherwise, use one of the defined constants to look for a var in one
195 * place specifically.
196 *
197 * Note: $search is an int value equal to one of the constants defined above.
198 *
199 * Example:
200 * sqgetGlobalVar('username',$username,SQ_SESSION);
201 * // No quotes around last param, it's a constant - not a string!
202 *
203 * @param string name the name of the var to search
204 * @param mixed value the variable to return
205 * @param int search constant defining where to look
206 * @param int typecast force variable to be cast to given type (please
207 * use SQ_TYPE_XXX constants or set to FALSE (default)
208 * to leave variable type unmolested)
209 * @return bool whether variable is found.
210 */
211 function sqgetGlobalVar($name, &$value, $search = SQ_INORDER, $default = NULL, $typecast = false) {
212
213 $result = false;
214
215 switch ($search) {
216 /* we want the default case to be first here,
217 so that if a valid value isn't specified,
218 all three arrays will be searched. */
219 default:
220 case SQ_INORDER: // check session, post, get
221 case SQ_SESSION:
222 if( isset($_SESSION[$name]) ) {
223 $value = $_SESSION[$name];
224 $result = TRUE;
225 break;
226 } elseif ( $search == SQ_SESSION ) {
227 break;
228 }
229 case SQ_FORM: // check post, get
230 case SQ_POST:
231 if( isset($_POST[$name]) ) {
232 $value = $_POST[$name];
233 $result = TRUE;
234 break;
235 } elseif ( $search == SQ_POST ) {
236 break;
237 }
238 case SQ_GET:
239 if ( isset($_GET[$name]) ) {
240 $value = $_GET[$name];
241 $result = TRUE;
242 break;
243 }
244 /* NO IF HERE. FOR SQ_INORDER CASE, EXIT after GET */
245 break;
246 case SQ_COOKIE:
247 if ( isset($_COOKIE[$name]) ) {
248 $value = $_COOKIE[$name];
249 $result = TRUE;
250 break;
251 }
252 break;
253 case SQ_SERVER:
254 if ( isset($_SERVER[$name]) ) {
255 $value = $_SERVER[$name];
256 $result = TRUE;
257 break;
258 }
259 break;
260 }
261 if ($result && $typecast) {
262 switch ($typecast) {
263 case SQ_TYPE_INT: $value = (int) $value; break;
264 case SQ_TYPE_STRING: $value = (string) $value; break;
265 case SQ_TYPE_BOOL: $value = (bool) $value; break;
266 default: break;
267 }
268 } else if (!$result && !is_null($default)) {
269 $value = $default;
270 }
271 return $result;
272 }
273
274 /**
275 * Deletes an existing session, more advanced than the standard PHP
276 * session_destroy(), it explicitly deletes the cookies and global vars.
277 *
278 * WARNING: Older PHP versions have some issues with session management.
279 * See http://bugs.php.net/11643 (warning, spammed bug tracker) and
280 * http://bugs.php.net/13834. SID constant is not destroyed in PHP 4.1.2,
281 * 4.2.3 and maybe other versions. If you restart session after session
282 * is destroyed, affected PHP versions produce PHP notice. Bug should
283 * be fixed only in 4.3.0
284 */
285 function sqsession_destroy() {
286
287 /*
288 * php.net says we can kill the cookie by setting just the name:
289 * http://www.php.net/manual/en/function.setcookie.php
290 * maybe this will help fix the session merging again.
291 *
292 * Changed the theory on this to kill the cookies first starting
293 * a new session will provide a new session for all instances of
294 * the browser, we don't want that, as that is what is causing the
295 * merging of sessions.
296 */
297
298 global $base_uri;
299
300 if (isset($_COOKIE[session_name()])) sqsetcookie(session_name(), '', 0, $base_uri);
301 if (isset($_COOKIE['username'])) sqsetcookie('username','',0,$base_uri);
302 if (isset($_COOKIE['key'])) sqsetcookie('key','',0,$base_uri);
303
304 $sessid = session_id();
305 if (!empty( $sessid )) {
306 $_SESSION = array();
307 @session_destroy();
308 }
309 }
310
311 /**
312 * Function to verify a session has been started. If it hasn't
313 * start a session up. php.net doesn't tell you that $_SESSION
314 * (even though autoglobal), is not created unless a session is
315 * started, unlike $_POST, $_GET and such
316 */
317 function sqsession_is_active() {
318 $sessid = session_id();
319 if ( empty( $sessid ) ) {
320 sqsession_start();
321 }
322 }
323
324 /**
325 * Function to start the session and store the cookie with the session_id as
326 * HttpOnly cookie which means that the cookie isn't accessible by javascript
327 * (IE6 only)
328 */
329 function sqsession_start() {
330 global $base_uri;
331
332 session_start();
333 $session_id = session_id();
334
335 // session_starts sets the sessionid cookie buth without the httponly var
336 // setting the cookie again sets the httponly cookie attribute
337
338 // disable, @see sqsetcookie and php 5.1.2
339 // sqsetcookie(session_name(),session_id(),false,$base_uri);
340 }
341
342
343 /**
344 * Set a cookie
345 * @param string $sName The name of the cookie.
346 * @param string $sValue The value of the cookie.
347 * @param int $iExpire The time the cookie expires. This is a Unix timestamp so is in number of seconds since the epoch.
348 * @param string $sPath The path on the server in which the cookie will be available on.
349 * @param string $sDomain The domain that the cookie is available.
350 * @param boolean $bSecure Indicates that the cookie should only be transmitted over a secure HTTPS connection.
351 * @param boolean $bHttpOnly Disallow JS to access the cookie (IE6 only)
352 * @return void
353 */
354 function sqsetcookie($sName,$sValue,$iExpire=false,$sPath="",$sDomain="",$bSecure=false,$bHttpOnly=true,$bFlush=false) {
355 static $sCookieCache;
356 if (!isset($sCache)) {
357 $sCache = '';
358 }
359 /**
360 * We have to send all cookies with one header call otherwise we loose cookies.
361 * In order to achieve that the sqsetcookieflush function calls this function with $bFlush = true.
362 * If that happens we send the cookie header.
363 */
364 if ($bFlush) {
365 // header($sCookieCache);
366 return;
367 }
368 if (!$sName) return;
369
370 // php 5.1.2 and 4.4.2 do not allow to send multiple headers at once.
371 // Because that's the only way to get this thing working we fallback to
372 // setcookie until we solved this
373 if ($iExpire===false) $iExpire = 0;
374 setcookie($sName, $sValue, $iExpire, $sPath);
375 return;
376
377 $sHeader = "Set-Cookie: $sName=$sValue";
378 if ($sPath) {
379 $sHeader .= "; path=$sPath";
380 }
381 if ($iExpire !== false) {
382 $sHeader .= "; Max-Age=$iExpire";
383 // php uses Expire header, also add the expire header
384 $sHeader .= "; expires=". gmdate('D, d-M-Y H:i:s T',$iExpire);
385 }
386 if ($sDomain) {
387 $sHeader .= "; Domain=$sDomain";
388 }
389 // TODO: IE for Mac (5.2) thinks that semicolon is part of cookie domain
390 if ($bSecure) {
391 $sHeader .= "; Secure";
392 }
393 if ($bHttpOnly) {
394 $sHeader .= "; HttpOnly";
395 }
396 // $sHeader .= "; Version=1";
397 $sCookieCache .= $sHeader ."\r\n";
398 //header($sHeader."\r\n");
399 }
400
401 /**
402 * Send the cookie header
403 *
404 * Cookies set with sqsetcookie will bet set after a sqsetcookieflush call.
405 * @return void
406 */
407 function sqsetcookieflush() {
408 sqsetcookie('','','','','','','',true);
409 }
410
411 /**
412 * session_regenerate_id replacement for PHP < 4.3.2
413 *
414 * This code is borrowed from Gallery, session.php version 1.53.2.1
415 */
416 if (!function_exists('session_regenerate_id')) {
417 function make_seed() {
418 list($usec, $sec) = explode(' ', microtime());
419 return (float)$sec + ((float)$usec * 100000);
420 }
421
422 function php_combined_lcg() {
423 mt_srand(make_seed());
424 $tv = gettimeofday();
425 $lcg['s1'] = $tv['sec'] ^ (~$tv['usec']);
426 $lcg['s2'] = mt_rand();
427 $q = (int) ($lcg['s1'] / 53668);
428 $lcg['s1'] = (int) (40014 * ($lcg['s1'] - 53668 * $q) - 12211 * $q);
429 if ($lcg['s1'] < 0) {
430 $lcg['s1'] += 2147483563;
431 }
432 $q = (int) ($lcg['s2'] / 52774);
433 $lcg['s2'] = (int) (40692 * ($lcg['s2'] - 52774 * $q) - 3791 * $q);
434 if ($lcg['s2'] < 0) {
435 $lcg['s2'] += 2147483399;
436 }
437 $z = (int) ($lcg['s1'] - $lcg['s2']);
438 if ($z < 1) {
439 $z += 2147483562;
440 }
441 return $z * 4.656613e-10;
442 }
443
444 function session_regenerate_id() {
445 global $base_uri;
446 $tv = gettimeofday();
447 sqgetGlobalVar('REMOTE_ADDR',$remote_addr,SQ_SERVER);
448 $buf = sprintf("%.15s%ld%ld%0.8f", $remote_addr, $tv['sec'], $tv['usec'], php_combined_lcg() * 10);
449 session_id(md5($buf));
450 if (ini_get('session.use_cookies')) {
451 // at a later stage we use sqsetcookie. At this point just do
452 // what session_regenerate_id would do
453 setcookie(session_name(), session_id(), NULL, $base_uri);
454 }
455 return TRUE;
456 }
457 }
458
459
460 /**
461 * php_self
462 *
463 * Creates an URL for the page calling this function, using either the PHP global
464 * REQUEST_URI, or the PHP global PHP_SELF with QUERY_STRING added. Before 1.5.1
465 * function was stored in function/strings.php.
466 *
467 * @return string the complete url for this page
468 * @since 1.2.3
469 */
470 function php_self () {
471 if ( sqgetGlobalVar('REQUEST_URI', $req_uri, SQ_SERVER) && !empty($req_uri) ) {
472 return $req_uri;
473 }
474
475 if ( sqgetGlobalVar('PHP_SELF', $php_self, SQ_SERVER) && !empty($php_self) ) {
476
477 // need to add query string to end of PHP_SELF to match REQUEST_URI
478 //
479 if ( sqgetGlobalVar('QUERY_STRING', $query_string, SQ_SERVER) && !empty($query_string) ) {
480 $php_self .= '?' . $query_string;
481 }
482
483 return $php_self;
484 }
485
486 return '';
487 }