2 /************************************************************************
3 * MXChange v0.2.1 Start: 09/20/2005 *
4 * =============== Last change: 09/20/2005 *
6 * -------------------------------------------------------------------- *
7 * File : security_functions.php *
8 * -------------------------------------------------------------------- *
9 * Short description : Secure all GET, POST and COOKIE data *
10 * -------------------------------------------------------------------- *
11 * Kurzbeschreibung : Alle GET, POST und COOKIE-Daten sichern *
12 * -------------------------------------------------------------------- *
14 * -------------------------------------------------------------------- *
15 * Copyright (c) 2003 - 2008 by Roland Haeder *
16 * For more information visit: http://www.mxchange.org *
18 * This program is free software; you can redistribute it and/or modify *
19 * it under the terms of the GNU General Public License as published by *
20 * the Free Software Foundation; either version 2 of the License, or *
21 * (at your option) any later version. *
23 * This program is distributed in the hope that it will be useful, *
24 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
25 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
26 * GNU General Public License for more details. *
28 * You should have received a copy of the GNU General Public License *
29 * along with this program; if not, write to the Free Software *
30 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, *
32 ************************************************************************/
34 // Some security stuff...
35 if (ereg(basename(__FILE__), $_SERVER['PHP_SELF'])) {
36 $INC = substr(dirname(__FILE__), 0, strpos(dirname(__FILE__), "/inc") + 4) . "/security.php";
41 * Function to secure input strings
43 * @param $str The unsecured string
44 * @param $strip Strip tags
45 * @return $str A (hopefully) secured string against XSS and other bad things
47 function secureString ($str, $strip=true) {
48 // Shall we strip HTML code?
49 if ($strip) $str = strip_tags($str);
55 $str = htmlentities($str, ENT_QUOTES);
59 // Run only once this security check/exchange
60 if (defined('__SECURITY')) return;
62 // Runtime/GPC quoting is off now...
63 @set_magic_quotes_runtime(false);
64 @ini_set('magic_quotes_gpc', false); // This may not work on some systems
66 // Check if important arrays are found and define them if missing
67 if (!isset($_SERVER)) {
69 $_SERVER = $GLOBALS['_SERVER'];
74 $_GET = $GLOBALS['_GET'];
79 $_POST = $GLOBALS['_POST'];
82 // Include IP-Filter here
83 //require("/usr/share/php/ipfilter.php");
85 // Generate arrays which holds the relevante chars to replace
86 $GLOBALS['security_chars'] = array(
87 // The chars we are looking for...
88 'from' => array("{", "}", "/", ".", "'", "$", "(", ")", '{--', '--}', "%", ";", "[", "]", ":", "--"),
89 // ... and we will replace to.
110 // Characters allowed in URLs
112 // Note: Do not replace 'to' with 'from' and vise-versa! When you do this all booked URLs will be
113 // rejected because of the {SLASH}, {DOT} and all below listed items inside the URL.
114 $GLOBALS['url_chars'] = array(
115 // Search for these secured characters
116 'to' => array("{SLASH}", "{DOT}", "{PER}", "{DBL_DOT}", "{COMMENT}"),
117 // Replace with these characters
118 'from' => array("/", ".", "%", ":", "--")
121 // Overworked security part:
122 if (is_array($_GET)) {
123 foreach ($_GET as $seckey => $secvalue) {
124 if (is_array($secvalue)) {
125 // Throw arrays away...
126 unset($_GET[$seckey]);
128 // Only variables are allowed (non-array) but we secure them all!
129 foreach ($GLOBALS['security_chars']['from'] as $key => $char) {
131 $_GET[$seckey] = str_replace($char , $GLOBALS['security_chars']['to'][$key], $_GET[$seckey]);
134 // Strip all other out
135 $_GET[$seckey] = strip_tags($_GET[$seckey]);
140 if (basename($_SERVER['PHP_SELF']) != "install.php") {
142 foreach ($_POST as $seckey => $secvalue) {
143 if (!is_array($secvalue)) {
144 // Only variables are allowed (non-array) to be secured...
145 foreach ($GLOBALS['security_chars']['from'] as $key => $char) {
147 $_POST[$seckey] = str_replace($char , $GLOBALS['security_chars']['to'][$key], $_POST[$seckey]);
150 // Strip all other out
151 $_POST[$seckey] = strip_tags($_POST[$seckey]);
156 // Activate caching or transparent compressing when it is not already done
157 if (!defined('_OB_CACHING')) {
158 if (phpversion() >= '4.0.4pl1' && (strstr(getenv('HTTP_USER_AGENT'),'compatible') || (strstr(getenv('HTTP_USER_AGENT'), "Mozilla")))) {
159 if ((extension_loaded('zlib')) && (function_exists('ob_start'))) {
161 define('_OB_CACHING', "on");
164 // Extension not loaded or required function is missing
165 define('_OB_CACHING', "404");
169 define('_OB_CACHING', "old");
173 // At last secure the $_SERVER['PHP_SELF'] element
174 $_SERVER['PHP_SELF'] = secureString($_SERVER['PHP_SELF']);
176 // Split it up into path and filename
177 $SELF_DIR = dirname($_SERVER['PHP_SELF']);
178 $SELF_FILE = basename($_SERVER['PHP_SELF']);
180 // Check for a .php inside the $SELF_DIR...
181 while (ereg(".php", $SELF_DIR)) {
182 // Correct the dirname
183 $SELF_DIR = substr($SELF_DIR, 0, (strpos($SELF_DIR, ".php") + 4));
184 // Rewrite filename...
185 $SELF_FILE = basename($SELF_DIR);
187 $SELF_DIR = dirname($SELF_DIR);
190 // Put both together again and let's pray it is secured now...
191 $_SERVER['PHP_SELF'] = $SELF_DIR."/".$SELF_FILE;
193 // Remove uneccessary variables
197 // Security system loaded...
198 define('__SECURITY', "1");