Merged bugfixes upto [0000072] (version 2.0.3).
[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         $index = "<?php\nheader(\"Location: ../../index.php\");\n?>";
141
142         if ($new)
143         {
144             $cdir = $comp_path.'/'.$id;
145             @mkdir($cdir);
146             save_to_file($cdir.'/'.'index.php', 0, $index);
147
148             foreach($comp_subdirs as $dir)
149             {
150                         @mkdir($cdir.'/'.$dir);
151                         save_to_file($cdir.'/'.$dir.'/'.'index.php', 0, $index);
152             }
153         }
154         return true;
155 }
156
157 //---------------------------------------------------------------------------------------------
158
159 function handle_delete()
160 {
161         global $comp_path, $def_coy, $db_connections, $comp_subdirs;
162
163         $id = $_GET['id'];
164
165         $cdir = $comp_path.'/'.$id;
166         @flush_dir($cdir);
167         if (!rmdir($cdir))
168         {
169                 display_error(_("Cannot remove company data directory ") . $cdir);
170                 return;
171         }
172         for($i = $id+1; $i < count($db_connections); $i++) {
173                 if (!rename($comp_path.'/'.$i, $comp_path.'/'.($i-1))) {
174                         display_error(_("Cannot rename company subdirectory"));
175                         return;
176                 }       
177         }
178         $err = remove_connection($id);
179         if ($err == 0)
180                 display_error(_("Error removing Database: ") . $dbase . _(", please remove it manually"));
181
182         if ($def_coy == $id)
183                 $def_coy = 0;
184         $error = write_config_db();
185         if ($error == -1)
186                 display_error(_("Cannot open the configuration file - ") . $path_to_root . "/config_db.php");
187         else if ($error == -2)
188                 display_error(_("Cannot write to the configuration file - ") . $path_to_root . "/config_db.php");
189         else if ($error == -3)
190                 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."));
191         if ($error != 0)
192                 return;
193
194         meta_forward($_SERVER['PHP_SELF']);
195 }
196
197 //---------------------------------------------------------------------------------------------
198
199 function display_companies()
200 {
201         global $table_style, $def_coy, $db_connections;
202
203         $coyno = $_SESSION["wa_current_user"]->company;
204
205         echo "
206                 <script language='javascript'>
207                 function deleteCompany(id) {
208                         if (!confirm('" . _("Are you sure you want to delete company no. ") . "'+id))
209                                 return
210                         document.location.replace('create_coy.php?c=df&id='+id)
211                 }
212                 </script>";
213         start_table($table_style);
214
215         $th = array(_("Company"), _("Database Host"), _("Database User"),
216                 _("Database Name"), _("Table Pref"), _("Default"), "", "");
217         table_header($th);
218
219         $k=0;
220         $conn = $db_connections;
221         $n = count($conn);
222         for ($i = 0; $i < $n; $i++)
223         {
224                 if ($i == $def_coy)
225                         $what = _("Yes");
226                 else
227                         $what = _("No");
228                 if ($i == $coyno)
229                 start_row("class='stockmankobg'");
230         else
231                 alt_table_row_color($k);
232
233                 label_cell($conn[$i]['name']);
234                 label_cell($conn[$i]['host']);
235                 label_cell($conn[$i]['dbuser']);
236                 label_cell($conn[$i]['dbname']);
237                 label_cell($conn[$i]['tbpref']);
238                 label_cell($what);
239                 label_cell("<a href=" . $_SERVER['PHP_SELF']. "?selected_id=" . $i . ">" . _("Edit") . "</a>");
240                 label_cell( $i == $coyno ? '' :
241         "<a href='javascript:deleteCompany(" . $i . ")'>" . _("Delete") . "</a>");
242                 end_row();
243         }
244
245         end_table();
246     display_note(_("The marked company is the current company which cannot be deleted."), 0, 0, "class='currentfg'");
247 }
248
249 //---------------------------------------------------------------------------------------------
250
251 function display_company_edit($selected_id)
252 {
253         global $def_coy, $db_connections, $tb_pref_counter, $table_style2;
254
255         if ($selected_id != -1)
256                 $n = $selected_id;
257         else
258                 $n = count($db_connections);
259
260         start_form(true, true);
261
262         echo "
263                 <script language='javascript'>
264                 function updateCompany() {
265                         if (document.forms[0].uploadfile.value!='' && document.forms[0].dbname.value!='') {
266                                 document.forms[0].action='create_coy.php?c=u&ul=1&id=" . $n . "&fn=' + document.forms[0].uploadfile.value
267                         }
268                         else {
269                                 document.forms[0].action='create_coy.php?c=u&id=" . $n . "&fn=' + document.forms[0].uploadfile.value
270                         }
271                         document.forms[0].submit()
272                 }
273                 </script>";
274
275         start_table($table_style2);
276
277         if ($selected_id != -1)
278         {
279                 $conn = $db_connections[$selected_id];
280                 $_POST['name'] = $conn['name'];
281                 $_POST['host']  = $conn['host'];
282                 $_POST['dbuser']  = $conn['dbuser'];
283                 $_POST['dbpassword']  = $conn['dbpassword'];
284                 $_POST['dbname']  = $conn['dbname'];
285                 $_POST['tbpref']  = $conn['tbpref'];
286                 if ($selected_id == $def_coy)
287                         $_POST['def'] = true;
288                 else
289                         $_POST['def'] = false;
290                 $_POST['dbcreate']  = false;
291                 hidden('selected_id', $selected_id);
292                 hidden('tbpref', $_POST['tbpref']);
293                 hidden('dbpassword', $_POST['dbpassword']);
294         }
295         else
296                 $_POST['tbpref'] = $tb_pref_counter."_";
297         text_row_ex(_("Company"), 'name', 30);
298         text_row_ex(_("Host"), 'host', 30);
299         text_row_ex(_("Database User"), 'dbuser', 30);
300         if ($selected_id == -1)
301                 text_row_ex(_("Database Password"), 'dbpassword', 30);
302         text_row_ex(_("Database Name"), 'dbname', 30);
303         if ($selected_id == -1)
304                 yesno_list_row(_("Table Pref"), 'tbpref', 1, $_POST['tbpref'], _("None"), false);
305         else
306                 label_row(_("Table Pref"), $_POST['tbpref']);
307         yesno_list_row(_("Default"), 'def', null, "", "", false);
308
309         start_row();
310         label_cell(_("Database Script"));
311         label_cell("<input name='uploadfile' type='file'>");
312         end_row();
313
314         text_row_ex(_("New script Admin Password"), 'admpassword', 20);
315
316         end_table();
317         display_note(_("Choose from Database scripts in SQL folder. No Database is created without a script."), 0, 1);
318         echo "<center><input onclick='javascript:updateCompany()' type='button' style='width:150px' value='". _("Save"). "'></center>";
319
320
321         end_form();
322 }
323
324
325 //---------------------------------------------------------------------------------------------
326
327 if (isset($_GET['c']) && $_GET['c'] == 'df')
328 {
329
330         handle_delete();
331 }
332
333 if (isset($_GET['c']) && $_GET['c'] == 'u')
334 {
335         if (handle_submit())
336         {
337                 meta_forward($_SERVER['PHP_SELF']);
338         }
339 }
340
341
342 //---------------------------------------------------------------------------------------------
343
344 display_companies();
345
346 hyperlink_no_params($_SERVER['PHP_SELF'], _("Create a new company"));
347
348 display_company_edit($selected_id);
349
350 //---------------------------------------------------------------------------------------------
351 end_page();
352
353 ?>