7e164e81bf3a5f9f27cfdb5578e70e8986fcf24a
[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 if (file_exists($path_to_root . "/installed_extensions.php"))
27         include($path_to_root . "/installed_extensions.php");
28 //-------------------------------------------------------------------------------------------------
29
30 function subpage_title($txt) 
31 {
32         global $path_to_root;
33         
34         echo '<center><img src="'.$path_to_root.'/themes/default/images/logo_frontaccounting.png" width="250" height="50" alt="Logo" >
35                 </center>';
36
37         $page = @$_POST['Page'] ? $_POST['Page'] : 1;
38
39         display_heading(
40                 $page == 6 ? $txt :
41                         _("FrontAccouting ERP Installation Wizard").'<br>'
42                         . sprintf(_('Step %d: %s'),  $page , $txt));
43         br();
44 }
45
46 function display_coas()
47 {
48         start_table(TABLESTYLE);
49         $th = array(_("Chart of accounts"), _("Encoding"), _("Description"), _("Install"));
50         table_header($th);
51
52         $k = 0;
53         $charts = get_charts_list();
54
55         foreach($charts as $pkg_name => $coa)
56         {
57                 $available = @$coa['available'];
58                 $installed = @$coa['version'];
59                 $id = @$coa['local_id'];
60
61                 alt_table_row_color($k);
62                 label_cell($coa['name']);
63                 label_cell($coa['encoding']);
64                 label_cell(is_array($coa['Descr']) ? implode('<br>', $coa['Descr']) :  $coa['Descr']);
65                 label_cell($installed ?
66                         _("Installed") : checkbox(null, 'coas['.$coa['package'].']'), "align='center'");
67
68                 end_row();
69         }
70         end_table(1);
71 }
72
73 function display_langs()
74 {
75         start_table(TABLESTYLE);
76         $th = array(_("Language"), _("Encoding"), _("Description"), _("Install"));
77         table_header($th);
78
79         $k = 0;
80         $langs = get_languages_list();
81
82         foreach($langs as $pkg_name => $lang)
83         {
84                 $available = @$lang['available'];
85                 $installed = @$lang['version'];
86                 $id = @$lang['local_id'];
87                 if (!$available) continue;
88
89                 alt_table_row_color($k);
90                 label_cell($lang['name']);
91                 label_cell($lang['encoding']);
92                 label_cell(is_array($lang['Descr']) ? implode('<br>', $lang['Descr']) :  $lang['Descr']);
93                 label_cell($installed ?
94                         _("Installed") : checkbox(null, 'langs['.$lang['package'].']'), "align='center'");
95                 end_row();
96         }
97         end_table(1);
98 }
99
100 function instlang_list_row($label, $name, $value=null) {
101
102         global $inst_langs;
103
104         $langs = array();
105         foreach ($inst_langs as $n => $lang)
106                         $langs[$n] = $lang['name'];
107
108         echo "<td>$label</td>\n" . "<td>\n" 
109                 .array_selector($name, $value, $langs, 
110                         array(
111                                 'select_submit' => true,
112                                 'async' => true
113                         )) . "</td>\n";
114 }
115
116 function install_connect_db() {
117
118         global $db;
119
120         $conn = $_SESSION['inst_set'];
121
122         $db = db_create_db($conn);
123         if (!$db) {
124                 display_error(_("Cannot connect to database. User or password is invalid or you have no permittions to create database."));
125         } else {
126                 if (strncmp(db_get_version(), "5.6", 3) >= 0) 
127                         db_query("SET sql_mode = ''");
128         }
129         return $db;
130 }
131
132 function do_install() {
133
134         global $path_to_root, $db_connections, $def_coy, $installed_extensions, $tb_pref_counter,
135                 $dflt_lang, $installed_languages;
136
137         $coa = $_SESSION['inst_set']['coa'];
138         if (install_connect_db() && db_import($path_to_root.'/sql/'.$coa, $_SESSION['inst_set'])) {
139                 $con = $_SESSION['inst_set'];
140                 $table_prefix = $con['tbpref'];
141
142                 $def_coy = 0;
143                 $tb_pref_counter = 0;
144                 $db_connections = array (0=> array (
145                  'name' => $con['name'],
146                  'host' => $con['host'],
147                  'port' => $con['port'],
148                  'dbname' => $con['dbname'],
149                  'collation' => $con['collation'],
150                  'tbpref' => $table_prefix,
151                  'dbuser' => $con['dbuser'],
152                  'dbpassword' => $con['dbpassword'],
153                 ));
154
155                 $_SESSION['wa_current_user']->cur_con = 0;
156                 
157                 update_company_prefs(array('coy_name'=>$con['name']));
158                 $admin = get_user_by_login('admin');
159                 update_user_prefs($admin['id'], array(
160                         'language' => $con['lang'], 
161                         'password' => md5($con['pass']),
162                         'user_id' => $con['admin']));
163
164                 if (!copy($path_to_root. "/config.default.php", $path_to_root. "/config.php")) {
165                         display_error(_("Cannot save system configuration file 'config.php'."));
166                         return false;
167                 }
168
169                 $err = write_config_db($table_prefix != "");
170
171                 if ($err == -1) {
172                         display_error(_("Cannot open 'config_db.php' configuration file."));
173                         return false;
174                 } else if ($err == -2) {
175                         display_error(_("Cannot write to the 'config_db.php' configuration file."));
176                         return false;
177                 } else if ($err == -3) {
178                         display_error(_("Configuration file 'config_db.php' is not writable. Change its permissions so it is, then re-run installation step."));
179                         return false;
180                 }
181                 // update default language
182                 if (file_exists($path_to_root . "/lang/installed_languages.inc"))
183                         include_once($path_to_root . "/lang/installed_languages.inc");
184                 $dflt_lang = $_POST['lang'];
185                 write_lang();
186                 return true;
187         }
188         return false;
189 }
190
191 if (!isset($_SESSION['inst_set']))  // default settings
192         $_SESSION['inst_set'] = array(
193                 'host'=>'localhost', 
194                 'port' => '', // 3306
195                 'dbuser' => 'root',
196                 'dbpassword' => '',
197                 'username' => 'admin',
198                 'tbpref' => '0_',
199                 'admin' => 'admin',
200                 'inst_lang' => 'C',
201                 'collation' => 'xx',
202         );
203
204 if (!@$_POST['Tests'])
205         $_POST['Page'] = 1; // set to start page
206
207 if (isset($_POST['back']) && (@$_POST['Page']>1)) {
208         if ($_POST['Page'] == 5)
209                 $_POST['Page'] = 2;
210         else
211                 $_POST['Page']--;
212 }
213 elseif (isset($_POST['continue'])) {
214         $_POST['Page'] = 2;
215 }
216 elseif (isset($_POST['db_test'])) {
217         if (get_post('host')=='') {
218                 display_error(_('Host name cannot be empty.'));
219                 set_focus('host');
220         }
221         elseif ($_POST['port'] != '' && !is_numeric($_POST['port'])) {
222                 display_error(_('Database port have to be numeric or empty.'));
223                 set_focus('port');
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                         'port' => $_POST['port'],
237                         'dbuser' => $_POST['dbuser'],
238                         'dbpassword' => $_POST['dbpassword'],
239                         'dbname' => $_POST['dbname'],
240                         'tbpref' => $_POST['tbpref'] ? '0_' : '',
241                         'sel_langs' => check_value('sel_langs'),
242                         'sel_coas' => check_value('sel_coas'),
243                         'collation' => $_POST['collation'],
244                 ));
245                 if (install_connect_db()) {
246                         $_POST['Page'] = check_value('sel_langs') ? 3 :
247                                 (check_value('sel_coas') ? 4 : 5);
248                 }
249         }
250         if (!file_exists($path_to_root . "/lang/installed_languages.inc")) {
251                 $installed_languages = array (
252                         0 => array ('code' => 'C', 'name' => 'English', 'encoding' => 'iso-8859-1'));
253                         $dflt_lang = 'C';
254                         write_lang();
255         }
256 }
257 elseif(get_post('install_langs')) 
258 {
259         $ret = true;
260         if (isset($_POST['langs']))
261                 foreach($_POST['langs'] as $package => $ok) {
262                         $ret &= install_language($package);
263                 }
264         if ($ret) {
265                 $_POST['Page'] = $_SESSION['inst_set']['sel_coas'] ? 4 : 5;
266         }
267 }
268 elseif(get_post('install_coas')) 
269 {
270         $ret = true;
271         $next_extension_id = 0;
272         
273         if (isset($_POST['coas']))
274                 foreach($_POST['coas'] as $package => $ok) {
275                         $ret &= install_extension($package);
276                 }
277         if ($ret) {
278                 if (file_exists($path_to_root . '/installed_extensions.php'))
279                         include($path_to_root.'/installed_extensions.php');
280                 $_POST['Page'] = 5;
281         }
282 } elseif (isset($_POST['set_admin'])) {
283         // check company settings
284         if (get_post('name')=='') {
285                 display_error(_('Company name cannot be empty.'));
286                 set_focus('name');
287         }
288         elseif (get_post('admin')=='') {
289                 display_error(_('Company admin name cannot be empty.'));
290                 set_focus('admin');
291         }
292         elseif (get_post('pass')=='') {
293                 display_error(_('Company admin password cannot be empty.'));
294                 set_focus('pass');
295         }
296         elseif (get_post('pass')!=get_post('repass')) {
297                 display_error(_('Company admin passwords differ.'));
298                 unset($_POST['pass'],$_POST['repass']);
299                 set_focus('pass');
300         }
301         else {
302
303                 $_SESSION['inst_set'] = array_merge($_SESSION['inst_set'], array(
304                         'coa' => $_POST['coa'],
305                         'pass' => $_POST['pass'],
306                         'name' => $_POST['name'],
307                         'admin' => $_POST['admin'],
308                         'lang' => $_POST['lang']
309                 ));
310                 if (do_install()) {
311                         $_POST['Page'] = 6;
312                 }
313         }
314 }
315
316 if (list_updated('inst_lang')) {
317         $_SESSION['inst_set']['inst_lang'] = get_post('inst_lang');
318         $Ajax->setEncoding($inst_langs[get_post('inst_lang')]['encoding']);
319         $Ajax->activate('welcome');
320 }
321
322 start_form();
323         switch(@$_POST['Page']) {
324                 default:
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, 60);
352                         text_row_ex(_("Server Port:"), 'port', 30, 60);
353                         text_row_ex(_("Database Name:"), 'dbname', 30);
354                         text_row_ex(_("Database User:"), 'dbuser', 30);
355                         password_row(_("Database Password:"), 'dbpassword', '');
356                         collations_list_row(_("Database Collation:"), 'collation');
357                         yesno_list_row(_("Use '0_' Table Prefix:"), 'tbpref', 1, _('Yes'), _('No'), false);
358                         check_row(_("Install Additional Language Packs from FA Repository:"), 'sel_langs');
359                         check_row(_("Install Additional COAs from FA Repository:"), 'sel_coas');
360                         end_table(1);
361                         display_note(_("Select collation you want to use. If you are unsure or you will use various languages, select unicode collation."));
362                         display_note(_("Use table prefix if you share selected database for more than one FA company using the same collation."));
363                         display_note(_("Do not select additional langs nor COAs if you have no working internet connection right now. You can install them later."));
364                         display_note(_("Set Only Port value if you cannot use the default port 3306."));
365                         submit_center_first('back', _('<< Back'));
366                         submit_center_last('db_test', _('Continue >>'));
367                         break;
368
369                 case '3': // select langauges
370                         subpage_title(_('User Interface Languages Selection'));
371                         display_langs();
372                         submit_center_first('back', _('<< Back'));
373                         submit_center_last('install_langs', _('Continue >>'));
374                         break;
375
376                 case '4': // select COA
377                         subpage_title(_('Charts of Accounts Selection'));
378                         display_coas();
379                         submit_center_first('back', _('<< Back'));
380                         submit_center_last('install_coas', _('Continue >>'));
381                         break;
382
383                 case '5':
384                         if (!isset($_POST['name'])) {
385                                 foreach($_SESSION['inst_set'] as $name => $val)
386                                         $_POST[$name] = $val;
387                                 set_focus('name');
388                         }
389                         if (!isset($installed_extensions)) {
390                                 $installed_extensions = array();
391                                 update_extensions($installed_extensions);
392                         }
393
394                         subpage_title(_('Company Settings'));
395                         start_table(TABLESTYLE);
396                         text_row_ex(_("Company Name:"), 'name', 30);
397                         text_row_ex(_("Admin Login:"), 'admin', 30);
398                         password_row(_("Admin Password:"), 'pass', @$_POST['pass']);
399                         password_row(_("Reenter Password:"), 'repass', @$_POST['repass']);
400                         coa_list_row(_("Select Chart of Accounts:"), 'coa');
401                         languages_list_row(_("Select Default Language:"), 'lang');
402                         end_table(1);
403                         submit_center_first('back', _('<< Back'));
404                         submit_center_last('set_admin', _('Install'), _('Start installation process'), 'default nonajax');
405                         break;
406
407                 case '6': // final screen
408                         subpage_title(_('FrontAccounting ERP has been installed successsfully.'));
409                         display_note(_('Please do not forget to remove install wizard folder.'));
410                         session_unset();
411                         session_destroy();
412                         hyperlink_no_params($path_to_root.'/index.php', _('Click here to start.'));
413                         break;
414
415         }
416
417         hidden('Tests');
418         hidden('Page');
419 end_form(1);
420
421 end_page(false, false, true);
422