Redirect was debugged as well...
[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  * 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 /************************************************************************
40  *      YOU MAY ALSO WANT TO REMOVE THIS FILE AFTER INSTALLTION!        *
41  ************************************************************************/
42
43 // Some security stuff...
44 if (!defined('__SECURITY')) {
45         $INC = substr(dirname(__FILE__), 0, strpos(dirname(__FILE__), '/inc') + 4) . '/security.php';
46         require($INC);
47 } // END - if
48
49 // Init variables
50 $mysql = array();
51 if ((REQUEST_ISSET_POST(('mysql'))) && (is_array(REQUEST_POST('mysql')))) $mysql = REQUEST_POST('mysql');
52
53 // Check if both passwords from SMTP are matching
54 if ((REQUEST_ISSET_GET('page') && (REQUEST_GET('page') == 5))) {
55         // Okay, we have to check it
56         if (REQUEST_ISSET_POST(('smtp_user')) && (!REQUEST_ISSET_POST(('smtp_host')))) {
57                 // Hostname not set
58                 OUTPUT_HTML(getMessage('INSTALL_SMTP_HOSTNAME_EMPTY')."<br />");
59                 REQUEST_SET_GET('page', 3);
60         } // END - if
61
62         if ((!REQUEST_ISSET_POST(('smtp_pass1'))) && (REQUEST_ISSET_POST(('smtp_pass2')))) {
63                 // Password is empty
64                 OUTPUT_HTML(getMessage('INSTALL_SMTP_PASS1_EMPTY')."<br />");
65                 REQUEST_SET_GET('page', 3);
66         } // END - if
67
68         if ((REQUEST_ISSET_POST(('smtp_pass1'))) && (!REQUEST_ISSET_POST(('smtp_pass2')))) {
69                 // Password repeat is empty
70                 OUTPUT_HTML(getMessage('INSTALL_SMTP_PASS2_EMPTY')."<br />");
71                 REQUEST_SET_GET('page', 3);
72         } // END - if
73
74         if (REQUEST_POST('smtp_pass1') != REQUEST_POST('smtp_pass1')) {
75                 // Passwords are not matching
76                 OUTPUT_HTML(getMessage('INSTALL_SMTP_PASS_MISMATCH')."<br />");
77                 REQUEST_SET_GET('page', 3);
78         } // END - if
79 } // END - if
80
81 // Is MXChange installed or no admin registered so far?
82 if ((!isInstalled()) || (!isAdminRegistered())) {
83         // Set URL for FORM actions
84         define('__BURL_ACTION', constant('URL'));
85
86         // Output page for entered value
87         switch (REQUEST_GET('page'))
88         {
89         case 'welcome': // Welcome to the installation!
90                 LOAD_TEMPLATE('install_welcome');
91                 break;
92
93         case '1': // Server path, base URL
94                 // @Profi-Concept: Hab meine Meinung doch geaendert! :-)
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                 OUTPUT_HTML("<form action=\"{!__BURL_ACTION!}/install.php?page=finalize\" method=\"POST\" target=\"_self\">
188 <table border=\"0\" cellspacing=\"0\" cellpadding=\"0\" class=\"install_main\">
189 <tr>
190   <td colspan=\"2\" align=\"center\">
191     <div class=\"install\">{--HEADER_TEXT_PAGE5--}</strong>
192   </td>
193 </tr>
194 <tr><td colspan=\"2\" height=\"21\" class=\"seperator\">&nbsp;</td></tr>
195 <tr>
196   <td colspan=\"2\"><span class=\"install_text\">
197     {--TEXT_PAGE_5--}
198   </span></td>
199 </tr>
200 <tr><td colspan=\"2\" height=\"21\" class=\"seperator\">&nbsp;</td></tr>
201 <tr>
202   <td class=\"install\" align=\"right\">{--LANG_OUTPUT_MODE--}:&nbsp;&nbsp;</td>
203   <td>
204     <select name=\"omode\" size=\"1\" class=\"install_select\">
205       <option value=\"render\">{--MODE_RENDER--}</option>
206       <option value=\"direct\">{--MODE_DIRECT--}</option>
207     </select>
208   </td>
209 </tr>
210 <tr><td colspan=\"2\" height=\"10\" class=\"seperator\">&nbsp;</td></tr>
211 <tr>
212   <td class=\"install\" align=\"right\">{--WARN_NO_PASSWORD--}:&nbsp;&nbsp;</td>
213   <td>
214     <select name=\"warn_no_pass\" size=\"1\" class=\"install_select\">
215       <option value=\"Y\">{--YES--}</option>
216       <option value=\"N\">{--NO--}</option>
217     </select>
218   </td>
219 </tr>
220 <tr><td colspan=\"2\" height=\"10\" class=\"seperator\">&nbsp;</td></tr>
221 <tr>
222   <td class=\"install\" align=\"right\">{--LANG_WRITE_FOOTER--}:&nbsp;&nbsp;</td>
223   <td>
224     <select name=\"wfooter\" size=\"1\" class=\"install_select\">
225       <option value=\"Y\">{--YES--}</option>
226       <option value=\"N\">{--NO--}</option>
227     </select>
228   </td>
229 </tr>
230 <tr><td colspan=\"2\" height=\"10\" class=\"seperator\">&nbsp;</td></tr>
231 <tr>
232   <td class=\"install\" align=\"right\">{--INSTALL_ENABLE_BACKLINK--}:&nbsp;&nbsp;</td>
233   <td>
234     <select name=\"blink\" size=\"1\" class=\"install_select\">
235       <option value=\"Y\">{--YES--}</option>
236       <option value=\"N\">{--NO--}</option>
237     </select>
238   </td>
239 </tr>
240 <tr><td colspan=\"2\" height=\"21\" class=\"seperator\">&nbsp;</td></tr>
241 <tr>
242   <td colspan=\"2\" align=\"center\">");
243                 foreach ($mysql as $key => $value) {
244                         OUTPUT_HTML("    <input type=\"hidden\" name=\"mysql[".$key."]\" value=\"".$value."\" />");
245                 }
246                 OUTPUT_HTML("    <input type=\"hidden\" name=\"spath\" value=\"{!__SPATH_VALUE!}\" />
247     <input type=\"hidden\" name=\"burl\" value=\"{!__BURL_VALUE!}\" />
248     <input type=\"hidden\" name=\"title\" value=\"{!__TITLE_VALUE!}\" />
249     <input type=\"hidden\" name=\"slogan\" value=\"{!__SLOGAN_VALUE!}\" />
250     <input type=\"hidden\" name=\"email\" value=\"{!__EMAIL_VALUE!}\" />
251     <input type=\"hidden\" name=\"smtp_host\" value=\"{!__SMTP_HOST!}\" />
252     <input type=\"hidden\" name=\"smtp_user\" value=\"{!__SMTP_USER!}\" />
253     <input type=\"hidden\" name=\"smtp_pass\" value=\"{!__SMTP_PASS!}\" />
254     <input type=\"submit\" class=\"admin_submit submit\" name=\"finalize\" value=\"{--NEXT_PAGE--}\" class=\"install_submit\" />
255   </td>
256 </tr>
257 <tr><td colspan=\"2\" height=\"21\" class=\"seperator\">&nbsp;</td></tr>
258 </table>
259 </form>");
260                 break;
261
262         case 'finalize': // Write captured data to files
263                 if ((REQUEST_ISSET_POST(('finalize'))) && (!isInstalled())) {
264                         // You have submitted data then we have to reset the SQLs
265                         INIT_SQLS();
266
267                         // Connect to MySQL server
268                         SQL_CONNECT($mysql['host'], $mysql['login'], $mysql['pass1'], __FILE__, __LINE__);
269                         if (SQL_IS_LINK_UP()) {
270                                 // Seems to work, also right database?
271                                 if (SQL_SELECT_DB($mysql['dbase'], __FILE__, __LINE__) === true) {
272                                         // Automatically run install.sql
273                                         if ((isFileReadable(REQUEST_POST('spath') . 'install/tables.sql')) && (isFileReadable(REQUEST_POST('spath') . 'install/menu-'.getLanguage().'.sql'))) {
274                                                 // Both exists so import them
275                                                 foreach (array('tables', 'menu-'.getLanguage()) as $dump) {
276                                                         // Should be save here because file_exists() is there but we check it again. :)
277                                                         $FQFN = REQUEST_POST('spath') . 'install/' . $dump . '.sql';
278
279                                                         // Is the file readable?
280                                                         if (isFileReadable($FQFN)) {
281                                                                 // Read the file
282                                                                 $fileContent = readFromFile($FQFN, true);
283
284                                                                 // Replace the {!prefix/type!} with actual one
285                                                                 foreach (array('prefix', 'type') as $replace) {
286                                                                         while (strpos($fileContent, '{!' . $replace . '!}') !== false) {
287                                                                                 $fileContent = str_replace('{!' . $replace . '!}', $mysql[$replace], $fileContent);
288                                                                         } // END - while
289                                                                 } // END - foreach
290
291                                                                 // Split it up against ";\n" and merge it into existing SQLs
292                                                                 MERGE_SQLS(explode(";\n", $fileContent));
293                                                         } else {
294                                                                 // Not readable!
295                                                                 debug_report_bug(sprintf("SQL dump %s is not readable!", $dump));
296                                                         }
297                                                 } // END - foreach
298
299                                                 // Are some SQLs found?
300                                                 if (COUNT_SQLS() == 0) {
301                                                         // Abort here
302                                                         addFatalMessage(__FILE__, __LINE__, getMessage('INSTALL_SQL_IMPORT_FAILED'));
303                                                         return;
304                                                 } // END - if
305
306                                                 // Now run all queries through and try to keep out empty or comment queries
307                                                 runFilterChain('run_sqls');
308
309                                                 // Copy the config template and verify it
310                                                 copyFileVerified(REQUEST_POST('spath') . 'inc/config-local.php.dist', REQUEST_POST('spath') . 'inc/cache/config-local.php', 0644);
311
312                                                 // Ok, all done. So we can write the config data to the php files
313                                                 if (REQUEST_POST('spath') != constant('PATH')) changeDataInFile(REQUEST_POST('spath') . 'inc/cache/config-local.php', "SERVER-PATH", "define('PATH', \"", "\");", REQUEST_POST('spath'), 0);
314                                                 if (REQUEST_POST('burl')  != constant('URL'))  changeDataInFile(REQUEST_POST('spath') . 'inc/cache/config-local.php', "HOST-URL", "define('URL', \"", "\");", REQUEST_POST('burl'), 0);
315                                                 changeDataInFile(REQUEST_POST('spath') . 'inc/cache/config-local.php', "MAIN-TITLE", "define('MAIN_TITLE', \"", "\");", REQUEST_POST('title'), 0);
316                                                 changeDataInFile(REQUEST_POST('spath') . 'inc/cache/config-local.php', "SLOGAN", "define('SLOGAN', \"", "\");", REQUEST_POST('slogan'), 0);
317                                                 changeDataInFile(REQUEST_POST('spath') . 'inc/cache/config-local.php', "WEBMASTER", "define('WEBMASTER', \"", "\");", REQUEST_POST('email'), 0);
318                                                 changeDataInFile(REQUEST_POST('spath') . 'inc/cache/config-local.php', "NULLPASS-WARNING", "setConfigEntry('WARN_NO_PASS', \"", "\");", REQUEST_POST('warn_no_pass'), 0);
319                                                 changeDataInFile(REQUEST_POST('spath') . 'inc/cache/config-local.php', "WRITE-FOOTER", "setConfigEntry('WRITE_FOOTER', \"", "\");", REQUEST_POST('wfooter'), 0);
320                                                 changeDataInFile(REQUEST_POST('spath') . 'inc/cache/config-local.php', "BACKLINK", "setConfigEntry('ENABLE_BACKLINK', \"", "\");", REQUEST_POST('blink'), 0);
321                                                 // DEACTIVATED: changeDataInFile(REQUEST_POST('spath') . 'inc/cache/config-local.php', "OUTPUT-MODE", "define('OUTPUT_MODE', \"", "\");", REQUEST_POST('omode'), 0);
322                                                 changeDataInFile(REQUEST_POST('spath') . 'inc/cache/config-local.php', "MYSQL-HOST", "  'host'     => \"", "\",", $mysql['host'], 0);
323                                                 changeDataInFile(REQUEST_POST('spath') . 'inc/cache/config-local.php', "MYSQL-DBASE", " 'dbase'    => \"", "\",", $mysql['dbase'], 0);
324                                                 changeDataInFile(REQUEST_POST('spath') . 'inc/cache/config-local.php', "MYSQL-LOGIN", " 'login'    => \"", "\",", $mysql['login'], 0);
325                                                 changeDataInFile(REQUEST_POST('spath') . 'inc/cache/config-local.php', "MYSQL-PASSWORD", "      'password' => \"", "\",", $mysql['pass1'], 0);
326                                                 changeDataInFile(REQUEST_POST('spath') . 'inc/cache/config-local.php', "MYSQL-PREFIX", "setConfigEntry('_MYSQL_PREFIX', \"", "\");", $mysql['prefix'], 0);
327                                                 changeDataInFile(REQUEST_POST('spath') . 'inc/cache/config-local.php', "TABLE-TYPE", "setConfigEntry('_TABLE_TYPE', \"", "\");", $mysql['type'], 0);
328                                                 changeDataInFile(REQUEST_POST('spath') . 'inc/cache/config-local.php', "SMTP-HOSTNAME", "setConfigEntry('SMTP_HOSTNAME', \"", "\");", REQUEST_POST('smtp_host'), 0);
329                                                 changeDataInFile(REQUEST_POST('spath') . 'inc/cache/config-local.php', "SMTP-USER", "setConfigEntry('SMTP_USER', \"", "\");", REQUEST_POST('smtp_user'), 0);
330                                                 changeDataInFile(REQUEST_POST('spath') . 'inc/cache/config-local.php', "SMTP-PASSWORD", "setConfigEntry('SMTP_PASSWORD', \"", "\");", REQUEST_POST('smtp_pass1'), 0);
331
332                                                 // Generate a long site key
333                                                 $siteKey = generatePassword(50);
334
335                                                 // And write it
336                                                 changeDataInFile(REQUEST_POST('spath') . 'inc/cache/config-local.php', "SITE-KEY", "setConfigEntry('SITE_KEY', \"", "\");", $siteKey, 0);
337
338                                                 // Script is now installed
339                                                 changeDataInFile(REQUEST_POST('spath') . 'inc/cache/config-local.php', "INSTALLED", "setConfigEntry('MXCHANGE_INSTALLED', \"", "\");", 'Y', 0);
340                                         } else {
341                                                 // Installation area not found!
342                                                 addFatalMessage(__FILE__, __LINE__, getMessage('INSTALL_MISSING_DUMPS'));
343                                         }
344                                 } // END - if
345                         } // END - if
346
347                         if (getTotalFatalErrors() > 0) {
348                                 $OUT = '';
349                                 foreach (getFatalArray() as $value) {
350                                         $OUT .= "    <li>".$value."</li>\n";
351                                 } // END foreach
352                                 define('__FATAL_ERROR_LI', $OUT);
353                                 $OUT = '';
354                                 foreach ($mysql as $key => $value) {
355                                         $OUT .= "    <input type=\"hidden\" name=\"mysql[".$key."]\" value=\"".$value."\" />\n";
356                                 } // END foreach
357                                 define('__MYSQL_DATA'   , $OUT);
358                                 define('__SPATH_VALUE'  , REQUEST_POST('spath'));
359                                 define('__BURL_VALUE'   , REQUEST_POST('burl'));
360                                 define('__TITLE_VALUE'  , REQUEST_POST('title'));
361                                 define('__SMTP_HOST'    , REQUEST_POST('smtp_host'));
362                                 define('__SMTP_USER'    , REQUEST_POST('smtp_user'));
363                                 define('__SMTP_PASS'    , REQUEST_POST('smtp_pass1'));
364
365                                 // Load template
366                                 LOAD_TEMPLATE('install_fatal_errors');
367                         } else {
368                                 // Installation is done!
369                                 redirectToUrl('install.php?page=finalize');
370                         }
371                 } elseif (isInstalled()) {
372                         // Redirection after writing data... :-)
373                         LOAD_TEMPLATE('install_finished');
374                 } else {
375                         // Something goes wrong during installation! :-(
376                         addFatalMessage(__FILE__, __LINE__, getMessage('INSTALL_FINALIZER_FAILED'));
377                         loadInclude('inc/fatal_errors.php');
378                 }
379                 break;
380
381         default:
382                 DEBUG_LOG(__FILE__, __LINE__, sprintf("Wrong page %s detected", REQUEST_GET('page')));
383                 OUTPUT_HTML("    <div class=\"install_error\">{--WRONG_PAGE--}</div>");
384                 break;
385         }
386 } else {
387         // Already installed!
388         addFatalMessage(__FILE__, __LINE__, getMessage('ALREADY_INSTALLED'));
389 }
390
391 //
392 ?>