Huge rewrite:
[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', 'language-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 // Disable block-mode by default
61 enableBlockMode(false);
62
63 // Init error handler
64 initErrorHandler();
65
66 // Init request
67 initRequest();
68
69 // Set important header_sent
70 if (!isset($GLOBALS['header_sent'])) $GLOBALS['header_sent'] = 0;
71
72 // Init fatal messages
73 initFatalMessages();
74
75 // Check if this file is writeable or read-only and warn the user
76 if ((!isInstalling()) && (isInstalled())) {
77         // Load "databases" aka static arrays
78         loadIncludeOnce('inc/databases.php');
79
80         // Load configuration file(s) here
81         loadIncludeOnce('inc/load_config.php');
82
83         // Check for write-permission for config.php and inc directory
84         if (!isModuleSet()) setModule(REQUEST_GET('module'));
85         if (!isModuleSet()) setModule('index');
86
87         // CSS array
88         EXT_INIT_CSS_FILES();
89
90         if ((!empty($GLOBALS['mysql']['host'])) && (!empty($GLOBALS['mysql']['login'])) && (!empty($GLOBALS['mysql']['password'])) && (!empty($GLOBALS['mysql']['dbase']))) {
91                 // Connect to DB
92                 SQL_CONNECT($GLOBALS['mysql']['host'], $GLOBALS['mysql']['login'], $GLOBALS['mysql']['password'], __FILE__, __LINE__);
93
94                 // Is the link valid?
95                 if (SQL_IS_LINK_UP()) {
96                         // Enable exit on error
97                         enableExitOnError();
98
99                         // Is it a valid resource?
100                         if (SQL_SELECT_DB($GLOBALS['mysql']['dbase'], __FILE__, __LINE__) === true) {
101                                 // This is required for extension 'optimize' to work
102                                 define('__DB_NAME', $GLOBALS['mysql']['dbase']);
103
104                                 // Remove MySQL array from namespace
105                                 unset($GLOBALS['mysql']);
106
107                                 // Load configuration stuff
108                                 loadConfiguration();
109
110                                 // Init session
111                                 loadIncludeOnce('inc/session.php');
112
113                                 // Load versions
114                                 loadIncludeOnce('inc/versions.php');
115
116                                 // Loading patching system is required here...
117                                 loadIncludeOnce('inc/patch-system.php');
118
119                                 // Run daily reset
120                                 loadIncludeOnce('inc/check-reset.php');
121
122                                 // Load admin include file if he is admin
123                                 if (IS_ADMIN()) {
124                                         // Administrative functions
125                                         loadIncludeOnce('inc/modules/admin/admin-inc.php');
126                                 } // END - if
127                                 //* DEBUG: */ ADD_POINTS_REFSYSTEM('test', 36, 1000);
128                                 //* DEBUG: */ die();
129
130                                 // Get all values
131                                 if ((getOutputMode() != 1) && (getOutputMode() != -1)) {
132                                         if (!isModuleSet())  setModule('empty');
133                                         if (!isWhatSet())    setWhat(getWhatFromModule(getModule()));
134                                         if (!isActionSet())  setAction(getModeAction(getModule(), getWhat()));
135                                 } else {
136                                         // Set action/what to empty
137                                         setAction('');
138                                         setWhat('');
139                                 }
140
141                                 // Run the init filter chain
142                                 runFilterChain('init');
143
144                                 // Set default 'what' value
145                                 //* DEBUG: */ OUTPUT_HTML('-'.getModule().'/'.getWhat()."-<br />");
146                                 if ((!isWhatSet()) && (!isActionSet()) && (getOutputMode() != 1) && (getOutputMode() != -1)) {
147                                         if (getModule() == 'admin') {
148                                                 // Set 'action' value to 'login' in admin menu
149                                                 setAction(getModeAction(getModule(), getWhat()));
150                                         } elseif ((getModule() == 'index') || (getModule() == 'login')) {
151                                                 // Set 'what' value to 'welcome' in guest and member menu
152                                                 setWhat('welcome');
153                                                 if (getConfig('index_home') != '') setWhatFromConfig('index_home');
154                                         } else {
155                                                 // Anything else like begging link
156                                                 setWhat('');
157                                         }
158                                 } // END - if
159
160                                 // Update sending pool
161                                 if ((getOutputMode() != '1') && (getOutputMode() != '-1')) loadIncludeOnce('inc/pool-update.php'); // Sends out mails in configureable steps
162
163                                 // Check module for permissions
164                                 $checkModule = checkModulePermissions(getModule());
165                                 if ($checkModule != 'done') {
166                                         // Not fine!
167                                         DEBUG_LOG(__FILE__, __LINE__, sprintf("Check of module %s results in unpexted value: %s",
168                                                 getModule(),
169                                                 $checkModule
170                                         ));
171                                 } // END - if
172
173                                 // Shall we activate the exchange?
174                                 if (getConfig('activate_xchange') > 0) activateExchange();
175
176                                 // Is the extension sql_patches installed and at least 0.3.6?
177                                 if (GET_EXT_VERSION('sql_patches') >= '0.3.6') {
178                                         // Generate random number
179                                         define('RAND_NUMBER', generateRandomCode(10, mt_rand(10000,32766), getUserId(), ''));
180                                 } else {
181                                         // Generate weak (!!!) code
182                                         define('RAND_NUMBER', mt_rand(1000000, 9999999));
183                                 }
184                         } else {
185                                 // Wrong database?
186                                 addFatalMessage(__FILE__, __LINE__, getMessage('WRONG_DB_SELECTED'));
187                         }
188                 } else {
189                         // No link to database!
190                         addFatalMessage(__FILE__, __LINE__, getMessage('NO_DB_LINK'));
191                 }
192         } else {
193                 // Maybe you forgot to enter your MySQL data?
194                 addFatalMessage(__FILE__, __LINE__, getMessage('MYSQL_DATA_MISSING'));
195         }
196 } else {
197         ///////////////////////////////////////////////////
198         // Include neccessary functions for installation //
199         ///////////////////////////////////////////////////
200
201         // Default output is 'direct' for HTML output
202         setConfigEntry('OUTPUT_MODE', 'direct');
203
204         // This hack prevents a backtrace in CSS output
205         if (getOutputMode() == '1') {
206                 // Problem with config so set output mode
207                 setConfigEntry('OUTPUT_MODE', 'render');
208         } // END - if
209
210         // Set other missing variables
211         if (!isOutputModeSet()) setOutputMode('0');
212
213         // Include databases.php
214         loadIncludeOnce('inc/databases.php');
215
216         // Init session
217         loadIncludeOnce('inc/session.php');
218
219         // Load versions
220         loadIncludeOnce('inc/versions.php');
221
222         // Check if we are in installation routine
223         if ((!isInstalling()) && (getOutputMode() != '1') && (getOutputMode() != -1)) {
224                 // Redirect to the installation system
225                 redirectToUrl('install.php');
226         } // END - if
227
228         // Double-check installation mode
229         if ((!isInstalled()) || (!isAdminRegistered())) {
230                 // Check for file permissions
231                 if (!isIncludeWriteable('config')) {
232                         addFatalMessage(__FILE__, __LINE__, getMessage('CONFIG_IS_WRITE_PROTECTED'));
233                 } // END - if
234                 if (!isIncludeWriteable('dummy')) {
235                         addFatalMessage(__FILE__, __LINE__, getMessage('DUMMY_IS_WRITE_PROTECTED'));
236                 } // END - if
237                 if (!isIncludeWriteable('.secret/dummy')) {
238                         addFatalMessage(__FILE__, __LINE__, getMessage('SECRET_IS_WRITE_PROTECTED'));
239                 } // END - if
240         } // END - if
241
242         // Run the init filter chain
243         runFilterChain('init');
244
245         // Load extension 'sql_patches'
246         LOAD_EXTENSION('sql_patches');
247
248         // Load configuration file(s) here
249         loadIncludeOnce('inc/load_config.php');
250 }
251
252 // Init session
253 loadIncludeOnce('inc/session.php');
254
255 // Load versions
256 loadIncludeOnce('inc/versions.php');
257
258 if ((getTotalFatalErrors() > 0) && (isInstalled()) && (!isInstalling()) && (getOutputMode() != '1')) {
259         // One or more fatal error(s) occur during connect...
260         loadIncludeOnce('inc/header.php');
261         loadIncludeOnce('inc/fatal_errors.php');
262         loadIncludeOnce('inc/footer.php');
263 } // END - if
264
265 //
266 ?>