c45dc021bd2bcb9c67a6c271cd8389e319d8dc21
[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 $mysql = array();
51 if ((isPostRequestParameterSet('mysql')) && (is_array(postRequestParameter('mysql')))) {
52         // Transfer 'mysql' array
53         $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($mysql['dbase']))  $mysql['dbase']  = 'your_database';
99                         if (empty($mysql['login']))  $mysql['login']  = 'your_login';
100                         if (empty($mysql['host']))   $mysql['host']   = 'localhost';
101                         if (empty($mysql['prefix'])) $mysql['prefix'] = 'mxchange';
102                         if (empty($mysql['type']))   $mysql['type']   = 'MyISAM';
103                         if (getTotalFatalErrors() > 0) {
104                                 addToInstallContent('<div class="install_fatal">');
105                                 foreach (getFatalArray() as $key => $err) {
106                                         addToInstallContent('<div class="para"><strong>&middot;</strong>&nbsp;{--FATAL_NO--}' . ($key + 1) . ':&nbsp;' . $err . '</div>');
107                                 }
108                                 addToInstallContent('</div>');
109                         }
110                         $content['mysql_host']   = $mysql['host'];
111                         $content['mysql_dbase']  = $mysql['dbase'];
112                         $content['mysql_prefix'] = $mysql['prefix'];
113                         $content['mysql_login']  = $mysql['login'];
114                         $content['table_type']   = $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  = getConfig('SMTP_HOSTNAME');
135                         $smtpUser  = getConfig('SMTP_USER');
136                         $smtpPass1 = getConfig('SMTP_PASSWORD');
137                         $smtpPass2 = getConfig('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']   = $mysql['host'];
149                         $content['mysql_dbase']  = $mysql['dbase'];
150                         $content['mysql_prefix'] = $mysql['prefix'];
151                         $content['table_type']   = $mysql['type'];
152                         $content['mysql_login']  = $mysql['login'];
153                         $content['mysql_pass1']  = $mysql['pass1'];
154                         $content['mysql_pass2']  = $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 ($mysql as $key => $value) {
182                                 $OUT .= "    <input type=\"hidden\" name=\"mysql[" . $key . "]\" value=\"" . $value . "\" />\n";
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($mysql['host'], $mysql['login'], $mysql['pass1'], __FILE__, __LINE__);
197                                 if (SQL_IS_LINK_UP()) {
198                                         // Seems to work, also right database?
199                                         if (SQL_SELECT_DB($mysql['dbase'], __FILE__, __LINE__) === true) {
200                                                 // Automatically run install.sql
201                                                 if ((!isFileReadable(postRequestParameter('spath') . 'install/tables.sql')) || (!isFileReadable(postRequestParameter('spath') . 'install/menu-'.getLanguage().'.sql'))) {
202                                                         // Installation area not found!
203                                                         addFatalMessage(__FILE__, __LINE__, getMessage('INSTALL_MISSING_DUMPS'));
204                                                 } // END - if
205
206                                                 if (getTotalFatalErrors() == '0') {
207                                                         // Set type and prefix from POST data
208                                                         setConfigEntry('_TABLE_TYPE'  , postRequestParameter('mysql', 'type'));
209                                                         setConfigEntry('_MYSQL_PREFIX', postRequestParameter('mysql', 'prefix'));
210
211                                                         // Both exists so import them
212                                                         foreach (array('tables', 'menu-'.getLanguage()) as $dump) {
213                                                                 // Should be save here because file_exists() is there but we check it again. :)
214                                                                 $FQFN = postRequestParameter('spath') . 'install/' . $dump . '.sql';
215
216                                                                 // Is the file readable?
217                                                                 if (isFileReadable($FQFN)) {
218                                                                         // Read the file
219                                                                         $fileContent = readSqlDump($FQFN);
220
221                                                                         // Split it up against ";\n" and merge it into existing SQLs
222                                                                         mergeSqls(explode(";\n", $fileContent), 'install');
223                                                                 } else {
224                                                                         // Not readable!
225                                                                         debug_report_bug(sprintf("SQL dump %s is not readable!", $dump));
226                                                                 }
227                                                         } // END - foreach
228                                                         //* DEBUG: */ die('<pre>'.print_r(getSqls(), true).'</pre>');
229
230                                                         // Are some SQLs found?
231                                                         if (countSqls() == '0') {
232                                                                 // Abort here
233                                                                 addFatalMessage(__FILE__, __LINE__, getMessage('INSTALL_SQL_IMPORT_FAILED'));
234                                                                 return;
235                                                         } // END - if
236
237                                                         // Now run all queries through and try to keep out empty or comment queries
238                                                         runFilterChain('run_sqls');
239
240                                                         // Copy the config template and verify it
241                                                         doInstallWriteLocalConfig();
242                                                 } // END - if
243                                         } // END - if
244                                 } // END - if
245
246                                 if (getTotalFatalErrors() > 0) {
247                                         $OUT = '';
248                                         foreach (getFatalArray() as $value) {
249                                                 $OUT .= '    <li>' . $value . '</li>';
250                                         } // END foreach
251                                         $content['fatal_errors'] = $OUT;
252                                         $OUT = '';
253                                         foreach ($mysql as $key => $value) {
254                                                 $OUT .= '    <input type="hidden" name="mysql[' . $key . ']" value="' . $value . '" />';
255                                         } // END foreach
256                                         $content['mysql_hidden'] = $OUT;
257                                         $content['spath']      = postRequestParameter('spath');
258                                         $content['burl']       = postRequestParameter('burl');
259                                         $content['title']      = postRequestParameter('title');
260                                         $content['smtp_host']  = postRequestParameter('smtp_host');
261                                         $content['smtp_user']  = postRequestParameter('smtp_user');
262                                         $content['smtp_pass']  = postRequestParameter('smtp_pass1');
263
264                                         // Load template
265                                         addTemplateToInstallContent('install_fatal_errors', $content);
266                                 } else {
267                                         // Installation is done!
268                                         redirectToUrl('install.php?page=finished');
269                                 }
270                         } else {
271                                 // Something goes wrong during installation! :-(
272                                 addFatalMessage(__FILE__, __LINE__, getMessage('INSTALL_FINALIZER_FAILED'));
273                         }
274                         break;
275
276                 case 'finished':
277                         if (isInstalled()) {
278                                 // Load template that we are finished
279                                 addTemplateToInstallContent('install_finished');
280                         } else {
281                                 // Not finished
282                                 redirectToUrl('install.php');
283                         }
284                         break;
285
286                 default:
287                         logDebugMessage(__FILE__, __LINE__, sprintf("Wrong page %s detected", getRequestParameter('page')));
288                         addTemplateToInstallContent('admin_settings_saved', '<div class="install_error">{--WRONG_PAGE--}</div>');
289                         break;
290         } // END - switch
291 } else {
292         // Already installed!
293         addFatalMessage(__FILE__, __LINE__, getMessage('ALREADY_INSTALLED'));
294 }
295
296 // [EOF]
297 ?>