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';
14 include_once($path_to_root . "/includes/session.inc");
15 include_once($path_to_root."/includes/packages.inc");
17 if ($use_popup_windows) {
18 $js = get_js_open_window(900, 500);
20 page(_($help_context = "Install/Activate extensions"));
22 include_once($path_to_root . "/includes/date_functions.inc");
23 include_once($path_to_root . "/admin/db/company_db.inc");
24 include_once($path_to_root . "/admin/db/maintenance_db.inc");
25 include_once($path_to_root . "/includes/ui.inc");
27 simple_page_mode(true);
28 //---------------------------------------------------------------------------------------------
29 // Check third-party extension parameters
31 function check_data($id, $exts)
33 if ($_POST['name'] == "") {
34 display_error(_("Extension name cannot be empty."));
37 foreach($exts as $n =>$ext) {
38 if ($_POST['name'] == $ext['name'] && $id != $n) {
39 display_error(_("Extension name have to be unique."));
44 if ($_POST['title'] == "") {
45 display_error(_("Extension title cannot be empty."));
48 if ($_POST['path'] == "") {
49 display_error(_("Extension folder name cannot be empty."));
52 if ($id == -1 && !is_uploaded_file($_FILES['uploadfile']['tmp_name'])) {
53 display_error(_("You have to select extension file to upload"));
59 //---------------------------------------------------------------------------------------------
61 function handle_submit()
63 global $path_to_root, $db_connections, $selected_id, $next_extension_id;
65 $extensions = get_company_extensions();
66 if (!check_data($selected_id, $extensions))
68 $id = $selected_id==-1 ? $next_extension_id : $selected_id;
70 if ($selected_id != -1 && $extensions[$id]['type'] != 'extension'
71 || (isset($extensions[$id]['tabs']) && count($extensions[$id]['tabs']))) {
72 display_error(_('Module installation support is not implemented.'));
76 $extensions[$id]['name'] = $_POST['name'];
77 $extensions[$id]['package'] = '';
78 $extensions[$id]['version'] = '';
79 $extensions[$id]['active'] = check_value('active');
80 $entry = $selected_id == -1 ? array() : $extensions[$id]['entries'][0];
82 $entry['tab_id'] = $_POST['tab'];
83 $entry['title'] = $_POST['title'];
84 $entry['section'] = 2; // menu section aka module
86 // Only simple plugin type extensions can be installed manually.
87 $extensions[$id]['type'] = 'extension';
88 $directory = $path_to_root . "/modules/" . $_POST['path'];
89 if (!file_exists($directory))
93 if (is_uploaded_file($_FILES['uploadfile']['tmp_name']))
95 $entry['url'] = '/modules/'.$_POST['path'].'/'.$_FILES['uploadfile']['name'];
96 $file1 = $_FILES['uploadfile']['tmp_name'];
97 $file2 = $directory . "/".$_FILES['uploadfile']['name'];
98 if (file_exists($file2))
100 move_uploaded_file($file1, $file2);
103 $entry['url'] = '/modules/'.$_POST['path'].'/'.get_post('filename');
105 if (is_uploaded_file($_FILES['uploadfile2']['tmp_name']))
107 $file1 = $_FILES['uploadfile2']['tmp_name'];
108 $db_name = $_SESSION["wa_current_user"]->company;
109 db_import($file1, $db_connections[$db_name]);
112 if (is_uploaded_file($_FILES['uploadfile3']['tmp_name']))
114 $extensions[$id]['acc_file'] = '/modules/'.$_POST['path'].'/'.$_FILES['uploadfile3']['name'];
115 $file1 = $_FILES['uploadfile3']['tmp_name'];
116 $file2 = $directory . "/".$_FILES['uploadfile3']['name'];
117 if (file_exists($file2))
119 move_uploaded_file($file1, $file2);
122 $extensions[$id]['acc_file'] = '/modules/'.$_POST['path'].'/'.get_post('acc_file');
124 // security area guess for plugins
125 $exttext = file_get_contents($path_to_root.$entry['url']);
127 if (preg_match('/.*\$page_security\s*=\s*[\'"]([^\'"]*)/', $exttext, $match)) {
128 $area = trim($match[1]);
130 $entry['access'] = $area;
132 $extensions[$id]['entries'] = array($entry);
134 if ($selected_id == -1)
136 $next_extension_id++;
138 if (!update_extensions($extensions))
143 function handle_delete($id)
145 global $path_to_root;
147 $extensions = get_company_extensions();
149 if ($extensions[$id]['package'] != '') {
150 if (!uninstall_package($extensions[$id]['package']))
154 $dirname = dirname(@$extensions[$id]['entries'][0]['url']);
156 $dirname = $path_to_root.$dirname;
157 flush_dir($dirname, true);
161 unset($extensions[$id]);
162 if (update_extensions($extensions)) {
163 display_notification(_("Selected extension has been successfully deleted"));
168 // Helper for formating menu tabs/entries to be displayed in extension table
170 function fmt_titles($defs)
172 if (!$defs) return '';
173 foreach($defs as $def) {
174 $str[] = access_string($def['title'], true);
176 return implode('<br>', array_values($str));
178 //---------------------------------------------------------------------------------------------
180 // Display list of all extensions - installed and available from repository
182 function display_extensions()
185 div_start('ext_tbl');
186 start_table(TABLESTYLE);
188 $th = array(_("Extension"),_("Modules provided"), _("Options provided"),
189 _("Installed"), _("Available"), "", "");
193 $mods = get_extensions_list('extension');
195 foreach($mods as $pkg_name => $ext)
197 $available = @$ext['available'];
198 $installed = @$ext['version'];
199 $id = @$ext['local_id'];
200 $is_mod = $ext['type'] == 'module';
202 $entries = fmt_titles(@$ext['entries']);
203 $tabs = fmt_titles(@$ext['tabs']);
205 alt_table_row_color($k);
206 // label_cell(is_array($ext['Descr']) ? $ext['Descr'][0] : $ext['Descr']);
207 label_cell($available ? get_package_view_str($pkg_name, $ext['name']) : $ext['name']);
209 label_cell($entries);
211 label_cell($id === null ? _("None") :
212 ($available && $installed ? $installed : _("Unknown")));
213 label_cell($available ? $available : _("None"));
215 if (!$available && $ext['type'] == 'extension' && !count($ext['tabs'])) // third-party plugin
216 button_cell('Edit'.$id, _("Edit"), _('Edit third-party extension parameters.'),
218 elseif (check_pkg_upgrade($installed, $available)) // outdated or not installed extension in repo
219 button_cell('Update'.$pkg_name, $installed ? _("Update") : _("Install"),
220 _('Upload and install latest extension package'), ICON_DOWN);
225 delete_button_cell('Delete'.$id, _('Delete'));
226 submit_js_confirm('Delete'.$id,
227 sprintf(_("You are about to remove package \'%s\'.\nDo you want to continue ?"),
237 submit_center_first('Refresh', _("Update"), '', null);
238 submit_center_last('Add', _("Add third-party extension"), '', false);
242 //---------------------------------------------------------------------------------
244 // Get all installed extensions and display
245 // with current status stored in company directory.
247 function company_extensions($id)
249 start_table(TABLESTYLE);
251 $th = array(_("Extension"),_("Modules provided"), _("Options provided"), _("Active"));
253 $mods = get_company_extensions();
254 $exts = get_company_extensions($id);
255 foreach($mods as $key => $ins) {
256 foreach($exts as $ext)
257 if ($ext['name'] == $ins['name']) {
258 $mods[$key]['active'] = @$ext['active'];
262 $mods = array_natsort($mods, null, 'name');
265 foreach($mods as $i => $mod)
267 if ($mod['type'] != 'extension') continue;
268 alt_table_row_color($k);
269 label_cell($mod['name']);
270 $entries = fmt_titles(@$mod['entries']);
271 $tabs = fmt_titles(@$mod['tabs']);
273 label_cell($entries);
275 check_cells(null, 'Active'.$i, @$mod['active'] ? 1:0,
276 false, false, "align='center'");
281 submit_center('Refresh', _('Update'), true, false, 'default');
284 //---------------------------------------------------------------------------------------------
286 // Third-party plugin installation
288 function display_ext_edit($selected_id)
292 $extensions = get_company_extensions();
294 start_table(TABLESTYLE2);
296 if ($selected_id != -1 && $extensions[$selected_id]['type'] == 'extension')
298 if ($Mode == 'Edit') {
299 $mod = $extensions[$selected_id];
300 $entry = $mod['entries'][0];
302 $_POST['name'] = $mod['name'];
303 $_POST['tab'] = $entry['tab_id'];
304 $_POST['title'] = $entry['title'];
305 $_POST['path'] = substr(dirname($entry['url']), 9); //strip '/modules/'
306 $_POST['filename'] = basename($entry['url']);
307 $_POST['acc_file'] = @$mod['acc_file'] ? basename($mod['acc_file']) : null;
308 hidden('filename', $_POST['filename']);
309 hidden('acc_file', $_POST['acc_file']);
311 hidden('selected_id', $selected_id);
313 text_row_ex(_("Name"), 'name', 30);
314 text_row_ex(_("Subfolder (in modules directory)"), 'path', 20);
316 tab_list_row(_("Menu Tab"), 'tab', null, true);
317 text_row_ex(_("Menu Link Text"), 'title', 30);
319 record_status_list_row(_("Default status"), 'active');
321 file_row(_("Extension File"), 'uploadfile');
322 file_row(_("Access Levels File"), 'uploadfile3');
323 file_row(_("SQL File"), 'uploadfile2');
326 display_note(_("Select your extension PHP files from your local harddisk."), 0, 1);
328 submit_add_or_update($selected_id == -1, '', 'both');
329 submit('RESET', _('Cancel'), true, '', 'cancel');
333 //---------------------------------------------------------------------------------------------
334 if ($Mode=='ADD_ITEM' || $Mode == 'UPDATE_ITEM') {
335 if(handle_submit()) {
336 if ($selected_id != -1)
337 display_notification(_("Extension data has been updated."));
339 display_notification(_("Extension has been installed."));
343 if ($Mode == 'Delete')
345 handle_delete($selected_id);
348 if (get_post('Refresh')) {
349 $exts = get_company_extensions(get_post('extset')); //
350 $comp = get_post('extset');
352 foreach($exts as $i => $ext) {
353 if ($ext['package'] && ($ext['active'] ^ check_value('Active'.$i))) {
354 $pkg = new package($ext['package'].'-'.$ext['version'].'.pkg');
355 $pkg->support(check_value('Active'.$i) ? 'activate':'deactivate', $comp);
357 $exts[$i]['active'] = check_value('Active'.$i);
359 write_extensions($exts, get_post('extset'));
360 if (get_post('extset') == user_company())
361 $installed_extensions = $exts;
362 display_notification(_('Current active extensions set has been saved.'));
365 if ($id = find_submit('Update', false))
366 install_extension($id);
368 if ($Mode == 'RESET')
374 //---------------------------------------------------------------------------------------------
376 if (list_updated('extset'))
377 $Ajax->activate('_page_body');
379 $set = get_post('extset', -1);
381 if (isset($_GET['popup']) || get_post('Add') || $Mode == 'Edit'
382 || $Mode == 'ADD_ITEM' || $Mode == 'UPDATE_ITEM')
384 display_ext_edit($selected_id);
387 echo "<center>" . _('Extensions:') . " ";
388 echo extset_list('extset', null, true);
389 echo "</center><br>";
392 display_extensions();
394 company_extensions($set);
397 //---------------------------------------------------------------------------------------------