Add some safeties around the charset-detection and transliteration

This commit is contained in:
Brady Wetherington 2025-01-23 13:04:16 +00:00
parent b8799f8038
commit 6dcd3bfd30

View file

@ -66,25 +66,33 @@ class ImportController extends Controller
if (! ini_get('auto_detect_line_endings')) { if (! ini_get('auto_detect_line_endings')) {
ini_set('auto_detect_line_endings', '1'); ini_set('auto_detect_line_endings', '1');
} }
$file_contents = $file->getContent(); //TODO - this *does* load the whole file in RAM, but we need that to be able to 'iconv' it? if (function_exists('iconv')) {
$encoding = $detector->getEncoding($file_contents); $file_contents = $file->getContent(); //TODO - this *does* load the whole file in RAM, but we need that to be able to 'iconv' it?
$reader = null; $encoding = $detector->getEncoding($file_contents);
if (strcasecmp($encoding, 'UTF-8') != 0) { $reader = null;
$transliterated = iconv($encoding, 'UTF-8', $file_contents); if (strcasecmp($encoding, 'UTF-8') != 0) {
if ($transliterated !== false) { $transliterated = false;
$tmpname = tempnam(sys_get_temp_dir(), ''); try {
$tmpresults = file_put_contents($tmpname, $transliterated); $transliterated = iconv(strtoupper($encoding), 'UTF-8', $file_contents);
if ($tmpresults !== false) { } catch (\Exception $e) {
$transliterated = null; //save on memory? $transliterated = false;
$newfile = new UploadedFile($tmpname, $file->getClientOriginalName(), null, null, true); //WARNING: this is enabling 'test mode' - which is gross, but otherwise the file won't be treated as 'uploaded' \Log::info("Unable to transliterate from $encoding to UTF-8");
if ($newfile->isValid()) { }
$file = $newfile; if ($transliterated !== false) {
$tmpname = tempnam(sys_get_temp_dir(), '');
$tmpresults = file_put_contents($tmpname, $transliterated);
if ($tmpresults !== false) {
$transliterated = null; //save on memory?
$newfile = new UploadedFile($tmpname, $file->getClientOriginalName(), null, null, true); //WARNING: this is enabling 'test mode' - which is gross, but otherwise the file won't be treated as 'uploaded'
if ($newfile->isValid()) {
$file = $newfile;
}
} }
} }
} }
$file_contents = null; //try to save on memory, I guess?
} }
$reader = Reader::createFromFileObject($file->openFile('r')); //file pointer leak? $reader = Reader::createFromFileObject($file->openFile('r')); //file pointer leak?
$file_contents = null; //try to save on memory, I guess?
try { try {
$import->header_row = $reader->fetchOne(0); $import->header_row = $reader->fetchOne(0);