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