8ef497c83855c18f2f195e8d6cd206bf59fb389a
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)
83 * @param int a major version number
84 * @param int b minor version number
85 * @param int c release number
88 function check_php_version ($a = '0', $b = '0', $c = '0')
90 global $SQ_PHP_VERSION;
92 if(!isset($SQ_PHP_VERSION))
93 $SQ_PHP_VERSION = substr( str_pad( preg_replace('/\D/','', PHP_VERSION
), 3, '0'), 0, 3);
95 return $SQ_PHP_VERSION >= ($a.$b.$c);
99 * returns true if the current internal SM version is at minimum a.b.c
100 * These are plain integer comparisons, as our internal version is
101 * constructed by us, as an array of 3 ints.
103 * Called: check_sm_version(1,3,3)
104 * @param int a major version number
105 * @param int b minor version number
106 * @param int c release number
109 function check_sm_version($a = 0, $b = 0, $c = 0)
111 global $SQM_INTERNAL_VERSION;
112 if ( !isset($SQM_INTERNAL_VERSION) ||
113 $SQM_INTERNAL_VERSION[0] < $a ||
114 $SQM_INTERNAL_VERSION[1] < $b ||
115 ( $SQM_INTERNAL_VERSION[1] == $b &&
116 $SQM_INTERNAL_VERSION[2] < $c ) ) {
124 * Recursively strip slashes from the values of an array.
125 * @param array array the array to strip, passed by reference
128 function sqstripslashes(&$array) {
129 if(count($array) > 0) {
130 foreach ($array as $index=>$value) {
131 if (is_array($array[$index])) {
132 sqstripslashes($array[$index]);
135 $array[$index] = stripslashes($value);
142 * Add a variable to the session.
143 * @param mixed $var the variable to register
144 * @param string $name the name to refer to this variable
147 function sqsession_register ($var, $name) {
149 sqsession_is_active();
151 if ( !check_php_version(4,1) ) {
152 global $HTTP_SESSION_VARS;
153 $HTTP_SESSION_VARS[$name] = $var;
156 $_SESSION["$name"] = $var;
158 @session_register
("$name");
162 * Delete a variable from the session.
163 * @param string $name the name of the var to delete
166 function sqsession_unregister ($name) {
168 sqsession_is_active();
170 if ( !check_php_version(4,1) ) {
171 global $HTTP_SESSION_VARS;
172 unset($HTTP_SESSION_VARS[$name]);
175 unset($_SESSION[$name]);
177 @session_unregister
("$name");
181 * Checks to see if a variable has already been registered
183 * @param string $name the name of the var to check
184 * @return bool whether the var has been registered
186 function sqsession_is_registered ($name) {
189 if ( !check_php_version(4,1) ) {
190 global $HTTP_SESSION_VARS;
191 if (isset($HTTP_SESSION_VARS[$test_name])) {
196 if (isset($_SESSION[$test_name])) {
204 define('SQ_INORDER',0);
207 define('SQ_SESSION',3);
208 define('SQ_COOKIE',4);
209 define('SQ_SERVER',5);
213 * Search for the var $name in $_SESSION, $_POST, $_GET,
214 * $_COOKIE, or $_SERVER and set it in provided var.
216 * If $search is not provided, or == SQ_INORDER, it will search
217 * $_SESSION, then $_POST, then $_GET. Otherwise,
218 * use one of the defined constants to look for
219 * a var in one place specifically.
221 * Note: $search is an int value equal to one of the
222 * constants defined above.
225 * sqgetGlobalVar('username',$username,SQ_SESSION);
226 * -- no quotes around last param!
228 * @param string name the name of the var to search
229 * @param mixed value the variable to return
230 * @param int search constant defining where to look
231 * @return bool whether variable is found.
233 function sqgetGlobalVar($name, &$value, $search = SQ_INORDER
) {
235 if ( !check_php_version(4,1) ) {
236 global $HTTP_COOKIE_VARS, $HTTP_GET_VARS, $HTTP_POST_VARS,
237 $HTTP_SERVER_VARS, $HTTP_SESSION_VARS;
239 $_COOKIE =& $HTTP_COOKIE_VARS;
240 $_GET =& $HTTP_GET_VARS;
241 $_POST =& $HTTP_POST_VARS;
242 $_SERVER =& $HTTP_SERVER_VARS;
243 $_SESSION =& $HTTP_SESSION_VARS;
246 /* NOTE: DO NOT enclose the constants in the switch
247 statement with quotes. They are constant values,
248 enclosing them in quotes will cause them to evaluate
251 /* we want the default case to be first here,
252 so that if a valid value isn't specified,
253 all three arrays will be searched. */
255 case SQ_INORDER
: // check session, post, get
257 if( isset($_SESSION[$name]) ) {
258 $value = $_SESSION[$name];
260 } elseif ( $search == SQ_SESSION
) {
263 case SQ_FORM
: // check post, get
265 if( isset($_POST[$name]) ) {
266 $value = $_POST[$name];
268 } elseif ( $search == SQ_POST
) {
272 if ( isset($_GET[$name]) ) {
273 $value = $_GET[$name];
276 /* NO IF HERE. FOR SQ_INORDER CASE, EXIT after GET */
279 if ( isset($_COOKIE[$name]) ) {
280 $value = $_COOKIE[$name];
285 if ( isset($_SERVER[$name]) ) {
286 $value = $_SERVER[$name];
295 * Deletes an existing session, more advanced than the standard PHP
296 * session_destroy(), it explicitly deletes the cookies and global vars.
298 function sqsession_destroy() {
301 * php.net says we can kill the cookie by setting just the name:
302 * http://www.php.net/manual/en/function.setcookie.php
303 * maybe this will help fix the session merging again.
305 * Changed the theory on this to kill the cookies first starting
306 * a new session will provide a new session for all instances of
307 * the browser, we don't want that, as that is what is causing the
308 * merging of sessions.
313 if (isset($_COOKIE[session_name()])) setcookie(session_name(), '', time() - 5, $base_uri);
314 if (isset($_COOKIE['username'])) setcookie('username','',time() - 5,$base_uri);
315 if (isset($_COOKIE['key'])) setcookie('key','',time() - 5,$base_uri);
317 $sessid = session_id();
318 if (!empty( $sessid )) {
319 if ( !check_php_version(4,1) ) {
320 global $HTTP_SESSION_VARS;
321 $HTTP_SESSION_VARS = array();
331 * Function to verify a session has been started. If it hasn't
332 * start a session up. php.net doesn't tell you that $_SESSION
333 * (even though autoglobal), is not created unless a session is
334 * started, unlike $_POST, $_GET and such
337 function sqsession_is_active() {
339 $sessid = session_id();
340 if ( empty( $sessid ) ) {