post.php: rework file image check

This commit is contained in:
Zankaria 2025-03-25 23:07:21 +01:00
parent 2b0473c2b8
commit 60119ddbe4

View file

@ -1290,25 +1290,31 @@ function handle_post(Context $ctx)
try { try {
$ret = Metadata\sniff_image($file['tmp_name']); $ret = Metadata\sniff_image($file['tmp_name']);
if (\in_array($ret[2], Metadata\SUPPORTED_IMAGE_MIME_TYPES)) { $ext = Metadata\mime_to_ext($ret[2]);
$ext = Metadata\mime_to_ext($ret[2]);
$file['is_an_image'] = !\in_array($ext, $config['allowed_ext_files']);
if ($file['is_an_image']) {
$file['width'] = $ret[0];
$file['height'] = $ret[1];
$file['mime'] = $ret[2];
}
}
$file['is_an_image'] = false;
} catch (\RuntimeException $e) {
$file['is_an_image'] = false;
}
if (!$file['is_an_image']) { // Is it some kind of supported image?
if (!\in_array($file['extension'], $config['allowed_ext_files'])) { if (\in_array($ret[2], Metadata\SUPPORTED_IMAGE_MIME_TYPES)) {
// Should be 'invalid media' really. // Is it allowed?
error($config['error']['invalidimg']); if (!\in_array($ext, $config['allowed_ext'])) {
error($config['error']['invalidimg']);
}
$file['is_an_image'] = true;
$file['width'] = $ret[0];
$file['height'] = $ret[1];
$file['mime'] = $ret[2];
} else {
// Is it a supported file?
if (!\in_array($file['extension'], $config['allowed_ext_files'])) {
// Should be 'invalid media' really.
error($config['error']['invalidimg']);
}
$file['is_an_image'] = false;
} }
} catch (\RuntimeException $e) {
$log = $ctx->get(LogDriver::class);
$log->log(LogDriver::ERROR, "Error while sniffing file '{$e->getMessage()}'");
error($config['error']['invalidimg']);
} }
// Truncate filename if it is too long // Truncate filename if it is too long