ILIAS  release_9 Revision v9.13-25-g2c18ec4c24f
class.QuestionFilesService.php
Go to the documentation of this file.
1 <?php
2 
3 declare(strict_types=1);
4 
5 namespace ILIAS\TestQuestionPool;
6 
8 {
9  protected static array $allowedImageMaterialFileExtensionsByMimeType = array(
10  'image/jpeg' => array('jpg', 'jpeg'),
11  'image/png' => array('png'),
12  'image/gif' => array('gif')
13  );
14 
18  public static function getAllowedImageMaterialFileExtensions(): array
19  {
20  $extensions = array();
21 
22  foreach (self::$allowedImageMaterialFileExtensionsByMimeType as $mimeType => $mimeExtensions) {
24  $extensions = array_merge($extensions, $mimeExtensions);
25  }
26  return array_unique($extensions);
27  }
28 
29  public const IMG_MIME_TYPE_JPG = 'image/jpeg';
30  public const IMG_MIME_TYPE_PNG = 'image/png';
31  public const IMG_MIME_TYPE_GIF = 'image/gif';
32 
33  protected static array $allowedFileExtensionsByMimeType = array(
34  self::IMG_MIME_TYPE_JPG => array('jpg', 'jpeg'),
35  self::IMG_MIME_TYPE_PNG => array('png'),
36  self::IMG_MIME_TYPE_GIF => array('gif')
37  );
38 
39  protected static array $allowedCharsetsByMimeType = array(
40  self::IMG_MIME_TYPE_JPG => array('binary'),
41  self::IMG_MIME_TYPE_PNG => array('binary'),
42  self::IMG_MIME_TYPE_GIF => array('binary')
43  );
44 
45  public function getAllowedFileExtensionsForMimeType(string $mimeType): array
46  {
47  foreach (self::$allowedFileExtensionsByMimeType as $allowedMimeType => $extensions) {
48  $rexCharsets = implode('|', self::$allowedCharsetsByMimeType[$allowedMimeType]);
49  $rexMimeType = preg_quote($allowedMimeType, '/');
50 
51  $rex = '/^' . $rexMimeType . '(;(\s)*charset=(' . $rexCharsets . '))*$/';
52 
53  if (!preg_match($rex, $mimeType)) {
54  continue;
55  }
56 
57  return $extensions;
58  }
59 
60  return array();
61  }
62 
63  public function isAllowedImageMimeType($mimeType): bool
64  {
65  return (bool) count($this->getAllowedFileExtensionsForMimeType($mimeType));
66  }
67 
68  public function isAllowedImageFileExtension(string $mimeType, string $fileExtension): bool
69  {
70  return in_array(strtolower($fileExtension), $this->getAllowedFileExtensionsForMimeType($mimeType), true);
71  }
72 
73  public function buildImagePath($questionId, $parentObjectId): string
74  {
75  return CLIENT_WEB_DIR . '/assessment/' . $parentObjectId . '/' . $questionId . '/images/';
76  }
77 }
isAllowedImageFileExtension(string $mimeType, string $fileExtension)
const CLIENT_WEB_DIR
Definition: constants.php:47
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...