config.php partly solved, see #117
[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  * $Revision::                                                        $ *
14  * $Date::                                                            $ *
15  * $Tag:: 0.2.1-FINAL                                                 $ *
16  * $Author::                                                          $ *
17  * Needs to be in all Files and every File needs "svn propset           *
18  * svn:keywords Date Revision" (autoprobset!) at least!!!!!!            *
19  * -------------------------------------------------------------------- *
20  * Copyright (c) 2003 - 2008 by Roland Haeder                           *
21  * For more information visit: http://www.mxchange.org                  *
22  *                                                                      *
23  * This program is free software; you can redistribute it and/or modify *
24  * it under the terms of the GNU General Public License as published by *
25  * the Free Software Foundation; either version 2 of the License, or    *
26  * (at your option) any later version.                                  *
27  *                                                                      *
28  * This program is distributed in the hope that it will be useful,      *
29  * but WITHOUT ANY WARRANTY; without even the implied warranty of       *
30  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the        *
31  * GNU General Public License for more details.                         *
32  *                                                                      *
33  * You should have received a copy of the GNU General Public License    *
34  * along with this program; if not, write to the Free Software          *
35  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,               *
36  * MA  02110-1301  USA                                                  *
37  ************************************************************************/
38
39 // Some security stuff...
40 if (!defined('__SECURITY')) {
41         $INC = substr(dirname(__FILE__), 0, strpos(dirname(__FILE__), '/inc') + 4) . '/security.php';
42         require($INC);
43 }
44
45 // Non-database functions
46 require('inc/functions.php');
47
48 // Wrapper functions
49 require('inc/wrapper-functions.php');
50
51 // Load more function libraries or includes
52 foreach (array('request-functions', 'session-functions', 'config-functions', 'code-functions', 'inc-functions', 'filters', 'mysql-manager', 'extensions', 'db/lib', 'handler', 'hooks') as $lib) {
53         // Load special functions
54         loadIncludeOnce('inc/' . $lib . '.php');
55 } // END - foreach
56
57 // Set error handler
58 set_error_handler('__errorHandler');
59
60 // Load configuration file(s) here
61 loadIncludeOnce('inc/load_config.php');
62
63 // Set important header_sent
64 $GLOBALS['header_sent'] = 0;
65
66 // Check if the user setups his MySQL stuff...
67 if ((empty($GLOBALS['mysql']['login'])) && (!isInstalling()) && (!REQUEST_ISSET_GET('installing')) && (isInstalled())) {
68         // No login entered and outside installation mode
69         OUTPUT_HTML('<strong>{--LANG_WARNING--}:</strong> ');
70         if (isInstalled()) {
71                 // You have changed my configuration file!
72                 app_die(__FILE__, __LINE__, '{--DIE_CONFIG_CHANGED_YOU--}');
73         } else {
74                 // Please run the installation script (maybe again)
75                 app_die(__FILE__, __LINE__, '{--DIE_RUN_INSTALL_MYSQL--}');
76         }
77 } elseif ((!isInstalling()) && (empty($GLOBALS['mysql']['password'])) && (getConfig('WARN_NO_PASS') == 'Y')) {
78         // No database password entered!!!
79         OUTPUT_HTML('<div>{--LANG_WARNING--}:</div> {--WARN_NULL_PASSWORD--}');
80 }
81
82 // Init fatal messages
83 initFatalMessages();
84
85 // Check if this file is writeable or read-only and warn the user
86 if ((!isInstalling()) && (isInstalled())) {
87         // Check for write-permission for config.php and inc directory
88         if (empty($GLOBALS['module'])) $GLOBALS['module'] = REQUEST_GET('module');
89         if (empty($GLOBALS['module'])) $GLOBALS['module'] = 'index';
90
91         // CSS array
92         EXT_INIT_CSS_FILES();
93
94         if ((!empty($GLOBALS['mysql']['host'])) && (!empty($GLOBALS['mysql']['login'])) && (!empty($GLOBALS['mysql']['password'])) && (!empty($GLOBALS['mysql']['dbase']))) {
95                 // Connect to DB
96                 SQL_CONNECT($GLOBALS['mysql']['host'], $GLOBALS['mysql']['login'], $GLOBALS['mysql']['password'], __FILE__, __LINE__);
97
98                 // Is the link valid?
99                 if (SQL_IS_LINK_UP()) {
100                         // Is it a valid resource?
101                         if (SQL_SELECT_DB($GLOBALS['mysql']['dbase'], __FILE__, __LINE__) === true) {
102                                 // This is required for extension 'optimize' to work
103                                 define('__DB_NAME', $GLOBALS['mysql']['dbase']);
104
105                                 // Remove MySQL array from namespace
106                                 unset($GLOBALS['mysql']);
107
108                                 // Load configuration stuff
109                                 loadConfiguration();
110
111                                 // Load "databases" aka static arrays
112                                 loadIncludeOnce('inc/databases.php');
113
114                                 // Init session
115                                 loadIncludeOnce('inc/session.php');
116
117                                 // Load versions
118                                 loadIncludeOnce('inc/versions.php');
119
120                                 // Loading patching system is required here...
121                                 loadIncludeOnce('inc/patch-system.php');
122
123                                 // Run daily reset
124                                 loadIncludeOnce('inc/check-reset.php');
125
126                                 // Load admin include file if he is admin
127                                 if (IS_ADMIN()) {
128                                         // Administrative functions
129                                         loadIncludeOnce('inc/modules/admin/admin-inc.php');
130                                 } // END - if
131                                 //* DEBUG: */ ADD_POINTS_REFSYSTEM('test', 36, 1000);
132                                 //* DEBUG: */ die();
133
134                                 // Get all values
135                                 if (($GLOBALS['output_mode'] != 1) && ($GLOBALS['output_mode'] != -1)) {
136                                         if (empty($GLOBALS['module']))  $GLOBALS['module'] = 'empty';
137                                         if (empty($GLOBALS['what']))    $GLOBALS['what']   = getWhatFromModule($GLOBALS['module']);
138                                         if (empty($GLOBALS['action']))  $GLOBALS['action'] = getModeAction($GLOBALS['module'], $GLOBALS['what']);
139                                 } else {
140                                         // Set action/what to empty
141                                         $GLOBALS['action'] = '';
142                                         $GLOBALS['what']   = '';
143                                 }
144
145                                 // Run the init filter chain
146                                 runFilterChain('init');
147
148                                 // Set default 'what' value
149                                 //* DEBUG: */ OUTPUT_HTML('-'.$GLOBALS['module'].'/'.$GLOBALS['what']."-<br />");
150                                 if ((empty($GLOBALS['what'])) && (empty($GLOBALS['action'])) && ($GLOBALS['output_mode'] != 1) && ($GLOBALS['output_mode'] != -1)) {
151                                         if ($GLOBALS['module'] == 'admin') {
152                                                 // Set 'action' value to 'login' in admin menu
153                                                 $GLOBALS['action'] = getModeAction($GLOBALS['module'], $GLOBALS['what']);
154                                         } elseif (($GLOBALS['module'] == 'index') || ($GLOBALS['module'] == 'login')) {
155                                                 // Set 'what' value to 'welcome' in guest and member menu
156                                                 $GLOBALS['what'] = 'welcome';
157                                                 if (getConfig('index_home') != '') $GLOBALS['what'] = getConfig('index_home');
158                                         } else {
159                                                 // Anything else like begging link
160                                                 $GLOBALS['what'] = '';
161                                         }
162                                 } // END - if
163
164                                 // Update sending pool
165                                 if (($GLOBALS['output_mode'] != '1') && ($GLOBALS['output_mode'] != '-1')) loadIncludeOnce('inc/pool-update.php'); // Sends out mails in configureable steps
166
167                                 // Load all active extension including language files when not upgrading.
168                                 // Check module for testing and count one click
169                                 if (checkModulePermissions($GLOBALS['module']) == 'done') countModuleHit($GLOBALS['module']);
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                                         define('RAND_NUMBER', generateRandomCodde(10, mt_rand(10000,32766), getUserId(), ''));
178                                 } else {
179                                         // Generate weak (!!!) code
180                                         define('RAND_NUMBER', mt_rand(1000000, 9999999));
181                                 }
182                         } else {
183                                 // Wrong database?
184                                 addFatalMessage(__FILE__, __LINE__, getMessage('WRONG_DB_SELECTED'));
185                         }
186                 } else {
187                         // No link to database!
188                         addFatalMessage(__FILE__, __LINE__, getMessage('NO_DB_LINK'));
189                 }
190         } else {
191                 // Maybe you forgot to enter your MySQL data?
192                 addFatalMessage(__FILE__, __LINE__, getMessage('MYSQL_DATA_MISSING'));
193         }
194 } else {
195         ///////////////////////////////////////////////////
196         // Include neccessary functions for installation //
197         ///////////////////////////////////////////////////
198
199         // Set other missing variables
200         if (!isset($GLOBALS['output_mode'])) $GLOBALS['output_mode'] = '0';
201
202         // Include databases.php
203         loadIncludeOnce('inc/databases.php');
204
205         // Init session
206         loadIncludeOnce('inc/session.php');
207
208         // Load versions
209         loadIncludeOnce('inc/versions.php');
210
211         // Check if we are in installation routine
212         if ((!isInstalling()) && ($GLOBALS['output_mode'] != '1') && ($GLOBALS['output_mode'] != -1)) {
213                 // Redirect to the installation system
214                 redirectToUrl('install.php');
215         } // END - if
216
217         // Double-check installation mode
218         if ((!isInstalled()) || (!isAdminRegistered())) {
219                 // Check for file permissions
220                 if (!isIncludeWriteable('config')) {
221                         addFatalMessage(__FILE__, __LINE__, getMessage('CONFIG_IS_WRITE_PROTECTED'));
222                 } // END - if
223                 if (!isIncludeWriteable('dummy')) {
224                         addFatalMessage(__FILE__, __LINE__, getMessage('DUMMY_IS_WRITE_PROTECTED'));
225                 } // END - if
226                 if (!isIncludeWriteable('.secret/dummy')) {
227                         addFatalMessage(__FILE__, __LINE__, getMessage('SECRET_IS_WRITE_PROTECTED'));
228                 } // END - if
229         } // END - if
230
231         // Run the init filter chain
232         runFilterChain('init');
233
234         // Load extension 'sql_patches'
235         LOAD_EXTENSION('sql_patches');
236 }
237
238 if ((getTotalFatalErrors() > 0) && (isInstalled()) && (!isInstalling()) && ($GLOBALS['output_mode'] != '1')) {
239         // One or more fatal error(s) occur during connect...
240         loadIncludeOnce('inc/header.php');
241         loadIncludeOnce('inc/fatal_errors.php');
242         loadIncludeOnce('inc/footer.php');
243 } // END - if
244
245 //
246 ?>