Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](http://semver.org/).

## [Unreleased]

### Added

- Make the plugin compatible with the Content Security Policy

### Fixed

- Fix the uploaded file name being lost after the client injection upload step
Expand Down
4 changes: 2 additions & 2 deletions inc/info.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -206,10 +206,10 @@ public static function showFormInfos(PluginDatainjectionModel $model)
echo "<tr>";
echo sprintf("<td><i class='%s fa-flip-horizontal fa-lg mx-2'></i></td>", $arrow);
echo "<td class='center' style='white-space:nowrap;'>";
echo sprintf("<a onclick= \"if ( markCheckboxes('%s') ) return false;\" href='#'>", $formname) . __s('Check all') . "</a></td>";
echo sprintf("<a href='#' data-datainjection-mark-checkboxes='%s'>", htmlescape($formname)) . __s('Check all') . "</a></td>";
echo "<td>/</td>";
echo "<td class='center' style='white-space:nowrap;'>";
echo sprintf("<a onclick= \"if ( unMarkCheckboxes('%s') ) return false;\" href='#'>", $formname) . __s('Uncheck all') . "</a></td>";
echo sprintf("<a href='#' data-datainjection-unmark-checkboxes='%s'>", htmlescape($formname)) . __s('Uncheck all') . "</a></td>";
echo "<td class='left' width='80%'>";

echo "<input type='submit' name='delete' ";
Expand Down
2 changes: 1 addition & 1 deletion inc/model.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -1363,7 +1363,7 @@ public static function showPreviewMappings($models_id)

Html::closeForm();
echo "<div style='margin-top:15px;text-align:center'>";
echo "<a href='javascript:window.close()'>" . __s('Close') . "</a>";
echo "<a href='#' data-datainjection-close-window>" . __s('Close') . "</a>";
echo "</div>";
}

Expand Down
57 changes: 57 additions & 0 deletions public/js/clientinjection.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/**
* -------------------------------------------------------------------------
* DataInjection plugin for GLPI
* -------------------------------------------------------------------------
*
* LICENSE
*
* This file is part of DataInjection.
*
* DataInjection 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.
*
* DataInjection 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 DataInjection. If not, see <http://www.gnu.org/licenses/>.
* -------------------------------------------------------------------------
* @copyright Copyright (C) 2007-2023 by DataInjection plugin team.
* @license GPLv2 https://www.gnu.org/licenses/gpl-2.0.html
* @link https://github.com/pluginsGLPI/datainjection
* -------------------------------------------------------------------------
*/

$(function() {
document.querySelectorAll('[data-datainjection-model-form]:not([data-datainjection-model-form-ready])').forEach((span) => {
span.setAttribute('data-datainjection-model-form-ready', '');

const rand = span.dataset.rand;
const uploadUrl = span.dataset.uploadUrl;
const resultUrl = span.dataset.resultUrl;
const modelId = Number(span.dataset.modelsId);
const step = Number(span.dataset.step);
const resultStep = Number(span.dataset.resultStep);

function loadFormForModel(modelId) {
if (modelId > 0) {
const url = (step === resultStep) ? resultUrl : uploadUrl;
$('#span_injection').load(url, { models_id: modelId });
}
}

// On model change
$('#clientinjection_form' + rand + ' select[name="dropdown_models"]').on('change select2:select', function() {
loadFormForModel($(this).val());
});

// Initial load
if (modelId > 0) {
loadFormForModel(modelId);
}
});
});
51 changes: 51 additions & 0 deletions public/js/datainjection.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,54 @@ function show_log(num) {
document.getElementById('log'+num+'_table').style.display = 'none';
}
}

document.addEventListener('click', (e) => {
const confirmButton = e.target.closest('[data-datainjection-confirm]');
if (confirmButton !== null && !window.confirm(confirmButton.dataset.datainjectionConfirm)) {
e.preventDefault();
e.stopImmediatePropagation();
}
}, true);

document.addEventListener('click', (e) => {
const markLink = e.target.closest('[data-datainjection-mark-checkboxes]');
if (markLink !== null) {
if (markCheckboxes(markLink.dataset.datainjectionMarkCheckboxes)) {
e.preventDefault();
}
return;
}

const unmarkLink = e.target.closest('[data-datainjection-unmark-checkboxes]');
if (unmarkLink !== null) {
if (unMarkCheckboxes(unmarkLink.dataset.datainjectionUnmarkCheckboxes)) {
e.preventDefault();
}
return;
}

const popupLink = e.target.closest('[data-datainjection-popup]');
if (popupLink !== null) {
const popup = window.open(
popupLink.dataset.datainjectionPopup,
'glpipopup',
popupLink.dataset.datainjectionPopupFeatures
);
popup.focus();
if ('datainjectionPopupPrevent' in popupLink.dataset) {
e.preventDefault();
}
return;
}

const locationLink = e.target.closest('[data-datainjection-location]');
if (locationLink !== null) {
location.href = locationLink.dataset.datainjectionLocation;
return;
}

if (e.target.closest('[data-datainjection-close-window]') !== null) {
e.preventDefault();
window.close();
}
});
7 changes: 7 additions & 0 deletions public/js/injection_progress.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,3 +99,10 @@ function startBatchInjection(container) {

processBatch();
}

$(function() {
document.querySelectorAll('[data-datainjection-injection]:not([data-datainjection-injection-ready])').forEach((container) => {
container.setAttribute('data-datainjection-injection-ready', '');
startBatchInjection(container);
});
});
1 change: 1 addition & 0 deletions setup.php
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ function plugin_version_datainjection()
'homepage' => 'https://github.com/pluginsGLPI/datainjection',
'license' => 'GPLv2+',
'version' => PLUGIN_DATAINJECTION_VERSION,
'csp_compliant' => true,
'requirements' => [
'glpi' => [
'min' => PLUGIN_DATAINJECTION_MIN_GLPI,
Expand Down
38 changes: 9 additions & 29 deletions templates/clientinjection.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -64,37 +64,17 @@

<div class="row mt-3">
<div class="col-12">
<span id="span_injection" name="span_injection"></span>
<span id="span_injection" name="span_injection"
data-datainjection-model-form
data-rand="{{ rand }}"
data-upload-url="{{ upload_url }}"
data-result-url="{{ result_url }}"
data-models-id="{{ models_id }}"
data-step="{{ step }}"
data-result-step="{{ result_step }}"></span>
{{ call('Html::script', ['/plugins/datainjection/js/clientinjection.js', {'version': constant('PLUGIN_DATAINJECTION_VERSION')}, false])|raw }}
</div>
</div>

<script>
$(function() {
const rand = '{{ rand }}';
const uploadUrl = "{{ upload_url }}";
const resultUrl = "{{ result_url }}";
const modelId = {{ models_id }};
const step = {{ step }};
const resultStep = {{ result_step }};

function loadFormForModel(modelId) {
if (modelId > 0) {
const url = (step === resultStep) ? resultUrl : uploadUrl;
$('#span_injection').load(url, { models_id: modelId });
}
}

// On model change
$('#clientinjection_form' + rand + ' select[name="dropdown_models"]').on('change select2:select', function() {
loadFormForModel($(this).val());
});

// Initial load
if (modelId > 0) {
loadFormForModel(modelId);
}
});
</script>
</div>
</form>
</div>
8 changes: 2 additions & 6 deletions templates/clientinjection_injection.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
</div>
<div class="card p-3 my-5"
id="injection_container"
data-datainjection-injection
data-batch-url="{{ batch_url }}"
data-result-url="{{ result_url }}"
data-model-id="{{ model_id }}"
Expand All @@ -58,9 +59,4 @@
{{ __('Injection of the file', 'datainjection') }} — 0 / {{ nblines }} {{ __(' lines', 'datainjection') }}
</div>

<script src="{{ plugin_url }}js/injection_progress.js"></script>
<script>
$(function() {
startBatchInjection(document.getElementById('injection_container'));
});
</script>
{{ call('Html::script', ['/plugins/datainjection/js/injection_progress.js', {'version': constant('PLUGIN_DATAINJECTION_VERSION')}, false])|raw }}
6 changes: 3 additions & 3 deletions templates/clientinjection_result.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -44,18 +44,18 @@

<div class="center mt-2">
<div class="d-flex justify-content-center gap-3">
<a href="#" onclick="var w = window.open('{{ popup_url }}', 'glpipopup', 'height=400, width=1000, top=100, left=100, scrollbars=yes');w.focus();" title="{{ __('See the log', 'datainjection') }}">
<a href="#" data-datainjection-popup="{{ popup_url }}" data-datainjection-popup-features="height=400, width=1000, top=100, left=100, scrollbars=yes" title="{{ __('See the log', 'datainjection') }}">
<h2><i class="fa-2x ti ti-file-search"></i></h2>
</a>

{% if has_pdf %}
<a href="#" onclick="location.href='export.pdf.php?models_id={{ model_id }}'" title="{{ __('Export rapport in PDF', 'datainjection') }}">
<a href="#" data-datainjection-location="export.pdf.php?models_id={{ model_id }}" title="{{ __('Export rapport in PDF', 'datainjection') }}">
<h2><i class="fa-2x ti ti-file-type-pdf"></i></h2>
</a>
{% endif %}

{% if has_errors %}
<a href="#" onclick="location.href='export.csv.php'" title="{{ __('Export the log', 'datainjection') }}">
<a href="#" data-datainjection-location="export.csv.php" title="{{ __('Export the log', 'datainjection') }}">
<h2><i class="fa-2x ti ti-file-type-csv" style="color: #dc3545;"></i></h2>
</a>
{% endif %}
Expand Down
2 changes: 1 addition & 1 deletion templates/clientinjection_upload_file.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@
{% else %}
{% set message = __("Watch out, you're about to inject data into GLPI. Are you sure you want to do it ?", 'datainjection') %}
{% endif %}
<input type="submit" class="btn btn-primary" name="upload" value="{{ submit_label }}" onclick="return window.confirm('{{ message|e('js') }}');">
<input type="submit" class="btn btn-primary" name="upload" value="{{ submit_label }}" data-datainjection-confirm="{{ message }}">
{% else %}
<input type="submit" class="btn btn-primary" name="upload" value="{{ submit_label }}">
{% endif %}
Expand Down
2 changes: 1 addition & 1 deletion templates/log_results.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,6 @@
{% endif %}

<div class="mt-4 text-center">
<a href="javascript:window.close()" class="btn btn-outline-danger">{{ __('Close') }}</a>
<a href="#" class="btn btn-outline-danger" data-datainjection-close-window>{{ __('Close') }}</a>
</div>
</div>
2 changes: 1 addition & 1 deletion templates/mappings_form.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
<table class="tab_cadre_fixe">
<tr class="tab_bg_1">
<td class="center">
<a href="#" onclick="var w = window.open('{{ preview_url }}', 'glpipopup', 'height=400, width=600, top=100, left=100, scrollbars=yes'); w.focus(); return false;">
<a href="#" data-datainjection-popup="{{ preview_url }}" data-datainjection-popup-prevent data-datainjection-popup-features="height=400, width=600, top=100, left=100, scrollbars=yes">
{{ __('See the file', 'datainjection') }}
</a>
</td>
Expand Down