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