Install wizard rewritten, added uploading COAs during install.
[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.php'))
16 //      header("Location: $path_to_root/index.php");
17
18 include($path_to_root . "/install/isession.inc");
19
20 page(_($help_context = "FrontAccouting ERP Installation Wizard"), true, false, "", '', false,
21         'stylesheet.css');
22
23 include($path_to_root . "/includes/ui.inc");
24 include($path_to_root . "/includes/system_tests.inc");
25 include($path_to_root . "/admin/db/maintenance_db.inc");
26 include($path_to_root . "/includes/packages.inc");
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         $page = @$_POST['Page'] ? $_POST['Page'] : 1;
37
38         display_heading(
39                 $page==5 ? $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"), _("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(is_array($coa['Descr']) ? implode('<br>', $coa['Descr']) :  $coa['Descr']);
63                 if ($installed)
64                         label_cell(_("Installed"));
65                 else
66                         check_cells(null, 'coas['.$coa['package'].']');
67
68                 end_row();
69         }
70         end_table(1);
71 }
72
73 function install_connect_db() {
74         global $db;
75
76         $conn = $_SESSION['inst_set'];
77         
78         $db = mysql_connect($conn["host"] , $conn["dbuser"], $conn["dbpassword"]);
79         if(!$db) {
80                 display_error('Cannot connect to database server. Host name, username and/or password incorrect.');
81                 return false;
82         }
83         if (!defined('TB_PREF'))
84                 define('TB_PREF', $conn["tbpref"]);
85
86         if (!mysql_select_db($conn["dbname"], $db)) {
87                 $sql = "CREATE DATABASE " . $conn["dbname"];
88                 if (!mysql_query($sql)) {
89                         display_error('Cannot create database. Check your permissions to database creation or selct already created database.');
90                         return false;
91                 }
92                 return mysql_select_db($conn["dbname"], $db);
93         }
94         return true;
95 }
96
97 function coa_type_list_row($label, $name, $selected=null)
98 {
99         
100         $coa_types = array(
101                 0 => _('New company with Standard American COA (4digit)'),
102                 _('Demo based on Standard American COA (4digit)'),
103                 _('Any COA selected from FA repository (inet connection required)')
104         );
105         echo "<tr><td>$label</td><td>\n";
106         echo array_selector($name, $selected, $coa_types);
107         echo "</td></tr>";
108 }
109
110 function do_install() {
111         global $path_to_root, $db_connections, $def_coy, $installed_extensions;
112
113         $coa = $_SESSION['inst_set']['coa'];
114         if (install_connect_db() && db_import($path_to_root.'/sql/'.$coa, $_SESSION['inst_set'])) {
115                 $con = $_SESSION['inst_set'];
116                 $table_prefix = $con['tbpref'];
117                 update_admin_password($con, md5($con['pass']));
118                 update_company_prefs(array('coy_name'=>$con['name']));
119
120                 $def_coy = 0;
121                 $tb_pref_counter = 0;
122                 $db_connections = array (0=> array (
123                  'name' => $con['name'],
124                  'host' => $con['host'],
125                  'dbuser' => $con['dbuser'],
126                  'dbpassword' => $con['dbpassword'],
127                  'dbname' => $con['dbname'],
128                  'tbpref' => $table_prefix
129                 ));
130                 $err = write_config_db($table_prefix != "");
131
132                 if ($err == -1) {
133                         display_error(_("Cannot open the config_db.php configuration file:"));
134                         return false;
135                 } else if ($err == -2) {
136                         display_error(_("Cannot write to the config_db.php configuration file"));
137                         return false;
138                 } else if ($err == -3) {
139                         display_error(_("The configuration file config_db.php is not writable. Change its permissions so it is, then re-run step 5."));
140                         return false;
141                 }
142                 if (!copy($path_to_root. "/config.default.php", $path_to_root. "/config.php")) {
143                         display_error(_("Cannot save system configuration file config.php"));
144                         return false;
145                 }
146                 if (count($installed_extensions))
147                  if (!update_extensions($installed_extensions)) { // update company 0 extensions (charts)
148                         display_error(_("Can't update extensions configuration."));
149                         return false;
150                  }
151                 return true;
152         }
153         return false;
154 }
155
156 if (!isset($_SESSION['inst_set']))  // default settings
157         $_SESSION['inst_set'] = array(
158                 'host'=>'localhost', 
159                 'dbuser' => 'root',
160                 'dbpassword' => '',
161                 'username' => 'admin',
162                 'tbpref' => '0_',
163                 'admin' => 'admin',
164                 'coa_type' => 0
165         );
166
167 if (!@$_POST['Tests'])
168         $_POST['Page'] = 1;
169
170 if (isset($_POST['back']) && (@$_POST['Page']>1)) {
171         $_POST['Page']--;
172         if ($_POST['Page'] == 3)
173                 $_POST['Page'] = 2;
174 }
175 elseif (isset($_POST['continue'])) {
176         $_POST['Page'] = 2;
177 }
178 elseif (isset($_POST['db_test'])) {
179         if (get_post('host')=='') {
180                 display_error(_('Host name cannot be empty'));
181                 set_focus('host');
182         }
183         elseif ($_POST['dbuser']=='') {
184                 display_error(_('Database user name cannot be empty'));
185                 set_focus('dbuser');
186         }
187         elseif ($_POST['dbname']=='') {
188                 display_error(_('Database name cannot be empty'));
189                 set_focus('dbname');
190         }
191         else {
192                 $_SESSION['inst_set'] = array_merge($_SESSION['inst_set'], array(
193                         'host' => $_POST['host'],
194                         'dbuser' => $_POST['dbuser'],
195                         'dbpassword' => $_POST['dbpassword'],
196                         'dbname' => $_POST['dbname'],
197                         'tbpref' => $_POST['tbpref'] ? '0_' : '',
198                 ));
199                 if (install_connect_db()) {
200                         $_POST['Page'] = check_value('sel_coas') ? 3 : 4;
201                 }
202         }
203 }
204 elseif(get_post('install_coas')) 
205 {
206         $ret = true;
207         if (isset($_POST['coas']))
208                 foreach($_POST['coas'] as $package => $ok) {
209                         $ret &= install_extension($package);
210                 }
211         if ($ret) {
212                 $_POST['Page'] = 4;
213         }
214 }
215 elseif (isset($_POST['set_admin'])) {
216         // check company settings
217         if (get_post('name')=='') {
218                 display_error(_('Company name cannot be empty.'));
219                 set_focus('name');
220         }
221         elseif (get_post('admin')=='') {
222                 display_error(_('Company admin name cannot be empty.'));
223                 set_focus('admin');
224         }
225         elseif (get_post('pass')=='') {
226                 display_error(_('Company admin password cannot be empty.'));
227                 set_focus('pass');
228         }
229         elseif (get_post('pass')!=get_post('repass')) {
230                 display_error(_('Company admin passwords differ.'));
231                 unset($_POST['pass'],$_POST['repass']);
232                 set_focus('pass');
233         }
234         else {
235
236                 $_SESSION['inst_set'] = array_merge($_SESSION['inst_set'], array(
237                         'coa' => $_POST['coa'],
238                         'pass' => $_POST['pass'],
239                         'name' => $_POST['name'],
240                         'admin' => $_POST['admin'],
241                 ));
242                 if (do_install()) {
243                         $_POST['Page'] = 5;
244                 }
245         }
246 }
247
248 start_form();
249         switch(@$_POST['Page']) {
250                 default:
251                         include ('../install.html');
252                         submit_center('continue', _('Continue >>'));
253                         break;
254                 case '1':
255                         subpage_title(_('System Diagnostics'));
256                         $_POST['Tests'] = display_system_tests(true);
257                         br();
258                         if (@$_POST['Tests']) {
259                                 display_notification(_('All application preliminary requirements seems to be correct. Please press Continue button below.'));
260                                 submit_center('continue', _('Continue >>'));
261                         } else {
262                                 display_error(_('Application cannot be installed. Please fix problems listed below in red, and press Refresh button.'));
263                                 submit_center('refresh', _('Refresh'));
264                         }
265                         break;
266
267                 case '2':
268                         if (!isset($_POST['host'])) {
269                                 foreach($_SESSION['inst_set'] as $name => $val)
270                                         $_POST[$name] = $val;
271                         }
272                         subpage_title(_('Database Server Settings'));
273                         start_table(TABLESTYLE);
274                         text_row_ex(_("Server Host"), 'host', 30);
275                         text_row_ex(_("Database User"), 'dbuser', 30);
276                         text_row_ex(_("Database Password"), 'dbpassword', 30);
277                         text_row_ex(_("Database Name"), 'dbname', 30);
278                         yesno_list_row(_("Use '0_' Table Prefix"), 'tbpref', 1, _('Yes'), _('No'), false);
279                         check_row(_("Install additional COAs form FA repository"), 'sel_coas');
280                         end_table(1);
281                         display_note(_('Use table prefix if you share selected database with another application, or you want to use it for more than one FA company.'));
282                         submit_center_first('back', _('<< Back'));
283                         submit_center_last('db_test', _('Continue >>'));
284                         break;
285
286                 case '3': // select COA
287                         subpage_title(_('Charts of accounts selection'));
288                         display_coas();
289                         submit_center_first('back', _('<< Back'));
290                         submit_center_last('install_coas', _('Continue >>'));
291                         break;
292
293                 case '4':
294                         if (!isset($_POST['name'])) {
295                                 foreach($_SESSION['inst_set'] as $name => $val)
296                                         $_POST[$name] = $val;
297                                 set_focus('name');
298                         }
299                         subpage_title(_('Company Settings'));
300                         start_table(TABLESTYLE);
301                         text_row_ex(_("Company Name"), 'name', 30);
302                         text_row_ex(_("Admin Login"), 'admin', 30);
303                         password_row(_("Admin Password"), 'pass', @$_POST['pass']);
304                         password_row(_("Reenter Password"), 'repass', @$_POST['repass']);
305                         coa_list_row(_("Select Chart of Accounts"), 'coa');
306                         end_table(1);
307                         submit_center_first('back', _('<< Back'));
308                         submit_center_last('set_admin', _('Continue >>'));
309                         break;
310
311                 case '5': // final screen
312                         subpage_title(_('FrontAccounting ERP has been installed successsfully.'));
313                         display_note(_('Please remove install wizard folder.'));
314                         $install_done = true;
315                         hyperlink_no_params($path_to_root.'/index.php', _('Click here to start.'));
316                         break;
317
318         }
319
320         hidden('Tests');
321         hidden('Page');
322 end_form(1);
323
324 end_page(false, false, true);
325
326 ?>