61d9ec71 |
1 | <?php |
2 | |
3 | /** |
0c701a88 |
4 | * global.php |
61d9ec71 |
5 | * |
62f7daa5 |
6 | * This includes code to update < 4.1.0 globals to the newer format |
242342d0 |
7 | * It also has some session register functions that work across various |
62f7daa5 |
8 | * php versions. |
61d9ec71 |
9 | * |
4b4abf93 |
10 | * @copyright © 1999-2005 The SquirrelMail Project Team |
11 | * @license http://opensource.org/licenses/gpl-license.php GNU Public License |
31841a9e |
12 | * @version $Id$ |
d6c32258 |
13 | * @package squirrelmail |
61d9ec71 |
14 | */ |
15 | |
ebabf3f5 |
16 | |
d6c32258 |
17 | /** set the name of the session cookie */ |
62f7daa5 |
18 | if(isset($session_name) && $session_name) { |
19 | ini_set('session.name' , $session_name); |
20 | } else { |
21 | ini_set('session.name' , 'SQMSESSID'); |
ebabf3f5 |
22 | } |
23 | |
051f6245 |
24 | /** |
25 | * If magic_quotes_runtime is on, SquirrelMail breaks in new and creative ways. |
26 | * Force magic_quotes_runtime off. |
27 | * tassium@squirrelmail.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. |
388c855c |
29 | */ |
5f660901 |
30 | ini_set('magic_quotes_runtime','0'); |
61d9ec71 |
31 | |
2ca4c65a |
32 | /* Since we decided all IMAP servers must implement the UID command as defined in |
33 | * the IMAP RFC, we force $uid_support to be on. |
34 | */ |
35 | |
36 | global $uid_support; |
37 | $uid_support = true; |
38 | |
d416901b |
39 | sqsession_is_active(); |
40 | |
180239ca |
41 | /* if running with magic_quotes_gpc then strip the slashes |
a32985a5 |
42 | from POST and GET global arrays */ |
43 | |
44 | if (get_magic_quotes_gpc()) { |
180239ca |
45 | sqstripslashes($_GET); |
46 | sqstripslashes($_POST); |
a32985a5 |
47 | } |
48 | |
49 | /* strip any tags added to the url from PHP_SELF. |
50 | This fixes hand crafted url XXS expoits for any |
51 | page that uses PHP_SELF as the FORM action */ |
52 | |
388c855c |
53 | $_SERVER['PHP_SELF'] = strip_tags($_SERVER['PHP_SELF']); |
a32985a5 |
54 | |
62f7daa5 |
55 | /** |
56 | * returns true if current php version is at mimimum a.b.c |
57 | * |
97bdc607 |
58 | * Called: check_php_version(4,1) |
8b096f0a |
59 | * @param int a major version number |
60 | * @param int b minor version number |
61 | * @param int c release number |
62 | * @return bool |
97bdc607 |
63 | */ |
62f7daa5 |
64 | function check_php_version ($a = '0', $b = '0', $c = '0') |
9697c5ab |
65 | { |
5673cabe |
66 | return version_compare ( PHP_VERSION, "$a.$b.$c", 'ge' ); |
9697c5ab |
67 | } |
68 | |
97bdc607 |
69 | /** |
62f7daa5 |
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 |
97bdc607 |
72 | * constructed by us, as an array of 3 ints. |
73 | * |
74 | * Called: check_sm_version(1,3,3) |
8b096f0a |
75 | * @param int a major version number |
76 | * @param int b minor version number |
77 | * @param int c release number |
78 | * @return bool |
97bdc607 |
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 || |
150c28d6 |
85 | ( $SQM_INTERNAL_VERSION[0] == $a && |
86 | $SQM_INTERNAL_VERSION[1] < $b) || |
87 | ( $SQM_INTERNAL_VERSION[0] == $a && |
88 | $SQM_INTERNAL_VERSION[1] == $b && |
97bdc607 |
89 | $SQM_INTERNAL_VERSION[2] < $c ) ) { |
90 | return FALSE; |
62f7daa5 |
91 | } |
92 | return TRUE; |
97bdc607 |
93 | } |
94 | |
95 | |
8b096f0a |
96 | /** |
97 | * Recursively strip slashes from the values of an array. |
98 | * @param array array the array to strip, passed by reference |
99 | * @return void |
100 | */ |
a32985a5 |
101 | function sqstripslashes(&$array) { |
3aa17cf9 |
102 | if(count($array) > 0) { |
103 | foreach ($array as $index=>$value) { |
104 | if (is_array($array[$index])) { |
105 | sqstripslashes($array[$index]); |
106 | } |
107 | else { |
108 | $array[$index] = stripslashes($value); |
109 | } |
a32985a5 |
110 | } |
111 | } |
112 | } |
113 | |
8b096f0a |
114 | /** |
115 | * Add a variable to the session. |
116 | * @param mixed $var the variable to register |
117 | * @param string $name the name to refer to this variable |
118 | * @return void |
119 | */ |
61d9ec71 |
120 | function sqsession_register ($var, $name) { |
281c3d5b |
121 | |
122 | sqsession_is_active(); |
123 | |
62f7daa5 |
124 | $_SESSION["$name"] = $var; |
125 | |
dcc1cc82 |
126 | session_register("$name"); |
61d9ec71 |
127 | } |
3aa17cf9 |
128 | |
8b096f0a |
129 | /** |
130 | * Delete a variable from the session. |
131 | * @param string $name the name of the var to delete |
132 | * @return void |
133 | */ |
61d9ec71 |
134 | function sqsession_unregister ($name) { |
281c3d5b |
135 | |
136 | sqsession_is_active(); |
137 | |
abd74f7d |
138 | unset($_SESSION[$name]); |
62f7daa5 |
139 | |
dcc1cc82 |
140 | session_unregister("$name"); |
61d9ec71 |
141 | } |
3aa17cf9 |
142 | |
8b096f0a |
143 | /** |
144 | * Checks to see if a variable has already been registered |
145 | * in the session. |
146 | * @param string $name the name of the var to check |
147 | * @return bool whether the var has been registered |
148 | */ |
d7c82551 |
149 | function sqsession_is_registered ($name) { |
150 | $test_name = &$name; |
151 | $result = false; |
62f7daa5 |
152 | |
abd74f7d |
153 | if (isset($_SESSION[$test_name])) { |
154 | $result = true; |
d7c82551 |
155 | } |
62f7daa5 |
156 | |
d7c82551 |
157 | return $result; |
158 | } |
159 | |
61d9ec71 |
160 | |
4cd8ae7d |
161 | define('SQ_INORDER',0); |
162 | define('SQ_GET',1); |
163 | define('SQ_POST',2); |
164 | define('SQ_SESSION',3); |
27d0841c |
165 | define('SQ_COOKIE',4); |
166 | define('SQ_SERVER',5); |
d9ad2525 |
167 | define('SQ_FORM',6); |
4cd8ae7d |
168 | |
169 | /** |
27d0841c |
170 | * Search for the var $name in $_SESSION, $_POST, $_GET, |
62f7daa5 |
171 | * $_COOKIE, or $_SERVER and set it in provided var. |
d1975c5b |
172 | * |
4cd8ae7d |
173 | * If $search is not provided, or == SQ_INORDER, it will search |
174 | * $_SESSION, then $_POST, then $_GET. Otherwise, |
62f7daa5 |
175 | * use one of the defined constants to look for |
4cd8ae7d |
176 | * a var in one place specifically. |
d1975c5b |
177 | * |
62f7daa5 |
178 | * Note: $search is an int value equal to one of the |
d1975c5b |
179 | * constants defined above. |
180 | * |
181 | * example: |
182 | * sqgetGlobalVar('username',$username,SQ_SESSION); |
183 | * -- no quotes around last param! |
184 | * |
8b096f0a |
185 | * @param string name the name of the var to search |
186 | * @param mixed value the variable to return |
187 | * @param int search constant defining where to look |
188 | * @return bool whether variable is found. |
4cd8ae7d |
189 | */ |
190 | function sqgetGlobalVar($name, &$value, $search = SQ_INORDER) { |
f79c19a4 |
191 | |
d1975c5b |
192 | /* NOTE: DO NOT enclose the constants in the switch |
193 | statement with quotes. They are constant values, |
194 | enclosing them in quotes will cause them to evaluate |
195 | as strings. */ |
4cd8ae7d |
196 | switch ($search) { |
62f7daa5 |
197 | /* we want the default case to be first here, |
051f6245 |
198 | so that if a valid value isn't specified, |
199 | all three arrays will be searched. */ |
d1975c5b |
200 | default: |
d9ad2525 |
201 | case SQ_INORDER: // check session, post, get |
d1975c5b |
202 | case SQ_SESSION: |
203 | if( isset($_SESSION[$name]) ) { |
4cd8ae7d |
204 | $value = $_SESSION[$name]; |
d1975c5b |
205 | return TRUE; |
206 | } elseif ( $search == SQ_SESSION ) { |
207 | break; |
208 | } |
d9ad2525 |
209 | case SQ_FORM: // check post, get |
d1975c5b |
210 | case SQ_POST: |
211 | if( isset($_POST[$name]) ) { |
4cd8ae7d |
212 | $value = $_POST[$name]; |
d1975c5b |
213 | return TRUE; |
214 | } elseif ( $search == SQ_POST ) { |
27d0841c |
215 | break; |
d1975c5b |
216 | } |
217 | case SQ_GET: |
218 | if ( isset($_GET[$name]) ) { |
219 | $value = $_GET[$name]; |
220 | return TRUE; |
62f7daa5 |
221 | } |
d1975c5b |
222 | /* NO IF HERE. FOR SQ_INORDER CASE, EXIT after GET */ |
223 | break; |
224 | case SQ_COOKIE: |
225 | if ( isset($_COOKIE[$name]) ) { |
226 | $value = $_COOKIE[$name]; |
62f7daa5 |
227 | return TRUE; |
d1975c5b |
228 | } |
229 | break; |
230 | case SQ_SERVER: |
d1975c5b |
231 | if ( isset($_SERVER[$name]) ) { |
232 | $value = $_SERVER[$name]; |
233 | return TRUE; |
234 | } |
235 | break; |
4cd8ae7d |
236 | } |
511f5c33 |
237 | /* Nothing found, return FALSE */ |
4cd8ae7d |
238 | return FALSE; |
239 | } |
240 | |
8b096f0a |
241 | /** |
242 | * Deletes an existing session, more advanced than the standard PHP |
243 | * session_destroy(), it explicitly deletes the cookies and global vars. |
244 | */ |
513db22c |
245 | function sqsession_destroy() { |
242342d0 |
246 | |
281c3d5b |
247 | /* |
248 | * php.net says we can kill the cookie by setting just the name: |
249 | * http://www.php.net/manual/en/function.setcookie.php |
250 | * maybe this will help fix the session merging again. |
251 | * |
252 | * Changed the theory on this to kill the cookies first starting |
253 | * a new session will provide a new session for all instances of |
254 | * the browser, we don't want that, as that is what is causing the |
255 | * merging of sessions. |
256 | */ |
242342d0 |
257 | |
f9902ccb |
258 | global $base_uri; |
f31687f6 |
259 | |
260 | if (isset($_COOKIE[session_name()])) setcookie(session_name(), '', time() - 5, $base_uri); |
261 | if (isset($_COOKIE['username'])) setcookie('username','',time() - 5,$base_uri); |
262 | if (isset($_COOKIE['key'])) setcookie('key','',time() - 5,$base_uri); |
281c3d5b |
263 | |
264 | $sessid = session_id(); |
265 | if (!empty( $sessid )) { |
abd74f7d |
266 | $_SESSION = array(); |
21e18f59 |
267 | @session_destroy(); |
242342d0 |
268 | } |
269 | |
281c3d5b |
270 | } |
242342d0 |
271 | |
8b096f0a |
272 | /** |
281c3d5b |
273 | * Function to verify a session has been started. If it hasn't |
274 | * start a session up. php.net doesn't tell you that $_SESSION |
275 | * (even though autoglobal), is not created unless a session is |
276 | * started, unlike $_POST, $_GET and such |
277 | */ |
278 | |
279 | function sqsession_is_active() { |
62f7daa5 |
280 | |
281c3d5b |
281 | $sessid = session_id(); |
282 | if ( empty( $sessid ) ) { |
283 | session_start(); |
284 | } |
513db22c |
285 | } |
286 | |
5673cabe |
287 | // vim: et ts=4 |
f8a1ed5a |
288 | ?> |