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-2005 The SquirrelMail Project Team
11 * @license http://opensource.org/licenses/gpl-license.php GNU Public License
13 * @package squirrelmail
17 /** set the name of the session cookie */
18 if(isset($session_name) && $session_name) {
19 ini_set('session.name' , $session_name);
21 ini_set('session.name' , 'SQMSESSID');
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.
30 ini_set('magic_quotes_runtime','0');
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.
39 sqsession_is_active();
41 /* if running with magic_quotes_gpc then strip the slashes
42 from POST and GET global arrays */
44 if (get_magic_quotes_gpc()) {
45 sqstripslashes($_GET);
46 sqstripslashes($_POST);
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 */
53 $_SERVER['PHP_SELF'] = strip_tags($_SERVER['PHP_SELF']);
56 * returns true if current php version is at mimimum a.b.c
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
64 function check_php_version ($a = '0', $b = '0', $c = '0')
66 return version_compare ( PHP_VERSION
, "$a.$b.$c", 'ge' );
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.
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
80 function check_sm_version($a = 0, $b = 0, $c = 0)
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 ) ) {
97 * Recursively strip slashes from the values of an array.
98 * @param array array the array to strip, passed by reference
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]);
108 $array[$index] = stripslashes($value);
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
120 function sqsession_register ($var, $name) {
122 sqsession_is_active();
124 $_SESSION["$name"] = $var;
126 session_register("$name");
130 * Delete a variable from the session.
131 * @param string $name the name of the var to delete
134 function sqsession_unregister ($name) {
136 sqsession_is_active();
138 unset($_SESSION[$name]);
140 session_unregister("$name");
144 * Checks to see if a variable has already been registered
146 * @param string $name the name of the var to check
147 * @return bool whether the var has been registered
149 function sqsession_is_registered ($name) {
153 if (isset($_SESSION[$test_name])) {
161 define('SQ_INORDER',0);
164 define('SQ_SESSION',3);
165 define('SQ_COOKIE',4);
166 define('SQ_SERVER',5);
170 * Search for the var $name in $_SESSION, $_POST, $_GET,
171 * $_COOKIE, or $_SERVER and set it in provided var.
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.
178 * Note: $search is an int value equal to one of the
179 * constants defined above.
182 * sqgetGlobalVar('username',$username,SQ_SESSION);
183 * -- no quotes around last param!
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.
190 function sqgetGlobalVar($name, &$value, $search = SQ_INORDER
) {
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
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. */
201 case SQ_INORDER
: // check session, post, get
203 if( isset($_SESSION[$name]) ) {
204 $value = $_SESSION[$name];
206 } elseif ( $search == SQ_SESSION
) {
209 case SQ_FORM
: // check post, get
211 if( isset($_POST[$name]) ) {
212 $value = $_POST[$name];
214 } elseif ( $search == SQ_POST
) {
218 if ( isset($_GET[$name]) ) {
219 $value = $_GET[$name];
222 /* NO IF HERE. FOR SQ_INORDER CASE, EXIT after GET */
225 if ( isset($_COOKIE[$name]) ) {
226 $value = $_COOKIE[$name];
231 if ( isset($_SERVER[$name]) ) {
232 $value = $_SERVER[$name];
237 /* Nothing found, return FALSE */
242 * Deletes an existing session, more advanced than the standard PHP
243 * session_destroy(), it explicitly deletes the cookies and global vars.
245 function sqsession_destroy() {
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.
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.
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);
264 $sessid = session_id();
265 if (!empty( $sessid )) {
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
278 function sqsession_is_active() {
279 $sessid = session_id();
280 if ( empty( $sessid ) ) {
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
290 function sqsession_start() {
293 $dirs = array('|src/.*|', '|plugins/.*|', '|functions/.*|');
294 $repl = array('', '', '');
295 $base_uri = preg_replace($dirs, $repl, $PHP_SELF);
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);
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)
316 function sqsetcookie($sName,$sValue,$iExpire=false,$sPath="",$sDomain="",$bSecure=false,$bHttpOnly=true) {
317 $sHeader = "Set-Cookie: $sName=$sValue";
319 $sHeader .= "; Path=\"$sPath\"";
321 if ($iExpire !==false) {
322 $sHeader .= "; Max-Age=$iExpire";
325 $sHeader .= "; Path=$sPath";
328 $sHeader .= "; Domain=$sDomain";
331 $sHeader .= "; Secure";
334 $sHeader .= "; HttpOnly";
336 $sHeader .= "; Version=1";