better validation for uploaded/imported image files

Signed-off-by: Michael Kaufmann <d00p@froxlor.org>
This commit is contained in:
Michael Kaufmann
2023-03-08 09:33:30 +01:00
parent c56e0b9dac
commit f36bc61fc7
3 changed files with 80 additions and 64 deletions

View File

@@ -334,4 +334,40 @@ class Validate
}
return false;
}
/**
* validates whether a given base64 string decodes to an image
*
* @param string $base64string
* @return bool
* @throws Exception
*/
public static function validateBase64Image(string $base64string) {
if (!extension_loaded('gd')) {
Response::standardError('phpgdextensionnotavailable', null, true);
}
// Decode the base64 string
$data = base64_decode($base64string);
// Create an image from the decoded data
$image = @imagecreatefromstring($data);
// Check if the image was created successfully
if (!$image) {
return false;
}
// Get the MIME type of the image
$mime = image_type_to_mime_type(getimagesizefromstring($data)[2]);
// Check if the MIME type is a valid image MIME type
if (strpos($mime, 'image/') !== 0) {
return false;
}
// If everything is okay, return true
return true;
}
}