From c98b7f544bfce57c4b69aae3fb81bce27213a841 Mon Sep 17 00:00:00 2001 From: Rom1-B <8530352+Rom1-B@users.noreply.github.com> Date: Fri, 13 Nov 2026 10:48:33 +0100 Subject: [PATCH] feat: make plugin CSP compliant --- setup.php | 1 + src/Config.php | 11 ++++++----- src/Source_Zone.php | 12 +++++++++++- templates/config.html.twig | 12 ++++++++---- templates/environmentalimpact-item.html.twig | 16 ++++++++-------- 5 files changed, 34 insertions(+), 18 deletions(-) diff --git a/setup.php b/setup.php index 9505e77f..e90dc20a 100644 --- a/setup.php +++ b/setup.php @@ -196,6 +196,7 @@ function plugin_version_carbon() 'author' => 'Teclib\'', 'license' => 'GPLv3', 'homepage' => '', + 'csp_compliant' => true, 'requirements' => [ 'glpi' => [ 'min' => PLUGIN_CARBON_MIN_GLPI_VERSION, diff --git a/src/Config.php b/src/Config.php index 043657e8..63bd1a98 100644 --- a/src/Config.php +++ b/src/Config.php @@ -45,7 +45,6 @@ use GlpiPlugin\Carbon\DataSource\Lca\ClientFactory as LcaClientFactory; use GlpiPlugin\Carbon\Impact\Embodied\Engine; use GuzzleHttp\Client; -use Html; use Monitor as GlpiMonitor; use NetworkEquipment as GlpiNetworkEquipment; use Override; @@ -132,8 +131,8 @@ public function showForm($ID, $options = []) '_glpi_csrf_token' => Session::getNewCSRFToken(), 'reset_all' => '', ]); - $usage_impact_action_url = 'submitGetLink("' . $CFG_GLPI['root_doc'] . '/plugins/carbon/front/usageimpact.form.php", ' . $reset_args . ')'; - $embodied_impact_action_url = 'submitGetLink("' . $CFG_GLPI['root_doc'] . '/plugins/carbon/front/embodiedimpact.form.php", ' . $reset_args . ')'; + $usage_impact_action_url = $CFG_GLPI['root_doc'] . '/plugins/carbon/front/usageimpact.form.php'; + $embodied_impact_action_url = $CFG_GLPI['root_doc'] . '/plugins/carbon/front/embodiedimpact.form.php'; $renderer = TemplateRenderer::getInstance(); $environment = $renderer->getEnvironment(); @@ -147,8 +146,10 @@ public function showForm($ID, $options = []) 'impact_engines' => Engine::getAvailableBackends(), 'include_configs' => $include_configs, 'action' => (isset($options['plugin_config']) ? Config::getFormURL() : GlpiConfig::getFormURL()), - 'usage_impact_action_url' => Html::getConfirmationOnActionScript($confirm_message, $usage_impact_action_url), - 'embodied_impact_action_url' => Html::getConfirmationOnActionScript($confirm_message, $embodied_impact_action_url), + 'confirm_message' => $confirm_message, + 'reset_args' => $reset_args, + 'usage_impact_action_url' => $usage_impact_action_url, + 'embodied_impact_action_url' => $embodied_impact_action_url, ]); return true; diff --git a/src/Source_Zone.php b/src/Source_Zone.php index a3d7fd07..dbe20eba 100644 --- a/src/Source_Zone.php +++ b/src/Source_Zone.php @@ -211,6 +211,11 @@ public static function showForSource(CommonDBTM $item) reloadTab(); }); }; + document.querySelectorAll("[data-carbon-toggle-zone]").forEach(function (button) { + button.addEventListener("click", function () { + plugin_carbon_toggleZone(button.dataset.carbonToggleZone); + }); + }); '); } } @@ -314,6 +319,11 @@ public static function showForZone(CommonDBTM $item) reloadTab(); }); }; + document.querySelectorAll("[data-carbon-toggle-zone]").forEach(function (button) { + button.addEventListener("click", function () { + plugin_carbon_toggleZone(button.dataset.carbonToggleZone); + }); + }); '); } } @@ -428,7 +438,7 @@ public function getFallbackFromDB(Source_Zone $source_zone): bool protected static function getToggleLink(int $zone_id, ?string $state): string { $state = $state == 0 ? __('No') : __('Yes'); - $link = '' . $state . ''; + $link = ''; return $link; } diff --git a/templates/config.html.twig b/templates/config.html.twig index 3f688a71..bb5124a7 100644 --- a/templates/config.html.twig +++ b/templates/config.html.twig @@ -79,8 +79,10 @@ {{ __('Reset usage impacts. This will delete all calculated usage impacts for all assets.', 'carbon') }} - {% set reset_args = "{_glpi_csrf_token: '" ~ csrf_token() ~ "', reset_all: ''}" %} - + {{ _x('button', 'Reset usage impacts') }} @@ -89,8 +91,10 @@ {{ __('Reset embodied impacts. This will delete all calculated embodied impacts for all assets.', 'carbon') }} - {% set reset_args = "{_glpi_csrf_token: '" ~ csrf_token() ~ "', reset_all: ''}" %} - + {{ _x('button', 'Reset embodied impacts') }} diff --git a/templates/environmentalimpact-item.html.twig b/templates/environmentalimpact-item.html.twig index a499c1ec..c04db988 100644 --- a/templates/environmentalimpact-item.html.twig +++ b/templates/environmentalimpact-item.html.twig @@ -61,11 +61,11 @@
{% if not usage_impact.isNewItem() or usage_carbon_emission_count > 0 %} - {% set reset_args = "{_glpi_csrf_token: '" ~ csrf_token() ~ "', reset: '', itemtype: '" ~ asset.getType() ~ "', items_id: '" ~ asset.getID() ~ "'}" %} - {{ __('Reset data', 'carbon') }} + {% set reset_args = {_glpi_csrf_token: csrf_token(), reset: '', itemtype: asset.getType(), items_id: asset.getID()}|json_encode %} + {{ __('Reset data', 'carbon') }} {% endif %} - {% set calculate_args = "{_glpi_csrf_token: '" ~ csrf_token() ~ "', calculate: '', itemtype: '" ~ asset.getType() ~ "', items_id: '" ~ asset.getID() ~ "'}" %} - {{ __('Calculate data', 'carbon') }} + {% set calculate_args = {_glpi_csrf_token: csrf_token(), calculate: '', itemtype: asset.getType(), items_id: asset.getID()}|json_encode %} + {{ __('Calculate data', 'carbon') }}
{{ fields.largeTitle( @@ -96,10 +96,10 @@
{% if not embodied_impact.isNewItem() %} - {% set reset_args = "{_glpi_csrf_token: '" ~ csrf_token() ~ "', reset: '', id: '" ~ embodied_impact.getID() ~ "'}" %} - {{ __('Reset data', 'carbon') }} + {% set reset_args = {_glpi_csrf_token: csrf_token(), reset: '', id: embodied_impact.getID()}|json_encode %} + {{ __('Reset data', 'carbon') }} {% endif %} - {% set calculate_args = "{_glpi_csrf_token: '" ~ csrf_token() ~ "', calculate: '', itemtype: '" ~ asset.getType() ~ "', items_id: '" ~ asset.getID() ~ "'}" %} - {{ __('Calculate data', 'carbon') }} + {% set calculate_args = {_glpi_csrf_token: csrf_token(), calculate: '', itemtype: asset.getType(), items_id: asset.getID()}|json_encode %} + {{ __('Calculate data', 'carbon') }}