More globals rewritten, see #100
[mailer.git] / inc / mysql-connect.php
1 <?php
2 /************************************************************************
3  * MXChange v0.2.1                                    Start: 11/16/2003 *
4  * ===============                              Last change: 12/13/2004 *
5  *                                                                      *
6  * -------------------------------------------------------------------- *
7  * File              : mysql-connect.php                                *
8  * -------------------------------------------------------------------- *
9  * Short description : Connects to your database                        *
10  * -------------------------------------------------------------------- *
11  * Kurzbeschreibung  : Verbindet zu Ihrer Datenbank                     *
12  * -------------------------------------------------------------------- *
13  *                                                                      *
14  * -------------------------------------------------------------------- *
15  * Copyright (c) 2003 - 2008 by Roland Haeder                           *
16  * For more information visit: http://www.mxchange.org                  *
17  *                                                                      *
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.                                  *
22  *                                                                      *
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.                         *
27  *                                                                      *
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,               *
31  * MA  02110-1301  USA                                                  *
32  ************************************************************************/
33
34 // Some security stuff...
35 if (!defined('__SECURITY')) {
36         $INC = substr(dirname(__FILE__), 0, strpos(dirname(__FILE__), "/inc") + 4) . "/security.php";
37         require($INC);
38 }
39
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);
42
43 // Non-database functions
44 require("inc/functions.php");
45
46 // Filter functions
47 LOAD_INC_ONCE("inc/filters.php");  
48
49 // Functions which interact with the database
50 LOAD_INC_ONCE("inc/mysql-manager.php");
51
52 // Load extensions and language
53 LOAD_INC_ONCE("inc/extensions.php");
54
55 // Load database library
56 LOAD_INC_ONCE("inc/db/lib.php");
57
58 // Error handler function
59 function __errorHandler ($errno, $errstr, $errfile, $errline) {
60         // Construct message
61         $msg = sprintf("errno=%s,errstr=%s,errfile=%s,errline=%s",
62                 $errno,
63                 $errstr,
64                 basename($errfile),
65                 $errline
66         );
67
68         // Write debug log message
69         DEBUG_LOG(__FUNCTION__, __LINE__, "".$msg, true);
70
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.");
75         } else {
76                 // No debug extension found, so regular output
77                 debug_report_bug($msg);
78         }
79 }
80
81 // Set error handler
82 set_error_handler('__errorHandler');
83
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);
88
89         if (SQL_IS_LINK_UP()) {
90                 // Close link
91                 SQL_CLOSE(__FILE__, __LINE__);
92         } else {
93                 // No database link
94                 addFatalMessage(getMessage('NO_DB_LINK'));
95         }
96 }
97
98 // Register shutdown hook
99 register_shutdown_function('__SHUTDOWN_HOOK');
100
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--}");
108         } else {
109                 // Please run the installation script (maybe again)
110                 mxchange_die("{--DIE_RUN_INSTALL_MYSQL--}");
111         }
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--}");
115 }
116
117 // Set dummy $_CONFIG array
118 $_CONFIG = array(
119         'code_length' => 0,
120         'patch_level' => 0,
121         'last_update' => time()
122 );
123
124 // Set important header_sent
125 $GLOBALS['header_sent'] = 0;
126
127 // Init fatal messages
128 initFatalMessages();
129
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";
134
135         // CSS array
136         EXT_INIT_CSS_FILES();
137
138         if ((!empty($MySQL['host'])) && (!empty($MySQL['login'])) && (!empty($MySQL['password'])) && (!empty($MySQL['dbase']))) {
139                 // Connect to DB
140                 SQL_CONNECT($MySQL['host'], $MySQL['login'], $MySQL['password'], __FILE__, __LINE__);
141
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']);
148
149                                 // Remove MySQL array from namespace
150                                 unset($MySQL);
151
152                                 // Load configuration stuff
153                                 mergeConfig(LOAD_CONFIG());
154
155                                 // Load "databases" aka static arrays
156                                 LOAD_INC_ONCE("inc/databases.php");
157
158                                 // Loading patching system is required here...
159                                 LOAD_INC_ONCE("inc/patch-system.php"); // Initialize patch system
160
161                                 // Session management
162                                 LOAD_INC_ONCE("inc/session.php");
163
164                                 // Run daily reset
165                                 LOAD_INC_ONCE("inc/check-reset.php");
166
167                                 // Load admin include file if he is admin
168                                 if (IS_ADMIN()) {
169                                         // Administrative functions
170                                         LOAD_INC_ONCE("inc/modules/admin/admin-inc.php");
171                                 } // END - if
172                                 //* DEBUG: */ ADD_POINTS_REFSYSTEM("test", 36, 1000);
173                                 //* DEBUG: */ die();
174
175                                 // Get all values
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']);
180                                 } else {
181                                         // Set action/what to empty
182                                         $GLOBALS['action'] = "";
183                                         $GLOBALS['what']   = "";
184                                 }
185
186                                 // Run the init filter chain
187                                 RUN_FILTER('init');
188
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');
199                                         } else {
200                                                 // Anything else like begging link
201                                                 $GLOBALS['what'] = "";
202                                         }
203                                 } // END - if
204
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
207
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']);
212                                 unset($dummy);
213
214                                 // Shall we activate the exchange?
215                                 if (getConfig('activate_xchange') > 0) activateExchange();
216
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'], ""));
222                                         } else {
223                                                 define('RAND_NUMBER', GEN_RANDOM_CODE(10, mt_rand(10000,32766), 0, ""));
224                                         }
225                                 } else {
226                                         // Generate weak (!!!) code
227                                         define('RAND_NUMBER', mt_rand(1000000, 9999999));
228                                 }
229                         } else {
230                                 // Add language system
231                                 LOAD_INC_ONCE("inc/language.php");
232
233                                 // Wrong database?
234                                 addFatalMessage(getMessage('WRONG_DB_SELECTED'));
235                         }
236                 } else {
237                         // Add language system
238                         LOAD_INC_ONCE("inc/language.php");
239
240                         // No link to database!
241                         addFatalMessage(getMessage('NO_DB_LINK'));
242                 }
243         } else {
244                 // Add language system
245                 LOAD_INC_ONCE("inc/language.php");
246
247                 // Maybe you forgot to enter your MySQL data?
248                 addFatalMessage(getMessage('MYSQL_DATA_MISSING'));
249         }
250 } else {
251         ///////////////////////////////////////////////////
252         // Include neccessary functions for installation //
253         ///////////////////////////////////////////////////
254
255         // Set other missing variables
256         if (!isset($GLOBALS['output_mode'])) $GLOBALS['output_mode'] = "0";
257
258         // Include required files
259         LOAD_INC_ONCE("inc/databases.php");
260         LOAD_INC_ONCE("inc/session.php");
261
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");
266         } // END - if
267
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'));
273                 } // END - if
274                 if (!IS_INC_WRITEABLE("dummy")) {
275                         addFatalMessage(getMessage('DUMMY_IS_WRITE_PROTECTED'));
276                 } // END - if
277                 if (!IS_INC_WRITEABLE(".secret/dummy")) {
278                         addFatalMessage(getMessage('SECRET_IS_WRITE_PROTECTED'));
279                 } // END - if
280         } // END - if
281 }
282
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");
288 } // END - if
289
290 //
291 ?>