Fix for installation
[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 or includes
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'))) && (isInstalled())) {
60         // No login entered and outside installation mode
61         OUTPUT_HTML("<strong>{--LANG_WARNING--}:</strong> ");
62         if (isInstalled()) {
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 ((!isInstalling()) && (!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 initConfig();
76
77 // Set important header_sent
78 $GLOBALS['header_sent'] = 0;
79
80 // Init fatal messages
81 initFatalMessages();
82
83 // Check if this file is writeable or read-only and warn the user
84 if ((!isInstalling()) && (isInstalled())) {
85         // Check for write-permission for config.php and inc directory
86         if (empty($GLOBALS['module'])) $GLOBALS['module'] = REQUEST_GET('module');
87         if (empty($GLOBALS['module'])) $GLOBALS['module'] = "index";
88
89         // CSS array
90         EXT_INIT_CSS_FILES();
91
92         if ((!empty($MySQL['host'])) && (!empty($MySQL['login'])) && (!empty($MySQL['password'])) && (!empty($MySQL['dbase']))) {
93                 // Connect to DB
94                 SQL_CONNECT($MySQL['host'], $MySQL['login'], $MySQL['password'], __FILE__, __LINE__);
95
96                 // Is the link valid?
97                 if (SQL_IS_LINK_UP()) {
98                         // Is it a valid resource?
99                         if (SQL_SELECT_DB($MySQL['dbase'], __FILE__, __LINE__) === true) {
100                                 // This is required for extension 'optimize' to work
101                                 define('__DB_NAME', $MySQL['dbase']);
102
103                                 // Remove MySQL array from namespace
104                                 unset($MySQL);
105
106                                 // Load configuration stuff
107                                 loadConfiguration();
108
109                                 // Init session
110                                 LOAD_INC_ONCE("inc/session.php");
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                                 // Run daily reset
119                                 LOAD_INC_ONCE("inc/check-reset.php");
120
121                                 // Load admin include file if he is admin
122                                 if (IS_ADMIN()) {
123                                         // Administrative functions
124                                         LOAD_INC_ONCE("inc/modules/admin/admin-inc.php");
125                                 } // END - if
126                                 //* DEBUG: */ ADD_POINTS_REFSYSTEM("test", 36, 1000);
127                                 //* DEBUG: */ die();
128
129                                 // Get all values
130                                 if (($GLOBALS['output_mode'] != 1) && ($GLOBALS['output_mode'] != -1)) {
131                                         if (empty($GLOBALS['module']))  $GLOBALS['module'] = "empty";
132                                         if (empty($GLOBALS['what']))    $GLOBALS['what']   = GET_WHAT($GLOBALS['module']);
133                                         if (empty($GLOBALS['action']))  $GLOBALS['action'] = GET_ACTION($GLOBALS['module'], $GLOBALS['what']);
134                                 } else {
135                                         // Set action/what to empty
136                                         $GLOBALS['action'] = "";
137                                         $GLOBALS['what']   = "";
138                                 }
139
140                                 // Run the init filter chain
141                                 runFilterChain('init');
142
143                                 // Set default 'what' value
144                                 //* DEBUG: */ OUTPUT_HTML("-".$GLOBALS['module']."/".$GLOBALS['what']."-<br />");
145                                 if ((empty($GLOBALS['what'])) && (empty($GLOBALS['action'])) && ($GLOBALS['output_mode'] != 1) && ($GLOBALS['output_mode'] != -1)) {
146                                         if ($GLOBALS['module'] == "admin") {
147                                                 // Set 'action' value to 'login' in admin menu
148                                                 $GLOBALS['action'] = GET_ACTION($GLOBALS['module'], $GLOBALS['what']);
149                                         } elseif (($GLOBALS['module'] == "index") || ($GLOBALS['module'] == "login")) {
150                                                 // Set 'what' value to 'welcome' in guest and member menu
151                                                 $GLOBALS['what'] = "welcome";
152                                                 if (getConfig('index_home') != "") $GLOBALS['what'] = getConfig('index_home');
153                                         } else {
154                                                 // Anything else like begging link
155                                                 $GLOBALS['what'] = "";
156                                         }
157                                 } // END - if
158
159                                 // Update sending pool
160                                 if (($GLOBALS['output_mode'] != "1") && ($GLOBALS['output_mode'] != "-1")) LOAD_INC_ONCE("inc/pool-update.php"); // Sends out mails in configureable steps
161
162                                 // Load all active extension including language files when not upgrading.
163                                 // Check module for testing and count one click
164                                 $dummy = checkModulePermissions($GLOBALS['module']);
165                                 if ($dummy == "done") countModuleHit($GLOBALS['module']);
166                                 unset($dummy);
167
168                                 // Shall we activate the exchange?
169                                 if (getConfig('activate_xchange') > 0) activateExchange();
170
171                                 // Is the extension sql_patches installed and at least 0.3.6?
172                                 if (GET_EXT_VERSION("sql_patches") >= "0.3.6") {
173                                         // Generate random number
174                                         if (isset($GLOBALS['userid'])) {
175                                                 define('RAND_NUMBER', generateRandomCodde(10, mt_rand(10000,32766), $GLOBALS['userid'], ""));
176                                         } else {
177                                                 define('RAND_NUMBER', generateRandomCodde(10, mt_rand(10000,32766), 0, ""));
178                                         }
179                                 } else {
180                                         // Generate weak (!!!) code
181                                         define('RAND_NUMBER', mt_rand(1000000, 9999999));
182                                 }
183                         } else {
184                                 // Wrong database?
185                                 addFatalMessage(__FILE__, __LINE__, getMessage('WRONG_DB_SELECTED'));
186                         }
187                 } else {
188                         // No link to database!
189                         addFatalMessage(__FILE__, __LINE__, getMessage('NO_DB_LINK'));
190                 }
191         } else {
192                 // Maybe you forgot to enter your MySQL data?
193                 addFatalMessage(__FILE__, __LINE__, getMessage('MYSQL_DATA_MISSING'));
194         }
195 } else {
196         ///////////////////////////////////////////////////
197         // Include neccessary functions for installation //
198         ///////////////////////////////////////////////////
199
200         // Set other missing variables
201         if (!isset($GLOBALS['output_mode'])) $GLOBALS['output_mode'] = "0";
202
203         // Init session
204         LOAD_INC_ONCE("inc/session.php");
205
206         // Include databases.php
207         LOAD_INC_ONCE("inc/databases.php");
208
209         // Check if we are in installation routine
210         if ((basename($_SERVER['PHP_SELF']) != "install.php") && ($GLOBALS['output_mode'] != "1") && ($GLOBALS['output_mode'] != -1)) {
211                 // Redirect to the installation system
212                 LOAD_URL("install.php");
213         } // END - if
214
215         // Double-check installation mode
216         if ((!isInstalled()) || (!isAdminRegistered())) {
217                 // Check for file permissions
218                 if (!IS_INC_WRITEABLE("config")) {
219                         addFatalMessage(__FILE__, __LINE__, getMessage('CONFIG_IS_WRITE_PROTECTED'));
220                 } // END - if
221                 if (!IS_INC_WRITEABLE("dummy")) {
222                         addFatalMessage(__FILE__, __LINE__, getMessage('DUMMY_IS_WRITE_PROTECTED'));
223                 } // END - if
224                 if (!IS_INC_WRITEABLE(".secret/dummy")) {
225                         addFatalMessage(__FILE__, __LINE__, getMessage('SECRET_IS_WRITE_PROTECTED'));
226                 } // END - if
227         } // END - if
228
229         // Run the init filter chain
230         runFilterChain('init');
231
232         // Load extension 'sql_patches'
233         LOAD_EXTENSION("sql_patches");
234 }
235
236 if ((getTotalFatalErrors() > 0) && (isInstalled()) && (!defined('mxchange_installing')) && ($GLOBALS['output_mode'] != "1")) {
237         // One or more fatal error(s) occur during connect...
238         LOAD_INC_ONCE("inc/header.php");
239         LOAD_INC_ONCE("inc/fatal_errors.php");
240         LOAD_INC_ONCE("inc/footer.php");
241 } // END - if
242
243 //
244 ?>