Added new function:
[squirrelmail.git] / functions / global.php
1 <?php
2
3 /**
4 * globals.php
5 *
6 * Copyright (c) 1999-2003 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 define('SQ_INORDER',0);
162 define('SQ_GET',1);
163 define('SQ_POST',2);
164 define('SQ_SESSION',3);
165
166 /**
167 * Search for the var $name in $_SESSION, $_POST, $_GET
168 * and set it in provided var.
169 * If $search is not provided, or == SQ_INORDER, it will search
170 * $_SESSION, then $_POST, then $_GET. Otherwise,
171 * use one of the defined constants to look for
172 * a var in one place specifically.
173 * Returns FALSE if variable is not found.
174 * Returns TRUE if it is.
175 */
176 function sqgetGlobalVar($name, &$value, $search = SQ_INORDER) {
177 if ( !check_php_version(4,1) ) {
178 global $_SESSION, $_GET, $_POST;
179 }
180
181 switch ($search) {
182 /* we want the default case to be first here,
183 so that if a valid value isn't specified,
184 all three arrays will be searched. */
185 default:
186 case SQ_INORDER:
187 case SQ_SESSION:
188 if( isset($_SESSION[$name]) ) {
189 $value = $_SESSION[$name];
190 return TRUE;
191 } elseif ( $search == SQ_SESSION ) {
192 break;
193 }
194 case SQ_POST:
195 if( isset($_POST[$name]) ) {
196 $value = $_POST[$name];
197 return TRUE;
198 } elseif ( $search == SQ_POST ) {
199 break;
200 }
201 case SQ_GET:
202 if ( isset($_GET[$name]) ) {
203 $value = $_GET[$name];
204 return TRUE;
205 } elseif ( $search == SQ_GET ) {
206 break;
207 }
208 }
209 return FALSE;
210 }
211
212
213 /**
214 * Search for the var $name in $_SESSION, $_POST, $_GET
215 * (in that order) and register it as a global var.
216 */
217 function sqextractGlobalVar ($name) {
218 if ( !check_php_version(4,1) ) {
219 global $_SESSION, $_GET, $_POST;
220 }
221 global $$name;
222 if( isset($_SESSION[$name]) ) {
223 $$name = $_SESSION[$name];
224 }
225 if( isset($_POST[$name]) ) {
226 $$name = $_POST[$name];
227 }
228 else if ( isset($_GET[$name]) ) {
229 $$name = $_GET[$name];
230 }
231 }
232
233 function sqsession_destroy() {
234
235 /*
236 * php.net says we can kill the cookie by setting just the name:
237 * http://www.php.net/manual/en/function.setcookie.php
238 * maybe this will help fix the session merging again.
239 *
240 * Changed the theory on this to kill the cookies first starting
241 * a new session will provide a new session for all instances of
242 * the browser, we don't want that, as that is what is causing the
243 * merging of sessions.
244 */
245
246 global $base_uri;
247
248 if (isset($_COOKIE[session_name()])) setcookie(session_name(), '', time() - 5, $base_uri);
249 if (isset($_COOKIE['username'])) setcookie('username','',time() - 5,$base_uri);
250 if (isset($_COOKIE['key'])) setcookie('key','',time() - 5,$base_uri);
251
252 $sessid = session_id();
253 if (!empty( $sessid )) {
254 if ( !check_php_version(4,1) ) {
255 global $HTTP_SESSION_VARS;
256 $HTTP_SESSION_VARS = array();
257 } else {
258 $_SESSION = array();
259 }
260 @session_destroy;
261 }
262
263 }
264
265 /*
266 * Function to verify a session has been started. If it hasn't
267 * start a session up. php.net doesn't tell you that $_SESSION
268 * (even though autoglobal), is not created unless a session is
269 * started, unlike $_POST, $_GET and such
270 */
271
272 function sqsession_is_active() {
273
274 $sessid = session_id();
275 if ( empty( $sessid ) ) {
276 session_start();
277 }
278 }
279
280
281 ?>