6613994455a6a5085b056109ad1657c20656ac37
[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 // Init variables
45 $mysql = "";
46 if ((REQUEST_ISSET_POST(('mysql'))) && (is_array(REQUEST_POST('mysql')))) $mysql = REQUEST_POST('mysql');
47
48 // Check if both passwords from SMTP are matching
49 if ((REQUEST_ISSET_GET(('page')) && (REQUEST_GET('page') == 5))) {
50         // Okay, we have to check it
51         if (REQUEST_ISSET_POST(('smtp_user')) && (!REQUEST_ISSET_POST(('smtp_host')))) {
52                 // Hostname not set
53                 OUTPUT_HTML(getMessage('INSTALL_SMTP_HOSTNAME_EMPTY')."<br />");
54                 REQUEST_SET_GET('page', 3);
55         } // END - if
56
57         if ((!REQUEST_ISSET_POST(('smtp_pass1'))) && (REQUEST_ISSET_POST(('smtp_pass2')))) {
58                 // Password is empty
59                 OUTPUT_HTML(getMessage('INSTALL_SMTP_PASS1_EMPTY')."<br />");
60                 REQUEST_SET_GET('page', 3);
61         } // END - if
62
63         if ((REQUEST_ISSET_POST(('smtp_pass1'))) && (!REQUEST_ISSET_POST(('smtp_pass2')))) {
64                 // Password repeat is empty
65                 OUTPUT_HTML(getMessage('INSTALL_SMTP_PASS2_EMPTY')."<br />");
66                 REQUEST_SET_GET('page', 3);
67         } // END - if
68
69         if (REQUEST_POST('smtp_pass1') != REQUEST_POST('smtp_pass1')) {
70                 // Passwords are not matching
71                 OUTPUT_HTML(getMessage('INSTALL_SMTP_PASS_MISMATCH')."<br />");
72                 REQUEST_SET_GET('page', 3);
73         } // END - if
74 } // END - if
75
76 // Is MXChange installed or no admin registered so far?
77 if ((!isBooleanConstantAndTrue('mxchange_installed')) || (!isBooleanConstantAndTrue('admin_registered')))
78 {
79         // Set URL for FORM actions
80         define('__BURL_ACTION', constant('URL'));
81
82         // Output page for entered value
83         switch (REQUEST_GET('page'))
84         {
85         case "welcome": // Welcome to the installation!
86                 LOAD_TEMPLATE("install_welcome");
87                 break;
88
89         case "1": // Server path, base URL
90                 // @Profi-Concept: Hab meine Meinung doch geaendert! :-)
91                 // Load template
92                 LOAD_TEMPLATE("install_page1");
93                 break;
94
95         case "2": // MySQL data (alone!)
96                 if (empty($mysql['dbase']))  $mysql['dbase']  = "your_database";
97                 if (empty($mysql['login']))  $mysql['login']  = "your_login";
98                 if (empty($mysql['host']))   $mysql['host']   = "localhost";
99                 if (empty($mysql['prefix'])) $mysql['prefix'] = "mxchange_";
100                 if (getTotalFatalErrors() > 0) {
101                         OUTPUT_HTML("<span class=\"install_fatal\">");
102                         foreach (getFatalArray() as $key => $err) {
103                                 OUTPUT_HTML("  <strong>&middot;</strong>&nbsp;{--FATAL_NO--}".($key + 1).":&nbsp;".$err."<br />");
104                         }
105                         OUTPUT_HTML("</span><br />");
106                 }
107                 define('__MYSQL_HOST'  , $mysql['host']);
108                 define('__MYSQL_DBASE' , $mysql['dbase']);
109                 define('__MYSQL_PREFIX', $mysql['prefix']);
110                 define('__MYSQL_LOGIN' , $mysql['login']);
111                 define('__SPATH_VALUE' , REQUEST_POST('spath'));
112                 define('__BURL_VALUE'  , REQUEST_POST('burl'));
113                 define('__TITLE_VALUE' , REQUEST_POST('title'));
114                 define('__SLOGAN_VALUE', REQUEST_POST('slogan'));
115                 define('__EMAIL_VALUE' , REQUEST_POST('email'));
116
117                 // Load template
118                 LOAD_TEMPLATE("install_page2");
119                 break;
120
121         case "3":
122                 // Set more values
123                 define('__SPATH_VALUE'  , REQUEST_POST('spath'));
124                 define('__BURL_VALUE'   , REQUEST_POST('burl'));
125                 define('__TITLE_VALUE'  , REQUEST_POST('title'));
126                 define('__SLOGAN_VALUE' , REQUEST_POST('slogan'));
127                 define('__EMAIL_VALUE'  , REQUEST_POST('email'));
128
129                 // Use default SMTP data
130                 $smtpHost  = constant('SMTP_HOSTNAME');
131                 $smtpUser  = constant('SMTP_USER');
132                 $smtpPass1 = constant('SMTP_PASSWORD');
133                 $smtpPass2 = constant('SMTP_PASSWORD');
134
135                 // Overwrite it with the data from sent (failed) form
136                 if (REQUEST_ISSET_POST(('smtp_host'))) $smtpHost = REQUEST_POST('smtp_host');
137                 if (REQUEST_ISSET_POST(('smtp_user'))) $smtpUser = REQUEST_POST('smtp_user');
138
139                 // MySQL settings
140                 define('__MYSQL_HOST'   , $mysql['host']);
141                 define('__MYSQL_DBASE'  , $mysql['dbase']);
142                 define('__MYSQL_PREFIX' , $mysql['prefix']);
143                 define('__MYSQL_LOGIN'  , $mysql['login']);
144                 define('__MYSQL_PASS1'  , $mysql['pass1']);
145                 define('__MYSQL_PASS2'  , $mysql['pass2']);
146
147                 // Set constants for SMTP data
148                 define('__SMTP_HOST'    , $smtpHost);
149                 define('__SMTP_USER'    , $smtpUser);
150                 define('__SMTP_PASS1'   , $smtpPass1);
151                 define('__SMTP_PASS2'   , $smtpPass2);
152
153                 // Load template
154                 LOAD_TEMPLATE("install_page3");
155                 break;
156
157         case "5": // Misc settings
158                 // General settings
159                 define('__SPATH_VALUE'  , REQUEST_POST('spath'));
160                 define('__BURL_VALUE'   , REQUEST_POST('burl'));
161                 define('__TITLE_VALUE'  , REQUEST_POST('title'));
162                 define('__SLOGAN_VALUE' , REQUEST_POST('slogan'));
163                 define('__EMAIL_VALUE'  , REQUEST_POST('email'));
164
165                 // MySQL settings
166                 define('__MYSQL_HOST'   , $mysql['host']);
167                 define('__MYSQL_DBASE'  , $mysql['dbase']);
168                 define('__MYSQL_PREFIX' , $mysql['prefix']);
169                 define('__MYSQL_LOGIN'  , $mysql['login']);
170
171                 // SMTP settings
172                 define('__SMTP_HOST'    , REQUEST_POST('smtp_host'));
173                 define('__SMTP_USER'    , REQUEST_POST('smtp_user'));
174                 define('__SMTP_PASS'    , REQUEST_POST('smtp_pass1'));
175                 OUTPUT_HTML("<form action=\"{!__BURL_ACTION!}/install.php?page=finalize\" method=\"POST\" target=\"_self\">
176 <table border=\"0\" cellspacing=\"0\" cellpadding=\"0\" class=\"install_content\">
177 <tr>
178   <td colspan=\"2\" align=\"center\">
179     <div class=\"install\">{--HEADER_TEXT_PAGE5--}</strong>
180   </td>
181 </tr>
182 <tr><td colspan=\"2\" height=\"21\" class=\"seperator\">&nbsp;</td></tr>
183 <tr>
184   <td colspan=\"2\"><span class=\"install_intro\">
185     {--TEXT_PAGE_5--}
186   </span></td>
187 </tr>
188 <tr><td colspan=\"2\" height=\"21\" class=\"seperator\">&nbsp;</td></tr>
189 <tr>
190   <td class=\"install\" align=\"right\">{--LANG_OUTPUT_MODE--}:&nbsp;&nbsp;</td>
191   <td>
192     <select name=\"omode\" size=\"1\" class=\"install_select\">
193       <option value=\"render\"");
194                 if (constant('OUTPUT_MODE') == "render") OUTPUT_HTML(" selected=\"selected\"");
195                 OUTPUT_HTML(">{--MODE_RENDER--}</option>
196       <option value=\"direct\"");
197                 if (constant('OUTPUT_MODE') == "direct") OUTPUT_HTML(" selected=\"selected\"");
198                 OUTPUT_HTML(">{--MODE_DIRECT--}</option>
199     </select>
200   </td>
201 </tr>
202 <tr><td colspan=\"2\" height=\"10\" class=\"seperator\">&nbsp;</td></tr>
203 <tr>
204   <td class=\"install\" align=\"right\">{--WARN_NO_PASSWORD--}:&nbsp;&nbsp;</td>
205   <td>
206     <select name=\"warn_no_pass\" size=\"1\" class=\"install_select\">
207       <option value=\"true\"");
208                 if (isBooleanConstantAndTrue('warn_no_pass')) OUTPUT_HTML(" selected=\"selected\"");
209                 OUTPUT_HTML(">{--YES--}</option>
210       <option value=\"false\"");
211                 if (!isBooleanConstantAndTrue('warn_no_pass')) OUTPUT_HTML(" selected=\"selected\"");
212                 OUTPUT_HTML(">{--NO--}</option>
213     </select>
214   </td>
215 </tr>
216 <tr><td colspan=\"2\" height=\"10\" class=\"seperator\">&nbsp;</td></tr>
217 <tr>
218   <td class=\"install\" align=\"right\">{--LANG_WRITE_FOOTER--}:&nbsp;&nbsp;</td>
219   <td>
220     <select name=\"wfooter\" size=\"1\" class=\"install_select\">
221       <option value=\"true\"");
222                 if (isBooleanConstantAndTrue('WRITE_FOOTER')) OUTPUT_HTML(" selected=\"selected\"");
223                 OUTPUT_HTML(">{--YES--}</option>
224       <option value=\"false\"");
225                 if (!isBooleanConstantAndTrue('WRITE_FOOTER')) OUTPUT_HTML(" selected=\"selected\"");
226                 OUTPUT_HTML(">{--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=\"true\"");
236                 if (isBooleanConstantAndTrue('ENABLE_BACKLINK')) OUTPUT_HTML(" selected=\"selected\"");
237                 OUTPUT_HTML(">{--YES--}</option>
238       <option value=\"false\"");
239                 if (!isBooleanConstantAndTrue('ENABLE_BACKLINK')) OUTPUT_HTML(" selected=\"selected\"");
240                 OUTPUT_HTML(">{--NO--}</option>
241     </select>
242   </td>
243 </tr>
244 <tr><td colspan=\"2\" height=\"21\" class=\"seperator\">&nbsp;</td></tr>
245 <tr>
246   <td colspan=\"2\" align=\"center\">");
247                 foreach ($mysql as $key => $value) {
248                         OUTPUT_HTML("    <input type=\"hidden\" name=\"mysql[".$key."]\" value=\"".$value."\">");
249                 }
250                 OUTPUT_HTML("    <input type=\"hidden\" name=\"spath\" value=\"{!__SPATH_VALUE!}\">
251     <input type=\"hidden\" name=\"burl\" value=\"{!__BURL_VALUE!}\" />
252     <input type=\"hidden\" name=\"title\" value=\"{!__TITLE_VALUE!}\" />
253     <input type=\"hidden\" name=\"slogan\" value=\"{!__SLOGAN_VALUE!}\" />
254     <input type=\"hidden\" name=\"email\" value=\"{!__EMAIL_VALUE!}\" />
255     <input type=\"hidden\" name=\"smtp_host\" value=\"{!__SMTP_HOST!}\" />
256     <input type=\"hidden\" name=\"smtp_user\" value=\"{!__SMTP_USER!}\" />
257     <input type=\"hidden\" name=\"smtp_pass\" value=\"{!__SMTP_PASS!}\" />
258     <input type=\"submit\" class=\"admin_submit submit\" name=\"finalize\" value=\"{--NEXT_PAGE--}\" class=\"install_submit\" />
259   </td>
260 </tr>
261 <tr><td colspan=\"2\" height=\"21\" class=\"seperator\">&nbsp;</td></tr>
262 </table>
263 </form>");
264                 break;
265
266         case "finalize": // Write captured data to files
267                 if ((REQUEST_ISSET_POST(('finalize'))) && (!isBooleanConstantAndTrue('mxchange_installed'))) {
268                         // You have submitted data then we have to reset the fatal messages
269                         $SQLs = array();
270
271                         // Connect to MySQL server
272                         SQL_CONNECT($mysql['host'], $mysql['login'], $mysql['pass1'], __FILE__, __LINE__);
273                         if (SQL_IS_LINK_UP()) {
274                                 // Seems to work, also right database?
275                                 if (SQL_SELECT_DB($mysql['dbase'], __FILE__, __LINE__) === true) {
276                                         // Automatically run install.sql
277                                         if ((FILE_READABLE(REQUEST_POST('spath')."install/tables.sql")) && (FILE_READABLE(REQUEST_POST('spath')."install/menu-".GET_LANGUAGE().".sql"))) {
278                                                 // Both exists so import them
279                                                 foreach (array("tables.sql", "menu-".GET_LANGUAGE().".sql") as $dump) {
280                                                         // Should be save here because file_exists() is there but we check it again. :)
281                                                         $FQFN = secureString(REQUEST_POST('spath')) . "install/" . $dump;
282                                                         if (FILE_READABLE($FQFN)) {
283                                                                 // Read the file
284                                                                 $SQLs = READ_FILE($FQFN, true);
285
286                                                                 // And split it up against ;\n ...
287                                                                 $SQLs = merge_array($SQLs, explode(";\n", $sql));
288                                                         } else {
289                                                                 // Not readable!
290                                                                 debug_report_bug(sprintf("SQL dump %s is not readable!", $dump));
291                                                         }
292                                                 } // END - foreach
293
294                                                 // Are some SQLs found?
295                                                 if (count($SQLs) == 0) {
296                                                         // Abort here
297                                                         addFatalMessage(getMessage('INSTALL_SQL_IMPORT_FAILED'));
298                                                         return;
299                                                 } // END - if
300
301                                                 // Now run all queries through and try to keep out empty or comment queries
302                                                 foreach ($SQLs as $sql) {
303                                                         // Trim spaces away
304                                                         $sql = trim($sql);
305
306                                                         // Is this query not empty and not a comment?
307                                                         if ((!empty($sql)) && (substr($sql, 0, 2) != "--") && (substr($sql, 0, 1) != "#")) {
308                                                                 // Then run it!
309                                                                 SQL_QUERY($sql, __FILE__, __LINE__);
310                                                         } // END - if
311                                                 } // END - foreach
312
313                                                 // Ok, all done. So we can write the config data to the php files
314                                                 if (REQUEST_POST('spath') != constant('PATH')) changeDataInFile(REQUEST_POST('spath')."inc/config.php", "SERVER-PATH", "define('PATH', \"", "\");", REQUEST_POST('spath'), 0);
315                                                 if (REQUEST_POST('burl')  != constant('URL'))  changeDataInFile(REQUEST_POST('spath')."inc/config.php", "HOST-URL", "define('URL', \"", "\");", REQUEST_POST('burl'), 0);
316                                                 changeDataInFile(REQUEST_POST('spath')."inc/config.php", "MAIN_TITLE", "define('MAIN_TITLE', \"", "\");", REQUEST_POST('title'), 0);
317                                                 changeDataInFile(REQUEST_POST('spath')."inc/config.php", "SLOGAN", "define('SLOGAN', \"", "\");", REQUEST_POST('slogan'), 0);
318                                                 changeDataInFile(REQUEST_POST('spath')."inc/config.php", "WEBMASTER", "define('WEBMASTER', \"", "\");", REQUEST_POST('email'), 0);
319                                                 changeDataInFile(REQUEST_POST('spath')."inc/config.php", "NULLPASS-WARNING", "define('warn_no_pass', ", ");", REQUEST_POST('warn_no_pass'), 0);
320                                                 changeDataInFile(REQUEST_POST('spath')."inc/config.php", "WRITE-FOOTER", "define('WRITE_FOOTER', ", ");", REQUEST_POST('wfooter'), 0);
321                                                 changeDataInFile(REQUEST_POST('spath')."inc/config.php", "BACKLINK", "define('ENABLE_BACKLINK', ", ");", REQUEST_POST('blink'), 0);
322                                                 // DEACTIVATED: changeDataInFile(REQUEST_POST('spath')."inc/config.php", "OUTPUT-MODE", "define('OUTPUT_MODE', \"", "\");", REQUEST_POST('omode'), 0);
323                                                 changeDataInFile(REQUEST_POST('spath')."inc/config.php", "MYSQL-HOST", "        'host'     => \"", "\",", $mysql['host'], 0);
324                                                 changeDataInFile(REQUEST_POST('spath')."inc/config.php", "MYSQL-DBASE", "       'dbase'    => \"", "\",", $mysql['dbase'], 0);
325                                                 changeDataInFile(REQUEST_POST('spath')."inc/config.php", "MYSQL-LOGIN", "       'login'    => \"", "\",", $mysql['login'], 0);
326                                                 changeDataInFile(REQUEST_POST('spath')."inc/config.php", "MYSQL-PASSWORD", "    'password' => \"", "\",", $mysql['pass1'], 0);
327                                                 changeDataInFile(REQUEST_POST('spath')."inc/config.php", "MYSQL-PREFIX", "define('_MYSQL_PREFIX', \"", "\");", $mysql['prefix'], 0);
328                                                 changeDataInFile(REQUEST_POST('spath')."inc/config.php", "SMTP-HOSTNAME", "define('SMTP_HOSTNAME', \"", "\");", REQUEST_POST('smtp_host'), 0);
329                                                 changeDataInFile(REQUEST_POST('spath')."inc/config.php", "SMTP-USER", "define('SMTP_USER', \"", "\");", REQUEST_POST('smtp_user'), 0);
330                                                 changeDataInFile(REQUEST_POST('spath')."inc/config.php", "SMTP-PASSWORD", "define('SMTP_PASSWORD', \"", "\");", REQUEST_POST('smtp_pass1'), 0);
331                                                 changeDataInFile(REQUEST_POST('spath')."inc/config.php", "INSTALLED", "define('mxchange_installed', ", ");", "true", 0);
332                                         } else {
333                                                 // Installation area not found!
334                                                 addFatalMessage(getMessage('INSTALL_MISSING_DUMPS'));
335                                         }
336                                 } // END - if
337                         } // END - if
338
339                         if (getTotalFatalErrors() > 0) {
340                                 $OUT = "";
341                                 foreach (getFatalArray() as $value) {
342                                         $OUT .= "    <li>".$value."</li>\n";
343                                 } // END foreach
344                                 define('__FATAL_ERROR_LI', $OUT);
345                                 $OUT = "";
346                                 foreach ($mysql as $key => $value) {
347                                         $OUT .= "    <input type=\"hidden\" name=\"mysql[".$key."]\" value=\"".$value."\">\n";
348                                 } // END foreach
349                                 define('__MYSQL_DATA'   , $OUT);
350                                 define('__SPATH_VALUE'  , REQUEST_POST('spath'));
351                                 define('__BURL_VALUE'   , REQUEST_POST('burl'));
352                                 define('__TITLE_VALUE'  , REQUEST_POST('title'));
353                                 define('__SMTP_HOST'    , REQUEST_POST('smtp_host'));
354                                 define('__SMTP_USER'    , REQUEST_POST('smtp_user'));
355                                 define('__SMTP_PASS'    , REQUEST_POST('smtp_pass1'));
356
357                                 // Load template
358                                 LOAD_TEMPLATE("install_fatal_errors");
359                         } else {
360                                 // Installation is done!
361                                 LOAD_URL("install.php?page=finalize");
362                         }
363                 } elseif (isBooleanConstantAndTrue('mxchange_installed')) {
364                         // Redirection after writing data... :-)
365                         LOAD_TEMPLATE("install_finished");
366                 } else {
367                         // Something goes wrong during installation! :-(
368                         addFatalMessage(getMessage('INSTALL_FINALIZER_FAILED'));
369                         LOAD_INC("inc/fatal_errors.php");
370                 }
371                 break;
372
373         default:
374                 DEBUG_LOG(__FILE__, __LINE__, sprintf("Wrong page %s detected", REQUEST_GET('page')));
375                 OUTPUT_HTML("    <div class=\"install_error\">{--WRONG_PAGE--}</strong>");
376                 break;
377         }
378 } else {
379         // Already installed!
380         addFatalMessage(getMessage('ALREADY_INSTALLED'));
381 }
382
383 //
384 ?>