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