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 return ((isset($GLOBALS['ctracker_debug_enabled'])) && ($GLOBALS['ctracker_debug_enabled'] === TRUE));
82 // Determines the real remote address
83 function determineCrackerTrackerRealRemoteAddress () {
88 if (isset($_SERVER['HTTP_X_FORWARDED_FOR'])) {
90 $address = trim($_SERVER['HTTP_X_FORWARDED_FOR']);
91 } elseif (isset($_SERVER['HTTP_CLIENT_IP'])) {
93 $address = trim($_SERVER['HTTP_CLIENT_IP']);
94 } elseif (isset($_SERVER['REMOTE_ADDR'])) {
95 // The regular address when no proxy was used
96 $address = trim(getenv('REMOTE_ADDR'));
99 if ($address == 'unknown') {
100 // Invalid IP somehow given
101 $address = '0.0.0.0';
102 } elseif (strstr($address, ',')) {
103 // This strips out the real address from proxy output
104 $addressArray = explode(',', $address);
105 $address = $addressArray[0];
112 // Determine if a proxy was used
113 function isCrackerTrackerProxyUsed () {
114 // Check if specific entries are set
115 $proxyUsed = ((isset($_SERVER['HTTP_X_FORWARDED_FOR'])) || (isset($_SERVER['HTTP_CLIENT_IP'])));
121 // Detects the user-agent string
122 function crackerTrackerUserAgent () {
123 // Default is 'unknown'
126 // Is the entry there?
127 if (isset($_SERVER['HTTP_USER_AGENT'])) {
128 // Then use it securely
129 $ua = crackerTrackerSecureString($_SERVER['HTTP_USER_AGENT']);
136 // Detects the script name
137 function crackerTrackerScriptName () {
139 if (!isset($_SERVER['SCRIPT_NAME'])) {
144 // Should always be there!
145 return crackerTrackerSecureString($_SERVER['SCRIPT_NAME']);
148 // Detects the query string
149 function crackerTrackerQueryString () {
151 if (!isset($_SERVER['QUERY_STRING'])) {
156 // Should always be there!
157 return crackerTrackerEscapeString(urldecode($_SERVER['QUERY_STRING']));
160 // Detects the server's name
161 function crackerTrackerServerName () {
163 if (!isset($_SERVER['SERVER_NAME'])) {
168 // Should always be there!
169 return crackerTrackerSecureString($_SERVER['SERVER_NAME']);
172 // Detects the referer
173 function crackerTrackerReferer () {
178 if (isset($_SERVER['HTTP_REFERER'])) {
179 // Then use it securely
180 $referer = crackerTrackerSecureString(urldecode($_SERVER['HTTP_REFERER']));
187 // Detects the scripts path
188 function crackerTrackerScriptPath () {
189 // Should always be there!
190 $path = dirname(crackerTrackerScriptName()) . '/';
192 // Return detected path
196 // Detects wether we have SSL
197 function crackerTrackerSecured () {
199 $ssl = ((isset($_SERVER['HTTPS'])) && ($_SERVER['HTTPS'] != 'off'));
205 // Secures a string by escaping it and passing it through strip_tags,htmlentities-chain
206 function crackerTrackerSecureString ($str) {
208 $str = crackerTrackerEscapeString($str);
210 // Then pass it through the functions
211 $str = htmlentities(strip_tags($str), ENT_QUOTES);
217 // Is the file there and readable?
218 function isCrackerTrackerFileFound ($FQFN) {
220 return ((file_exists($FQFN)) && (is_readable($FQFN)));
223 // Loads a given "template" (this is more an include file)
224 function crackerTrackerLoadTemplate ($template) {
225 // Create the full-qualified filename (FQFN)
226 $FQFN = sprintf('%s/templates/%s.tpl.php',
231 // Is this template found?
232 if (isCrackerTrackerFileFound($FQFN)) {
234 crackerTrackerLanguage();
239 // Not found, so die here
244 // Loads a given "template" (this is more an include file)
245 function crackerTrackerLoadLocalizedTemplate ($template) {
246 // Create the full-qualified filename (FQFN)
247 $FQFN = sprintf('%s/templates/%s/%s.tpl.php',
249 getCrackerTrackerLanguage(),
253 // Is this template found?
254 if (isCrackerTrackerFileFound($FQFN)) {
258 // Not found, so die here
263 // Detects the browser's language file and tries to load it, fall-back on english!
264 function crackerTrackerLanguage () {
265 // Default is English
266 $GLOBALS['ctracker_language'] = 'en';
269 // Is the language string there?
270 if (isset($_SERVER['HTTP_ACCEPT_LANGUAGE'])) {
272 foreach (explode(',', $_SERVER['HTTP_ACCEPT_LANGUAGE']) as $lang) {
273 // Split it again for q=x.x value
274 $langArray = explode(';', $lang);
276 // If there is an entry, we have a weight
277 if ((isset($langArray[1])) && ($langArray[1] > $weight)) {
278 // Use this language/weight instead
279 $GLOBALS['ctracker_language'] = $langArray[0];
280 $weight = $langArray[1];
286 $FQFN = sprintf('%s/language/%s.php',
288 getCrackerTrackerLanguage()
291 // Is it not there, switch to "en"
292 if ((getCrackerTrackerLanguage() != 'en') && (!isCrackerTrackerFileFound($FQFN))) {
293 // English is default!
294 $GLOBALS['ctracker_language'] = 'en';
296 // Construct FQFN again
297 $FQFN = sprintf('%s/language/en.php', dirname(__FILE__));
300 // Load the language file
304 // Loads a given email template and passes through $content
305 function crackerTrackerLoadEmailTemplate ($template, array $content = array(), $language = NULL) {
307 crackerTrackerLanguage();
310 $FQFN = sprintf('%s/mails/%s/%s.tpl',
312 getCrackerTrackerLanguage($language),
316 // So is the file there?
317 if (isCrackerTrackerFileFound($FQFN)) {
319 $result = 'No result from template ' . $template . '. Please report this at http://forum.ship-simu.org Thank you.';
322 eval('$result = "' . crackerTrackerCompileCode(file_get_contents($FQFN)) . '";');
332 // Getter for message
333 function getCrackerTrackerLocalized ($message) {
335 $output = '!' . $message . '!';
337 // Is the language string there?
338 if (isset($GLOBALS['ctracker_localized'][$message])) {
340 $output = $GLOBALS['ctracker_localized'][$message];
347 // Tries to find a message and outputs it
348 function crackerTrackerOutputLocalized ($message) {
350 print getCrackerTrackerLocalized($message);
353 // Compiles the given code
354 function crackerTrackerCompileCode ($code) {
355 // Find all $content[foo]
356 preg_match_all('/\$(content|GLOBALS)((\[([a-zA-Z0-9-_]+)\])*)/', $code, $matches);
358 // Replace " with {QUOTE}
359 $code = str_replace('"', '{QUOTE}', $code);
362 foreach ($matches[0] as $key=>$match) {
364 if (substr($match, 0, 8) == '$GLOBALS') {
366 $code = str_replace($match, "\" . \$GLOBALS['" . $matches[4][$key] . "'] . \"", $code);
367 } elseif (substr($match, 0, 8) == '$content') {
369 $code = str_replace($match, "\" . \$content['" . $matches[4][$key] . "'] . \"", $code);
377 // "Getter" for language
378 function getCrackerTrackerLanguage ($lang = NULL) {
379 // Default is from browser
380 $language = $GLOBALS['ctracker_language'];
383 if (!is_null($lang)) {
384 // Then use this instead
392 // "Getter" for ticket id
393 function getCrackerTrackerTicketId () {
398 if (isset($GLOBALS['ctracker_last_ticket']['ctracker_ticket'])) {
400 $id = $GLOBALS['ctracker_last_ticket']['ctracker_ticket'];
407 // Sends a cookie to the user that he would not see this security warning again
408 function sendCrackerTrackerCookie () {
410 // @TODO Why can't domain be set to value from crackerTrackerServerName() ?
411 setcookie('ctracker_ticket', getCrackerTrackerTicketId(), (time() + 60*60*24), '/', '', crackerTrackerSecured(), TRUE);
412 $_COOKIE['ctracker_ticket'] = getCrackerTrackerTicketId();
415 // Is the cookie set?
416 function ifCrackerTrackerCookieIsSet () {
417 // Is it set and valid?
418 return ((isset($_COOKIE['ctracker_ticket'])) && ($_COOKIE['ctracker_ticket'] > 0));
421 // Redirects to the same URL
422 function crackerTrackerRedirectSameUrl () {
424 $url = '://' . crackerTrackerServerName() . crackerTrackerScriptName() . '?' . crackerTrackerQueryString();
427 if (crackerTrackerSecured()) {
429 $url = 'https' . $url;
432 $url = 'http' . $url;
436 crackerTrackerSendRawRedirect($url);
440 * Send a HTTP redirect to the browser. This function was taken from DokuWiki
441 * (GNU GPL 2; http://www.dokuwiki.org) and modified to fit into this script.
443 * Works arround Microsoft IIS cookie sending bug. Does exit the script.
445 * @link http://support.microsoft.com/kb/q176113/
446 * @author Andreas Gohr <andi@splitbrain.org>
449 function crackerTrackerSendRawRedirect ($url) {
450 // Better remove any data by ctracker
453 // always close the session
454 session_write_close();
456 // Revert entity &
457 $url = str_replace('&', '&', $url);
459 // check if running on IIS < 6 with CGI-PHP
460 if ((isset($_SERVER['SERVER_SOFTWARE'])) && (isset($_SERVER['GATEWAY_INTERFACE'])) &&
461 (strpos($_SERVER['GATEWAY_INTERFACE'],'CGI') !== FALSE) &&
462 (preg_match('|^Microsoft-IIS/(\d)\.\d$|', trim($_SERVER['SERVER_SOFTWARE']), $matches)) &&
464 // Send the IIS header
465 header('Refresh: 0;url=' . $url);
467 // Send generic header
468 header('Location: ' . $url);
473 // Removes all ctracker-related data from global space
474 function unsetCtrackerData () {
475 // Unset all ctracker data
481 'ctracker_debug_enabled',
483 'ctracker_whitelist',
484 'ctracker_get_blacklist',
485 'ctracker_post_blacklist',
487 'ctracker_post_track',
488 'ctracker_checkworm',
489 'ctracker_check_post',
491 'ctracker_last_result',
495 'ctracker_localized',
499 unset($GLOBALS[$key]);