Feature 5388: Print Invoices (documents) list gets too long. Fixed by default 180...
[fa-stable.git] / admin / create_coy.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_CREATECOMPANY';
13 $path_to_root="..";
14 include_once($path_to_root . "/includes/session.inc");
15
16 include_once($path_to_root . "/includes/date_functions.inc");
17 include_once($path_to_root . "/admin/db/company_db.inc");
18 include_once($path_to_root . "/admin/db/maintenance_db.inc");
19 include_once($path_to_root . "/includes/ui.inc");
20
21 page(_($help_context = "Create/Update Company"));
22
23 $comp_subdirs = array('images', 'pdf_files', 'backup','js_cache', 'reporting', 'attachments');
24
25 simple_page_mode(true);
26 /*
27         FIXME: tb_pref_counter should track prefix per database.
28 */
29 //---------------------------------------------------------------------------------------------
30 function check_data($selected_id)
31 {
32         global $db_connections, $tb_pref_counter;
33
34         if ($selected_id != -1) {
35                 if ($_POST['name'] == "")
36                 {
37                         display_error(_("Database settings are not specified."));
38                         return false;
39                 }
40         } else {
41                 if (!get_post('name') || !get_post('host') || !get_post('dbuser') || !get_post('dbname'))
42                 {
43                         display_error(_("Database settings are not specified."));
44                         return false;
45                 }
46                 if ($_POST['port'] != '' && !is_numeric($_POST['port'])) 
47                 {
48                         display_error(_('Database port has to be numeric or empty.'));
49                         return false;
50                 }
51         
52                 foreach($db_connections as $id=>$con)
53                 {
54                  if($id != $selected_id && $_POST['host'] == $con['host'] 
55                         && $_POST['dbname'] == $con['dbname'])
56                         {
57                                 if ($_POST['tbpref'] == $con['tbpref'])
58                                 {
59                                         display_error(_("This database settings are already used by another company."));
60                                         return false;
61                                 }
62                                 if (($_POST['tbpref'] == 0) ^ ($con['tbpref'] == ''))
63                                 {
64                                         display_error(_("You cannot have table set without prefix together with prefixed sets in the same database."));
65                                         return false;
66                                 }
67                         }
68                 }
69         }
70         return true;
71 }
72
73 //---------------------------------------------------------------------------------------------
74
75 function remove_connection($id) {
76         global $db_connections;
77
78         $err = db_drop_db($db_connections[$id]);
79
80         unset($db_connections[$id]);
81         $conn = array_values($db_connections);
82         $db_connections = $conn;
83     return $err;
84 }
85 //---------------------------------------------------------------------------------------------
86
87 function handle_submit($selected_id)
88 {
89         global $db_connections, $def_coy, $tb_pref_counter, $db,
90             $comp_subdirs, $path_to_root, $Mode;
91
92         $error = false;
93
94         if ($selected_id==-1)
95                 $selected_id = count($db_connections);
96
97         $new = !isset($db_connections[$selected_id]);
98
99         if (check_value('def'))
100                 $def_coy = $selected_id;
101
102         $db_connections[$selected_id]['name'] = $_POST['name'];
103         if ($new) {
104                 $db_connections[$selected_id]['host'] = $_POST['host'];
105                 $db_connections[$selected_id]['port'] = $_POST['port'];
106                 $db_connections[$selected_id]['dbuser'] = $_POST['dbuser'];
107                 $db_connections[$selected_id]['dbpassword'] = html_entity_decode($_POST['dbpassword'], ENT_QUOTES, 
108                         $_SESSION['language']->encoding=='iso-8859-2' ? 'ISO-8859-1' : $_SESSION['language']->encoding);
109                 $db_connections[$selected_id]['dbname'] = $_POST['dbname'];
110                 $db_connections[$selected_id]['collation'] = $_POST['collation'];
111                 if (is_numeric($_POST['tbpref']))
112                 {
113                         $db_connections[$selected_id]['tbpref'] = $_POST['tbpref'] == 1 ?
114                           $tb_pref_counter."_" : '';
115                 }
116                 else if ($_POST['tbpref'] != "")
117                         $db_connections[$selected_id]['tbpref'] = $_POST['tbpref'];
118                 else
119                         $db_connections[$selected_id]['tbpref'] = "";
120
121                 $conn = $db_connections[$selected_id];
122                 if (($db = db_create_db($conn)) === false)
123                 {
124                         display_error(_("Error creating Database: ") . $conn['dbname'] . _(", Please create it manually"));
125                         $error = true;
126                 } else {
127                         if (strncmp(db_get_version(), "5.6", 3) >= 0) 
128                                 db_query("SET sql_mode = ''");
129                         if (!db_import($path_to_root.'/sql/'.get_post('coa'), $conn, $selected_id)) {
130                                 display_error(_('Cannot create new company due to bugs in sql file.'));
131                                 $error = true;
132                         } 
133                         else
134                         {
135                                 if (!isset($_POST['admpassword']) || $_POST['admpassword'] == "")
136                                         $_POST['admpassword'] = "password";
137                                 update_admin_password($conn, md5($_POST['admpassword']));
138                         }
139                 }
140                 if ($error) {
141                         remove_connection($selected_id);
142                         return false;
143                 }
144         }
145         $error = write_config_db($new);
146
147         if ($error == -1)
148                 display_error(_("Cannot open the configuration file - ") . $path_to_root . "/config_db.php");
149         else if ($error == -2)
150                 display_error(_("Cannot write to the configuration file - ") . $path_to_root . "/config_db.php");
151         else if ($error == -3)
152                 display_error(_("The configuration file ") . $path_to_root . "/config_db.php" . _(" is not writable. Change its permissions so it is, then re-run the operation."));
153         if ($error != 0)
154         {
155                 return false;
156         }
157
158         if ($new)
159         {
160                 create_comp_dirs(company_path($selected_id), $comp_subdirs);
161                 $exts = get_company_extensions();
162                 write_extensions($exts, $selected_id);
163         }
164         display_notification($new ? _('New company has been created.') : _('Company has been updated.'));
165
166         $Mode = 'RESET';
167         return true;
168 }
169
170 //---------------------------------------------------------------------------------------------
171
172 function handle_delete($id)
173 {
174         global $Ajax, $def_coy, $db_connections, $comp_subdirs, $path_to_root, $Mode;
175
176         // First make sure all company directories from the one under removal are writable. 
177         // Without this after operation we end up with changed per-company owners!
178         for($i = $id; $i < count($db_connections); $i++) {
179                         $comp_path = company_path($i);
180                 if (!is_dir($comp_path) || !is_writable($comp_path)) {
181                         display_error(_('Broken company subdirectories system. You have to remove this company manually.'));
182                         return;
183                 }
184         }
185         // make sure config file is writable
186         if (!is_writeable($path_to_root . "/config_db.php"))
187         {
188                 display_error(_("The configuration file ") . $path_to_root . "/config_db.php" . _(" is not writable. Change its permissions so it is, then re-run the operation."));
189                 return;
190         }
191         // rename directory to temporary name to ensure all
192         // other subdirectories will have right owners even after
193         // unsuccessfull removal.
194         $cdir = company_path($id);
195         $tmpname  = company_path('/old_'.$id);
196         if (!@rename($cdir, $tmpname)) {
197                 display_error(_('Cannot rename subdirectory to temporary name.'));
198                 return;
199         }
200         // 'shift' company directories names
201         for ($i = $id+1; $i < count($db_connections); $i++) {
202                 if (!rename(company_path($i), company_path($i-1))) {
203                         display_error(_("Cannot rename company subdirectory"));
204                         return;
205                 }
206         }
207         $err = remove_connection($id);
208         if ($err == 0)
209                 display_error(_("Error removing Database: ") . $id . _(", please remove it manually"));
210
211         if ($def_coy == $id)
212                 $def_coy = 0;
213
214         $error = write_config_db();
215         if ($error == -1)
216                 display_error(_("Cannot open the configuration file - ") . $path_to_root . "/config_db.php");
217         else if ($error == -2)
218                 display_error(_("Cannot write to the configuration file - ") . $path_to_root . "/config_db.php");
219         else if ($error == -3)
220                 display_error(_("The configuration file ") . $path_to_root . "/config_db.php" . _(" is not writable. Change its permissions so it is, then re-run the operation."));
221         if ($error != 0) {
222                 @rename($tmpname, $cdir);
223                 return;
224         }
225         // finally remove renamed company directory
226         @flush_dir($tmpname, true);
227         if (!@rmdir($tmpname))
228         {
229                 display_error(_("Cannot remove temporary renamed company data directory ") . $tmpname);
230                 return;
231         }
232         display_notification(_("Selected company has been deleted"));
233         $Ajax->activate('_page_body');
234         $Mode = 'RESET';
235 }
236
237 //---------------------------------------------------------------------------------------------
238
239 function display_companies()
240 {
241         global $def_coy, $db_connections, $supported_collations;
242
243         $coyno = user_company();
244
245         start_table(TABLESTYLE);
246
247         $th = array(_("Company"), _("Database Host"), _("Database Port"), _("Database User"),
248                 _("Database Name"), _("Table Pref"), _("Charset"), _("Default"), "", "");
249         table_header($th);
250
251         $k=0;
252         $conn = $db_connections;
253         $n = count($conn);
254         for ($i = 0; $i < $n; $i++)
255         {
256                 if ($i == $coyno)
257                 start_row("class='stockmankobg'");
258         else
259                 alt_table_row_color($k);
260
261                 label_cell($conn[$i]['name']);
262                 label_cell($conn[$i]['host']);
263                 label_cell(isset($conn[$i]['port']) ? $conn[$i]['port'] : '');
264                 label_cell($conn[$i]['dbuser']);
265                 label_cell($conn[$i]['dbname']);
266                 label_cell($conn[$i]['tbpref']);
267                 label_cell(isset($conn[$i]['collation']) ? $supported_collations[$conn[$i]['collation']] : '');
268                 label_cell($i == $def_coy ? _("Yes") : _("No"));
269                 edit_button_cell("Edit".$i, _("Edit"));
270                 if ($i != $coyno)
271                 {
272                         delete_button_cell("Delete".$i, _("Delete"));
273                         submit_js_confirm("Delete".$i, 
274                                 sprintf(_("You are about to remove company \'%s\'.\nDo you want to continue ?"), 
275                                         $conn[$i]['name']));
276                 } else
277                         label_cell('');
278                 end_row();
279         }
280
281         end_table();
282     display_note(_("The marked company is the current company which cannot be deleted."), 0, 0, "class='currentfg'");
283     display_note(_("If no Admin Password is entered, the new Admin Password will be '<b>password</b>' by default "));
284     display_note(_("Set Only Port value if you cannot use the default port 3306."));
285 }
286
287 //---------------------------------------------------------------------------------------------
288
289 function display_company_edit($selected_id)
290 {
291         global $def_coy, $db_connections, $tb_pref_counter;
292
293         start_table(TABLESTYLE2);
294
295         if ($selected_id != -1)
296         {
297                 $conn = $db_connections[$selected_id];
298                 $_POST['name'] = $conn['name'];
299                 $_POST['host']  = $conn['host'];
300                 $_POST['port'] = isset($conn['port']) ? $conn['port'] : '';
301                 $_POST['dbuser']  = $conn['dbuser'];
302                 $_POST['dbpassword']  = $conn['dbpassword'];
303                 $_POST['dbname']  = $conn['dbname'];
304                 $_POST['tbpref']  = $conn['tbpref'];
305                 $_POST['def'] = $selected_id == $def_coy;
306                 $_POST['dbcreate']  = false;
307                 $_POST['collation']  = isset($conn['collation']) ? $conn['collation'] : '';
308                 hidden('tbpref', $_POST['tbpref']);
309                 hidden('dbpassword', $_POST['dbpassword']);
310         }
311         else
312         {
313                 $_POST['tbpref'] = $tb_pref_counter."_";
314
315                 // Use current settings as default
316                 $conn = $db_connections[user_company()];
317                 $_POST['name'] = '';
318                 $_POST['host']  = $conn['host'];
319                 $_POST['port']  = isset($conn['port']) ? $conn['port'] : '';
320                 $_POST['dbuser']  = $conn['dbuser'];
321                 $_POST['dbpassword']  = $conn['dbpassword'];
322                 $_POST['dbname']  = $conn['dbname'];
323                 $_POST['collation']  = isset($conn['collation']) ? $conn['collation'] : '';
324                 unset($_POST['def']);
325         }
326
327         text_row_ex(_("Company"), 'name', 50);
328
329         if ($selected_id == -1)
330         {
331                 text_row_ex(_("Host"), 'host', 30, 60);
332                 text_row_ex(_("Port"), 'port', 30, 60);
333                 text_row_ex(_("Database User"), 'dbuser', 30);
334                 text_row_ex(_("Database Password"), 'dbpassword', 30);
335                 text_row_ex(_("Database Name"), 'dbname', 30);
336                 collations_list_row(_("Database Collation:"), 'collation');
337                 yesno_list_row(_("Table Pref"), 'tbpref', 1, $_POST['tbpref'], _("None"), false);
338                 check_row(_("Default Company"), 'def');
339                 coa_list_row(_("Database Script"), 'coa');
340                 text_row_ex(_("New script Admin Password"), 'admpassword', 20);
341         } else {
342                 label_row(_("Host"), $_POST['host']);
343                 label_row(_("Port"), $_POST['port']);
344                 label_row(_("Database User"), $_POST['dbuser']);
345                 label_row(_("Database Name"), $_POST['dbname']);
346                 collations_list_row(_("Database Collation:"), 'collation');
347                 label_row(_("Table Pref"), $_POST['tbpref']);
348                 if (!get_post('def'))
349                         check_row(_("Default Company"), 'def');
350                 else
351                         label_row(_("Default Company"), _("Yes"));
352         }
353
354         end_table(1);
355         hidden('selected_id', $selected_id);
356 }
357
358 //---------------------------------------------------------------------------------------------
359
360 if (($Mode=='ADD_ITEM' || $Mode=='UPDATE_ITEM') && check_data($selected_id))
361         handle_submit($selected_id);
362
363 if ($Mode == 'Delete')
364         handle_delete($selected_id);
365
366 if ($Mode == 'RESET')
367 {
368         $selected_id = -1;
369         unset($_POST);
370 }
371
372 //---------------------------------------------------------------------------------------------
373
374 start_form();
375
376         display_companies();
377         display_company_edit($selected_id);
378         submit_add_or_update_center($selected_id == -1, '', 'upgrade');
379
380 end_form();
381
382 end_page();
383