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