RevBomb patch applied (thanks to profi-concept)
[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:: 856                                                    $ *
14  * $Date:: 2009-03-06 20:24:32 +0100 (Fr, 06. Mär 2009)              $ *
15  * $Tag:: 0.2.1-FINAL                                                 $ *
16  * $Author:: stelzi                                                   $ *
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 }
48
49 // Load sql_patches here
50 LOAD_EXTENSION("sql_patches");
51
52 // Init variables
53 $mysql = "";
54 if ((REQUEST_ISSET_POST(('mysql'))) && (is_array(REQUEST_POST('mysql')))) $mysql = REQUEST_POST('mysql');
55
56 // Check if both passwords from SMTP are matching
57 if ((REQUEST_ISSET_GET(('page')) && (REQUEST_GET('page') == 5))) {
58         // Okay, we have to check it
59         if (REQUEST_ISSET_POST(('smtp_user')) && (!REQUEST_ISSET_POST(('smtp_host')))) {
60                 // Hostname not set
61                 OUTPUT_HTML(getMessage('INSTALL_SMTP_HOSTNAME_EMPTY')."<br />");
62                 REQUEST_SET_GET('page', 3);
63         } // END - if
64
65         if ((!REQUEST_ISSET_POST(('smtp_pass1'))) && (REQUEST_ISSET_POST(('smtp_pass2')))) {
66                 // Password is empty
67                 OUTPUT_HTML(getMessage('INSTALL_SMTP_PASS1_EMPTY')."<br />");
68                 REQUEST_SET_GET('page', 3);
69         } // END - if
70
71         if ((REQUEST_ISSET_POST(('smtp_pass1'))) && (!REQUEST_ISSET_POST(('smtp_pass2')))) {
72                 // Password repeat is empty
73                 OUTPUT_HTML(getMessage('INSTALL_SMTP_PASS2_EMPTY')."<br />");
74                 REQUEST_SET_GET('page', 3);
75         } // END - if
76
77         if (REQUEST_POST('smtp_pass1') != REQUEST_POST('smtp_pass1')) {
78                 // Passwords are not matching
79                 OUTPUT_HTML(getMessage('INSTALL_SMTP_PASS_MISMATCH')."<br />");
80                 REQUEST_SET_GET('page', 3);
81         } // END - if
82 } // END - if
83
84 // Is MXChange installed or no admin registered so far?
85 if ((!isInstalled()) || (!isAdminRegistered()))
86 {
87         // Set URL for FORM actions
88         define('__BURL_ACTION', constant('URL'));
89
90         // Output page for entered value
91         switch (REQUEST_GET('page'))
92         {
93         case "welcome": // Welcome to the installation!
94                 LOAD_TEMPLATE("install_welcome");
95                 break;
96
97         case "1": // Server path, base URL
98                 // @Profi-Concept: Hab meine Meinung doch geaendert! :-)
99                 // Load template
100                 LOAD_TEMPLATE("install_page1");
101                 break;
102
103         case "2": // MySQL data (alone!)
104                 if (empty($mysql['dbase']))  $mysql['dbase']  = "your_database";
105                 if (empty($mysql['login']))  $mysql['login']  = "your_login";
106                 if (empty($mysql['host']))   $mysql['host']   = "localhost";
107                 if (empty($mysql['prefix'])) $mysql['prefix'] = "mxchange_";
108                 if (empty($mysql['type']))   $mysql['type']   = "MyISAM";
109                 if (getTotalFatalErrors() > 0) {
110                         OUTPUT_HTML("<span class=\"install_fatal\">");
111                         foreach (getFatalArray() as $key => $err) {
112                                 OUTPUT_HTML("  <strong>&middot;</strong>&nbsp;{--FATAL_NO--}".($key + 1).":&nbsp;".$err."<br />");
113                         }
114                         OUTPUT_HTML("</span><br />");
115                 }
116                 define('__MYSQL_HOST'  , $mysql['host']);
117                 define('__MYSQL_DBASE' , $mysql['dbase']);
118                 define('__MYSQL_PREFIX', $mysql['prefix']);
119                 define('__TABLE_TYPE'  , $mysql['type']);
120                 define('__MYSQL_LOGIN' , $mysql['login']);
121                 define('__SPATH_VALUE' , REQUEST_POST('spath'));
122                 define('__BURL_VALUE'  , REQUEST_POST('burl'));
123                 define('__TITLE_VALUE' , REQUEST_POST('title'));
124                 define('__SLOGAN_VALUE', REQUEST_POST('slogan'));
125                 define('__EMAIL_VALUE' , REQUEST_POST('email'));
126
127                 // Load template
128                 LOAD_TEMPLATE("install_page2");
129                 break;
130
131         case "3":
132                 // Set more values
133                 define('__SPATH_VALUE'  , REQUEST_POST('spath'));
134                 define('__BURL_VALUE'   , REQUEST_POST('burl'));
135                 define('__TITLE_VALUE'  , REQUEST_POST('title'));
136                 define('__SLOGAN_VALUE' , REQUEST_POST('slogan'));
137                 define('__EMAIL_VALUE'  , REQUEST_POST('email'));
138
139                 // Use default SMTP data
140                 $smtpHost  = constant('SMTP_HOSTNAME');
141                 $smtpUser  = constant('SMTP_USER');
142                 $smtpPass1 = constant('SMTP_PASSWORD');
143                 $smtpPass2 = constant('SMTP_PASSWORD');
144
145                 // Overwrite it with the data from sent (failed) form
146                 if (REQUEST_ISSET_POST(('smtp_host'))) $smtpHost = REQUEST_POST('smtp_host');
147                 if (REQUEST_ISSET_POST(('smtp_user'))) $smtpUser = REQUEST_POST('smtp_user');
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_content\">
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_intro\">
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\"");
206                 if (constant('OUTPUT_MODE') == "render") OUTPUT_HTML(" selected=\"selected\"");
207                 OUTPUT_HTML(">{--MODE_RENDER--}</option>
208       <option value=\"direct\"");
209                 if (constant('OUTPUT_MODE') == "direct") OUTPUT_HTML(" selected=\"selected\"");
210                 OUTPUT_HTML(">{--MODE_DIRECT--}</option>
211     </select>
212   </td>
213 </tr>
214 <tr><td colspan=\"2\" height=\"10\" class=\"seperator\">&nbsp;</td></tr>
215 <tr>
216   <td class=\"install\" align=\"right\">{--WARN_NO_PASSWORD--}:&nbsp;&nbsp;</td>
217   <td>
218     <select name=\"warn_no_pass\" size=\"1\" class=\"install_select\">
219       <option value=\"true\"");
220                 if (isBooleanConstantAndTrue('warn_no_pass')) OUTPUT_HTML(" selected=\"selected\"");
221                 OUTPUT_HTML(">{--YES--}</option>
222       <option value=\"false\"");
223                 if (!isBooleanConstantAndTrue('warn_no_pass')) OUTPUT_HTML(" selected=\"selected\"");
224                 OUTPUT_HTML(">{--NO--}</option>
225     </select>
226   </td>
227 </tr>
228 <tr><td colspan=\"2\" height=\"10\" class=\"seperator\">&nbsp;</td></tr>
229 <tr>
230   <td class=\"install\" align=\"right\">{--LANG_WRITE_FOOTER--}:&nbsp;&nbsp;</td>
231   <td>
232     <select name=\"wfooter\" size=\"1\" class=\"install_select\">
233       <option value=\"true\"");
234                 if (isBooleanConstantAndTrue('WRITE_FOOTER')) OUTPUT_HTML(" selected=\"selected\"");
235                 OUTPUT_HTML(">{--YES--}</option>
236       <option value=\"false\"");
237                 if (!isBooleanConstantAndTrue('WRITE_FOOTER')) OUTPUT_HTML(" selected=\"selected\"");
238                 OUTPUT_HTML(">{--NO--}</option>
239     </select>
240   </td>
241 </tr>
242 <tr><td colspan=\"2\" height=\"10\" class=\"seperator\">&nbsp;</td></tr>
243 <tr>
244   <td class=\"install\" align=\"right\">{--INSTALL_ENABLE_BACKLINK--}:&nbsp;&nbsp;</td>
245   <td>
246     <select name=\"blink\" size=\"1\" class=\"install_select\">
247       <option value=\"true\"");
248                 if (isBooleanConstantAndTrue('ENABLE_BACKLINK')) OUTPUT_HTML(" selected=\"selected\"");
249                 OUTPUT_HTML(">{--YES--}</option>
250       <option value=\"false\"");
251                 if (!isBooleanConstantAndTrue('ENABLE_BACKLINK')) OUTPUT_HTML(" selected=\"selected\"");
252                 OUTPUT_HTML(">{--NO--}</option>
253     </select>
254   </td>
255 </tr>
256 <tr><td colspan=\"2\" height=\"21\" class=\"seperator\">&nbsp;</td></tr>
257 <tr>
258   <td colspan=\"2\" align=\"center\">");
259                 foreach ($mysql as $key => $value) {
260                         OUTPUT_HTML("    <input type=\"hidden\" name=\"mysql[".$key."]\" value=\"".$value."\" />");
261                 }
262                 OUTPUT_HTML("    <input type=\"hidden\" name=\"spath\" value=\"{!__SPATH_VALUE!}\" />
263     <input type=\"hidden\" name=\"burl\" value=\"{!__BURL_VALUE!}\" />
264     <input type=\"hidden\" name=\"title\" value=\"{!__TITLE_VALUE!}\" />
265     <input type=\"hidden\" name=\"slogan\" value=\"{!__SLOGAN_VALUE!}\" />
266     <input type=\"hidden\" name=\"email\" value=\"{!__EMAIL_VALUE!}\" />
267     <input type=\"hidden\" name=\"smtp_host\" value=\"{!__SMTP_HOST!}\" />
268     <input type=\"hidden\" name=\"smtp_user\" value=\"{!__SMTP_USER!}\" />
269     <input type=\"hidden\" name=\"smtp_pass\" value=\"{!__SMTP_PASS!}\" />
270     <input type=\"submit\" class=\"admin_submit submit\" name=\"finalize\" value=\"{--NEXT_PAGE--}\" class=\"install_submit\" />
271   </td>
272 </tr>
273 <tr><td colspan=\"2\" height=\"21\" class=\"seperator\">&nbsp;</td></tr>
274 </table>
275 </form>");
276                 break;
277
278         case "finalize": // Write captured data to files
279                 if ((REQUEST_ISSET_POST(('finalize'))) && (!isInstalled())) {
280                         // You have submitted data then we have to reset the SQLs
281                         INIT_SQLS();
282
283                         // Connect to MySQL server
284                         SQL_CONNECT($mysql['host'], $mysql['login'], $mysql['pass1'], __FILE__, __LINE__);
285                         if (SQL_IS_LINK_UP()) {
286                                 // Seems to work, also right database?
287                                 if (SQL_SELECT_DB($mysql['dbase'], __FILE__, __LINE__) === true) {
288                                         // Automatically run install.sql
289                                         if ((FILE_READABLE(REQUEST_POST('spath')."install/tables.sql")) && (FILE_READABLE(REQUEST_POST('spath')."install/menu-".GET_LANGUAGE().".sql"))) {
290                                                 // Both exists so import them
291                                                 foreach (array("tables", "menu-".GET_LANGUAGE()) as $dump) {
292                                                         // Should be save here because file_exists() is there but we check it again. :)
293                                                         $FQFN = REQUEST_POST('spath') . "install/" . $dump . ".sql";
294                                                         if (FILE_READABLE($FQFN)) {
295                                                                 // Read the file
296                                                                 $fileContent = READ_FILE($FQFN, true);
297
298                                                                 // Replace the {!prefix!} with actual one
299                                                                 while (strpos($fileContent, "{!prefix!}") !== false) {
300                                                                         $fileContent = str_replace("{!prefix!}", $mysql['prefix'], $fileContent);
301                                                                 } // END - while
302
303                                                                 // Replace the {!type!} with actual one
304                                                                 while (strpos($fileContent, "{!type!}") !== false) {
305                                                                         $fileContent = str_replace("{!type!}", $mysql['type'], $fileContent);
306                                                                 } // END - while
307
308                                                                 // And split it up against ;\n ...
309                                                                 SET_SQLS(merge_array(GET_SQLS(), explode(";\n", $fileContent)));
310                                                         } else {
311                                                                 // Not readable!
312                                                                 debug_report_bug(sprintf("SQL dump %s is not readable!", $dump));
313                                                         }
314                                                 } // END - foreach
315
316                                                 // Are some SQLs found?
317                                                 if (COUNT_SQLS() == 0) {
318                                                         // Abort here
319                                                         addFatalMessage(__FILE__, __LINE__, getMessage('INSTALL_SQL_IMPORT_FAILED'));
320                                                         return;
321                                                 } // END - if
322
323                                                 // Now run all queries through and try to keep out empty or comment queries
324                                                 runFilterChain('run_sqls');
325
326                                                 // Ok, all done. So we can write the config data to the php files
327                                                 if (REQUEST_POST('spath') != constant('PATH')) changeDataInFile(REQUEST_POST('spath')."inc/config.php", "SERVER-PATH", "define('PATH', \"", "\");", REQUEST_POST('spath'), 0);
328                                                 if (REQUEST_POST('burl')  != constant('URL'))  changeDataInFile(REQUEST_POST('spath')."inc/config.php", "HOST-URL", "define('URL', \"", "\");", REQUEST_POST('burl'), 0);
329                                                 changeDataInFile(REQUEST_POST('spath')."inc/config.php", "MAIN_TITLE", "define('MAIN_TITLE', \"", "\");", REQUEST_POST('title'), 0);
330                                                 changeDataInFile(REQUEST_POST('spath')."inc/config.php", "SLOGAN", "define('SLOGAN', \"", "\");", REQUEST_POST('slogan'), 0);
331                                                 changeDataInFile(REQUEST_POST('spath')."inc/config.php", "WEBMASTER", "define('WEBMASTER', \"", "\");", REQUEST_POST('email'), 0);
332                                                 changeDataInFile(REQUEST_POST('spath')."inc/config.php", "NULLPASS-WARNING", "define('warn_no_pass', ", ");", REQUEST_POST('warn_no_pass'), 0);
333                                                 changeDataInFile(REQUEST_POST('spath')."inc/config.php", "WRITE-FOOTER", "define('WRITE_FOOTER', ", ");", REQUEST_POST('wfooter'), 0);
334                                                 changeDataInFile(REQUEST_POST('spath')."inc/config.php", "BACKLINK", "define('ENABLE_BACKLINK', ", ");", REQUEST_POST('blink'), 0);
335                                                 // DEACTIVATED: changeDataInFile(REQUEST_POST('spath')."inc/config.php", "OUTPUT-MODE", "define('OUTPUT_MODE', \"", "\");", REQUEST_POST('omode'), 0);
336                                                 changeDataInFile(REQUEST_POST('spath')."inc/config.php", "MYSQL-HOST", "        'host'     => \"", "\",", $mysql['host'], 0);
337                                                 changeDataInFile(REQUEST_POST('spath')."inc/config.php", "MYSQL-DBASE", "       'dbase'    => \"", "\",", $mysql['dbase'], 0);
338                                                 changeDataInFile(REQUEST_POST('spath')."inc/config.php", "MYSQL-LOGIN", "       'login'    => \"", "\",", $mysql['login'], 0);
339                                                 changeDataInFile(REQUEST_POST('spath')."inc/config.php", "MYSQL-PASSWORD", "    'password' => \"", "\",", $mysql['pass1'], 0);
340                                                 changeDataInFile(REQUEST_POST('spath')."inc/config.php", "MYSQL-PREFIX", "define('_MYSQL_PREFIX', \"", "\");", $mysql['prefix'], 0);
341                                                 changeDataInFile(REQUEST_POST('spath')."inc/config.php", "TABLE-TYPE", "define('_TABLE_TYPE', \"", "\");", $mysql['type'], 0);
342                                                 changeDataInFile(REQUEST_POST('spath')."inc/config.php", "SMTP-HOSTNAME", "define('SMTP_HOSTNAME', \"", "\");", REQUEST_POST('smtp_host'), 0);
343                                                 changeDataInFile(REQUEST_POST('spath')."inc/config.php", "SMTP-USER", "define('SMTP_USER', \"", "\");", REQUEST_POST('smtp_user'), 0);
344                                                 changeDataInFile(REQUEST_POST('spath')."inc/config.php", "SMTP-PASSWORD", "define('SMTP_PASSWORD', \"", "\");", REQUEST_POST('smtp_pass1'), 0);
345                                                 changeDataInFile(REQUEST_POST('spath')."inc/config.php", "INSTALLED", "define('mxchange_installed', ", ");", "true", 0);
346                                         } else {
347                                                 // Installation area not found!
348                                                 addFatalMessage(__FILE__, __LINE__, getMessage('INSTALL_MISSING_DUMPS'));
349                                         }
350                                 } // END - if
351                         } // END - if
352
353                         if (getTotalFatalErrors() > 0) {
354                                 $OUT = "";
355                                 foreach (getFatalArray() as $value) {
356                                         $OUT .= "    <li>".$value."</li>\n";
357                                 } // END foreach
358                                 define('__FATAL_ERROR_LI', $OUT);
359                                 $OUT = "";
360                                 foreach ($mysql as $key => $value) {
361                                         $OUT .= "    <input type=\"hidden\" name=\"mysql[".$key."]\" value=\"".$value."\">\n";
362                                 } // END foreach
363                                 define('__MYSQL_DATA'   , $OUT);
364                                 define('__SPATH_VALUE'  , REQUEST_POST('spath'));
365                                 define('__BURL_VALUE'   , REQUEST_POST('burl'));
366                                 define('__TITLE_VALUE'  , REQUEST_POST('title'));
367                                 define('__SMTP_HOST'    , REQUEST_POST('smtp_host'));
368                                 define('__SMTP_USER'    , REQUEST_POST('smtp_user'));
369                                 define('__SMTP_PASS'    , REQUEST_POST('smtp_pass1'));
370
371                                 // Load template
372                                 LOAD_TEMPLATE("install_fatal_errors");
373                         } else {
374                                 // Installation is done!
375                                 LOAD_URL("install.php?page=finalize");
376                         }
377                 } elseif (isInstalled()) {
378                         // Redirection after writing data... :-)
379                         LOAD_TEMPLATE("install_finished");
380                 } else {
381                         // Something goes wrong during installation! :-(
382                         addFatalMessage(__FILE__, __LINE__, getMessage('INSTALL_FINALIZER_FAILED'));
383                         LOAD_INC("inc/fatal_errors.php");
384                 }
385                 break;
386
387         default:
388                 DEBUG_LOG(__FILE__, __LINE__, sprintf("Wrong page %s detected", REQUEST_GET('page')));
389                 OUTPUT_HTML("    <div class=\"install_error\">{--WRONG_PAGE--}</strong>");
390                 break;
391         }
392 } else {
393         // Already installed!
394         addFatalMessage(__FILE__, __LINE__, getMessage('ALREADY_INSTALLED'));
395 }
396
397 //
398 ?>