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