diff --git a/externallib.php b/externallib.php index e6dd8547ff001d3e0eaf38b82a8e03e65cb1b3db..0a6949979beed519d0608b6050a3c58aef58e370 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 1d2201baad7d5754d47c4eae3b21512e52b1d54a..37500801b8b69d908d2e30171a4bbae4e6e165ba 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); } }