Several kinky code smoothed, fixes for admin login
[mailer.git] / inc / mysql-connect.php
index 0d53569e82584100cf0b2705e8f5c45098af3adf..a733ea9abd2440a6c38aa14c0f974e3fcc7b151d 100644 (file)
  * -------------------------------------------------------------------- *
  * Kurzbeschreibung  : Verbindet zu Ihrer Datenbank                     *
  * -------------------------------------------------------------------- *
- *                                                                      *
+ * $Revision::                                                        $ *
+ * $Date::                                                            $ *
+ * $Tag:: 0.2.1-FINAL                                                 $ *
+ * $Author::                                                          $ *
+ * Needs to be in all Files and every File needs "svn propset           *
+ * svn:keywords Date Revision" (autoprobset!) at least!!!!!!            *
  * -------------------------------------------------------------------- *
- * Copyright (c) 2003 - 2008 by Roland Haeder                           *
+ * Copyright (c) 2003 - 2009 by Roland Haeder                           *
  * For more information visit: http://www.mxchange.org                  *
  *                                                                      *
  * This program is free software; you can redistribute it and/or modify *
 
 // Some security stuff...
 if (!defined('__SECURITY')) {
-       $INC = substr(dirname(__FILE__), 0, strpos(dirname(__FILE__), "/inc") + 4) . "/security.php";
-       require($INC);
-}
-
-// CFG: DEBUG-SQL (if enabled and DEBUG_MODE is enabled all SQL queries will be logged to debug.log)
-define('DEBUG_SQL', false);
-
-// Non-database functions
-require("inc/functions.php");
-
-// Filter functions
-LOAD_INC_ONCE("inc/filters.php");  
-
-// Functions which interact with the database
-LOAD_INC_ONCE("inc/mysql-manager.php");
-
-// Load extensions and language
-LOAD_INC_ONCE("inc/extensions.php");
-
-// Load database library
-LOAD_INC_ONCE("inc/db/lib.php");
-
-// Error handler function
-function __errorHandler ($errno, $errstr, $errfile, $errline) {
-       // Construct message
-       $msg = sprintf("errno=%s,errstr=%s,errfile=%s,errline=%s",
-               $errno,
-               $errstr,
-               basename($errfile),
-               $errline
-       );
-
-       // Write debug log message
-       DEBUG_LOG(__FUNCTION__, __LINE__, "".$msg, true);
+       die();
+} // END - if
 
-       // Output message to user and die
-       if (EXT_IS_ACTIVE("debug")) {
-               // Debug extension found! So Output a small message
-               mxchange_die("Error message written to debug.log. Please try to call <a href=\"{!URL!}\">the main page</a> to continue.");
-       } else {
-               // No debug extension found, so regular output
-               debug_report_bug($msg);
-       }
-}
+// Load more function libraries or includes
+foreach (array('functions', 'request-functions', 'session-functions', 'code-functions', 'language-functions', 'sql-functions', 'filter-functions', 'filters', 'mysql-manager', 'extensions-functions', 'handler') as $lib) {
+       // Load special functions
+       loadIncludeOnce('inc/' . $lib . '.php');
+} // END - foreach
 
 // Set error handler
 set_error_handler('__errorHandler');
 
-// Call-back function for running shutdown functions and close database connection
-function __SHUTDOWN_HOOK () {
-       // Call the filter chain 'shutdown'
-       RUN_FILTER('shutdown', null, false);
+// Disable block-mode by default
+enableBlockMode(false);
 
-       if (SQL_IS_LINK_UP()) {
-               // Close link
-               SQL_CLOSE(__FILE__, __LINE__);
-       } else {
-               // No database link
-               addFatalMessage(getMessage('NO_DB_LINK'));
-       }
-}
+// Init error handler
+initErrorHandler();
 
-// Register shutdown hook
-register_shutdown_function('__SHUTDOWN_HOOK');
-
-// Check if the user setups his MySQL stuff...
-if ((empty($MySQL['login'])) && (!defined('mxchange_installing')) && (!isset($_GET['installing'])) && (isBooleanConstantAndTrue('mxchange_installed'))) {
-       // No login entered and outside installation mode
-       OUTPUT_HTML("<strong>{--LANG_WARNING--}:</strong> ");
-       if (isBooleanConstantAndTrue('mxchange_installed')) {
-               // You have changed my configuration file!
-               mxchange_die("{--DIE_CONFIG_CHANGED_YOU--}");
-       } else {
-               // Please run the installation script (maybe again)
-               mxchange_die("{--DIE_RUN_INSTALL_MYSQL--}");
-       }
-} elseif ((!isBooleanConstantAndTrue('mxchange_installing')) && (!isset($_GET['installing'])) && (empty($MySQL['password'])) && (isBooleanConstantAndTrue('warn_no_pass'))) {
-       // No database password entered!!!
-       OUTPUT_HTML("<div>{--LANG_WARNING--}:</div> {--WARN_NULL_PASSWORD--}");
-}
-
-// Set dummy $_CONFIG array
-$_CONFIG = array(
-       'code_length' => 0,
-       'patch_level' => 0,
-       'last_update' => time()
-);
+// Init request
+initRequest();
 
 // Set important header_sent
-$GLOBALS['header_sent'] = 0;
+if (!isset($GLOBALS['header_sent'])) $GLOBALS['header_sent'] = 0;
 
 // Init fatal messages
 initFatalMessages();
 
+// Init message system
+initMessages();
+
 // Check if this file is writeable or read-only and warn the user
-if ((!isBooleanConstantAndTrue('mxchange_installing')) && (isBooleanConstantAndTrue('mxchange_installed'))) {
-       // Check for write-permission for config.php and inc directory
-       if (empty($GLOBALS['module'])) $GLOBALS['module'] = "index";
+if (!isInstalling()) {
+       // Load configuration file(s) here
+       loadIncludeOnce('inc/load_config.php');
+
+       // Load database layer here
+       loadIncludeOnce('inc/db/lib.php');
 
        // CSS array
-       EXT_INIT_CSS_FILES();
+       initExtensionCssFiles();
 
-       if ((!empty($MySQL['host'])) && (!empty($MySQL['login'])) && (!empty($MySQL['password'])) && (!empty($MySQL['dbase']))) {
+       if ((!empty($GLOBALS['mysql']['host'])) && (!empty($GLOBALS['mysql']['login'])) && (!empty($GLOBALS['mysql']['password'])) && (!empty($GLOBALS['mysql']['dbase']))) {
                // Connect to DB
-               SQL_CONNECT($MySQL['host'], $MySQL['login'], $MySQL['password'], __FILE__, __LINE__);
+               SQL_CONNECT($GLOBALS['mysql']['host'], $GLOBALS['mysql']['login'], $GLOBALS['mysql']['password'], __FILE__, __LINE__);
 
                // Is the link valid?
                if (SQL_IS_LINK_UP()) {
+                       // Enable exit on error
+                       enableExitOnError();
+
                        // Is it a valid resource?
-                       if (SQL_SELECT_DB($MySQL['dbase'], __FILE__, __LINE__) === true) {
+                       if (SQL_SELECT_DB($GLOBALS['mysql']['dbase'], __FILE__, __LINE__) === true) {
                                // This is required for extension 'optimize' to work
-                               define('__DB_NAME', $MySQL['dbase']);
+                               setConfigEntry('__DB_NAME', $GLOBALS['mysql']['dbase']);
 
                                // Remove MySQL array from namespace
-                               unset($MySQL);
-
-                               // Load configuration stuff
-                               mergeConfig(LOAD_CONFIG());
-
-                               // Load "databases" aka static arrays
-                               LOAD_INC_ONCE("inc/databases.php");
-
-                               // Loading patching system is required here...
-                               LOAD_INC_ONCE("inc/patch-system.php"); // Initialize patch system
-
-                               // Session management
-                               LOAD_INC_ONCE("inc/session.php");
-
-                               // Run daily reset
-                               LOAD_INC_ONCE("inc/check-reset.php");
-
-                               // Load admin include file if he is admin
-                               if (IS_ADMIN()) {
-                                       // Administrative functions
-                                       LOAD_INC_ONCE("inc/modules/admin/admin-inc.php");
-                               } // END - if
-                               //* DEBUG: */ ADD_POINTS_REFSYSTEM("test", 36, 1000);
-                               //* DEBUG: */ die();
-
-                               // Get all values
-                               if (($GLOBALS['output_mode'] != 1) && ($GLOBALS['output_mode'] != -1)) {
-                                       if (empty($GLOBALS['module']))  $GLOBALS['module'] = "empty";
-                                       if (empty($GLOBALS['what']))    $GLOBALS['what']   = GET_WHAT($GLOBALS['module']);
-                                       if (empty($GLOBALS['action']))  $GLOBALS['action'] = GET_ACTION($GLOBALS['module'], $GLOBALS['what']);
-                               } else {
-                                       // Set action/what to empty
-                                       $GLOBALS['action'] = "";
-                                       $GLOBALS['what']   = "";
-                               }
+                               unset($GLOBALS['mysql']);
+
+                               // Load cache
+                               loadIncludeOnce('inc/load_cache.php');
+
+                               // Init filter system
+                               initFilterSystem();
 
                                // Run the init filter chain
-                               RUN_FILTER('init');
-
-                               // Set default 'what' value
-                               //* DEBUG: */ OUTPUT_HTML("-".$GLOBALS['module']."/".$GLOBALS['what']."-<br />");
-                               if ((empty($GLOBALS['what'])) && (empty($GLOBALS['action'])) && ($GLOBALS['output_mode'] != 1) && ($GLOBALS['output_mode'] != -1)) {
-                                       if ($GLOBALS['module'] == "admin") {
-                                               // Set 'action' value to 'login' in admin menu
-                                               $GLOBALS['action'] = GET_ACTION($GLOBALS['module'], $GLOBALS['what']);
-                                       } elseif (($GLOBALS['module'] == "index") || ($GLOBALS['module'] == "login")) {
-                                               // Set 'what' value to 'welcome' in guest and member menu
-                                               $GLOBALS['what'] = "welcome";
-                                               if (getConfig('index_home') != "") $GLOBALS['what'] = getConfig('index_home');
-                                       } else {
-                                               // Anything else like begging link
-                                               $GLOBALS['what'] = "";
-                                       }
-                               } // END - if
-
-                               // Update sending pool
-                               if (($GLOBALS['output_mode'] != "1") && ($GLOBALS['output_mode'] != "-1")) LOAD_INC_ONCE("inc/pool-update.php"); // Sends out mails in configureable steps
-
-                               // Load all active extension including language files when not upgrading.
-                               // Check module for testing and count one click
-                               $dummy = CHECK_MODULE($GLOBALS['module']);
-                               if ($dummy == "done") COUNT_MODULE($GLOBALS['module']);
-                               unset($dummy);
-
-                               // Shall we activate the exchange?
-                               if (getConfig('activate_xchange') > 0) activateExchange();
-
-                               // Is the extension sql_patches installed and at least 0.3.6?
-                               if (GET_EXT_VERSION("sql_patches") >= "0.3.6") {
-                                       // Generate random number
-                                       if (isset($GLOBALS['userid'])) {
-                                               define('RAND_NUMBER', GEN_RANDOM_CODE(10, mt_rand(10000,32766), $GLOBALS['userid'], ""));
-                                       } else {
-                                               define('RAND_NUMBER', GEN_RANDOM_CODE(10, mt_rand(10000,32766), 0, ""));
-                                       }
-                               } else {
-                                       // Generate weak (!!!) code
-                                       define('RAND_NUMBER', mt_rand(1000000, 9999999));
+                               runFilterChain('init');
+
+                               // Check module for permissions
+                               $checkModule = checkModulePermissions();
+
+                               // Admin module should be accessable by guests to login
+                               if ((getModule() == 'admin') && ($checkModule == 'admin_only')) {
+                                       // This is fine and can be ignored
+                               } elseif ($checkModule != 'done') {
+                                       // Not fine!
+                                       logDebugMessage(__FILE__, __LINE__, sprintf("Check of module %s results in unexpected value: %s",
+                                               getModule(),
+                                               $checkModule
+                                       ));
                                }
                        } else {
-                               // Add language system
-                               LOAD_INC_ONCE("inc/language.php");
-
                                // Wrong database?
-                               addFatalMessage(getMessage('WRONG_DB_SELECTED'));
+                               addFatalMessage(__FILE__, __LINE__, getMessage('WRONG_DB_SELECTED'));
                        }
                } else {
-                       // Add language system
-                       LOAD_INC_ONCE("inc/language.php");
-
                        // No link to database!
-                       addFatalMessage(getMessage('NO_DB_LINK'));
+                       addFatalMessage(__FILE__, __LINE__, getMessage('NO_DB_LINK'));
                }
        } else {
-               // Add language system
-               LOAD_INC_ONCE("inc/language.php");
-
                // Maybe you forgot to enter your MySQL data?
-               addFatalMessage(getMessage('MYSQL_DATA_MISSING'));
+               addFatalMessage(__FILE__, __LINE__, getMessage('MYSQL_DATA_MISSING'));
        }
 } else {
        ///////////////////////////////////////////////////
        // Include neccessary functions for installation //
        ///////////////////////////////////////////////////
 
+       // Default output is 'direct' for HTML output
+       setConfigEntry('OUTPUT_MODE', 'direct');
+
+       // This hack prevents a backtrace in CSS output
+       if (getOutputMode() == 1) {
+               // Problem with config so set output mode
+               setConfigEntry('OUTPUT_MODE', 'render');
+       } // END - if
+
        // Set other missing variables
-       if (!isset($GLOBALS['output_mode'])) $GLOBALS['output_mode'] = "0";
+       if (!isOutputModeSet()) setOutputMode(0);
 
-       // Include required files
-       LOAD_INC_ONCE("inc/databases.php");
-       LOAD_INC_ONCE("inc/session.php");
+       // Include more
+       foreach (array('inc/databases.php','inc/versions.php','inc/db/lib.php','inc/session.php','inc/install-functions.php','inc/load_config.php') as $inc) {
+               // Load the include
+               loadIncludeOnce($inc);
+       } // END - foreach
 
-       // Check if we are in installation routine
-       if ((basename($_SERVER['PHP_SELF']) != "install.php") && ($GLOBALS['output_mode'] != "1") && ($GLOBALS['output_mode'] != -1)) {
-               // Redirect to the installation system
-               LOAD_URL("install.php");
-       } // END - if
+       // Load config
+       loadIncludeOnce('inc/load_config.php');
 
-       // Double-check installation mode
-       if ((!isBooleanConstantAndTrue('mxchange_installed')) || (!isBooleanConstantAndTrue('admin_registered'))) {
-               // Check for file permissions
-               if (!IS_INC_WRITEABLE("config")) {
-                       addFatalMessage(getMessage('CONFIG_IS_WRITE_PROTECTED'));
-               } // END - if
-               if (!IS_INC_WRITEABLE("dummy")) {
-                       addFatalMessage(getMessage('DUMMY_IS_WRITE_PROTECTED'));
-               } // END - if
-               if (!IS_INC_WRITEABLE(".secret/dummy")) {
-                       addFatalMessage(getMessage('SECRET_IS_WRITE_PROTECTED'));
-               } // END - if
+       // Are we installation routine?
+       if ((!isInstalling()) && (getOutputMode() != 1) && (getOutputMode() != -1)) {
+               // You have to install first!
+               redirectToUrl('install.php');
        } // END - if
+
+       // Init filter system here
+       initFilterSystem();
+
+       // Load cache
+       loadIncludeOnce('inc/load_cache.php');
+
+       // Run the init filter chain
+       runFilterChain('init');
 }
 
-if ((getTotalFatalErrors() > 0) && (isBooleanConstantAndTrue('mxchange_installed')) && (!defined('mxchange_installing')) && ($GLOBALS['output_mode'] != "1")) {
-       // One or more fatal error(s) occur during connect...
-       LOAD_INC_ONCE("inc/header.php");
-       LOAD_INC_ONCE("inc/fatal_errors.php");
-       LOAD_INC_ONCE("inc/footer.php");
-} // END - if
+// Handle fatal errors
+runFilterChain('handle_fatal_errors');
 
-//
+// [EOF]
 ?>