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