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