More misc fixes and rewrites (sorry, lame description)
[mailer.git] / inc / install-inc.php
1 <?php
2 /************************************************************************
3  * MXChange v0.2.1                                    Start: 08/29/2003 *
4  * ===============                              Last change: 11/11/2004 *
5  *                                                                      *
6  * -------------------------------------------------------------------- *
7  * File              : install-inc.php                                  *
8  * -------------------------------------------------------------------- *
9  * Short description : Functions for installation procedure             *
10  * -------------------------------------------------------------------- *
11  * Kurzbeschreibung  : Funktionen fuer die Installationsroutine         *
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  * @TODO Rewrite all constants in this include file                     *
21  * -------------------------------------------------------------------- *
22  * Copyright (c) 2003 - 2008 by Roland Haeder                           *
23  * For more information visit: http://www.mxchange.org                  *
24  *                                                                      *
25  * This program is free software; you can redistribute it and/or modify *
26  * it under the terms of the GNU General Public License as published by *
27  * the Free Software Foundation; either version 2 of the License, or    *
28  * (at your option) any later version.                                  *
29  *                                                                      *
30  * This program is distributed in the hope that it will be useful,      *
31  * but WITHOUT ANY WARRANTY; without even the implied warranty of       *
32  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the        *
33  * GNU General Public License for more details.                         *
34  *                                                                      *
35  * You should have received a copy of the GNU General Public License    *
36  * along with this program; if not, write to the Free Software          *
37  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,               *
38  * MA  02110-1301  USA                                                  *
39  ************************************************************************/
40
41 /************************************************************************
42  *      YOU MAY ALSO WANT TO REMOVE THIS FILE AFTER INSTALLTION!        *
43  ************************************************************************/
44
45 // Some security stuff...
46 if (!defined('__SECURITY')) {
47         $INC = substr(dirname(__FILE__), 0, strpos(dirname(__FILE__), '/inc') + 4) . '/security.php';
48         require($INC);
49 } // END - if
50
51 // Init variables
52 $mysql = array();
53 if ((REQUEST_ISSET_POST('mysql')) && (is_array(REQUEST_POST('mysql')))) $mysql = REQUEST_POST('mysql');
54
55 // Check if both passwords from SMTP are matching
56 if ((REQUEST_ISSET_GET('page') && (REQUEST_GET('page') == 5))) {
57         // Okay, we have to check it
58         if (REQUEST_ISSET_POST('smtp_user') && (!REQUEST_ISSET_POST('smtp_host'))) {
59                 // Hostname not set
60                 OUTPUT_HTML(getMessage('INSTALL_SMTP_HOSTNAME_EMPTY') . '<br />');
61                 REQUEST_SET_GET('page', 3);
62         } // END - if
63
64         if ((!REQUEST_ISSET_POST('smtp_pass1')) && (REQUEST_ISSET_POST('smtp_pass2'))) {
65                 // Password is empty
66                 OUTPUT_HTML(getMessage('INSTALL_SMTP_PASS1_EMPTY') . '<br />');
67                 REQUEST_SET_GET('page', 3);
68         } // END - if
69
70         if ((REQUEST_ISSET_POST('smtp_pass1')) && (!REQUEST_ISSET_POST('smtp_pass2'))) {
71                 // Password repeat is empty
72                 OUTPUT_HTML(getMessage('INSTALL_SMTP_PASS2_EMPTY') . '<br />');
73                 REQUEST_SET_GET('page', 3);
74         } // END - if
75
76         if (REQUEST_POST('smtp_pass1') != REQUEST_POST('smtp_pass1')) {
77                 // Passwords are not matching
78                 OUTPUT_HTML(getMessage('INSTALL_SMTP_PASS_MISMATCH') . '<br />');
79                 REQUEST_SET_GET('page', 3);
80         } // END - if
81 } // END - if
82
83 // Is MXChange installed or no admin registered so far?
84 if ((!isInstalled()) || (!isAdminRegistered())) {
85         // Set URL for FORM actions
86         define('__BURL_ACTION', constant('URL'));
87
88         // Output page for entered value
89         switch (REQUEST_GET('page')) {
90                 case 'welcome': // Welcome to the installation!
91                         LOAD_TEMPLATE('install_welcome');
92                         break;
93
94                 case '1': // Server path, base URL
95                         // Load template
96                         LOAD_TEMPLATE('install_page1');
97                         break;
98
99                 case '2': // MySQL data (alone!)
100                         if (empty($mysql['dbase']))  $mysql['dbase']  = 'your_database';
101                         if (empty($mysql['login']))  $mysql['login']  = 'your_login';
102                         if (empty($mysql['host']))   $mysql['host']   = 'localhost';
103                         if (empty($mysql['prefix'])) $mysql['prefix'] = 'mxchange';
104                         if (empty($mysql['type']))   $mysql['type']   = 'MyISAM';
105                         if (getTotalFatalErrors() > 0) {
106                                 OUTPUT_HTML('<span class="install_fatal">');
107                                 foreach (getFatalArray() as $key => $err) {
108                                         OUTPUT_HTML('<strong>&middot;</strong>&nbsp;{--FATAL_NO--}' . ($key + 1) . ':&nbsp;' . $err . '<br />');
109                                 }
110                                 OUTPUT_HTML('</span><br />');
111                         }
112                         define('__MYSQL_HOST'  , $mysql['host']);
113                         define('__MYSQL_DBASE' , $mysql['dbase']);
114                         define('__MYSQL_PREFIX', $mysql['prefix']);
115                         define('__TABLE_TYPE'  , $mysql['type']);
116                         define('__MYSQL_LOGIN' , $mysql['login']);
117                         define('__SPATH_VALUE' , REQUEST_POST('spath'));
118                         define('__BURL_VALUE'  , REQUEST_POST('burl'));
119                         define('__TITLE_VALUE' , REQUEST_POST('title'));
120                         define('__SLOGAN_VALUE', REQUEST_POST('slogan'));
121                         define('__EMAIL_VALUE' , REQUEST_POST('email'));
122
123                         // Load template
124                         LOAD_TEMPLATE('install_page2');
125                         break;
126
127                 case '3':
128                         // Set more values
129                         define('__SPATH_VALUE' , REQUEST_POST('spath'));
130                         define('__BURL_VALUE'  , REQUEST_POST('burl'));
131                         define('__TITLE_VALUE' , REQUEST_POST('title'));
132                         define('__SLOGAN_VALUE', REQUEST_POST('slogan'));
133                         define('__EMAIL_VALUE' , REQUEST_POST('email'));
134
135                         // Use default SMTP data
136                         $smtpHost  = getConfig('SMTP_HOSTNAME');
137                         $smtpUser  = getConfig('SMTP_USER');
138                         $smtpPass1 = getConfig('SMTP_PASSWORD');
139                         $smtpPass2 = getConfig('SMTP_PASSWORD');
140
141                         // Overwrite it with the data from sent (failed) form
142                         if (REQUEST_ISSET_POST('smtp_host')) $smtpHost = REQUEST_POST('smtp_host');
143                         if (REQUEST_ISSET_POST('smtp_user')) $smtpUser = REQUEST_POST('smtp_user');
144                         if (REQUEST_ISSET_POST('smtp_pass')) {
145                                 $smtpPass1 = REQUEST_POST('smtp_pass');
146                                 $smtpPass2 = REQUEST_POST('smtp_pass');
147                         } // END - if
148
149                         // MySQL settings
150                         define('__MYSQL_HOST'  , $mysql['host']);
151                         define('__MYSQL_DBASE' , $mysql['dbase']);
152                         define('__MYSQL_PREFIX', $mysql['prefix']);
153                         define('__TABLE_TYPE'  , $mysql['type']);
154                         define('__MYSQL_LOGIN' , $mysql['login']);
155                         define('__MYSQL_PASS1' , $mysql['pass1']);
156                         define('__MYSQL_PASS2' , $mysql['pass2']);
157
158                         // Set constants for SMTP data
159                         define('__SMTP_HOST' , $smtpHost);
160                         define('__SMTP_USER' , $smtpUser);
161                         define('__SMTP_PASS1', $smtpPass1);
162                         define('__SMTP_PASS2', $smtpPass2);
163
164                         // Load template
165                         LOAD_TEMPLATE('install_page3');
166                         break;
167
168                 case '5': // Misc settings
169                         // General settings
170                         define('__SPATH_VALUE' , REQUEST_POST('spath'));
171                         define('__BURL_VALUE'  , REQUEST_POST('burl'));
172                         define('__TITLE_VALUE' , REQUEST_POST('title'));
173                         define('__SLOGAN_VALUE', REQUEST_POST('slogan'));
174                         define('__EMAIL_VALUE' , REQUEST_POST('email'));
175
176                         // MySQL settings
177                         define('__MYSQL_HOST'  , $mysql['host']);
178                         define('__MYSQL_DBASE' , $mysql['dbase']);
179                         define('__MYSQL_PREFIX', $mysql['prefix']);
180                         define('__TABLE_TYPE'  , $mysql['type']);
181                         define('__MYSQL_LOGIN' , $mysql['login']);
182
183                         // SMTP settings
184                         define('__SMTP_HOST', REQUEST_POST('smtp_host'));
185                         define('__SMTP_USER', REQUEST_POST('smtp_user'));
186                         define('__SMTP_PASS', REQUEST_POST('smtp_pass1'));
187
188                         // MySQL data
189                         $content = '';
190                         foreach ($mysql as $key => $value) {
191                                 $content .= "    <input type=\"hidden\" name=\"mysql[" . $key . "]\" value=\"" . $value . "\" />\n";
192                         }
193                         define('__MYSQL_DATA', $content);
194
195                         // Load template
196                         LOAD_TEMPLATE('install_page5');
197                         break;
198
199                 case 'finalize': // Write captured data to files
200                         if ((REQUEST_ISSET_POST('finalize')) && (!isInstalled())) {
201                                 // You have submitted data then we have to reset the SQLs
202                                 INIT_SQLS();
203
204                                 // Connect to MySQL server
205                                 SQL_CONNECT($mysql['host'], $mysql['login'], $mysql['pass1'], __FILE__, __LINE__);
206                                 if (SQL_IS_LINK_UP()) {
207                                         // Seems to work, also right database?
208                                         if (SQL_SELECT_DB($mysql['dbase'], __FILE__, __LINE__) === true) {
209                                                 // Automatically run install.sql
210                                                 if ((!isFileReadable(REQUEST_POST('spath') . 'install/tables.sql')) || (!isFileReadable(REQUEST_POST('spath') . 'install/menu-'.getLanguage().'.sql'))) {
211                                                         // Installation area not found!
212                                                         addFatalMessage(__FILE__, __LINE__, getMessage('INSTALL_MISSING_DUMPS'));
213                                                 } // END - if
214
215                                                 if (getTotalFatalErrors() == 0) {
216                                                         // Both exists so import them
217                                                         foreach (array('tables', 'menu-'.getLanguage()) as $dump) {
218                                                                 // Should be save here because file_exists() is there but we check it again. :)
219                                                                 $FQFN = REQUEST_POST('spath') . 'install/' . $dump . '.sql';
220
221                                                                 // Is the file readable?
222                                                                 if (isFileReadable($FQFN)) {
223                                                                         // Read the file
224                                                                         $fileContent = readFromFile($FQFN, true);
225
226                                                                         // Replace the {!prefix/type!} with actual one
227                                                                         foreach (array('prefix', 'type') as $replace) {
228                                                                                 while (strpos($fileContent, '{!' . $replace . '!}') !== false) {
229                                                                                         $fileContent = str_replace('{!' . $replace . '!}', $mysql[$replace], $fileContent);
230                                                                                 } // END - while
231                                                                         } // END - foreach
232
233                                                                         // Split it up against ";\n" and merge it into existing SQLs
234                                                                         MERGE_SQLS(explode(";\n", $fileContent));
235                                                                 } else {
236                                                                         // Not readable!
237                                                                         debug_report_bug(sprintf("SQL dump %s is not readable!", $dump));
238                                                                 }
239                                                         } // END - foreach
240
241                                                         // Are some SQLs found?
242                                                         if (COUNT_SQLS() == 0) {
243                                                                 // Abort here
244                                                                 addFatalMessage(__FILE__, __LINE__, getMessage('INSTALL_SQL_IMPORT_FAILED'));
245                                                                 return;
246                                                         } // END - if
247
248                                                         // Now run all queries through and try to keep out empty or comment queries
249                                                         runFilterChain('run_sqls');
250
251                                                         // Copy the config template and verify it
252                                                         copyFileVerified(REQUEST_POST('spath') . 'inc/config-local.php.dist', REQUEST_POST('spath') . 'inc/cache/config-local.php', 0644);
253
254                                                         // Ok, all done. So we can write the config data to the php files
255                                                         if (REQUEST_POST('spath') != constant('PATH')) changeDataInFile(REQUEST_POST('spath') . 'inc/cache/config-local.php', 'SERVER-PATH', "define('PATH', '", "');", REQUEST_POST('spath'), 0);
256                                                         if (REQUEST_POST('burl')  != constant('URL'))  changeDataInFile(REQUEST_POST('spath') . 'inc/cache/config-local.php', 'HOST-URL', "define('URL', '", "');", REQUEST_POST('burl'), 0);
257                                                         changeDataInFile(REQUEST_POST('spath') . 'inc/cache/config-local.php', 'MAIN-TITLE', "define('MAIN_TITLE', '", "');", REQUEST_POST('title'), 0);
258                                                         changeDataInFile(REQUEST_POST('spath') . 'inc/cache/config-local.php', 'SLOGAN', "define('SLOGAN', '", "');", REQUEST_POST('slogan'), 0);
259                                                         changeDataInFile(REQUEST_POST('spath') . 'inc/cache/config-local.php', 'WEBMASTER', "define('WEBMASTER', '", "');", REQUEST_POST('email'), 0);
260                                                         changeDataInFile(REQUEST_POST('spath') . 'inc/cache/config-local.php', 'NULLPASS-WARNING', "setConfigEntry('WARN_NO_PASS', '", "');", REQUEST_POST('warn_no_pass'), 0);
261                                                         changeDataInFile(REQUEST_POST('spath') . 'inc/cache/config-local.php', 'WRITE-FOOTER', "setConfigEntry('WRITE_FOOTER', '", "');", REQUEST_POST('wfooter'), 0);
262                                                         changeDataInFile(REQUEST_POST('spath') . 'inc/cache/config-local.php', 'BACKLINK', "setConfigEntry('ENABLE_BACKLINK', '", "');", REQUEST_POST('blink'), 0);
263                                                         // DEACTIVATED: changeDataInFile(REQUEST_POST('spath') . 'inc/cache/config-local.php', 'OUTPUT-MODE', "define('OUTPUT_MODE', '", "');", REQUEST_POST('omode'), 0);
264                                                         changeDataInFile(REQUEST_POST('spath') . 'inc/cache/config-local.php', 'MYSQL-HOST', "  'host'     => '", "',", $mysql['host'], 0);
265                                                         changeDataInFile(REQUEST_POST('spath') . 'inc/cache/config-local.php', 'MYSQL-DBASE', " 'dbase'    => '", "',", $mysql['dbase'], 0);
266                                                         changeDataInFile(REQUEST_POST('spath') . 'inc/cache/config-local.php', 'MYSQL-LOGIN', " 'login'    => '", "',", $mysql['login'], 0);
267                                                         changeDataInFile(REQUEST_POST('spath') . 'inc/cache/config-local.php', 'MYSQL-PASSWORD', "      'password' => '", "',", $mysql['pass1'], 0);
268                                                         changeDataInFile(REQUEST_POST('spath') . 'inc/cache/config-local.php', 'MYSQL-PREFIX', "setConfigEntry('_MYSQL_PREFIX', '", "');", $mysql['prefix'], 0);
269                                                         changeDataInFile(REQUEST_POST('spath') . 'inc/cache/config-local.php', 'TABLE-TYPE', "setConfigEntry('_TABLE_TYPE', '", "');", $mysql['type'], 0);
270                                                         changeDataInFile(REQUEST_POST('spath') . 'inc/cache/config-local.php', 'SMTP-HOSTNAME', "setConfigEntry('SMTP_HOSTNAME', '", "');", REQUEST_POST('smtp_host'), 0);
271                                                         changeDataInFile(REQUEST_POST('spath') . 'inc/cache/config-local.php', 'SMTP-USER', "setConfigEntry('SMTP_USER', '", "');", REQUEST_POST('smtp_user'), 0);
272                                                         changeDataInFile(REQUEST_POST('spath') . 'inc/cache/config-local.php', 'SMTP-PASSWORD', "setConfigEntry('SMTP_PASSWORD', '", "');", REQUEST_POST('smtp_pass1'), 0);
273
274                                                         // Generate a long site key
275                                                         $siteKey = generatePassword(50);
276
277                                                         // And write it
278                                                         changeDataInFile(REQUEST_POST('spath') . 'inc/cache/config-local.php', 'SITE-KEY', "setConfigEntry('SITE_KEY', '", "');", $siteKey, 0);
279
280                                                         // Script is now installed
281                                                         changeDataInFile(REQUEST_POST('spath') . 'inc/cache/config-local.php', 'INSTALLED', "setConfigEntry('MXCHANGE_INSTALLED', '", "');", 'Y', 0);
282                                                 } // END - if
283                                         } // END - if
284                                 } // END - if
285
286                                 if (getTotalFatalErrors() > 0) {
287                                         $OUT = '';
288                                         foreach (getFatalArray() as $value) {
289                                                 $OUT .= "    <li>" . $value . "</li>\n";
290                                         } // END foreach
291                                         define('__FATAL_ERROR_LI', $OUT);
292                                         $OUT = '';
293                                         foreach ($mysql as $key => $value) {
294                                                 $OUT .= "    <input type=\"hidden\" name=\"mysql[" . $key . "]\" value=\"" . $value . "\" />\n";
295                                         } // END foreach
296                                         define('__MYSQL_DATA'   , $OUT);
297                                         define('__SPATH_VALUE'  , REQUEST_POST('spath'));
298                                         define('__BURL_VALUE'   , REQUEST_POST('burl'));
299                                         define('__TITLE_VALUE'  , REQUEST_POST('title'));
300                                         define('__SMTP_HOST'    , REQUEST_POST('smtp_host'));
301                                         define('__SMTP_USER'    , REQUEST_POST('smtp_user'));
302                                         define('__SMTP_PASS'    , REQUEST_POST('smtp_pass1'));
303
304                                         // Load template
305                                         LOAD_TEMPLATE('install_fatal_errors');
306                                 } else {
307                                         // Installation is done!
308                                         redirectToUrl('install.php?page=finished');
309                                 }
310                         } else {
311                                 // Something goes wrong during installation! :-(
312                                 addFatalMessage(__FILE__, __LINE__, getMessage('INSTALL_FINALIZER_FAILED'));
313                                 loadInclude('inc/fatal_errors.php');
314                         }
315                         break;
316
317                 case 'finished':
318                         if (isInstalled()) {
319                                 // Load template that we are finished
320                                 LOAD_TEMPLATE('install_finished');
321                         } else {
322                                 // Not finished
323                                 redirectToUrl('install.php');
324                         }
325                         break;
326
327                 default:
328                         DEBUG_LOG(__FILE__, __LINE__, sprintf("Wrong page %s detected", REQUEST_GET('page')));
329                         OUTPUT_HTML("    <div class=\"install_error\">{--WRONG_PAGE--}</div>");
330                         break;
331         }
332 } else {
333         // Already installed!
334         addFatalMessage(__FILE__, __LINE__, getMessage('ALREADY_INSTALLED'));
335 }
336
337 //
338 ?>