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 * @return $str A (hopefully) secured string against XSS and other bad things
46 function secureString ($str) {
47 $str = trim(strip_tags($str));
48 $str = htmlentities($str, ENT_QUOTES);
52 // Run only once this security check/exchange
53 if (defined('__SECURITY')) return;
55 // Fatal messages goes here
59 // Runtime/GPC quoting is off now...
60 @set_magic_quotes_runtime(false);
61 @ini_set('magic_quotes_gpc', false); // This may not work on some systems
63 // Check if important arrays are found and define them if missing
64 if (!isset($_SERVER)) {
66 $_SERVER = $GLOBALS['_SERVER'];
71 $_GET = $GLOBALS['_GET'];
76 $_POST = $GLOBALS['_POST'];
79 if (!isset($_COOKIE)) {
81 $_COOKIE = $GLOBALS['_COOKIE'];
84 // Include IP-Filter here
85 //require("/usr/share/php/ipfilter.php");
87 // Generate arrays which holds the relevante chars to replace
88 global $SEC_CHARS, $URL_CHARS;
90 // The chars we are looking for...
91 'from' => array("{", "}", "/", ".", "'", "$", "(", ")", '{--', '--}', "%", ";", "[", "]", ":", "--"),
92 // ... and we will replace to.
113 // Characters allowed in URLs
115 // Note: Do not replace 'to' with 'from' and vise-versa! When you do this all booked URLs will be
116 // rejected because of the {SLASH}, {DOT} and all below listed items inside the URL.
118 // Search for these secured characters
119 'to' => array("{SLASH}", "{DOT}", "{PER}", "{DBL_DOT}", "{COMMENT}"),
120 // Replace with these characters
121 'from' => array("/", ".", "%", ":", "--")
124 // Overworked security part:
125 if (is_array($_GET)) {
126 foreach ($_GET as $seckey => $secvalue) {
127 if (is_array($secvalue)) {
128 // Throw arrays away...
129 unset($_GET[$seckey]);
131 // Only variables are allowed (non-array) but we secure them all!
132 foreach ($SEC_CHARS['from'] as $key => $char) {
134 $_GET[$seckey] = str_replace($char , $SEC_CHARS['to'][$key], $_GET[$seckey]);
137 // Strip all other out
138 $_GET[$seckey] = strip_tags($_GET[$seckey]);
143 if (basename($_SERVER['PHP_SELF']) != "install.php") {
145 foreach ($_POST as $seckey => $secvalue) {
146 if (!is_array($secvalue)) {
147 // Only variables are allowed (non-array) to be secured...
148 foreach ($SEC_CHARS['from'] as $key => $char) {
150 $_POST[$seckey] = str_replace($char , $SEC_CHARS['to'][$key], $_POST[$seckey]);
153 // Strip all other out
154 $_POST[$seckey] = strip_tags($_POST[$seckey]);
158 // ... and finally cookies
159 foreach ($_COOKIE as $seckey => $secvalue) {
160 if (is_array($secvalue)) {
161 // Throw arrays away...
162 unset($_COOKIE[$seckey]);
164 // Only variables are allowed (non-array) but we secure them all!
165 foreach ($SEC_CHARS['from'] as $key => $char) {
167 $_COOKIE[$seckey] = str_replace($char , $SEC_CHARS['to'][$key], $_COOKIE[$seckey]);
170 // Strip all other out
171 $_COOKIE[$seckey] = strip_tags($_COOKIE[$seckey]);
176 // Activate caching or transparent compressing when it is not already done
177 if (!defined('_OB_CACHING')) {
178 if (phpversion() >= '4.0.4pl1' && (strstr(getenv('HTTP_USER_AGENT'),'compatible') || (strstr(getenv('HTTP_USER_AGENT'), "Mozilla")))) {
179 if ((extension_loaded('zlib')) && (function_exists('ob_start'))) {
181 define('_OB_CACHING', "on");
184 // Extension not loaded or required function is missing
185 define('_OB_CACHING', "404");
189 define('_OB_CACHING', "old");
193 // At last secure the $_SERVER['PHP_SELF'] element
194 $_SERVER['PHP_SELF'] = secureString($_SERVER['PHP_SELF']);
196 // Split it up into path and filename
197 $SELF_DIR = dirname($_SERVER['PHP_SELF']);
198 $SELF_FILE = basename($_SERVER['PHP_SELF']);
200 // Check for a .php inside the $SELF_DIR...
201 while (ereg(".php", $SELF_DIR)) {
202 // Correct the dirname
203 $SELF_DIR = substr($SELF_DIR, 0, (strpos($SELF_DIR, ".php") + 4));
204 // Rewrite filename...
205 $SELF_FILE = basename($SELF_DIR);
207 $SELF_DIR = dirname($SELF_DIR);
210 // Put both together again and let's pray it is secured now...
211 $_SERVER['PHP_SELF'] = $SELF_DIR."/".$SELF_FILE;
213 // Remove uneccessary variables
217 // Security system loaded...
218 define('__SECURITY', "1");