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 * -------------------------------------------------------------------- *
15 * $Tag:: 0.2.1-FINAL $ *
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 - 2009 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 // Run only once this security check/exchange
40 if (defined('__SECURITY')) return;
42 // Some security stuff...
43 if (ereg(basename(__FILE__), $_SERVER['PHP_SELF'])) {
48 * Function to secure input strings
50 * @param $str The unsecured string
51 * @param $strip Strip tags
52 * @return $str A (hopefully) secured string against XSS and other bad things
54 function secureString ($str, $strip=true) {
55 // Shall we strip HTML code?
56 if ($strip === true) $str = strip_tags($str);
62 $str = htmlentities($str, ENT_QUOTES);
66 // Runtime/GPC quoting is off now...
67 set_magic_quotes_runtime(false);
68 ini_set('magic_quotes_gpc', false); // This may not work on some systems
70 // Check if important arrays are found and define them if missing
71 if (!isset($_SERVER)) {
73 $_SERVER = $GLOBALS['_SERVER'];
78 $_GET = $GLOBALS['_GET'];
83 $_POST = $GLOBALS['_POST'];
86 // Include IP-Filter here
87 //require("/usr/share/php/ipfilter.php");
89 // Generate arrays which holds the relevante chars to replace
90 $GLOBALS['security_chars'] = array(
91 // The chars we are looking for...
92 'from' => array('{', '}', '/', '.', "'", "$", '(', ')', '{--', '--}', '{?', '?}', '%', ';', '[', ']', ':', '--'),
93 // ... and we will replace to.
116 // Characters allowed in URLs
118 // Note: Do not replace 'to' with 'from' and vise-versa! When you do this all booked URLs will be
119 // rejected because of the {SLASH}, {DOT} and all below listed items inside the URL.
120 $GLOBALS['url_chars'] = array(
121 // Search for these secured characters
122 'to' => array('{SLASH}', '{DOT}', '{PER}', '{DBL_DOT}', '{COMMENT}'),
123 // Replace with these characters
124 'from' => array('/', '.', '%', ':', '--')
127 // Overworked security part:
128 if (is_array($_GET)) {
129 foreach ($_GET as $seckey => $secvalue) {
130 if (is_array($secvalue)) {
131 // Throw arrays away...
132 unset($_GET[$seckey]);
134 // Only variables are allowed (non-array) but we secure them all!
135 foreach ($GLOBALS['security_chars']['from'] as $key => $char) {
137 $_GET[$seckey] = str_replace($char , $GLOBALS['security_chars']['to'][$key], $_GET[$seckey]);
140 // Strip all other out
141 $_GET[$seckey] = secureString($_GET[$seckey]);
146 // Activate caching or transparent compressing when it is not already done
147 if (phpversion() >= '4.0.4pl1' && (strstr(getenv('HTTP_USER_AGENT'),'compatible') || (strstr(getenv('HTTP_USER_AGENT'), 'Mozilla')))) {
148 if ((extension_loaded('zlib')) && (function_exists('ob_start'))) {
150 $GLOBALS['php_caching'] = 'on';
153 // Extension not loaded or required function is missing
154 $GLOBALS['php_caching'] = '404';
158 $GLOBALS['php_caching'] = 'old';
161 // At last secure the $_SERVER['PHP_SELF'] element
162 $_SERVER['PHP_SELF'] = secureString($_SERVER['PHP_SELF']);
164 // Split it up into path and filename
165 $phpSelfDirectory = dirname($_SERVER['PHP_SELF']);
166 $phpSelfFile = basename($_SERVER['PHP_SELF']);
168 // Check for a .php inside the $phpSelfDirectory...
169 while (ereg('.php', $phpSelfDirectory)) {
170 // Correct the dirname
171 $phpSelfDirectory = substr($phpSelfDirectory, 0, (strpos($phpSelfDirectory, '.php') + 4));
172 // Rewrite filename...
173 $phpSelfFile = basename($phpSelfDirectory);
175 $phpSelfDirectory = dirname($phpSelfDirectory);
178 // Put both together again and let's pray it is secured now...
179 $_SERVER['PHP_SELF'] = $phpSelfDirectory . '/' . $phpSelfFile;
181 // Remove uneccessary variables
182 unset($phpSelfDirectory);
185 // Security system loaded...
186 define('__SECURITY', 1);