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