Huge script change, see http://forum.mxchange.org/topic-458.html 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 - 2009 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         die();
46 } // END - if
47
48 // Init variables
49 $mysql = array();
50 if ((isPostRequestElementSet('mysql')) && (is_array(postRequestElement('mysql')))) $mysql = postRequestElement('mysql');
51
52 // Check if both passwords from SMTP are matching
53 if ((isGetRequestElementSet('page') && (getRequestElement('page') == 5))) {
54         // Okay, we have to check it
55         if (isPostRequestElementSet('smtp_user') && (!isPostRequestElementSet('smtp_host'))) {
56                 // Hostname not set
57                 addToInstallContent(getMessage('INSTALL_SMTP_HOSTNAME_EMPTY') . '<br />');
58                 setRequestGetElement('page', 3);
59         } // END - if
60
61         if ((!isPostRequestElementSet('smtp_pass1')) && (isPostRequestElementSet('smtp_pass2'))) {
62                 // Password is empty
63                 addToInstallContent(getMessage('INSTALL_SMTP_PASS1_EMPTY') . '<br />');
64                 setRequestGetElement('page', 3);
65         } // END - if
66
67         if ((isPostRequestElementSet('smtp_pass1')) && (!isPostRequestElementSet('smtp_pass2'))) {
68                 // Password repeat is empty
69                 addToInstallContent(getMessage('INSTALL_SMTP_PASS2_EMPTY') . '<br />');
70                 setRequestGetElement('page', 3);
71         } // END - if
72
73         if (postRequestElement('smtp_pass1') != postRequestElement('smtp_pass1')) {
74                 // Passwords are not matching
75                 addToInstallContent(getMessage('INSTALL_SMTP_PASS_MISMATCH') . '<br />');
76                 setRequestGetElement('page', 3);
77         } // END - if
78 } // END - if
79
80 // Is MXChange installed or no admin registered so far?
81 if ((!isInstalled()) || (!isAdminRegistered())) {
82         // Output page for entered value
83         switch (getRequestElement('page')) {
84                 case 'welcome': // Welcome to the installation!
85                         addTemplateToInstallContent('install_welcome');
86                         break;
87
88                 case '1': // Server path, base URL
89                         // Load template
90                         addTemplateToInstallContent('install_page1');
91                         break;
92
93                 case '2': // MySQL data (alone!)
94                         if (empty($mysql['dbase']))  $mysql['dbase']  = 'your_database';
95                         if (empty($mysql['login']))  $mysql['login']  = 'your_login';
96                         if (empty($mysql['host']))   $mysql['host']   = 'localhost';
97                         if (empty($mysql['prefix'])) $mysql['prefix'] = 'mxchange';
98                         if (empty($mysql['type']))   $mysql['type']   = 'MyISAM';
99                         if (getTotalFatalErrors() > 0) {
100                                 addToInstallContent('<span class="install_fatal">');
101                                 foreach (getFatalArray() as $key => $err) {
102                                         addToInstallContent('<strong>&middot;</strong>&nbsp;{--FATAL_NO--}' . ($key + 1) . ':&nbsp;' . $err . '<br />');
103                                 }
104                                 addToInstallContent('</span><br />');
105                         }
106                         $content['mysql_host']   = $mysql['host'];
107                         $content['mysql_dbase']  = $mysql['dbase'];
108                         $content['mysql_prefix'] = $mysql['prefix'];
109                         $content['mysql_login']  = $mysql['login'];
110                         $content['table_type']   = $mysql['type'];
111                         $content['spath']        = postRequestElement('spath');
112                         $content['burl']         = postRequestElement('burl');
113                         $content['title']        = postRequestElement('title');
114                         $content['slogan']       = postRequestElement('slogan');
115                         $content['email']        = postRequestElement('email');
116
117                         // Load template
118                         addTemplateToInstallContent('install_page2', false, $content);
119                         break;
120
121                 case '3':
122                         // Set more values
123                         $content['spath']      = postRequestElement('spath');
124                         $content['burl']       = postRequestElement('burl');
125                         $content['title']      = postRequestElement('title');
126                         $content['slogan']     = postRequestElement('slogan');
127                         $content['email']      = postRequestElement('email');
128
129                         // Use default SMTP data
130                         $smtpHost  = getConfig('SMTP_HOSTNAME');
131                         $smtpUser  = getConfig('SMTP_USER');
132                         $smtpPass1 = getConfig('SMTP_PASSWORD');
133                         $smtpPass2 = getConfig('SMTP_PASSWORD');
134
135                         // Overwrite it with the data from sent (failed) form
136                         if (isPostRequestElementSet('smtp_host')) $smtpHost = postRequestElement('smtp_host');
137                         if (isPostRequestElementSet('smtp_user')) $smtpUser = postRequestElement('smtp_user');
138                         if (isPostRequestElementSet('smtp_pass')) {
139                                 $smtpPass1 = postRequestElement('smtp_pass');
140                                 $smtpPass2 = postRequestElement('smtp_pass');
141                         } // END - if
142
143                         // MySQL settings
144                         $content['mysql_host']   = $mysql['host'];
145                         $content['mysql_dbase']  = $mysql['dbase'];
146                         $content['mysql_prefix'] = $mysql['prefix'];
147                         $content['table_type']   = $mysql['type'];
148                         $content['mysql_login']  = $mysql['login'];
149                         $content['mysql_pass1']  = $mysql['pass1'];
150                         $content['mysql_pass2']  = $mysql['pass2'];
151
152                         // Set constants for SMTP data
153                         $content['smtp_host']  = $smtpHost;
154                         $content['smtp_user']  = $smtpUser;
155                         $content['smtp_pass1'] = $smtpPass1;
156                         $content['smtp_pass2'] = $smtpPass2;
157
158                         // Load template
159                         addTemplateToInstallContent('install_page3', false, $content);
160                         break;
161
162                 case '5': // Misc settings
163                         // General settings
164                         $content['spath']  = postRequestElement('spath');
165                         $content['burl']   = postRequestElement('burl');
166                         $content['title']  = postRequestElement('title');
167                         $content['slogan'] = postRequestElement('slogan');
168                         $content['email']  = postRequestElement('email');
169
170                         // MySQL settings
171                         $content['mysql_host']   = $mysql['host'];
172                         $content['mysql_dbase']  = $mysql['dbase'];
173                         $content['mysql_prefix'] = $mysql['prefix'];
174                         $content['mysql_login']  = $mysql['login'];
175                         $content['table_type']   = $mysql['type'];
176
177                         // SMTP settings
178                         $content['smtp_host'] = postRequestElement('smtp_host');
179                         $content['smtp_user'] = postRequestElement('smtp_user');
180                         $content['smtp_pass'] = postRequestElement('smtp_pass1');
181
182                         // MySQL data
183                         $OUT = '';
184                         foreach ($mysql as $key => $value) {
185                                 $OUT .= "    <input type=\"hidden\" name=\"mysql[" . $key . "]\" value=\"" . $value . "\" />\n";
186                         }
187                         $content['mysql_hidden'] = $OUT;
188
189                         // Load template
190                         addTemplateToInstallContent('install_page5', false, $content);
191                         break;
192
193                 case 'finalize': // Write captured data to files
194                         if ((isPostRequestElementSet('finalize')) && (!isInstalled())) {
195                                 // You have submitted data then we have to reset the SQLs
196                                 initSqls();
197
198                                 // Connect to MySQL server
199                                 SQL_CONNECT($mysql['host'], $mysql['login'], $mysql['pass1'], __FILE__, __LINE__);
200                                 if (SQL_IS_LINK_UP()) {
201                                         // Seems to work, also right database?
202                                         if (SQL_SELECT_DB($mysql['dbase'], __FILE__, __LINE__) === true) {
203                                                 // Automatically run install.sql
204                                                 if ((!isFileReadable(postRequestElement('spath') . 'install/tables.sql')) || (!isFileReadable(postRequestElement('spath') . 'install/menu-'.getLanguage().'.sql'))) {
205                                                         // Installation area not found!
206                                                         addFatalMessage(__FILE__, __LINE__, getMessage('INSTALL_MISSING_DUMPS'));
207                                                 } // END - if
208
209                                                 if (getTotalFatalErrors() == 0) {
210                                                         // Set type and prefix from POST data
211                                                         setConfigEntry('_TABLE_TYPE'  , postRequestElement('mysql', 'type'));
212                                                         setConfigEntry('_MYSQL_PREFIX', postRequestElement('mysql', 'prefix'));
213
214                                                         // Both exists so import them
215                                                         foreach (array('tables', 'menu-'.getLanguage()) as $dump) {
216                                                                 // Should be save here because file_exists() is there but we check it again. :)
217                                                                 $FQFN = postRequestElement('spath') . 'install/' . $dump . '.sql';
218
219                                                                 // Is the file readable?
220                                                                 if (isFileReadable($FQFN)) {
221                                                                         // Read the file
222                                                                         $fileContent = readFromFile($FQFN, true);
223
224                                                                         // Compile all config entries (we use a filter here, yes...)
225                                                                         $fileContent = FILTER_COMPILE_CONFIG($fileContent);
226
227                                                                         // Split it up against ";\n" and merge it into existing SQLs
228                                                                         mergeSqls(explode(";\n", $fileContent), 'install');
229                                                                 } else {
230                                                                         // Not readable!
231                                                                         debug_report_bug(sprintf("SQL dump %s is not readable!", $dump));
232                                                                 }
233                                                         } // END - foreach
234                                                         //* DEBUG: */ die('<pre>'.print_r(getSqls(), true).'</pre>');
235
236                                                         // Are some SQLs found?
237                                                         if (countSqls() == 0) {
238                                                                 // Abort here
239                                                                 addFatalMessage(__FILE__, __LINE__, getMessage('INSTALL_SQL_IMPORT_FAILED'));
240                                                                 return;
241                                                         } // END - if
242
243                                                         // Now run all queries through and try to keep out empty or comment queries
244                                                         runFilterChain('run_sqls');
245
246                                                         // Copy the config template and verify it
247                                                         doInstallWriteLocalConfig();
248                                                 } // END - if
249                                         } // END - if
250                                 } // END - if
251
252                                 if (getTotalFatalErrors() > 0) {
253                                         $OUT = '';
254                                         foreach (getFatalArray() as $value) {
255                                                 $OUT .= "    <li>" . $value . "</li>\n";
256                                         } // END foreach
257                                         $content['fatal_errors'] = $OUT;
258                                         $OUT = '';
259                                         foreach ($mysql as $key => $value) {
260                                                 $OUT .= "    <input type=\"hidden\" name=\"mysql[" . $key . "]\" value=\"" . $value . "\" />\n";
261                                         } // END foreach
262                                         $content['mysql_hidden'] = $OUT;
263                                         $content['spath']      = postRequestElement('spath');
264                                         $content['burl']       = postRequestElement('burl');
265                                         $content['title']      = postRequestElement('title');
266                                         $content['smtp_host']  = postRequestElement('smtp_host');
267                                         $content['smtp_user']  = postRequestElement('smtp_user');
268                                         $content['smtp_pass']  = postRequestElement('smtp_pass1');
269
270                                         // Load template
271                                         addTemplateToInstallContent('install_fatal_errors', false, $content);
272                                 } else {
273                                         // Installation is done!
274                                         redirectToUrl('install.php?page=finished');
275                                 }
276                         } else {
277                                 // Something goes wrong during installation! :-(
278                                 addFatalMessage(__FILE__, __LINE__, getMessage('INSTALL_FINALIZER_FAILED'));
279                         }
280                         break;
281
282                 case 'finished':
283                         if (isInstalled()) {
284                                 // Load template that we are finished
285                                 addTemplateToInstallContent('install_finished');
286                         } else {
287                                 // Not finished
288                                 redirectToUrl('install.php');
289                         }
290                         break;
291
292                 default:
293                         logDebugMessage(__FILE__, __LINE__, sprintf("Wrong page %s detected", getRequestElement('page')));
294                         addTemplateToInstallContent('admin_settings_saved', "<div class=\"install_error\">{--WRONG_PAGE--}</div>");
295                         break;
296         } // END - switch
297 } else {
298         // Already installed!
299         addFatalMessage(__FILE__, __LINE__, getMessage('ALREADY_INSTALLED'));
300 }
301
302 // [EOF]
303 ?>