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