Session naming in devel.
[squirrelmail.git] / functions / global.php
1 <?php
2
3 /**
4 * globals.php
5 *
6 * Copyright (c) 1999-2002 The SquirrelMail Project Team
7 * Licensed under the GNU GPL. For full terms see the file COPYING.
8 *
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
11 * php versions.
12 *
13 * $Id$
14 */
15
16 require_once(SM_PATH . 'config/config.php');
17
18 /* set the name of the session cookie */
19 if(isset($session_name) && $session_name) {
20 ini_set('session.name' , $session_name);
21 } else {
22 ini_set('session.name' , 'SQMSESSID');
23 }
24
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.
29 */
30 ini_set('magic_quotes_runtime','0');
31
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.
36 */
37
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;
49 }
50
51 /* if running with magic_quotes_gpc then strip the slashes
52 from POST and GET global arrays */
53
54 if (get_magic_quotes_gpc()) {
55 sqstripslashes($_GET);
56 sqstripslashes($_POST);
57 }
58
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 */
62
63 $_SERVER['PHP_SELF'] = strip_tags($_SERVER['PHP_SELF']);
64
65 /**
66 * returns true if current php version is at mimimum a.b.c
67 *
68 * Called: check_php_version(4,1)
69 */
70 function check_php_version ($a = '0', $b = '0', $c = '0')
71 {
72 global $SQ_PHP_VERSION;
73
74 if(!isset($SQ_PHP_VERSION))
75 $SQ_PHP_VERSION = substr( str_pad( preg_replace('/\D/','', PHP_VERSION), 3, '0'), 0, 3);
76
77 return $SQ_PHP_VERSION >= ($a.$b.$c);
78 }
79
80 /**
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.
84 *
85 * Called: check_sm_version(1,3,3)
86 */
87 function check_sm_version($a = 0, $b = 0, $c = 0)
88 {
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 ) ) {
95 return FALSE;
96 }
97 return TRUE;
98 }
99
100
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]);
107 }
108 else {
109 $array[$index] = stripslashes($value);
110 }
111 }
112 }
113 }
114
115 function sqsession_register ($var, $name) {
116
117 sqsession_is_active();
118
119 if ( !check_php_version(4,1) ) {
120 global $HTTP_SESSION_VARS;
121 $HTTP_SESSION_VARS[$name] = $var;
122 }
123 else {
124 $_SESSION["$name"] = $var;
125 }
126 session_register("$name");
127 }
128
129 function sqsession_unregister ($name) {
130
131 sqsession_is_active();
132
133 if ( !check_php_version(4,1) ) {
134 global $HTTP_SESSION_VARS;
135 unset($HTTP_SESSION_VARS[$name]);
136 }
137 else {
138 unset($_SESSION[$name]);
139 }
140 session_unregister("$name");
141 }
142
143 function sqsession_is_registered ($name) {
144 $test_name = &$name;
145 $result = false;
146 if ( !check_php_version(4,1) ) {
147 global $HTTP_SESSION_VARS;
148 if (isset($HTTP_SESSION_VARS[$test_name])) {
149 $result = true;
150 }
151 }
152 else {
153 if (isset($_SESSION[$test_name])) {
154 $result = true;
155 }
156 }
157 return $result;
158 }
159
160
161 /**
162 * Search for the var $name in $_SESSION, $_POST, $_GET
163 * (in that order) and register it as a global var.
164 */
165 function sqextractGlobalVar ($name) {
166 if ( !check_php_version(4,1) ) {
167 global $_SESSION, $_GET, $_POST;
168 }
169 global $$name;
170 if( isset($_SESSION[$name]) ) {
171 $$name = $_SESSION[$name];
172 }
173 if( isset($_POST[$name]) ) {
174 $$name = $_POST[$name];
175 }
176 else if ( isset($_GET[$name]) ) {
177 $$name = $_GET[$name];
178 }
179 }
180
181 function sqsession_destroy() {
182
183 /*
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.
187 *
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.
192 */
193
194 global $base_uri;
195
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);
199
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();
205 } else {
206 $_SESSION = array();
207 }
208 @session_destroy;
209 }
210
211 }
212
213 /*
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
218 */
219
220 function sqsession_is_active() {
221
222 $sessid = session_id();
223 if ( empty( $sessid ) ) {
224 session_start();
225 }
226 }
227
228
229 ?>