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