ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
QuestionFiles.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
22
24{
25 protected static array $allowedImageMaterialFileExtensionsByMimeType = array(
26 'image/jpeg' => array('jpg', 'jpeg'),
27 'image/png' => array('png'),
28 'image/gif' => array('gif')
29 );
30
34 public static function getAllowedImageMaterialFileExtensions(): array
35 {
36 $extensions = array();
37
38 foreach (self::$allowedImageMaterialFileExtensionsByMimeType as $mimeType => $mimeExtensions) {
40 $extensions = array_merge($extensions, $mimeExtensions);
41 }
42 return array_unique($extensions);
43 }
44
45 public const IMG_MIME_TYPE_JPG = 'image/jpeg';
46 public const IMG_MIME_TYPE_PNG = 'image/png';
47 public const IMG_MIME_TYPE_GIF = 'image/gif';
48
49 protected static array $allowedFileExtensionsByMimeType = array(
50 self::IMG_MIME_TYPE_JPG => array('jpg', 'jpeg'),
51 self::IMG_MIME_TYPE_PNG => array('png'),
52 self::IMG_MIME_TYPE_GIF => array('gif')
53 );
54
55 protected static array $allowedCharsetsByMimeType = array(
56 self::IMG_MIME_TYPE_JPG => array('binary'),
57 self::IMG_MIME_TYPE_PNG => array('binary'),
58 self::IMG_MIME_TYPE_GIF => array('binary')
59 );
60
61 public function getAllowedFileExtensionsForMimeType(string $mimeType): array
62 {
63 foreach (self::$allowedFileExtensionsByMimeType as $allowedMimeType => $extensions) {
64 $rexCharsets = implode('|', self::$allowedCharsetsByMimeType[$allowedMimeType]);
65 $rexMimeType = preg_quote($allowedMimeType, '/');
66
67 $rex = '/^' . $rexMimeType . '(;(\s)*charset=(' . $rexCharsets . '))*$/';
68
69 if (!preg_match($rex, $mimeType)) {
70 continue;
71 }
72
73 return $extensions;
74 }
75
76 return array();
77 }
78
79 public function isAllowedImageMimeType($mimeType): bool
80 {
81 return (bool) count($this->getAllowedFileExtensionsForMimeType($mimeType));
82 }
83
84 public function isAllowedImageFileExtension(string $mimeType, string $fileExtension): bool
85 {
86 return in_array(strtolower($fileExtension), $this->getAllowedFileExtensionsForMimeType($mimeType), true);
87 }
88
89 public function buildImagePath($questionId, $parentObjectId): string
90 {
91 return CLIENT_WEB_DIR . '/assessment/' . $parentObjectId . '/' . $questionId . '/images/';
92 }
93}
isAllowedImageFileExtension(string $mimeType, string $fileExtension)
const CLIENT_WEB_DIR
Definition: constants.php:47