Fix for paginator, 1 * 0.75 evaluates as 0. 1 * 3/4 evaluates as 0.75
[squirrelmail.git] / functions / global.php
CommitLineData
61d9ec71 1<?php
2
3/**
0c701a88 4 * global.php
61d9ec71 5 *
76911253 6 * Copyright (c) 1999-2003 The SquirrelMail Project Team
61d9ec71 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
242342d0 10 * It also has some session register functions that work across various
61d9ec71 11 * php versions.
12 *
242342d0 13 * $Id$
61d9ec71 14 */
15
ebabf3f5 16require_once(SM_PATH . 'config/config.php');
17
18/* set the name of the session cookie */
19if(isset($session_name) && $session_name) {
20 ini_set('session.name' , $session_name);
21} else {
22 ini_set('session.name' , 'SQMSESSID');
23}
24
388c855c 25/* If magic_quotes_runtime is on, SquirrelMail breaks in new and creative ways.
26 * Force magic_quotes_runtime off.
0c701a88 27 * tassium@squirrelmail.org - I put it here in the hopes that all SM code includes this.
388c855c 28 * If there's a better place, please let me know.
29 */
5f660901 30ini_set('magic_quotes_runtime','0');
61d9ec71 31
0c701a88 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
36global $uid_support;
37$uid_support = true;
38
d416901b 39sqsession_is_active();
40
61d9ec71 41/* convert old-style superglobals to current method
42 * this is executed if you are running PHP 4.0.x.
43 * it is run via a require_once directive in validate.php
44 * and redirect.php. Patch submitted by Ray Black.
45 */
46
9697c5ab 47if ( !check_php_version(4,1) ) {
61d9ec71 48 global $_COOKIE, $_ENV, $_FILES, $_GET, $_POST, $_SERVER, $_SESSION;
49 global $HTTP_COOKIE_VARS, $HTTP_ENV_VARS, $HTTP_POST_FILES, $HTTP_GET_VARS,
cc407257 50 $HTTP_POST_VARS, $HTTP_SERVER_VARS, $HTTP_SESSION_VARS, $PHP_SELF;
61d9ec71 51 $_COOKIE =& $HTTP_COOKIE_VARS;
52 $_ENV =& $HTTP_ENV_VARS;
53 $_FILES =& $HTTP_POST_FILES;
54 $_GET =& $HTTP_GET_VARS;
55 $_POST =& $HTTP_POST_VARS;
56 $_SERVER =& $HTTP_SERVER_VARS;
57 $_SESSION =& $HTTP_SESSION_VARS;
cc407257 58 if (!isset($PHP_SELF) || empty($PHP_SELF)) {
59 $PHP_SELF = $HTTP_SERVER_VARS['PHP_SELF'];
60 }
61d9ec71 61}
62
180239ca 63/* if running with magic_quotes_gpc then strip the slashes
a32985a5 64 from POST and GET global arrays */
65
66if (get_magic_quotes_gpc()) {
180239ca 67 sqstripslashes($_GET);
68 sqstripslashes($_POST);
a32985a5 69}
70
71/* strip any tags added to the url from PHP_SELF.
72 This fixes hand crafted url XXS expoits for any
73 page that uses PHP_SELF as the FORM action */
74
388c855c 75$_SERVER['PHP_SELF'] = strip_tags($_SERVER['PHP_SELF']);
a32985a5 76
97bdc607 77/**
78 * returns true if current php version is at mimimum a.b.c
79 *
80 * Called: check_php_version(4,1)
81 */
9697c5ab 82function check_php_version ($a = '0', $b = '0', $c = '0')
83{
3aa17cf9 84 global $SQ_PHP_VERSION;
97bdc607 85
3aa17cf9 86 if(!isset($SQ_PHP_VERSION))
5123154f 87 $SQ_PHP_VERSION = substr( str_pad( preg_replace('/\D/','', PHP_VERSION), 3, '0'), 0, 3);
9697c5ab 88
3aa17cf9 89 return $SQ_PHP_VERSION >= ($a.$b.$c);
9697c5ab 90}
91
97bdc607 92/**
93 * returns true if the current internal SM version is at minimum a.b.c
94 * These are plain integer comparisons, as our internal version is
95 * constructed by us, as an array of 3 ints.
96 *
97 * Called: check_sm_version(1,3,3)
98 */
99function check_sm_version($a = 0, $b = 0, $c = 0)
100{
101 global $SQM_INTERNAL_VERSION;
102 if ( !isset($SQM_INTERNAL_VERSION) ||
103 $SQM_INTERNAL_VERSION[0] < $a ||
104 $SQM_INTERNAL_VERSION[1] < $b ||
105 ( $SQM_INTERNAL_VERSION[1] == $b &&
106 $SQM_INTERNAL_VERSION[2] < $c ) ) {
107 return FALSE;
108 }
109 return TRUE;
110}
111
112
3aa17cf9 113/* recursively strip slashes from the values of an array */
a32985a5 114function sqstripslashes(&$array) {
3aa17cf9 115 if(count($array) > 0) {
116 foreach ($array as $index=>$value) {
117 if (is_array($array[$index])) {
118 sqstripslashes($array[$index]);
119 }
120 else {
121 $array[$index] = stripslashes($value);
122 }
a32985a5 123 }
124 }
125}
126
61d9ec71 127function sqsession_register ($var, $name) {
281c3d5b 128
129 sqsession_is_active();
130
9697c5ab 131 if ( !check_php_version(4,1) ) {
61d9ec71 132 global $HTTP_SESSION_VARS;
9697c5ab 133 $HTTP_SESSION_VARS[$name] = $var;
61d9ec71 134 }
135 else {
d7c82551 136 $_SESSION["$name"] = $var;
61d9ec71 137 }
3658a999 138 session_register("$name");
61d9ec71 139}
3aa17cf9 140
61d9ec71 141function sqsession_unregister ($name) {
281c3d5b 142
143 sqsession_is_active();
144
9697c5ab 145 if ( !check_php_version(4,1) ) {
d7c82551 146 global $HTTP_SESSION_VARS;
9697c5ab 147 unset($HTTP_SESSION_VARS[$name]);
61d9ec71 148 }
149 else {
9697c5ab 150 unset($_SESSION[$name]);
61d9ec71 151 }
3658a999 152 session_unregister("$name");
61d9ec71 153}
3aa17cf9 154
d7c82551 155function sqsession_is_registered ($name) {
156 $test_name = &$name;
157 $result = false;
9697c5ab 158 if ( !check_php_version(4,1) ) {
d7c82551 159 global $HTTP_SESSION_VARS;
160 if (isset($HTTP_SESSION_VARS[$test_name])) {
161 $result = true;
162 }
163 }
164 else {
165 if (isset($_SESSION[$test_name])) {
166 $result = true;
167 }
168 }
169 return $result;
170}
171
61d9ec71 172
4cd8ae7d 173define('SQ_INORDER',0);
174define('SQ_GET',1);
175define('SQ_POST',2);
176define('SQ_SESSION',3);
27d0841c 177define('SQ_COOKIE',4);
178define('SQ_SERVER',5);
d9ad2525 179define('SQ_FORM',6);
4cd8ae7d 180
181/**
27d0841c 182 * Search for the var $name in $_SESSION, $_POST, $_GET,
183 * $_COOKIE, or $_SERVER and set it in provided var.
d1975c5b 184 *
4cd8ae7d 185 * If $search is not provided, or == SQ_INORDER, it will search
186 * $_SESSION, then $_POST, then $_GET. Otherwise,
187 * use one of the defined constants to look for
188 * a var in one place specifically.
d1975c5b 189 *
190 * Note: $search is an int value equal to one of the
191 * constants defined above.
192 *
193 * example:
194 * sqgetGlobalVar('username',$username,SQ_SESSION);
195 * -- no quotes around last param!
196 *
4cd8ae7d 197 * Returns FALSE if variable is not found.
198 * Returns TRUE if it is.
199 */
200function sqgetGlobalVar($name, &$value, $search = SQ_INORDER) {
f79c19a4 201
4cd8ae7d 202 if ( !check_php_version(4,1) ) {
f79c19a4 203 global $HTTP_COOKIE_VARS, $HTTP_GET_VARS, $HTTP_POST_VARS,
204 $HTTP_SERVER_VARS, $HTTP_SESSION_VARS;
205
206 $_COOKIE =& $HTTP_COOKIE_VARS;
207 $_GET =& $HTTP_GET_VARS;
208 $_POST =& $HTTP_POST_VARS;
209 $_SERVER =& $HTTP_SERVER_VARS;
210 $_SESSION =& $HTTP_SESSION_VARS;
4cd8ae7d 211 }
d1975c5b 212
213 /* NOTE: DO NOT enclose the constants in the switch
214 statement with quotes. They are constant values,
215 enclosing them in quotes will cause them to evaluate
216 as strings. */
4cd8ae7d 217 switch ($search) {
218 /* we want the default case to be first here,
219 so that if a valid value isn't specified,
220 all three arrays will be searched. */
d1975c5b 221 default:
d9ad2525 222 case SQ_INORDER: // check session, post, get
d1975c5b 223 case SQ_SESSION:
224 if( isset($_SESSION[$name]) ) {
4cd8ae7d 225 $value = $_SESSION[$name];
d1975c5b 226 return TRUE;
227 } elseif ( $search == SQ_SESSION ) {
228 break;
229 }
d9ad2525 230 case SQ_FORM: // check post, get
d1975c5b 231 case SQ_POST:
232 if( isset($_POST[$name]) ) {
4cd8ae7d 233 $value = $_POST[$name];
d1975c5b 234 return TRUE;
235 } elseif ( $search == SQ_POST ) {
27d0841c 236 break;
d1975c5b 237 }
238 case SQ_GET:
239 if ( isset($_GET[$name]) ) {
240 $value = $_GET[$name];
241 return TRUE;
242 }
243 /* NO IF HERE. FOR SQ_INORDER CASE, EXIT after GET */
244 break;
245 case SQ_COOKIE:
246 if ( isset($_COOKIE[$name]) ) {
247 $value = $_COOKIE[$name];
248 return TRUE;
249 }
250 break;
251 case SQ_SERVER:
d1975c5b 252 if ( isset($_SERVER[$name]) ) {
253 $value = $_SERVER[$name];
254 return TRUE;
255 }
256 break;
4cd8ae7d 257 }
258 return FALSE;
259}
260
513db22c 261function sqsession_destroy() {
242342d0 262
281c3d5b 263 /*
264 * php.net says we can kill the cookie by setting just the name:
265 * http://www.php.net/manual/en/function.setcookie.php
266 * maybe this will help fix the session merging again.
267 *
268 * Changed the theory on this to kill the cookies first starting
269 * a new session will provide a new session for all instances of
270 * the browser, we don't want that, as that is what is causing the
271 * merging of sessions.
272 */
242342d0 273
f9902ccb 274 global $base_uri;
f31687f6 275
276 if (isset($_COOKIE[session_name()])) setcookie(session_name(), '', time() - 5, $base_uri);
277 if (isset($_COOKIE['username'])) setcookie('username','',time() - 5,$base_uri);
278 if (isset($_COOKIE['key'])) setcookie('key','',time() - 5,$base_uri);
281c3d5b 279
280 $sessid = session_id();
281 if (!empty( $sessid )) {
282 if ( !check_php_version(4,1) ) {
283 global $HTTP_SESSION_VARS;
284 $HTTP_SESSION_VARS = array();
285 } else {
286 $_SESSION = array();
287 }
21e18f59 288 @session_destroy();
242342d0 289 }
290
281c3d5b 291}
242342d0 292
281c3d5b 293/*
294 * Function to verify a session has been started. If it hasn't
295 * start a session up. php.net doesn't tell you that $_SESSION
296 * (even though autoglobal), is not created unless a session is
297 * started, unlike $_POST, $_GET and such
298 */
299
300function sqsession_is_active() {
301
302 $sessid = session_id();
303 if ( empty( $sessid ) ) {
304 session_start();
305 }
513db22c 306}
307
281c3d5b 308
61d9ec71 309?>