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 if ($selected_id == -1 && (!isset($_GET['ul']) || $_GET['ul'] != 1))
47 display_error(_("When creating a new company, you must provide a Database script file."));
50 foreach($db_connections as $id=>$con)
52 if($id != $selected_id && $_POST['host'] == $con['host']
53 && $_POST['dbname'] == $con['dbname'])
55 if ($_POST['tbpref'] == $con['tbpref'])
57 display_error(_("This database settings are already used by another company."));
60 if (($_POST['tbpref'] == 0) ^ ($con['tbpref'] == ''))
62 display_error(_("You cannot have table set without prefix together with prefixed sets in the same database."));
70 //---------------------------------------------------------------------------------------------
72 function remove_connection($id) {
73 global $db_connections;
75 $err = db_drop_db($db_connections[$id]);
77 unset($db_connections[$id]);
78 $conn = array_values($db_connections);
79 $db_connections = $conn;
80 //$$db_connections = array_values($db_connections);
83 //---------------------------------------------------------------------------------------------
85 function handle_submit()
87 global $db_connections, $def_coy, $tb_pref_counter, $db,
88 $comp_subdirs, $path_to_root;
95 $new = !isset($db_connections[$id]);
97 $db_connections[$id]['name'] = $_POST['name'];
98 $db_connections[$id]['host'] = $_POST['host'];
99 $db_connections[$id]['dbuser'] = $_POST['dbuser'];
100 $db_connections[$id]['dbpassword'] = $_POST['dbpassword'];
101 $db_connections[$id]['dbname'] = $_POST['dbname'];
102 if (isset($_GET['ul']) && $_GET['ul'] == 1)
104 if (is_numeric($_POST['tbpref']))
106 $db_connections[$id]['tbpref'] = $_POST['tbpref'] == 1 ?
107 $tb_pref_counter."_" : '';
109 else if ($_POST['tbpref'] != "")
110 $db_connections[$id]['tbpref'] = $_POST['tbpref'];
112 $db_connections[$id]['tbpref'] = "";
114 if ((bool)$_POST['def'] == true)
116 if (isset($_GET['ul']) && $_GET['ul'] == 1)
118 $conn = $db_connections[$id];
119 if (($db = db_create_db($conn)) == 0)
121 display_error(_("Error creating Database: ") . $conn['dbname'] . _(", Please create it manually"));
125 $filename = $_FILES['uploadfile']['tmp_name'];
126 if (is_uploaded_file ($filename))
128 if (!db_import($filename, $conn, $id)) {
129 display_error(_('Cannot create new company due to bugs in sql file.'));
132 if (isset($_POST['admpassword']) && $_POST['admpassword'] != "")
133 update_admin_password($conn, md5($_POST['admpassword']));
137 display_error(_("Error uploading Database Script, please upload it manually"));
141 set_global_connection();
143 remove_connection($id);
147 if ($_GET['c'] = 'u') {
148 $conn = $db_connections[$id];
149 if (($db = db_create_db($conn)) == 0)
151 display_error(_("Error connecting to Database: ") . $conn['dbname'] . _(", Please correct it"));
153 } elseif ($_POST['admpassword'] != "") {
154 db_query("UPDATE ".$conn['tbpref']."users set password = '".md5($_POST['admpassword']). "' WHERE user_id = 'admin'");
158 $error = write_config_db($new);
160 display_error(_("Cannot open the configuration file - ") . $path_to_root . "/config_db.php");
161 else if ($error == -2)
162 display_error(_("Cannot write to the configuration file - ") . $path_to_root . "/config_db.php");
163 else if ($error == -3)
164 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."));
172 create_comp_dirs(company_path($id), $comp_subdirs);
174 $exts = get_company_extensions();
175 write_extensions($exts, $id);
176 display_notification($new ? _('New company has been created.') : _('Company has been updated.'));
180 //---------------------------------------------------------------------------------------------
182 function handle_delete()
184 global $def_coy, $db_connections, $comp_subdirs, $path_to_root;
188 // First make sure all company directories from the one under removal are writable.
189 // Without this after operation we end up with changed per-company owners!
190 for($i = $id; $i < count($db_connections); $i++) {
191 $comp_path = company_path($i);
192 if (!is_dir($comp_path) || !is_writable($comp_path)) {
193 display_error(_('Broken company subdirectories system. You have to remove this company manually.'));
197 // make sure config file is writable
198 if (!is_writeable($path_to_root . "/config_db.php"))
200 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."));
203 // rename directory to temporary name to ensure all
204 // other subdirectories will have right owners even after
205 // unsuccessfull removal.
206 $cdir = company_path($id);
207 $tmpname = company_path('/old_'.$id);
208 if (!@rename($cdir, $tmpname)) {
209 display_error(_('Cannot rename subdirectory to temporary name.'));
212 // 'shift' company directories names
213 for ($i = $id+1; $i < count($db_connections); $i++) {
214 if (!rename(company_path($i), company_path($i-1))) {
215 display_error(_("Cannot rename company subdirectory"));
219 $err = remove_connection($id);
221 display_error(_("Error removing Database: ") . $dbase . _(", please remove it manually"));
225 $error = write_config_db();
227 display_error(_("Cannot open the configuration file - ") . $path_to_root . "/config_db.php");
228 else if ($error == -2)
229 display_error(_("Cannot write to the configuration file - ") . $path_to_root . "/config_db.php");
230 else if ($error == -3)
231 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."));
233 @rename($tmpname, $cdir);
236 // finally remove renamed company directory
237 @flush_dir($tmpname, true);
238 if (!@rmdir($tmpname))
240 display_error(_("Cannot remove temporary renamed company data directory ") . $tmpname);
243 display_notification(_("Selected company has been deleted"));
246 //---------------------------------------------------------------------------------------------
248 function display_companies()
250 global $def_coy, $db_connections;
252 $coyno = $_SESSION["wa_current_user"]->company;
255 <script language='javascript'>
256 function deleteCompany(id) {
257 if (!confirm('" . _("Are you sure you want to delete company no. ") . "'+id))
259 document.location.replace('create_coy.php?c=df&id='+id)
262 start_table(TABLESTYLE);
264 $th = array(_("Company"), _("Database Host"), _("Database User"),
265 _("Database Name"), _("Table Pref"), _("Default"), "", "");
269 $conn = $db_connections;
271 for ($i = 0; $i < $n; $i++)
278 start_row("class='stockmankobg'");
280 alt_table_row_color($k);
282 label_cell($conn[$i]['name']);
283 label_cell($conn[$i]['host']);
284 label_cell($conn[$i]['dbuser']);
285 label_cell($conn[$i]['dbname']);
286 label_cell($conn[$i]['tbpref']);
289 $delete = _("Delete");
290 if (user_graphic_links())
292 $edit = set_icon(ICON_EDIT, $edit);
293 $delete = set_icon(ICON_DELETE, $delete);
295 label_cell("<a href='" . $_SERVER['PHP_SELF']. "?selected_id=$i'>$edit</a>");
296 label_cell( $i == $coyno ? '' :
297 "<a href='javascript:deleteCompany(" . $i . ")'>$delete</a>");
302 display_note(_("The marked company is the current company which cannot be deleted."), 0, 0, "class='currentfg'");
305 //---------------------------------------------------------------------------------------------
307 function display_company_edit($selected_id)
309 global $def_coy, $db_connections, $tb_pref_counter;
311 if ($selected_id != -1)
314 $n = count($db_connections);
319 <script language='javascript'>
320 function updateCompany() {
321 if (document.forms[0].uploadfile.value!='' && document.forms[0].dbname.value!='') {
322 document.forms[0].action='create_coy.php?c=u&ul=1&id=" . $n . "&fn=' + document.forms[0].uploadfile.value
325 document.forms[0].action='create_coy.php?c=u&id=" . $n . "'
327 document.forms[0].submit()
331 start_table(TABLESTYLE2);
333 if ($selected_id != -1)
335 $conn = $db_connections[$selected_id];
336 $_POST['name'] = $conn['name'];
337 $_POST['host'] = $conn['host'];
338 $_POST['dbuser'] = $conn['dbuser'];
339 $_POST['dbpassword'] = $conn['dbpassword'];
340 $_POST['dbname'] = $conn['dbname'];
341 $_POST['tbpref'] = $conn['tbpref'];
342 if ($selected_id == $def_coy)
343 $_POST['def'] = true;
345 $_POST['def'] = false;
346 $_POST['dbcreate'] = false;
347 hidden('selected_id', $selected_id);
348 hidden('tbpref', $_POST['tbpref']);
349 hidden('dbpassword', $_POST['dbpassword']);
352 $_POST['tbpref'] = $tb_pref_counter."_";
353 text_row_ex(_("Company"), 'name', 30);
354 text_row_ex(_("Host"), 'host', 30);
355 text_row_ex(_("Database User"), 'dbuser', 30);
356 if ($selected_id == -1)
357 text_row_ex(_("Database Password"), 'dbpassword', 30);
358 text_row_ex(_("Database Name"), 'dbname', 30);
359 if ($selected_id == -1)
360 yesno_list_row(_("Table Pref"), 'tbpref', 1, $_POST['tbpref'], _("None"), false);
362 label_row(_("Table Pref"), $_POST['tbpref']);
363 yesno_list_row(_("Default"), 'def', null, "", "", false);
365 file_row(_("Database Script"), "uploadfile");
367 text_row_ex(_("New script Admin Password"), 'admpassword', 20);
370 display_note(_("Choose from Database scripts in SQL folder. No Database is created without a script."), 0, 1);
371 echo "<center><input onclick='javascript:updateCompany()' type='button' style='width:150px' value='". _("Save"). "'></center>";
378 //---------------------------------------------------------------------------------------------
380 if (isset($_GET['c']) && $_GET['c'] == 'df') {
385 if (isset($_GET['c']) && $_GET['c'] == 'u')
390 //---------------------------------------------------------------------------------------------
394 hyperlink_no_params($_SERVER['PHP_SELF'], _("Create a new company"));
396 display_company_edit($selected_id);
398 //---------------------------------------------------------------------------------------------