2 /************************************************************************
3 * MXChange v0.2.1 Start: 11/16/2003 *
4 * =============== Last change: 12/13/2004 *
6 * -------------------------------------------------------------------- *
7 * File : mysql-connect.php *
8 * -------------------------------------------------------------------- *
9 * Short description : Connects to your database *
10 * -------------------------------------------------------------------- *
11 * Kurzbeschreibung : Verbindet zu Ihrer Datenbank *
12 * -------------------------------------------------------------------- *
14 * -------------------------------------------------------------------- *
15 * Copyright (c) 2003 - 2008 by Roland Haeder *
16 * For more information visit: http://www.mxchange.org *
18 * This program is free software; you can redistribute it and/or modify *
19 * it under the terms of the GNU General Public License as published by *
20 * the Free Software Foundation; either version 2 of the License, or *
21 * (at your option) any later version. *
23 * This program is distributed in the hope that it will be useful, *
24 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
25 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
26 * GNU General Public License for more details. *
28 * You should have received a copy of the GNU General Public License *
29 * along with this program; if not, write to the Free Software *
30 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, *
32 ************************************************************************/
34 // Some security stuff...
35 if (!defined('__SECURITY')) {
36 $INC = substr(dirname(__FILE__), 0, strpos(dirname(__FILE__), "/inc") + 4) . "/security.php";
40 // CFG: DEBUG-SQL (if enabled and DEBUG_MODE is enabled all SQL queries will be logged to debug.log)
41 define('DEBUG_SQL', false);
43 // Non-database functions
44 require("inc/functions.php");
47 LOAD_INC_ONCE("inc/filters.php");
49 // Functions which interact with the database
50 LOAD_INC_ONCE("inc/mysql-manager.php");
52 // Load extensions and language
53 LOAD_INC_ONCE("inc/extensions.php");
55 // Load database library
56 LOAD_INC_ONCE("inc/db/lib.php");
58 // Error handler function
59 function __errorHandler ($errno, $errstr, $errfile, $errline) {
61 $msg = sprintf("errno=%s,errstr=%s,errfile=%s,errline=%s",
68 // Write debug log message
69 DEBUG_LOG(__FUNCTION__, __LINE__, "".$msg, true);
71 // Output message to user and die
72 if (EXT_IS_ACTIVE("debug")) {
73 // Debug extension found! So Output a small message
74 mxchange_die("Error message written to debug.log. Please try to call <a href=\"{!URL!}\">the main page</a> to continue.");
76 // No debug extension found, so regular output
77 debug_report_bug($msg);
82 set_error_handler('__errorHandler');
84 // Call-back function for running shutdown functions and close database connection
85 function __SHUTDOWN_HOOK () {
86 // Call the filter chain 'shutdown'
87 RUN_FILTER('shutdown', null, false);
89 if (SQL_IS_LINK_UP()) {
91 SQL_CLOSE(__FILE__, __LINE__);
94 addFatalMessage(getMessage('NO_DB_LINK'));
98 // Register shutdown hook
99 register_shutdown_function('__SHUTDOWN_HOOK');
101 // Check if the user setups his MySQL stuff...
102 if ((empty($MySQL['login'])) && (!defined('mxchange_installing')) && (!isset($_GET['installing'])) && (isBooleanConstantAndTrue('mxchange_installed'))) {
103 // No login entered and outside installation mode
104 OUTPUT_HTML("<strong>{--LANG_WARNING--}:</strong> ");
105 if (isBooleanConstantAndTrue('mxchange_installed')) {
106 // You have changed my configuration file!
107 mxchange_die("{--DIE_CONFIG_CHANGED_YOU--}");
109 // Please run the installation script (maybe again)
110 mxchange_die("{--DIE_RUN_INSTALL_MYSQL--}");
112 } elseif ((!isBooleanConstantAndTrue('mxchange_installing')) && (!isset($_GET['installing'])) && (empty($MySQL['password'])) && (isBooleanConstantAndTrue('warn_no_pass'))) {
113 // No database password entered!!!
114 OUTPUT_HTML("<div>{--LANG_WARNING--}:</div> {--WARN_NULL_PASSWORD--}");
117 // Set dummy $_CONFIG array
121 'last_update' => time()
124 // Set important header_sent
125 $GLOBALS['header_sent'] = 0;
127 // Init fatal messages
130 // Check if this file is writeable or read-only and warn the user
131 if ((!isBooleanConstantAndTrue('mxchange_installing')) && (isBooleanConstantAndTrue('mxchange_installed'))) {
132 // Check for write-permission for config.php and inc directory
133 if (empty($GLOBALS['module'])) $GLOBALS['module'] = "index";
136 EXT_INIT_CSS_FILES();
138 if ((!empty($MySQL['host'])) && (!empty($MySQL['login'])) && (!empty($MySQL['password'])) && (!empty($MySQL['dbase']))) {
140 SQL_CONNECT($MySQL['host'], $MySQL['login'], $MySQL['password'], __FILE__, __LINE__);
142 // Is the link valid?
143 if (SQL_IS_LINK_UP()) {
144 // Is it a valid resource?
145 if (SQL_SELECT_DB($MySQL['dbase'], __FILE__, __LINE__) === true) {
146 // This is required for extension 'optimize' to work
147 define('__DB_NAME', $MySQL['dbase']);
149 // Remove MySQL array from namespace
152 // Load configuration stuff
153 mergeConfig(LOAD_CONFIG());
155 // Load "databases" aka static arrays
156 LOAD_INC_ONCE("inc/databases.php");
158 // Loading patching system is required here...
159 LOAD_INC_ONCE("inc/patch-system.php"); // Initialize patch system
161 // Session management
162 LOAD_INC_ONCE("inc/session.php");
165 LOAD_INC_ONCE("inc/check-reset.php");
167 // Load admin include file if he is admin
169 // Administrative functions
170 LOAD_INC_ONCE("inc/modules/admin/admin-inc.php");
172 //* DEBUG: */ ADD_POINTS_REFSYSTEM("test", 36, 1000);
176 if (($GLOBALS['output_mode'] != 1) && ($GLOBALS['output_mode'] != -1)) {
177 if (empty($GLOBALS['module'])) $GLOBALS['module'] = "empty";
178 if (empty($GLOBALS['what'])) $GLOBALS['what'] = GET_WHAT($GLOBALS['module']);
179 if (empty($GLOBALS['action'])) $GLOBALS['action'] = GET_ACTION($GLOBALS['module'], $GLOBALS['what']);
181 // Set action/what to empty
182 $GLOBALS['action'] = "";
183 $GLOBALS['what'] = "";
186 // Run the init filter chain
189 // Set default 'what' value
190 //* DEBUG: */ OUTPUT_HTML("-".$GLOBALS['module']."/".$GLOBALS['what']."-<br />");
191 if ((empty($GLOBALS['what'])) && (empty($GLOBALS['action'])) && ($GLOBALS['output_mode'] != 1) && ($GLOBALS['output_mode'] != -1)) {
192 if ($GLOBALS['module'] == "admin") {
193 // Set 'action' value to 'login' in admin menu
194 $GLOBALS['action'] = GET_ACTION($GLOBALS['module'], $GLOBALS['what']);
195 } elseif (($GLOBALS['module'] == "index") || ($GLOBALS['module'] == "login")) {
196 // Set 'what' value to 'welcome' in guest and member menu
197 $GLOBALS['what'] = "welcome";
198 if (getConfig('index_home') != "") $GLOBALS['what'] = getConfig('index_home');
200 // Anything else like begging link
201 $GLOBALS['what'] = "";
205 // Update sending pool
206 if (($GLOBALS['output_mode'] != "1") && ($GLOBALS['output_mode'] != "-1")) LOAD_INC_ONCE("inc/pool-update.php"); // Sends out mails in configureable steps
208 // Load all active extension including language files when not upgrading.
209 // Check module for testing and count one click
210 $dummy = CHECK_MODULE($GLOBALS['module']);
211 if ($dummy == "done") COUNT_MODULE($GLOBALS['module']);
214 // Shall we activate the exchange?
215 if (getConfig('activate_xchange') > 0) activateExchange();
217 // Is the extension sql_patches installed and at least 0.3.6?
218 if (GET_EXT_VERSION("sql_patches") >= "0.3.6") {
219 // Generate random number
220 if (isset($GLOBALS['userid'])) {
221 define('RAND_NUMBER', GEN_RANDOM_CODE(10, mt_rand(10000,32766), $GLOBALS['userid'], ""));
223 define('RAND_NUMBER', GEN_RANDOM_CODE(10, mt_rand(10000,32766), 0, ""));
226 // Generate weak (!!!) code
227 define('RAND_NUMBER', mt_rand(1000000, 9999999));
230 // Add language system
231 LOAD_INC_ONCE("inc/language.php");
234 addFatalMessage(getMessage('WRONG_DB_SELECTED'));
237 // Add language system
238 LOAD_INC_ONCE("inc/language.php");
240 // No link to database!
241 addFatalMessage(getMessage('NO_DB_LINK'));
244 // Add language system
245 LOAD_INC_ONCE("inc/language.php");
247 // Maybe you forgot to enter your MySQL data?
248 addFatalMessage(getMessage('MYSQL_DATA_MISSING'));
251 ///////////////////////////////////////////////////
252 // Include neccessary functions for installation //
253 ///////////////////////////////////////////////////
255 // Set other missing variables
256 if (!isset($GLOBALS['output_mode'])) $GLOBALS['output_mode'] = "0";
258 // Include required files
259 LOAD_INC_ONCE("inc/databases.php");
260 LOAD_INC_ONCE("inc/session.php");
262 // Check if we are in installation routine
263 if ((basename($_SERVER['PHP_SELF']) != "install.php") && ($GLOBALS['output_mode'] != "1") && ($GLOBALS['output_mode'] != -1)) {
264 // Redirect to the installation system
265 LOAD_URL("install.php");
268 // Double-check installation mode
269 if ((!isBooleanConstantAndTrue('mxchange_installed')) || (!isBooleanConstantAndTrue('admin_registered'))) {
270 // Check for file permissions
271 if (!IS_INC_WRITEABLE("config")) {
272 addFatalMessage(getMessage('CONFIG_IS_WRITE_PROTECTED'));
274 if (!IS_INC_WRITEABLE("dummy")) {
275 addFatalMessage(getMessage('DUMMY_IS_WRITE_PROTECTED'));
277 if (!IS_INC_WRITEABLE(".secret/dummy")) {
278 addFatalMessage(getMessage('SECRET_IS_WRITE_PROTECTED'));
283 if ((getTotalFatalErrors() > 0) && (isBooleanConstantAndTrue('mxchange_installed')) && (!defined('mxchange_installing')) && ($GLOBALS['output_mode'] != "1")) {
284 // One or more fatal error(s) occur during connect...
285 LOAD_INC_ONCE("inc/header.php");
286 LOAD_INC_ONCE("inc/fatal_errors.php");
287 LOAD_INC_ONCE("inc/footer.php");