ILIAS  release_8 Revision v8.19
All Data Structures Namespaces Files Functions Variables Modules Pages
class.ilQtiMatImageSecurity.php
Go to the documentation of this file.
1 <?php
2 
3 declare(strict_types=1);
4 
29 {
31  protected string $detectedMimeType = "";
32 
33  public function __construct(ilQTIMatimage $imageMaterial)
34  {
35  $this->setImageMaterial($imageMaterial);
36 
37  if (!strlen($this->getImageMaterial()->getRawContent())) {
38  throw new ilQtiException('cannot import image without content');
39  }
40 
41  $this->setDetectedMimeType(
42  $this->determineMimeType($this->getImageMaterial()->getRawContent())
43  );
44  }
45 
46  public function getImageMaterial(): ilQTIMatimage
47  {
48  return $this->imageMaterial;
49  }
50 
51  public function setImageMaterial(ilQTIMatimage $imageMaterial): void
52  {
53  $this->imageMaterial = $imageMaterial;
54  }
55 
56  protected function getDetectedMimeType(): string
57  {
59  }
60 
61  protected function setDetectedMimeType(string $detectedMimeType): void
62  {
63  $this->detectedMimeType = $detectedMimeType;
64  }
65 
66  public function validate(): bool
67  {
68  if (!$this->validateLabel()) {
69  return false;
70  }
71 
72  if (!$this->validateContent()) {
73  return false;
74  }
75 
76  return true;
77  }
78 
79  protected function validateContent(): bool
80  {
81  if ($this->getImageMaterial()->getImagetype() && !assQuestion::isAllowedImageMimeType($this->getImageMaterial()->getImagetype())) {
82  return false;
83  }
84 
86  return false;
87  }
88 
89  if ($this->getImageMaterial()->getImagetype()) {
90  $declaredMimeType = assQuestion::fetchMimeTypeIdentifier($this->getImageMaterial()->getImagetype());
91  $detectedMimeType = assQuestion::fetchMimeTypeIdentifier($this->getDetectedMimeType());
92 
93  if ($declaredMimeType != $detectedMimeType) {
94  // since ilias exports jpeg declared pngs itself, we skip this validation ^^
95  // return false;
96 
97  /* @var ilComponentLogger $log */
98  $log = $GLOBALS['DIC'] ? $GLOBALS['DIC']['ilLog'] : $GLOBALS['ilLog'];
99  $log->log(
100  'QPL: imported image with declared mime (' . $declaredMimeType . ') '
101  . 'and detected mime (' . $detectedMimeType . ')'
102  );
103  }
104  }
105 
106  return true;
107  }
108 
109  protected function validateLabel(): bool
110  {
111  if ($this->getImageMaterial()->getUri()) {
112  if (!$this->hasFileExtension($this->getImageMaterial()->getUri())) {
113  return true;
114  }
115 
116  $extension = $this->determineFileExtension($this->getImageMaterial()->getUri());
117  } else {
118  $extension = $this->determineFileExtension($this->getImageMaterial()->getLabel());
119  }
120 
122  }
123 
124  public function sanitizeLabel(): void
125  {
126  $label = $this->getImageMaterial()->getLabel();
127 
128  $label = basename($label);
129  $label = ilUtil::stripSlashes($label);
130  $label = ilFileUtils::getASCIIFilename($label);
131 
132  $this->getImageMaterial()->setLabel($label);
133  }
134 
135  protected function determineMimeType(?string $content): string
136  {
137  $finfo = new finfo(FILEINFO_MIME);
138 
139  return $finfo->buffer($content);
140  }
141 
142  protected function determineFileExtension(string $label): ?string
143  {
144  $pathInfo = pathinfo($label);
145 
146  if (isset($pathInfo['extension'])) {
147  return $pathInfo['extension'];
148  }
149 
150  return null;
151  }
152 
153  protected function hasFileExtension(string $label): bool
154  {
155  $pathInfo = pathinfo($label);
156 
157  return array_key_exists('extension', $pathInfo);
158  }
159 }
static fetchMimeTypeIdentifier(string $contentType)
static stripSlashes(string $a_str, bool $a_strip_html=true, string $a_allow="")
static isAllowedImageFileExtension(string $mimeType, string $fileExtension)
setImageMaterial(ilQTIMatimage $imageMaterial)
static getASCIIFilename(string $a_filename)
static isAllowedImageMimeType($mimeType)
$log
Definition: result.php:33
if(!defined('PATH_SEPARATOR')) $GLOBALS['_PEAR_default_error_mode']
Definition: PEAR.php:64
setDetectedMimeType(string $detectedMimeType)
__construct(ilQTIMatimage $imageMaterial)