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