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