From 7c4f019f4ebf97a51b84837c61cba4094f06ea65 Mon Sep 17 00:00:00 2001 From: Isabel <isabel-andrea.uffinger@stud.hs-hannover.de> Date: Sat, 4 May 2024 13:14:47 +0200 Subject: [PATCH] checking concrete encodings and added error mesage --- externallib.php | 3 +++ locallib.php | 13 ++++++++----- 2 files changed, 11 insertions(+), 5 deletions(-) diff --git a/externallib.php b/externallib.php index e6dd854..0a69499 100644 --- a/externallib.php +++ b/externallib.php @@ -195,6 +195,9 @@ class qtype_moopt_external extends external_api { $encoding = $child->attributes->getNamedItem('encoding')->nodeValue; $filecontent = get_text_content_from_file($usercontext, $draftid, $keepfilename, $pathinfo['dirname'] . '/', $pathinfo['basename'], true, $encoding); + if($filecontent === null){ + $returnval['error'] = "Das Encoding der Datei ".$pathinfo['basename']." konnte nicht erkannt werden"; + } $defaultfilename = basename($child->nodeValue); $fileid = $file->attributes->getNamedItem('id')->nodeValue; $enablefreetext = true; diff --git a/locallib.php b/locallib.php index 1d2201b..3750080 100644 --- a/locallib.php +++ b/locallib.php @@ -294,18 +294,21 @@ function get_text_content_from_file($usercontext, $draftareaid, $keepfilename, $ // TODO: make sure the mimetype is plain text // even task.xmls may contain mistakes (eg PDF ) - //check if encoding of attached file is utf-8 else convert + //check if encoding of attached file is utf-8 else convert if encoding could be detectet $content = $file->get_content(); + $test_encodings = array('UTF-8', 'ISO-8859-1', 'ISO-8859-15', 'Windows-1252', 'UTF-16'); if($attached){ if($encoding!=null){ $enc=$encoding; } else { - $enc = mb_detect_encoding($content, null, true); - if($enc===false){ - throw new invalid_parameter_exception('Encoding of attached file ' . $filepath . $filename . ' could\'nt be detectet.'); + $enc = mb_detect_encoding($content, null, false); + if ($enc==false){ + $enc = mb_detect_encoding($content, $test_encodings, true); } } - if($enc!=='UTF-8'){ + if($enc==false){ + return null; + } else if($enc!=='UTF-8'){ $content = mb_convert_encoding($content, 'UTF-8', $enc); } } -- GitLab