diff --git a/pandora_console/extras/delete_files/delete_files.txt b/pandora_console/extras/delete_files/delete_files.txt index 466cb2b571..10e49c14fd 100644 --- a/pandora_console/extras/delete_files/delete_files.txt +++ b/pandora_console/extras/delete_files/delete_files.txt @@ -99,4 +99,4 @@ include/javascript/update_manager.js enterprise/include/functions_update_manager.php include/ajax/rolling_release.ajax.php extensions/plugin_registration.php - +enterprise/include/functions_plugins.php diff --git a/pandora_console/godmode/servers/plugin.php b/pandora_console/godmode/servers/plugin.php index e519581838..e8d12e654c 100644 --- a/pandora_console/godmode/servers/plugin.php +++ b/pandora_console/godmode/servers/plugin.php @@ -1,19 +1,36 @@ $plugin_id] - ); - - if (empty($plugin_modules)) { - $plugin_modules = []; - } - - foreach ($plugin_modules as $pm) { - modules_delete_agent_module($pm['id_agente_modulo']); - } - - if (enterprise_installed()) { - enterprise_include_once('include/functions_policies.php'); - $policies_ids = db_get_all_rows_filter('tpolicy_modules', ['id_plugin' => $plugin_id]); - foreach ($policies_ids as $policies_id) { - policies_change_delete_pending_module($policies_id['id']); - } - } - - if (is_metaconsole()) { - enterprise_include_once('include/functions_plugins.php'); - $result = plugins_delete_plugin($plugin_id); - if (!$result) { - ui_print_error_message(__('Problem deleting plugin')); - } else { - ui_print_success_message(__('Plugin deleted successfully')); - } + if ((int) $plugin_id > 0) { + // Delete all iformation related with this plugin. + $result = plugins_delete_plugin($plugin_id); + if (empty($result) === false) { + ui_print_error_message( + implode('
', $result) + ); + } else { + ui_print_success_message(__('Plugin deleted successfully')); } } } diff --git a/pandora_console/godmode/servers/plugin_registration.php b/pandora_console/godmode/servers/plugin_registration.php index b273967d3b..8d70afee05 100644 --- a/pandora_console/godmode/servers/plugin_registration.php +++ b/pandora_console/godmode/servers/plugin_registration.php @@ -542,7 +542,10 @@ if (file_exists($uploaded_filename) === true) { if ($error !== null && $error !== '') { ui_print_error_message($error); -} else if (is_management_allowed() === true && is_metaconsole() === true) { +} else if ($error === null + && is_management_allowed() === true + && is_metaconsole() === true +) { $attachment = '/'.str_replace( $config['homedir'], '', diff --git a/pandora_console/include/functions_plugins.php b/pandora_console/include/functions_plugins.php new file mode 100644 index 0000000000..c2245cea16 --- /dev/null +++ b/pandora_console/include/functions_plugins.php @@ -0,0 +1,123 @@ + $id_plugin ], + 'id_agente_modulo' + ); + + $ret = []; + + foreach ($plugin_modules as $id) { + try { + $module = new PandoraFMS\Module($id); + $module->delete(); + } catch (Exception $e) { + $ret[] = __('Failed to erase module %d: %s', $id, $e->getMessage()); + } + } + + if (enterprise_installed() === true) { + enterprise_include_once('include/functions_policies.php'); + $policies_ids = db_get_all_rows_filter( + 'tpolicy_modules', + ['id_plugin' => $id_plugin], + 'id' + ); + + foreach ($policies_ids as $id) { + if (policies_change_delete_pending_module($id) !== true) { + $ret[] = __('Failed to erase policy module: %d', $id); + } + } + } + + return $ret; +} + + +/** + * Effectively remove a plugin from the system. + * + * @param integer $id_plugin Plugin id. + * + * @return array Of errors, empty if no errors. + */ +function plugins_delete_plugin(int $id_plugin) +{ + $result = []; + + $problem = plugins_remove_modules($id_plugin) !== true; + if (empty($problem) !== false) { + $result = $problem; + } + + if (is_metaconsole() === true && is_management_allowed() === true) { + $sc = new Synchronizer(); + $problems = $sc->apply( + function ($node) use ($id_plugin) { + $rt = []; + try { + $node->connect(); + + $rt = plugins_remove_modules($id_plugin); + + $node->disconnect(); + } catch (Exception $e) { + $rt[] = $e->getMessage(); + } + + return $rt; + }, + false + ); + + foreach ($problems as $prob) { + $result = array_merge($result, $prob); + } + } + + return $result; +}