Rewritten extensions system to enable per company module/plugin
[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;
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         unset($extensions[$id]);
171         $mods = array_values($extensions);
172         $extensions = $mods;
173
174         if (!write_extensions($extensions))
175                 return;
176         
177         // should we also delete module form per company extension files?
178         
179         meta_forward($_SERVER['PHP_SELF']);
180 }
181
182 //---------------------------------------------------------------------------------------------
183
184 function display_extensions()
185 {
186         global $table_style, $tabs;
187
188         echo "
189                 <script language='javascript'>
190                 function deleteExtension(id, name) {
191                         if (!confirm('" . _("Are you sure you want to delete extension: ") . "'+name))
192                                 return
193                         document.location.replace('inst_module.php?c=df&id='+id)
194                 }
195                 </script>";
196         start_table($table_style);
197         $th = array(_("Name"),_("Tab"), _("Link text"), _("Folder"), _("Filename"), 
198                 _("Access extensions"),"", "");
199         table_header($th);
200
201         $k = 0;
202         $mods = get_company_extensions();
203         $n = count($mods);
204         for ($i = 0; $i < $n; $i++)
205         {
206                 $is_mod = $mods[$i]['type'] == 'module';
207                 alt_table_row_color($k);
208                 label_cell($mods[$i]['name']);
209                 label_cell( $is_mod ? $mods[$i]['title'] : $tabs[$mods[$i]['tab']]);
210                 $ttl = access_string($mods[$i]['title']);
211                 label_cell($ttl[0]);
212                 label_cell($mods[$i]['path']);
213                 label_cell($mods[$i]['filename']);
214                 label_cell(@$mods[$i]['acc_file']);
215                 $edit = _("Edit");
216                 $delete = _("Delete");
217                 if ($is_mod)
218                 {
219                         label_cell(''); // not implemented (yet)
220                         label_cell('');
221                 }
222                 else
223                 {
224                         if (user_graphic_links())
225                         {
226                                 $edit = set_icon(ICON_EDIT, $edit);
227                                 $delete = set_icon(ICON_DELETE, $delete);
228                         }
229                 label_cell("<a href='" . $_SERVER['PHP_SELF']. "?selected_id=$i'>$edit</a>");
230                         label_cell("<a href='javascript:deleteExtension(".$i.", \"" . $mods[$i]['name'] . "\")'>$delete</a>");
231                 }
232                 end_row();
233         }
234
235         end_table();
236 }
237
238 function company_extensions($id)
239 {
240         global $table_style, $tabs;
241
242         start_table($table_style);
243         
244         $th = array(_("Name"),_("Tab"), _("Link text"), _("Active"));
245         
246         // get all available extensions and display
247         // with current status stored in company directory.
248
249         $mods = get_company_extensions();
250         $exts = get_company_extensions($id);
251         foreach($mods as $key => $ins) {
252                 foreach($exts as $ext)
253                         if ($ext['name'] == $ins['name']) {
254                                 $mods[$key]['active'] = @$ext['active'];
255                                 continue 2;
256                         }
257         }
258         
259         table_header($th);
260         $k = 0;
261         $n = count($mods);
262         for ($i = 0; $i < $n; $i++)
263         {
264                 alt_table_row_color($k);
265                 label_cell($mods[$i]['name']);
266                 label_cell($mods[$i]['type'] == 'module' ? $mods[$i]['title'] : $tabs[$mods[$i]['tab']]);
267                 $ttl = access_string($mods[$i]['title']);
268                 label_cell($ttl[0]);
269                 check_cells(null, 'Active'.$i, @$mods[$i]['active'] ? 1:0, 
270                         false, false, "align='center'");
271                 end_row();
272         }
273
274         end_table(1);
275         submit_center('Update', _('Update'), true, false, 'default');
276 }
277
278 //---------------------------------------------------------------------------------------------
279
280 function display_ext_edit($selected_id)
281 {
282         global $table_style2;
283
284         $extensions = get_company_extensions();
285         if ($selected_id != -1)
286                 $n = $selected_id;
287         else
288                 $n = count($extensions);
289
290
291         echo "
292                 <script language='javascript'>
293                 function updateModule() {
294                         document.forms[0].action='inst_module.php?c=u&id=" . $n . "'
295                         document.forms[0].submit()
296                 }
297                 </script>";
298
299         start_table($table_style2);
300
301         if ($selected_id != -1 && $extensions[$selected_id]['type'] == 'plugin')
302         {
303                 $mod = $extensions[$selected_id];
304                 $_POST['tab']  = $mod['tab'];
305                 $_POST['name'] = $mod['name'];
306                 $_POST['title'] = $mod['title'];
307                 $_POST['path'] = $mod['path'];
308                 $_POST['filename'] = $mod['filename'];
309                 $_POST['acc_file'] = @$mod['acc_file'];
310                 hidden('selected_id', $selected_id);
311                 hidden('filename', $_POST['filename']);
312                 hidden('acc_file', $_POST['acc_file']);
313         }
314         text_row_ex(_("Name"), 'name', 30);
315         text_row_ex(_("Folder"), 'path', 20);
316
317         tab_list_row(_("Menu Tab"), 'tab', null);
318         text_row_ex(_("Menu Link Text"), 'title', 30);
319         record_status_list_row(_("Default status"), 'active');
320
321         label_row(_("Module File"), "<input name='uploadfile' type='file'>");
322         label_row(_("Access Levels Extensions"), "<input name='uploadfile3' type='file'>");
323         label_row(_("SQL File"), "<input name='uploadfile2' type='file'>");
324
325         end_table(0);
326         display_note(_("Select your module PHP file from your local harddisk."), 0, 1);
327
328         echo "<center><input onclick='javascript:updateModule()' type='button' style='width:150px' value='". _("Save"). "'></center>";
329
330 }
331
332 //---------------------------------------------------------------------------------------------
333 if (get_post('Update')) {
334         $exts = get_company_extensions();
335         for($i = 0; $i < count($exts); $i++) {
336                 $exts[$i]['active'] = check_value('Active'.$i);
337         }
338         write_extensions($exts, get_post('extset'));
339         if (get_post('extset') == user_company())
340                 $installed_extensions = $exts;
341         display_notification(_('Current active extensions set has been saved.'));
342 }
343 elseif (isset($_GET['c']))
344 {
345         if ($_GET['c'] == 'df')
346         {
347                 handle_delete();
348         }
349
350         if ($_GET['c'] == 'u')
351         {
352                 if (handle_submit())
353                 {
354                         if ($selected_id != -1)
355                                 display_notification(_("Extension data has been updated."));
356                         else
357                                 display_notification(_("Extension has been installed."));
358                 }
359         }
360 }
361
362 //---------------------------------------------------------------------------------------------
363 start_form(true);
364 if (list_updated('extset'))
365         $Ajax->activate('_page_body');
366
367 echo "<center>" . _('Extensions:') . "&nbsp;&nbsp;";
368 extset_list('extset', null, true);
369 echo "</center><br>";
370
371 $set = get_post('extset');
372
373 if ($set == -1) {
374         display_extensions();
375
376         hyperlink_no_params($_SERVER['PHP_SELF'], _("Add new extension"));
377
378         display_ext_edit($selected_id);
379 } else {
380         company_extensions($set);
381 }
382 //---------------------------------------------------------------------------------------------
383 end_form();
384
385 end_page();
386
387 ?>