d3418fde37818f0d4680a236cd51a217d5a900f8
[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 // Load more function libraries
47 foreach (array('request-functions', 'session-functions', 'config-functions', 'filters', 'mysql-manager', 'extensions', 'db/lib', 'handler', 'hooks') as $lib) {
48         // Load special functions
49         LOAD_INC_ONCE(sprintf("inc/%s.php", $lib));
50 } // END - foreach
51
52 // Set error handler
53 set_error_handler('__errorHandler');
54
55 // Register shutdown hook
56 register_shutdown_function('__SHUTDOWN_HOOK');
57
58 // Check if the user setups his MySQL stuff...
59 if ((empty($MySQL['login'])) && (!defined('mxchange_installing')) && (!REQUEST_ISSET_GET(('installing'))) && (isBooleanConstantAndTrue('mxchange_installed'))) {
60         // No login entered and outside installation mode
61         OUTPUT_HTML("<strong>{--LANG_WARNING--}:</strong> ");
62         if (isBooleanConstantAndTrue('mxchange_installed')) {
63                 // You have changed my configuration file!
64                 mxchange_die("{--DIE_CONFIG_CHANGED_YOU--}");
65         } else {
66                 // Please run the installation script (maybe again)
67                 mxchange_die("{--DIE_RUN_INSTALL_MYSQL--}");
68         }
69 } elseif ((!isBooleanConstantAndTrue('mxchange_installing')) && (!REQUEST_ISSET_GET(('installing'))) && (empty($MySQL['password'])) && (isBooleanConstantAndTrue('warn_no_pass'))) {
70         // No database password entered!!!
71         OUTPUT_HTML("<div>{--LANG_WARNING--}:</div> {--WARN_NULL_PASSWORD--}");
72 }
73
74 // Set dummy $_CONFIG array
75 $_CONFIG = array(
76         'code_length' => 0,
77         'patch_level' => 0,
78         'last_update' => time()
79 );
80
81 // Set important header_sent
82 $GLOBALS['header_sent'] = 0;
83
84 // Init fatal messages
85 initFatalMessages();
86
87 // Check if this file is writeable or read-only and warn the user
88 if ((!isBooleanConstantAndTrue('mxchange_installing')) && (isBooleanConstantAndTrue('mxchange_installed'))) {
89         // Check for write-permission for config.php and inc directory
90         if (empty($GLOBALS['module'])) $GLOBALS['module'] = "index";
91
92         // CSS array
93         EXT_INIT_CSS_FILES();
94
95         if ((!empty($MySQL['host'])) && (!empty($MySQL['login'])) && (!empty($MySQL['password'])) && (!empty($MySQL['dbase']))) {
96                 // Connect to DB
97                 SQL_CONNECT($MySQL['host'], $MySQL['login'], $MySQL['password'], __FILE__, __LINE__);
98
99                 // Is the link valid?
100                 if (SQL_IS_LINK_UP()) {
101                         // Is it a valid resource?
102                         if (SQL_SELECT_DB($MySQL['dbase'], __FILE__, __LINE__) === true) {
103                                 // This is required for extension 'optimize' to work
104                                 define('__DB_NAME', $MySQL['dbase']);
105
106                                 // Remove MySQL array from namespace
107                                 unset($MySQL);
108
109                                 // Load configuration stuff
110                                 mergeConfig(LOAD_CONFIG());
111
112                                 // Load "databases" aka static arrays
113                                 LOAD_INC_ONCE("inc/databases.php");
114
115                                 // Loading patching system is required here...
116                                 LOAD_INC_ONCE("inc/patch-system.php"); // Initialize patch system
117
118                                 // Session management
119                                 LOAD_INC_ONCE("inc/session.php");
120
121                                 // Run daily reset
122                                 LOAD_INC_ONCE("inc/check-reset.php");
123
124                                 // Load admin include file if he is admin
125                                 if (IS_ADMIN()) {
126                                         // Administrative functions
127                                         LOAD_INC_ONCE("inc/modules/admin/admin-inc.php");
128                                 } // END - if
129                                 //* DEBUG: */ ADD_POINTS_REFSYSTEM("test", 36, 1000);
130                                 //* DEBUG: */ die();
131
132                                 // Get all values
133                                 if (($GLOBALS['output_mode'] != 1) && ($GLOBALS['output_mode'] != -1)) {
134                                         if (empty($GLOBALS['module']))  $GLOBALS['module'] = "empty";
135                                         if (empty($GLOBALS['what']))    $GLOBALS['what']   = GET_WHAT($GLOBALS['module']);
136                                         if (empty($GLOBALS['action']))  $GLOBALS['action'] = GET_ACTION($GLOBALS['module'], $GLOBALS['what']);
137                                 } else {
138                                         // Set action/what to empty
139                                         $GLOBALS['action'] = "";
140                                         $GLOBALS['what']   = "";
141                                 }
142
143                                 // Run the init filter chain
144                                 RUN_FILTER('init');
145
146                                 // Set default 'what' value
147                                 //* DEBUG: */ OUTPUT_HTML("-".$GLOBALS['module']."/".$GLOBALS['what']."-<br />");
148                                 if ((empty($GLOBALS['what'])) && (empty($GLOBALS['action'])) && ($GLOBALS['output_mode'] != 1) && ($GLOBALS['output_mode'] != -1)) {
149                                         if ($GLOBALS['module'] == "admin") {
150                                                 // Set 'action' value to 'login' in admin menu
151                                                 $GLOBALS['action'] = GET_ACTION($GLOBALS['module'], $GLOBALS['what']);
152                                         } elseif (($GLOBALS['module'] == "index") || ($GLOBALS['module'] == "login")) {
153                                                 // Set 'what' value to 'welcome' in guest and member menu
154                                                 $GLOBALS['what'] = "welcome";
155                                                 if (getConfig('index_home') != "") $GLOBALS['what'] = getConfig('index_home');
156                                         } else {
157                                                 // Anything else like begging link
158                                                 $GLOBALS['what'] = "";
159                                         }
160                                 } // END - if
161
162                                 // Update sending pool
163                                 if (($GLOBALS['output_mode'] != "1") && ($GLOBALS['output_mode'] != "-1")) LOAD_INC_ONCE("inc/pool-update.php"); // Sends out mails in configureable steps
164
165                                 // Load all active extension including language files when not upgrading.
166                                 // Check module for testing and count one click
167                                 $dummy = CHECK_MODULE($GLOBALS['module']);
168                                 if ($dummy == "done") COUNT_MODULE($GLOBALS['module']);
169                                 unset($dummy);
170
171                                 // Shall we activate the exchange?
172                                 if (getConfig('activate_xchange') > 0) activateExchange();
173
174                                 // Is the extension sql_patches installed and at least 0.3.6?
175                                 if (GET_EXT_VERSION("sql_patches") >= "0.3.6") {
176                                         // Generate random number
177                                         if (isset($GLOBALS['userid'])) {
178                                                 define('RAND_NUMBER', GEN_RANDOM_CODE(10, mt_rand(10000,32766), $GLOBALS['userid'], ""));
179                                         } else {
180                                                 define('RAND_NUMBER', GEN_RANDOM_CODE(10, mt_rand(10000,32766), 0, ""));
181                                         }
182                                 } else {
183                                         // Generate weak (!!!) code
184                                         define('RAND_NUMBER', mt_rand(1000000, 9999999));
185                                 }
186                         } else {
187                                 // Add language system
188                                 LOAD_INC_ONCE("inc/language.php");
189
190                                 // Wrong database?
191                                 addFatalMessage(getMessage('WRONG_DB_SELECTED'));
192                         }
193                 } else {
194                         // Add language system
195                         LOAD_INC_ONCE("inc/language.php");
196
197                         // No link to database!
198                         addFatalMessage(getMessage('NO_DB_LINK'));
199                 }
200         } else {
201                 // Add language system
202                 LOAD_INC_ONCE("inc/language.php");
203
204                 // Maybe you forgot to enter your MySQL data?
205                 addFatalMessage(getMessage('MYSQL_DATA_MISSING'));
206         }
207 } else {
208         ///////////////////////////////////////////////////
209         // Include neccessary functions for installation //
210         ///////////////////////////////////////////////////
211
212         // Set other missing variables
213         if (!isset($GLOBALS['output_mode'])) $GLOBALS['output_mode'] = "0";
214
215         // Include required files
216         LOAD_INC_ONCE("inc/databases.php");
217         LOAD_INC_ONCE("inc/session.php");
218
219         // Check if we are in installation routine
220         if ((basename($_SERVER['PHP_SELF']) != "install.php") && ($GLOBALS['output_mode'] != "1") && ($GLOBALS['output_mode'] != -1)) {
221                 // Redirect to the installation system
222                 LOAD_URL("install.php");
223         } // END - if
224
225         // Double-check installation mode
226         if ((!isBooleanConstantAndTrue('mxchange_installed')) || (!isBooleanConstantAndTrue('admin_registered'))) {
227                 // Check for file permissions
228                 if (!IS_INC_WRITEABLE("config")) {
229                         addFatalMessage(getMessage('CONFIG_IS_WRITE_PROTECTED'));
230                 } // END - if
231                 if (!IS_INC_WRITEABLE("dummy")) {
232                         addFatalMessage(getMessage('DUMMY_IS_WRITE_PROTECTED'));
233                 } // END - if
234                 if (!IS_INC_WRITEABLE(".secret/dummy")) {
235                         addFatalMessage(getMessage('SECRET_IS_WRITE_PROTECTED'));
236                 } // END - if
237         } // END - if
238 }
239
240 if ((getTotalFatalErrors() > 0) && (isBooleanConstantAndTrue('mxchange_installed')) && (!defined('mxchange_installing')) && ($GLOBALS['output_mode'] != "1")) {
241         // One or more fatal error(s) occur during connect...
242         LOAD_INC_ONCE("inc/header.php");
243         LOAD_INC_ONCE("inc/fatal_errors.php");
244         LOAD_INC_ONCE("inc/footer.php");
245 } // END - if
246
247 //
248 ?>