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
10 * @copyright © 1999-2006 The SquirrelMail Project Team
11 * @license http://opensource.org/licenses/gpl-license.php GNU Public License
13 * @package squirrelmail
18 define('SQ_INORDER',0);
21 define('SQ_SESSION',3);
22 define('SQ_COOKIE',4);
23 define('SQ_SERVER',5);
28 * returns true if current php version is at mimimum a.b.c
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
36 function check_php_version ($a = '0', $b = '0', $c = '0')
38 return version_compare ( PHP_VERSION
, "$a.$b.$c", 'ge' );
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.
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
52 function check_sm_version($a = 0, $b = 0, $c = 0)
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 ) ) {
69 * Recursively strip slashes from the values of an array.
70 * @param array array the array to strip, passed by reference
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]);
80 $array[$index] = stripslashes($value);
87 * Merges two variables into a single array
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.
96 * If either of the elements being merged is not an array,
97 * it will simply be added to the returned array.
99 * If both values are strings and $concat_strings is TRUE,
100 * a concatenated string is returned instead of an array.
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)
109 * @return array The merged $a and $b in one array
114 function sq_array_merge($a, $b, $concat_strings=true) {
121 if (is_string($a) && is_string($b) && $concat_strings) {
129 foreach ($b as $key => $value) {
130 if (isset($ret[$key])) {
131 $ret[$key] = sq_array_merge($ret[$key], $value, $concat_strings);
145 * Add a variable to the session.
146 * @param mixed $var the variable to register
147 * @param string $name the name to refer to this variable
150 function sqsession_register ($var, $name) {
152 sqsession_is_active();
154 $_SESSION["$name"] = $var;
156 session_register("$name");
160 * Delete a variable from the session.
161 * @param string $name the name of the var to delete
164 function sqsession_unregister ($name) {
166 sqsession_is_active();
168 unset($_SESSION[$name]);
170 session_unregister("$name");
174 * Checks to see if a variable has already been registered
176 * @param string $name the name of the var to check
177 * @return bool whether the var has been registered
179 function sqsession_is_registered ($name) {
183 if (isset($_SESSION[$test_name])) {
191 * Search for the var $name in $_SESSION, $_POST, $_GET, $_COOKIE, or $_SERVER
192 * and set it in provided var.
194 * If $search is not provided, or if it is SQ_INORDER, it will search $_SESSION,
195 * then $_POST, then $_GET. If $search is SQ_FORM it will search $_POST and
196 * $_GET. Otherwise, use one of the defined constants to look for a var in one
197 * place specifically.
199 * Note: $search is an int value equal to one of the constants defined above.
202 * sqgetGlobalVar('username',$username,SQ_SESSION);
203 * // No quotes around last param, it's a constant - not a string!
205 * @param string name the name of the var to search
206 * @param mixed value the variable to return
207 * @param int search constant defining where to look
208 * @param int typecast force variable to be cast to given type (please
209 * use SQ_TYPE_XXX constants or set to FALSE (default)
210 * to leave variable type unmolested)
211 * @return bool whether variable is found.
213 function sqgetGlobalVar($name, &$value, $search = SQ_INORDER
, $default = NULL, $typecast = false) {
218 /* we want the default case to be first here,
219 so that if a valid value isn't specified,
220 all three arrays will be searched. */
222 case SQ_INORDER
: // check session, post, get
224 if( isset($_SESSION[$name]) ) {
225 $value = $_SESSION[$name];
228 } elseif ( $search == SQ_SESSION
) {
231 case SQ_FORM
: // check post, get
233 if( isset($_POST[$name]) ) {
234 $value = $_POST[$name];
237 } elseif ( $search == SQ_POST
) {
241 if ( isset($_GET[$name]) ) {
242 $value = $_GET[$name];
246 /* NO IF HERE. FOR SQ_INORDER CASE, EXIT after GET */
249 if ( isset($_COOKIE[$name]) ) {
250 $value = $_COOKIE[$name];
256 if ( isset($_SERVER[$name]) ) {
257 $value = $_SERVER[$name];
263 if ($result && $typecast) {
265 case SQ_TYPE_INT
: $value = (int) $value; break;
266 case SQ_TYPE_STRING
: $value = (string) $value; break;
267 case SQ_TYPE_BOOL
: $value = (bool) $value; break;
270 } else if (!$result && !is_null($default)) {
277 * Deletes an existing session, more advanced than the standard PHP
278 * session_destroy(), it explicitly deletes the cookies and global vars.
280 * WARNING: Older PHP versions have some issues with session management.
281 * See http://bugs.php.net/11643 (warning, spammed bug tracker) and
282 * http://bugs.php.net/13834. SID constant is not destroyed in PHP 4.1.2,
283 * 4.2.3 and maybe other versions. If you restart session after session
284 * is destroyed, affected PHP versions produce PHP notice. Bug should
285 * be fixed only in 4.3.0
287 function sqsession_destroy() {
290 * php.net says we can kill the cookie by setting just the name:
291 * http://www.php.net/manual/en/function.setcookie.php
292 * maybe this will help fix the session merging again.
294 * Changed the theory on this to kill the cookies first starting
295 * a new session will provide a new session for all instances of
296 * the browser, we don't want that, as that is what is causing the
297 * merging of sessions.
302 if (isset($_COOKIE[session_name()])) sqsetcookie(session_name(), '', 0, $base_uri);
303 if (isset($_COOKIE['username'])) sqsetcookie('username','',0,$base_uri);
304 if (isset($_COOKIE['key'])) sqsetcookie('key','',0,$base_uri);
306 $sessid = session_id();
307 if (!empty( $sessid )) {
314 * Function to verify a session has been started. If it hasn't
315 * start a session up. php.net doesn't tell you that $_SESSION
316 * (even though autoglobal), is not created unless a session is
317 * started, unlike $_POST, $_GET and such
319 function sqsession_is_active() {
320 $sessid = session_id();
321 if ( empty( $sessid ) ) {
327 * Function to start the session and store the cookie with the session_id as
328 * HttpOnly cookie which means that the cookie isn't accessible by javascript
331 function sqsession_start() {
335 $session_id = session_id();
337 // session_starts sets the sessionid cookie buth without the httponly var
338 // setting the cookie again sets the httponly cookie attribute
340 // disable, @see sqsetcookie and php 5.1.2
341 // sqsetcookie(session_name(),session_id(),false,$base_uri);
347 * @param string $sName The name of the cookie.
348 * @param string $sValue The value of the cookie.
349 * @param int $iExpire The time the cookie expires. This is a Unix timestamp so is in number of seconds since the epoch.
350 * @param string $sPath The path on the server in which the cookie will be available on.
351 * @param string $sDomain The domain that the cookie is available.
352 * @param boolean $bSecure Indicates that the cookie should only be transmitted over a secure HTTPS connection.
353 * @param boolean $bHttpOnly Disallow JS to access the cookie (IE6 only)
356 function sqsetcookie($sName,$sValue,$iExpire=false,$sPath="",$sDomain="",$bSecure=false,$bHttpOnly=true,$bFlush=false) {
357 static $sCookieCache;
358 if (!isset($sCache)) {
362 * We have to send all cookies with one header call otherwise we loose cookies.
363 * In order to achieve that the sqsetcookieflush function calls this function with $bFlush = true.
364 * If that happens we send the cookie header.
367 // header($sCookieCache);
372 // php 5.1.2 and 4.4.2 do not allow to send multiple headers at once.
373 // Because that's the only way to get this thing working we fallback to
374 // setcookie until we solved this
375 if ($iExpire===false) $iExpire = 0;
376 setcookie($sName, $sValue, $iExpire, $sPath);
379 $sHeader = "Set-Cookie: $sName=$sValue";
381 $sHeader .= "; path=$sPath";
383 if ($iExpire !== false) {
384 $sHeader .= "; Max-Age=$iExpire";
385 // php uses Expire header, also add the expire header
386 $sHeader .= "; expires=". gmdate('D, d-M-Y H:i:s T',$iExpire);
389 $sHeader .= "; Domain=$sDomain";
391 // TODO: IE for Mac (5.2) thinks that semicolon is part of cookie domain
393 $sHeader .= "; Secure";
396 $sHeader .= "; HttpOnly";
398 // $sHeader .= "; Version=1";
399 $sCookieCache .= $sHeader ."\r\n";
400 //header($sHeader."\r\n");
404 * Send the cookie header
406 * Cookies set with sqsetcookie will bet set after a sqsetcookieflush call.
409 function sqsetcookieflush() {
410 sqsetcookie('','','','','','','',true);
414 * session_regenerate_id replacement for PHP < 4.3.2
416 * This code is borrowed from Gallery, session.php version 1.53.2.1
418 if (!function_exists('session_regenerate_id')) {
419 function make_seed() {
420 list($usec, $sec) = explode(' ', microtime());
421 return (float)$sec +
((float)$usec * 100000);
424 function php_combined_lcg() {
425 mt_srand(make_seed());
426 $tv = gettimeofday();
427 $lcg['s1'] = $tv['sec'] ^
(~
$tv['usec']);
428 $lcg['s2'] = mt_rand();
429 $q = (int) ($lcg['s1'] / 53668);
430 $lcg['s1'] = (int) (40014 * ($lcg['s1'] - 53668 * $q) - 12211 * $q);
431 if ($lcg['s1'] < 0) {
432 $lcg['s1'] +
= 2147483563;
434 $q = (int) ($lcg['s2'] / 52774);
435 $lcg['s2'] = (int) (40692 * ($lcg['s2'] - 52774 * $q) - 3791 * $q);
436 if ($lcg['s2'] < 0) {
437 $lcg['s2'] +
= 2147483399;
439 $z = (int) ($lcg['s1'] - $lcg['s2']);
443 return $z * 4.656613e-10;
446 function session_regenerate_id() {
448 $tv = gettimeofday();
449 sqgetGlobalVar('REMOTE_ADDR',$remote_addr,SQ_SERVER
);
450 $buf = sprintf("%.15s%ld%ld%0.8f", $remote_addr, $tv['sec'], $tv['usec'], php_combined_lcg() * 10);
451 session_id(md5($buf));
452 if (ini_get('session.use_cookies')) {
453 // at a later stage we use sqsetcookie. At this point just do
454 // what session_regenerate_id would do
455 setcookie(session_name(), session_id(), NULL, $base_uri);
465 * Creates an URL for the page calling this function, using either the PHP global
466 * REQUEST_URI, or the PHP global PHP_SELF with QUERY_STRING added. Before 1.5.1
467 * function was stored in function/strings.php.
469 * @return string the complete url for this page
472 function php_self () {
473 if ( sqgetGlobalVar('REQUEST_URI', $req_uri, SQ_SERVER
) && !empty($req_uri) ) {
477 if ( sqgetGlobalVar('PHP_SELF', $php_self, SQ_SERVER
) && !empty($php_self) ) {
479 // need to add query string to end of PHP_SELF to match REQUEST_URI
481 if ( sqgetGlobalVar('QUERY_STRING', $query_string, SQ_SERVER
) && !empty($query_string) ) {
482 $php_self .= '?' . $query_string;