pandorafms/pandora_console/include/functions_plugins.php

130 lines
3.5 KiB
PHP
Raw Normal View History

2021-06-16 18:18:38 +02:00
<?php
/**
* Aux functions to manage plugins.
*
* @category library
* @package Pandora FMS
* @subpackage Plugins
* @version 1.0.0
* @license See below
*
* ______ ___ _______ _______ ________
2023-06-08 12:42:10 +02:00
* | __ \.-----.--.--.--| |.-----.----.-----. | ___| | | __|
* | __/| _ | | _ || _ | _| _ | | ___| |__ |
2021-06-16 18:18:38 +02:00
* |___| |___._|__|__|_____||_____|__| |___._| |___| |__|_|__|_______|
*
* ============================================================================
2023-06-08 11:53:13 +02:00
* Copyright (c) 2005-2023 Pandora FMS
2023-06-08 13:19:01 +02:00
* Please see https://pandorafms.com/community/ for full contribution list
2021-06-16 18:18:38 +02:00
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation for version 2.
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* ============================================================================
*/
use PandoraFMS\Enterprise\Metaconsole\Synchronizer;
// Begin.
global $config;
/**
* Remove all modules associated to this plugin.
*
* @param integer $id_plugin Plugin id.
*
* @return array Of erros if any.
*/
function plugins_remove_modules(int $id_plugin)
{
// Delete all resources related with this plugin.
$plugin_modules = db_get_all_rows_filter(
'tagente_modulo',
[ 'id_plugin' => $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;
}
// Remove all components related to this plugin.
db_process_sql_delete(
'tnetwork_component',
[ 'id_plugin' => $id_plugin ]
);
2021-06-16 18:18:38 +02:00
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;
}