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