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
16 require_once(SM_PATH
. 'config/config.php');
18 /* set the name of the session cookie */
19 if(isset($session_name) && $session_name) {
20 ini_set('session.name' , $session_name);
22 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 * chilts@birdbrained.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 sqsession_is_active();
34 /* convert old-style superglobals to current method
35 * this is executed if you are running PHP 4.0.x.
36 * it is run via a require_once directive in validate.php
37 * and redirect.php. Patch submitted by Ray Black.
40 if ( !check_php_version(4,1) ) {
41 global $_COOKIE, $_ENV, $_FILES, $_GET, $_POST, $_SERVER, $_SESSION;
42 global $HTTP_COOKIE_VARS, $HTTP_ENV_VARS, $HTTP_POST_FILES, $HTTP_GET_VARS,
43 $HTTP_POST_VARS, $HTTP_SERVER_VARS, $HTTP_SESSION_VARS, $PHP_SELF;
44 $_COOKIE =& $HTTP_COOKIE_VARS;
45 $_ENV =& $HTTP_ENV_VARS;
46 $_FILES =& $HTTP_POST_FILES;
47 $_GET =& $HTTP_GET_VARS;
48 $_POST =& $HTTP_POST_VARS;
49 $_SERVER =& $HTTP_SERVER_VARS;
50 $_SESSION =& $HTTP_SESSION_VARS;
51 if (!isset($PHP_SELF) ||
empty($PHP_SELF)) {
52 $PHP_SELF = $HTTP_SERVER_VARS['PHP_SELF'];
56 /* if running with magic_quotes_gpc then strip the slashes
57 from POST and GET global arrays */
59 if (get_magic_quotes_gpc()) {
60 sqstripslashes($_GET);
61 sqstripslashes($_POST);
64 /* strip any tags added to the url from PHP_SELF.
65 This fixes hand crafted url XXS expoits for any
66 page that uses PHP_SELF as the FORM action */
68 $_SERVER['PHP_SELF'] = strip_tags($_SERVER['PHP_SELF']);
71 * returns true if current php version is at mimimum a.b.c
73 * Called: check_php_version(4,1)
75 function check_php_version ($a = '0', $b = '0', $c = '0')
77 global $SQ_PHP_VERSION;
79 if(!isset($SQ_PHP_VERSION))
80 $SQ_PHP_VERSION = substr( str_pad( preg_replace('/\D/','', PHP_VERSION
), 3, '0'), 0, 3);
82 return $SQ_PHP_VERSION >= ($a.$b.$c);
86 * returns true if the current internal SM version is at minimum a.b.c
87 * These are plain integer comparisons, as our internal version is
88 * constructed by us, as an array of 3 ints.
90 * Called: check_sm_version(1,3,3)
92 function check_sm_version($a = 0, $b = 0, $c = 0)
94 global $SQM_INTERNAL_VERSION;
95 if ( !isset($SQM_INTERNAL_VERSION) ||
96 $SQM_INTERNAL_VERSION[0] < $a ||
97 $SQM_INTERNAL_VERSION[1] < $b ||
98 ( $SQM_INTERNAL_VERSION[1] == $b &&
99 $SQM_INTERNAL_VERSION[2] < $c ) ) {
106 /* recursively strip slashes from the values of an array */
107 function sqstripslashes(&$array) {
108 if(count($array) > 0) {
109 foreach ($array as $index=>$value) {
110 if (is_array($array[$index])) {
111 sqstripslashes($array[$index]);
114 $array[$index] = stripslashes($value);
120 function sqsession_register ($var, $name) {
122 sqsession_is_active();
124 if ( !check_php_version(4,1) ) {
125 global $HTTP_SESSION_VARS;
126 $HTTP_SESSION_VARS[$name] = $var;
129 $_SESSION["$name"] = $var;
131 session_register("$name");
134 function sqsession_unregister ($name) {
136 sqsession_is_active();
138 if ( !check_php_version(4,1) ) {
139 global $HTTP_SESSION_VARS;
140 unset($HTTP_SESSION_VARS[$name]);
143 unset($_SESSION[$name]);
145 session_unregister("$name");
148 function sqsession_is_registered ($name) {
151 if ( !check_php_version(4,1) ) {
152 global $HTTP_SESSION_VARS;
153 if (isset($HTTP_SESSION_VARS[$test_name])) {
158 if (isset($_SESSION[$test_name])) {
166 define('SQ_INORDER',0);
169 define('SQ_SESSION',3);
170 define('SQ_COOKIE',4);
171 define('SQ_SERVER',5);
175 * Search for the var $name in $_SESSION, $_POST, $_GET,
176 * $_COOKIE, or $_SERVER and set it in provided var.
178 * If $search is not provided, or == SQ_INORDER, it will search
179 * $_SESSION, then $_POST, then $_GET. Otherwise,
180 * use one of the defined constants to look for
181 * a var in one place specifically.
183 * Note: $search is an int value equal to one of the
184 * constants defined above.
187 * sqgetGlobalVar('username',$username,SQ_SESSION);
188 * -- no quotes around last param!
190 * Returns FALSE if variable is not found.
191 * Returns TRUE if it is.
193 function sqgetGlobalVar($name, &$value, $search = SQ_INORDER
) {
195 if ( !check_php_version(4,1) ) {
196 global $HTTP_COOKIE_VARS, $HTTP_GET_VARS, $HTTP_POST_VARS,
197 $HTTP_SERVER_VARS, $HTTP_SESSION_VARS;
199 $_COOKIE =& $HTTP_COOKIE_VARS;
200 $_GET =& $HTTP_GET_VARS;
201 $_POST =& $HTTP_POST_VARS;
202 $_SERVER =& $HTTP_SERVER_VARS;
203 $_SESSION =& $HTTP_SESSION_VARS;
206 /* NOTE: DO NOT enclose the constants in the switch
207 statement with quotes. They are constant values,
208 enclosing them in quotes will cause them to evaluate
211 /* we want the default case to be first here,
212 so that if a valid value isn't specified,
213 all three arrays will be searched. */
215 case SQ_INORDER
: // check session, post, get
217 if( isset($_SESSION[$name]) ) {
218 $value = $_SESSION[$name];
220 } elseif ( $search == SQ_SESSION
) {
223 case SQ_FORM
: // check post, get
225 if( isset($_POST[$name]) ) {
226 $value = $_POST[$name];
228 } elseif ( $search == SQ_POST
) {
232 if ( isset($_GET[$name]) ) {
233 $value = $_GET[$name];
236 /* NO IF HERE. FOR SQ_INORDER CASE, EXIT after GET */
239 if ( isset($_COOKIE[$name]) ) {
240 $value = $_COOKIE[$name];
245 if ( isset($_SERVER[$name]) ) {
246 $value = $_SERVER[$name];
254 function sqsession_destroy() {
257 * php.net says we can kill the cookie by setting just the name:
258 * http://www.php.net/manual/en/function.setcookie.php
259 * maybe this will help fix the session merging again.
261 * Changed the theory on this to kill the cookies first starting
262 * a new session will provide a new session for all instances of
263 * the browser, we don't want that, as that is what is causing the
264 * merging of sessions.
269 if (isset($_COOKIE[session_name()])) setcookie(session_name(), '', time() - 5, $base_uri);
270 if (isset($_COOKIE['username'])) setcookie('username','',time() - 5,$base_uri);
271 if (isset($_COOKIE['key'])) setcookie('key','',time() - 5,$base_uri);
273 $sessid = session_id();
274 if (!empty( $sessid )) {
275 if ( !check_php_version(4,1) ) {
276 global $HTTP_SESSION_VARS;
277 $HTTP_SESSION_VARS = array();
287 * Function to verify a session has been started. If it hasn't
288 * start a session up. php.net doesn't tell you that $_SESSION
289 * (even though autoglobal), is not created unless a session is
290 * started, unlike $_POST, $_GET and such
293 function sqsession_is_active() {
295 $sessid = session_id();
296 if ( empty( $sessid ) ) {