Support for HttpOnly cookies.
[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 *
4b4abf93 10 * @copyright &copy; 1999-2005 The SquirrelMail Project Team
11 * @license http://opensource.org/licenses/gpl-license.php GNU Public License
31841a9e 12 * @version $Id$
d6c32258 13 * @package squirrelmail
61d9ec71 14 */
15
ebabf3f5 16
d6c32258 17/** set the name of the session cookie */
62f7daa5 18if(isset($session_name) && $session_name) {
19 ini_set('session.name' , $session_name);
20} else {
21 ini_set('session.name' , 'SQMSESSID');
ebabf3f5 22}
23
051f6245 24/**
25 * If magic_quotes_runtime is on, SquirrelMail breaks in new and creative ways.
26 * Force magic_quotes_runtime off.
27 * tassium@squirrelmail.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.
388c855c 29 */
5f660901 30ini_set('magic_quotes_runtime','0');
61d9ec71 31
2ca4c65a 32/* Since we decided all IMAP servers must implement the UID command as defined in
33 * the IMAP RFC, we force $uid_support to be on.
34 */
35
36global $uid_support;
37$uid_support = true;
38
d416901b 39sqsession_is_active();
40
180239ca 41/* if running with magic_quotes_gpc then strip the slashes
a32985a5 42 from POST and GET global arrays */
43
44if (get_magic_quotes_gpc()) {
180239ca 45 sqstripslashes($_GET);
46 sqstripslashes($_POST);
a32985a5 47}
48
49/* strip any tags added to the url from PHP_SELF.
50 This fixes hand crafted url XXS expoits for any
51 page that uses PHP_SELF as the FORM action */
52
388c855c 53$_SERVER['PHP_SELF'] = strip_tags($_SERVER['PHP_SELF']);
a32985a5 54
62f7daa5 55/**
56 * returns true if current php version is at mimimum a.b.c
57 *
97bdc607 58 * Called: check_php_version(4,1)
8b096f0a 59 * @param int a major version number
60 * @param int b minor version number
61 * @param int c release number
62 * @return bool
97bdc607 63 */
62f7daa5 64function check_php_version ($a = '0', $b = '0', $c = '0')
9697c5ab 65{
5673cabe 66 return version_compare ( PHP_VERSION, "$a.$b.$c", 'ge' );
9697c5ab 67}
68
97bdc607 69/**
62f7daa5 70 * returns true if the current internal SM version is at minimum a.b.c
71 * These are plain integer comparisons, as our internal version is
97bdc607 72 * constructed by us, as an array of 3 ints.
73 *
74 * Called: check_sm_version(1,3,3)
8b096f0a 75 * @param int a major version number
76 * @param int b minor version number
77 * @param int c release number
78 * @return bool
97bdc607 79 */
80function check_sm_version($a = 0, $b = 0, $c = 0)
81{
82 global $SQM_INTERNAL_VERSION;
83 if ( !isset($SQM_INTERNAL_VERSION) ||
84 $SQM_INTERNAL_VERSION[0] < $a ||
150c28d6 85 ( $SQM_INTERNAL_VERSION[0] == $a &&
86 $SQM_INTERNAL_VERSION[1] < $b) ||
87 ( $SQM_INTERNAL_VERSION[0] == $a &&
88 $SQM_INTERNAL_VERSION[1] == $b &&
97bdc607 89 $SQM_INTERNAL_VERSION[2] < $c ) ) {
90 return FALSE;
62f7daa5 91 }
92 return TRUE;
97bdc607 93}
94
95
8b096f0a 96/**
97 * Recursively strip slashes from the values of an array.
98 * @param array array the array to strip, passed by reference
99 * @return void
100 */
a32985a5 101function sqstripslashes(&$array) {
3aa17cf9 102 if(count($array) > 0) {
103 foreach ($array as $index=>$value) {
104 if (is_array($array[$index])) {
105 sqstripslashes($array[$index]);
106 }
107 else {
108 $array[$index] = stripslashes($value);
109 }
a32985a5 110 }
111 }
112}
113
8b096f0a 114/**
115 * Add a variable to the session.
116 * @param mixed $var the variable to register
117 * @param string $name the name to refer to this variable
118 * @return void
119 */
61d9ec71 120function sqsession_register ($var, $name) {
281c3d5b 121
122 sqsession_is_active();
123
62f7daa5 124 $_SESSION["$name"] = $var;
125
dcc1cc82 126 session_register("$name");
61d9ec71 127}
3aa17cf9 128
8b096f0a 129/**
130 * Delete a variable from the session.
131 * @param string $name the name of the var to delete
132 * @return void
133 */
61d9ec71 134function sqsession_unregister ($name) {
281c3d5b 135
136 sqsession_is_active();
137
abd74f7d 138 unset($_SESSION[$name]);
62f7daa5 139
dcc1cc82 140 session_unregister("$name");
61d9ec71 141}
3aa17cf9 142
8b096f0a 143/**
144 * Checks to see if a variable has already been registered
145 * in the session.
146 * @param string $name the name of the var to check
147 * @return bool whether the var has been registered
148 */
d7c82551 149function sqsession_is_registered ($name) {
150 $test_name = &$name;
151 $result = false;
62f7daa5 152
abd74f7d 153 if (isset($_SESSION[$test_name])) {
154 $result = true;
d7c82551 155 }
62f7daa5 156
d7c82551 157 return $result;
158}
159
61d9ec71 160
4cd8ae7d 161define('SQ_INORDER',0);
162define('SQ_GET',1);
163define('SQ_POST',2);
164define('SQ_SESSION',3);
27d0841c 165define('SQ_COOKIE',4);
166define('SQ_SERVER',5);
d9ad2525 167define('SQ_FORM',6);
4cd8ae7d 168
169/**
27d0841c 170 * Search for the var $name in $_SESSION, $_POST, $_GET,
62f7daa5 171 * $_COOKIE, or $_SERVER and set it in provided var.
d1975c5b 172 *
4cd8ae7d 173 * If $search is not provided, or == SQ_INORDER, it will search
174 * $_SESSION, then $_POST, then $_GET. Otherwise,
62f7daa5 175 * use one of the defined constants to look for
4cd8ae7d 176 * a var in one place specifically.
d1975c5b 177 *
62f7daa5 178 * Note: $search is an int value equal to one of the
d1975c5b 179 * constants defined above.
180 *
181 * example:
182 * sqgetGlobalVar('username',$username,SQ_SESSION);
183 * -- no quotes around last param!
184 *
8b096f0a 185 * @param string name the name of the var to search
186 * @param mixed value the variable to return
187 * @param int search constant defining where to look
188 * @return bool whether variable is found.
4cd8ae7d 189 */
190function sqgetGlobalVar($name, &$value, $search = SQ_INORDER) {
f79c19a4 191
d1975c5b 192 /* NOTE: DO NOT enclose the constants in the switch
193 statement with quotes. They are constant values,
194 enclosing them in quotes will cause them to evaluate
195 as strings. */
4cd8ae7d 196 switch ($search) {
62f7daa5 197 /* we want the default case to be first here,
051f6245 198 so that if a valid value isn't specified,
199 all three arrays will be searched. */
d1975c5b 200 default:
d9ad2525 201 case SQ_INORDER: // check session, post, get
d1975c5b 202 case SQ_SESSION:
203 if( isset($_SESSION[$name]) ) {
4cd8ae7d 204 $value = $_SESSION[$name];
d1975c5b 205 return TRUE;
206 } elseif ( $search == SQ_SESSION ) {
207 break;
208 }
d9ad2525 209 case SQ_FORM: // check post, get
d1975c5b 210 case SQ_POST:
211 if( isset($_POST[$name]) ) {
4cd8ae7d 212 $value = $_POST[$name];
d1975c5b 213 return TRUE;
214 } elseif ( $search == SQ_POST ) {
27d0841c 215 break;
d1975c5b 216 }
217 case SQ_GET:
218 if ( isset($_GET[$name]) ) {
219 $value = $_GET[$name];
220 return TRUE;
62f7daa5 221 }
d1975c5b 222 /* NO IF HERE. FOR SQ_INORDER CASE, EXIT after GET */
223 break;
224 case SQ_COOKIE:
225 if ( isset($_COOKIE[$name]) ) {
226 $value = $_COOKIE[$name];
62f7daa5 227 return TRUE;
d1975c5b 228 }
229 break;
230 case SQ_SERVER:
d1975c5b 231 if ( isset($_SERVER[$name]) ) {
232 $value = $_SERVER[$name];
233 return TRUE;
234 }
235 break;
4cd8ae7d 236 }
511f5c33 237 /* Nothing found, return FALSE */
4cd8ae7d 238 return FALSE;
239}
240
8b096f0a 241/**
242 * Deletes an existing session, more advanced than the standard PHP
243 * session_destroy(), it explicitly deletes the cookies and global vars.
244 */
513db22c 245function sqsession_destroy() {
242342d0 246
281c3d5b 247 /*
248 * php.net says we can kill the cookie by setting just the name:
249 * http://www.php.net/manual/en/function.setcookie.php
250 * maybe this will help fix the session merging again.
251 *
252 * Changed the theory on this to kill the cookies first starting
253 * a new session will provide a new session for all instances of
254 * the browser, we don't want that, as that is what is causing the
255 * merging of sessions.
256 */
242342d0 257
f9902ccb 258 global $base_uri;
f31687f6 259
3a1de9f1 260 if (isset($_COOKIE[session_name()])) sqsetcookie(session_name(), '', 0, $base_uri);
261 if (isset($_COOKIE['username'])) sqsetcookie('username','',0,$base_uri);
262 if (isset($_COOKIE['key'])) sqsetcookie('key','',0,$base_uri);
281c3d5b 263
264 $sessid = session_id();
265 if (!empty( $sessid )) {
abd74f7d 266 $_SESSION = array();
21e18f59 267 @session_destroy();
242342d0 268 }
269
281c3d5b 270}
242342d0 271
8b096f0a 272/**
281c3d5b 273 * Function to verify a session has been started. If it hasn't
274 * start a session up. php.net doesn't tell you that $_SESSION
275 * (even though autoglobal), is not created unless a session is
276 * started, unlike $_POST, $_GET and such
277 */
281c3d5b 278function sqsession_is_active() {
281c3d5b 279 $sessid = session_id();
280 if ( empty( $sessid ) ) {
3a1de9f1 281 sqsession_start();
281c3d5b 282 }
513db22c 283}
284
3a1de9f1 285/**
286 * Function to start the session and store the cookie with the session_id as
287 * HttpOnly cookie which means that the cookie isn't accessible by javascript
288 * (IE6 only)
289 */
290function sqsession_start() {
291 global $PHP_SELF;
292
293 $dirs = array('|src/.*|', '|plugins/.*|', '|functions/.*|');
294 $repl = array('', '', '');
295 $base_uri = preg_replace($dirs, $repl, $PHP_SELF);
296
297 session_start();
298 $sessid = session_id();
299 // session_starts sets the sessionid cookie buth without the httponly var
300 // setting the cookie again sets the httponly cookie attribute
301 sqsetcookie(session_name(),$sessid,false,$base_uri);
302}
303
304
305/**
306 * Set a cookie
307 * @param string $sName The name of the cookie.
308 * @param string $sValue The value of the cookie.
309 * @param int $iExpire The time the cookie expires. This is a Unix timestamp so is in number of seconds since the epoch.
310 * @param string $sPath The path on the server in which the cookie will be available on.
311 * @param string $sDomain The domain that the cookie is available.
312 * @param boolean $bSecure Indicates that the cookie should only be transmitted over a secure HTTPS connection.
313 * @param boolean $bHttpOnly Disallow JS to access the cookie (IE6 only)
314 * @return void
315 */
316function sqsetcookie($sName,$sValue,$iExpire=false,$sPath="",$sDomain="",$bSecure=false,$bHttpOnly=true) {
317 $sHeader = "Set-Cookie: $sName=$sValue";
318 if ($sPath) {
319 $sHeader .= "; Path=\"$sPath\"";
320 }
321 if ($iExpire !==false) {
322 $sHeader .= "; Max-Age=$iExpire";
323 }
324 if ($sPath) {
325 $sHeader .= "; Path=$sPath";
326 }
327 if ($sDomain) {
328 $sHeader .= "; Domain=$sDomain";
329 }
330 if ($bSecure) {
331 $sHeader .= "; Secure";
332 }
333 if ($bHttpOnly) {
334 $sHeader .= "; HttpOnly";
335 }
336 $sHeader .= "; Version=1";
337
338 header($sHeader);
339}
5673cabe 340// vim: et ts=4
f8a1ed5a 341?>