hooks.php was an idea, but now we have filters.php
[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 - 2009 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         die();
42 } // END - if
43
44 // Load more function libraries or includes
45 foreach (array('functions', 'request-functions', 'session-functions', 'code-functions', 'language-functions', 'sql-functions', 'filter-functions', 'filters', 'mysql-manager', 'extensions-functions', 'handler') as $lib) {
46         // Load special functions
47         loadIncludeOnce('inc/' . $lib . '.php');
48 } // END - foreach
49
50 // Set error handler
51 set_error_handler('__errorHandler');
52
53 // Disable block-mode by default
54 enableBlockMode(false);
55
56 // Init error handler
57 initErrorHandler();
58
59 // Init request
60 initRequest();
61
62 // Set important header_sent
63 if (!isset($GLOBALS['header_sent'])) $GLOBALS['header_sent'] = 0;
64
65 // Init fatal messages
66 initFatalMessages();
67
68 // Init message system
69 initMessages();
70
71 // Check if this file is writeable or read-only and warn the user
72 if ((!isInstalling()) && (!isInstallationPhase())) {
73         // Load "databases" aka static arrays
74         loadIncludeOnce('inc/databases.php');
75
76         // Load database layer here
77         loadIncludeOnce('inc/db/lib.php');
78
79         // Set missing module
80         if (!isModuleSet()) setModule(getRequestElement('module'));
81         if (!isModuleSet()) setModule('index');
82
83         // Load configuration file(s) here
84         loadIncludeOnce('inc/load_config.php');
85
86         // CSS array
87         initExtensionCssFiles();
88
89         if ((!empty($GLOBALS['mysql']['host'])) && (!empty($GLOBALS['mysql']['login'])) && (!empty($GLOBALS['mysql']['password'])) && (!empty($GLOBALS['mysql']['dbase']))) {
90                 // Connect to DB
91                 SQL_CONNECT($GLOBALS['mysql']['host'], $GLOBALS['mysql']['login'], $GLOBALS['mysql']['password'], __FILE__, __LINE__);
92
93                 // Is the link valid?
94                 if (SQL_IS_LINK_UP()) {
95                         // Enable exit on error
96                         enableExitOnError();
97
98                         // Is it a valid resource?
99                         if (SQL_SELECT_DB($GLOBALS['mysql']['dbase'], __FILE__, __LINE__) === true) {
100                                 // This is required for extension 'optimize' to work
101                                 setConfigEntry('__DB_NAME', $GLOBALS['mysql']['dbase']);
102
103                                 // Remove MySQL array from namespace
104                                 unset($GLOBALS['mysql']);
105
106                                 // Load cache
107                                 loadIncludeOnce('inc/load_cache.php');
108
109                                 // Init filter system
110                                 initFilterSystem();
111
112                                 // Run the init filter chain
113                                 runFilterChain('init');
114
115                                 // Is the extension sql_patches installed and at least 0.3.6?
116                                 if ((isExtensionActive('sql_patches')) && (getExtensionVersion('sql_patches') >= '0.3.6')) {
117                                         // Generate random number
118                                         setConfigEntry('RAND_NUMBER', generateRandomCode(10, mt_rand(10000, 32766), getUserId(), ''));
119                                 } else {
120                                         // Generate weak (!!!) code
121                                         setConfigEntry('RAND_NUMBER', mt_rand(1000000, 9999999));
122                                 }
123
124                                 // Check module for permissions
125                                 $checkModule = checkModulePermissions();
126
127                                 // Cound module here
128                                 countModuleHit(getModule());
129
130                                 // Admin module should be accessable by guests to login
131                                 if ((getModule() == 'admin') && ($checkModule == 'admin_only')) {
132                                         // This is fine and can be ignored
133                                 } elseif ($checkModule != 'done') {
134                                         // Not fine!
135                                         logDebugMessage(__FILE__, __LINE__, sprintf("Check of module %s results in unexpected value: %s",
136                                                 getModule(),
137                                                 $checkModule
138                                         ));
139                                 }
140                         } else {
141                                 // Wrong database?
142                                 addFatalMessage(__FILE__, __LINE__, getMessage('WRONG_DB_SELECTED'));
143                         }
144                 } else {
145                         // No link to database!
146                         addFatalMessage(__FILE__, __LINE__, getMessage('NO_DB_LINK'));
147                 }
148         } else {
149                 // Maybe you forgot to enter your MySQL data?
150                 addFatalMessage(__FILE__, __LINE__, getMessage('MYSQL_DATA_MISSING'));
151         }
152 } else {
153         ///////////////////////////////////////////////////
154         // Include neccessary functions for installation //
155         ///////////////////////////////////////////////////
156
157         // Default output is 'direct' for HTML output
158         setConfigEntry('OUTPUT_MODE', 'direct');
159
160         // This hack prevents a backtrace in CSS output
161         if (getOutputMode() == '1') {
162                 // Problem with config so set output mode
163                 setConfigEntry('OUTPUT_MODE', 'render');
164         } // END - if
165
166         // Set other missing variables
167         if (!isOutputModeSet()) setOutputMode('0');
168
169         // Include more
170         foreach (array('inc/databases.php','inc/db/lib.php','inc/session.php','inc/versions.php','inc/install-functions.php','inc/load_config.php') as $inc) {
171                 // Load the include
172                 loadIncludeOnce($inc);
173         } // END - foreach
174
175         // Load config
176         loadIncludeOnce('inc/load_config.php');
177
178         // Are we installation routine?
179         if ((!isInstalling()) && (getOutputMode() != '1') && (getOutputMode() != -1)) {
180                 // You have to install first!
181                 redirectToUrl('install.php');
182         } // END - if
183
184         // Init filter system here
185         initFilterSystem();
186
187         // Load cache
188         loadIncludeOnce('inc/load_cache.php');
189
190         // Run the init filter chain
191         runFilterChain('init');
192 }
193
194 // Handle fatal errors
195 handleFatalErrors();
196
197 // [EOF]
198 ?>