diff options
| author | Jan-Hendrik Willms <tleilax+github@gmail.com> | 2023-12-20 09:53:37 +0100 |
|---|---|---|
| committer | Jan-Hendrik Willms <tleilax+github@gmail.com> | 2024-04-08 14:26:27 +0200 |
| commit | 8142b3f34ba92644acbcac8f0ec974316db80e10 (patch) | |
| tree | e5b85b8b5dc74f17d8dc2fa863018c12b7553377 /lib/classes/Services/ImageValidator.php | |
| parent | bcd76d3c5ac715a33668d4c6611b7be8f11e3404 (diff) | |
implement use image validator service, fixes #3593tic-3593
Diffstat (limited to 'lib/classes/Services/ImageValidator.php')
| -rw-r--r-- | lib/classes/Services/ImageValidator.php | 51 |
1 files changed, 51 insertions, 0 deletions
diff --git a/lib/classes/Services/ImageValidator.php b/lib/classes/Services/ImageValidator.php new file mode 100644 index 0000000..c26377c --- /dev/null +++ b/lib/classes/Services/ImageValidator.php @@ -0,0 +1,51 @@ +<?php +namespace Studip\Services; + +final class ImageValidator +{ + public const VALID_EXTENSIONS = [ + 'gif', + 'jpeg', 'jpg', + 'png', + 'webp', + ]; + + public const VALID_MIMETYPES = [ + 'image/gif', + 'image/jpeg', + 'image/png', + 'image/webp', + ]; + + public function validate(string $filename): bool + { + return $this->validateName($filename) + && $this->validateMimeType(get_mime_type($filename)) + && $this->validateContents($filename); + } + + public function validateMimeType(string $mime_type): bool + { + return str_starts_with($mime_type, 'image/') + && in_array($mime_type, self::VALID_MIMETYPES); + } + + public function validateName(string $filename): bool + { + $extension = pathinfo($filename, PATHINFO_EXTENSION); + $extension = strtolower($extension); + return in_array($extension, self::VALID_EXTENSIONS); + } + + public function validateContents(string $filename): bool + { + $check = imagecreatefromstring(file_get_contents($filename)); + if ($check === false) { + return false; + } + + imagedestroy($check); + + return true; + } +} |
