1f08065de4695decd07ae7f2413e15ded8c5bbf0
[mailer.git] / inc / libs / security_functions.php
1 <?php
2 /************************************************************************
3  * MXChange v0.2.1                                    Start: 09/20/2005 *
4  * ===============                              Last change: 09/20/2005 *
5  *                                                                      *
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  * -------------------------------------------------------------------- *
13  *                                                                      *
14  * -------------------------------------------------------------------- *
15  * Copyright (c) 2003 - 2008 by Roland Haeder                           *
16  * For more information visit: http://www.mxchange.org                  *
17  *                                                                      *
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.                                  *
22  *                                                                      *
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.                         *
27  *                                                                      *
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,               *
31  * MA  02110-1301  USA                                                  *
32  ************************************************************************/
33
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";
37         require($INC);
38 }
39
40 /**
41  * Function to secure input strings
42  *
43  * @param       $str    The unsecured string
44  * @param       $strip  Strip tags
45  * @return      $str    A (hopefully) secured string against XSS and other bad things
46  */
47 function secureString ($str, $strip=true) {
48         // Shall we strip HTML code?
49         if ($strip) $str = strip_tags($str);
50
51         // Trim string
52         $str = trim($str);
53
54         // Encode in entities
55         $str = htmlentities($str, ENT_QUOTES);
56         return $str;
57 }
58
59 // Run only once this security check/exchange
60 if (defined('__SECURITY')) return;
61
62 // Runtime/GPC quoting is off now...
63 @set_magic_quotes_runtime(false);
64 @ini_set('magic_quotes_gpc', false); // This may not work on some systems
65
66 // Check if important arrays are found and define them if missing
67 if (!isset($_SERVER)) {
68         global $_SERVER;
69         $_SERVER = $GLOBALS['_SERVER'];
70 }
71
72 if (!isset($_GET)) {
73         global $_GET;
74         $_GET = $GLOBALS['_GET'];
75 }
76
77 if (!isset($_POST)) {
78         global $_POST;
79         $_POST = $GLOBALS['_POST'];
80 }
81
82 // Include IP-Filter here
83 //require("/usr/share/php/ipfilter.php");
84
85 // Generate arrays which holds the relevante chars to replace
86 global $SEC_CHARS, $URL_CHARS;
87 $SEC_CHARS = array(
88         // The chars we are looking for...
89         'from' => array("{", "}", "/", ".", "'", "$", "(", ")", '{--', '--}', "%", ";", "[", "]", ":", "--"),
90         // ... and we will replace to.
91         'to'   => array(
92                 "{OPEN_ANCHOR2}",
93                 "{CLOSE_ANCHOR2}",
94                 "{SLASH}",
95                 "{DOT}",
96                 "{QUOT}",
97                 "{DOLLAR}",
98                 "{OPEN_ANCHOR}",
99                 "{CLOSE_ANCHOR}",
100                 "{OPEN_TEMPLATE}",
101                 "{CLOSE_TEMPLATE}",
102                 "{PER}",
103                 "{SEMI}",
104                 "{OPEN_INDEX}",
105                 "{CLOSE_INDEX}",
106                 "{DBL_DOT}",
107                 "{COMMENT}"
108         ),
109 );
110
111 // Characters allowed in URLs
112 //
113 // Note: Do not replace 'to' with 'from' and vise-versa! When you do this all booked URLs will be
114 //       rejected because of the {SLASH}, {DOT} and all below listed items inside the URL.
115 $URL_CHARS = array(
116         // Search for these secured characters
117         'to'   => array("{SLASH}", "{DOT}", "{PER}", "{DBL_DOT}", "{COMMENT}"),
118         // Replace with these characters
119         'from' => array("/", ".", "%", ":", "--")
120 );
121
122 // Overworked security part:
123 if (is_array($_GET)) {
124         foreach ($_GET as $seckey => $secvalue) {
125                 if (is_array($secvalue)) {
126                         // Throw arrays away...
127                         unset($_GET[$seckey]);
128                 } else {
129                         // Only variables are allowed (non-array) but we secure them all!
130                         foreach ($SEC_CHARS['from'] as $key => $char) {
131                                 // Pass all through
132                                 $_GET[$seckey] = str_replace($char  , $SEC_CHARS['to'][$key], $_GET[$seckey]);
133                         }
134
135                         // Strip all other out
136                         $_GET[$seckey] = strip_tags($_GET[$seckey]);
137                 }
138         }
139 }
140
141 if (basename($_SERVER['PHP_SELF']) != "install.php") {
142         // And POST data
143         foreach ($_POST as $seckey => $secvalue) {
144                 if (!is_array($secvalue)) {
145                         // Only variables are allowed (non-array) to be secured...
146                         foreach ($SEC_CHARS['from'] as $key => $char) {
147                                 // Pass all through
148                                 $_POST[$seckey] = str_replace($char  , $SEC_CHARS['to'][$key], $_POST[$seckey]);
149                         }
150
151                         // Strip all other out
152                         $_POST[$seckey] = strip_tags($_POST[$seckey]);
153                 }
154         }
155 }
156
157 // Activate caching or transparent compressing when it is not already done
158 if (!defined('_OB_CACHING')) {
159         if (phpversion() >= '4.0.4pl1' && (strstr(getenv('HTTP_USER_AGENT'),'compatible') || (strstr(getenv('HTTP_USER_AGENT'), "Mozilla")))) {
160                 if ((extension_loaded('zlib')) && (function_exists('ob_start'))) {
161                         // Start caching
162                         define('_OB_CACHING', "on");
163                         ob_start();
164                 } else {
165                         // Extension not loaded or required function is missing
166                         define('_OB_CACHING', "404");
167                 }
168         } else {
169                 // Old PHP version
170                 define('_OB_CACHING', "old");
171         }
172 }
173
174 // At last secure the $_SERVER['PHP_SELF'] element
175 $_SERVER['PHP_SELF'] = secureString($_SERVER['PHP_SELF']);
176
177 // Split it up into path and filename
178 $SELF_DIR  = dirname($_SERVER['PHP_SELF']);
179 $SELF_FILE = basename($_SERVER['PHP_SELF']);
180
181 // Check for a .php inside the $SELF_DIR...
182 while (ereg(".php", $SELF_DIR)) {
183         // Correct the dirname
184         $SELF_DIR = substr($SELF_DIR, 0, (strpos($SELF_DIR, ".php") + 4));
185         // Rewrite filename...
186         $SELF_FILE = basename($SELF_DIR);
187         // ... and dirname
188         $SELF_DIR = dirname($SELF_DIR);
189 }
190
191 // Put both together again and let's pray it is secured now...
192 $_SERVER['PHP_SELF'] = $SELF_DIR."/".$SELF_FILE;
193
194 // Remove uneccessary variables
195 unset($SELF_DIR);
196 unset($SELF_FILE);
197
198 // Security system loaded...
199 define('__SECURITY', "1");
200
201 //
202 ?>