6344ddcb7282ef22f7d595fedb75ee368803d294
6 * Copyright (c) 1999-2003 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 /* convert old-style superglobals to current method
44 * this is executed if you are running PHP 4.0.x.
45 * it is run via a require_once directive in validate.php
46 * and redirect.php. Patch submitted by Ray Black.
49 if ( !check_php_version(4,1) ) {
50 global $_COOKIE, $_ENV, $_FILES, $_GET, $_POST, $_SERVER, $_SESSION;
51 global $HTTP_COOKIE_VARS, $HTTP_ENV_VARS, $HTTP_POST_FILES, $HTTP_GET_VARS,
52 $HTTP_POST_VARS, $HTTP_SERVER_VARS, $HTTP_SESSION_VARS, $PHP_SELF;
53 $_COOKIE =& $HTTP_COOKIE_VARS;
54 $_ENV =& $HTTP_ENV_VARS;
55 $_FILES =& $HTTP_POST_FILES;
56 $_GET =& $HTTP_GET_VARS;
57 $_POST =& $HTTP_POST_VARS;
58 $_SERVER =& $HTTP_SERVER_VARS;
59 $_SESSION =& $HTTP_SESSION_VARS;
60 if (!isset($PHP_SELF) ||
empty($PHP_SELF)) {
61 $PHP_SELF = $HTTP_SERVER_VARS['PHP_SELF'];
65 /* if running with magic_quotes_gpc then strip the slashes
66 from POST and GET global arrays */
68 if (get_magic_quotes_gpc()) {
69 sqstripslashes($_GET);
70 sqstripslashes($_POST);
73 /* strip any tags added to the url from PHP_SELF.
74 This fixes hand crafted url XXS expoits for any
75 page that uses PHP_SELF as the FORM action */
77 $_SERVER['PHP_SELF'] = strip_tags($_SERVER['PHP_SELF']);
80 * returns true if current php version is at mimimum a.b.c
82 * Called: check_php_version(4,1)
84 function check_php_version ($a = '0', $b = '0', $c = '0')
86 global $SQ_PHP_VERSION;
88 if(!isset($SQ_PHP_VERSION))
89 $SQ_PHP_VERSION = substr( str_pad( preg_replace('/\D/','', PHP_VERSION
), 3, '0'), 0, 3);
91 return $SQ_PHP_VERSION >= ($a.$b.$c);
95 * returns true if the current internal SM version is at minimum a.b.c
96 * These are plain integer comparisons, as our internal version is
97 * constructed by us, as an array of 3 ints.
99 * Called: check_sm_version(1,3,3)
101 function check_sm_version($a = 0, $b = 0, $c = 0)
103 global $SQM_INTERNAL_VERSION;
104 if ( !isset($SQM_INTERNAL_VERSION) ||
105 $SQM_INTERNAL_VERSION[0] < $a ||
106 $SQM_INTERNAL_VERSION[1] < $b ||
107 ( $SQM_INTERNAL_VERSION[1] == $b &&
108 $SQM_INTERNAL_VERSION[2] < $c ) ) {
115 /* recursively strip slashes from the values of an array */
116 function sqstripslashes(&$array) {
117 if(count($array) > 0) {
118 foreach ($array as $index=>$value) {
119 if (is_array($array[$index])) {
120 sqstripslashes($array[$index]);
123 $array[$index] = stripslashes($value);
129 function sqsession_register ($var, $name) {
131 sqsession_is_active();
133 if ( !check_php_version(4,1) ) {
134 global $HTTP_SESSION_VARS;
135 $HTTP_SESSION_VARS[$name] = $var;
138 $_SESSION["$name"] = $var;
140 session_register("$name");
143 function sqsession_unregister ($name) {
145 sqsession_is_active();
147 if ( !check_php_version(4,1) ) {
148 global $HTTP_SESSION_VARS;
149 unset($HTTP_SESSION_VARS[$name]);
152 unset($_SESSION[$name]);
154 session_unregister("$name");
157 function sqsession_is_registered ($name) {
160 if ( !check_php_version(4,1) ) {
161 global $HTTP_SESSION_VARS;
162 if (isset($HTTP_SESSION_VARS[$test_name])) {
167 if (isset($_SESSION[$test_name])) {
175 define('SQ_INORDER',0);
178 define('SQ_SESSION',3);
179 define('SQ_COOKIE',4);
180 define('SQ_SERVER',5);
184 * Search for the var $name in $_SESSION, $_POST, $_GET,
185 * $_COOKIE, or $_SERVER and set it in provided var.
187 * If $search is not provided, or == SQ_INORDER, it will search
188 * $_SESSION, then $_POST, then $_GET. Otherwise,
189 * use one of the defined constants to look for
190 * a var in one place specifically.
192 * Note: $search is an int value equal to one of the
193 * constants defined above.
196 * sqgetGlobalVar('username',$username,SQ_SESSION);
197 * -- no quotes around last param!
199 * Returns FALSE if variable is not found.
200 * Returns TRUE if it is.
202 function sqgetGlobalVar($name, &$value, $search = SQ_INORDER
) {
204 if ( !check_php_version(4,1) ) {
205 global $HTTP_COOKIE_VARS, $HTTP_GET_VARS, $HTTP_POST_VARS,
206 $HTTP_SERVER_VARS, $HTTP_SESSION_VARS;
208 $_COOKIE =& $HTTP_COOKIE_VARS;
209 $_GET =& $HTTP_GET_VARS;
210 $_POST =& $HTTP_POST_VARS;
211 $_SERVER =& $HTTP_SERVER_VARS;
212 $_SESSION =& $HTTP_SESSION_VARS;
215 /* NOTE: DO NOT enclose the constants in the switch
216 statement with quotes. They are constant values,
217 enclosing them in quotes will cause them to evaluate
220 /* we want the default case to be first here,
221 so that if a valid value isn't specified,
222 all three arrays will be searched. */
224 case SQ_INORDER
: // check session, post, get
226 if( isset($_SESSION[$name]) ) {
227 $value = $_SESSION[$name];
229 } elseif ( $search == SQ_SESSION
) {
232 case SQ_FORM
: // check post, get
234 if( isset($_POST[$name]) ) {
235 $value = $_POST[$name];
237 } elseif ( $search == SQ_POST
) {
241 if ( isset($_GET[$name]) ) {
242 $value = $_GET[$name];
245 /* NO IF HERE. FOR SQ_INORDER CASE, EXIT after GET */
248 if ( isset($_COOKIE[$name]) ) {
249 $value = $_COOKIE[$name];
254 if ( isset($_SERVER[$name]) ) {
255 $value = $_SERVER[$name];
263 function sqsession_destroy() {
266 * php.net says we can kill the cookie by setting just the name:
267 * http://www.php.net/manual/en/function.setcookie.php
268 * maybe this will help fix the session merging again.
270 * Changed the theory on this to kill the cookies first starting
271 * a new session will provide a new session for all instances of
272 * the browser, we don't want that, as that is what is causing the
273 * merging of sessions.
278 if (isset($_COOKIE[session_name()])) setcookie(session_name(), '', time() - 5, $base_uri);
279 if (isset($_COOKIE['username'])) setcookie('username','',time() - 5,$base_uri);
280 if (isset($_COOKIE['key'])) setcookie('key','',time() - 5,$base_uri);
282 $sessid = session_id();
283 if (!empty( $sessid )) {
284 if ( !check_php_version(4,1) ) {
285 global $HTTP_SESSION_VARS;
286 $HTTP_SESSION_VARS = array();
296 * Function to verify a session has been started. If it hasn't
297 * start a session up. php.net doesn't tell you that $_SESSION
298 * (even though autoglobal), is not created unless a session is
299 * started, unlike $_POST, $_GET and such
302 function sqsession_is_active() {
304 $sessid = session_id();
305 if ( empty( $sessid ) ) {