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