changelog
[squirrelmail.git] / functions / global.php
CommitLineData
61d9ec71 1<?php
2
3/**
4 * globals.php
5 *
6 * Copyright (c) 1999-2002 The SquirrelMail Project Team
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
388c855c 16/* If magic_quotes_runtime is on, SquirrelMail breaks in new and creative ways.
17 * Force magic_quotes_runtime off.
18 * chilts@birdbrained.org - I put it here in the hopes that all SM code includes this.
19 * If there's a better place, please let me know.
20 */
5f660901 21ini_set('magic_quotes_runtime','0');
61d9ec71 22
23/* convert old-style superglobals to current method
24 * this is executed if you are running PHP 4.0.x.
25 * it is run via a require_once directive in validate.php
26 * and redirect.php. Patch submitted by Ray Black.
27 */
28
9697c5ab 29if ( !check_php_version(4,1) ) {
61d9ec71 30 global $_COOKIE, $_ENV, $_FILES, $_GET, $_POST, $_SERVER, $_SESSION;
31 global $HTTP_COOKIE_VARS, $HTTP_ENV_VARS, $HTTP_POST_FILES, $HTTP_GET_VARS,
32 $HTTP_POST_VARS, $HTTP_SERVER_VARS, $HTTP_SESSION_VARS;
33 $_COOKIE =& $HTTP_COOKIE_VARS;
34 $_ENV =& $HTTP_ENV_VARS;
35 $_FILES =& $HTTP_POST_FILES;
36 $_GET =& $HTTP_GET_VARS;
37 $_POST =& $HTTP_POST_VARS;
38 $_SERVER =& $HTTP_SERVER_VARS;
39 $_SESSION =& $HTTP_SESSION_VARS;
40}
41
180239ca 42/* if running with magic_quotes_gpc then strip the slashes
a32985a5 43 from POST and GET global arrays */
44
45if (get_magic_quotes_gpc()) {
180239ca 46 sqstripslashes($_GET);
47 sqstripslashes($_POST);
a32985a5 48}
49
50/* strip any tags added to the url from PHP_SELF.
51 This fixes hand crafted url XXS expoits for any
52 page that uses PHP_SELF as the FORM action */
53
388c855c 54$_SERVER['PHP_SELF'] = strip_tags($_SERVER['PHP_SELF']);
a32985a5 55
97bdc607 56/**
57 * returns true if current php version is at mimimum a.b.c
58 *
59 * Called: check_php_version(4,1)
60 */
9697c5ab 61function check_php_version ($a = '0', $b = '0', $c = '0')
62{
3aa17cf9 63 global $SQ_PHP_VERSION;
97bdc607 64
3aa17cf9 65 if(!isset($SQ_PHP_VERSION))
5123154f 66 $SQ_PHP_VERSION = substr( str_pad( preg_replace('/\D/','', PHP_VERSION), 3, '0'), 0, 3);
9697c5ab 67
3aa17cf9 68 return $SQ_PHP_VERSION >= ($a.$b.$c);
9697c5ab 69}
70
97bdc607 71/**
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
74 * constructed by us, as an array of 3 ints.
75 *
76 * Called: check_sm_version(1,3,3)
77 */
78function check_sm_version($a = 0, $b = 0, $c = 0)
79{
80 global $SQM_INTERNAL_VERSION;
81 if ( !isset($SQM_INTERNAL_VERSION) ||
82 $SQM_INTERNAL_VERSION[0] < $a ||
83 $SQM_INTERNAL_VERSION[1] < $b ||
84 ( $SQM_INTERNAL_VERSION[1] == $b &&
85 $SQM_INTERNAL_VERSION[2] < $c ) ) {
86 return FALSE;
87 }
88 return TRUE;
89}
90
91
3aa17cf9 92/* recursively strip slashes from the values of an array */
a32985a5 93function sqstripslashes(&$array) {
3aa17cf9 94 if(count($array) > 0) {
95 foreach ($array as $index=>$value) {
96 if (is_array($array[$index])) {
97 sqstripslashes($array[$index]);
98 }
99 else {
100 $array[$index] = stripslashes($value);
101 }
a32985a5 102 }
103 }
104}
105
61d9ec71 106function sqsession_register ($var, $name) {
281c3d5b 107
108 sqsession_is_active();
109
9697c5ab 110 if ( !check_php_version(4,1) ) {
61d9ec71 111 global $HTTP_SESSION_VARS;
9697c5ab 112 $HTTP_SESSION_VARS[$name] = $var;
61d9ec71 113 }
114 else {
d7c82551 115 $_SESSION["$name"] = $var;
61d9ec71 116 }
3658a999 117 session_register("$name");
61d9ec71 118}
3aa17cf9 119
61d9ec71 120function sqsession_unregister ($name) {
281c3d5b 121
122 sqsession_is_active();
123
9697c5ab 124 if ( !check_php_version(4,1) ) {
d7c82551 125 global $HTTP_SESSION_VARS;
9697c5ab 126 unset($HTTP_SESSION_VARS[$name]);
61d9ec71 127 }
128 else {
9697c5ab 129 unset($_SESSION[$name]);
61d9ec71 130 }
3658a999 131 session_unregister("$name");
61d9ec71 132}
3aa17cf9 133
d7c82551 134function sqsession_is_registered ($name) {
135 $test_name = &$name;
136 $result = false;
9697c5ab 137 if ( !check_php_version(4,1) ) {
d7c82551 138 global $HTTP_SESSION_VARS;
139 if (isset($HTTP_SESSION_VARS[$test_name])) {
140 $result = true;
141 }
142 }
143 else {
144 if (isset($_SESSION[$test_name])) {
145 $result = true;
146 }
147 }
148 return $result;
149}
150
61d9ec71 151
152/**
153 * Search for the var $name in $_SESSION, $_POST, $_GET
154 * (in that order) and register it as a global var.
155 */
156function sqextractGlobalVar ($name) {
9697c5ab 157 if ( !check_php_version(4,1) ) {
a32985a5 158 global $_SESSION, $_GET, $_POST;
159 }
160 global $$name;
61d9ec71 161 if( isset($_SESSION[$name]) ) {
162 $$name = $_SESSION[$name];
163 }
164 if( isset($_POST[$name]) ) {
165 $$name = $_POST[$name];
166 }
167 else if ( isset($_GET[$name]) ) {
168 $$name = $_GET[$name];
169 }
170}
513db22c 171
172function sqsession_destroy() {
242342d0 173
281c3d5b 174 /*
175 * php.net says we can kill the cookie by setting just the name:
176 * http://www.php.net/manual/en/function.setcookie.php
177 * maybe this will help fix the session merging again.
178 *
179 * Changed the theory on this to kill the cookies first starting
180 * a new session will provide a new session for all instances of
181 * the browser, we don't want that, as that is what is causing the
182 * merging of sessions.
183 */
242342d0 184
f9902ccb 185 global $base_uri;
f31687f6 186
187 if (isset($_COOKIE[session_name()])) setcookie(session_name(), '', time() - 5, $base_uri);
188 if (isset($_COOKIE['username'])) setcookie('username','',time() - 5,$base_uri);
189 if (isset($_COOKIE['key'])) setcookie('key','',time() - 5,$base_uri);
281c3d5b 190
191 $sessid = session_id();
192 if (!empty( $sessid )) {
193 if ( !check_php_version(4,1) ) {
194 global $HTTP_SESSION_VARS;
195 $HTTP_SESSION_VARS = array();
196 } else {
197 $_SESSION = array();
198 }
199 @session_destroy;
242342d0 200 }
201
281c3d5b 202}
242342d0 203
281c3d5b 204/*
205 * Function to verify a session has been started. If it hasn't
206 * start a session up. php.net doesn't tell you that $_SESSION
207 * (even though autoglobal), is not created unless a session is
208 * started, unlike $_POST, $_GET and such
209 */
210
211function sqsession_is_active() {
212
213 $sessid = session_id();
214 if ( empty( $sessid ) ) {
215 session_start();
216 }
513db22c 217}
218
281c3d5b 219
61d9ec71 220?>