Remove almost all left overs of our non-uid-imap-server support.
[squirrelmail.git] / functions / global.php
1 <?php
2
3 /**
4 * global.php
5 *
6 * Copyright (c) 1999-2004 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 * @package squirrelmail
15 */
16
17 /** Bring in the config file. */
18 require_once(SM_PATH . 'config/config.php');
19
20 /** set the name of the session cookie */
21 if(isset($session_name) && $session_name) {
22 ini_set('session.name' , $session_name);
23 } else {
24 ini_set('session.name' , 'SQMSESSID');
25 }
26
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.
31 */
32 ini_set('magic_quotes_runtime','0');
33
34 sqsession_is_active();
35
36 /* if running with magic_quotes_gpc then strip the slashes
37 from POST and GET global arrays */
38
39 if (get_magic_quotes_gpc()) {
40 sqstripslashes($_GET);
41 sqstripslashes($_POST);
42 }
43
44 /* strip any tags added to the url from PHP_SELF.
45 This fixes hand crafted url XXS expoits for any
46 page that uses PHP_SELF as the FORM action */
47
48 $_SERVER['PHP_SELF'] = strip_tags($_SERVER['PHP_SELF']);
49
50 /**
51 * returns true if current php version is at mimimum a.b.c
52 *
53 * Called: check_php_version(4,1)
54 * @param int a major version number
55 * @param int b minor version number
56 * @param int c release number
57 * @return bool
58 */
59 function check_php_version ($a = '0', $b = '0', $c = '0')
60 {
61 global $SQ_PHP_VERSION;
62
63 if(!isset($SQ_PHP_VERSION))
64 $SQ_PHP_VERSION = substr( str_pad( preg_replace('/\D/','', PHP_VERSION), 3, '0'), 0, 3);
65
66 return $SQ_PHP_VERSION >= ($a.$b.$c);
67 }
68
69 /**
70 * returns true if the current internal SM version is at minimum a.b.c
71 * These are plain integer comparisons, as our internal version is
72 * constructed by us, as an array of 3 ints.
73 *
74 * Called: check_sm_version(1,3,3)
75 * @param int a major version number
76 * @param int b minor version number
77 * @param int c release number
78 * @return bool
79 */
80 function check_sm_version($a = 0, $b = 0, $c = 0)
81 {
82 global $SQM_INTERNAL_VERSION;
83 if ( !isset($SQM_INTERNAL_VERSION) ||
84 $SQM_INTERNAL_VERSION[0] < $a ||
85 $SQM_INTERNAL_VERSION[1] < $b ||
86 ( $SQM_INTERNAL_VERSION[1] == $b &&
87 $SQM_INTERNAL_VERSION[2] < $c ) ) {
88 return FALSE;
89 }
90 return TRUE;
91 }
92
93
94 /**
95 * Recursively strip slashes from the values of an array.
96 * @param array array the array to strip, passed by reference
97 * @return void
98 */
99 function sqstripslashes(&$array) {
100 if(count($array) > 0) {
101 foreach ($array as $index=>$value) {
102 if (is_array($array[$index])) {
103 sqstripslashes($array[$index]);
104 }
105 else {
106 $array[$index] = stripslashes($value);
107 }
108 }
109 }
110 }
111
112 /**
113 * Add a variable to the session.
114 * @param mixed $var the variable to register
115 * @param string $name the name to refer to this variable
116 * @return void
117 */
118 function sqsession_register ($var, $name) {
119
120 sqsession_is_active();
121
122 $_SESSION["$name"] = $var;
123
124 session_register("$name");
125 }
126
127 /**
128 * Delete a variable from the session.
129 * @param string $name the name of the var to delete
130 * @return void
131 */
132 function sqsession_unregister ($name) {
133
134 sqsession_is_active();
135
136 unset($_SESSION[$name]);
137
138 session_unregister("$name");
139 }
140
141 /**
142 * Checks to see if a variable has already been registered
143 * in the session.
144 * @param string $name the name of the var to check
145 * @return bool whether the var has been registered
146 */
147 function sqsession_is_registered ($name) {
148 $test_name = &$name;
149 $result = false;
150
151 if (isset($_SESSION[$test_name])) {
152 $result = true;
153 }
154
155 return $result;
156 }
157
158
159 define('SQ_INORDER',0);
160 define('SQ_GET',1);
161 define('SQ_POST',2);
162 define('SQ_SESSION',3);
163 define('SQ_COOKIE',4);
164 define('SQ_SERVER',5);
165 define('SQ_FORM',6);
166
167 /**
168 * Search for the var $name in $_SESSION, $_POST, $_GET,
169 * $_COOKIE, or $_SERVER and set it in provided var.
170 *
171 * If $search is not provided, or == SQ_INORDER, it will search
172 * $_SESSION, then $_POST, then $_GET. Otherwise,
173 * use one of the defined constants to look for
174 * a var in one place specifically.
175 *
176 * Note: $search is an int value equal to one of the
177 * constants defined above.
178 *
179 * example:
180 * sqgetGlobalVar('username',$username,SQ_SESSION);
181 * -- no quotes around last param!
182 *
183 * @param string name the name of the var to search
184 * @param mixed value the variable to return
185 * @param int search constant defining where to look
186 * @return bool whether variable is found.
187 */
188 function sqgetGlobalVar($name, &$value, $search = SQ_INORDER) {
189
190 /* NOTE: DO NOT enclose the constants in the switch
191 statement with quotes. They are constant values,
192 enclosing them in quotes will cause them to evaluate
193 as strings. */
194 switch ($search) {
195 /* we want the default case to be first here,
196 so that if a valid value isn't specified,
197 all three arrays will be searched. */
198 default:
199 case SQ_INORDER: // check session, post, get
200 case SQ_SESSION:
201 if( isset($_SESSION[$name]) ) {
202 $value = $_SESSION[$name];
203 return TRUE;
204 } elseif ( $search == SQ_SESSION ) {
205 break;
206 }
207 case SQ_FORM: // check post, get
208 case SQ_POST:
209 if( isset($_POST[$name]) ) {
210 $value = $_POST[$name];
211 return TRUE;
212 } elseif ( $search == SQ_POST ) {
213 break;
214 }
215 case SQ_GET:
216 if ( isset($_GET[$name]) ) {
217 $value = $_GET[$name];
218 return TRUE;
219 }
220 /* NO IF HERE. FOR SQ_INORDER CASE, EXIT after GET */
221 break;
222 case SQ_COOKIE:
223 if ( isset($_COOKIE[$name]) ) {
224 $value = $_COOKIE[$name];
225 return TRUE;
226 }
227 break;
228 case SQ_SERVER:
229 if ( isset($_SERVER[$name]) ) {
230 $value = $_SERVER[$name];
231 return TRUE;
232 }
233 break;
234 }
235 return FALSE;
236 }
237
238 /**
239 * Deletes an existing session, more advanced than the standard PHP
240 * session_destroy(), it explicitly deletes the cookies and global vars.
241 */
242 function sqsession_destroy() {
243
244 /*
245 * php.net says we can kill the cookie by setting just the name:
246 * http://www.php.net/manual/en/function.setcookie.php
247 * maybe this will help fix the session merging again.
248 *
249 * Changed the theory on this to kill the cookies first starting
250 * a new session will provide a new session for all instances of
251 * the browser, we don't want that, as that is what is causing the
252 * merging of sessions.
253 */
254
255 global $base_uri;
256
257 if (isset($_COOKIE[session_name()])) setcookie(session_name(), '', time() - 5, $base_uri);
258 if (isset($_COOKIE['username'])) setcookie('username','',time() - 5,$base_uri);
259 if (isset($_COOKIE['key'])) setcookie('key','',time() - 5,$base_uri);
260
261 $sessid = session_id();
262 if (!empty( $sessid )) {
263 $_SESSION = array();
264 @session_destroy();
265 }
266
267 }
268
269 /**
270 * Function to verify a session has been started. If it hasn't
271 * start a session up. php.net doesn't tell you that $_SESSION
272 * (even though autoglobal), is not created unless a session is
273 * started, unlike $_POST, $_GET and such
274 */
275
276 function sqsession_is_active() {
277
278 $sessid = session_id();
279 if ( empty( $sessid ) ) {
280 session_start();
281 }
282 }
283
284
285 ?>