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