Support for HttpOnly cookies.
[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-2005 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 /** set the name of the session cookie */
18 if(isset($session_name) && $session_name) {
19 ini_set('session.name' , $session_name);
20 } else {
21 ini_set('session.name' , 'SQMSESSID');
22 }
23
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.
29 */
30 ini_set('magic_quotes_runtime','0');
31
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
36 global $uid_support;
37 $uid_support = true;
38
39 sqsession_is_active();
40
41 /* if running with magic_quotes_gpc then strip the slashes
42 from POST and GET global arrays */
43
44 if (get_magic_quotes_gpc()) {
45 sqstripslashes($_GET);
46 sqstripslashes($_POST);
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
53 $_SERVER['PHP_SELF'] = strip_tags($_SERVER['PHP_SELF']);
54
55 /**
56 * returns true if current php version is at mimimum a.b.c
57 *
58 * Called: check_php_version(4,1)
59 * @param int a major version number
60 * @param int b minor version number
61 * @param int c release number
62 * @return bool
63 */
64 function check_php_version ($a = '0', $b = '0', $c = '0')
65 {
66 return version_compare ( PHP_VERSION, "$a.$b.$c", 'ge' );
67 }
68
69 /**
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
72 * constructed by us, as an array of 3 ints.
73 *
74 * Called: check_sm_version(1,3,3)
75 * @param int a major version number
76 * @param int b minor version number
77 * @param int c release number
78 * @return bool
79 */
80 function 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 ||
85 ( $SQM_INTERNAL_VERSION[0] == $a &&
86 $SQM_INTERNAL_VERSION[1] < $b) ||
87 ( $SQM_INTERNAL_VERSION[0] == $a &&
88 $SQM_INTERNAL_VERSION[1] == $b &&
89 $SQM_INTERNAL_VERSION[2] < $c ) ) {
90 return FALSE;
91 }
92 return TRUE;
93 }
94
95
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 */
101 function sqstripslashes(&$array) {
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 }
110 }
111 }
112 }
113
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 */
120 function sqsession_register ($var, $name) {
121
122 sqsession_is_active();
123
124 $_SESSION["$name"] = $var;
125
126 session_register("$name");
127 }
128
129 /**
130 * Delete a variable from the session.
131 * @param string $name the name of the var to delete
132 * @return void
133 */
134 function sqsession_unregister ($name) {
135
136 sqsession_is_active();
137
138 unset($_SESSION[$name]);
139
140 session_unregister("$name");
141 }
142
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 */
149 function sqsession_is_registered ($name) {
150 $test_name = &$name;
151 $result = false;
152
153 if (isset($_SESSION[$test_name])) {
154 $result = true;
155 }
156
157 return $result;
158 }
159
160
161 define('SQ_INORDER',0);
162 define('SQ_GET',1);
163 define('SQ_POST',2);
164 define('SQ_SESSION',3);
165 define('SQ_COOKIE',4);
166 define('SQ_SERVER',5);
167 define('SQ_FORM',6);
168
169 /**
170 * Search for the var $name in $_SESSION, $_POST, $_GET,
171 * $_COOKIE, or $_SERVER and set it in provided var.
172 *
173 * If $search is not provided, or == SQ_INORDER, it will search
174 * $_SESSION, then $_POST, then $_GET. Otherwise,
175 * use one of the defined constants to look for
176 * a var in one place specifically.
177 *
178 * Note: $search is an int value equal to one of the
179 * constants defined above.
180 *
181 * example:
182 * sqgetGlobalVar('username',$username,SQ_SESSION);
183 * -- no quotes around last param!
184 *
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.
189 */
190 function sqgetGlobalVar($name, &$value, $search = SQ_INORDER) {
191
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. */
196 switch ($search) {
197 /* we want the default case to be first here,
198 so that if a valid value isn't specified,
199 all three arrays will be searched. */
200 default:
201 case SQ_INORDER: // check session, post, get
202 case SQ_SESSION:
203 if( isset($_SESSION[$name]) ) {
204 $value = $_SESSION[$name];
205 return TRUE;
206 } elseif ( $search == SQ_SESSION ) {
207 break;
208 }
209 case SQ_FORM: // check post, get
210 case SQ_POST:
211 if( isset($_POST[$name]) ) {
212 $value = $_POST[$name];
213 return TRUE;
214 } elseif ( $search == SQ_POST ) {
215 break;
216 }
217 case SQ_GET:
218 if ( isset($_GET[$name]) ) {
219 $value = $_GET[$name];
220 return TRUE;
221 }
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];
227 return TRUE;
228 }
229 break;
230 case SQ_SERVER:
231 if ( isset($_SERVER[$name]) ) {
232 $value = $_SERVER[$name];
233 return TRUE;
234 }
235 break;
236 }
237 /* Nothing found, return FALSE */
238 return FALSE;
239 }
240
241 /**
242 * Deletes an existing session, more advanced than the standard PHP
243 * session_destroy(), it explicitly deletes the cookies and global vars.
244 */
245 function sqsession_destroy() {
246
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 */
257
258 global $base_uri;
259
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);
263
264 $sessid = session_id();
265 if (!empty( $sessid )) {
266 $_SESSION = array();
267 @session_destroy();
268 }
269
270 }
271
272 /**
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 */
278 function sqsession_is_active() {
279 $sessid = session_id();
280 if ( empty( $sessid ) ) {
281 sqsession_start();
282 }
283 }
284
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 */
290 function 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 */
316 function 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 }
340 // vim: et ts=4
341 ?>