Started section for 1.4.0-rc2. Please contribute what I've missed.
[squirrelmail.git] / functions / global.php
CommitLineData
61d9ec71 1<?php
2
3/**
4 * globals.php
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.
27 * chilts@birdbrained.org - I put it here in the hopes that all SM code includes this.
28 * If there's a better place, please let me know.
29 */
5f660901 30ini_set('magic_quotes_runtime','0');
61d9ec71 31
32/* convert old-style superglobals to current method
33 * this is executed if you are running PHP 4.0.x.
34 * it is run via a require_once directive in validate.php
35 * and redirect.php. Patch submitted by Ray Black.
36 */
37
9697c5ab 38if ( !check_php_version(4,1) ) {
61d9ec71 39 global $_COOKIE, $_ENV, $_FILES, $_GET, $_POST, $_SERVER, $_SESSION;
40 global $HTTP_COOKIE_VARS, $HTTP_ENV_VARS, $HTTP_POST_FILES, $HTTP_GET_VARS,
41 $HTTP_POST_VARS, $HTTP_SERVER_VARS, $HTTP_SESSION_VARS;
42 $_COOKIE =& $HTTP_COOKIE_VARS;
43 $_ENV =& $HTTP_ENV_VARS;
44 $_FILES =& $HTTP_POST_FILES;
45 $_GET =& $HTTP_GET_VARS;
46 $_POST =& $HTTP_POST_VARS;
47 $_SERVER =& $HTTP_SERVER_VARS;
48 $_SESSION =& $HTTP_SESSION_VARS;
49}
50
180239ca 51/* if running with magic_quotes_gpc then strip the slashes
a32985a5 52 from POST and GET global arrays */
53
54if (get_magic_quotes_gpc()) {
180239ca 55 sqstripslashes($_GET);
56 sqstripslashes($_POST);
a32985a5 57}
58
59/* strip any tags added to the url from PHP_SELF.
60 This fixes hand crafted url XXS expoits for any
61 page that uses PHP_SELF as the FORM action */
62
388c855c 63$_SERVER['PHP_SELF'] = strip_tags($_SERVER['PHP_SELF']);
a32985a5 64
97bdc607 65/**
66 * returns true if current php version is at mimimum a.b.c
67 *
68 * Called: check_php_version(4,1)
69 */
9697c5ab 70function check_php_version ($a = '0', $b = '0', $c = '0')
71{
3aa17cf9 72 global $SQ_PHP_VERSION;
97bdc607 73
3aa17cf9 74 if(!isset($SQ_PHP_VERSION))
5123154f 75 $SQ_PHP_VERSION = substr( str_pad( preg_replace('/\D/','', PHP_VERSION), 3, '0'), 0, 3);
9697c5ab 76
3aa17cf9 77 return $SQ_PHP_VERSION >= ($a.$b.$c);
9697c5ab 78}
79
97bdc607 80/**
81 * returns true if the current internal SM version is at minimum a.b.c
82 * These are plain integer comparisons, as our internal version is
83 * constructed by us, as an array of 3 ints.
84 *
85 * Called: check_sm_version(1,3,3)
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 ||
92 $SQM_INTERNAL_VERSION[1] < $b ||
93 ( $SQM_INTERNAL_VERSION[1] == $b &&
94 $SQM_INTERNAL_VERSION[2] < $c ) ) {
95 return FALSE;
96 }
97 return TRUE;
98}
99
100
3aa17cf9 101/* recursively strip slashes from the values of an array */
a32985a5 102function sqstripslashes(&$array) {
3aa17cf9 103 if(count($array) > 0) {
104 foreach ($array as $index=>$value) {
105 if (is_array($array[$index])) {
106 sqstripslashes($array[$index]);
107 }
108 else {
109 $array[$index] = stripslashes($value);
110 }
a32985a5 111 }
112 }
113}
114
61d9ec71 115function sqsession_register ($var, $name) {
281c3d5b 116
117 sqsession_is_active();
118
9697c5ab 119 if ( !check_php_version(4,1) ) {
61d9ec71 120 global $HTTP_SESSION_VARS;
9697c5ab 121 $HTTP_SESSION_VARS[$name] = $var;
61d9ec71 122 }
123 else {
d7c82551 124 $_SESSION["$name"] = $var;
61d9ec71 125 }
3658a999 126 session_register("$name");
61d9ec71 127}
3aa17cf9 128
61d9ec71 129function sqsession_unregister ($name) {
281c3d5b 130
131 sqsession_is_active();
132
9697c5ab 133 if ( !check_php_version(4,1) ) {
d7c82551 134 global $HTTP_SESSION_VARS;
9697c5ab 135 unset($HTTP_SESSION_VARS[$name]);
61d9ec71 136 }
137 else {
9697c5ab 138 unset($_SESSION[$name]);
61d9ec71 139 }
3658a999 140 session_unregister("$name");
61d9ec71 141}
3aa17cf9 142
d7c82551 143function sqsession_is_registered ($name) {
144 $test_name = &$name;
145 $result = false;
9697c5ab 146 if ( !check_php_version(4,1) ) {
d7c82551 147 global $HTTP_SESSION_VARS;
148 if (isset($HTTP_SESSION_VARS[$test_name])) {
149 $result = true;
150 }
151 }
152 else {
153 if (isset($_SESSION[$test_name])) {
154 $result = true;
155 }
156 }
157 return $result;
158}
159
61d9ec71 160
4cd8ae7d 161define('SQ_INORDER',0);
162define('SQ_GET',1);
163define('SQ_POST',2);
164define('SQ_SESSION',3);
165
166/**
167 * Search for the var $name in $_SESSION, $_POST, $_GET
168 * and set it in provided var.
169 * If $search is not provided, or == SQ_INORDER, it will search
170 * $_SESSION, then $_POST, then $_GET. Otherwise,
171 * use one of the defined constants to look for
172 * a var in one place specifically.
173 * Returns FALSE if variable is not found.
174 * Returns TRUE if it is.
175 */
176function sqgetGlobalVar($name, &$value, $search = SQ_INORDER) {
177 if ( !check_php_version(4,1) ) {
178 global $_SESSION, $_GET, $_POST;
179 }
180
181 switch ($search) {
182 /* we want the default case to be first here,
183 so that if a valid value isn't specified,
184 all three arrays will be searched. */
185 default:
186 case SQ_INORDER:
187 case SQ_SESSION:
188 if( isset($_SESSION[$name]) ) {
189 $value = $_SESSION[$name];
190 return TRUE;
191 } elseif ( $search == SQ_SESSION ) {
192 break;
193 }
194 case SQ_POST:
195 if( isset($_POST[$name]) ) {
196 $value = $_POST[$name];
197 return TRUE;
198 } elseif ( $search == SQ_POST ) {
199 break;
200 }
201 case SQ_GET:
202 if ( isset($_GET[$name]) ) {
203 $value = $_GET[$name];
204 return TRUE;
205 } elseif ( $search == SQ_GET ) {
206 break;
207 }
208 }
209 return FALSE;
210}
211
212
61d9ec71 213/**
214 * Search for the var $name in $_SESSION, $_POST, $_GET
215 * (in that order) and register it as a global var.
216 */
217function sqextractGlobalVar ($name) {
9697c5ab 218 if ( !check_php_version(4,1) ) {
a32985a5 219 global $_SESSION, $_GET, $_POST;
220 }
221 global $$name;
61d9ec71 222 if( isset($_SESSION[$name]) ) {
223 $$name = $_SESSION[$name];
224 }
225 if( isset($_POST[$name]) ) {
226 $$name = $_POST[$name];
227 }
228 else if ( isset($_GET[$name]) ) {
229 $$name = $_GET[$name];
230 }
231}
513db22c 232
233function sqsession_destroy() {
242342d0 234
281c3d5b 235 /*
236 * php.net says we can kill the cookie by setting just the name:
237 * http://www.php.net/manual/en/function.setcookie.php
238 * maybe this will help fix the session merging again.
239 *
240 * Changed the theory on this to kill the cookies first starting
241 * a new session will provide a new session for all instances of
242 * the browser, we don't want that, as that is what is causing the
243 * merging of sessions.
244 */
242342d0 245
f9902ccb 246 global $base_uri;
f31687f6 247
248 if (isset($_COOKIE[session_name()])) setcookie(session_name(), '', time() - 5, $base_uri);
249 if (isset($_COOKIE['username'])) setcookie('username','',time() - 5,$base_uri);
250 if (isset($_COOKIE['key'])) setcookie('key','',time() - 5,$base_uri);
281c3d5b 251
252 $sessid = session_id();
253 if (!empty( $sessid )) {
254 if ( !check_php_version(4,1) ) {
255 global $HTTP_SESSION_VARS;
256 $HTTP_SESSION_VARS = array();
257 } else {
258 $_SESSION = array();
259 }
260 @session_destroy;
242342d0 261 }
262
281c3d5b 263}
242342d0 264
281c3d5b 265/*
266 * Function to verify a session has been started. If it hasn't
267 * start a session up. php.net doesn't tell you that $_SESSION
268 * (even though autoglobal), is not created unless a session is
269 * started, unlike $_POST, $_GET and such
270 */
271
272function sqsession_is_active() {
273
274 $sessid = session_id();
275 if ( empty( $sessid ) ) {
276 session_start();
277 }
513db22c 278}
279
281c3d5b 280
61d9ec71 281?>