Deprecated set_magic_quotes_runtime in PHP 5.3.1 rewritren
[mailer.git] / inc / libs / security_functions.php
index ba081bd00fe80d1cd1251977a691b76e3bf38ec8..c57d1e4c6eb35a638e4fdc997c61ad449847e051 100644 (file)
@@ -1,7 +1,7 @@
 <?php
 /************************************************************************
- * MXChange v0.2.1                                    Start: 09/20/2005 *
- * ===============                              Last change: 09/20/2005 *
+ * Mailer v0.2.1-FINAL                                Start: 09/20/2005 *
+ * ===================                          Last change: 09/20/2005 *
  *                                                                      *
  * -------------------------------------------------------------------- *
  * File              : security_functions.php                           *
@@ -18,6 +18,7 @@
  * svn:keywords Date Revision" (autoprobset!) at least!!!!!!            *
  * -------------------------------------------------------------------- *
  * Copyright (c) 2003 - 2009 by Roland Haeder                           *
+ * Copyright (c) 2009, 2010 by Mailer Developer Team                    *
  * For more information visit: http://www.mxchange.org                  *
  *                                                                      *
  * This program is free software; you can redistribute it and/or modify *
  * MA  02110-1301  USA                                                  *
  ************************************************************************/
 
-// Run only once this security check/exchange
+// Run only once this security check/replacement
 if (defined('__SECURITY')) return;
 
 // Some security stuff...
-if (ereg(basename(__FILE__), $_SERVER['PHP_SELF'])) {
+if (strpos($_SERVER['PHP_SELF'], basename(__FILE__)) !== false) {
        die();
 } // END - if
 
@@ -51,45 +52,112 @@ if (ereg(basename(__FILE__), $_SERVER['PHP_SELF'])) {
  * @param      $strip  Strip tags
  * @return     $str    A (hopefully) secured string against XSS and other bad things
  */
-function secureString ($str, $strip=true) {
+function secureString ($str, $strip = true, $encode = false) {
        // Shall we strip HTML code?
        if ($strip === true) $str = strip_tags($str);
 
        // Trim string
        $str = trim($str);
 
-       // Encode in entities
-       $str = htmlentities($str, ENT_QUOTES);
+       // Encode in entities if requested
+       if ($encode === true) {
+               // Encode in entities (this breakes UTF-8!)
+               $str = htmlentities($str, ENT_QUOTES);
+       } // END - if
+
+       // Return result
        return $str;
 }
 
+/**
+ * Secures $_SERVER['PHP_SELF'] against attacks
+ *
+ * @return     void
+ */
+function securePhpSelf () {
+       // Did it run before?
+       if (isset($GLOBALS['php_self_secured'])) {
+               // Please do not call this twice!
+               die('PHP_SELF is already secured. Please do not call ' . __FUNCTION__ . ' for your self.');
+       } // END - if
+
+       // Secure the string
+       $_SERVER['PHP_SELF'] = secureString($_SERVER['PHP_SELF']);
+
+       // Split it up into path and filename
+       $phpSelfDirectory = dirname($_SERVER['PHP_SELF']);
+       $phpSelfFile      = basename($_SERVER['PHP_SELF']);
+
+       // Check for a .php inside the $phpSelfDirectory...
+       while (strpos($phpSelfDirectory, '.php') !== false) {
+               // Correct the dirname
+               $phpSelfDirectory = substr($phpSelfDirectory, 0, (strpos($phpSelfDirectory, '.php') + 4));
+               // Rewrite filename...
+               $phpSelfFile = basename($phpSelfDirectory);
+               // ... and dirname
+               $phpSelfDirectory = dirname($phpSelfDirectory);
+       } // END - while
+
+       // Put both together again and let's pray it is secured now...
+       $_SERVER['PHP_SELF'] = $phpSelfDirectory . '/' . $phpSelfFile;
+
+       // Did run...
+       $GLOBALS['php_self_secured'] = true;
+
+       // Remove uneccessary variables
+       unset($phpSelfDirectory);
+       unset($phpSelfFile);
+}
+
+/**
+ * Detects caching in PHP
+ *
+ * @return     void
+ */
+function detectPhpCaching () {
+       // Activate caching or transparent compressing when it is not already done
+       if (phpversion() >= '4.0.4pl1' && (strstr(getenv('HTTP_USER_AGENT'),'compatible') || (strstr(getenv('HTTP_USER_AGENT'), 'Mozilla')))) {
+               if ((extension_loaded('zlib')) && (function_exists('ob_start'))) {
+                       // Start caching
+                       $GLOBALS['php_caching'] = 'on';
+                       ob_start();
+               } else {
+                       // Extension not loaded or required function is missing
+                       $GLOBALS['php_caching'] = '404';
+               }
+       } else {
+               // Old PHP version
+               $GLOBALS['php_caching'] = 'old';
+       }
+}
+
 // Runtime/GPC quoting is off now...
-set_magic_quotes_runtime(false);
+ini_set('magic_quotes_runtime', false);
 ini_set('magic_quotes_gpc', false); // This may not work on some systems
 
 // Check if important arrays are found and define them if missing
 if (!isset($_SERVER)) {
        global $_SERVER;
        $_SERVER = $GLOBALS['_SERVER'];
-}
+} // END - if
 
 if (!isset($_GET)) {
        global $_GET;
        $_GET = $GLOBALS['_GET'];
-}
+} // END - if
 
 if (!isset($_POST)) {
        global $_POST;
        $_POST = $GLOBALS['_POST'];
-}
+} // END - if
 
 // Include IP-Filter here
-//require("/usr/share/php/ipfilter.php");
+//include("/usr/share/php/ipfilter.php");
 
 // Generate arrays which holds the relevante chars to replace
 $GLOBALS['security_chars'] = array(
        // The chars we are looking for...
-       'from' => array('{', '}', '/', '.', "'", "$", '(', ')', '{--', '--}', '{?', '?}', '%', ';', '[', ']', ':', '--'),
+       'from' => array('{', '}', '/', '.', "'", '$', '(', ')', '{--', '--}', '{?', '?}', '%', ';', '[', ']', ':', '--'),
        // ... and we will replace to.
        'to'   => array(
                '{OPEN_ANCHOR2}',
@@ -143,47 +211,14 @@ if (is_array($_GET)) {
        } // END - foreach
 } // END - if
 
-// Activate caching or transparent compressing when it is not already done
-if (phpversion() >= '4.0.4pl1' && (strstr(getenv('HTTP_USER_AGENT'),'compatible') || (strstr(getenv('HTTP_USER_AGENT'), 'Mozilla')))) {
-       if ((extension_loaded('zlib')) && (function_exists('ob_start'))) {
-               // Start caching
-               $GLOBALS['php_caching'] = 'on';
-               ob_start();
-       } else {
-               // Extension not loaded or required function is missing
-               $GLOBALS['php_caching'] = '404';
-       }
-} else {
-       // Old PHP version
-       $GLOBALS['php_caching'] = 'old';
-}
+// Detect PHP caching
+detectPhpCaching();
 
 // At last secure the $_SERVER['PHP_SELF'] element
-$_SERVER['PHP_SELF'] = secureString($_SERVER['PHP_SELF']);
-
-// Split it up into path and filename
-$phpSelfDirectory = dirname($_SERVER['PHP_SELF']);
-$phpSelfFile      = basename($_SERVER['PHP_SELF']);
-
-// Check for a .php inside the $phpSelfDirectory...
-while (ereg('.php', $phpSelfDirectory)) {
-       // Correct the dirname
-       $phpSelfDirectory = substr($phpSelfDirectory, 0, (strpos($phpSelfDirectory, '.php') + 4));
-       // Rewrite filename...
-       $phpSelfFile = basename($phpSelfDirectory);
-       // ... and dirname
-       $phpSelfDirectory = dirname($phpSelfDirectory);
-} // END - while
-
-// Put both together again and let's pray it is secured now...
-$_SERVER['PHP_SELF'] = $phpSelfDirectory . '/' . $phpSelfFile;
-
-// Remove uneccessary variables
-unset($phpSelfDirectory);
-unset($phpSelfFile);
+securePhpSelf();
 
 // Security system loaded...
-define('__SECURITY', '1');
+define('__SECURITY', 1);
 
 // [EOF]
 ?>