3 * General functions library
5 * @author Roland Haeder <webmaster@ship-simu.org>
7 * @copyright Copyright (c) 2009 - 2011 Cracker Tracker Team
8 * @license GNU GPL 3.0 or any newer version
9 * @link http://www.ship-simu.org
11 * This program is free software: you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation, either version 3 of the License, or
14 * (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program. If not, see <http://www.gnu.org/licenses/>.
25 if (!function_exists('implode_r')) {
26 // Implode recursive a multi-dimension array, taken from www.php.net
27 function implode_r ($glue, $array, $array_name = NULL) {
29 while(list($key,$value) = @each($array)) {
30 if(is_array($value)) {
31 // Is an array again, so call recursive
32 $return[] = implode_r($glue, $value, (string) $key);
34 if($array_name != NULL) {
35 $return[] = $array_name . '[' . (string) $key . ']=' . $value . "\n";
37 $return[] = $key . '=' . $value."\n";
42 // Return resulting array
43 return(implode($glue, $return));
47 if (!function_exists('implode_secure')) {
48 // Implode a simple array with a 'call-back' to our escaper function
49 function implode_secure ($array) {
54 foreach ($array as $entry) {
56 if (in_array($entry, array('NOW()'))) {
57 // Add it with non-string glue
58 $return .= $entry . ',';
59 } elseif (empty($entry)) {
60 // Empty strings need no escaping
63 // Secure this string and add it
64 $return .= '"' . crackerTrackerEscapeString($entry) . '",';
69 $return = substr($return, 0, -1);
76 // Getter for ctracker_debug_enabled
77 function isCrackerTrackerDebug () {
79 $result = ((isset($GLOBALS['ctracker_debug_enabled'])) && ($GLOBALS['ctracker_debug_enabled'] === TRUE));
82 //* DEBUG: */ error_log('result=' . intval($result));
88 // Determines the real remote address
89 function determineCrackerTrackerRealRemoteAddress () {
94 if (isset($_SERVER['HTTP_X_FORWARDED_FOR'])) {
96 $address = trim($_SERVER['HTTP_X_FORWARDED_FOR']);
97 } elseif (isset($_SERVER['HTTP_CLIENT_IP'])) {
99 $address = trim($_SERVER['HTTP_CLIENT_IP']);
100 } elseif (isset($_SERVER['REMOTE_ADDR'])) {
101 // The regular address when no proxy was used
102 $address = trim(getenv('REMOTE_ADDR'));
105 if ($address == 'unknown') {
106 // Invalid IP somehow given
107 $address = '0.0.0.0';
108 } elseif (strstr($address, ',')) {
109 // This strips out the real address from proxy output
110 $addressArray = explode(',', $address);
111 $address = $addressArray[0];
118 // Determine if a proxy was used
119 function isCrackerTrackerProxyUsed () {
120 // Check if specific entries are set
121 $proxyUsed = ((isset($_SERVER['HTTP_X_FORWARDED_FOR'])) || (isset($_SERVER['HTTP_CLIENT_IP'])));
127 // Detects the user-agent string
128 function crackerTrackerUserAgent () {
129 // Default is 'unknown'
132 // Is the entry there?
133 if (isset($_SERVER['HTTP_USER_AGENT'])) {
134 // Then use it securely
135 $ua = crackerTrackerSecureString($_SERVER['HTTP_USER_AGENT']);
142 // Detects the script name
143 function crackerTrackerScriptName () {
145 if (!isset($_SERVER['SCRIPT_NAME'])) {
150 // Should always be there!
151 return crackerTrackerSecureString($_SERVER['SCRIPT_NAME']);
154 // Detects the query string
155 function crackerTrackerQueryString () {
157 if (!isset($_SERVER['QUERY_STRING'])) {
162 // Should always be there!
163 return crackerTrackerEscapeString(urldecode($_SERVER['QUERY_STRING']));
166 // Detects the server's name
167 function crackerTrackerServerName () {
169 if (!isset($_SERVER['SERVER_NAME'])) {
174 // Should always be there!
175 return crackerTrackerSecureString($_SERVER['SERVER_NAME']);
178 // Detects the referer
179 function crackerTrackerReferer () {
184 if (isset($_SERVER['HTTP_REFERER'])) {
185 // Then use it securely
186 $referer = crackerTrackerSecureString(urldecode($_SERVER['HTTP_REFERER']));
193 // Detects the scripts path
194 function crackerTrackerScriptPath () {
195 // Should always be there!
196 $path = dirname(crackerTrackerScriptName()) . '/';
198 // Return detected path
202 // Detects wether we have SSL
203 function crackerTrackerSecured () {
205 $ssl = ((isset($_SERVER['HTTPS'])) && ($_SERVER['HTTPS'] != 'off'));
211 // Secures a string by escaping it and passing it through strip_tags,htmlentities-chain
212 function crackerTrackerSecureString ($str) {
214 $str = crackerTrackerEscapeString($str);
216 // Then pass it through the functions
217 $str = htmlentities(strip_tags($str), ENT_QUOTES);
223 // Is the file there and readable?
224 function isCrackerTrackerFileFound ($FQFN) {
226 return ((file_exists($FQFN)) && (is_readable($FQFN)));
229 // Loads a given "template" (this is more an include file)
230 function crackerTrackerLoadTemplate ($template) {
231 // Create the full-qualified filename (FQFN)
232 $FQFN = sprintf('%s/templates/%s.tpl.php',
237 // Is this template found?
238 if (isCrackerTrackerFileFound($FQFN)) {
240 crackerTrackerLanguage();
245 // Not found, so die here
250 // Loads a given "template" (this is more an include file)
251 function crackerTrackerLoadLocalizedTemplate ($template) {
252 // Create the full-qualified filename (FQFN)
253 $FQFN = sprintf('%s/templates/%s/%s.tpl.php',
255 getCrackerTrackerLanguage(),
259 // Is this template found?
260 if (isCrackerTrackerFileFound($FQFN)) {
264 // Not found, so die here
269 // Detects the browser's language file and tries to load it, fall-back on english!
270 function crackerTrackerLanguage () {
271 // Default is English
272 $GLOBALS['ctracker_language'] = 'en';
275 // Is the language string there?
276 if (isset($_SERVER['HTTP_ACCEPT_LANGUAGE'])) {
278 foreach (explode(',', $_SERVER['HTTP_ACCEPT_LANGUAGE']) as $lang) {
279 // Split it again for q=x.x value
280 $langArray = explode(';', $lang);
282 // If there is an entry, we have a weight
283 if ((isset($langArray[1])) && ($langArray[1] > $weight)) {
284 // Use this language/weight instead
285 $GLOBALS['ctracker_language'] = $langArray[0];
286 $weight = $langArray[1];
292 $FQFN = sprintf('%s/language/%s.php',
294 getCrackerTrackerLanguage()
297 // Is it not there, switch to "en"
298 if ((getCrackerTrackerLanguage() != 'en') && (!isCrackerTrackerFileFound($FQFN))) {
299 // English is default!
300 $GLOBALS['ctracker_language'] = 'en';
302 // Construct FQFN again
303 $FQFN = sprintf('%s/language/en.php', dirname(__FILE__));
306 // Load the language file
310 // Loads a given email template and passes through $content
311 function crackerTrackerLoadEmailTemplate ($template, array $content = array(), $language = NULL) {
313 crackerTrackerLanguage();
316 $FQFN = sprintf('%s/mails/%s/%s.tpl',
318 getCrackerTrackerLanguage($language),
322 // So is the file there?
323 if (isCrackerTrackerFileFound($FQFN)) {
325 $result = 'No result from template ' . $template . '. Please report this at http://forum.ship-simu.org Thank you.';
328 eval('$result = "' . crackerTrackerCompileCode(file_get_contents($FQFN)) . '";');
338 // Getter for message
339 function getCrackerTrackerLocalized ($message) {
341 $output = '!' . $message . '!';
343 // Is the language string there?
344 if (isset($GLOBALS['ctracker_localized'][$message])) {
346 $output = $GLOBALS['ctracker_localized'][$message];
353 // Tries to find a message and outputs it
354 function crackerTrackerOutputLocalized ($message) {
356 print getCrackerTrackerLocalized($message);
359 // Compiles the given code
360 function crackerTrackerCompileCode ($code) {
361 // Find all $content[foo]
362 preg_match_all('/\$(content|GLOBALS)((\[([a-zA-Z0-9-_]+)\])*)/', $code, $matches);
364 // Replace " with {QUOTE}
365 $code = str_replace('"', '{QUOTE}', $code);
368 foreach ($matches[0] as $key=>$match) {
370 if (substr($match, 0, 8) == '$GLOBALS') {
372 $code = str_replace($match, "\" . \$GLOBALS['" . $matches[4][$key] . "'] . \"", $code);
373 } elseif (substr($match, 0, 8) == '$content') {
375 $code = str_replace($match, "\" . \$content['" . $matches[4][$key] . "'] . \"", $code);
383 // "Getter" for language
384 function getCrackerTrackerLanguage ($lang = NULL) {
385 // Default is from browser
386 $language = $GLOBALS['ctracker_language'];
389 if (!is_null($lang)) {
390 // Then use this instead
398 // "Getter" for ticket id
399 function getCrackerTrackerTicketId () {
404 if (isset($GLOBALS['ctracker_last_ticket']['ctracker_ticket'])) {
406 $id = $GLOBALS['ctracker_last_ticket']['ctracker_ticket'];
413 // Sends a cookie to the user that he would not see this security warning again
414 function sendCrackerTrackerCookie () {
416 // @TODO Why can't domain be set to value from crackerTrackerServerName() ?
417 setcookie('ctracker_ticket', getCrackerTrackerTicketId(), (time() + 60*60*24), '/', '', crackerTrackerSecured(), TRUE);
418 $_COOKIE['ctracker_ticket'] = getCrackerTrackerTicketId();
421 // Is the cookie set?
422 function ifCrackerTrackerCookieIsSet () {
423 // Is it set and valid?
424 return ((isset($_COOKIE['ctracker_ticket'])) && ($_COOKIE['ctracker_ticket'] > 0));
427 // Redirects to the same URL
428 function crackerTrackerRedirectSameUrl () {
430 $url = '://' . crackerTrackerServerName() . crackerTrackerScriptName() . '?' . crackerTrackerQueryString();
433 if (crackerTrackerSecured()) {
435 $url = 'https' . $url;
438 $url = 'http' . $url;
442 crackerTrackerSendRawRedirect($url);
446 * Send a HTTP redirect to the browser. This function was taken from DokuWiki
447 * (GNU GPL 2; http://www.dokuwiki.org) and modified to fit into this script.
449 * Works arround Microsoft IIS cookie sending bug. Does exit the script.
451 * @link http://support.microsoft.com/kb/q176113/
452 * @author Andreas Gohr <andi@splitbrain.org>
455 function crackerTrackerSendRawRedirect ($url) {
456 // Better remove any data by ctracker
459 // always close the session
460 session_write_close();
462 // Revert entity &
463 $url = str_replace('&', '&', $url);
465 // check if running on IIS < 6 with CGI-PHP
466 if ((isset($_SERVER['SERVER_SOFTWARE'])) && (isset($_SERVER['GATEWAY_INTERFACE'])) &&
467 (strpos($_SERVER['GATEWAY_INTERFACE'],'CGI') !== FALSE) &&
468 (preg_match('|^Microsoft-IIS/(\d)\.\d$|', trim($_SERVER['SERVER_SOFTWARE']), $matches)) &&
470 // Send the IIS header
471 header('Refresh: 0;url=' . $url);
473 // Send generic header
474 header('Location: ' . $url);
479 // Removes all ctracker-related data from global space
480 function unsetCtrackerData () {
482 //* DEBUG: */ error_log(__FUNCTION__ . ': CALLED!');
484 // Unset all ctracker data
490 'ctracker_debug_enabled',
492 'ctracker_whitelist',
493 'ctracker_get_blacklist',
494 'ctracker_post_blacklist',
496 'ctracker_post_track',
497 'ctracker_checkworm',
498 'ctracker_check_post',
500 'ctracker_last_result',
504 'ctracker_localized',
508 unset($GLOBALS[$key]);