Fix check_php_version for versions that contain a patchlevel.
[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
16
17/* convert old-style superglobals to current method
18 * this is executed if you are running PHP 4.0.x.
19 * it is run via a require_once directive in validate.php
20 * and redirect.php. Patch submitted by Ray Black.
21 */
22
9697c5ab 23if ( !check_php_version(4,1) ) {
61d9ec71 24 global $_COOKIE, $_ENV, $_FILES, $_GET, $_POST, $_SERVER, $_SESSION;
25 global $HTTP_COOKIE_VARS, $HTTP_ENV_VARS, $HTTP_POST_FILES, $HTTP_GET_VARS,
26 $HTTP_POST_VARS, $HTTP_SERVER_VARS, $HTTP_SESSION_VARS;
27 $_COOKIE =& $HTTP_COOKIE_VARS;
28 $_ENV =& $HTTP_ENV_VARS;
29 $_FILES =& $HTTP_POST_FILES;
30 $_GET =& $HTTP_GET_VARS;
31 $_POST =& $HTTP_POST_VARS;
32 $_SERVER =& $HTTP_SERVER_VARS;
33 $_SESSION =& $HTTP_SESSION_VARS;
34}
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
48strip_tags($_SERVER['PHP_SELF']);
49
9697c5ab 50/* returns true if current php version is at mimimum a.b.c */
51function check_php_version ($a = '0', $b = '0', $c = '0')
52{
3aa17cf9 53 global $SQ_PHP_VERSION;
9697c5ab 54
3aa17cf9 55 if(!isset($SQ_PHP_VERSION))
5123154f 56 $SQ_PHP_VERSION = substr( str_pad( preg_replace('/\D/','', PHP_VERSION), 3, '0'), 0, 3);
9697c5ab 57
3aa17cf9 58 return $SQ_PHP_VERSION >= ($a.$b.$c);
9697c5ab 59}
60
3aa17cf9 61/* recursively strip slashes from the values of an array */
a32985a5 62function sqstripslashes(&$array) {
3aa17cf9 63 if(count($array) > 0) {
64 foreach ($array as $index=>$value) {
65 if (is_array($array[$index])) {
66 sqstripslashes($array[$index]);
67 }
68 else {
69 $array[$index] = stripslashes($value);
70 }
a32985a5 71 }
72 }
73}
74
61d9ec71 75function sqsession_register ($var, $name) {
9697c5ab 76 if ( !check_php_version(4,1) ) {
61d9ec71 77 global $HTTP_SESSION_VARS;
9697c5ab 78 $HTTP_SESSION_VARS[$name] = $var;
61d9ec71 79 }
80 else {
d7c82551 81 $_SESSION["$name"] = $var;
61d9ec71 82 }
83}
3aa17cf9 84
61d9ec71 85function sqsession_unregister ($name) {
9697c5ab 86 if ( !check_php_version(4,1) ) {
d7c82551 87 global $HTTP_SESSION_VARS;
9697c5ab 88 unset($HTTP_SESSION_VARS[$name]);
61d9ec71 89 }
90 else {
9697c5ab 91 unset($_SESSION[$name]);
61d9ec71 92 }
93}
3aa17cf9 94
d7c82551 95function sqsession_is_registered ($name) {
96 $test_name = &$name;
97 $result = false;
9697c5ab 98 if ( !check_php_version(4,1) ) {
d7c82551 99 global $HTTP_SESSION_VARS;
100 if (isset($HTTP_SESSION_VARS[$test_name])) {
101 $result = true;
102 }
103 }
104 else {
105 if (isset($_SESSION[$test_name])) {
106 $result = true;
107 }
108 }
109 return $result;
110}
111
61d9ec71 112
113/**
114 * Search for the var $name in $_SESSION, $_POST, $_GET
115 * (in that order) and register it as a global var.
116 */
117function sqextractGlobalVar ($name) {
9697c5ab 118 if ( !check_php_version(4,1) ) {
a32985a5 119 global $_SESSION, $_GET, $_POST;
120 }
121 global $$name;
61d9ec71 122 if( isset($_SESSION[$name]) ) {
123 $$name = $_SESSION[$name];
124 }
125 if( isset($_POST[$name]) ) {
126 $$name = $_POST[$name];
127 }
128 else if ( isset($_GET[$name]) ) {
129 $$name = $_GET[$name];
130 }
131}
513db22c 132
133function sqsession_destroy() {
242342d0 134 global $base_uri;
135
136 /* start session to be able to destroy it later */
137 session_start();
138
9697c5ab 139 if ( !check_php_version(4,1) ) {
242342d0 140 global $HTTP_SESSION_VARS;
141 $HTTP_SESSION_VARS = array();
142 }
143 else {
144 $_SESSION = array();
145 }
146
147 /*
148 * now reset cookies to 5 seconds ago to delete from browser
149 */
150
151 @session_destroy();
152 $cookie_params = session_get_cookie_params();
153 setcookie(session_name(), '', time() - 5, $cookie_params['path'],
154 $cookie_params['domain']);
155 setcookie('username', '', time() - 5, $base_uri);
156 setcookie('key', '', time() - 5 , $base_uri);
513db22c 157}
158
61d9ec71 159?>