Skip to content

Commit

Permalink
Excel: Error Invalid UTF-8 & Remove Noncharacters
Browse files Browse the repository at this point in the history
  • Loading branch information
kergomard committed Nov 11, 2024
1 parent 2a21251 commit 333da2a
Showing 1 changed file with 23 additions and 6 deletions.
29 changes: 23 additions & 6 deletions Services/Excel/classes/class.ilExcel.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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
}

/**
Expand Down Expand Up @@ -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);
}
}

0 comments on commit 333da2a

Please sign in to comment.