Fix plugin list in admin plugin
[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
161/**
162 * Search for the var $name in $_SESSION, $_POST, $_GET
163 * (in that order) and register it as a global var.
164 */
165function sqextractGlobalVar ($name) {
9697c5ab 166 if ( !check_php_version(4,1) ) {
a32985a5 167 global $_SESSION, $_GET, $_POST;
168 }
169 global $$name;
61d9ec71 170 if( isset($_SESSION[$name]) ) {
171 $$name = $_SESSION[$name];
172 }
173 if( isset($_POST[$name]) ) {
174 $$name = $_POST[$name];
175 }
176 else if ( isset($_GET[$name]) ) {
177 $$name = $_GET[$name];
178 }
179}
513db22c 180
181function sqsession_destroy() {
242342d0 182
281c3d5b 183 /*
184 * php.net says we can kill the cookie by setting just the name:
185 * http://www.php.net/manual/en/function.setcookie.php
186 * maybe this will help fix the session merging again.
187 *
188 * Changed the theory on this to kill the cookies first starting
189 * a new session will provide a new session for all instances of
190 * the browser, we don't want that, as that is what is causing the
191 * merging of sessions.
192 */
242342d0 193
f9902ccb 194 global $base_uri;
f31687f6 195
196 if (isset($_COOKIE[session_name()])) setcookie(session_name(), '', time() - 5, $base_uri);
197 if (isset($_COOKIE['username'])) setcookie('username','',time() - 5,$base_uri);
198 if (isset($_COOKIE['key'])) setcookie('key','',time() - 5,$base_uri);
281c3d5b 199
200 $sessid = session_id();
201 if (!empty( $sessid )) {
202 if ( !check_php_version(4,1) ) {
203 global $HTTP_SESSION_VARS;
204 $HTTP_SESSION_VARS = array();
205 } else {
206 $_SESSION = array();
207 }
208 @session_destroy;
242342d0 209 }
210
281c3d5b 211}
242342d0 212
281c3d5b 213/*
214 * Function to verify a session has been started. If it hasn't
215 * start a session up. php.net doesn't tell you that $_SESSION
216 * (even though autoglobal), is not created unless a session is
217 * started, unlike $_POST, $_GET and such
218 */
219
220function sqsession_is_active() {
221
222 $sessid = session_id();
223 if ( empty( $sessid ) ) {
224 session_start();
225 }
513db22c 226}
227
281c3d5b 228
61d9ec71 229?>