MyISAM/InnoDB support added
[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  *                                                                      *
14  * -------------------------------------------------------------------- *
15  * Copyright (c) 2003 - 2008 by Roland Haeder                           *
16  * For more information visit: http://www.mxchange.org                  *
17  *                                                                      *
18  * This program is free software; you can redistribute it and/or modify *
19  * it under the terms of the GNU General Public License as published by *
20  * the Free Software Foundation; either version 2 of the License, or    *
21  * (at your option) any later version.                                  *
22  *                                                                      *
23  * This program is distributed in the hope that it will be useful,      *
24  * but WITHOUT ANY WARRANTY; without even the implied warranty of       *
25  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the        *
26  * GNU General Public License for more details.                         *
27  *                                                                      *
28  * You should have received a copy of the GNU General Public License    *
29  * along with this program; if not, write to the Free Software          *
30  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,               *
31  * MA  02110-1301  USA                                                  *
32  ************************************************************************/
33
34 /************************************************************************
35  *      YOU MAY ALSO WANT TO REMOVE THIS FILE AFTER INSTALLTION!        *
36  ************************************************************************/
37
38 // Some security stuff...
39 if (!defined('__SECURITY')) {
40         $INC = substr(dirname(__FILE__), 0, strpos(dirname(__FILE__), "/inc") + 4) . "/security.php";
41         require($INC);
42 }
43
44 // Load sql_patches here
45 LOAD_EXTENSION("sql_patches");
46
47 // Init variables
48 $mysql = "";
49 if ((REQUEST_ISSET_POST(('mysql'))) && (is_array(REQUEST_POST('mysql')))) $mysql = REQUEST_POST('mysql');
50
51 // Check if both passwords from SMTP are matching
52 if ((REQUEST_ISSET_GET(('page')) && (REQUEST_GET('page') == 5))) {
53         // Okay, we have to check it
54         if (REQUEST_ISSET_POST(('smtp_user')) && (!REQUEST_ISSET_POST(('smtp_host')))) {
55                 // Hostname not set
56                 OUTPUT_HTML(getMessage('INSTALL_SMTP_HOSTNAME_EMPTY')."<br />");
57                 REQUEST_SET_GET('page', 3);
58         } // END - if
59
60         if ((!REQUEST_ISSET_POST(('smtp_pass1'))) && (REQUEST_ISSET_POST(('smtp_pass2')))) {
61                 // Password is empty
62                 OUTPUT_HTML(getMessage('INSTALL_SMTP_PASS1_EMPTY')."<br />");
63                 REQUEST_SET_GET('page', 3);
64         } // END - if
65
66         if ((REQUEST_ISSET_POST(('smtp_pass1'))) && (!REQUEST_ISSET_POST(('smtp_pass2')))) {
67                 // Password repeat is empty
68                 OUTPUT_HTML(getMessage('INSTALL_SMTP_PASS2_EMPTY')."<br />");
69                 REQUEST_SET_GET('page', 3);
70         } // END - if
71
72         if (REQUEST_POST('smtp_pass1') != REQUEST_POST('smtp_pass1')) {
73                 // Passwords are not matching
74                 OUTPUT_HTML(getMessage('INSTALL_SMTP_PASS_MISMATCH')."<br />");
75                 REQUEST_SET_GET('page', 3);
76         } // END - if
77 } // END - if
78
79 // Is MXChange installed or no admin registered so far?
80 if ((!isInstalled()) || (!isAdminRegistered()))
81 {
82         // Set URL for FORM actions
83         define('__BURL_ACTION', constant('URL'));
84
85         // Output page for entered value
86         switch (REQUEST_GET('page'))
87         {
88         case "welcome": // Welcome to the installation!
89                 LOAD_TEMPLATE("install_welcome");
90                 break;
91
92         case "1": // Server path, base URL
93                 // @Profi-Concept: Hab meine Meinung doch geaendert! :-)
94                 // Load template
95                 LOAD_TEMPLATE("install_page1");
96                 break;
97
98         case "2": // MySQL data (alone!)
99                 if (empty($mysql['dbase']))  $mysql['dbase']  = "your_database";
100                 if (empty($mysql['login']))  $mysql['login']  = "your_login";
101                 if (empty($mysql['host']))   $mysql['host']   = "localhost";
102                 if (empty($mysql['prefix'])) $mysql['prefix'] = "mxchange_";
103                 if (empty($mysql['type']))   $mysql['type']   = "MyISAM";
104                 if (getTotalFatalErrors() > 0) {
105                         OUTPUT_HTML("<span class=\"install_fatal\">");
106                         foreach (getFatalArray() as $key => $err) {
107                                 OUTPUT_HTML("  <strong>&middot;</strong>&nbsp;{--FATAL_NO--}".($key + 1).":&nbsp;".$err."<br />");
108                         }
109                         OUTPUT_HTML("</span><br />");
110                 }
111                 define('__MYSQL_HOST'  , $mysql['host']);
112                 define('__MYSQL_DBASE' , $mysql['dbase']);
113                 define('__MYSQL_PREFIX', $mysql['prefix']);
114                 define('__TABLE_TYPE'  , $mysql['type']);
115                 define('__MYSQL_LOGIN' , $mysql['login']);
116                 define('__SPATH_VALUE' , REQUEST_POST('spath'));
117                 define('__BURL_VALUE'  , REQUEST_POST('burl'));
118                 define('__TITLE_VALUE' , REQUEST_POST('title'));
119                 define('__SLOGAN_VALUE', REQUEST_POST('slogan'));
120                 define('__EMAIL_VALUE' , REQUEST_POST('email'));
121
122                 // Load template
123                 LOAD_TEMPLATE("install_page2");
124                 break;
125
126         case "3":
127                 // Set more values
128                 define('__SPATH_VALUE'  , REQUEST_POST('spath'));
129                 define('__BURL_VALUE'   , REQUEST_POST('burl'));
130                 define('__TITLE_VALUE'  , REQUEST_POST('title'));
131                 define('__SLOGAN_VALUE' , REQUEST_POST('slogan'));
132                 define('__EMAIL_VALUE'  , REQUEST_POST('email'));
133
134                 // Use default SMTP data
135                 $smtpHost  = constant('SMTP_HOSTNAME');
136                 $smtpUser  = constant('SMTP_USER');
137                 $smtpPass1 = constant('SMTP_PASSWORD');
138                 $smtpPass2 = constant('SMTP_PASSWORD');
139
140                 // Overwrite it with the data from sent (failed) form
141                 if (REQUEST_ISSET_POST(('smtp_host'))) $smtpHost = REQUEST_POST('smtp_host');
142                 if (REQUEST_ISSET_POST(('smtp_user'))) $smtpUser = REQUEST_POST('smtp_user');
143
144                 // MySQL settings
145                 define('__MYSQL_HOST'   , $mysql['host']);
146                 define('__MYSQL_DBASE'  , $mysql['dbase']);
147                 define('__MYSQL_PREFIX' , $mysql['prefix']);
148                 define('__TABLE_TYPE'   , $mysql['type']);
149                 define('__MYSQL_LOGIN'  , $mysql['login']);
150                 define('__MYSQL_PASS1'  , $mysql['pass1']);
151                 define('__MYSQL_PASS2'  , $mysql['pass2']);
152
153                 // Set constants for SMTP data
154                 define('__SMTP_HOST'    , $smtpHost);
155                 define('__SMTP_USER'    , $smtpUser);
156                 define('__SMTP_PASS1'   , $smtpPass1);
157                 define('__SMTP_PASS2'   , $smtpPass2);
158
159                 // Load template
160                 LOAD_TEMPLATE("install_page3");
161                 break;
162
163         case "5": // Misc settings
164                 // General settings
165                 define('__SPATH_VALUE'  , REQUEST_POST('spath'));
166                 define('__BURL_VALUE'   , REQUEST_POST('burl'));
167                 define('__TITLE_VALUE'  , REQUEST_POST('title'));
168                 define('__SLOGAN_VALUE' , REQUEST_POST('slogan'));
169                 define('__EMAIL_VALUE'  , REQUEST_POST('email'));
170
171                 // MySQL settings
172                 define('__MYSQL_HOST'   , $mysql['host']);
173                 define('__MYSQL_DBASE'  , $mysql['dbase']);
174                 define('__MYSQL_PREFIX' , $mysql['prefix']);
175                 define('__TABLE_TYPE'   , $mysql['type']);
176                 define('__MYSQL_LOGIN'  , $mysql['login']);
177
178                 // SMTP settings
179                 define('__SMTP_HOST'    , REQUEST_POST('smtp_host'));
180                 define('__SMTP_USER'    , REQUEST_POST('smtp_user'));
181                 define('__SMTP_PASS'    , REQUEST_POST('smtp_pass1'));
182                 OUTPUT_HTML("<form action=\"{!__BURL_ACTION!}/install.php?page=finalize\" method=\"POST\" target=\"_self\">
183 <table border=\"0\" cellspacing=\"0\" cellpadding=\"0\" class=\"install_content\">
184 <tr>
185   <td colspan=\"2\" align=\"center\">
186     <div class=\"install\">{--HEADER_TEXT_PAGE5--}</strong>
187   </td>
188 </tr>
189 <tr><td colspan=\"2\" height=\"21\" class=\"seperator\">&nbsp;</td></tr>
190 <tr>
191   <td colspan=\"2\"><span class=\"install_intro\">
192     {--TEXT_PAGE_5--}
193   </span></td>
194 </tr>
195 <tr><td colspan=\"2\" height=\"21\" class=\"seperator\">&nbsp;</td></tr>
196 <tr>
197   <td class=\"install\" align=\"right\">{--LANG_OUTPUT_MODE--}:&nbsp;&nbsp;</td>
198   <td>
199     <select name=\"omode\" size=\"1\" class=\"install_select\">
200       <option value=\"render\"");
201                 if (constant('OUTPUT_MODE') == "render") OUTPUT_HTML(" selected=\"selected\"");
202                 OUTPUT_HTML(">{--MODE_RENDER--}</option>
203       <option value=\"direct\"");
204                 if (constant('OUTPUT_MODE') == "direct") OUTPUT_HTML(" selected=\"selected\"");
205                 OUTPUT_HTML(">{--MODE_DIRECT--}</option>
206     </select>
207   </td>
208 </tr>
209 <tr><td colspan=\"2\" height=\"10\" class=\"seperator\">&nbsp;</td></tr>
210 <tr>
211   <td class=\"install\" align=\"right\">{--WARN_NO_PASSWORD--}:&nbsp;&nbsp;</td>
212   <td>
213     <select name=\"warn_no_pass\" size=\"1\" class=\"install_select\">
214       <option value=\"true\"");
215                 if (isBooleanConstantAndTrue('warn_no_pass')) OUTPUT_HTML(" selected=\"selected\"");
216                 OUTPUT_HTML(">{--YES--}</option>
217       <option value=\"false\"");
218                 if (!isBooleanConstantAndTrue('warn_no_pass')) OUTPUT_HTML(" selected=\"selected\"");
219                 OUTPUT_HTML(">{--NO--}</option>
220     </select>
221   </td>
222 </tr>
223 <tr><td colspan=\"2\" height=\"10\" class=\"seperator\">&nbsp;</td></tr>
224 <tr>
225   <td class=\"install\" align=\"right\">{--LANG_WRITE_FOOTER--}:&nbsp;&nbsp;</td>
226   <td>
227     <select name=\"wfooter\" size=\"1\" class=\"install_select\">
228       <option value=\"true\"");
229                 if (isBooleanConstantAndTrue('WRITE_FOOTER')) OUTPUT_HTML(" selected=\"selected\"");
230                 OUTPUT_HTML(">{--YES--}</option>
231       <option value=\"false\"");
232                 if (!isBooleanConstantAndTrue('WRITE_FOOTER')) OUTPUT_HTML(" selected=\"selected\"");
233                 OUTPUT_HTML(">{--NO--}</option>
234     </select>
235   </td>
236 </tr>
237 <tr><td colspan=\"2\" height=\"10\" class=\"seperator\">&nbsp;</td></tr>
238 <tr>
239   <td class=\"install\" align=\"right\">{--INSTALL_ENABLE_BACKLINK--}:&nbsp;&nbsp;</td>
240   <td>
241     <select name=\"blink\" size=\"1\" class=\"install_select\">
242       <option value=\"true\"");
243                 if (isBooleanConstantAndTrue('ENABLE_BACKLINK')) OUTPUT_HTML(" selected=\"selected\"");
244                 OUTPUT_HTML(">{--YES--}</option>
245       <option value=\"false\"");
246                 if (!isBooleanConstantAndTrue('ENABLE_BACKLINK')) OUTPUT_HTML(" selected=\"selected\"");
247                 OUTPUT_HTML(">{--NO--}</option>
248     </select>
249   </td>
250 </tr>
251 <tr><td colspan=\"2\" height=\"21\" class=\"seperator\">&nbsp;</td></tr>
252 <tr>
253   <td colspan=\"2\" align=\"center\">");
254                 foreach ($mysql as $key => $value) {
255                         OUTPUT_HTML("    <input type=\"hidden\" name=\"mysql[".$key."]\" value=\"".$value."\" />");
256                 }
257                 OUTPUT_HTML("    <input type=\"hidden\" name=\"spath\" value=\"{!__SPATH_VALUE!}\" />
258     <input type=\"hidden\" name=\"burl\" value=\"{!__BURL_VALUE!}\" />
259     <input type=\"hidden\" name=\"title\" value=\"{!__TITLE_VALUE!}\" />
260     <input type=\"hidden\" name=\"slogan\" value=\"{!__SLOGAN_VALUE!}\" />
261     <input type=\"hidden\" name=\"email\" value=\"{!__EMAIL_VALUE!}\" />
262     <input type=\"hidden\" name=\"smtp_host\" value=\"{!__SMTP_HOST!}\" />
263     <input type=\"hidden\" name=\"smtp_user\" value=\"{!__SMTP_USER!}\" />
264     <input type=\"hidden\" name=\"smtp_pass\" value=\"{!__SMTP_PASS!}\" />
265     <input type=\"submit\" class=\"admin_submit submit\" name=\"finalize\" value=\"{--NEXT_PAGE--}\" class=\"install_submit\" />
266   </td>
267 </tr>
268 <tr><td colspan=\"2\" height=\"21\" class=\"seperator\">&nbsp;</td></tr>
269 </table>
270 </form>");
271                 break;
272
273         case "finalize": // Write captured data to files
274                 if ((REQUEST_ISSET_POST(('finalize'))) && (!isInstalled())) {
275                         // You have submitted data then we have to reset the SQLs
276                         INIT_SQLS();
277
278                         // Connect to MySQL server
279                         SQL_CONNECT($mysql['host'], $mysql['login'], $mysql['pass1'], __FILE__, __LINE__);
280                         if (SQL_IS_LINK_UP()) {
281                                 // Seems to work, also right database?
282                                 if (SQL_SELECT_DB($mysql['dbase'], __FILE__, __LINE__) === true) {
283                                         // Automatically run install.sql
284                                         if ((FILE_READABLE(REQUEST_POST('spath')."install/tables.sql")) && (FILE_READABLE(REQUEST_POST('spath')."install/menu-".GET_LANGUAGE().".sql"))) {
285                                                 // Both exists so import them
286                                                 foreach (array("tables", "menu-".GET_LANGUAGE()) as $dump) {
287                                                         // Should be save here because file_exists() is there but we check it again. :)
288                                                         $FQFN = REQUEST_POST('spath') . "install/" . $dump . ".sql";
289                                                         if (FILE_READABLE($FQFN)) {
290                                                                 // Read the file
291                                                                 $fileContent = READ_FILE($FQFN, true);
292
293                                                                 // Replace the {!prefix!} with actual one
294                                                                 while (strpos($fileContent, "{!prefix!}") !== false) {
295                                                                         $fileContent = str_replace("{!prefix!}", $mysql['prefix'], $fileContent);
296                                                                 } // END - while
297
298                                                                 // Replace the {!type!} with actual one
299                                                                 while (strpos($fileContent, "{!type!}") !== false) {
300                                                                         $fileContent = str_replace("{!type!}", $mysql['type'], $fileContent);
301                                                                 } // END - while
302
303                                                                 // And split it up against ;\n ...
304                                                                 SET_SQLS(merge_array(GET_SQLS(), explode(";\n", $fileContent)));
305                                                         } else {
306                                                                 // Not readable!
307                                                                 debug_report_bug(sprintf("SQL dump %s is not readable!", $dump));
308                                                         }
309                                                 } // END - foreach
310
311                                                 // Are some SQLs found?
312                                                 if (COUNT_SQLS() == 0) {
313                                                         // Abort here
314                                                         addFatalMessage(__FILE__, __LINE__, getMessage('INSTALL_SQL_IMPORT_FAILED'));
315                                                         return;
316                                                 } // END - if
317
318                                                 // Now run all queries through and try to keep out empty or comment queries
319                                                 runFilterChain('run_sqls');
320
321                                                 // Ok, all done. So we can write the config data to the php files
322                                                 if (REQUEST_POST('spath') != constant('PATH')) changeDataInFile(REQUEST_POST('spath')."inc/config.php", "SERVER-PATH", "define('PATH', \"", "\");", REQUEST_POST('spath'), 0);
323                                                 if (REQUEST_POST('burl')  != constant('URL'))  changeDataInFile(REQUEST_POST('spath')."inc/config.php", "HOST-URL", "define('URL', \"", "\");", REQUEST_POST('burl'), 0);
324                                                 changeDataInFile(REQUEST_POST('spath')."inc/config.php", "MAIN_TITLE", "define('MAIN_TITLE', \"", "\");", REQUEST_POST('title'), 0);
325                                                 changeDataInFile(REQUEST_POST('spath')."inc/config.php", "SLOGAN", "define('SLOGAN', \"", "\");", REQUEST_POST('slogan'), 0);
326                                                 changeDataInFile(REQUEST_POST('spath')."inc/config.php", "WEBMASTER", "define('WEBMASTER', \"", "\");", REQUEST_POST('email'), 0);
327                                                 changeDataInFile(REQUEST_POST('spath')."inc/config.php", "NULLPASS-WARNING", "define('warn_no_pass', ", ");", REQUEST_POST('warn_no_pass'), 0);
328                                                 changeDataInFile(REQUEST_POST('spath')."inc/config.php", "WRITE-FOOTER", "define('WRITE_FOOTER', ", ");", REQUEST_POST('wfooter'), 0);
329                                                 changeDataInFile(REQUEST_POST('spath')."inc/config.php", "BACKLINK", "define('ENABLE_BACKLINK', ", ");", REQUEST_POST('blink'), 0);
330                                                 // DEACTIVATED: changeDataInFile(REQUEST_POST('spath')."inc/config.php", "OUTPUT-MODE", "define('OUTPUT_MODE', \"", "\");", REQUEST_POST('omode'), 0);
331                                                 changeDataInFile(REQUEST_POST('spath')."inc/config.php", "MYSQL-HOST", "        'host'     => \"", "\",", $mysql['host'], 0);
332                                                 changeDataInFile(REQUEST_POST('spath')."inc/config.php", "MYSQL-DBASE", "       'dbase'    => \"", "\",", $mysql['dbase'], 0);
333                                                 changeDataInFile(REQUEST_POST('spath')."inc/config.php", "MYSQL-LOGIN", "       'login'    => \"", "\",", $mysql['login'], 0);
334                                                 changeDataInFile(REQUEST_POST('spath')."inc/config.php", "MYSQL-PASSWORD", "    'password' => \"", "\",", $mysql['pass1'], 0);
335                                                 changeDataInFile(REQUEST_POST('spath')."inc/config.php", "MYSQL-PREFIX", "define('_MYSQL_PREFIX', \"", "\");", $mysql['prefix'], 0);
336                                                 changeDataInFile(REQUEST_POST('spath')."inc/config.php", "TABLE-TYPE", "define('_TABLE_TYPE', \"", "\");", $mysql['type'], 0);
337                                                 changeDataInFile(REQUEST_POST('spath')."inc/config.php", "SMTP-HOSTNAME", "define('SMTP_HOSTNAME', \"", "\");", REQUEST_POST('smtp_host'), 0);
338                                                 changeDataInFile(REQUEST_POST('spath')."inc/config.php", "SMTP-USER", "define('SMTP_USER', \"", "\");", REQUEST_POST('smtp_user'), 0);
339                                                 changeDataInFile(REQUEST_POST('spath')."inc/config.php", "SMTP-PASSWORD", "define('SMTP_PASSWORD', \"", "\");", REQUEST_POST('smtp_pass1'), 0);
340                                                 changeDataInFile(REQUEST_POST('spath')."inc/config.php", "INSTALLED", "define('mxchange_installed', ", ");", "true", 0);
341                                         } else {
342                                                 // Installation area not found!
343                                                 addFatalMessage(__FILE__, __LINE__, getMessage('INSTALL_MISSING_DUMPS'));
344                                         }
345                                 } // END - if
346                         } // END - if
347
348                         if (getTotalFatalErrors() > 0) {
349                                 $OUT = "";
350                                 foreach (getFatalArray() as $value) {
351                                         $OUT .= "    <li>".$value."</li>\n";
352                                 } // END foreach
353                                 define('__FATAL_ERROR_LI', $OUT);
354                                 $OUT = "";
355                                 foreach ($mysql as $key => $value) {
356                                         $OUT .= "    <input type=\"hidden\" name=\"mysql[".$key."]\" value=\"".$value."\">\n";
357                                 } // END foreach
358                                 define('__MYSQL_DATA'   , $OUT);
359                                 define('__SPATH_VALUE'  , REQUEST_POST('spath'));
360                                 define('__BURL_VALUE'   , REQUEST_POST('burl'));
361                                 define('__TITLE_VALUE'  , REQUEST_POST('title'));
362                                 define('__SMTP_HOST'    , REQUEST_POST('smtp_host'));
363                                 define('__SMTP_USER'    , REQUEST_POST('smtp_user'));
364                                 define('__SMTP_PASS'    , REQUEST_POST('smtp_pass1'));
365
366                                 // Load template
367                                 LOAD_TEMPLATE("install_fatal_errors");
368                         } else {
369                                 // Installation is done!
370                                 LOAD_URL("install.php?page=finalize");
371                         }
372                 } elseif (isInstalled()) {
373                         // Redirection after writing data... :-)
374                         LOAD_TEMPLATE("install_finished");
375                 } else {
376                         // Something goes wrong during installation! :-(
377                         addFatalMessage(__FILE__, __LINE__, getMessage('INSTALL_FINALIZER_FAILED'));
378                         LOAD_INC("inc/fatal_errors.php");
379                 }
380                 break;
381
382         default:
383                 DEBUG_LOG(__FILE__, __LINE__, sprintf("Wrong page %s detected", REQUEST_GET('page')));
384                 OUTPUT_HTML("    <div class=\"install_error\">{--WRONG_PAGE--}</strong>");
385                 break;
386         }
387 } else {
388         // Already installed!
389         addFatalMessage(__FILE__, __LINE__, getMessage('ALREADY_INSTALLED'));
390 }
391
392 //
393 ?>