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';
14 include_once($path_to_root . "/includes/session.inc");
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");
21 page(_($help_context = "Create/Update Company"));
23 $comp_subdirs = array('images', 'pdf_files', 'backup','js_cache', 'reporting', 'attachments');
25 simple_page_mode(true);
27 FIXME: tb_pref_counter should track prefix per database.
29 //---------------------------------------------------------------------------------------------
30 function check_data($selected_id)
32 global $db_connections, $tb_pref_counter;
34 if ($selected_id != -1) {
35 if ($_POST['name'] == "")
37 display_error(_("Database settings are not specified."));
41 if (!get_post('name') || !get_post('host') || !get_post('dbuser') || !get_post('dbname'))
43 display_error(_("Database settings are not specified."));
46 foreach($db_connections as $id=>$con)
48 if($id != $selected_id && $_POST['host'] == $con['host']
49 && $_POST['dbname'] == $con['dbname'])
51 if ($_POST['tbpref'] == $con['tbpref'])
53 display_error(_("This database settings are already used by another company."));
56 if (($_POST['tbpref'] == 0) ^ ($con['tbpref'] == ''))
58 display_error(_("You cannot have table set without prefix together with prefixed sets in the same database."));
67 //---------------------------------------------------------------------------------------------
69 function remove_connection($id) {
70 global $db_connections;
72 $err = db_drop_db($db_connections[$id]);
74 unset($db_connections[$id]);
75 $conn = array_values($db_connections);
76 $db_connections = $conn;
79 //---------------------------------------------------------------------------------------------
81 function handle_submit($selected_id)
83 global $db_connections, $def_coy, $tb_pref_counter, $db,
84 $comp_subdirs, $path_to_root, $Mode;
89 $selected_id = count($db_connections);
91 $new = !isset($db_connections[$selected_id]);
93 if (check_value('def'))
94 $def_coy = $selected_id;
96 $db_connections[$selected_id]['name'] = $_POST['name'];
98 $db_connections[$selected_id]['host'] = $_POST['host'];
99 $db_connections[$selected_id]['dbuser'] = $_POST['dbuser'];
100 $db_connections[$selected_id]['dbpassword'] = html_entity_decode($_POST['dbpassword'], ENT_QUOTES,
101 $_SESSION['language']->encoding=='iso-8859-2' ? 'ISO-8859-1' : $_SESSION['language']->encoding);
102 $db_connections[$selected_id]['dbname'] = $_POST['dbname'];
103 $db_connections[$selected_id]['collation'] = $_POST['collation'];
104 if (is_numeric($_POST['tbpref']))
106 $db_connections[$selected_id]['tbpref'] = $_POST['tbpref'] == 1 ?
107 $tb_pref_counter."_" : '';
109 else if ($_POST['tbpref'] != "")
110 $db_connections[$selected_id]['tbpref'] = $_POST['tbpref'];
112 $db_connections[$selected_id]['tbpref'] = "";
114 $conn = $db_connections[$selected_id];
115 if (($db = db_create_db($conn)) === false)
117 display_error(_("Error creating Database: ") . $conn['dbname'] . _(", Please create it manually"));
120 if (strncmp(db_get_version(), "5.6", 3) >= 0)
121 db_query("SET sql_mode = ''");
122 if (!db_import($path_to_root.'/sql/'.get_post('coa'), $conn, $selected_id)) {
123 display_error(_('Cannot create new company due to bugs in sql file.'));
128 if (!isset($_POST['admpassword']) || $_POST['admpassword'] == "")
129 $_POST['admpassword'] = "password";
130 update_admin_password($conn, md5($_POST['admpassword']));
134 remove_connection($selected_id);
138 $error = write_config_db($new);
141 display_error(_("Cannot open the configuration file - ") . $path_to_root . "/config_db.php");
142 else if ($error == -2)
143 display_error(_("Cannot write to the configuration file - ") . $path_to_root . "/config_db.php");
144 else if ($error == -3)
145 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 create_comp_dirs(company_path($selected_id), $comp_subdirs);
154 $exts = get_company_extensions();
155 write_extensions($exts, $selected_id);
157 display_notification($new ? _('New company has been created.') : _('Company has been updated.'));
163 //---------------------------------------------------------------------------------------------
165 function handle_delete($id)
167 global $Ajax, $def_coy, $db_connections, $comp_subdirs, $path_to_root, $Mode;
169 // First make sure all company directories from the one under removal are writable.
170 // Without this after operation we end up with changed per-company owners!
171 for($i = $id; $i < count($db_connections); $i++) {
172 $comp_path = company_path($i);
173 if (!is_dir($comp_path) || !is_writable($comp_path)) {
174 display_error(_('Broken company subdirectories system. You have to remove this company manually.'));
178 // make sure config file is writable
179 if (!is_writeable($path_to_root . "/config_db.php"))
181 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."));
184 // rename directory to temporary name to ensure all
185 // other subdirectories will have right owners even after
186 // unsuccessfull removal.
187 $cdir = company_path($id);
188 $tmpname = company_path('/old_'.$id);
189 if (!@rename($cdir, $tmpname)) {
190 display_error(_('Cannot rename subdirectory to temporary name.'));
193 // 'shift' company directories names
194 for ($i = $id+1; $i < count($db_connections); $i++) {
195 if (!rename(company_path($i), company_path($i-1))) {
196 display_error(_("Cannot rename company subdirectory"));
200 $err = remove_connection($id);
202 display_error(_("Error removing Database: ") . $id . _(", please remove it manually"));
207 $error = write_config_db();
209 display_error(_("Cannot open the configuration file - ") . $path_to_root . "/config_db.php");
210 else if ($error == -2)
211 display_error(_("Cannot write to the configuration file - ") . $path_to_root . "/config_db.php");
212 else if ($error == -3)
213 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."));
215 @rename($tmpname, $cdir);
218 // finally remove renamed company directory
219 @flush_dir($tmpname, true);
220 if (!@rmdir($tmpname))
222 display_error(_("Cannot remove temporary renamed company data directory ") . $tmpname);
225 display_notification(_("Selected company has been deleted"));
226 $Ajax->activate('_page_body');
230 //---------------------------------------------------------------------------------------------
232 function display_companies()
234 global $def_coy, $db_connections, $supported_collations;
236 $coyno = user_company();
238 start_table(TABLESTYLE);
240 $th = array(_("Company"), _("Database Host"), _("Database User"),
241 _("Database Name"), _("Table Pref"), _("Charset"), _("Default"), "", "");
245 $conn = $db_connections;
247 for ($i = 0; $i < $n; $i++)
250 start_row("class='stockmankobg'");
252 alt_table_row_color($k);
254 label_cell($conn[$i]['name']);
255 label_cell($conn[$i]['host']);
256 label_cell($conn[$i]['dbuser']);
257 label_cell($conn[$i]['dbname']);
258 label_cell($conn[$i]['tbpref']);
259 label_cell($supported_collations[$conn[$i]['collation']]);
260 label_cell($i == $def_coy ? _("Yes") : _("No"));
261 edit_button_cell("Edit".$i, _("Edit"));
264 delete_button_cell("Delete".$i, _("Delete"));
265 submit_js_confirm("Delete".$i,
266 sprintf(_("You are about to remove company \'%s\'.\nDo you want to continue ?"),
274 display_note(_("The marked company is the current company which cannot be deleted."), 0, 0, "class='currentfg'");
275 display_note(_("If no Admin Password is entered, the new Admin Password will be '<b>password</b>' by default "));
278 //---------------------------------------------------------------------------------------------
280 function display_company_edit($selected_id)
282 global $def_coy, $db_connections, $tb_pref_counter;
284 start_table(TABLESTYLE2);
286 if ($selected_id != -1)
288 $conn = $db_connections[$selected_id];
289 $_POST['name'] = $conn['name'];
290 $_POST['host'] = $conn['host'];
291 $_POST['dbuser'] = $conn['dbuser'];
292 $_POST['dbpassword'] = $conn['dbpassword'];
293 $_POST['dbname'] = $conn['dbname'];
294 $_POST['tbpref'] = $conn['tbpref'];
295 $_POST['def'] = $selected_id == $def_coy;
296 $_POST['dbcreate'] = false;
297 $_POST['collation'] = $conn['collation'];
298 hidden('tbpref', $_POST['tbpref']);
299 hidden('dbpassword', $_POST['dbpassword']);
303 $_POST['tbpref'] = $tb_pref_counter."_";
305 // Use current settings as default
306 $conn = $db_connections[user_company()];
308 $_POST['host'] = $conn['host'];
309 $_POST['dbuser'] = $conn['dbuser'];
310 $_POST['dbpassword'] = $conn['dbpassword'];
311 $_POST['dbname'] = $conn['dbname'];
312 $_POST['collation'] = $conn['collation'];
313 unset($_POST['def']);
316 text_row_ex(_("Company"), 'name', 50);
318 if ($selected_id == -1)
320 text_row_ex(_("Host"), 'host', 30, 60);
321 text_row_ex(_("Database User"), 'dbuser', 30);
322 text_row_ex(_("Database Password"), 'dbpassword', 30);
323 text_row_ex(_("Database Name"), 'dbname', 30);
324 collations_list_row(_("Database Collation:"), 'collation');
325 yesno_list_row(_("Table Pref"), 'tbpref', 1, $_POST['tbpref'], _("None"), false);
326 check_row(_("Default Company"), 'def');
327 coa_list_row(_("Database Script"), 'coa');
328 text_row_ex(_("New script Admin Password"), 'admpassword', 20);
330 label_row(_("Host"), $_POST['host']);
331 label_row(_("Database User"), $_POST['dbuser']);
332 label_row(_("Database Name"), $_POST['dbname']);
333 label_row(_("Table Pref"), $_POST['tbpref']);
334 if (!get_post('def'))
335 check_row(_("Default Company"), 'def');
337 label_row(_("Default Company"), _("Yes"));
341 hidden('selected_id', $selected_id);
344 //---------------------------------------------------------------------------------------------
346 if (($Mode=='ADD_ITEM' || $Mode=='UPDATE_ITEM') && check_data($selected_id))
347 handle_submit($selected_id);
349 if ($Mode == 'Delete')
350 handle_delete($selected_id);
352 if ($Mode == 'RESET')
358 //---------------------------------------------------------------------------------------------
363 display_company_edit($selected_id);
364 submit_add_or_update_center($selected_id == -1, '', 'upgrade');