6 * Copyright (c) 1999-2002 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 /* convert old-style superglobals to current method
33 * this is executed if you are running PHP 4.0.x.
34 * it is run via a require_once directive in validate.php
35 * and redirect.php. Patch submitted by Ray Black.
38 if ( !check_php_version(4,1) ) {
39 global $_COOKIE, $_ENV, $_FILES, $_GET, $_POST, $_SERVER, $_SESSION;
40 global $HTTP_COOKIE_VARS, $HTTP_ENV_VARS, $HTTP_POST_FILES, $HTTP_GET_VARS,
41 $HTTP_POST_VARS, $HTTP_SERVER_VARS, $HTTP_SESSION_VARS;
42 $_COOKIE =& $HTTP_COOKIE_VARS;
43 $_ENV =& $HTTP_ENV_VARS;
44 $_FILES =& $HTTP_POST_FILES;
45 $_GET =& $HTTP_GET_VARS;
46 $_POST =& $HTTP_POST_VARS;
47 $_SERVER =& $HTTP_SERVER_VARS;
48 $_SESSION =& $HTTP_SESSION_VARS;
51 /* if running with magic_quotes_gpc then strip the slashes
52 from POST and GET global arrays */
54 if (get_magic_quotes_gpc()) {
55 sqstripslashes($_GET);
56 sqstripslashes($_POST);
59 /* strip any tags added to the url from PHP_SELF.
60 This fixes hand crafted url XXS expoits for any
61 page that uses PHP_SELF as the FORM action */
63 $_SERVER['PHP_SELF'] = strip_tags($_SERVER['PHP_SELF']);
66 * returns true if current php version is at mimimum a.b.c
68 * Called: check_php_version(4,1)
70 function check_php_version ($a = '0', $b = '0', $c = '0')
72 global $SQ_PHP_VERSION;
74 if(!isset($SQ_PHP_VERSION))
75 $SQ_PHP_VERSION = substr( str_pad( preg_replace('/\D/','', PHP_VERSION
), 3, '0'), 0, 3);
77 return $SQ_PHP_VERSION >= ($a.$b.$c);
81 * returns true if the current internal SM version is at minimum a.b.c
82 * These are plain integer comparisons, as our internal version is
83 * constructed by us, as an array of 3 ints.
85 * Called: check_sm_version(1,3,3)
87 function check_sm_version($a = 0, $b = 0, $c = 0)
89 global $SQM_INTERNAL_VERSION;
90 if ( !isset($SQM_INTERNAL_VERSION) ||
91 $SQM_INTERNAL_VERSION[0] < $a ||
92 $SQM_INTERNAL_VERSION[1] < $b ||
93 ( $SQM_INTERNAL_VERSION[1] == $b &&
94 $SQM_INTERNAL_VERSION[2] < $c ) ) {
101 /* recursively strip slashes from the values of an array */
102 function sqstripslashes(&$array) {
103 if(count($array) > 0) {
104 foreach ($array as $index=>$value) {
105 if (is_array($array[$index])) {
106 sqstripslashes($array[$index]);
109 $array[$index] = stripslashes($value);
115 function sqsession_register ($var, $name) {
117 sqsession_is_active();
119 if ( !check_php_version(4,1) ) {
120 global $HTTP_SESSION_VARS;
121 $HTTP_SESSION_VARS[$name] = $var;
124 $_SESSION["$name"] = $var;
126 session_register("$name");
129 function sqsession_unregister ($name) {
131 sqsession_is_active();
133 if ( !check_php_version(4,1) ) {
134 global $HTTP_SESSION_VARS;
135 unset($HTTP_SESSION_VARS[$name]);
138 unset($_SESSION[$name]);
140 session_unregister("$name");
143 function sqsession_is_registered ($name) {
146 if ( !check_php_version(4,1) ) {
147 global $HTTP_SESSION_VARS;
148 if (isset($HTTP_SESSION_VARS[$test_name])) {
153 if (isset($_SESSION[$test_name])) {
162 * Search for the var $name in $_SESSION, $_POST, $_GET
163 * (in that order) and register it as a global var.
165 function sqextractGlobalVar ($name) {
166 if ( !check_php_version(4,1) ) {
167 global $_SESSION, $_GET, $_POST;
170 if( isset($_SESSION[$name]) ) {
171 $
$name = $_SESSION[$name];
173 if( isset($_POST[$name]) ) {
174 $
$name = $_POST[$name];
176 else if ( isset($_GET[$name]) ) {
177 $
$name = $_GET[$name];
181 function sqsession_destroy() {
184 * php.net says we can kill the cookie by setting just the name:
185 * http://www.php.net/manual/en/function.setcookie.php
186 * maybe this will help fix the session merging again.
188 * Changed the theory on this to kill the cookies first starting
189 * a new session will provide a new session for all instances of
190 * the browser, we don't want that, as that is what is causing the
191 * merging of sessions.
196 if (isset($_COOKIE[session_name()])) setcookie(session_name(), '', time() - 5, $base_uri);
197 if (isset($_COOKIE['username'])) setcookie('username','',time() - 5,$base_uri);
198 if (isset($_COOKIE['key'])) setcookie('key','',time() - 5,$base_uri);
200 $sessid = session_id();
201 if (!empty( $sessid )) {
202 if ( !check_php_version(4,1) ) {
203 global $HTTP_SESSION_VARS;
204 $HTTP_SESSION_VARS = array();
214 * Function to verify a session has been started. If it hasn't
215 * start a session up. php.net doesn't tell you that $_SESSION
216 * (even though autoglobal), is not created unless a session is
217 * started, unlike $_POST, $_GET and such
220 function sqsession_is_active() {
222 $sessid = session_id();
223 if ( empty( $sessid ) ) {