]> git.mxchange.org Git - ctracker.git/blobdiff - libs/lib_general.php
Updated a lot:
[ctracker.git] / libs / lib_general.php
index 6e7fc62ea8ef653a296d0cdaae4a2dd616bc03c6..8d5dd1e93ae268deb63ba850cc1d3d0b5cc8f519 100644 (file)
@@ -2,11 +2,11 @@
 /**
  * General functions library
  *
- * @author             Roland Haeder <webmaster@ship-simu.org>
+ * @author             Roland Haeder <webmaster@shipsimu.org>
  * @version            3.0.0
- * @copyright  Copyright (c) 2009, 2010 Cracker Tracker Team
+ * @copyright  Copyright (c) 2009 - 2011 Cracker Tracker Team
  * @license            GNU GPL 3.0 or any newer version
- * @link               http://www.ship-simu.org
+ * @link               http://www.shipsimu.org
  *
  * This program is free software: you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
@@ -73,31 +73,61 @@ if (!function_exists('implode_secure')) {
        } // END - function
 } // END - if
 
+// Load configuration, if found
+function crackerTrackerLoadConfiguration () {
+       // FQFN
+       $fqfn = sprintf('%s/config/db_config.php', $GLOBALS['ctracker_base_path']);
+
+       // Is the file readable?
+       if (!isCrackerTrackerFileFound($fqfn)) {
+               // No config file found
+               die(__FUNCTION__.': No configuration file found.');
+       } // END - if
+
+       // Load it
+       require($fqfn);
+
+       // Load email header
+       $GLOBALS['ctracker_header'] = crackerTrackerLoadEmailTemplate('header');
+}
+
 // Getter for ctracker_debug_enabled
 function isCrackerTrackerDebug () {
        // Is it set?
-       return ((isset($GLOBALS['ctracker_debug_enabled'])) && ($GLOBALS['ctracker_debug_enabled'] === true));
+       $result = ((isset($GLOBALS['ctracker_debug_enabled'])) && ($GLOBALS['ctracker_debug_enabled'] === TRUE));
+
+       // Debug message
+       //* DEBUG: */ error_log('result=' . intval($result));
+
+       // Return it
+       return $result;
 }
 
 // Determines the real remote address
 function determineCrackerTrackerRealRemoteAddress () {
+       // Initial value
+       $address = '0.0.0.0';
+
        // Is a proxy in use?
        if (isset($_SERVER['HTTP_X_FORWARDED_FOR'])) {
                // Proxy was used
-               $address = $_SERVER['HTTP_X_FORWARDED_FOR'];
+               $address = trim($_SERVER['HTTP_X_FORWARDED_FOR']);
        } elseif (isset($_SERVER['HTTP_CLIENT_IP'])) {
                // Yet, another proxy
-               $address = $_SERVER['HTTP_CLIENT_IP'];
-       } else {
+               $address = trim($_SERVER['HTTP_CLIENT_IP']);
+       } elseif (isset($_SERVER['REMOTE_ADDR'])) {
                // The regular address when no proxy was used
-               $address = $_SERVER['REMOTE_ADDR'];
+               $address = trim(getenv('REMOTE_ADDR'));
        }
 
-       // This strips out the real address from proxy output
-       if (strstr($address, ',')) {
+       if ($address == 'unknown') {
+               // Invalid IP somehow given
+               $address = '0.0.0.0';
+       } elseif (strstr($address, ',')) {
+               // This strips out the real address from proxy output
                $addressArray = explode(',', $address);
                $address = $addressArray[0];
-       } // END - if
+       }
 
        // Return the result
        return $address;
@@ -129,18 +159,36 @@ function crackerTrackerUserAgent () {
 
 // Detects the script name
 function crackerTrackerScriptName () {
+       // Is it there?
+       if (!isset($_SERVER['SCRIPT_NAME'])) {
+               // Return NULL
+               return NULL;
+       } // END - if
+
        // Should always be there!
        return crackerTrackerSecureString($_SERVER['SCRIPT_NAME']);
 }
 
 // Detects the query string
 function crackerTrackerQueryString () {
+       // Is it there?
+       if (!isset($_SERVER['QUERY_STRING'])) {
+               // Return NULL
+               return NULL;
+       } // END - if
+
        // Should always be there!
-       return crackerTrackerEscapeString($_SERVER['QUERY_STRING']);
+       return crackerTrackerEscapeString(urldecode($_SERVER['QUERY_STRING']));
 }
 
 // Detects the server's name
 function crackerTrackerServerName () {
+       // Is it there?
+       if (!isset($_SERVER['SERVER_NAME'])) {
+               // Return NULL
+               return NULL;
+       } // END - if
+
        // Should always be there!
        return crackerTrackerSecureString($_SERVER['SERVER_NAME']);
 }
@@ -153,7 +201,7 @@ function crackerTrackerReferer () {
        // Is it there?
        if (isset($_SERVER['HTTP_REFERER'])) {
                // Then use it securely
-               $referer = crackerTrackerSecureString($_SERVER['HTTP_REFERER']);
+               $referer = crackerTrackerSecureString(urldecode($_SERVER['HTTP_REFERER']));
        } // END - if
 
        // Return it
@@ -199,8 +247,8 @@ function isCrackerTrackerFileFound ($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__),
+       $FQFN = sprintf('%s/libs/templates/%s.tpl.php',
+               $GLOBALS['ctracker_base_path'],
                $template
        );
 
@@ -220,8 +268,8 @@ function crackerTrackerLoadTemplate ($template) {
 // 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__),
+       $FQFN = sprintf('%s/libs/templates/%s/%s.tpl.php',
+               $GLOBALS['ctracker_base_path'],
                getCrackerTrackerLanguage(),
                $template
        );
@@ -259,8 +307,8 @@ function crackerTrackerLanguage () {
        } // END - if
 
        // Construct FQFN
-       $FQFN = sprintf("%s/language/%s.php",
-               dirname(__FILE__),
+       $FQFN = sprintf('%s/libs/language/%s.php',
+               $GLOBALS['ctracker_base_path'],
                getCrackerTrackerLanguage()
        );
 
@@ -270,9 +318,7 @@ function crackerTrackerLanguage () {
                $GLOBALS['ctracker_language'] = 'en';
 
                // Construct FQFN again
-               $FQFN = sprintf("%s/language/en.php",
-                       dirname(__FILE__)
-               );
+               $FQFN = sprintf('%s/libs/language/en.php', $GLOBALS['ctracker_base_path']);
        } // END - if
 
        // Load the language file
@@ -280,13 +326,13 @@ function crackerTrackerLanguage () {
 }
 
 // Loads a given email template and passes through $content
-function crackerTrackerLoadEmailTemplate ($template, array $content = array(), $language = null) {
+function crackerTrackerLoadEmailTemplate ($template, array $content = array(), $language = NULL) {
        // Init language
        crackerTrackerLanguage();
 
        // Generate the FQFN
-       $FQFN = sprintf("%s/mails/%s/%s.tpl",
-               dirname(__FILE__),
+       $FQFN = sprintf('%s/libs/mails/%s/%s.tpl',
+               $GLOBALS['ctracker_base_path'],
                getCrackerTrackerLanguage($language),
                $template
        );
@@ -294,10 +340,11 @@ function crackerTrackerLoadEmailTemplate ($template, array $content = array(), $
        // 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.';
+               $result = 'No result from template ' . $template . '. Please report this at http://forum.shipsimu.org Thank you.';
 
                // Then load it
-               eval('$result = "' . crackerTrackerCompileCode(file_get_contents($FQFN)) . '";');
+               //* DEBUG-DIE: */ die('<pre>$result = "' . crackerTrackerCompileCode(trim(file_get_contents($FQFN))) . '";</pre>');
+               eval('$result = "' . crackerTrackerCompileCode(trim(file_get_contents($FQFN))) . '";');
 
                // Return the result
                return $result;
@@ -353,7 +400,7 @@ function crackerTrackerCompileCode ($code) {
 }
 
 // "Getter" for language
-function getCrackerTrackerLanguage ($lang = null) {
+function getCrackerTrackerLanguage ($lang = NULL) {
        // Default is from browser
        $language = $GLOBALS['ctracker_language'];
 
@@ -386,7 +433,7 @@ function getCrackerTrackerTicketId () {
 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);
+       setcookie('ctracker_ticket', getCrackerTrackerTicketId(), (time() + 60*60*24), '/', '', crackerTrackerSecured(), TRUE);
        $_COOKIE['ctracker_ticket'] = getCrackerTrackerTicketId();
 }
 
@@ -415,13 +462,9 @@ function crackerTrackerRedirectSameUrl () {
 }
 
 /**
- * Send a HTTP redirect to the browser. This function wass taken from DokuWiki
+ * Send a HTTP redirect to the browser. This function was 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/
@@ -429,6 +472,9 @@ function crackerTrackerRedirectSameUrl () {
  * @access  private
  */
 function crackerTrackerSendRawRedirect ($url) {
+       // Better remove any data by ctracker
+       unsetCtrackerData();
+
        // always close the session
        session_write_close();
 
@@ -437,7 +483,7 @@ function crackerTrackerSendRawRedirect ($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) &&
+               (strpos($_SERVER['GATEWAY_INTERFACE'],'CGI') !== FALSE) &&
                (preg_match('|^Microsoft-IIS/(\d)\.\d$|', trim($_SERVER['SERVER_SOFTWARE']), $matches)) &&
                ($matches[1] < 6)) {
                // Send the IIS header
@@ -449,5 +495,39 @@ function crackerTrackerSendRawRedirect ($url) {
        exit();
 }
 
+// Removes all ctracker-related data from global space
+function unsetCtrackerData () {
+       // Debug message
+       //* DEBUG: */ error_log(__FUNCTION__ . ': CALLED!');
+
+       // Unset all ctracker data
+       foreach (array(
+                       'ctracker_base_path',
+                       'ctracker_host',
+                       'ctracker_dbname',
+                       'ctracker_user',
+                       'ctracker_password',
+                       'ctracker_debug_enabled',
+                       'ctracker_email',
+                       'ctracker_whitelist',
+                       'ctracker_get_blacklist',
+                       'ctracker_post_blacklist',
+                       'ctracker_header',
+                       'ctracker_post_track',
+                       'ctracker_checkworm',
+                       'ctracker_check_post',
+                       'ctracker_last_sql',
+                       'ctracker_last_result',
+                       'ctracker_config',
+                       'ctracker_updates',
+                       'ctracker_language',
+                       'ctracker_localized',
+                       'ctracker_link',
+               ) as $key) {
+                       // Unset it
+                       unset($GLOBALS[$key]);
+       } // END - foreach
+}
+
 // [EOF]
 ?>