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 = $_GET['selected_id'];
30 elseif (isset($_POST['selected_id']))
32 $selected_id = $_POST['selected_id'];
37 //---------------------------------------------------------------------------------------------
41 global $db_connections, $tb_pref_counter, $selected_id;
43 if ($_POST['name'] == "" || $_POST['host'] == "" || $_POST['dbuser'] == "" || $_POST['dbname'] == "")
45 foreach($db_connections as $id=>$con)
47 if($id != $selected_id && $_POST['host'] == $con['host']
48 && $_POST['dbname'] == $con['dbname'])
50 if ($_POST['tbpref'] == $con['tbpref'])
52 display_error(_("This database settings are already used by another company."));
55 if (($_POST['tbpref'] == 0) ^ ($con['tbpref'] == ''))
57 display_error(_("You cannot have table set without prefix together with prefixed sets in the same database."));
65 //---------------------------------------------------------------------------------------------
67 function remove_connection($id) {
68 global $db_connections;
70 $err = db_drop_db($db_connections[$id]);
72 unset($db_connections[$id]);
73 $conn = array_values($db_connections);
74 $db_connections = $conn;
75 //$$db_connections = array_values($db_connections);
78 //---------------------------------------------------------------------------------------------
80 function handle_submit()
82 global $db_connections, $def_coy, $tb_pref_counter, $db,
83 $comp_path, $comp_subdirs, $path_to_root;
92 $db_connections[$id]['name'] = $_POST['name'];
93 $db_connections[$id]['host'] = $_POST['host'];
94 $db_connections[$id]['dbuser'] = $_POST['dbuser'];
95 $db_connections[$id]['dbpassword'] = $_POST['dbpassword'];
96 $db_connections[$id]['dbname'] = $_POST['dbname'];
97 if (isset($_GET['ul']) && $_GET['ul'] == 1)
99 if (is_numeric($_POST['tbpref']))
101 $db_connections[$id]['tbpref'] = $_POST['tbpref'] == 1 ?
102 $tb_pref_counter."_" : '';
105 else if ($_POST['tbpref'] != "")
106 $db_connections[$id]['tbpref'] = $_POST['tbpref'];
108 $db_connections[$id]['tbpref'] = "";
110 if ((bool)$_POST['def'] == true)
112 if (isset($_GET['ul']) && $_GET['ul'] == 1)
114 $conn = $db_connections[$id];
115 if (($db = db_create_db($conn)) == 0)
117 display_error(_("Error creating Database: ") . $conn['dbname'] . _(", Please create it manually"));
121 $filename = $_FILES['uploadfile']['tmp_name'];
122 if (is_uploaded_file ($filename))
124 if (!db_import($filename, $conn, $id)) {
125 display_error(_('Cannot create new company due to bugs in sql file.'));
128 if (isset($_POST['admpassword']) && $_POST['admpassword'] != "")
129 db_query("UPDATE ".$conn['tbpref']."users set password = '".md5($_POST['admpassword']). "' WHERE user_id = 'admin'");
133 display_error(_("Error uploading Database Script, please upload it manually"));
137 set_global_connection();
139 remove_connection($id);
143 if ($_GET['c'] = 'u') {
144 $conn = $db_connections[$id];
145 if (($db = db_create_db($conn)) == 0)
147 display_error(_("Error connecting to Database: ") . $conn['dbname'] . _(", Please correct it"));
150 db_query("UPDATE ".$conn['tbpref']."users set password = '".md5($_POST['admpassword']). "' WHERE user_id = 'admin'");
154 $error = write_config_db($new);
156 display_error(_("Cannot open the configuration file - ") . $path_to_root . "/config_db.php");
157 else if ($error == -2)
158 display_error(_("Cannot write to the configuration file - ") . $path_to_root . "/config_db.php");
159 else if ($error == -3)
160 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."));
168 create_comp_dirs("$comp_path/$id", $comp_subdirs);
170 $exts = get_company_extensions();
171 write_extensions($exts, $id);
172 display_notification($new ? _('New company has been created.') : _('Company has been updated.'));
176 //---------------------------------------------------------------------------------------------
178 function handle_delete()
180 global $comp_path, $def_coy, $db_connections, $comp_subdirs, $path_to_root;
184 // First make sure all company directories from the one under removal are writable.
185 // Without this after operation we end up with changed per-company owners!
186 for($i = $id; $i < count($db_connections); $i++) {
187 if (!is_dir($comp_path.'/'.$i) || !is_writable($comp_path.'/'.$i)) {
188 display_error(_('Broken company subdirectories system. You have to remove this company manually.'));
192 // make sure config file is writable
193 if (!is_writeable($path_to_root . "/config_db.php"))
195 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."));
198 // rename directory to temporary name to ensure all
199 // other subdirectories will have right owners even after
200 // unsuccessfull removal.
201 $cdir = $comp_path.'/'.$id;
202 $tmpname = $comp_path.'/old_'.$id;
203 if (!@rename($cdir, $tmpname)) {
204 display_error(_('Cannot rename subdirectory to temporary name.'));
207 // 'shift' company directories names
208 for ($i = $id+1; $i < count($db_connections); $i++) {
209 if (!rename($comp_path.'/'.$i, $comp_path.'/'.($i-1))) {
210 display_error(_("Cannot rename company subdirectory"));
214 $err = remove_connection($id);
216 display_error(_("Error removing Database: ") . $dbase . _(", please remove it manually"));
220 $error = write_config_db();
222 display_error(_("Cannot open the configuration file - ") . $path_to_root . "/config_db.php");
223 else if ($error == -2)
224 display_error(_("Cannot write to the configuration file - ") . $path_to_root . "/config_db.php");
225 else if ($error == -3)
226 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."));
228 @rename($tmpname, $cdir);
231 // finally remove renamed company directory
232 @flush_dir($tmpname, true);
233 if (!@rmdir($tmpname))
235 display_error(_("Cannot remove temporary renamed company data directory ") . $tmpname);
238 display_notification(_("Selected company as been deleted"));
241 //---------------------------------------------------------------------------------------------
243 function display_companies()
245 global $table_style, $def_coy, $db_connections;
247 $coyno = $_SESSION["wa_current_user"]->company;
250 <script language='javascript'>
251 function deleteCompany(id) {
252 if (!confirm('" . _("Are you sure you want to delete company no. ") . "'+id))
254 document.location.replace('create_coy.php?c=df&id='+id)
257 start_table($table_style);
259 $th = array(_("Company"), _("Database Host"), _("Database User"),
260 _("Database Name"), _("Table Pref"), _("Default"), "", "");
264 $conn = $db_connections;
266 for ($i = 0; $i < $n; $i++)
273 start_row("class='stockmankobg'");
275 alt_table_row_color($k);
277 label_cell($conn[$i]['name']);
278 label_cell($conn[$i]['host']);
279 label_cell($conn[$i]['dbuser']);
280 label_cell($conn[$i]['dbname']);
281 label_cell($conn[$i]['tbpref']);
284 $delete = _("Delete");
285 if (user_graphic_links())
287 $edit = set_icon(ICON_EDIT, $edit);
288 $delete = set_icon(ICON_DELETE, $delete);
290 label_cell("<a href='" . $_SERVER['PHP_SELF']. "?selected_id=$i'>$edit</a>");
291 label_cell( $i == $coyno ? '' :
292 "<a href='javascript:deleteCompany(" . $i . ")'>$delete</a>");
297 display_note(_("The marked company is the current company which cannot be deleted."), 0, 0, "class='currentfg'");
300 //---------------------------------------------------------------------------------------------
302 function display_company_edit($selected_id)
304 global $def_coy, $db_connections, $tb_pref_counter, $table_style2;
306 if ($selected_id != -1)
309 $n = count($db_connections);
314 <script language='javascript'>
315 function updateCompany() {
316 if (document.forms[0].uploadfile.value!='' && document.forms[0].dbname.value!='') {
317 document.forms[0].action='create_coy.php?c=u&ul=1&id=" . $n . "&fn=' + document.forms[0].uploadfile.value
320 document.forms[0].action='create_coy.php?c=u&id=" . $n . "'
322 document.forms[0].submit()
326 start_table($table_style2);
328 if ($selected_id != -1)
330 $conn = $db_connections[$selected_id];
331 $_POST['name'] = $conn['name'];
332 $_POST['host'] = $conn['host'];
333 $_POST['dbuser'] = $conn['dbuser'];
334 $_POST['dbpassword'] = $conn['dbpassword'];
335 $_POST['dbname'] = $conn['dbname'];
336 $_POST['tbpref'] = $conn['tbpref'];
337 if ($selected_id == $def_coy)
338 $_POST['def'] = true;
340 $_POST['def'] = false;
341 $_POST['dbcreate'] = false;
342 hidden('selected_id', $selected_id);
343 hidden('tbpref', $_POST['tbpref']);
344 hidden('dbpassword', $_POST['dbpassword']);
347 $_POST['tbpref'] = $tb_pref_counter."_";
348 text_row_ex(_("Company"), 'name', 30);
349 text_row_ex(_("Host"), 'host', 30);
350 text_row_ex(_("Database User"), 'dbuser', 30);
351 if ($selected_id == -1)
352 text_row_ex(_("Database Password"), 'dbpassword', 30);
353 text_row_ex(_("Database Name"), 'dbname', 30);
354 if ($selected_id == -1)
355 yesno_list_row(_("Table Pref"), 'tbpref', 1, $_POST['tbpref'], _("None"), false);
357 label_row(_("Table Pref"), $_POST['tbpref']);
358 yesno_list_row(_("Default"), 'def', null, "", "", false);
360 file_row(_("Database Script"), "uploadfile");
362 text_row_ex(_("New script Admin Password"), 'admpassword', 20);
365 display_note(_("Choose from Database scripts in SQL folder. No Database is created without a script."), 0, 1);
366 echo "<center><input onclick='javascript:updateCompany()' type='button' style='width:150px' value='". _("Save"). "'></center>";
373 //---------------------------------------------------------------------------------------------
375 if (isset($_GET['c']) && $_GET['c'] == 'df') {
380 if (isset($_GET['c']) && $_GET['c'] == 'u')
385 //---------------------------------------------------------------------------------------------
389 hyperlink_no_params($_SERVER['PHP_SELF'], _("Create a new company"));
391 display_company_edit($selected_id);
393 //---------------------------------------------------------------------------------------------