X-Git-Url: https://git.mxchange.org/?a=blobdiff_plain;f=libs%2Flib_general.php;h=dee78e189b0ccfc467f66797d36f78d40942e307;hb=a9131bd6b5cd7ac4312aef3e226f3a890a4e176f;hp=85fed70d2c2fadaa5670cdfc2f31b31be7775ea9;hpb=7fa424e2fb3bb33a79ba1d793f1785809f99c508;p=ctracker.git diff --git a/libs/lib_general.php b/libs/lib_general.php index 85fed70..dee78e1 100644 --- a/libs/lib_general.php +++ b/libs/lib_general.php @@ -4,7 +4,7 @@ * * @author Roland Haeder * @version 3.0.0 - * @copyright Copyright (c) 2009 Cracker Tracker Team + * @copyright Copyright (c) 2009, 2010 Cracker Tracker Team * @license GNU GPL 3.0 or any newer version * @link http://www.ship-simu.org * @@ -112,5 +112,342 @@ function isCrackerTrackerProxyUsed () { return $proxyUsed; } +// Detects the user-agent string +function crackerTrackerUserAgent () { + // Default is 'unknown' + $ua = 'unknown'; + + // Is the entry there? + if (isset($_SERVER['HTTP_USER_AGENT'])) { + // Then use it + $ua = $_SERVER['HTTP_USER_AGENT']; + } // END - if + + // Return it + return $ua; +} + +// Detects the script name +function crackerTrackerScriptName () { + // Should always be there! + return crackerTrackerSecureString($_SERVER['SCRIPT_NAME']); +} + +// Detects the query string +function crackerTrackerQueryString () { + // Should always be there! + return crackerTrackerEscapeString($_SERVER['QUERY_STRING']); +} + +// Detects the server's name +function crackerTrackerServerName () { + // Should always be there! + return crackerTrackerSecureString($_SERVER['SERVER_NAME']); +} + +// Detects the referer +function crackerTrackerReferer () { + // Default is a dash + $referer = '-'; + + // Is it there? + if (isset($_SERVER['HTTP_REFERER'])) { + // Then use it securely + $referer = crackerTrackerSecureString($_SERVER['HTTP_REFERER']); + } // END - if + + // Return it + return $referer; +} + +// Detects the scripts path +function crackerTrackerScriptPath () { + // Should always be there! + $path = dirname(crackerTrackerScriptName()) . '/'; + + // Return detected path + return $path; +} + +// Detects wether we have SSL +function crackerTrackerSecured () { + // Detect it + $ssl = ((isset($_SERVER['HTTPS'])) && ($_SERVER['HTTPS'] != 'off')); + + // Return result + return $ssl; +} + +// Secures a string by escaping it and passing it through strip_tags,htmlentities-chain +function crackerTrackerSecureString ($str) { + // First escape it + $str = crackerTrackerEscapeString($str); + + // Then pass it through the functions + $str = htmlentities(strip_tags($str), ENT_QUOTES); + + // Return it + return $str; +} + +// Is the file there and readable? +function isCrackerTrackerFileFound ($FQFN) { + // Simply check it + return ((file_exists($FQFN)) && (is_readable($FQFN))); +} + +// Loads a given "template" (this is more an include file) +function crackerTrackerLoadTemplate ($template) { + // Create the full-qualified filename (FQFN) + $FQFN = sprintf("%s/templates/%s.tpl.php", + dirname(__FILE__), + $template + ); + + // Is this template found? + if (isCrackerTrackerFileFound($FQFN)) { + // Detect language + crackerTrackerLanguage(); + + // Load it + require_once($FQFN); + } else { + // Not found, so die here + crackerTrackerDie(); + } +} + +// Loads a given "template" (this is more an include file) +function crackerTrackerLoadLocalizedTemplate ($template) { + // Create the full-qualified filename (FQFN) + $FQFN = sprintf("%s/templates/%s/%s.tpl.php", + dirname(__FILE__), + getCrackerTrackerLanguage(), + $template + ); + + // Is this template found? + if (isCrackerTrackerFileFound($FQFN)) { + // Load it + require_once($FQFN); + } else { + // Not found, so die here + crackerTrackerDie(); + } +} + +// Detects the browser's language file and tries to load it, fall-back on english! +function crackerTrackerLanguage () { + // Default is English + $GLOBALS['ctracker_language'] = 'en'; + $weight = 1; + + // Is the language string there? + if (isset($_SERVER['HTTP_ACCEPT_LANGUAGE'])) { + // Then detect it + foreach (explode(',', $_SERVER['HTTP_ACCEPT_LANGUAGE']) as $lang) { + // Split it again for q=x.x value + $langArray = explode(';', $lang); + + // If there is an entry, we have a weight + if ((isset($langArray[1])) && ($langArray[1] > $weight)) { + // Use this language/weight instead + $GLOBALS['ctracker_language'] = $langArray[0]; + $weight = $langArray[1]; + } // END - if + } // END - foreach + } // END - if + + // Construct FQFN + $FQFN = sprintf("%s/language/%s.php", + dirname(__FILE__), + getCrackerTrackerLanguage() + ); + + // Is it not there, switch to "en" + if ((getCrackerTrackerLanguage() != 'en') && (!isCrackerTrackerFileFound($FQFN))) { + // English is default! + $GLOBALS['ctracker_language'] = 'en'; + + // Construct FQFN again + $FQFN = sprintf("%s/language/en.php", + dirname(__FILE__) + ); + } // END - if + + // Load the language file + require($FQFN); +} + +// Loads a given email template and passes through $content +function crackerTrackerLoadEmailTemplate ($template, array $content = array(), $language = null) { + // Init language + crackerTrackerLanguage(); + + // Generate the FQFN + $FQFN = sprintf("%s/mails/%s/%s.tpl", + dirname(__FILE__), + getCrackerTrackerLanguage($language), + $template + ); + + // So is the file there? + if (isCrackerTrackerFileFound($FQFN)) { + // Init result + $result = 'No result from template ' . $template . '. Please report this at http://forum.ship-simu.org Thank you.'; + + // Then load it + eval('$result = "' . crackerTrackerCompileCode(file_get_contents($FQFN)) . '";'); + + // Return the result + return $result; + } else { + // Not found + crackerTrackerDie(); + } +} + +// Getter for message +function getCrackerTrackerLocalized ($message) { + // Default message + $output = '!' . $message . '!'; + + // Is the language string there? + if (isset($GLOBALS['ctracker_localized'][$message])) { + // Use this instead + $output = $GLOBALS['ctracker_localized'][$message]; + } // END - if + + // Return it + return $output; +} + +// Tries to find a message and outputs it +function crackerTrackerOutputLocalized ($message) { + // Output it + print getCrackerTrackerLocalized($message); +} + +// Compiles the given code +function crackerTrackerCompileCode ($code) { + // Find all $content[foo] + preg_match_all('/\$(content|GLOBALS)((\[([a-zA-Z0-9-_]+)\])*)/', $code, $matches); + + // Replace " with {QUOTE} + $code = str_replace('"', '{QUOTE}', $code); + + // Replace all + foreach ($matches[0] as $key=>$match) { + // Replace it + if (substr($match, 0, 8) == '$GLOBALS') { + // $GLOBALS + $code = str_replace($match, "\" . \$GLOBALS['" . $matches[4][$key] . "'] . \"", $code); + } elseif (substr($match, 0, 8) == '$content') { + // $content + $code = str_replace($match, "\" . \$content['" . $matches[4][$key] . "'] . \"", $code); + } + } // END - foreach + + // Return it + return $code; +} + +// "Getter" for language +function getCrackerTrackerLanguage ($lang = null) { + // Default is from browser + $language = $GLOBALS['ctracker_language']; + + // Is $lang set? + if (!is_null($lang)) { + // Then use this instead + $language = $lang; + } // END - if + + // Return it + return $language; +} + +// "Getter" for ticket id +function getCrackerTrackerTicketId () { + // Default is zero + $id = 0; + + // Is it set? + if (isset($GLOBALS['ctracker_last_ticket']['ctracker_ticket'])) { + // Then use it + $id = $GLOBALS['ctracker_last_ticket']['ctracker_ticket']; + } // END - if + + // Return the number + return $id; +} + +// Sends a cookie to the user that he would not see this security warning again +function sendCrackerTrackerCookie () { + // Set the cookie + // @TODO Why can't domain be set to value from crackerTrackerServerName() ? + setcookie('ctracker_ticket', getCrackerTrackerTicketId(), (time() + 60*60*24), '/', '', crackerTrackerSecured(), true); + $_COOKIE['ctracker_ticket'] = getCrackerTrackerTicketId(); +} + +// Is the cookie set? +function ifCrackerTrackerCookieIsSet () { + // Is it set and valid? + return ((isset($_COOKIE['ctracker_ticket'])) && ($_COOKIE['ctracker_ticket'] > 0)); +} + +// Redirects to the same URL +function crackerTrackerRedirectSameUrl () { + // Construct the url + $url = '://' . crackerTrackerServerName() . crackerTrackerScriptName() . '?' . crackerTrackerQueryString(); + + // Do we have SSL? + if (crackerTrackerSecured()) { + // HTTPS + $url = 'https' . $url; + } else { + // HTTP + $url = 'http' . $url; + } + + // And redirect + crackerTrackerSendRawRedirect($url); +} + +/** + * Send a HTTP redirect to the browser. This function wass taken from DokuWiki + * (GNU GPL 2; http://www.dokuwiki.org) and modified to fit into this script. + * + * ---------------------------------------------------------------------------- + * If you want to redirect, please use redirectToUrl(); instead + * ---------------------------------------------------------------------------- + * + * Works arround Microsoft IIS cookie sending bug. Does exit the script. + * + * @link http://support.microsoft.com/kb/q176113/ + * @author Andreas Gohr + * @access private + */ +function crackerTrackerSendRawRedirect ($url) { + // always close the session + session_write_close(); + + // Revert entity & + $url = str_replace('&', '&', $url); + + // check if running on IIS < 6 with CGI-PHP + if ((isset($_SERVER['SERVER_SOFTWARE'])) && (isset($_SERVER['GATEWAY_INTERFACE'])) && + (strpos($_SERVER['GATEWAY_INTERFACE'],'CGI') !== false) && + (preg_match('|^Microsoft-IIS/(\d)\.\d$|', trim($_SERVER['SERVER_SOFTWARE']), $matches)) && + ($matches[1] < 6)) { + // Send the IIS header + header('Refresh: 0;url=' . $url); + } else { + // Send generic header + header('Location: ' . $url); + } + exit(); +} + // [EOF] ?>