diff --git a/CHANGELOG.md b/CHANGELOG.md index d63779fa..962c9000 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](http://semver.org/). ## [UNRELEASED] +### Changed + +- Make the plugin compatible with the Content Security Policy + ### Fixed - Remove redundant variable initialization in the question type AJAX controller diff --git a/inc/container.class.php b/inc/container.class.php index dd4c7559..e396b60d 100644 --- a/inc/container.class.php +++ b/inc/container.class.php @@ -1476,7 +1476,8 @@ public static function showFormItemtype($params = []) */ public static function showFormSubtype($params, $display = false) { - $out = ""; + $out = ''; + $tab_tr_display = 'hide'; if (isset($params['type']) && $params['type'] == 'domtab' && class_exists($params['itemtype'])) { $dbu = new DbUtils(); $item = $dbu->getItemForItemtype($params['itemtype']); @@ -1512,11 +1513,14 @@ public static function showFormSubtype($params, $display = false) 'display' => false, ], ); - $out .= ""; + $tab_tr_display = 'show'; } } } + $out .= sprintf("", $tab_tr_display); + $out .= Html::script('/plugins/fields/js/container_subtype.js', [], false); + if ($display === false) { return $out; } diff --git a/inc/field.class.php b/inc/field.class.php index 5b260692..b697fb80 100644 --- a/inc/field.class.php +++ b/inc/field.class.php @@ -690,17 +690,14 @@ public function showSummary($container) 'plugin_fields_containers_id' => $cID, 'id' => -1, ]; - echo Html::scriptBlock(' - viewAddField' . $cID . $rand . ' = function() { - $("#viewField' . $cID . $rand . '").load( - "' . $CFG_GLPI['root_doc'] . '/ajax/viewsubitem.php", - ' . json_encode($ajax_params) . ' - ); - }; - '); - echo "
" - . sprintf("", $cID, $rand); + . sprintf( + "", + $cID, + $rand, + htmlescape($CFG_GLPI['root_doc'] . '/ajax/viewsubitem.php'), + htmlescape(json_encode($ajax_params)), + ); echo __('Add a new field', 'fields') . '

'; if (count($iterator) === 0) { diff --git a/inc/labeltranslation.class.php b/inc/labeltranslation.class.php index f189f51f..31555888 100644 --- a/inc/labeltranslation.class.php +++ b/inc/labeltranslation.class.php @@ -196,17 +196,8 @@ public static function showTranslations(CommonDBTM $item) 'items_id' => $item->fields['id'], 'id' => -1, ]; - echo Html::scriptBlock(' - addTranslation' . $item->getID() . $rand . ' = function() { - $("#viewtranslation' . $item->getID() . $rand . '").load( - "' . $CFG_GLPI['root_doc'] . '/plugins/fields/ajax/viewtranslations.php", - ' . json_encode($ajax_params) . ' - ); - }; - '); - echo "
" - . "") + . "" . __('Add a new translation') . '

'; } @@ -238,8 +229,13 @@ public static function showTranslations(CommonDBTM $item) echo '' . __('Language', 'fields') . ''; echo '' . __('Label', 'fields') . ''; foreach ($found as $data) { - echo "'; + $row_params = htmlescape(json_encode([ + 'type' => self::class, + 'itemtype' => $item::getType(), + 'items_id' => $item->getID(), + 'id' => $data['id'], + ])); + echo "'; if ($canedit) { echo ""; Html::showMassiveActionCheckBox(self::class, $data['id']); @@ -247,6 +243,7 @@ public static function showTranslations(CommonDBTM $item) } echo ''; +<<<<<<< HEAD if ($canedit) { $ajax_params = [ 'type' => self::class, @@ -264,6 +261,8 @@ public static function showTranslations(CommonDBTM $item) '); } +======= +>>>>>>> 8a6ae60 (feat: make plugin CSP compliant (#1350)) echo Dropdown::getLanguageName($data['language']); echo ''; echo htmlspecialchars((string) $data['label'], ENT_QUOTES); diff --git a/public/js/container_subtype.js b/public/js/container_subtype.js new file mode 100644 index 00000000..d8efd272 --- /dev/null +++ b/public/js/container_subtype.js @@ -0,0 +1,37 @@ +/** + * ------------------------------------------------------------------------- + * Fields plugin for GLPI + * ------------------------------------------------------------------------- + * + * LICENSE + * + * This file is part of Fields. + * + * Fields 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. + * + * Fields 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. + * + * You should have received a copy of the GNU General Public License + * along with Fields. If not, see . + * ------------------------------------------------------------------------- + * @copyright Copyright (C) 2013-2023 by Fields plugin team. + * @license GPLv2 https://www.gnu.org/licenses/gpl-2.0.html + * @link https://github.com/pluginsGLPI/fields + * ------------------------------------------------------------------------- + */ + +// Loaded right after its marker, not on DOM ready: keep the original timing. +$('[data-fields-tab-tr]:not([data-fields-ready])').each(function () { + const marker = $(this).attr('data-fields-ready', ''); + if (marker.attr('data-fields-tab-tr') === 'show') { + $('#tab_tr').show(); + } else { + $('#tab_tr').hide(); + } +}); diff --git a/public/js/fields.js b/public/js/fields.js new file mode 100644 index 00000000..eb580bc9 --- /dev/null +++ b/public/js/fields.js @@ -0,0 +1,131 @@ +/** + * ------------------------------------------------------------------------- + * Fields plugin for GLPI + * ------------------------------------------------------------------------- + * + * LICENSE + * + * This file is part of Fields. + * + * Fields 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. + * + * Fields 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. + * + * You should have received a copy of the GNU General Public License + * along with Fields. If not, see . + * ------------------------------------------------------------------------- + * @copyright Copyright (C) 2013-2023 by Fields plugin team. + * @license GPLv2 https://www.gnu.org/licenses/gpl-2.0.html + * @link https://github.com/pluginsGLPI/fields + * ------------------------------------------------------------------------- + */ + +// Load a subitem form into a target element. Used by links and table rows. +$(document).on('click', '[data-fields-load-target]', function (e) { + if ($(this).is('a')) { + e.preventDefault(); + } + $($(this).attr('data-fields-load-target')).load( + $(this).attr('data-fields-load-url'), + JSON.parse($(this).attr('data-fields-load-params')) + ); +}); + +// Container display conditions list +$(document).on('click', '[data-fields-conditions] button[name="edit"]', function (e) { + const row = $(e.currentTarget).closest('tr'); + const id = row.find('input[name="id"]').val(); + const container_id = row.find('input[name="plugin_fields_containers_id"]').val(); + + glpi_ajax_dialog( + { + title: __('Edit condition to hide block', 'fields'), + url: CFG_GLPI.root_doc + '/plugins/fields/ajax/container_display_condition.php', + method: 'get', + params: { + action: 'get_edit_form', + id: id, + plugin_fields_containers_id: container_id + }, + dialogclass: 'modal-xl' + } + ); +}); + +$(document).on('click', '[data-fields-conditions] button[name="switch_add"]', function (e) { + const container_id = $(e.currentTarget).attr('data-container-id'); + + glpi_ajax_dialog( + { + title: __('Add condition to hide block', 'fields'), + url: CFG_GLPI.root_doc + '/plugins/fields/ajax/container_display_condition.php', + method: 'get', + params: { + action: 'get_add_form', + plugin_fields_containers_id: container_id + }, + dialogclass: 'modal-xl' + } + ); +}); + +// Status overrides list +$(document).on('click', '[data-fields-status-overrides] button[name="edit"]', function (e) { + const row = $(e.currentTarget).closest('tr'); + const id = row.find('input[name="id"]').val(); + const container_id = row.find('input[name="container_id"]').val(); + + glpi_ajax_dialog( + { + title: __('Edit status override', 'fields'), + url: CFG_GLPI.root_doc + '/plugins/fields/ajax/status_override.php', + method: 'get', + params: { + action: 'get_edit_form', + id: id, + container_id: container_id + }, + dialogclass: 'modal-xl' + } + ); +}); + +$(document).on('click', '[data-fields-status-overrides] button[name="switch_add"]', function (e) { + const container_id = $(e.currentTarget).attr('data-container-id'); + + glpi_ajax_dialog( + { + title: __('Add new status override', 'fields'), + url: CFG_GLPI.root_doc + '/plugins/fields/ajax/status_override.php', + method: 'get', + params: { + action: 'get_add_form', + container_id: container_id + }, + dialogclass: 'modal-xl' + } + ); +}); + +// Status override form: refresh the status list when the item type changes +$(document).on('change', 'form[data-fields-status-override] select[name="itemtype"]', function () { + const form = $(this).closest('form'); + const rand = form.attr('data-fields-status-override'); + + $.ajax({ + url: CFG_GLPI.root_doc + '/plugins/fields/ajax/status_override.php', + method: 'GET', + data: { + action: 'get_status_dropdown', + itemtype: $(this).val() + } + }).then((result) => { + $('#status_inner_container' + rand).html(result); + }); +}); diff --git a/public/js/question_type.js b/public/js/question_type.js new file mode 100644 index 00000000..63f7a69a --- /dev/null +++ b/public/js/question_type.js @@ -0,0 +1,51 @@ +/** + * ------------------------------------------------------------------------- + * Fields plugin for GLPI + * ------------------------------------------------------------------------- + * + * LICENSE + * + * This file is part of Fields. + * + * Fields 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. + * + * Fields 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. + * + * You should have received a copy of the GNU General Public License + * along with Fields. If not, see . + * ------------------------------------------------------------------------- + * @copyright Copyright (C) 2013-2023 by Fields plugin team. + * @license GPLv2 https://www.gnu.org/licenses/gpl-2.0.html + * @link https://github.com/pluginsGLPI/fields + * ------------------------------------------------------------------------- + */ + +// Question type field: init (existing or new question) then lock the mandatory input. +// init is "none" on an AJAX reload, where only the lock applies. +$('[data-fields-question-type]:not([data-fields-ready])').each(function () { + const marker = $(this).attr('data-fields-ready', ''); + const rand = marker.attr('data-fields-question-type'); + const init = marker.attr('data-fields-question-type-init'); + const mandatory = marker.attr('data-fields-mandatory') === '1' ? 1 : 0; + + import(CFG_GLPI.root_doc + GLPI_PLUGINS_PATH.fields + '/js/modules/QuestionField.js').then((m) => { + const questionField = new m.GlpiPluginFieldsQuestionTypeField(); + + if (init === 'existing') { + questionField.initExistingQuestion(rand); + } else if (init === 'new') { + questionField.initEventHandlers(); + } + + questionField.lockMandatoryInput( + $('[data-glpi-form-editor-question-type-fields-field-id-selector="' + rand + '"]').closest('[data-glpi-form-editor-question-details]'), + mandatory + ); + }); +}); diff --git a/setup.php b/setup.php index a7e9b379..5f7c282c 100644 --- a/setup.php +++ b/setup.php @@ -182,6 +182,8 @@ function plugin_init_fields() // Add/delete profiles to automaticaly to container $PLUGIN_HOOKS['item_add']['fields']['Profile'] = ['PluginFieldsProfile', 'addNewProfile']; $PLUGIN_HOOKS['pre_item_purge']['fields']['Profile'] = ['PluginFieldsProfile', 'deleteProfile']; + // Event handlers for the subitem loaders and the display conditions and status overrides lists + $PLUGIN_HOOKS['add_javascript']['fields'][] = 'js/fields.js'; //load drag and drop javascript library on Package Interface @@ -234,12 +236,13 @@ function plugin_fields_script_endswith($scriptname) function plugin_version_fields() { return [ - 'name' => __('Additional fields', 'fields'), - 'version' => PLUGIN_FIELDS_VERSION, - 'author' => "Teclib', Olivier Moron", - 'homepage' => 'https://github.com/pluginsGLPI/fields', - 'license' => 'GPLv2+', - 'requirements' => [ + 'name' => __('Additional fields', 'fields'), + 'version' => PLUGIN_FIELDS_VERSION, + 'author' => "Teclib', Olivier Moron", + 'homepage' => 'https://github.com/pluginsGLPI/fields', + 'license' => 'GPLv2+', + 'csp_compliant' => true, + 'requirements' => [ 'glpi' => [ 'min' => PLUGIN_FIELDS_MIN_GLPI, 'max' => PLUGIN_FIELDS_MAX_GLPI, diff --git a/templates/container_display_conditions.html.twig b/templates/container_display_conditions.html.twig index 3eff1578..7855a735 100644 --- a/templates/container_display_conditions.html.twig +++ b/templates/container_display_conditions.html.twig @@ -29,7 +29,7 @@ {% import 'components/form/fields_macros.html.twig' as fields %} {% set rand = random() %} -
+
{{ __('The engine is used for hide block when main object meets condition') }} @@ -102,46 +102,4 @@
{# .flex-row #}
{# .card-body #} -
diff --git a/templates/forms/status_override.html.twig b/templates/forms/status_override.html.twig index 5f789a5f..a9ef6f4e 100644 --- a/templates/forms/status_override.html.twig +++ b/templates/forms/status_override.html.twig @@ -29,7 +29,7 @@ {% import 'components/form/fields_macros.html.twig' as fields %} {% set rand = random() %} -
+
@@ -70,24 +70,3 @@
{# .card-body #}
- diff --git a/templates/question_type_administration.html.twig b/templates/question_type_administration.html.twig index 6de9abee..99c4e9a1 100644 --- a/templates/question_type_administration.html.twig +++ b/templates/question_type_administration.html.twig @@ -75,31 +75,7 @@ } ) }} -{% if not is_ajax_reload %} - -{% else %} - -{% endif %} + +{{ call('Html::script', ['/plugins/fields/js/question_type.js', {}, false])|raw }} diff --git a/templates/status_overrides.html.twig b/templates/status_overrides.html.twig index d91e2193..0d228354 100644 --- a/templates/status_overrides.html.twig +++ b/templates/status_overrides.html.twig @@ -29,7 +29,7 @@ {% import 'components/form/fields_macros.html.twig' as fields %} {% set rand = random() %} -
+
{# .flex-row #}
{# .card-body #} -