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 if ($_POST['port'] != '' && !is_numeric($_POST['port']))
48 display_error(_('Database port has to be numeric or empty.'));
52 foreach($db_connections as $id=>$con)
54 if($id != $selected_id && $_POST['host'] == $con['host']
55 && $_POST['dbname'] == $con['dbname'])
57 if ($_POST['tbpref'] == $con['tbpref'])
59 display_error(_("This database settings are already used by another company."));
62 if (($_POST['tbpref'] == 0) ^ ($con['tbpref'] == ''))
64 display_error(_("You cannot have table set without prefix together with prefixed sets in the same database."));
73 //---------------------------------------------------------------------------------------------
75 function remove_connection($id) {
76 global $db_connections;
78 $err = db_drop_db($db_connections[$id]);
80 unset($db_connections[$id]);
81 $conn = array_values($db_connections);
82 $db_connections = $conn;
85 //---------------------------------------------------------------------------------------------
87 function handle_submit($selected_id)
89 global $db_connections, $def_coy, $tb_pref_counter, $db,
90 $comp_subdirs, $path_to_root, $Mode;
95 $selected_id = count($db_connections);
97 $new = !isset($db_connections[$selected_id]);
99 if (check_value('def'))
100 $def_coy = $selected_id;
102 $db_connections[$selected_id]['name'] = $_POST['name'];
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']))
113 $db_connections[$selected_id]['tbpref'] = $_POST['tbpref'] == 1 ?
114 $tb_pref_counter."_" : '';
116 else if ($_POST['tbpref'] != "")
117 $db_connections[$selected_id]['tbpref'] = $_POST['tbpref'];
119 $db_connections[$selected_id]['tbpref'] = "";
121 $conn = $db_connections[$selected_id];
122 if (($db = db_create_db($conn)) === false)
124 display_error(_("Error creating Database: ") . $conn['dbname'] . _(", Please create it manually"));
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.'));
135 if (!isset($_POST['admpassword']) || $_POST['admpassword'] == "")
136 $_POST['admpassword'] = "password";
137 update_admin_password($conn, md5($_POST['admpassword']));
141 remove_connection($selected_id);
145 $error = write_config_db($new);
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."));
160 create_comp_dirs(company_path($selected_id), $comp_subdirs);
161 $exts = get_company_extensions();
162 write_extensions($exts, $selected_id);
164 display_notification($new ? _('New company has been created.') : _('Company has been updated.'));
170 //---------------------------------------------------------------------------------------------
172 function handle_delete($id)
174 global $Ajax, $def_coy, $db_connections, $comp_subdirs, $path_to_root, $Mode;
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.'));
185 // make sure config file is writable
186 if (!is_writeable($path_to_root . "/config_db.php"))
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."));
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.'));
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"));
207 $err = remove_connection($id);
209 display_error(_("Error removing Database: ") . $id . _(", please remove it manually"));
214 $error = write_config_db();
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."));
222 @rename($tmpname, $cdir);
225 // finally remove renamed company directory
226 @flush_dir($tmpname, true);
227 if (!@rmdir($tmpname))
229 display_error(_("Cannot remove temporary renamed company data directory ") . $tmpname);
232 display_notification(_("Selected company has been deleted"));
233 $Ajax->activate('_page_body');
237 //---------------------------------------------------------------------------------------------
239 function display_companies()
241 global $def_coy, $db_connections, $supported_collations;
243 $coyno = user_company();
245 start_table(TABLESTYLE);
247 $th = array(_("Company"), _("Database Host"), _("Database Port"), _("Database User"),
248 _("Database Name"), _("Table Pref"), _("Charset"), _("Default"), "", "");
252 $conn = $db_connections;
254 for ($i = 0; $i < $n; $i++)
257 start_row("class='stockmankobg'");
259 alt_table_row_color($k);
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"));
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 ?"),
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."));
287 //---------------------------------------------------------------------------------------------
289 function display_company_edit($selected_id)
291 global $def_coy, $db_connections, $tb_pref_counter;
293 start_table(TABLESTYLE2);
295 if ($selected_id != -1)
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']);
313 $_POST['tbpref'] = $tb_pref_counter."_";
315 // Use current settings as default
316 $conn = $db_connections[user_company()];
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']);
327 text_row_ex(_("Company"), 'name', 50);
329 if ($selected_id == -1)
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);
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');
351 label_row(_("Default Company"), _("Yes"));
355 hidden('selected_id', $selected_id);
358 //---------------------------------------------------------------------------------------------
360 if (($Mode=='ADD_ITEM' || $Mode=='UPDATE_ITEM') && check_data($selected_id))
361 handle_submit($selected_id);
363 if ($Mode == 'Delete')
364 handle_delete($selected_id);
366 if ($Mode == 'RESET')
372 //---------------------------------------------------------------------------------------------
377 display_company_edit($selected_id);
378 submit_add_or_update_center($selected_id == -1, '', 'upgrade');