Commit 7889068e authored by Bertrand Gauthier's avatar Bertrand Gauthier
Browse files

Module STepStar : tentative de suppression des caractères unicode qui font...

Module STepStar : tentative de suppression des caractères unicode qui font planter le parser XML dans les résumés
parent beff3409
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -52,7 +52,7 @@ class TransformShellCommand extends ShellCommand
        $parts = [
            $this->executable,
            $this->verbose ? '-t' : null,
            sprintf("-s:'%s' -xsl:'%s' -o:'%s'", $this->inputFilePath, $this->xslFilePath, $this->outputFilePath),
            sprintf("-s:'%s' -xsl:'%s' -o:'%s' 2>&1", $this->inputFilePath, $this->xslFilePath, $this->outputFilePath),
        ];

        $this->commandLine = implode(' ', array_filter($parts));
+52 −2
Original line number Diff line number Diff line
@@ -455,8 +455,8 @@ class XmlService
            self::TITRE_LANGUE => $langue = $metadonnees['langue'] ?? 'fr',
            self::TITRE_TRADUIT => $metadonnees['titreAutreLangue'],
            self::TITRE_TRADUIT_LANGUE => $langue === 'fr' ? 'en' : 'fr',
            self::RESUME_FRANCAIS => $metadonnees['resume'] ?? null,
            self::RESUME_ANGLAIS => $metadonnees['resumeAnglais'] ?? null,
            self::RESUME_FRANCAIS => $this->sanitizeXML($metadonnees['resume'] ?? ''),
            self::RESUME_ANGLAIS => $this->sanitizeXML($metadonnees['resumeAnglais'] ?? ''),

            // contrat doctoral
            self::CONTRAT_DOCTORAL => $contratDoctoral,
@@ -675,4 +675,54 @@ class XmlService
    {
        return $date ? $date->format('Y-m-d') : null;
    }

    /**
     * Removes invalid characters from a UTF-8 XML string
     *
     * @param string a XML string potentially containing invalid characters
     * @return string
     */
    private function sanitizeXML(string $string): string
    {
        if (!empty($string))
        {
            // remove EOT+NOREP+EOX|EOT+<char> sequence (FatturaPA)
            $string = preg_replace('/(\x{0004}(?:\x{201A}|\x{FFFD})(?:\x{0003}|\x{0004}).)/u', '', $string);

            $regex = '/(
            [\xC0-\xC1] # Invalid UTF-8 Bytes
            | [\xF5-\xFF] # Invalid UTF-8 Bytes
            | \xE0[\x80-\x9F] # Overlong encoding of prior code point
            | \xF0[\x80-\x8F] # Overlong encoding of prior code point
            | [\xC2-\xDF](?![\x80-\xBF]) # Invalid UTF-8 Sequence Start
            | [\xE0-\xEF](?![\x80-\xBF]{2}) # Invalid UTF-8 Sequence Start
            | [\xF0-\xF4](?![\x80-\xBF]{3}) # Invalid UTF-8 Sequence Start
            | (?<=[\x0-\x7F\xF5-\xFF])[\x80-\xBF] # Invalid UTF-8 Sequence Middle
            | (?<![\xC2-\xDF]|[\xE0-\xEF]|[\xE0-\xEF][\x80-\xBF]|[\xF0-\xF4]|[\xF0-\xF4][\x80-\xBF]|[\xF0-\xF4][\x80-\xBF]{2})[\x80-\xBF] # Overlong Sequence
            | (?<=[\xE0-\xEF])[\x80-\xBF](?![\x80-\xBF]) # Short 3 byte sequence
            | (?<=[\xF0-\xF4])[\x80-\xBF](?![\x80-\xBF]{2}) # Short 4 byte sequence
            | (?<=[\xF0-\xF4][\x80-\xBF])[\x80-\xBF](?![\x80-\xBF]) # Short 4 byte sequence (2)
        )/x';
            $string = preg_replace($regex, '', $string);

            $result = "";
            $length = strlen($string);
            for ($i=0; $i < $length; $i++)
            {
                $current = ord($string[$i]);
                if (($current == 0x9) ||
                    ($current == 0xA) ||
                    ($current == 0xD) ||
                    (($current >= 0x20) && ($current <= 0xD7FF)) ||
                    (($current >= 0xE000) && ($current <= 0xFFFD)) ||
                    (($current >= 0x10000) && ($current <= 0x10FFFF)))
                {
                    $result .= chr($current);
                }
            }
            $string = $result;
        }

        return $string;
    }
}
 No newline at end of file