Additional extension system related fixes.
[fa-stable.git] / admin / inst_module.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_CREATEMODULES';
13 $path_to_root="..";
14 include_once($path_to_root . "/includes/session.inc");
15
16 page(_("Install/Activate extensions"));
17
18 include_once($path_to_root . "/includes/date_functions.inc");
19 include_once($path_to_root . "/admin/db/company_db.inc");
20 include_once($path_to_root . "/admin/db/maintenance_db.inc");
21 include_once($path_to_root . "/includes/ui.inc");
22
23 //---------------------------------------------------------------------------------------------
24
25 if (isset($_GET['selected_id']))
26 {
27         $selected_id = $_GET['selected_id'];
28 }
29 elseif (isset($_POST['selected_id']))
30 {
31         $selected_id = $_POST['selected_id'];
32 }
33 else
34         $selected_id = -1;
35
36 //---------------------------------------------------------------------------------------------
37 function get_company_extensions($id = -1) {
38
39         global $path_to_root;
40
41         $file = $path_to_root.($id == -1 ? '' : '/company/'.$id).'/installed_extensions.php';
42         $installed_extensions = array();
43         if (is_file($file)) {
44                 include($file);
45         }
46         return $installed_extensions;
47 }
48
49 function check_data($id, $exts)
50 {
51         if ($_POST['name'] == "") {
52                 display_error(_("Extension name cannot be empty."));
53                 return false;
54         }
55         foreach($exts as $n =>$ext) {
56                 if ($_POST['name'] == $ext['name'] && $id != $n) {
57                         display_error(_("Extension name have to be unique."));
58                         return false;
59                 }
60         }
61
62         if ($_POST['title'] == "") {
63                 display_error(_("Extension title cannot be empty."));
64                 return false;
65         }
66         if ($_POST['path'] == "") {
67                 display_error(_("Extension folder name cannot be empty."));
68                 return false;
69         }
70         if ($id == -1 && !is_uploaded_file($_FILES['uploadfile']['tmp_name'])) {
71                 display_error(_("You have to select plugin file to upload"));
72                 return false; 
73         }
74         return true;
75 }
76
77 //---------------------------------------------------------------------------------------------
78
79 function handle_submit()
80 {
81         global $path_to_root, $db_connections, $selected_id;
82
83         $extensions = get_company_extensions();
84         if (!check_data($selected_id, $extensions))
85                 return false;
86
87         $id = $_GET['id'];
88
89         $extensions[$id]['tab'] = $_POST['tab'];
90         $extensions[$id]['name'] = $_POST['name'];
91         $extensions[$id]['path'] = $_POST['path'];
92         $extensions[$id]['title'] = $_POST['title'];
93         $extensions[$id]['active'] = $_POST['active'];
94
95         // Currently we support only plugin extensions here.
96         $extensions[$id]['type'] = 'plugin';
97         $directory = $path_to_root . "/modules/" . $_POST['path'];
98         if (!file_exists($directory))
99         {
100                 mkdir($directory);
101         }
102         if (is_uploaded_file($_FILES['uploadfile']['tmp_name']))
103         {
104                 $extensions[$id]['filename'] = $_FILES['uploadfile']['name'];
105                 $file1 = $_FILES['uploadfile']['tmp_name'];
106                 $file2 = $directory . "/".$_FILES['uploadfile']['name'];
107                 if (file_exists($file2))
108                         unlink($file2);
109                 move_uploaded_file($file1, $file2);
110         }
111         else
112                 $extensions[$id]['filename'] = get_post('filename');
113         if (is_uploaded_file($_FILES['uploadfile2']['tmp_name']))
114         {
115                 $file1 = $_FILES['uploadfile2']['tmp_name'];
116                 $file2 = $directory . "/".$_FILES['uploadfile2']['name'];
117                 if (file_exists($file2))
118                         unlink($file2);
119                 move_uploaded_file($file1, $file2);
120                 $db_name = $_SESSION["wa_current_user"]->company;
121                 db_import($file2, $db_connections[$db_name]);
122         }
123         
124         if (is_uploaded_file($_FILES['uploadfile3']['tmp_name']))
125         {
126                 $extensions[$id]['acc_file'] = $_FILES['uploadfile3']['name'];
127                 $file1 = $_FILES['uploadfile3']['tmp_name'];
128                 $file2 = $directory . "/".$_FILES['uploadfile3']['name'];
129                 if (file_exists($file2))
130                         unlink($file2);
131                 move_uploaded_file($file1, $file2);
132         }
133         else
134                 $extensions[$id]['acc_file'] = get_post('acc_file');
135         
136         if (!write_extensions($extensions))
137                 return false;
138         return true;
139 }
140
141 //---------------------------------------------------------------------------------------------
142
143 function handle_delete()
144 {
145         global  $path_to_root, $db_connections;
146         
147         $extensions = get_company_extensions();
148
149         $id = $_GET['id'];
150
151         $path = $extensions[$id]['path'];
152
153         if ($extensions[$id]['type'] != 'plugin') {
154                 display_error(_('Module installation support is not implemented yet. You have to do it manually.'));
155                 return;
156         }
157         
158         $filename = "$path_to_root/modules/$path";
159         if ($h = opendir($filename))
160         {
161                 while (($file = readdir($h)) !== false)
162                 {
163                         if (is_file("$filename/$file"))
164                         unlink("$filename/$file");
165                 }
166                 closedir($h);
167         }
168         rmdir($filename);
169
170         $ident = $extensions[$id]['name'];
171         unset($extensions[$id]);
172         $mods = array_values($extensions);
173         $extensions = $mods;
174
175         if (!write_extensions($extensions))
176                 return;
177
178         // update per company files
179         $cnt = count($db_connections);
180         for($i = 0; $i < $cnt; $i++) 
181         {
182                 $exts = get_company_extensions($i);
183                 foreach($exts as $key => $ext) {
184                         if ($ext['name'] == $ident) {
185                                 unset($exts[$key]);
186                                 break;
187                         }
188                 }
189                 write_extensions($exts, $i);
190         }
191         meta_forward($_SERVER['PHP_SELF']);
192 }
193
194 //---------------------------------------------------------------------------------------------
195
196 function display_extensions()
197 {
198         global $table_style, $tabs;
199
200         echo "
201                 <script language='javascript'>
202                 function deleteExtension(id, name) {
203                         if (!confirm('" . _("Are you sure you want to delete extension: ") . "'+name))
204                                 return
205                         document.location.replace('inst_module.php?c=df&id='+id)
206                 }
207                 </script>";
208         start_table($table_style);
209         $th = array(_("Name"),_("Tab"), _("Link text"), _("Folder"), _("Filename"), 
210                 _("Access extensions"),"", "");
211         table_header($th);
212
213         $k = 0;
214         $mods = get_company_extensions();
215         $n = count($mods);
216         for ($i = 0; $i < $n; $i++)
217         {
218                 $is_mod = $mods[$i]['type'] == 'module';
219                 alt_table_row_color($k);
220                 label_cell($mods[$i]['name']);
221                 label_cell( $is_mod ? $mods[$i]['title'] : $tabs[$mods[$i]['tab']]);
222                 $ttl = access_string($mods[$i]['title']);
223                 label_cell($ttl[0]);
224                 label_cell($mods[$i]['path']);
225                 label_cell($mods[$i]['filename']);
226                 label_cell(@$mods[$i]['acc_file']);
227                 $edit = _("Edit");
228                 $delete = _("Delete");
229                 if ($is_mod)
230                 {
231                         label_cell(''); // not implemented (yet)
232                         label_cell('');
233                 }
234                 else
235                 {
236                         if (user_graphic_links())
237                         {
238                                 $edit = set_icon(ICON_EDIT, $edit);
239                                 $delete = set_icon(ICON_DELETE, $delete);
240                         }
241                 label_cell("<a href='" . $_SERVER['PHP_SELF']. "?selected_id=$i'>$edit</a>");
242                         label_cell("<a href='javascript:deleteExtension(".$i.", \"" . $mods[$i]['name'] . "\")'>$delete</a>");
243                 }
244                 end_row();
245         }
246
247         end_table();
248 }
249
250 function company_extensions($id)
251 {
252         global $table_style, $tabs;
253
254         start_table($table_style);
255         
256         $th = array(_("Name"),_("Tab"), _("Link text"), _("Active"));
257         
258         // get all available extensions and display
259         // with current status stored in company directory.
260
261         $mods = get_company_extensions();
262         $exts = get_company_extensions($id);
263         foreach($mods as $key => $ins) {
264                 foreach($exts as $ext)
265                         if ($ext['name'] == $ins['name']) {
266                                 $mods[$key]['active'] = @$ext['active'];
267                                 continue 2;
268                         }
269         }
270         
271         table_header($th);
272         $k = 0;
273         $n = count($mods);
274         for ($i = 0; $i < $n; $i++)
275         {
276                 alt_table_row_color($k);
277                 label_cell($mods[$i]['name']);
278                 label_cell($mods[$i]['type'] == 'module' ? $mods[$i]['title'] : $tabs[$mods[$i]['tab']]);
279                 $ttl = access_string($mods[$i]['title']);
280                 label_cell($ttl[0]);
281                 check_cells(null, 'Active'.$i, @$mods[$i]['active'] ? 1:0, 
282                         false, false, "align='center'");
283                 end_row();
284         }
285
286         end_table(1);
287         submit_center('Update', _('Update'), true, false, 'default');
288 }
289
290 //---------------------------------------------------------------------------------------------
291
292 function display_ext_edit($selected_id)
293 {
294         global $table_style2;
295
296         $extensions = get_company_extensions();
297         if ($selected_id != -1)
298                 $n = $selected_id;
299         else
300                 $n = count($extensions);
301
302
303         echo "
304                 <script language='javascript'>
305                 function updateModule() {
306                         document.forms[0].action='inst_module.php?c=u&id=" . $n . "'
307                         document.forms[0].submit()
308                 }
309                 </script>";
310
311         start_table($table_style2);
312
313         if ($selected_id != -1 && $extensions[$selected_id]['type'] == 'plugin')
314         {
315                 $mod = $extensions[$selected_id];
316                 $_POST['tab']  = $mod['tab'];
317                 $_POST['name'] = $mod['name'];
318                 $_POST['title'] = $mod['title'];
319                 $_POST['path'] = $mod['path'];
320                 $_POST['filename'] = $mod['filename'];
321                 $_POST['acc_file'] = @$mod['acc_file'];
322                 hidden('selected_id', $selected_id);
323                 hidden('filename', $_POST['filename']);
324                 hidden('acc_file', $_POST['acc_file']);
325         }
326         text_row_ex(_("Name"), 'name', 30);
327         text_row_ex(_("Folder"), 'path', 20);
328
329         tab_list_row(_("Menu Tab"), 'tab', null);
330         text_row_ex(_("Menu Link Text"), 'title', 30);
331         record_status_list_row(_("Default status"), 'active');
332
333         label_row(_("Module File"), "<input name='uploadfile' type='file'>");
334         label_row(_("Access Levels Extensions"), "<input name='uploadfile3' type='file'>");
335         label_row(_("SQL File"), "<input name='uploadfile2' type='file'>");
336
337         end_table(0);
338         display_note(_("Select your module PHP file from your local harddisk."), 0, 1);
339
340         echo "<center><input onclick='javascript:updateModule()' type='button' style='width:150px' value='". _("Save"). "'></center>";
341
342 }
343
344 //---------------------------------------------------------------------------------------------
345 if (get_post('Update')) {
346         $exts = get_company_extensions();
347         for($i = 0; $i < count($exts); $i++) {
348                 $exts[$i]['active'] = check_value('Active'.$i);
349         }
350         write_extensions($exts, get_post('extset'));
351         if (get_post('extset') == user_company())
352                 $installed_extensions = $exts;
353         display_notification(_('Current active extensions set has been saved.'));
354 }
355 elseif (isset($_GET['c']))
356 {
357         if ($_GET['c'] == 'df')
358         {
359                 handle_delete();
360         }
361
362         if ($_GET['c'] == 'u')
363         {
364                 if (handle_submit())
365                 {
366                         if ($selected_id != -1)
367                                 display_notification(_("Extension data has been updated."));
368                         else
369                                 display_notification(_("Extension has been installed."));
370                 }
371         }
372 }
373
374 //---------------------------------------------------------------------------------------------
375 start_form(true);
376 if (list_updated('extset'))
377         $Ajax->activate('_page_body');
378
379 echo "<center>" . _('Extensions:') . "&nbsp;&nbsp;";
380 extset_list('extset', null, true);
381 echo "</center><br>";
382
383 $set = get_post('extset');
384
385 if ($set == -1) {
386         display_extensions();
387
388         hyperlink_no_params($_SERVER['PHP_SELF'], _("Add new extension"));
389
390         display_ext_edit($selected_id);
391 } else {
392         company_extensions($set);
393 }
394 //---------------------------------------------------------------------------------------------
395 end_form();
396
397 end_page();
398
399 ?>