Waring in installer cleanup.
[fa-stable.git] / install / index.php
1 <?php
2 /**********************************************************************
3     Copyright (C) FrontAccounting, LLC.
4         Released under the terms of the GNU General Public License, GPL, 
5         as published by the Free Software Foundation, either version 3 
6         of the License, or (at your option) any later version.
7     This program is distributed in the hope that it will be useful,
8     but WITHOUT ANY WARRANTY; without even the implied warranty of
9     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  
10     See the License here <http://www.gnu.org/licenses/gpl-3.0.html>.
11 ***********************************************************************/
12 $page_security = 'SA_OPEN';
13 $path_to_root="..";
14
15 if (file_exists($path_to_root.'/config_db.php'))
16         header("Location: $path_to_root/index.php");
17
18 include($path_to_root . "/install/isession.inc");
19
20 page(_("FrontAccouting ERP Installation Wizard"), true, false, "", '', false, 'stylesheet.css');
21
22 include($path_to_root . "/includes/ui.inc");
23 include($path_to_root . "/includes/system_tests.inc");
24 include($path_to_root . "/admin/db/maintenance_db.inc");
25 include($path_to_root . "/includes/packages.inc");
26 @include($path_to_root . "/installed_extensions.php");
27 //-------------------------------------------------------------------------------------------------
28
29 function subpage_title($txt) 
30 {
31         global $path_to_root;
32         
33         echo '<center><img src="'.$path_to_root.'/themes/default/images/logo_frontaccounting.png" width="250" height="50" alt="Logo" />
34                 </center>';
35
36         $page = @$_POST['Page'] ? $_POST['Page'] : 1;
37
38         display_heading(
39                 $page == 6 ? $txt :
40                         _("FrontAccouting ERP Installation Wizard").'<br>'
41                         . sprintf(_('Step %d: %s'),  $page , $txt));
42         br();
43 }
44
45 function display_coas()
46 {
47         start_table(TABLESTYLE);
48         $th = array(_("Chart of accounts"), _("Encoding"), _("Description"), _("Install"));
49         table_header($th);
50
51         $k = 0;
52         $charts = get_charts_list();
53
54         foreach($charts as $pkg_name => $coa)
55         {
56                 $available = @$coa['available'];
57                 $installed = @$coa['version'];
58                 $id = @$coa['local_id'];
59
60                 alt_table_row_color($k);
61                 label_cell($coa['name']);
62                 label_cell($coa['encoding']);
63                 label_cell(is_array($coa['Descr']) ? implode('<br>', $coa['Descr']) :  $coa['Descr']);
64                 label_cell($installed ?
65                         _("Installed") : checkbox(null, 'coas['.$coa['package'].']'), "align='center'");
66
67                 end_row();
68         }
69         end_table(1);
70 }
71
72 function display_langs()
73 {
74         start_table(TABLESTYLE);
75         $th = array(_("Language"), _("Encoding"), _("Description"), _("Install"));
76         table_header($th);
77
78         $k = 0;
79         $langs = get_languages_list();
80
81         foreach($langs as $pkg_name => $lang)
82         {
83                 $available = @$lang['available'];
84                 $installed = @$lang['version'];
85                 $id = @$lang['local_id'];
86                 if (!$available) continue;
87
88                 alt_table_row_color($k);
89                 label_cell($lang['name']);
90                 label_cell($lang['encoding']);
91                 label_cell(is_array($lang['Descr']) ? implode('<br>', $lang['Descr']) :  $lang['Descr']);
92                 label_cell($installed ?
93                         _("Installed") : checkbox(null, 'langs['.$lang['package'].']'), "align='center'");
94                 end_row();
95         }
96         end_table(1);
97 }
98
99 function instlang_list_row($label, $name, $value=null) {
100
101         global $inst_langs;
102
103         $langs = array();
104         foreach ($inst_langs as $n => $lang)
105                         $langs[$n] = $lang['name'];
106
107         echo "<td>$label</td>\n" . "<td>\n" 
108                 .array_selector($name, $value, $langs, 
109                         array(
110                                 'select_submit' => true,
111                                 'async' => true
112                         )) . "</td>\n";
113 }
114
115 function install_connect_db() {
116
117         global $db;
118
119         $conn = $_SESSION['inst_set'];
120         
121         $db = mysql_connect($conn["host"] , $conn["dbuser"], $conn["dbpassword"]);
122         if(!$db) {
123                 display_error('Cannot connect to database server. Host name, username and/or password incorrect.');
124                 return false;
125         }
126         if (!defined('TB_PREF'))
127                 define('TB_PREF', $conn["tbpref"]);
128
129         if (!mysql_select_db($conn["dbname"], $db)) {
130                 $sql = "CREATE DATABASE " . $conn["dbname"];
131                 if (!mysql_query($sql)) {
132                         display_error('Cannot create database. Check your permissions to database creation or selct already created database.');
133                         return false;
134                 }
135                 return mysql_select_db($conn["dbname"], $db);
136         }
137         return true;
138 }
139
140 function do_install() {
141
142         global $path_to_root, $db_connections, $def_coy, $installed_extensions, 
143                 $dflt_lang, $installed_languages;
144
145         $coa = $_SESSION['inst_set']['coa'];
146         if (install_connect_db() && db_import($path_to_root.'/sql/'.$coa, $_SESSION['inst_set'])) {
147                 $con = $_SESSION['inst_set'];
148                 $table_prefix = $con['tbpref'];
149                 update_company_prefs(array('coy_name'=>$con['name']));
150                 $admin = get_user_by_login('admin');
151 //              update_admin_password($con, md5($con['pass']));
152                 update_user_prefs($admin['id'], array('language' => $_POST['lang'], 
153                         'password' => md5($con['pass'])));
154
155                 if (!copy($path_to_root. "/config.default.php", $path_to_root. "/config.php")) {
156                         display_error(_("Cannot save system configuration file 'config.php'."));
157                         return false;
158                 }
159
160                 $def_coy = 0;
161                 $tb_pref_counter = 0;
162                 $db_connections = array (0=> array (
163                  'name' => $con['name'],
164                  'host' => $con['host'],
165                  'dbuser' => $con['dbuser'],
166                  'dbpassword' => $con['dbpassword'],
167                  'dbname' => $con['dbname'],
168                  'tbpref' => $table_prefix
169                 ));
170                 $err = write_config_db($table_prefix != "");
171
172                 if ($err == -1) {
173                         display_error(_("Cannot open 'config_db.php' configuration file."));
174                         return false;
175                 } else if ($err == -2) {
176                         display_error(_("Cannot write to the 'config_db.php' configuration file."));
177                         return false;
178                 } else if ($err == -3) {
179                         display_error(_("Configuration file 'config_db.php' is not writable. Change its permissions so it is, then re-run installation step."));
180                         return false;
181                 }
182                 // update default language
183                 include_once($path_to_root . "/lang/installed_languages.inc");
184                 $dflt_lang = $_POST['lang'];
185                 write_lang();
186
187                 return true;
188         }
189         return false;
190 }
191
192 if (!isset($_SESSION['inst_set']))  // default settings
193         $_SESSION['inst_set'] = array(
194                 'host'=>'localhost', 
195                 'dbuser' => 'root',
196                 'dbpassword' => '',
197                 'username' => 'admin',
198                 'tbpref' => '0_',
199                 'admin' => 'admin',
200                 'inst_lang' => 'C'
201         );
202
203 if (!@$_POST['Tests'])
204         $_POST['Page'] = 1; // set to start page
205
206 if (isset($_POST['back']) && (@$_POST['Page']>1)) {
207         if ($_POST['Page'] == 5)
208                 $_POST['Page'] = 2;
209         else
210                 $_POST['Page']--;
211 }
212 elseif (isset($_POST['continue'])) {
213         $_POST['Page'] = 2;
214 }
215 elseif (isset($_POST['db_test'])) {
216         if (get_post('host')=='') {
217                 display_error(_('Host name cannot be empty.'));
218                 set_focus('host');
219         }
220         elseif ($_POST['dbuser']=='') {
221                 display_error(_('Database user name cannot be empty.'));
222                 set_focus('dbuser');
223         }
224         elseif ($_POST['dbname']=='') {
225                 display_error(_('Database name cannot be empty.'));
226                 set_focus('dbname');
227         }
228         else {
229                 $_SESSION['inst_set'] = array_merge($_SESSION['inst_set'], array(
230                         'host' => $_POST['host'],
231                         'dbuser' => $_POST['dbuser'],
232                         'dbpassword' => $_POST['dbpassword'],
233                         'dbname' => $_POST['dbname'],
234                         'tbpref' => $_POST['tbpref'] ? '0_' : '',
235                         'sel_langs' => check_value('sel_langs'),
236                         'sel_coas' => check_value('sel_coas'),
237                 ));
238                 if (install_connect_db()) {
239                         $_POST['Page'] = check_value('sel_langs') ? 3 :
240                                 (check_value('sel_coas') ? 4 : 5);
241                 }
242         }
243         if (!file_exists($path_to_root . "/lang/installed_languages.inc")) {
244                 $installed_languages = array (
245                         0 => array ('code' => 'C', 'name' => 'English', 'encoding' => 'iso-8859-1'));
246                         $dflt_lang = 'C';
247                         write_lang();
248         }
249 }
250 elseif(get_post('install_langs')) 
251 {
252         $ret = true;
253         if (isset($_POST['langs']))
254                 foreach($_POST['langs'] as $package => $ok) {
255                         $ret &= install_language($package);
256                 }
257         if ($ret) {
258                 $_POST['Page'] = $_SESSION['inst_set']['sel_coas'] ? 4 : 5;
259         }
260 }
261 elseif(get_post('install_coas')) 
262 {
263         $ret = true;
264
265         if (isset($_POST['coas']))
266                 foreach($_POST['coas'] as $package => $ok) {
267                         $ret &= install_extension($package);
268                 }
269         if ($ret) {
270                 include($path_to_root.'/installed_extensions.php');
271                 $_POST['Page'] = 5;
272         }
273 }
274 elseif (isset($_POST['set_admin'])) {
275         // check company settings
276         if (get_post('name')=='') {
277                 display_error(_('Company name cannot be empty.'));
278                 set_focus('name');
279         }
280         elseif (get_post('admin')=='') {
281                 display_error(_('Company admin name cannot be empty.'));
282                 set_focus('admin');
283         }
284         elseif (get_post('pass')=='') {
285                 display_error(_('Company admin password cannot be empty.'));
286                 set_focus('pass');
287         }
288         elseif (get_post('pass')!=get_post('repass')) {
289                 display_error(_('Company admin passwords differ.'));
290                 unset($_POST['pass'],$_POST['repass']);
291                 set_focus('pass');
292         }
293         else {
294
295                 $_SESSION['inst_set'] = array_merge($_SESSION['inst_set'], array(
296                         'coa' => $_POST['coa'],
297                         'pass' => $_POST['pass'],
298                         'name' => $_POST['name'],
299                         'admin' => $_POST['admin'],
300                 ));
301                 if (do_install()) {
302                         $_POST['Page'] = 6;
303                 }
304         }
305 }
306
307 if (list_updated('inst_lang')) {
308         $_SESSION['inst_set']['inst_lang'] = get_post('inst_lang');
309         $Ajax->setEncoding($inst_langs[get_post('inst_lang')]['encoding']);
310         $Ajax->activate('welcome');
311 }
312
313 start_form();
314         switch(@$_POST['Page']) {
315                 default:
316 //                      include ('../install.html');
317 //                      submit_center('continue', _('Continue >>'));
318 //                      break;
319                 case '1':
320                         div_start('welcome');
321                         subpage_title(_('System Diagnostics'));
322                         start_table();
323                         instlang_list_row(_("Select install wizard language:"), 'inst_lang',
324                                 $_SESSION['inst_set']['inst_lang']);
325                         end_table(1);
326                         $_POST['Tests'] = display_system_tests(true);
327                         br();
328                         if (@$_POST['Tests']) {
329                                 display_notification(_('All application preliminary requirements seems to be correct. Please press Continue button below.'));
330                                 submit_center('continue', _('Continue >>'));
331                         } else {
332                                 display_error(_('Application cannot be installed. Please fix problems listed below in red, and press Refresh button.'));
333                                 submit_center('refresh', _('Refresh'));
334                         }
335                         div_end();
336                         break;
337
338                 case '2':
339                         if (!isset($_POST['host'])) {
340                                 foreach($_SESSION['inst_set'] as $name => $val)
341                                         $_POST[$name] = $val;
342                         }
343                         subpage_title(_('Database Server Settings'));
344                         start_table(TABLESTYLE);
345                         text_row_ex(_("Server Host:"), 'host', 30);
346                         text_row_ex(_("Database User:"), 'dbuser', 30);
347                         text_row_ex(_("Database Password:"), 'dbpassword', 30);
348                         text_row_ex(_("Database Name:"), 'dbname', 30);
349                         yesno_list_row(_("Use '0_' Table Prefix:"), 'tbpref', 1, _('Yes'), _('No'), false);
350                         check_row(_("Install Additional Language Packs from FA Repository:"), 'sel_langs');
351                         check_row(_("Install Additional COAs from FA Repository:"), 'sel_coas');
352                         end_table(1);
353                         display_note(_('Use table prefix if you share selected database for more than one FA company.'));
354                         display_note(_("Do not select additional langs nor COAs if you have no working internet connection right now. You can install them later."));
355                         submit_center_first('back', _('<< Back'));
356                         submit_center_last('db_test', _('Continue >>'));
357                         break;
358
359                 case '3': // select langauges
360                         subpage_title(_('User Interface Languages Selection'));
361                         display_langs();
362                         submit_center_first('back', _('<< Back'));
363                         submit_center_last('install_langs', _('Continue >>'));
364                         break;
365
366                 case '4': // select COA
367                         subpage_title(_('Charts of Accounts Selection'));
368                         display_coas();
369                         submit_center_first('back', _('<< Back'));
370                         submit_center_last('install_coas', _('Continue >>'));
371                         break;
372
373                 case '5':
374                         if (!isset($_POST['name'])) {
375                                 foreach($_SESSION['inst_set'] as $name => $val)
376                                         $_POST[$name] = $val;
377                                 set_focus('name');
378                         }
379                         subpage_title(_('Company Settings'));
380                         start_table(TABLESTYLE);
381                         text_row_ex(_("Company Name:"), 'name', 30);
382                         text_row_ex(_("Admin Login:"), 'admin', 30);
383                         password_row(_("Admin Password:"), 'pass', @$_POST['pass']);
384                         password_row(_("Reenter Password:"), 'repass', @$_POST['repass']);
385                         coa_list_row(_("Select Chart of Accounts:"), 'coa');
386                         languages_list_row(_("Select Default Language:"), 'lang');
387                         end_table(1);
388                         submit_center_first('back', _('<< Back'));
389                         submit_center_last('set_admin', _('Continue >>'));
390                         break;
391
392                 case '6': // final screen
393                         subpage_title(_('FrontAccounting ERP has been installed successsfully.'));
394                         display_note(_('Please do not forget to remove install wizard folder.'));
395                         $install_done = true;
396                         hyperlink_no_params($path_to_root.'/index.php', _('Click here to start.'));
397                         break;
398
399         }
400
401         hidden('Tests');
402         hidden('Page');
403 end_form(1);
404
405 end_page(false, false, true);
406
407 ?>