fdc059a8fb27904640f3cce2499486a0081d2199
[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         global $link;
87
88         // Call the filter chain 'shutdown'
89         RUN_FILTER('shutdown', null, false);
90
91         if (is_resource($link)) {
92                 // Close link
93                 SQL_CLOSE($link, __FILE__, __LINE__);
94         } else {
95                 // No database link
96                 addFatalMessage(getMessage('NO_DB_LINK'));
97         }
98 }
99
100 // Register shutdown hook
101 register_shutdown_function('__SHUTDOWN_HOOK');
102
103 // Check if the user setups his MySQL stuff...
104 if ((empty($MySQL['login'])) && (!defined('mxchange_installing')) && (!isset($_GET['installing'])) && (isBooleanConstantAndTrue('mxchange_installed'))) {
105         // No login entered and outside installation mode
106         OUTPUT_HTML("<strong>{--LANG_WARNING--}:</strong> ");
107         if (isBooleanConstantAndTrue('mxchange_installed')) {
108                 // You have changed my configuration file!
109                 mxchange_die("{--DIE_CONFIG_CHANGED_YOU--}");
110         } else {
111                 // Please run the installation script (maybe again)
112                 mxchange_die("{--DIE_RUN_INSTALL_MYSQL--}");
113         }
114 } elseif ((!isBooleanConstantAndTrue('mxchange_installing')) && (!isset($_GET['installing'])) && (empty($MySQL['password'])) && (isBooleanConstantAndTrue('warn_no_pass'))) {
115         // No database password entered!!!
116         OUTPUT_HTML("<div>{--LANG_WARNING--}:</div> {--WARN_NULL_PASSWORD--}");
117 }
118
119 // Set dummy $_CONFIG array
120 $_CONFIG = array(
121         'code_length' => 0,
122         'patch_level' => 0,
123         'last_update' => time()
124 );
125
126 // Set important header_sent
127 $GLOBALS['header_sent'] = 0;
128
129 // Init fatal messages
130 global $FATAL;
131 $FATAL = array();
132
133 // Check if this file is writeable or read-only and warn the user
134 if ((!isBooleanConstantAndTrue('mxchange_installing')) && (isBooleanConstantAndTrue('mxchange_installed'))) {
135         // Check for write-permission for config.php and inc directory
136         if (empty($GLOBALS['module'])) $GLOBALS['module'] = "index";
137
138         // CSS array
139         $EXT_CSS_FILES = array();
140
141         if ((!empty($MySQL['host'])) && (!empty($MySQL['login'])) && (!empty($MySQL['password'])) && (!empty($MySQL['dbase']))) {
142                 // Connect to DB
143                 global $link;
144                 $link = SQL_CONNECT($MySQL['host'], $MySQL['login'], $MySQL['password'], __FILE__, __LINE__);
145
146                 // Is the link valid?
147                 if (is_resource($link)) {
148                         // Choose the database
149                         global $db;
150                         $db = SQL_SELECT_DB($MySQL['dbase'], $link, __FILE__, __LINE__);
151
152                         // Is it a valid resource?
153                         if ($db === true) {
154                                 // This is required for extension 'optimize' to work
155                                 define('__DB_NAME', $MySQL['dbase']);
156
157                                 // Remove MySQL array from namespace
158                                 unset($MySQL);
159
160                                 // Load configuration stuff
161                                 mergeConfig(LOAD_CONFIG());
162
163                                 // Load "databases" aka static arrays
164                                 LOAD_INC_ONCE("inc/databases.php");
165
166                                 // Loading patching system is required here...
167                                 LOAD_INC_ONCE("inc/patch-system.php"); // Initialize patch system
168
169                                 // Session management
170                                 LOAD_INC_ONCE("inc/session.php");
171
172                                 // Run daily reset
173                                 LOAD_INC_ONCE("inc/check-reset.php");
174
175                                 // Load admin include file if he is admin
176                                 if (IS_ADMIN()) {
177                                         // Administrative functions
178                                         LOAD_INC_ONCE("inc/modules/admin/admin-inc.php");
179                                 } // END - if
180                                 //* DEBUG: */ ADD_POINTS_REFSYSTEM("test", 36, 1000);
181                                 //* DEBUG: */ die();
182
183                                 // Get all values
184                                 if (($GLOBALS['output_mode'] != 1) && ($GLOBALS['output_mode'] != -1)) {
185                                         if (empty($GLOBALS['module']))  $GLOBALS['module'] = "empty";
186                                         if (empty($GLOBALS['what']))    $GLOBALS['what']   = GET_WHAT($GLOBALS['module']);
187                                         if (empty($GLOBALS['action']))  $GLOBALS['action'] = GET_ACTION($GLOBALS['module'], $GLOBALS['what']);
188                                 } else {
189                                         // Set action/what to empty
190                                         $GLOBALS['action'] = "";
191                                         $GLOBALS['what']   = "";
192                                 }
193
194                                 // Run the init filter chain
195                                 RUN_FILTER('init');
196
197                                 // Set default 'what' value
198                                 //* DEBUG: */ OUTPUT_HTML("-".$GLOBALS['module']."/".$GLOBALS['what']."-<br />");
199                                 if ((empty($GLOBALS['what'])) && (empty($GLOBALS['action'])) && ($GLOBALS['output_mode'] != 1) && ($GLOBALS['output_mode'] != -1)) {
200                                         if ($GLOBALS['module'] == "admin") {
201                                                 // Set 'action' value to 'login' in admin menu
202                                                 $GLOBALS['action'] = GET_ACTION($GLOBALS['module'], $GLOBALS['what']);
203                                         } elseif (($GLOBALS['module'] == "index") || ($GLOBALS['module'] == "login")) {
204                                                 // Set 'what' value to 'welcome' in guest and member menu
205                                                 $GLOBALS['what'] = "welcome";
206                                                 if (getConfig('index_home') != "") $GLOBALS['what'] = getConfig('index_home');
207                                         } else {
208                                                 // Anything else like begging link
209                                                 $GLOBALS['what'] = "";
210                                         }
211                                 } // END - if
212
213                                 // Update sending pool
214                                 if (($GLOBALS['output_mode'] != "1") && ($GLOBALS['output_mode'] != "-1")) LOAD_INC_ONCE("inc/pool-update.php"); // Sends out mails in configureable steps
215
216                                 // Load all active extension including language files when not upgrading.
217                                 // Check module for testing and count one click
218                                 $dummy = CHECK_MODULE($GLOBALS['module']);
219                                 if ($dummy == "done") COUNT_MODULE($GLOBALS['module']);
220                                 unset($dummy);
221
222                                 // Shall we activate the exchange?
223                                 if (getConfig('activate_xchange') > 0) activateExchange();
224
225                                 // Is the extension sql_patches installed and at least 0.3.6?
226                                 if (GET_EXT_VERSION("sql_patches") >= "0.3.6") {
227                                         // Generate random number
228                                         if (isset($GLOBALS['userid'])) {
229                                                 define('RAND_NUMBER', GEN_RANDOM_CODE(10, mt_rand(10000,32766), $GLOBALS['userid'], ""));
230                                         } else {
231                                                 define('RAND_NUMBER', GEN_RANDOM_CODE(10, mt_rand(10000,32766), 0, ""));
232                                         }
233                                 } else {
234                                         // Generate weak (!!!) code
235                                         define('RAND_NUMBER', mt_rand(1000000, 9999999));
236                                 }
237                         } else {
238                                 // Add language system
239                                 LOAD_INC_ONCE("inc/language.php");
240
241                                 // Wrong database?
242                                 addFatalMessage(getMessage('WRONG_DB_SELECTED'));
243                         }
244                 } else {
245                         // Add language system
246                         LOAD_INC_ONCE("inc/language.php");
247
248                         // No link to database!
249                         addFatalMessage(getMessage('NO_DB_LINK'));
250                         $db = false;
251                 }
252         } else {
253                 // Add language system
254                 LOAD_INC_ONCE("inc/language.php");
255
256                 // Maybe you forgot to enter your MySQL data?
257                 addFatalMessage(getMessage('MYSQL_DATA_MISSING'));
258         }
259 } else {
260         ///////////////////////////////////////////////////
261         // Include neccessary functions for installation //
262         ///////////////////////////////////////////////////
263
264         // Set other missing variables
265         if (!isset($GLOBALS['output_mode'])) $GLOBALS['output_mode'] = "0";
266         $link = false; // No database link by default
267
268         // Include required files
269         LOAD_INC_ONCE("inc/databases.php");
270         LOAD_INC_ONCE("inc/session.php");
271
272         // Check if we are in installation routine
273         if ((basename($_SERVER['PHP_SELF']) != "install.php") && ($GLOBALS['output_mode'] != "1") && ($GLOBALS['output_mode'] != -1)) {
274                 // Redirect to the installation system
275                 LOAD_URL("install.php");
276         } // END - if
277
278         // Double-check installation mode
279         if ((!isBooleanConstantAndTrue('mxchange_installed')) || (!isBooleanConstantAndTrue('admin_registered'))) {
280                 // Check for file permissions
281                 if (!IS_INC_WRITEABLE("config")) {
282                         addFatalMessage(getMessage('CONFIG_IS_WRITE_PROTECTED'));
283                 } // END - if
284                 if (!IS_INC_WRITEABLE("dummy")) {
285                         addFatalMessage(getMessage('DUMMY_IS_WRITE_PROTECTED'));
286                 } // END - if
287                 if (!IS_INC_WRITEABLE(".secret/dummy")) {
288                         addFatalMessage(getMessage('SECRET_IS_WRITE_PROTECTED'));
289                 } // END - if
290         } // END - if
291 }
292
293 if ((getTotalFatalErrors() > 0) && (isBooleanConstantAndTrue('mxchange_installed')) && (!defined('mxchange_installing')) && ($GLOBALS['output_mode'] != "1")) {
294         // One or more fatal error(s) occur during connect...
295         LOAD_INC_ONCE("inc/header.php");
296         LOAD_INC_ONCE("inc/fatal_errors.php");
297         LOAD_INC_ONCE("inc/footer.php");
298 } // END - if
299
300 //
301 ?>