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