Skip to content
Snippets Groups Projects
Commit 7c4f019f authored by Isabel Uffinger's avatar Isabel Uffinger
Browse files

checking concrete encodings and added error mesage

parent 18a5b9f4
No related branches found
No related tags found
No related merge requests found
......@@ -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;
......
......@@ -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);
}
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment