Fixed installer (sys_prefs update error).
[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', '&TB_PREF&');
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
150                 $def_coy = 0;
151                 $tb_pref_counter = 0;
152                 $db_connections = array (0=> array (
153                  'name' => $con['name'],
154                  'host' => $con['host'],
155                  'dbuser' => $con['dbuser'],
156                  'dbpassword' => $con['dbpassword'],
157                  'dbname' => $con['dbname'],
158                  'tbpref' => $table_prefix
159                 ));
160
161                 $_SESSION['wa_current_user']->cur_con = 0;
162                 
163                 update_company_prefs(array('coy_name'=>$con['name']));
164                 $admin = get_user_by_login('admin');
165 //              update_admin_password($con, md5($con['pass']));
166                 update_user_prefs($admin['id'], array('language' => $_POST['lang'], 
167                         'password' => md5($con['pass'])));
168
169                 if (!copy($path_to_root. "/config.default.php", $path_to_root. "/config.php")) {
170                         display_error(_("Cannot save system configuration file 'config.php'."));
171                         return false;
172                 }
173
174                 $err = write_config_db($table_prefix != "");
175
176                 if ($err == -1) {
177                         display_error(_("Cannot open 'config_db.php' configuration file."));
178                         return false;
179                 } else if ($err == -2) {
180                         display_error(_("Cannot write to the 'config_db.php' configuration file."));
181                         return false;
182                 } else if ($err == -3) {
183                         display_error(_("Configuration file 'config_db.php' is not writable. Change its permissions so it is, then re-run installation step."));
184                         return false;
185                 }
186                 // update default language
187                 include_once($path_to_root . "/lang/installed_languages.inc");
188                 $dflt_lang = $_POST['lang'];
189                 write_lang();
190                 if (!isset($installed_extensions))
191                         write_extensions(array());
192                 return true;
193         }
194         return false;
195 }
196
197 if (!isset($_SESSION['inst_set']))  // default settings
198         $_SESSION['inst_set'] = array(
199                 'host'=>'localhost', 
200                 'dbuser' => 'root',
201                 'dbpassword' => '',
202                 'username' => 'admin',
203                 'tbpref' => '0_',
204                 'admin' => 'admin',
205                 'inst_lang' => 'C'
206         );
207
208 if (!@$_POST['Tests'])
209         $_POST['Page'] = 1; // set to start page
210
211 if (isset($_POST['back']) && (@$_POST['Page']>1)) {
212         if ($_POST['Page'] == 5)
213                 $_POST['Page'] = 2;
214         else
215                 $_POST['Page']--;
216 }
217 elseif (isset($_POST['continue'])) {
218         $_POST['Page'] = 2;
219 }
220 elseif (isset($_POST['db_test'])) {
221         if (get_post('host')=='') {
222                 display_error(_('Host name cannot be empty.'));
223                 set_focus('host');
224         }
225         elseif ($_POST['dbuser']=='') {
226                 display_error(_('Database user name cannot be empty.'));
227                 set_focus('dbuser');
228         }
229         elseif ($_POST['dbname']=='') {
230                 display_error(_('Database name cannot be empty.'));
231                 set_focus('dbname');
232         }
233         else {
234                 $_SESSION['inst_set'] = array_merge($_SESSION['inst_set'], array(
235                         'host' => $_POST['host'],
236                         'dbuser' => $_POST['dbuser'],
237                         'dbpassword' => $_POST['dbpassword'],
238                         'dbname' => $_POST['dbname'],
239                         'tbpref' => $_POST['tbpref'] ? '0_' : '',
240                         'sel_langs' => check_value('sel_langs'),
241                         'sel_coas' => check_value('sel_coas'),
242                 ));
243                 if (install_connect_db()) {
244                         $_POST['Page'] = check_value('sel_langs') ? 3 :
245                                 (check_value('sel_coas') ? 4 : 5);
246                 }
247         }
248         if (!file_exists($path_to_root . "/lang/installed_languages.inc")) {
249                 $installed_languages = array (
250                         0 => array ('code' => 'C', 'name' => 'English', 'encoding' => 'iso-8859-1'));
251                         $dflt_lang = 'C';
252                         write_lang();
253         }
254 }
255 elseif(get_post('install_langs')) 
256 {
257         $ret = true;
258         if (isset($_POST['langs']))
259                 foreach($_POST['langs'] as $package => $ok) {
260                         $ret &= install_language($package);
261                 }
262         if ($ret) {
263                 $_POST['Page'] = $_SESSION['inst_set']['sel_coas'] ? 4 : 5;
264         }
265 }
266 elseif(get_post('install_coas')) 
267 {
268         $ret = true;
269         $next_extension_id = 0;
270         
271         if (isset($_POST['coas']))
272                 foreach($_POST['coas'] as $package => $ok) {
273                         $ret &= install_extension($package);
274                 }
275         if ($ret) {
276                 @include($path_to_root.'/installed_extensions.php');
277                 $_POST['Page'] = 5;
278         }
279 }
280 elseif (isset($_POST['set_admin'])) {
281         // check company settings
282         if (get_post('name')=='') {
283                 display_error(_('Company name cannot be empty.'));
284                 set_focus('name');
285         }
286         elseif (get_post('admin')=='') {
287                 display_error(_('Company admin name cannot be empty.'));
288                 set_focus('admin');
289         }
290         elseif (get_post('pass')=='') {
291                 display_error(_('Company admin password cannot be empty.'));
292                 set_focus('pass');
293         }
294         elseif (get_post('pass')!=get_post('repass')) {
295                 display_error(_('Company admin passwords differ.'));
296                 unset($_POST['pass'],$_POST['repass']);
297                 set_focus('pass');
298         }
299         else {
300
301                 $_SESSION['inst_set'] = array_merge($_SESSION['inst_set'], array(
302                         'coa' => $_POST['coa'],
303                         'pass' => $_POST['pass'],
304                         'name' => $_POST['name'],
305                         'admin' => $_POST['admin'],
306                 ));
307                 if (do_install()) {
308                         $_POST['Page'] = 6;
309                 }
310         }
311 }
312
313 if (list_updated('inst_lang')) {
314         $_SESSION['inst_set']['inst_lang'] = get_post('inst_lang');
315         $Ajax->setEncoding($inst_langs[get_post('inst_lang')]['encoding']);
316         $Ajax->activate('welcome');
317 }
318
319 start_form();
320         switch(@$_POST['Page']) {
321                 default:
322 //                      include ('../install.html');
323 //                      submit_center('continue', _('Continue >>'));
324 //                      break;
325                 case '1':
326                         div_start('welcome');
327                         subpage_title(_('System Diagnostics'));
328                         start_table();
329                         instlang_list_row(_("Select install wizard language:"), 'inst_lang',
330                                 $_SESSION['inst_set']['inst_lang']);
331                         end_table(1);
332                         $_POST['Tests'] = display_system_tests(true);
333                         br();
334                         if (@$_POST['Tests']) {
335                                 display_notification(_('All application preliminary requirements seems to be correct. Please press Continue button below.'));
336                                 submit_center('continue', _('Continue >>'));
337                         } else {
338                                 display_error(_('Application cannot be installed. Please fix problems listed below in red, and press Refresh button.'));
339                                 submit_center('refresh', _('Refresh'));
340                         }
341                         div_end();
342                         break;
343
344                 case '2':
345                         if (!isset($_POST['host'])) {
346                                 foreach($_SESSION['inst_set'] as $name => $val)
347                                         $_POST[$name] = $val;
348                         }
349                         subpage_title(_('Database Server Settings'));
350                         start_table(TABLESTYLE);
351                         text_row_ex(_("Server Host:"), 'host', 30);
352                         text_row_ex(_("Database User:"), 'dbuser', 30);
353                         text_row_ex(_("Database Password:"), 'dbpassword', 30);
354                         text_row_ex(_("Database Name:"), 'dbname', 30);
355                         yesno_list_row(_("Use '0_' Table Prefix:"), 'tbpref', 1, _('Yes'), _('No'), false);
356                         check_row(_("Install Additional Language Packs from FA Repository:"), 'sel_langs');
357                         check_row(_("Install Additional COAs from FA Repository:"), 'sel_coas');
358                         end_table(1);
359                         display_note(_('Use table prefix if you share selected database for more than one FA company.'));
360                         display_note(_("Do not select additional langs nor COAs if you have no working internet connection right now. You can install them later."));
361                         submit_center_first('back', _('<< Back'));
362                         submit_center_last('db_test', _('Continue >>'));
363                         break;
364
365                 case '3': // select langauges
366                         subpage_title(_('User Interface Languages Selection'));
367                         display_langs();
368                         submit_center_first('back', _('<< Back'));
369                         submit_center_last('install_langs', _('Continue >>'));
370                         break;
371
372                 case '4': // select COA
373                         subpage_title(_('Charts of Accounts Selection'));
374                         display_coas();
375                         submit_center_first('back', _('<< Back'));
376                         submit_center_last('install_coas', _('Continue >>'));
377                         break;
378
379                 case '5':
380                         if (!isset($_POST['name'])) {
381                                 foreach($_SESSION['inst_set'] as $name => $val)
382                                         $_POST[$name] = $val;
383                                 set_focus('name');
384                         }
385                         subpage_title(_('Company Settings'));
386                         start_table(TABLESTYLE);
387                         text_row_ex(_("Company Name:"), 'name', 30);
388                         text_row_ex(_("Admin Login:"), 'admin', 30);
389                         password_row(_("Admin Password:"), 'pass', @$_POST['pass']);
390                         password_row(_("Reenter Password:"), 'repass', @$_POST['repass']);
391                         coa_list_row(_("Select Chart of Accounts:"), 'coa');
392                         languages_list_row(_("Select Default Language:"), 'lang');
393                         end_table(1);
394                         submit_center_first('back', _('<< Back'));
395                         submit_center_last('set_admin', _('Continue >>'));
396                         break;
397
398                 case '6': // final screen
399                         subpage_title(_('FrontAccounting ERP has been installed successsfully.'));
400                         display_note(_('Please do not forget to remove install wizard folder.'));
401                         $install_done = true;
402                         hyperlink_no_params($path_to_root.'/index.php', _('Click here to start.'));
403                         break;
404
405         }
406
407         hidden('Tests');
408         hidden('Page');
409 end_form(1);
410
411 end_page(false, false, true);
412
413 ?>