Various fixes to fix bug #226, comment #562, removed extension dependency register...
[mailer.git] / inc / install-inc.php
1 <?php
2 /************************************************************************
3  * Mailer v0.2.1-FINAL                                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  * -------------------------------------------------------------------- *
18  * Copyright (c) 2003 - 2009 by Roland Haeder                           *
19  * Copyright (c) 2009 - 2012 by Mailer Developer Team                   *
20  * For more information visit: http://mxchange.org                      *
21  *                                                                      *
22  * This program is free software; you can redistribute it and/or modify *
23  * it under the terms of the GNU General Public License as published by *
24  * the Free Software Foundation; either version 2 of the License, or    *
25  * (at your option) any later version.                                  *
26  *                                                                      *
27  * This program is distributed in the hope that it will be useful,      *
28  * but WITHOUT ANY WARRANTY; without even the implied warranty of       *
29  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the        *
30  * GNU General Public License for more details.                         *
31  *                                                                      *
32  * You should have received a copy of the GNU General Public License    *
33  * along with this program; if not, write to the Free Software          *
34  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,               *
35  * MA  02110-1301  USA                                                  *
36  ************************************************************************/
37
38 // Some security stuff...
39 if ((!defined('__SECURITY')) || (!isInstallationPhase())) {
40         die();
41 } // END - if
42
43 // Load extension timezone
44 loadExtension('timezone', 'test');
45
46 // Set config entry
47 setConfigEntry('timezone', 'Europe/Berlin');
48
49 // And init timezone
50 FILTER_INIT_TIMEZONE(array());
51
52 // Init variables
53 $GLOBALS['install_mysql'] = array();
54 if ((isPostRequestElementSet('mysql')) && (ifPostContainsSelections('mysql'))) {
55         // Transfer 'mysql' array
56         $GLOBALS['install_mysql'] = postRequestElement('mysql');
57 } // END - if
58
59 // Check if both passwords from SMTP are matching
60 if ((isGetRequestElementSet('install_page') && (getRequestElement('install_page') == 5))) {
61         // Okay, we have to check it
62         if (isPostRequestElementSet('smtp_user') && (!isPostRequestElementSet('smtp_host'))) {
63                 // Hostname not set
64                 addToInstallContent('<div class="para">{--INSTALLER_SMTP_HOSTNAME_EMPTY--}</div>');
65                 setGetRequestElement('install_page', 3);
66         } // END - if
67
68         if ((!isPostRequestElementSet('smtp_pass1')) && (isPostRequestElementSet('smtp_pass2'))) {
69                 // Password is empty
70                 addToInstallContent('<div class="para">{--INSTALLER_SMTP_PASSWORD1_EMPTY--}</div>');
71                 setGetRequestElement('install_page', 3);
72         } // END - if
73
74         if ((isPostRequestElementSet('smtp_pass1')) && (!isPostRequestElementSet('smtp_pass2'))) {
75                 // Password repeat is empty
76                 addToInstallContent('<div class="para">{--INSTALLER_SMTP_PASSWORD2_EMPTY--}</div>');
77                 setGetRequestElement('install_page', 3);
78         } // END - if
79
80         if (postRequestElement('smtp_pass1') != postRequestElement('smtp_pass1')) {
81                 // Passwords are not matching
82                 addToInstallContent('<div class="para">{--INSTALLER_SMTP_PASS_MISMATCH--}</div>');
83                 setGetRequestElement('install_page', 3);
84         } // END - if
85 } // END - if
86
87 // Is the mailer installed or no admin registered so far?
88 if ((!isInstalled()) || (!isAdminRegistered())) {
89         // Output page for entered value
90         switch (getRequestElement('install_page')) {
91                 case 'welcome': // Welcome to the installation!
92                         addTemplateToInstallContent('install_welcome');
93                         break;
94
95                 case '1': // Server path, base URL
96                         // Load template
97                         addTemplateToInstallContent('install_page1');
98                         break;
99
100                 case '2': // Database login data
101                         if (empty($GLOBALS['install_mysql']['dbase']))  $GLOBALS['install_mysql']['dbase']  = 'your_database';
102                         if (empty($GLOBALS['install_mysql']['login']))  $GLOBALS['install_mysql']['login']  = 'your_login';
103                         if (empty($GLOBALS['install_mysql']['host']))   $GLOBALS['install_mysql']['host']   = 'localhost';
104                         if (empty($GLOBALS['install_mysql']['prefix'])) $GLOBALS['install_mysql']['prefix'] = 'mailer';
105                         if (empty($GLOBALS['install_mysql']['type']))   $GLOBALS['install_mysql']['type']   = 'MyISAM';
106                         if (ifFatalErrorsDetected()) {
107                                 addToInstallContent('<div class="install_fatal"><ul>');
108                                 foreach (getFatalArray() as $key => $err) {
109                                         addToInstallContent('<li> {--FATAL_NO--}' . ($key + 1) . ': ' . $err . '</li>');
110                                 } // END - foreach
111                                 addToInstallContent('</ol></div>');
112                         } // END - if
113
114                         $content['mysql_host']   = $GLOBALS['install_mysql']['host'];
115                         $content['mysql_dbase']  = $GLOBALS['install_mysql']['dbase'];
116                         $content['mysql_prefix'] = $GLOBALS['install_mysql']['prefix'];
117                         $content['mysql_login']  = $GLOBALS['install_mysql']['login'];
118                         $content['table_type']   = $GLOBALS['install_mysql']['type'];
119                         $content['spath']        = postRequestElement('spath');
120                         $content['burl']         = postRequestElement('burl');
121                         $content['title']        = postRequestElement('title');
122                         $content['slogan']       = postRequestElement('slogan');
123                         $content['email']        = postRequestElement('email');
124
125                         // Load template
126                         addTemplateToInstallContent('install_page2', $content);
127                         break;
128
129                 case '3':
130                         // Set more values
131                         $content['spath']      = postRequestElement('spath');
132                         $content['burl']       = postRequestElement('burl');
133                         $content['title']      = postRequestElement('title');
134                         $content['slogan']     = postRequestElement('slogan');
135                         $content['email']      = postRequestElement('email');
136
137                         // Use default SMTP data
138                         $smtpHost  = '{?SMTP_HOSTNAME?}';
139                         $smtpUser  = '{?SMTP_USER?}';
140                         $smtpPass1 = '{?SMTP_PASSWORD?}';
141                         $smtpPass2 = '{?SMTP_PASSWORD?}';
142
143                         // Overwrite it with the data from sent (failed) form
144                         if (isPostRequestElementSet('smtp_host')) $smtpHost = postRequestElement('smtp_host');
145                         if (isPostRequestElementSet('smtp_user')) $smtpUser = postRequestElement('smtp_user');
146                         if (isPostRequestElementSet('smtp_pass')) {
147                                 $smtpPass1 = postRequestElement('smtp_pass');
148                                 $smtpPass2 = postRequestElement('smtp_pass');
149                         } // END - if
150
151                         // Database login data
152                         $content['mysql_host']   = $GLOBALS['install_mysql']['host'];
153                         $content['mysql_dbase']  = $GLOBALS['install_mysql']['dbase'];
154                         $content['mysql_prefix'] = $GLOBALS['install_mysql']['prefix'];
155                         $content['table_type']   = $GLOBALS['install_mysql']['type'];
156                         $content['mysql_login']  = $GLOBALS['install_mysql']['login'];
157                         $content['mysql_pass1']  = $GLOBALS['install_mysql']['pass1'];
158                         $content['mysql_pass2']  = $GLOBALS['install_mysql']['pass2'];
159
160                         // Set constants for SMTP data
161                         $content['smtp_host']  = $smtpHost;
162                         $content['smtp_user']  = $smtpUser;
163                         $content['smtp_pass1'] = $smtpPass1;
164                         $content['smtp_pass2'] = $smtpPass2;
165
166                         // Load template
167                         addTemplateToInstallContent('install_page3', $content);
168                         break;
169
170                 case '5': // Misc settings
171                         // General settings
172                         $content['spath']  = postRequestElement('spath');
173                         $content['burl']   = postRequestElement('burl');
174                         $content['title']  = postRequestElement('title');
175                         $content['slogan'] = postRequestElement('slogan');
176                         $content['email']  = postRequestElement('email');
177
178                         // SMTP settings
179                         $content['smtp_host'] = postRequestElement('smtp_host');
180                         $content['smtp_user'] = postRequestElement('smtp_user');
181                         $content['smtp_pass'] = postRequestElement('smtp_pass1');
182
183                         // Database login data
184                         $OUT = '';
185                         foreach ($GLOBALS['install_mysql'] as $key => $value) {
186                                 $OUT .= '    <input type="hidden" name="mysql[' . $key . ']" value="' . $value . '" />';
187                         } // END - foreach
188                         $content['mysql_hidden'] = $OUT;
189
190                         // Load template
191                         addTemplateToInstallContent('install_page5', $content);
192                         break;
193
194                 case 'finalize': // Write captured data to files
195                         if ((isPostRequestElementSet('finalize')) && (!isInstalled())) {
196                                 // You have submitted data then we have to reset the SQLs
197                                 initSqls();
198
199                                 /*
200                                  * Restore PHPs error handler to prevent ours to handle errors,
201                                  * e.g. failed connection attempts. We want to handle them on
202                                  * our own.
203                                  */
204                                 restore_error_handler();
205
206                                 // Connect to database server
207                                 SQL_CONNECT($GLOBALS['install_mysql']['host'], $GLOBALS['install_mysql']['login'], $GLOBALS['install_mysql']['pass1'], __FILE__, __LINE__);
208
209                                 // Is the link up?
210                                 if (SQL_IS_LINK_UP()) {
211                                         // Seems to work, also right database?
212                                         if (SQL_SELECT_DB($GLOBALS['install_mysql']['dbase'], __FILE__, __LINE__) === true) {
213                                                 // Check for dumps
214                                                 if ((!isFileReadable(postRequestElement('spath') . 'install/tables.sql')) || (!isFileReadable(postRequestElement('spath') . 'install/menu-'.getLanguage().'.sql'))) {
215                                                         // Installation area not found
216                                                         reportBug(__FILE__, __LINE__, 'SQL dumps not found. Please extract ALL files from the archive or checkout all files out from SVN.');
217                                                         return;
218                                                 } // END - if
219
220                                                 // Any errors detected?
221                                                 if (!ifFatalErrorsDetected()) {
222                                                         // Set type, prefix from POST data and database name for later queries
223                                                         setConfigEntry('_TABLE_TYPE'  , postRequestElement('mysql', 'type'));
224                                                         setConfigEntry('_MYSQL_PREFIX', postRequestElement('mysql', 'prefix'));
225                                                         setConfigEntry('__DB_NAME'    , $GLOBALS['install_mysql']['dbase']);
226
227                                                         // Both exists so import them
228                                                         foreach (array('tables', 'menu-'.getLanguage()) as $dump) {
229                                                                 // Should be save here because file_exists() is there but we check it again. :)
230                                                                 $FQFN = postRequestElement('spath') . 'install/' . $dump . '.sql';
231
232                                                                 // Is the file readable?
233                                                                 if (isFileReadable($FQFN)) {
234                                                                         // Read the file
235                                                                         $fileContent = readSqlDump($FQFN);
236
237                                                                         // Split it up against ";\n" and merge it into existing SQLs
238                                                                         mergeSqls(explode(";\n", $fileContent), 'install');
239                                                                 } else {
240                                                                         // Not readable!
241                                                                         reportBug(__FILE__, __LINE__, sprintf("SQL dump %s is not readable.", $dump));
242                                                                 }
243                                                         } // END - foreach
244                                                         //* DEBUG: */ die(__FUNCTION__.'['.__LINE__.']:'<pre>'.print_r(getSqls(), true).'</pre>');
245
246                                                         // Are some SQLs found?
247                                                         if (countSqls() == 0) {
248                                                                 // Abort here
249                                                                 addFatalMessage(__FILE__, __LINE__, '{--INSTALLER_SQL_IMPORT_FAILED--}');
250                                                                 return;
251                                                         } // END - if
252
253                                                         // Now run all queries through
254                                                         runFilterChain('run_sqls');
255
256                                                         // Copy the config template and verify it
257                                                         doInstallWriteLocalConfigurationFile(postRequestElement('spath'));
258                                                 } // END - if
259                                         } // END - if
260                                 } // END - if
261
262                                 // Are some fatal errors there?
263                                 if (ifFatalErrorsDetected()) {
264                                         $OUT = '';
265                                         foreach (getFatalArray() as $value) {
266                                                 $OUT .= '    <li>' . $value . '</li>';
267                                         } // END foreach
268                                         $content['fatal_errors'] = $OUT;
269                                         $OUT = '';
270                                         foreach ($GLOBALS['install_mysql'] as $key => $value) {
271                                                 $OUT .= '    <input type="hidden" name="mysql[' . $key . ']" value="' . $value . '" />';
272                                         } // END foreach
273                                         $content['mysql_hidden'] = $OUT;
274                                         $content['spath']      = postRequestElement('spath');
275                                         $content['burl']       = postRequestElement('burl');
276                                         $content['title']      = postRequestElement('title');
277                                         $content['smtp_host']  = postRequestElement('smtp_host');
278                                         $content['smtp_user']  = postRequestElement('smtp_user');
279                                         $content['smtp_pass']  = postRequestElement('smtp_pass1');
280
281                                         // Load template
282                                         addTemplateToInstallContent('install_fatal_errors', $content);
283
284                                         // We have handled all fatal errors here
285                                         initFatalMessages();
286                                 } else {
287                                         // Register ext-sql_patches and ext-task
288                                         if ((registerExtension('sql_patches', NULL)) && (registerExtension('task', NULL))) {
289                                                 // Installation is done!
290                                                 redirectToUrl('install.php?install_page=finished');
291                                         } else {
292                                                 // Something goes wrong on registration of ext-sql_patches
293                                                 addFatalMessage(__FILE__, __LINE__, '{--INSTALLER_FINALIZER_CANNOT_REGISTER_SQL_PATCHES--}');
294                                         }
295                                 }
296                         } else {
297                                 // Something goes wrong during installation! :-(
298                                 addFatalMessage(__FILE__, __LINE__, '{--INSTALLER_FINALIZER_FAILED--}');
299                         }
300                         break;
301
302                 case 'finished':
303                         if (isInstalled()) {
304                                 // Load template that we are finished
305                                 addTemplateToInstallContent('install_finished');
306                         } else {
307                                 // Not finished
308                                 redirectToUrl('install.php');
309                         }
310                         break;
311
312                 default:
313                         logDebugMessage(__FILE__, __LINE__, sprintf("Wrong install_page=%s detected", getRequestElement('install_page')));
314                         addTemplateToInstallContent('admin_settings_unsaved', '{--WRONG_PAGE--}');
315                         break;
316         } // END - switch
317 } else {
318         // Already installed!
319         addFatalMessage(__FILE__, __LINE__, '{--ALREADY_INSTALLED--}');
320 }
321
322 // [EOF]
323 ?>