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 //---------------------------------------------------------------------------------------------
26 if (isset($_GET['selected_id']))
28 $selected_id = (int)$_GET['selected_id'];
30 elseif (isset($_POST['selected_id']))
32 $selected_id = (int)$_POST['selected_id'];
37 //---------------------------------------------------------------------------------------------
39 function check_data($selected_id)
41 global $db_connections, $tb_pref_counter;
43 if($selected_id != -1) {
44 if ($_POST['name'] == "")
46 display_error(_("Database settings are not specified."));
50 if ($_POST['name'] == "" || $_POST['host'] == "" || $_POST['dbuser'] == "" || $_POST['dbname'] == "")
52 display_error(_("Database settings are not specified."));
55 foreach($db_connections as $id=>$con)
57 if($id != $selected_id && $_POST['host'] == $con['host']
58 && $_POST['dbname'] == $con['dbname'])
60 if ($_POST['tbpref'] == $con['tbpref'])
62 display_error(_("This database settings are already used by another company."));
65 if (($_POST['tbpref'] == 0) ^ ($con['tbpref'] == ''))
67 display_error(_("You cannot have table set without prefix together with prefixed sets in the same database."));
76 //---------------------------------------------------------------------------------------------
78 function remove_connection($id) {
79 global $db_connections;
81 $err = db_drop_db($db_connections[$id]);
83 unset($db_connections[$id]);
84 $conn = array_values($db_connections);
85 $db_connections = $conn;
86 //$$db_connections = array_values($db_connections);
89 //---------------------------------------------------------------------------------------------
91 function handle_submit($selected_id)
93 global $Ajax, $db_connections, $def_coy, $tb_pref_counter, $db,
94 $comp_subdirs, $path_to_root;
97 if (!check_data($selected_id))
100 if ($selected_id==-1)
101 $selected_id = count($db_connections);
103 $new = !isset($db_connections[$selected_id]);
105 if ((bool)$_POST['def'] == true)
106 $def_coy = $selected_id;
108 $db_connections[$selected_id]['name'] = $_POST['name'];
110 $db_connections[$selected_id]['host'] = $_POST['host'];
111 $db_connections[$selected_id]['dbuser'] = $_POST['dbuser'];
112 $db_connections[$selected_id]['dbpassword'] = html_entity_decode($_POST['dbpassword'], ENT_QUOTES,
113 $_SESSION['language']->encoding=='iso-8859-2' ? 'ISO-8859-1' : $_SESSION['language']->encoding);
114 $db_connections[$selected_id]['dbname'] = $_POST['dbname'];
115 if (is_numeric($_POST['tbpref']))
117 $db_connections[$selected_id]['tbpref'] = $_POST['tbpref'] == 1 ?
118 $tb_pref_counter."_" : '';
120 else if ($_POST['tbpref'] != "")
121 $db_connections[$selected_id]['tbpref'] = $_POST['tbpref'];
123 $db_connections[$selected_id]['tbpref'] = "";
125 $conn = $db_connections[$selected_id];
126 if (($db = db_create_db($conn)) === false)
128 display_error(_("Error creating Database: ") . $conn['dbname'] . _(", Please create it manually"));
131 if (strncmp(db_get_version(), "5.6", 3) >= 0)
132 db_query("SET sql_mode = ''");
133 if (!db_import($path_to_root.'/sql/'.get_post('coa'), $conn, $selected_id)) {
134 display_error(_('Cannot create new company due to bugs in sql file.'));
139 if (!isset($_POST['admpassword']) || $_POST['admpassword'] == "")
140 $_POST['admpassword'] = "password";
141 update_admin_password($conn, md5($_POST['admpassword']));
145 remove_connection($selected_id);
150 $error = write_config_db($new);
151 set_global_connection();
153 display_error(_("Cannot open the configuration file - ") . $path_to_root . "/config_db.php");
154 else if ($error == -2)
155 display_error(_("Cannot write to the configuration file - ") . $path_to_root . "/config_db.php");
156 else if ($error == -3)
157 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."));
165 create_comp_dirs(company_path($selected_id), $comp_subdirs);
167 $exts = get_company_extensions();
168 write_extensions($exts, $selected_id);
169 display_notification($new ? _('New company has been created.') : _('Company has been updated.'));
170 $Ajax->activate('_page_body');
174 //---------------------------------------------------------------------------------------------
176 function handle_delete($id)
178 global $Ajax, $def_coy, $db_connections, $comp_subdirs, $path_to_root;
180 // First make sure all company directories from the one under removal are writable.
181 // Without this after operation we end up with changed per-company owners!
182 for($i = $id; $i < count($db_connections); $i++) {
183 $comp_path = company_path($i);
184 if (!is_dir($comp_path) || !is_writable($comp_path)) {
185 display_error(_('Broken company subdirectories system. You have to remove this company manually.'));
189 // make sure config file is writable
190 if (!is_writeable($path_to_root . "/config_db.php"))
192 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."));
195 // rename directory to temporary name to ensure all
196 // other subdirectories will have right owners even after
197 // unsuccessfull removal.
198 $cdir = company_path($id);
199 $tmpname = company_path('/old_'.$id);
200 if (!@rename($cdir, $tmpname)) {
201 display_error(_('Cannot rename subdirectory to temporary name.'));
204 // 'shift' company directories names
205 for ($i = $id+1; $i < count($db_connections); $i++) {
206 if (!rename(company_path($i), company_path($i-1))) {
207 display_error(_("Cannot rename company subdirectory"));
211 $err = remove_connection($id);
213 display_error(_("Error removing Database: ") . $dbase . _(", please remove it manually"));
218 $error = write_config_db();
220 display_error(_("Cannot open the configuration file - ") . $path_to_root . "/config_db.php");
221 else if ($error == -2)
222 display_error(_("Cannot write to the configuration file - ") . $path_to_root . "/config_db.php");
223 else if ($error == -3)
224 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."));
226 @rename($tmpname, $cdir);
229 // finally remove renamed company directory
230 @flush_dir($tmpname, true);
231 if (!@rmdir($tmpname))
233 display_error(_("Cannot remove temporary renamed company data directory ") . $tmpname);
236 display_notification(_("Selected company has been deleted"));
237 $Ajax->activate('_page_body');
240 //---------------------------------------------------------------------------------------------
242 function display_companies()
244 global $def_coy, $db_connections;
246 $coyno = user_company();
249 <script type='text/javascript'>
250 function deleteCompany(id, name) {
251 if (!confirm('" . _("Are you sure you want to delete company no. ") . "'+id+' '+name+'?'))
253 document.location.replace('create_coy.php?c=df&id='+id)
256 start_table(TABLESTYLE);
258 $th = array(_("Company"), _("Database Host"), _("Database User"),
259 _("Database Name"), _("Table Pref"), _("Default"), "", "");
263 $conn = $db_connections;
265 for ($i = 0; $i < $n; $i++)
272 start_row("class='stockmankobg'");
274 alt_table_row_color($k);
276 label_cell($conn[$i]['name']);
277 label_cell($conn[$i]['host']);
278 label_cell($conn[$i]['dbuser']);
279 label_cell($conn[$i]['dbname']);
280 label_cell($conn[$i]['tbpref']);
283 $delete = _("Delete");
284 if (user_graphic_links())
286 $edit = set_icon(ICON_EDIT, $edit);
287 $delete = set_icon(ICON_DELETE, $delete);
289 label_cell("<a href='" . $_SERVER['PHP_SELF']. "?selected_id=$i'>$edit</a>");
290 $name = "\"".$conn[$i]['name']."\"";
291 label_cell( $i == $coyno ? '' :
292 "<a href='javascript:deleteCompany($i, $name)'>$delete</a>");
297 display_note(_("The marked company is the current company which cannot be deleted."), 0, 0, "class='currentfg'");
298 display_note(_("If no Admin Password is entered, the new Admin Password will be '<b>password</b>' by default "), 1, 0, "class='currentfg'");
301 //---------------------------------------------------------------------------------------------
303 function display_company_edit($selected_id)
305 global $def_coy, $db_connections, $tb_pref_counter;
309 start_table(TABLESTYLE2);
311 if ($selected_id != -1)
313 $conn = $db_connections[$selected_id];
314 $_POST['name'] = $conn['name'];
315 $_POST['host'] = $conn['host'];
316 $_POST['dbuser'] = $conn['dbuser'];
317 $_POST['dbpassword'] = $conn['dbpassword'];
318 $_POST['dbname'] = $conn['dbname'];
319 $_POST['tbpref'] = $conn['tbpref'];
320 if ($selected_id == $def_coy)
321 $_POST['def'] = true;
323 $_POST['def'] = false;
324 $_POST['dbcreate'] = false;
325 hidden('selected_id', $selected_id);
326 hidden('tbpref', $_POST['tbpref']);
327 hidden('dbpassword', $_POST['dbpassword']);
331 $_POST['tbpref'] = $tb_pref_counter."_";
332 // Insert the current settings as default
333 $conn = $db_connections[user_company()];
335 $_POST['host'] = $conn['host'];
336 $_POST['dbuser'] = $conn['dbuser'];
337 $_POST['dbpassword'] = $conn['dbpassword'];
338 $_POST['dbname'] = $conn['dbname'];
341 text_row_ex(_("Company"), 'name', 50);
343 if ($selected_id == -1)
345 text_row_ex(_("Host"), 'host', 30, 60);
346 text_row_ex(_("Database User"), 'dbuser', 30);
347 text_row_ex(_("Database Password"), 'dbpassword', 30);
348 text_row_ex(_("Database Name"), 'dbname', 30);
349 yesno_list_row(_("Table Pref"), 'tbpref', 1, $_POST['tbpref'], _("None"), false);
351 label_row(_("Host"), $_POST['host']);
352 label_row(_("Database User"), $_POST['dbuser']);
353 label_row(_("Database Name"), $_POST['dbname']);
354 label_row(_("Table Pref"), $_POST['tbpref']);
356 yesno_list_row(_("Default"), 'def', null, "", "", false);
358 if ($selected_id == -1)
360 coa_list_row(_("Database Script"), 'coa');
361 text_row_ex(_("New script Admin Password"), 'admpassword', 20);
365 submit_center('save', _("Save"));
371 //---------------------------------------------------------------------------------------------
373 if (isset($_GET['c']) && $_GET['c'] == 'df') {
374 handle_delete( (int)$_GET['id'] );
378 if (get_post('save')) {
379 if (handle_submit($selected_id))
383 //---------------------------------------------------------------------------------------------
387 hyperlink_no_params($_SERVER['PHP_SELF'], _("Create a new company"));
389 display_company_edit($selected_id);
391 //---------------------------------------------------------------------------------------------