From 5ebccc313cf0bd57760e19f32a852eb605c439d2 Mon Sep 17 00:00:00 2001 From: Elorfin Date: Thu, 23 Mar 2023 14:43:50 +0100 Subject: [PATCH] [ClacoForm] strip HTML in export --- .../app/Resources/modules/intl/date/index.js | 5 - .../Library/Normalizer/TextNormalizer.php | 184 +++++++++++------- .../core/Library/Utilities/ClaroUtilities.php | 83 +------- src/main/core/Manager/LogManager.php | 50 ++--- src/main/core/Manager/ResourceManager.php | 44 ++--- .../Resources/config/services/manager.yml | 2 - .../Administration/LogController.php | 129 ------------ .../Resource/DashboardController.php | 45 +---- .../Controller/Workspace/LogController.php | 129 ------------ .../Resources/config/services/controller.yml | 18 -- .../API/SectionCommentController.php | 66 +------ .../Resources/config/services/controller.yml | 5 +- .../modules/files/audio/components/player.jsx | 10 +- .../Resources/translations/audio.en.json | 1 - .../Resources/translations/audio.fr.json | 1 - .../claco-form/Manager/ExportManager.php | 29 +-- .../AnswerableItemDefinitionInterface.php | 2 +- src/plugin/exo/Manager/ExerciseManager.php | 75 ++----- .../exo/Resources/config/services/manager.yml | 4 - 19 files changed, 189 insertions(+), 693 deletions(-) delete mode 100644 src/plugin/analytics/Controller/Administration/LogController.php delete mode 100644 src/plugin/analytics/Controller/Workspace/LogController.php diff --git a/src/main/app/Resources/modules/intl/date/index.js b/src/main/app/Resources/modules/intl/date/index.js index 4396e641857..f552448b629 100644 --- a/src/main/app/Resources/modules/intl/date/index.js +++ b/src/main/app/Resources/modules/intl/date/index.js @@ -186,10 +186,6 @@ function getTimeDiff(startDate, endDate) { return moment.duration(diff).asSeconds() } -function nowAdd(addition, local = true) { - return local ? moment().utc().local().add(addition).format(getApiFormat()) : moment().utc().add(addition).format(getApiFormat()) -} - export { getApiFormat, getDisplayFormat, @@ -201,7 +197,6 @@ export { dateToDisplayFormat, computeElapsedTime, getTimeDiff, - nowAdd, displayDuration, displayTime, displayDateRange diff --git a/src/main/core/Library/Normalizer/TextNormalizer.php b/src/main/core/Library/Normalizer/TextNormalizer.php index 007456533d8..6e08478c5ef 100644 --- a/src/main/core/Library/Normalizer/TextNormalizer.php +++ b/src/main/core/Library/Normalizer/TextNormalizer.php @@ -4,15 +4,8 @@ class TextNormalizer { - /** - * @param $string - * - * @return string - */ - public static function stripDiacritics($string) + public static function stripDiacritics(string $string): string { - $string = (string) $string; - if (!preg_match('/[\x80-\xff]/', $string)) { return $string; } @@ -82,9 +75,8 @@ public static function stripDiacritics($string) // grave accent 'Ǜ' => 'U', 'ǜ' => 'u', ]; - $string = str_replace(array_keys($transliteration), array_values($transliteration), $string); - return $string; + return str_replace(array_keys($transliteration), array_values($transliteration), $string); } public static function toKey($string, int $length = null) @@ -118,6 +110,9 @@ public static function toUtf8(string $string): string return $string; } + /** + * Converts a string into UTF-8 and replaces EOL by PHP ones. + */ public static function sanitize(string $string): string { // If encoding not UTF-8 then convert it to UTF-8 @@ -126,6 +121,7 @@ public static function sanitize(string $string): string // normalize end of lines $string = str_replace("\r\n", PHP_EOL, $string); $string = str_replace("\r", PHP_EOL, $string); + $string = str_replace("\n", PHP_EOL, $string); return $string; } @@ -135,87 +131,127 @@ public static function sanitize(string $string): string */ public static function resumeHtml(string $text, int $nbCharacter, ?string $readMoreText = ''): string { - if (is_numeric($nbCharacter)) { - $lengthBeforeWithoutHtml = strlen(trim(strip_tags($text))); - $htmlSplitMask = '##'; - $htmlMatchMask = '#<(?:/([a-zA-Z1-6]+)|([a-zA-Z1-6]+)(?: +[a-zA-Z]+="[^"]*")*( ?/)?)>#'; + $lengthBeforeWithoutHtml = strlen(trim(strip_tags($text))); + $htmlSplitMask = '##'; + $htmlMatchMask = '#<(?:/([a-zA-Z1-6]+)|([a-zA-Z1-6]+)(?: +[a-zA-Z]+="[^"]*")*( ?/)?)>#'; + $text .= ' '; + $textPieces = preg_split($htmlSplitMask, $text, -1, PREG_SPLIT_OFFSET_CAPTURE | PREG_SPLIT_NO_EMPTY); + $pieceNumber = count($textPieces); + + if (1 === $pieceNumber) { $text .= ' '; - $textPieces = preg_split($htmlSplitMask, $text, -1, PREG_SPLIT_OFFSET_CAPTURE | PREG_SPLIT_NO_EMPTY); - $pieceNumber = count($textPieces); + $lengthBefore = strlen($text); + $text = substr($text, 0, strpos($text, ' ', $lengthBefore > $nbCharacter ? $nbCharacter : $lengthBefore)); - if (1 === $pieceNumber) { - $text .= ' '; - $lengthBefore = strlen($text); - $text = substr($text, 0, strpos($text, ' ', $lengthBefore > $nbCharacter ? $nbCharacter : $lengthBefore)); + if ('' != $readMoreText && $lengthBefore > $nbCharacter) { + $text .= $readMoreText; + } + } else { + $length = 0; + $indexLastPiece = $pieceNumber - 1; + $position = $textPieces[$indexLastPiece][1] + strlen($textPieces[$indexLastPiece][0]) - 1; + $indexPiece = $indexLastPiece; + $searchSpace = true; - if ('' != $readMoreText && $lengthBefore > $nbCharacter) { - $text .= $readMoreText; - } - } else { - $length = 0; - $indexLastPiece = $pieceNumber - 1; - $position = $textPieces[$indexLastPiece][1] + strlen($textPieces[$indexLastPiece][0]) - 1; - $indexPiece = $indexLastPiece; - $searchSpace = true; - - foreach ($textPieces as $index => $bout) { - $length += strlen($bout[0]); - if ($length >= $nbCharacter) { - $positionEndPiece = $bout[1] + strlen($bout[0]) - 1; - $position = $positionEndPiece - ($length - $nbCharacter); - - $positionSpace = strpos($bout[0], ' ', $position - $bout[1]); - if (false !== $positionSpace) { - $position = $bout[1] + $positionSpace; - $searchSpace = false; - } - if ($index != $indexLastPiece) { - $indexPiece = $index + 1; - } - break; + foreach ($textPieces as $index => $bout) { + $length += strlen($bout[0]); + if ($length >= $nbCharacter) { + $positionEndPiece = $bout[1] + strlen($bout[0]) - 1; + $position = $positionEndPiece - ($length - $nbCharacter); + + $positionSpace = strpos($bout[0], ' ', $position - $bout[1]); + if (false !== $positionSpace) { + $position = $bout[1] + $positionSpace; + $searchSpace = false; } + if ($index != $indexLastPiece) { + $indexPiece = $index + 1; + } + break; } + } - if (true === $searchSpace) { - for ($i = $indexPiece; $i <= $indexLastPiece; ++$i) { - $position = $textPieces[$i][1]; - $positionSpace = strpos($textPieces[$i][0], ' '); - if (false !== $positionSpace) { - $position += $positionSpace; - break; - } + if (true === $searchSpace) { + for ($i = $indexPiece; $i <= $indexLastPiece; ++$i) { + $position = $textPieces[$i][1]; + $positionSpace = strpos($textPieces[$i][0], ' '); + if (false !== $positionSpace) { + $position += $positionSpace; + break; } } + } - $text = substr($text, 0, $position); - preg_match_all($htmlMatchMask, $text, $return, PREG_OFFSET_CAPTURE); - $tagPieces = []; + $text = substr($text, 0, $position); + preg_match_all($htmlMatchMask, $text, $return, PREG_OFFSET_CAPTURE); + $tagPieces = []; - foreach ($return[0] as $index => $tag) { - if (isset($return[3][$index][0])) { - continue; - } - if ('/' != $return[0][$index][0][1]) { - array_unshift($tagPieces, $return[2][$index][0]); - } else { - array_shift($tagPieces); - } + foreach ($return[0] as $index => $tag) { + if (isset($return[3][$index][0])) { + continue; } - - if (!empty($tagPieces)) { - foreach ($tagPieces as $tag) { - $text .= ''; - } + if ('/' != $return[0][$index][0][1]) { + array_unshift($tagPieces, $return[2][$index][0]); + } else { + array_shift($tagPieces); } + } - if ('' != $readMoreText && $lengthBeforeWithoutHtml > $nbCharacter) { - $text .= 'SuspensionPoint'; - $pattern = '#((]*>[\n\t\r ]*)?(]*>[\n\t\r ]*)?(]*>[\n\t\r ]*)?(]*>[\n\t\r ]*)?(]*>)[\n\t\r ]*SuspensionPoint)#i'; - $text = preg_replace($pattern, $readMoreText.'${2}${3}${4}${5}${6}', $text); + if (!empty($tagPieces)) { + foreach ($tagPieces as $tag) { + $text .= ''; } } + + if ('' != $readMoreText && $lengthBeforeWithoutHtml > $nbCharacter) { + $text .= 'SuspensionPoint'; + $pattern = '#((]*>[\n\t\r ]*)?(]*>[\n\t\r ]*)?(]*>[\n\t\r ]*)?(]*>[\n\t\r ]*)?(]*>)[\n\t\r ]*SuspensionPoint)#i'; + $text = preg_replace($pattern, $readMoreText.'${2}${3}${4}${5}${6}', $text); + } } return $text; } + + public static function stripHtml(string $htmlStr, ?bool $preserveMedia = false): string + { + $csvStr = self::sanitize($htmlStr); + if ($preserveMedia) { + $csvStr = strip_tags($csvStr, '