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 () {
85 if (isset($_SERVER['HTTP_X_FORWARDED_FOR'])) {
87 $address = $_SERVER['HTTP_X_FORWARDED_FOR'];
88 } elseif (isset($_SERVER['HTTP_CLIENT_IP'])) {
90 $address = $_SERVER['HTTP_CLIENT_IP'];
92 // The regular address when no proxy was used
93 $address = $_SERVER['REMOTE_ADDR'];
96 // This strips out the real address from proxy output
97 if (strstr($address, ',')) {
98 $addressArray = explode(',', $address);
99 $address = $addressArray[0];
106 // Determine if a proxy was used
107 function isCrackerTrackerProxyUsed () {
108 // Check if specific entries are set
109 $proxyUsed = ((isset($_SERVER['HTTP_X_FORWARDED_FOR'])) || (isset($_SERVER['HTTP_CLIENT_IP'])));
115 // Detects the user-agent string
116 function crackerTrackerUserAgent () {
117 // Default is 'unknown'
120 // Is the entry there?
121 if (isset($_SERVER['HTTP_USER_AGENT'])) {
122 // Then use it securely
123 $ua = crackerTrackerSecureString($_SERVER['HTTP_USER_AGENT']);
130 // Detects the script name
131 function crackerTrackerScriptName () {
132 // Should always be there!
133 return crackerTrackerSecureString($_SERVER['SCRIPT_NAME']);
136 // Detects the query string
137 function crackerTrackerQueryString () {
138 // Should always be there!
139 return crackerTrackerEscapeString($_SERVER['QUERY_STRING']);
142 // Detects the server's name
143 function crackerTrackerServerName () {
144 // Should always be there!
145 return crackerTrackerSecureString($_SERVER['SERVER_NAME']);
148 // Detects the referer
149 function crackerTrackerReferer () {
154 if (isset($_SERVER['HTTP_REFERER'])) {
155 // Then use it securely
156 $referer = crackerTrackerSecureString($_SERVER['HTTP_REFERER']);
163 // Detects the scripts path
164 function crackerTrackerScriptPath () {
165 // Should always be there!
166 $path = dirname(crackerTrackerScriptName()) . '/';
168 // Return detected path
172 // Detects wether we have SSL
173 function crackerTrackerSecured () {
175 $ssl = ((isset($_SERVER['HTTPS'])) && ($_SERVER['HTTPS'] != 'off'));
181 // Secures a string by escaping it and passing it through strip_tags,htmlentities-chain
182 function crackerTrackerSecureString ($str) {
184 $str = crackerTrackerEscapeString($str);
186 // Then pass it through the functions
187 $str = htmlentities(strip_tags($str), ENT_QUOTES);
193 // Is the file there and readable?
194 function isCrackerTrackerFileFound ($FQFN) {
196 return ((file_exists($FQFN)) && (is_readable($FQFN)));
199 // Loads a given "template" (this is more an include file)
200 function crackerTrackerLoadTemplate ($template) {
201 // Create the full-qualified filename (FQFN)
202 $FQFN = sprintf("%s/templates/%s.tpl.php",
207 // Is this template found?
208 if (isCrackerTrackerFileFound($FQFN)) {
210 crackerTrackerLanguage();
215 // Not found, so die here
220 // Loads a given "template" (this is more an include file)
221 function crackerTrackerLoadLocalizedTemplate ($template) {
222 // Create the full-qualified filename (FQFN)
223 $FQFN = sprintf("%s/templates/%s/%s.tpl.php",
225 getCrackerTrackerLanguage(),
229 // Is this template found?
230 if (isCrackerTrackerFileFound($FQFN)) {
234 // Not found, so die here
239 // Detects the browser's language file and tries to load it, fall-back on english!
240 function crackerTrackerLanguage () {
241 // Default is English
242 $GLOBALS['ctracker_language'] = 'en';
245 // Is the language string there?
246 if (isset($_SERVER['HTTP_ACCEPT_LANGUAGE'])) {
248 foreach (explode(',', $_SERVER['HTTP_ACCEPT_LANGUAGE']) as $lang) {
249 // Split it again for q=x.x value
250 $langArray = explode(';', $lang);
252 // If there is an entry, we have a weight
253 if ((isset($langArray[1])) && ($langArray[1] > $weight)) {
254 // Use this language/weight instead
255 $GLOBALS['ctracker_language'] = $langArray[0];
256 $weight = $langArray[1];
262 $FQFN = sprintf("%s/language/%s.php",
264 getCrackerTrackerLanguage()
267 // Is it not there, switch to "en"
268 if ((getCrackerTrackerLanguage() != 'en') && (!isCrackerTrackerFileFound($FQFN))) {
269 // English is default!
270 $GLOBALS['ctracker_language'] = 'en';
272 // Construct FQFN again
273 $FQFN = sprintf("%s/language/en.php",
278 // Load the language file
282 // Loads a given email template and passes through $content
283 function crackerTrackerLoadEmailTemplate ($template, array $content = array(), $language = null) {
285 crackerTrackerLanguage();
288 $FQFN = sprintf("%s/mails/%s/%s.tpl",
290 getCrackerTrackerLanguage($language),
294 // So is the file there?
295 if (isCrackerTrackerFileFound($FQFN)) {
297 $result = 'No result from template ' . $template . '. Please report this at http://forum.ship-simu.org Thank you.';
300 eval('$result = "' . crackerTrackerCompileCode(file_get_contents($FQFN)) . '";');
310 // Getter for message
311 function getCrackerTrackerLocalized ($message) {
313 $output = '!' . $message . '!';
315 // Is the language string there?
316 if (isset($GLOBALS['ctracker_localized'][$message])) {
318 $output = $GLOBALS['ctracker_localized'][$message];
325 // Tries to find a message and outputs it
326 function crackerTrackerOutputLocalized ($message) {
328 print getCrackerTrackerLocalized($message);
331 // Compiles the given code
332 function crackerTrackerCompileCode ($code) {
333 // Find all $content[foo]
334 preg_match_all('/\$(content|GLOBALS)((\[([a-zA-Z0-9-_]+)\])*)/', $code, $matches);
336 // Replace " with {QUOTE}
337 $code = str_replace('"', '{QUOTE}', $code);
340 foreach ($matches[0] as $key=>$match) {
342 if (substr($match, 0, 8) == '$GLOBALS') {
344 $code = str_replace($match, "\" . \$GLOBALS['" . $matches[4][$key] . "'] . \"", $code);
345 } elseif (substr($match, 0, 8) == '$content') {
347 $code = str_replace($match, "\" . \$content['" . $matches[4][$key] . "'] . \"", $code);
355 // "Getter" for language
356 function getCrackerTrackerLanguage ($lang = null) {
357 // Default is from browser
358 $language = $GLOBALS['ctracker_language'];
361 if (!is_null($lang)) {
362 // Then use this instead
370 // "Getter" for ticket id
371 function getCrackerTrackerTicketId () {
376 if (isset($GLOBALS['ctracker_last_ticket']['ctracker_ticket'])) {
378 $id = $GLOBALS['ctracker_last_ticket']['ctracker_ticket'];
385 // Sends a cookie to the user that he would not see this security warning again
386 function sendCrackerTrackerCookie () {
388 // @TODO Why can't domain be set to value from crackerTrackerServerName() ?
389 setcookie('ctracker_ticket', getCrackerTrackerTicketId(), (time() + 60*60*24), '/', '', crackerTrackerSecured(), true);
390 $_COOKIE['ctracker_ticket'] = getCrackerTrackerTicketId();
393 // Is the cookie set?
394 function ifCrackerTrackerCookieIsSet () {
395 // Is it set and valid?
396 return ((isset($_COOKIE['ctracker_ticket'])) && ($_COOKIE['ctracker_ticket'] > 0));
399 // Redirects to the same URL
400 function crackerTrackerRedirectSameUrl () {
402 $url = '://' . crackerTrackerServerName() . crackerTrackerScriptName() . '?' . crackerTrackerQueryString();
405 if (crackerTrackerSecured()) {
407 $url = 'https' . $url;
410 $url = 'http' . $url;
414 crackerTrackerSendRawRedirect($url);
418 * Send a HTTP redirect to the browser. This function was taken from DokuWiki
419 * (GNU GPL 2; http://www.dokuwiki.org) and modified to fit into this script.
421 * Works arround Microsoft IIS cookie sending bug. Does exit the script.
423 * @link http://support.microsoft.com/kb/q176113/
424 * @author Andreas Gohr <andi@splitbrain.org>
427 function crackerTrackerSendRawRedirect ($url) {
428 // always close the session
429 session_write_close();
431 // Revert entity &
432 $url = str_replace('&', '&', $url);
434 // check if running on IIS < 6 with CGI-PHP
435 if ((isset($_SERVER['SERVER_SOFTWARE'])) && (isset($_SERVER['GATEWAY_INTERFACE'])) &&
436 (strpos($_SERVER['GATEWAY_INTERFACE'],'CGI') !== false) &&
437 (preg_match('|^Microsoft-IIS/(\d)\.\d$|', trim($_SERVER['SERVER_SOFTWARE']), $matches)) &&
439 // Send the IIS header
440 header('Refresh: 0;url=' . $url);
442 // Send generic header
443 header('Location: ' . $url);