diff --git a/CHANGELOG.md b/CHANGELOG.md index 1e33be7f..1815d827 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,18 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](http://keepachangelog.com/) and this project adheres to [Semantic Versioning](http://semver.org/). +## [Unreleased] + +### Fixed + +- Fix handling of additional information values in the injection form +- Fix model checks on the injection upload and the PDF report export +- Fix rights used by the model field dropdowns +- Fix CSV export of headers and of the model sample file +- Fix file name display in the PDF report header +- Fix temporary error file not being removed when its download fails +- Use the query builder for the remaining queries + ## [2.15.13] - 2026-11-13 ### Fixed diff --git a/ajax/dropdownChooseField.php b/ajax/dropdownChooseField.php index 2cd618dd..d87c9f6d 100644 --- a/ajax/dropdownChooseField.php +++ b/ajax/dropdownChooseField.php @@ -34,7 +34,7 @@ Html::header_nocache(); } -Session::checkCentralAccess(); +Session::checkRight(PluginDatainjectionClientInjection::$rightname, READ); // Reads the session only: release it so that the calls of a tab run in parallel Session::writeClose(); PluginDatainjectionInjectionType::dropdownFields($_POST); diff --git a/ajax/dropdownMandatory.php b/ajax/dropdownMandatory.php index f2f575b3..393cfc24 100644 --- a/ajax/dropdownMandatory.php +++ b/ajax/dropdownMandatory.php @@ -34,7 +34,7 @@ Html::header_nocache(); } -Session::checkCentralAccess(); +Session::checkRight(PluginDatainjectionClientInjection::$rightname, READ); // Reads the session only: release it so that the calls of a tab run in parallel Session::writeClose(); PluginDatainjectionInjectionType::showMandatoryCheckbox($_POST); diff --git a/front/clientinjection.form.php b/front/clientinjection.form.php index 4b4bd5d8..42a35ee8 100644 --- a/front/clientinjection.form.php +++ b/front/clientinjection.form.php @@ -28,6 +28,9 @@ * ------------------------------------------------------------------------- */ +use Glpi\Exception\Http\AccessDeniedHttpException; +use Glpi\Exception\Http\BadRequestHttpException; + Session::checkRight("plugin_datainjection_use", READ); Html::header( @@ -43,7 +46,16 @@ PluginDatainjectionClientInjection::showInjectionForm($model, $_SESSION['glpiactive_entity']); } elseif (isset($_POST['upload'])) { $model = new PluginDatainjectionModel(); - $model->can($_POST['id'], READ); + $models_id = (int) ($_POST['id'] ?? 0); + if ($models_id <= 0) { + throw new BadRequestHttpException(); + } + + //Injection users may lack the model management right: only check the model itself is visible + if (!$model->getFromDB($models_id) || !$model->canViewItem()) { + throw new AccessDeniedHttpException(); + } + $_SESSION['datainjection']['infos'] = ($_POST['info'] ?? []); //If additional informations provided : check if mandatory infos are present diff --git a/front/export.pdf.php b/front/export.pdf.php index 2c82c7d5..4f4baed5 100644 --- a/front/export.pdf.php +++ b/front/export.pdf.php @@ -28,6 +28,20 @@ * ------------------------------------------------------------------------- */ +use Glpi\Exception\Http\AccessDeniedHttpException; +use Glpi\Exception\Http\BadRequestHttpException; + Session::checkRight(PluginDatainjectionClientInjection::$rightname, READ); -PluginDatainjectionModel::exportAsPDF($_GET['models_id']); +$models_id = (int) ($_GET['models_id'] ?? 0); +if ($models_id <= 0) { + throw new BadRequestHttpException(); +} + +//Injection users may lack the model management right: only check the model itself is visible +$model = new PluginDatainjectionModel(); +if (!$model->getFromDB($models_id) || !$model->canViewItem()) { + throw new AccessDeniedHttpException(); +} + +PluginDatainjectionModel::exportAsPDF($models_id); diff --git a/inc/clientinjection.class.php b/inc/clientinjection.class.php index a8a12128..67ef2b9f 100644 --- a/inc/clientinjection.class.php +++ b/inc/clientinjection.class.php @@ -328,9 +328,9 @@ public static function showResultsForm(PluginDatainjectionModel $model) TemplateRenderer::getInstance()->display('@datainjection/clientinjection_result.html.twig', $data); } - private static function escapeCsvFormula(mixed $value): mixed + public static function escapeCsvFormula(mixed $value): mixed { - if (is_string($value) && isset($value[0]) && in_array($value[0], ['=', '+', '-', '@'], true)) { + if (is_string($value) && isset($value[0]) && in_array($value[0], ['=', '+', '-', '@', "\t", "\r"], true)) { return "'" . $value; } @@ -351,7 +351,7 @@ private static function writeErrorsCsv(string $dir, array $error_lines, array $h $tmpfile = fopen($file, 'w'); if ($headers !== []) { - fputcsv($tmpfile, $headers, $delimiter); + fputcsv($tmpfile, array_map(self::escapeCsvFormula(...), $headers), $delimiter); } foreach ($error_lines as $line) { @@ -422,8 +422,11 @@ public static function exportErrorsInCSV() header('Pragma: no-cache'); header('Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0'); header('Expires: 0'); - readfile($file); - unlink($file); + try { + readfile($file); + } finally { + unlink($file); + } } } } diff --git a/inc/info.class.php b/inc/info.class.php index ff7bae6a..13829dfe 100644 --- a/inc/info.class.php +++ b/inc/info.class.php @@ -365,9 +365,9 @@ public static function showAdditionalInformation( $value = ($option['default'] ?? ''); } - echo sprintf(""; @@ -411,7 +411,7 @@ public static function showAdditionalInformation( break; case 'multiline_text': - echo sprintf("", $name, $value); + echo sprintf("", htmlescape($name), htmlescape((string) $value)); break; case 'dropdown_integer': @@ -496,13 +496,14 @@ public static function dropdownTemplates($name, $table) $values = [0 => Dropdown::EMPTY_VALUE]; - $sql = "SELECT `id`, `template_name` - FROM `" . $table . "` - WHERE `is_template`=1 " . - getEntitiesRestrictRequest(' AND ', $table) . - "ORDER BY `template_name`"; + $iterator = $DB->request([ + 'SELECT' => ['id', 'template_name'], + 'FROM' => $table, + 'WHERE' => ['is_template' => 1] + getEntitiesRestrictCriteria($table), + 'ORDER' => 'template_name', + ]); - foreach ($DB->doQuery($sql) as $data) { + foreach ($iterator as $data) { $values[$data['id']] = $data['template_name']; } diff --git a/inc/infocollection.class.php b/inc/infocollection.class.php index efbc6096..0945567a 100644 --- a/inc/infocollection.class.php +++ b/inc/infocollection.class.php @@ -45,12 +45,13 @@ public function load($models_id) /** @var DBmysql $DB */ global $DB; - $query = "SELECT * - FROM `glpi_plugin_datainjection_infos` - WHERE `models_id` = '" . $models_id . "' - ORDER BY `itemtype` ASC"; + $iterator = $DB->request([ + 'FROM' => 'glpi_plugin_datainjection_infos', + 'WHERE' => ['models_id' => (int) $models_id], + 'ORDER' => 'itemtype ASC', + ]); - foreach ($DB->doQuery($query) as $data) { + foreach ($iterator as $data) { $infos = new PluginDatainjectionInfo(); $infos->fields = $data; $this->infosCollection[] = $infos; diff --git a/inc/mapping.class.php b/inc/mapping.class.php index 002f1394..17c4010a 100644 --- a/inc/mapping.class.php +++ b/inc/mapping.class.php @@ -29,6 +29,7 @@ */ use Glpi\Application\View\TemplateRenderer; +use Glpi\DBAL\QueryExpression; use function Safe\ob_get_clean; use function Safe\ob_start; @@ -151,15 +152,18 @@ public static function getSeveralMappedField($models_id) global $DB; $several = []; - $query = "SELECT `value`, - COUNT(*) AS counter - FROM `glpi_plugin_datainjection_mappings` - WHERE `models_id` = '" . $models_id . "' - AND `value` NOT IN ('none') - GROUP BY `value` - HAVING `counter` > 1"; - - foreach ($DB->doQuery($query) as $mapping) { + $iterator = $DB->request([ + 'SELECT' => ['value', new QueryExpression('COUNT(*) AS counter')], + 'FROM' => 'glpi_plugin_datainjection_mappings', + 'WHERE' => [ + 'models_id' => (int) $models_id, + 'NOT' => ['value' => 'none'], + ], + 'GROUPBY' => 'value', + 'HAVING' => ['counter' => ['>', 1]], + ]); + + foreach ($iterator as $mapping) { $several[] = $mapping['value']; } @@ -176,11 +180,13 @@ public static function getMappingsSortedByRank($models_id) global $DB; $mappings = []; - $query = "SELECT `name` - FROM `glpi_plugin_datainjection_mappings` - WHERE `models_id` = '" . $models_id . "' - ORDER BY `rank` ASC"; - foreach ($DB->doQuery($query) as $data) { + $iterator = $DB->request([ + 'SELECT' => 'name', + 'FROM' => 'glpi_plugin_datainjection_mappings', + 'WHERE' => ['models_id' => (int) $models_id], + 'ORDER' => 'rank ASC', + ]); + foreach ($iterator as $data) { $mappings[] = $data['name']; } diff --git a/inc/mappingcollection.class.php b/inc/mappingcollection.class.php index 71cb8bfa..4a480fc7 100644 --- a/inc/mappingcollection.class.php +++ b/inc/mappingcollection.class.php @@ -44,14 +44,15 @@ public function load($models_id) /** @var DBmysql $DB */ global $DB; - $sql = "SELECT * - FROM `glpi_plugin_datainjection_mappings` - WHERE `models_id` = '{$models_id}' - ORDER BY `rank` ASC"; + $iterator = $DB->request([ + 'FROM' => 'glpi_plugin_datainjection_mappings', + 'WHERE' => ['models_id' => (int) $models_id], + 'ORDER' => 'rank ASC', + ]); $this->mappingCollection = []; - foreach ($data = $DB->doQuery($sql) as $data) { + foreach ($iterator as $data) { $mapping = new PluginDatainjectionMapping(); $mapping->fields = $data; $this->mappingCollection[] = $mapping; diff --git a/inc/model.class.php b/inc/model.class.php index aa7ca103..e69ac22a 100644 --- a/inc/model.class.php +++ b/inc/model.class.php @@ -1205,8 +1205,13 @@ public function isFileCorrect() ), ); $name_from_db = trim(mb_strtoupper((string) $mapping->getName(), 'UTF-8')); + // Sample and error files are exported with formula-escaped headers + $escaped_name_from_db = trim(mb_strtoupper( + (string) PluginDatainjectionClientInjection::escapeCsvFormula((string) $mapping->getName()), + 'UTF-8', + )); - if ($name_from_db !== $name_from_file) { + if ($name_from_db !== $name_from_file && $escaped_name_from_db !== $name_from_file) { if ($error['error_message'] == '') { $error['error_message'] = __('At least one column is incorrect', 'datainjection'); } @@ -1549,8 +1554,8 @@ public static function exportAsPDF($models_id) $pdf->setHeader( sprintf( __('%1$s (%2$s)'), - __('Data injection report', 'datainjection') . ' - ' . - PluginDatainjectionSession::getParam('file_name') . '', + __('Data injection report', 'datainjection') . ' - ' + . PluginDatainjectionSession::getParam('file_name'), $model->getName(), ), ); diff --git a/inc/modelcsv.class.php b/inc/modelcsv.class.php index 8666a76b..2067f3a4 100644 --- a/inc/modelcsv.class.php +++ b/inc/modelcsv.class.php @@ -30,6 +30,12 @@ use Glpi\Application\View\TemplateRenderer; +use function Safe\fclose; +use function Safe\fopen; +use function Safe\fputcsv; +use function Safe\rewind; +use function Safe\stream_get_contents; + /** * ------------------------------------------------------------------------- * DataInjection plugin for GLPI @@ -119,7 +125,11 @@ public function showSample(PluginDatainjectionModel $model) { $headers = PluginDatainjectionMapping::getMappingsSortedByRank($model->fields['id']); - $sample = '"' . implode('"' . $this->getDelimiter() . '"', $headers) . "\"\n"; + $handle = fopen('php://temp', 'r+'); + fputcsv($handle, array_map(PluginDatainjectionClientInjection::escapeCsvFormula(...), $headers), $this->getDelimiter()); + rewind($handle); + $sample = stream_get_contents($handle); + fclose($handle); header( 'Content-disposition: attachment; filename="' . str_replace( diff --git a/inc/profile.class.php b/inc/profile.class.php index bd657c27..e2bace97 100644 --- a/inc/profile.class.php +++ b/inc/profile.class.php @@ -48,21 +48,6 @@ public static function getAllRights() ]; } - /** - * Clean profiles_id from plugin's profile table - * - * @param int $ID - **/ - public function cleanProfiles($ID) - { - /** @var DBmysql $DB */ - global $DB; - $query = "DELETE FROM `glpi_profiles` - WHERE `profiles_id`='{$ID}' - AND `name` LIKE '%plugin_datainjection%'"; - $DB->doQuery($query); - } - public function getTabNameForItem(CommonGLPI $item, $withtemplate = 0) { @@ -147,10 +132,13 @@ public static function migrateProfiles() $profiles = getAllDataFromTable('glpi_plugin_datainjection_profiles'); foreach ($profiles as $id => $profile) { - $query = "SELECT `id` FROM `glpi_profiles` WHERE `name`='" . $profile['name'] . "'"; - $result = $DB->doQuery($query); - if ($DB->numrows($result) == 1) { - $id = $DB->result($result, 0, 'id'); + $iterator = $DB->request([ + 'SELECT' => 'id', + 'FROM' => 'glpi_profiles', + 'WHERE' => ['name' => $profile['name']], + ]); + if (count($iterator) === 1) { + $id = $iterator->current()['id']; switch ($profile['model']) { case 'r': $value = READ; diff --git a/tests/unit/AdditionalInformationTest.php b/tests/unit/AdditionalInformationTest.php index f366d82f..12fc5e99 100644 --- a/tests/unit/AdditionalInformationTest.php +++ b/tests/unit/AdditionalInformationTest.php @@ -131,4 +131,23 @@ public function testFormInputIsNamedAfterItemtypeAndField(): void self::assertStringContainsString("name='info[DeviceProcessor][designation]'", $html); self::assertStringContainsString(DeviceProcessor::getTypeName(1) . ' - ', $html); } + + public function testPostedValueIsEscaped(): void + { + $this->login(); + + $info = new PluginDatainjectionInfo(); + $info->fields = ['itemtype' => 'DeviceProcessor', 'value' => 'designation', 'is_mandatory' => 0]; + + $_SESSION['datainjection']['infos'] = ['DeviceProcessor' => ['designation' => "'>"]]; + + ob_start(); + PluginDatainjectionInfo::displayAdditionalInformation($info); + $html = ob_get_clean(); + + unset($_SESSION['datainjection']['infos']); + + self::assertStringNotContainsString('