Files
cacti/cli/plugin_manage.php
Thomas Vincent 2b6e96b23a style(php): [develop] wrap CLI and poller calls without changing execution (#8303)
* style(php): wrap CLI and poller calls without changing execution

Signed-off-by: Thomas Vincent <thomasvincent@gmail.com>

* fix(ci): review security inventories after poller wrapping

Signed-off-by: Thomas Vincent <thomasvincent@gmail.com>

* test(boost): compare handoff source contracts by PHP tokens

Signed-off-by: Thomas Vincent <thomasvincent@gmail.com>

* style(php): wrap poller constant strings without changing values

Signed-off-by: Thomas Vincent <thomasvincent@gmail.com>

* style(php): wrap poller concatenations with token-preserving source contracts

Signed-off-by: Thomas Vincent <thomasvincent@gmail.com>

* test(cli): assert CDEF repair queries after constant folding

Signed-off-by: Thomas Vincent <thomasvincent@gmail.com>

* test: verify PHP formatting preserves parsed behavior

Signed-off-by: Thomas Vincent <thomasvincent@gmail.com>

---------

Signed-off-by: Thomas Vincent <thomasvincent@gmail.com>
2026-10-14 00:01:26 -07:00

292 lines
8.1 KiB
PHP
Executable File

#!/usr/bin/env php
<?php
/*
+-------------------------------------------------------------------------+
| Copyright (C) 2004-2026 The Cacti Group |
| |
| 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; either version 2 |
| of the License, or (at your option) any later version. |
| |
| 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. |
+-------------------------------------------------------------------------+
| Cacti: The Complete RRDtool-based Graphing Solution |
+-------------------------------------------------------------------------+
| This code is designed, written, and maintained by the Cacti Group. See |
| about.php and/or the AUTHORS file for specific developer information. |
+-------------------------------------------------------------------------+
| http://www.cacti.net/ |
+-------------------------------------------------------------------------+
*/
require(__DIR__ . '/../include/cli_check.php');
$parms = $_SERVER['argv'];
$install = false;
$enable = false;
$disable = false;
$uninstall = false;
$allperms = false;
$plugins = [];
if (cacti_sizeof($parms)) {
$shortopts = 'VvHh';
$longopts = [
'plugin::',
'install',
'enable',
'disable',
'uninstall',
'allperms',
'version',
'help'
];
$options = getopt($shortopts, $longopts);
foreach ($options as $arg => $value) {
switch($arg) {
case 'plugin':
if (is_array($value)) {
$plugins = $value;
} else {
$plugins[] = $value;
}
break;
case 'install':
$install = true;
break;
case 'uninstall':
$uninstall = true;
break;
case 'disable':
$disable = true;
break;
case 'enable':
$enable = true;
break;
case 'allperms':
$allperms = true;
break;
case 'version':
case 'V':
case 'v':
display_version();
exit(0);
case 'help':
case 'H':
case 'h':
display_help();
exit(0);
default:
print "ERROR: Invalid Argument: ($arg)" . PHP_EOL . PHP_EOL;
display_help();
exit(1);
}
}
// Sanity checks
if ($enable && $disable) {
print 'FATAL: Options --enable and --disable are mutually exclusive' . PHP_EOL;
exit(1);
}
if ($install && $uninstall) {
print 'FATAL: Options --install and --uninstall are mutually exclusive' . PHP_EOL;
exit(1);
}
if ($install && $disable) {
print 'FATAL: Options --install and --disable are mutually exclusive' . PHP_EOL;
exit(1);
}
if ($uninstall && $enable) {
print 'FATAL: Options --uninstall and --enable are mutually exclusive' . PHP_EOL;
exit(1);
}
} else {
display_help();
exit(1);
}
print 'NOTE: ' . cacti_sizeof($plugins) . ' Plugins to be acted on.' . PHP_EOL;
$success = true;
if (cacti_sizeof($plugins)) {
foreach ($plugins as $plugin) {
print "NOTE: Plugin '$plugin' processing started" . PHP_EOL;
if ($install) {
$installed = false;
if (is_dir(CACTI_PATH_PLUGINS . '/' . $plugin)) {
print "NOTE: Plugin directory for Plugin $plugin exists" . PHP_EOL;
if (!api_plugin_installed($plugin)) {
$message = '';
if (api_plugin_can_install($plugin, $message)) {
$result = api_plugin_install($plugin);
$status = db_fetch_cell_prepared('SELECT status FROM plugin_config WHERE directory = ?', [$plugin]);
if ($result !== false && $status == 4) {
print "NOTE: Plugin $plugin installed successfully." . PHP_EOL;
$installed = true;
if ($enable) {
api_plugin_enable($plugin);
if (db_fetch_cell_prepared('SELECT status FROM plugin_config WHERE directory = ?', [$plugin]) == 1) {
print "NOTE: Plugin $plugin enabled." . PHP_EOL;
} else {
$success = false;
$installed = false;
print "ERROR: Plugin '$plugin' could not be enabled. Review the plugin settings and Cacti log." . PHP_EOL;
}
}
} else {
$success = false;
if ($result !== false && $status == 2) {
print "ERROR: Plugin '$plugin' needs configuration. Review the plugin settings and Cacti log before enabling it." . PHP_EOL;
} else {
print "ERROR: Plugin '$plugin' installation failed. Review the plugin setup and Cacti log before enabling it." . PHP_EOL;
}
}
} else {
$success = false;
print "WARNING: Plugin '$plugin' can not install. Message is: $message" . PHP_EOL;
}
} else {
$status = db_fetch_cell_prepared('SELECT status FROM plugin_config WHERE directory = ?', [$plugin]);
if ($status == 2) {
$success = false;
print "ERROR: Plugin '$plugin' needs configuration. Review the plugin setup and Cacti log before enabling it." . PHP_EOL;
} else {
$installed = true;
print "WARNING: Plugin '$plugin' already installed." . PHP_EOL;
}
}
} else {
$success = false;
print "WARNING: Plugin '$plugin' missing plugin directory. Plugin not installed" . PHP_EOL;
}
if ($installed && $allperms) {
if (!plugin_manage_install_allrealms($plugin)) {
$success = false;
}
}
} elseif ($uninstall || $disable || $enable) {
if ($disable) {
print "NOTE: Disabling Plugin $plugin." . PHP_EOL;
api_plugin_disable($plugin);
}
if ($uninstall) {
print "NOTE: Uninstalling Plugin $plugin." . PHP_EOL;
api_plugin_uninstall($plugin);
}
if ($enable) {
print "NOTE: Enabling Plugin $plugin." . PHP_EOL;
api_plugin_enable($plugin);
}
}
}
}
exit($success ? 0 : 1);
function plugin_manage_install_allrealms(string $plugin) : bool {
print "NOTE: Enabling Plugin '$plugin' permissions for administrative accounts" . PHP_EOL;
$realms = db_fetch_assoc_prepared('SELECT id
FROM plugin_realms
WHERE plugin = ?',
[$plugin]);
if (!cacti_sizeof($realms)) {
return true;
}
$user_ids = [(int) read_config_option('admin_user')];
if (!empty($_SESSION['sess_user_id'])) {
$user_ids[] = (int) $_SESSION['sess_user_id'];
}
foreach (array_unique($user_ids) as $user_id) {
if ($user_id <= 0 || !db_fetch_cell_prepared('SELECT id FROM user_auth WHERE id = ?', [$user_id])) {
print "ERROR: Plugin '$plugin' permissions could not be granted: administrative account not found." . PHP_EOL;
return false;
}
foreach ($realms as $realm) {
if (!db_execute_prepared('REPLACE INTO user_auth_realm (user_id, realm_id) VALUES (?, ?)', [$user_id, $realm['id'] + 100])) {
print "ERROR: Plugin '$plugin' permissions could not be granted. Review the Cacti log." . PHP_EOL;
return false;
}
}
}
return true;
}
/**
* display_version - displays version information
*
* @return void
*/
function display_version() : void {
$version = get_cacti_cli_version();
print "Cacti Install Utility, Version $version, " . COPYRIGHT_YEARS . PHP_EOL;
}
/**
* display_help - displays the usage of the function
*
* @return void
*/
function display_help() : void {
print ('usage: plugin_manage.php [--plugin=S] [--install --enable --allperms] ' .
'[--uninstall ] [--disable]') . PHP_EOL . PHP_EOL;
print 'A utility to install/uninstall a Cacti plugin or plugins' . PHP_EOL . PHP_EOL;
print 'Required:' . PHP_EOL;
print ' --plugin=S - The plugin to install. Use the option multiple time for more plugins' . PHP_EOL . PHP_EOL;
print 'Optional:' . PHP_EOL;
print ' --install - Install the plugin or plugins' . PHP_EOL;
print ' --enable - Enable the plugin or plugins' . PHP_EOL;
print ' --allperms - Enable all permission for plugin or plugins' . PHP_EOL;
print ' --uninstall - Uninstall the plugin or plugins' . PHP_EOL;
print ' --disable - Disable the plugin or plugins' . PHP_EOL;
}