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 * $Date:: 2009-03-06 20:24:32 +0100 (Fr, 06. Mär 2009) $ *
15 * $Tag:: 0.2.1-FINAL $ *
16 * $Author:: stelzi $ *
17 * Needs to be in all Files and every File needs "svn propset *
18 * svn:keywords Date Revision" (autoprobset!) at least!!!!!! *
19 * -------------------------------------------------------------------- *
20 * Copyright (c) 2003 - 2008 by Roland Haeder *
21 * For more information visit: http://www.mxchange.org *
23 * This program is free software; you can redistribute it and/or modify *
24 * it under the terms of the GNU General Public License as published by *
25 * the Free Software Foundation; either version 2 of the License, or *
26 * (at your option) any later version. *
28 * This program is distributed in the hope that it will be useful, *
29 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
30 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
31 * GNU General Public License for more details. *
33 * You should have received a copy of the GNU General Public License *
34 * along with this program; if not, write to the Free Software *
35 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, *
37 ************************************************************************/
39 // Some security stuff...
40 if (ereg(basename(__FILE__), $_SERVER['PHP_SELF'])) {
41 $INC = substr(dirname(__FILE__), 0, strpos(dirname(__FILE__), "/inc") + 4) . "/security.php";
46 * Function to secure input strings
48 * @param $str The unsecured string
49 * @param $strip Strip tags
50 * @return $str A (hopefully) secured string against XSS and other bad things
52 function secureString ($str, $strip=true) {
53 // Shall we strip HTML code?
54 if ($strip) $str = strip_tags($str);
60 $str = htmlentities($str, ENT_QUOTES);
64 // Run only once this security check/exchange
65 if (defined('__SECURITY')) return;
67 // Runtime/GPC quoting is off now...
68 @set_magic_quotes_runtime(false);
69 @ini_set('magic_quotes_gpc', false); // This may not work on some systems
71 // Check if important arrays are found and define them if missing
72 if (!isset($_SERVER)) {
74 $_SERVER = $GLOBALS['_SERVER'];
79 $_GET = $GLOBALS['_GET'];
84 $_POST = $GLOBALS['_POST'];
87 // Include IP-Filter here
88 //require("/usr/share/php/ipfilter.php");
90 // Generate arrays which holds the relevante chars to replace
91 $GLOBALS['security_chars'] = array(
92 // The chars we are looking for...
93 'from' => array("{", "}", "/", ".", "'", "$", "(", ")", '{--', '--}', "%", ";", "[", "]", ":", "--"),
94 // ... and we will replace to.
115 // Characters allowed in URLs
117 // Note: Do not replace 'to' with 'from' and vise-versa! When you do this all booked URLs will be
118 // rejected because of the {SLASH}, {DOT} and all below listed items inside the URL.
119 $GLOBALS['url_chars'] = array(
120 // Search for these secured characters
121 'to' => array("{SLASH}", "{DOT}", "{PER}", "{DBL_DOT}", "{COMMENT}"),
122 // Replace with these characters
123 'from' => array("/", ".", "%", ":", "--")
126 // Overworked security part:
127 if (is_array($_GET)) {
128 foreach ($_GET as $seckey => $secvalue) {
129 if (is_array($secvalue)) {
130 // Throw arrays away...
131 unset($_GET[$seckey]);
133 // Only variables are allowed (non-array) but we secure them all!
134 foreach ($GLOBALS['security_chars']['from'] as $key => $char) {
136 $_GET[$seckey] = str_replace($char , $GLOBALS['security_chars']['to'][$key], $_GET[$seckey]);
139 // Strip all other out
140 $_GET[$seckey] = strip_tags($_GET[$seckey]);
145 if (basename($_SERVER['PHP_SELF']) != "install.php") {
147 foreach ($_POST as $seckey => $secvalue) {
148 if (!is_array($secvalue)) {
149 // Only variables are allowed (non-array) to be secured...
150 foreach ($GLOBALS['security_chars']['from'] as $key => $char) {
152 $_POST[$seckey] = str_replace($char , $GLOBALS['security_chars']['to'][$key], $_POST[$seckey]);
155 // Strip all other out
156 $_POST[$seckey] = strip_tags($_POST[$seckey]);
161 // Activate caching or transparent compressing when it is not already done
162 if (!defined('_OB_CACHING')) {
163 if (phpversion() >= '4.0.4pl1' && (strstr(getenv('HTTP_USER_AGENT'),'compatible') || (strstr(getenv('HTTP_USER_AGENT'), "Mozilla")))) {
164 if ((extension_loaded('zlib')) && (function_exists('ob_start'))) {
166 define('_OB_CACHING', "on");
169 // Extension not loaded or required function is missing
170 define('_OB_CACHING', "404");
174 define('_OB_CACHING', "old");
178 // At last secure the $_SERVER['PHP_SELF'] element
179 $_SERVER['PHP_SELF'] = secureString($_SERVER['PHP_SELF']);
181 // Split it up into path and filename
182 $SELF_DIR = dirname($_SERVER['PHP_SELF']);
183 $SELF_FILE = basename($_SERVER['PHP_SELF']);
185 // Check for a .php inside the $SELF_DIR...
186 while (ereg(".php", $SELF_DIR)) {
187 // Correct the dirname
188 $SELF_DIR = substr($SELF_DIR, 0, (strpos($SELF_DIR, ".php") + 4));
189 // Rewrite filename...
190 $SELF_FILE = basename($SELF_DIR);
192 $SELF_DIR = dirname($SELF_DIR);
195 // Put both together again and let's pray it is secured now...
196 $_SERVER['PHP_SELF'] = $SELF_DIR."/".$SELF_FILE;
198 // Remove uneccessary variables
202 // Security system loaded...
203 define('__SECURITY', "1");