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 sqsession_is_active();
36 /* if running with magic_quotes_gpc then strip the slashes
37 from POST and GET global arrays */
39 if (get_magic_quotes_gpc()) {
40 sqstripslashes($_GET);
41 sqstripslashes($_POST);
44 /* strip any tags added to the url from PHP_SELF.
45 This fixes hand crafted url XXS expoits for any
46 page that uses PHP_SELF as the FORM action */
48 $_SERVER['PHP_SELF'] = strip_tags($_SERVER['PHP_SELF']);
51 * returns true if current php version is at mimimum a.b.c
53 * Called: check_php_version(4,1)
54 * @param int a major version number
55 * @param int b minor version number
56 * @param int c release number
59 function check_php_version ($a = '0', $b = '0', $c = '0')
61 global $SQ_PHP_VERSION;
63 if(!isset($SQ_PHP_VERSION))
64 $SQ_PHP_VERSION = substr( str_pad( preg_replace('/\D/','', PHP_VERSION
), 3, '0'), 0, 3);
66 return $SQ_PHP_VERSION >= ($a.$b.$c);
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[1] < $b ||
86 ( $SQM_INTERNAL_VERSION[1] == $b &&
87 $SQM_INTERNAL_VERSION[2] < $c ) ) {
95 * Recursively strip slashes from the values of an array.
96 * @param array array the array to strip, passed by reference
99 function sqstripslashes(&$array) {
100 if(count($array) > 0) {
101 foreach ($array as $index=>$value) {
102 if (is_array($array[$index])) {
103 sqstripslashes($array[$index]);
106 $array[$index] = stripslashes($value);
113 * Add a variable to the session.
114 * @param mixed $var the variable to register
115 * @param string $name the name to refer to this variable
118 function sqsession_register ($var, $name) {
120 sqsession_is_active();
122 $_SESSION["$name"] = $var;
124 session_register("$name");
128 * Delete a variable from the session.
129 * @param string $name the name of the var to delete
132 function sqsession_unregister ($name) {
134 sqsession_is_active();
136 unset($_SESSION[$name]);
138 session_unregister("$name");
142 * Checks to see if a variable has already been registered
144 * @param string $name the name of the var to check
145 * @return bool whether the var has been registered
147 function sqsession_is_registered ($name) {
151 if (isset($_SESSION[$test_name])) {
159 define('SQ_INORDER',0);
162 define('SQ_SESSION',3);
163 define('SQ_COOKIE',4);
164 define('SQ_SERVER',5);
168 * Search for the var $name in $_SESSION, $_POST, $_GET,
169 * $_COOKIE, or $_SERVER and set it in provided var.
171 * If $search is not provided, or == SQ_INORDER, it will search
172 * $_SESSION, then $_POST, then $_GET. Otherwise,
173 * use one of the defined constants to look for
174 * a var in one place specifically.
176 * Note: $search is an int value equal to one of the
177 * constants defined above.
180 * sqgetGlobalVar('username',$username,SQ_SESSION);
181 * -- no quotes around last param!
183 * @param string name the name of the var to search
184 * @param mixed value the variable to return
185 * @param int search constant defining where to look
186 * @return bool whether variable is found.
188 function sqgetGlobalVar($name, &$value, $search = SQ_INORDER
) {
190 /* NOTE: DO NOT enclose the constants in the switch
191 statement with quotes. They are constant values,
192 enclosing them in quotes will cause them to evaluate
195 /* we want the default case to be first here,
196 so that if a valid value isn't specified,
197 all three arrays will be searched. */
199 case SQ_INORDER
: // check session, post, get
201 if( isset($_SESSION[$name]) ) {
202 $value = $_SESSION[$name];
204 } elseif ( $search == SQ_SESSION
) {
207 case SQ_FORM
: // check post, get
209 if( isset($_POST[$name]) ) {
210 $value = $_POST[$name];
212 } elseif ( $search == SQ_POST
) {
216 if ( isset($_GET[$name]) ) {
217 $value = $_GET[$name];
220 /* NO IF HERE. FOR SQ_INORDER CASE, EXIT after GET */
223 if ( isset($_COOKIE[$name]) ) {
224 $value = $_COOKIE[$name];
229 if ( isset($_SERVER[$name]) ) {
230 $value = $_SERVER[$name];
239 * Deletes an existing session, more advanced than the standard PHP
240 * session_destroy(), it explicitly deletes the cookies and global vars.
242 function sqsession_destroy() {
245 * php.net says we can kill the cookie by setting just the name:
246 * http://www.php.net/manual/en/function.setcookie.php
247 * maybe this will help fix the session merging again.
249 * Changed the theory on this to kill the cookies first starting
250 * a new session will provide a new session for all instances of
251 * the browser, we don't want that, as that is what is causing the
252 * merging of sessions.
257 if (isset($_COOKIE[session_name()])) setcookie(session_name(), '', time() - 5, $base_uri);
258 if (isset($_COOKIE['username'])) setcookie('username','',time() - 5,$base_uri);
259 if (isset($_COOKIE['key'])) setcookie('key','',time() - 5,$base_uri);
261 $sessid = session_id();
262 if (!empty( $sessid )) {
270 * Function to verify a session has been started. If it hasn't
271 * start a session up. php.net doesn't tell you that $_SESSION
272 * (even though autoglobal), is not created unless a session is
273 * started, unlike $_POST, $_GET and such
276 function sqsession_is_active() {
278 $sessid = session_id();
279 if ( empty( $sessid ) ) {