# {0000481] No warning when user does not supply db credentials in create_coy
[fa-stable.git] / admin / create_coy.php
1 <?php
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';
13 $path_to_root="..";
14 include_once($path_to_root . "/includes/session.inc");
15
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");
20
21 page(_($help_context = "Create/Update Company"));
22
23 $comp_subdirs = array('images', 'pdf_files', 'backup','js_cache', 'reporting', 'attachments');
24
25 //---------------------------------------------------------------------------------------------
26 if (isset($_GET['selected_id']))
27 {
28         $selected_id = $_GET['selected_id'];
29 }
30 elseif (isset($_POST['selected_id']))
31 {
32         $selected_id = $_POST['selected_id'];
33 }
34 else
35         $selected_id = -1;
36
37 //---------------------------------------------------------------------------------------------
38
39 function check_data()
40 {
41         global $db_connections, $tb_pref_counter, $selected_id;
42
43         if ($_POST['name'] == "" || $_POST['host'] == "" || $_POST['dbuser'] == "" || $_POST['dbname'] == "")
44         {
45                 display_error(_("Database setting are not specified."));
46                 return false;
47         }
48
49         foreach($db_connections as $id=>$con)
50         {
51          if($id != $selected_id && $_POST['host'] == $con['host'] 
52                 && $_POST['dbname'] == $con['dbname'])
53                 {
54                         if ($_POST['tbpref'] == $con['tbpref'])
55                         {
56                                 display_error(_("This database settings are already used by another company."));
57                                 return false;
58                         }
59                         if (($_POST['tbpref'] == 0) ^ ($con['tbpref'] == ''))
60                         {
61                                 display_error(_("You cannot have table set without prefix together with prefixed sets in the same database."));
62                                 return false;
63                         }
64                 }
65         }
66         return true;
67 }
68
69 //---------------------------------------------------------------------------------------------
70
71 function remove_connection($id) {
72         global $db_connections;
73
74         $err = db_drop_db($db_connections[$id]);
75
76         unset($db_connections[$id]);
77         $conn = array_values($db_connections);
78         $db_connections = $conn;
79         //$$db_connections = array_values($db_connections);
80     return $err;
81 }
82 //---------------------------------------------------------------------------------------------
83
84 function handle_submit()
85 {
86         global $db_connections, $def_coy, $tb_pref_counter, $db,
87             $comp_subdirs, $path_to_root, $selected_id;
88
89         $error = false;
90         if (!check_data())
91                 return false;
92
93         if ($selected_id==-1)
94                 $selected_id = count($db_connections);
95
96         $new = !isset($db_connections[$selected_id]);
97
98         $db_connections[$selected_id]['name'] = $_POST['name'];
99         $db_connections[$selected_id]['host'] = $_POST['host'];
100         $db_connections[$selected_id]['dbuser'] = $_POST['dbuser'];
101         $db_connections[$selected_id]['dbpassword'] = $_POST['dbpassword'];
102         $db_connections[$selected_id]['dbname'] = $_POST['dbname'];
103         if (is_numeric($_POST['tbpref']))
104         {
105                 $db_connections[$selected_id]['tbpref'] = $_POST['tbpref'] == 1 ?
106                   $tb_pref_counter."_" : '';
107         }
108         else if ($_POST['tbpref'] != "")
109                 $db_connections[$selected_id]['tbpref'] = $_POST['tbpref'];
110         else
111                 $db_connections[$selected_id]['tbpref'] = "";
112
113         if ((bool)$_POST['def'] == true)
114                 $def_coy = $selected_id;
115
116         $conn = $db_connections[$selected_id];
117         if (($db = db_create_db($conn)) == 0)
118         {
119                 display_error(_("Error creating Database: ") . $conn['dbname'] . _(", Please create it manually"));
120                 $error = true;
121         } else {
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.'));
124                         $error = true;
125                 } else
126                         if (isset($_POST['admpassword']) && $_POST['admpassword'] != "")
127                                 update_admin_password($conn, md5($_POST['admpassword']));
128         }
129         set_global_connection();
130         if ($error) {
131                 remove_connection($selected_id);
132                 return false;
133         }
134         $error = write_config_db($new);
135         if ($error == -1)
136                 display_error(_("Cannot open the configuration file - ") . $path_to_root . "/config_db.php");
137         else if ($error == -2)
138                 display_error(_("Cannot write to the configuration file - ") . $path_to_root . "/config_db.php");
139         else if ($error == -3)
140                 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."));
141         if ($error != 0)
142         {
143                 return false;
144         }
145
146         if ($new)
147         {
148                 create_comp_dirs(company_path($selected_id), $comp_subdirs);
149         }
150         $exts = get_company_extensions();
151         write_extensions($exts, $selected_id);
152         display_notification($new ? _('New company has been created.') : _('Company has been updated.'));
153         return true;
154 }
155
156 //---------------------------------------------------------------------------------------------
157
158 function handle_delete()
159 {
160         global $def_coy, $db_connections, $comp_subdirs, $path_to_root;
161
162         $id = $_GET['id'];
163
164         // First make sure all company directories from the one under removal are writable. 
165         // Without this after operation we end up with changed per-company owners!
166         for($i = $id; $i < count($db_connections); $i++) {
167                         $comp_path = company_path($i);
168                 if (!is_dir($comp_path) || !is_writable($comp_path)) {
169                         display_error(_('Broken company subdirectories system. You have to remove this company manually.'));
170                         return;
171                 }
172         }
173         // make sure config file is writable
174         if (!is_writeable($path_to_root . "/config_db.php"))
175         {
176                 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."));
177                 return;
178         }
179         // rename directory to temporary name to ensure all
180         // other subdirectories will have right owners even after
181         // unsuccessfull removal.
182         $cdir = company_path($id);
183         $tmpname  = company_path('/old_'.$id);
184         if (!@rename($cdir, $tmpname)) {
185                 display_error(_('Cannot rename subdirectory to temporary name.'));
186                 return;
187         }
188         // 'shift' company directories names
189         for ($i = $id+1; $i < count($db_connections); $i++) {
190                 if (!rename(company_path($i), company_path($i-1))) {
191                         display_error(_("Cannot rename company subdirectory"));
192                         return;
193                 }
194         }
195         $err = remove_connection($id);
196         if ($err == 0)
197                 display_error(_("Error removing Database: ") . $dbase . _(", please remove it manually"));
198
199         if ($def_coy == $id)
200                 $def_coy = 0;
201         $error = write_config_db();
202         if ($error == -1)
203                 display_error(_("Cannot open the configuration file - ") . $path_to_root . "/config_db.php");
204         else if ($error == -2)
205                 display_error(_("Cannot write to the configuration file - ") . $path_to_root . "/config_db.php");
206         else if ($error == -3)
207                 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."));
208         if ($error != 0) {
209                 @rename($tmpname, $cdir);
210                 return;
211         }
212         // finally remove renamed company directory
213         @flush_dir($tmpname, true);
214         if (!@rmdir($tmpname))
215         {
216                 display_error(_("Cannot remove temporary renamed company data directory ") . $tmpname);
217                 return;
218         }
219         display_notification(_("Selected company has been deleted"));
220 }
221
222 //---------------------------------------------------------------------------------------------
223
224 function display_companies()
225 {
226         global $def_coy, $db_connections;
227
228         $coyno = $_SESSION["wa_current_user"]->company;
229
230         echo "
231                 <script language='javascript'>
232                 function deleteCompany(id) {
233                         if (!confirm('" . _("Are you sure you want to delete company no. ") . "'+id))
234                                 return
235                         document.location.replace('create_coy.php?c=df&id='+id)
236                 }
237                 </script>";
238         start_table(TABLESTYLE);
239
240         $th = array(_("Company"), _("Database Host"), _("Database User"),
241                 _("Database Name"), _("Table Pref"), _("Default"), "", "");
242         table_header($th);
243
244         $k=0;
245         $conn = $db_connections;
246         $n = count($conn);
247         for ($i = 0; $i < $n; $i++)
248         {
249                 if ($i == $def_coy)
250                         $what = _("Yes");
251                 else
252                         $what = _("No");
253                 if ($i == $coyno)
254                 start_row("class='stockmankobg'");
255         else
256                 alt_table_row_color($k);
257
258                 label_cell($conn[$i]['name']);
259                 label_cell($conn[$i]['host']);
260                 label_cell($conn[$i]['dbuser']);
261                 label_cell($conn[$i]['dbname']);
262                 label_cell($conn[$i]['tbpref']);
263                 label_cell($what);
264                 $edit = _("Edit");
265                 $delete = _("Delete");
266                 if (user_graphic_links())
267                 {
268                         $edit = set_icon(ICON_EDIT, $edit);
269                         $delete = set_icon(ICON_DELETE, $delete);
270                 }
271         label_cell("<a href='" . $_SERVER['PHP_SELF']. "?selected_id=$i'>$edit</a>");
272                 label_cell( $i == $coyno ? '' :
273                         "<a href='javascript:deleteCompany(" . $i . ")'>$delete</a>");
274                 end_row();
275         }
276
277         end_table();
278     display_note(_("The marked company is the current company which cannot be deleted."), 0, 0, "class='currentfg'");
279 }
280
281 //---------------------------------------------------------------------------------------------
282
283 function display_company_edit($selected_id)
284 {
285         global $def_coy, $db_connections, $tb_pref_counter;
286
287         if ($selected_id != -1)
288                 $n = $selected_id;
289         else
290                 $n = count($db_connections);
291
292         start_form();
293
294         start_table(TABLESTYLE2);
295
296         if ($selected_id != -1)
297         {
298                 $conn = $db_connections[$selected_id];
299                 $_POST['name'] = $conn['name'];
300                 $_POST['host']  = $conn['host'];
301                 $_POST['dbuser']  = $conn['dbuser'];
302                 $_POST['dbpassword']  = $conn['dbpassword'];
303                 $_POST['dbname']  = $conn['dbname'];
304                 $_POST['tbpref']  = $conn['tbpref'];
305                 if ($selected_id == $def_coy)
306                         $_POST['def'] = true;
307                 else
308                         $_POST['def'] = false;
309                 $_POST['dbcreate']  = false;
310                 hidden('selected_id', $selected_id);
311                 hidden('tbpref', $_POST['tbpref']);
312                 hidden('dbpassword', $_POST['dbpassword']);
313         }
314         else
315         {
316                 $_POST['tbpref'] = $tb_pref_counter."_";
317                 // Insert the current settings as default
318                 $conn = $db_connections[user_company()];
319                 $_POST['host']  = $conn['host'];
320                 $_POST['dbuser']  = $conn['dbuser'];
321                 $_POST['dbpassword']  = $conn['dbpassword'];
322                 $_POST['dbname']  = $conn['dbname'];
323         }
324         text_row_ex(_("Company"), 'name', 30);
325         text_row_ex(_("Host"), 'host', 30, 60);
326         text_row_ex(_("Database User"), 'dbuser', 30);
327         if ($selected_id == -1)
328                 text_row_ex(_("Database Password"), 'dbpassword', 30);
329         text_row_ex(_("Database Name"), 'dbname', 30);
330         if ($selected_id == -1)
331                 yesno_list_row(_("Table Pref"), 'tbpref', 1, $_POST['tbpref'], _("None"), false);
332         else
333                 label_row(_("Table Pref"), $_POST['tbpref']);
334         yesno_list_row(_("Default"), 'def', null, "", "", false);
335
336         coa_list_row(_("Database Script"), 'coa');
337
338         text_row_ex(_("New script Admin Password"), 'admpassword', 20);
339         end_table(1);
340
341         submit_center('save', _("Save"));
342 //      echo "<center><input type='button' style='width:150px' value='". _("Save"). "'></center>";
343
344
345         end_form();
346 }
347
348
349 //---------------------------------------------------------------------------------------------
350
351 if (isset($_GET['c']) && $_GET['c'] == 'df') {
352         handle_delete();
353         $selected_id = -1;
354 }
355
356 if (get_post('save')) {
357         if (handle_submit())
358                 $selected_id = -1;
359 }
360
361 //---------------------------------------------------------------------------------------------
362
363 display_companies();
364
365 hyperlink_no_params($_SERVER['PHP_SELF'], _("Create a new company"));
366
367 display_company_edit($selected_id);
368
369 //---------------------------------------------------------------------------------------------
370 end_page();
371
372 ?>