Fixed company subdirs creation during upgrade and company addition.
[fa-stable.git] / admin / create_coy.php
1 <?php
2
3 $page_security = 20;
4 $path_to_root="..";
5 include_once($path_to_root . "/includes/session.inc");
6
7 include_once($path_to_root . "/includes/date_functions.inc");
8 include_once($path_to_root . "/admin/db/company_db.inc");
9 include_once($path_to_root . "/admin/db/maintenance_db.inc");
10 include_once($path_to_root . "/includes/ui.inc");
11
12 page(_("Create/Update Company"));
13
14 $comp_subdirs = array('images', 'pdf_files', 'backup','js_cache', 'reporting');
15
16 //---------------------------------------------------------------------------------------------
17 if (isset($_GET['selected_id']))
18 {
19         $selected_id = $_GET['selected_id'];
20 }
21 elseif (isset($_POST['selected_id']))
22 {
23         $selected_id = $_POST['selected_id'];
24 }
25 else
26         $selected_id = -1;
27
28 //---------------------------------------------------------------------------------------------
29
30 function check_data()
31 {
32         global $db_connections, $tb_pref_counter, $selected_id;
33
34         if ($_POST['name'] == "" || $_POST['host'] == "" || $_POST['dbuser'] == "" || $_POST['dbname'] == "")
35                 return false;
36         foreach($db_connections as $id=>$con)
37         {
38          if($id != $selected_id && $_POST['host'] == $con['host'] 
39                 && $_POST['dbname'] == $con['dbname'])
40                 {
41                         if ($_POST['tbpref'] == $con['tbpref'])
42                         {
43                                 display_error(_("This database settings are already used by another company."));
44                                 return false;
45                         }
46                         if ($_POST['tbpref'] == 0 || $con['tbpref'] == '')
47                         {
48                                 display_error(_("You cannot have table set without prefix together with prefixed sets in the same database."));
49                                 return false;
50                         }
51                 }
52         }
53         return true;
54 }
55
56 //---------------------------------------------------------------------------------------------
57
58 function remove_connection($id) {
59         global $db_connections;
60
61         $err = db_drop_db($db_connections[$id]);
62
63         unset($db_connections[$id]);
64         $conn = array_values($db_connections);
65         $db_connections = $conn;
66         //$$db_connections = array_values($db_connections);
67     return $err;
68 }
69 //---------------------------------------------------------------------------------------------
70
71 function handle_submit()
72 {
73         global $db_connections, $def_coy, $tb_pref_counter, $db,
74             $comp_path, $comp_subdirs;
75
76         $new = false;
77
78         if (!check_data())
79                 return false;
80
81         $id = $_GET['id'];
82
83         $db_connections[$id]['name'] = $_POST['name'];
84         $db_connections[$id]['host'] = $_POST['host'];
85         $db_connections[$id]['dbuser'] = $_POST['dbuser'];
86         $db_connections[$id]['dbpassword'] = $_POST['dbpassword'];
87         $db_connections[$id]['dbname'] = $_POST['dbname'];
88         if (isset($_GET['ul']) && $_GET['ul'] == 1)
89         {
90                 if (is_numeric($_POST['tbpref']))
91                 {
92                         $db_connections[$id]['tbpref'] = $_POST['tbpref'] == 1 ?
93                           $tb_pref_counter."_" : '';
94                         $new = true;
95                 }
96                 else if ($_POST['tbpref'] != "")
97                         $db_connections[$id]['tbpref'] = $_POST['tbpref'];
98                 else
99                         $db_connections[$id]['tbpref'] = "";
100         }
101         if ((bool)$_POST['def'] == true)
102                 $def_coy = $id;
103         if (isset($_GET['ul']) && $_GET['ul'] == 1)
104         {
105                 $conn = $db_connections[$id];
106                 if (($db = db_create_db($conn)) == 0)
107                 {
108                         display_error(_("Error creating Database: ") . $conn['dbname'] . _(", Please create it manually"));
109                         remove_connection($id);
110                         set_global_connection();
111                         return false;
112                 }
113
114                 $filename = $_FILES['uploadfile']['tmp_name'];
115                 if (is_uploaded_file ($filename))
116                 {
117                         db_import($filename, $conn, $id);
118                         if (isset($_POST['admpassword']) && $_POST['admpassword'] != "")
119                                 db_query("UPDATE ".$conn['tbpref']."users set password = '".md5($_POST['admpassword']). "' WHERE user_id = 'admin'");
120                 }
121                 else
122                 {
123                         display_error(_("Error uploading Database Script, please upload it manually"));
124                         set_global_connection();
125                         return false;
126                 }
127                 set_global_connection();
128         }
129         $error = write_config_db($new);
130         if ($error == -1)
131                 display_error(_("Cannot open the configuration file - ") . $path_to_root . "/config_db.php");
132         else if ($error == -2)
133                 display_error(_("Cannot write to the configuration file - ") . $path_to_root . "/config_db.php");
134         else if ($error == -3)
135                 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."));
136         if ($error != 0)
137         {
138                 return false;
139         }
140
141         if ($new)
142         {
143                 create_comp_dirs("$comp_path/$id", $comp_subdirs);
144         }
145         return true;
146 }
147
148 //---------------------------------------------------------------------------------------------
149
150 function handle_delete()
151 {
152         global $comp_path, $def_coy, $db_connections, $comp_subdirs;
153
154         $id = $_GET['id'];
155
156         $cdir = $comp_path.'/'.$id;
157         @flush_dir($cdir);
158         if (!rmdir($cdir))
159         {
160                 display_error(_("Cannot remove company data directory ") . $cdir);
161                 return;
162         }
163         for($i = $id+1; $i < count($db_connections); $i++) {
164                 if (!rename($comp_path.'/'.$i, $comp_path.'/'.($i-1))) {
165                         display_error(_("Cannot rename company subdirectory"));
166                         return;
167                 }       
168         }
169         $err = remove_connection($id);
170         if ($err == 0)
171                 display_error(_("Error removing Database: ") . $dbase . _(", please remove it manually"));
172
173         if ($def_coy == $id)
174                 $def_coy = 0;
175         $error = write_config_db();
176         if ($error == -1)
177                 display_error(_("Cannot open the configuration file - ") . $path_to_root . "/config_db.php");
178         else if ($error == -2)
179                 display_error(_("Cannot write to the configuration file - ") . $path_to_root . "/config_db.php");
180         else if ($error == -3)
181                 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."));
182         if ($error != 0)
183                 return;
184
185         meta_forward($_SERVER['PHP_SELF']);
186 }
187
188 //---------------------------------------------------------------------------------------------
189
190 function display_companies()
191 {
192         global $table_style, $def_coy, $db_connections;
193
194         $coyno = $_SESSION["wa_current_user"]->company;
195
196         echo "
197                 <script language='javascript'>
198                 function deleteCompany(id) {
199                         if (!confirm('" . _("Are you sure you want to delete company no. ") . "'+id))
200                                 return
201                         document.location.replace('create_coy.php?c=df&id='+id)
202                 }
203                 </script>";
204         start_table($table_style);
205
206         $th = array(_("Company"), _("Database Host"), _("Database User"),
207                 _("Database Name"), _("Table Pref"), _("Default"), "", "");
208         table_header($th);
209
210         $k=0;
211         $conn = $db_connections;
212         $n = count($conn);
213         for ($i = 0; $i < $n; $i++)
214         {
215                 if ($i == $def_coy)
216                         $what = _("Yes");
217                 else
218                         $what = _("No");
219                 if ($i == $coyno)
220                 start_row("class='stockmankobg'");
221         else
222                 alt_table_row_color($k);
223
224                 label_cell($conn[$i]['name']);
225                 label_cell($conn[$i]['host']);
226                 label_cell($conn[$i]['dbuser']);
227                 label_cell($conn[$i]['dbname']);
228                 label_cell($conn[$i]['tbpref']);
229                 label_cell($what);
230                 label_cell("<a href=" . $_SERVER['PHP_SELF']. "?selected_id=" . $i . ">" . _("Edit") . "</a>");
231                 label_cell( $i == $coyno ? '' :
232         "<a href='javascript:deleteCompany(" . $i . ")'>" . _("Delete") . "</a>");
233                 end_row();
234         }
235
236         end_table();
237     display_note(_("The marked company is the current company which cannot be deleted."), 0, 0, "class='currentfg'");
238 }
239
240 //---------------------------------------------------------------------------------------------
241
242 function display_company_edit($selected_id)
243 {
244         global $def_coy, $db_connections, $tb_pref_counter, $table_style2;
245
246         if ($selected_id != -1)
247                 $n = $selected_id;
248         else
249                 $n = count($db_connections);
250
251         start_form(true, true);
252
253         echo "
254                 <script language='javascript'>
255                 function updateCompany() {
256                         if (document.forms[0].uploadfile.value!='' && document.forms[0].dbname.value!='') {
257                                 document.forms[0].action='create_coy.php?c=u&ul=1&id=" . $n . "&fn=' + document.forms[0].uploadfile.value
258                         }
259                         else {
260                                 document.forms[0].action='create_coy.php?c=u&id=" . $n . "&fn=' + document.forms[0].uploadfile.value
261                         }
262                         document.forms[0].submit()
263                 }
264                 </script>";
265
266         start_table($table_style2);
267
268         if ($selected_id != -1)
269         {
270                 $conn = $db_connections[$selected_id];
271                 $_POST['name'] = $conn['name'];
272                 $_POST['host']  = $conn['host'];
273                 $_POST['dbuser']  = $conn['dbuser'];
274                 $_POST['dbpassword']  = $conn['dbpassword'];
275                 $_POST['dbname']  = $conn['dbname'];
276                 $_POST['tbpref']  = $conn['tbpref'];
277                 if ($selected_id == $def_coy)
278                         $_POST['def'] = true;
279                 else
280                         $_POST['def'] = false;
281                 $_POST['dbcreate']  = false;
282                 hidden('selected_id', $selected_id);
283                 hidden('tbpref', $_POST['tbpref']);
284                 hidden('dbpassword', $_POST['dbpassword']);
285         }
286         else
287                 $_POST['tbpref'] = $tb_pref_counter."_";
288         text_row_ex(_("Company"), 'name', 30);
289         text_row_ex(_("Host"), 'host', 30);
290         text_row_ex(_("Database User"), 'dbuser', 30);
291         if ($selected_id == -1)
292                 text_row_ex(_("Database Password"), 'dbpassword', 30);
293         text_row_ex(_("Database Name"), 'dbname', 30);
294         if ($selected_id == -1)
295                 yesno_list_row(_("Table Pref"), 'tbpref', 1, $_POST['tbpref'], _("None"), false);
296         else
297                 label_row(_("Table Pref"), $_POST['tbpref']);
298         yesno_list_row(_("Default"), 'def', null, "", "", false);
299
300         start_row();
301         label_cell(_("Database Script"));
302         label_cell("<input name='uploadfile' type='file'>");
303         end_row();
304
305         text_row_ex(_("New script Admin Password"), 'admpassword', 20);
306
307         end_table();
308         display_note(_("Choose from Database scripts in SQL folder. No Database is created without a script."), 0, 1);
309         echo "<center><input onclick='javascript:updateCompany()' type='button' style='width:150px' value='". _("Save"). "'></center>";
310
311
312         end_form();
313 }
314
315
316 //---------------------------------------------------------------------------------------------
317
318 if (isset($_GET['c']) && $_GET['c'] == 'df')
319 {
320
321         handle_delete();
322 }
323
324 if (isset($_GET['c']) && $_GET['c'] == 'u')
325 {
326         if (handle_submit())
327         {
328                 meta_forward($_SERVER['PHP_SELF']);
329         }
330 }
331
332
333 //---------------------------------------------------------------------------------------------
334
335 display_companies();
336
337 hyperlink_no_params($_SERVER['PHP_SELF'], _("Create a new company"));
338
339 display_company_edit($selected_id);
340
341 //---------------------------------------------------------------------------------------------
342 end_page();
343
344 ?>