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