6 * Copyright (c) 1999-2004 The SquirrelMail Project Team
7 * Licensed under the GNU GPL. For full terms see the file COPYING.
9 * This includes code to update < 4.1.0 globals to the newer format
10 * It also has some session register functions that work across various
14 * @package squirrelmail
17 /** Bring in the config file. */
18 require_once(SM_PATH
. 'config/config.php');
20 /** set the name of the session cookie */
21 if(isset($session_name) && $session_name) {
22 ini_set('session.name' , $session_name);
24 ini_set('session.name' , 'SQMSESSID');
27 /** If magic_quotes_runtime is on, SquirrelMail breaks in new and creative ways.
28 * Force magic_quotes_runtime off.
29 * tassium@squirrelmail.org - I put it here in the hopes that all SM code includes this.
30 * If there's a better place, please let me know.
32 ini_set('magic_quotes_runtime','0');
34 /* Since we decided all IMAP servers must implement the UID command as defined in
35 * the IMAP RFC, we force $uid_support to be on.
41 sqsession_is_active();
43 /* if running with magic_quotes_gpc then strip the slashes
44 from POST and GET global arrays */
46 if (get_magic_quotes_gpc()) {
47 sqstripslashes($_GET);
48 sqstripslashes($_POST);
51 /* strip any tags added to the url from PHP_SELF.
52 This fixes hand crafted url XXS expoits for any
53 page that uses PHP_SELF as the FORM action */
55 $_SERVER['PHP_SELF'] = strip_tags($_SERVER['PHP_SELF']);
58 * returns true if current php version is at mimimum a.b.c
60 * Called: check_php_version(4,1)
61 * @param int a major version number
62 * @param int b minor version number
63 * @param int c release number
66 function check_php_version ($a = '0', $b = '0', $c = '0')
68 return version_compare ( PHP_VERSION
, "$a.$b.$c", 'ge' );
72 * returns true if the current internal SM version is at minimum a.b.c
73 * These are plain integer comparisons, as our internal version is
74 * constructed by us, as an array of 3 ints.
76 * Called: check_sm_version(1,3,3)
77 * @param int a major version number
78 * @param int b minor version number
79 * @param int c release number
82 function check_sm_version($a = 0, $b = 0, $c = 0)
84 global $SQM_INTERNAL_VERSION;
85 if ( !isset($SQM_INTERNAL_VERSION) ||
86 $SQM_INTERNAL_VERSION[0] < $a ||
87 ( $SQM_INTERNAL_VERSION[0] == $a &&
88 $SQM_INTERNAL_VERSION[1] < $b) ||
89 ( $SQM_INTERNAL_VERSION[0] == $a &&
90 $SQM_INTERNAL_VERSION[1] == $b &&
91 $SQM_INTERNAL_VERSION[2] < $c ) ) {
99 * Recursively strip slashes from the values of an array.
100 * @param array array the array to strip, passed by reference
103 function sqstripslashes(&$array) {
104 if(count($array) > 0) {
105 foreach ($array as $index=>$value) {
106 if (is_array($array[$index])) {
107 sqstripslashes($array[$index]);
110 $array[$index] = stripslashes($value);
117 * Add a variable to the session.
118 * @param mixed $var the variable to register
119 * @param string $name the name to refer to this variable
122 function sqsession_register ($var, $name) {
124 sqsession_is_active();
126 $_SESSION["$name"] = $var;
128 session_register("$name");
132 * Delete a variable from the session.
133 * @param string $name the name of the var to delete
136 function sqsession_unregister ($name) {
138 sqsession_is_active();
140 unset($_SESSION[$name]);
142 session_unregister("$name");
146 * Checks to see if a variable has already been registered
148 * @param string $name the name of the var to check
149 * @return bool whether the var has been registered
151 function sqsession_is_registered ($name) {
155 if (isset($_SESSION[$test_name])) {
163 define('SQ_INORDER',0);
166 define('SQ_SESSION',3);
167 define('SQ_COOKIE',4);
168 define('SQ_SERVER',5);
172 * Search for the var $name in $_SESSION, $_POST, $_GET,
173 * $_COOKIE, or $_SERVER and set it in provided var.
175 * If $search is not provided, or == SQ_INORDER, it will search
176 * $_SESSION, then $_POST, then $_GET. Otherwise,
177 * use one of the defined constants to look for
178 * a var in one place specifically.
180 * Note: $search is an int value equal to one of the
181 * constants defined above.
184 * sqgetGlobalVar('username',$username,SQ_SESSION);
185 * -- no quotes around last param!
187 * @param string name the name of the var to search
188 * @param mixed value the variable to return
189 * @param int search constant defining where to look
190 * @return bool whether variable is found.
192 function sqgetGlobalVar($name, &$value, $search = SQ_INORDER
) {
194 /* NOTE: DO NOT enclose the constants in the switch
195 statement with quotes. They are constant values,
196 enclosing them in quotes will cause them to evaluate
199 /* we want the default case to be first here,
200 so that if a valid value isn't specified,
201 all three arrays will be searched. */
203 case SQ_INORDER
: // check session, post, get
205 if( isset($_SESSION[$name]) ) {
206 $value = $_SESSION[$name];
208 } elseif ( $search == SQ_SESSION
) {
211 case SQ_FORM
: // check post, get
213 if( isset($_POST[$name]) ) {
214 $value = $_POST[$name];
216 } elseif ( $search == SQ_POST
) {
220 if ( isset($_GET[$name]) ) {
221 $value = $_GET[$name];
224 /* NO IF HERE. FOR SQ_INORDER CASE, EXIT after GET */
227 if ( isset($_COOKIE[$name]) ) {
228 $value = $_COOKIE[$name];
233 if ( isset($_SERVER[$name]) ) {
234 $value = $_SERVER[$name];
243 * Deletes an existing session, more advanced than the standard PHP
244 * session_destroy(), it explicitly deletes the cookies and global vars.
246 function sqsession_destroy() {
249 * php.net says we can kill the cookie by setting just the name:
250 * http://www.php.net/manual/en/function.setcookie.php
251 * maybe this will help fix the session merging again.
253 * Changed the theory on this to kill the cookies first starting
254 * a new session will provide a new session for all instances of
255 * the browser, we don't want that, as that is what is causing the
256 * merging of sessions.
261 if (isset($_COOKIE[session_name()])) setcookie(session_name(), '', time() - 5, $base_uri);
262 if (isset($_COOKIE['username'])) setcookie('username','',time() - 5,$base_uri);
263 if (isset($_COOKIE['key'])) setcookie('key','',time() - 5,$base_uri);
265 $sessid = session_id();
266 if (!empty( $sessid )) {
274 * Function to verify a session has been started. If it hasn't
275 * start a session up. php.net doesn't tell you that $_SESSION
276 * (even though autoglobal), is not created unless a session is
277 * started, unlike $_POST, $_GET and such
280 function sqsession_is_active() {
282 $sessid = session_id();
283 if ( empty( $sessid ) ) {