2 /************************************************************************
3 * Mailer v0.2.1-FINAL 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 * -------------------------------------------------------------------- *
18 * Copyright (c) 2003 - 2009 by Roland Haeder *
19 * Copyright (c) 2009 - 2011 by Mailer Developer Team *
20 * For more information visit: http://mxchange.org *
22 * This program is free software; you can redistribute it and/or modify *
23 * it under the terms of the GNU General Public License as published by *
24 * the Free Software Foundation; either version 2 of the License, or *
25 * (at your option) any later version. *
27 * This program is distributed in the hope that it will be useful, *
28 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
29 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
30 * GNU General Public License for more details. *
32 * You should have received a copy of the GNU General Public License *
33 * along with this program; if not, write to the Free Software *
34 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, *
36 ************************************************************************/
38 // Run only once this security check/replacement
39 if (defined('__SECURITY')) {
43 // Some security stuff...
44 if (strpos($_SERVER['PHP_SELF'], basename(__FILE__)) !== false) {
48 // Include ctracker, recommended place!
49 //require_once('ctracker.php');
52 * Function to secure input strings
54 * @param $str The unsecured string
55 * @param $stripTags Strip tags
56 * @return $str A (hopefully) secured string against XSS and other bad things
58 function secureString ($str, $stripTags = true, $encode = false) {
59 // Shall we strip HTML code?
60 if ($stripTags === true) {
61 $str = strip_tags($str);
67 // Encode in entities if requested
68 if ($encode === true) {
69 // Encode in entities (this breakes UTF-8!)
70 $str = htmlentities($str, ENT_QUOTES);
73 // Replace {,} with entities
74 $str = str_replace(array('{', '}'), array('{', '}'), $str);
81 * Secures $_SERVER['PHP_SELF'] against attacks
85 function securePhpSelf () {
87 if (isset($GLOBALS['php_self_secured'])) {
88 // Please do not call this twice!
89 die('PHP_SELF is already secured. Please do not call ' . __FUNCTION__ . ' for your self.');
93 $_SERVER['PHP_SELF'] = secureString($_SERVER['PHP_SELF']);
95 // Split it up into path and filename
96 $phpSelfDirectory = dirname($_SERVER['PHP_SELF']);
97 $phpSelfFile = basename($_SERVER['PHP_SELF']);
99 // Check for a .php inside the $phpSelfDirectory...
100 while (strpos($phpSelfDirectory, '.php') !== false) {
101 // Correct the dirname
102 $phpSelfDirectory = substr($phpSelfDirectory, 0, (strpos($phpSelfDirectory, '.php') + 4));
103 // Rewrite filename...
104 $phpSelfFile = basename($phpSelfDirectory);
106 $phpSelfDirectory = dirname($phpSelfDirectory);
109 // Put both together again and let's pray it is secured now...
110 $_SERVER['PHP_SELF'] = $phpSelfDirectory . '/' . $phpSelfFile;
113 $GLOBALS['php_self_secured'] = true;
115 // Remove uneccessary variables
116 unset($phpSelfDirectory);
121 * Checks if PHP_VERSION is newer or equal to given version string
123 * @param $versionString A PHP'ized version string which shall be compared with PHP_VERSION
124 * @return $isEqualNewer Wether the given version string is equal or newer to PHP_VERSION
126 function isPhpVersionEqualNewer ($versionString) {
127 return (version_compare(PHP_VERSION, $versionString, '>='));
131 * Detects caching in PHP
135 function detectPhpCaching () {
136 // Activate caching or transparent compressing when it is not already done
137 if ((isPhpVersionEqualNewer('4.0.4pl1')) && (strstr(getenv('HTTP_USER_AGENT'),'compatible') || (strstr(getenv('HTTP_USER_AGENT'), 'Mozilla')))) {
138 if ((extension_loaded('zlib')) && (function_exists('ob_start'))) {
140 $GLOBALS['php_caching'] = 'on';
143 // Extension not loaded or required function is missing
144 $GLOBALS['php_caching'] = '404';
148 $GLOBALS['php_caching'] = 'old';
152 // Runtime/GPC quoting is off now...
153 ini_set('magic_quotes_runtime', false);
154 ini_set('magic_quotes_gpc', false); // This may not work on some systems
157 * No compatibility with Zend Engine 1, else an error like 'Implicit cloning'
160 if (isPhpVersionEqualNewer('5.0')) {
161 ini_set('zend.ze1_compatibility_mode', 'Off');
164 // Check if important arrays are found and define them if missing
165 if (!isset($_SERVER)) {
167 $_SERVER = $GLOBALS['_SERVER'];
172 $_GET = $GLOBALS['_GET'];
175 if (!isset($_POST)) {
177 $_POST = $GLOBALS['_POST'];
180 // Generate arrays which holds the relevante chars to replace
181 $GLOBALS['security_chars'] = array(
182 // The chars we are looking for...
183 'from' => array('/', '.', chr(39), '$', '(', ')', '{--', '--}', '{?', '?}', '%', ';', '[', ']', ':', '--', chr(92)),
184 // ... and we will replace to.
207 * Characters allowed in URLs
209 * Note: Do not replace 'to' with 'from' and vise-versa! When you do this all booked URLs will be
210 * rejected because of the {SLASH}, {DOT} and all below listed items inside the URL.
212 $GLOBALS['url_chars'] = array(
213 // Search for these secured characters
214 'to' => array('{SLASH}', '{DOT}', '{PER}', '{DBL_DOT}', '{COMMENT}'),
215 // Replace with these characters
216 'from' => array('/', '.', '%', ':', '--')
219 // Overworked security part:
220 if (is_array($_GET)) {
221 foreach ($_GET as $seckey => $secvalue) {
222 if (is_array($secvalue)) {
223 // Throw arrays away...
224 unset($_GET[$seckey]);
226 // Only variables are allowed (non-array) but we secure them all!
227 $_GET[$seckey] = str_replace($GLOBALS['security_chars']['from'], $GLOBALS['security_chars']['to'], $_GET[$seckey]);
229 // Strip all other out
230 $_GET[$seckey] = secureString($_GET[$seckey]);
235 // Secure also $_POST data (only simple, no replace)
236 if (is_array($_POST)) {
237 // Secure only simple data
238 foreach ($_POST as $seckey => $secvalue) {
240 if (!is_array($secvalue)) {
241 // Strip all other out
242 $_POST[$seckey] = secureString($_POST[$seckey]);
247 // Detect PHP caching
250 // At last secure the $_SERVER['PHP_SELF'] element
253 // Security system loaded...
254 define('__SECURITY', 1);