diff --git a/Services/Excel/classes/class.ilExcel.php b/Services/Excel/classes/class.ilExcel.php index 6e13f94033a8..f7196a646e0a 100644 --- a/Services/Excel/classes/class.ilExcel.php +++ b/Services/Excel/classes/class.ilExcel.php @@ -37,6 +37,8 @@ class ilExcel public const FORMAT_BIFF = "Xls"; protected string $format; + private string $noncharacters = '[\uFFFE-\uFFFF\uFDD0-\uFDEF]'; + protected ilLanguage $lng; protected Spreadsheet $workbook; protected string $type; @@ -158,15 +160,23 @@ public function getSheetTitle(): string * Prepare value for cell * @param mixed $a_value * @return mixed + * @throws InvalidArgumentException */ protected function prepareValue($a_value) { if (is_bool($a_value)) { - $a_value = $this->prepareBooleanValue($a_value); - } elseif ($a_value instanceof ilDateTime) { - $a_value = $this->prepareDateValue($a_value); - } elseif (is_string($a_value)) { - $a_value = $this->prepareString($a_value); + return $this->prepareBooleanValue($a_value); + } + + if ($a_value instanceof ilDateTime) { + return $this->prepareDateValue($a_value); + } + + if (is_string($a_value)) { + if (!mb_check_encoding($a_value, 'UTF-8')) { + throw new InvalidArgumentException('Invalid UTF-8 passed.'); + } + return $this->prepareString($a_value); } return $a_value; @@ -200,7 +210,9 @@ protected function prepareBooleanValue(bool $a_value): string protected function prepareString(string $a_value): string { - return strip_tags($a_value); // #14542 + return $this->cleanupNonCharachters( + strip_tags($a_value) + ); // #14542 } /** @@ -555,4 +567,9 @@ public function mergeCells(string $coordinatesRange): void { $this->workbook->getActiveSheet()->mergeCells($coordinatesRange); } + + private function cleanupNonCharachters(string $string): string + { + return mb_ereg_replace($this->noncharacters, '', $string); + } }