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