ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
class.ilQtiMatImageSecurity.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
22
30{
31 protected string $detectedMimeType = "";
32
33 public function __construct(
34 protected ilQTIMatimage $image_material,
35 private QuestionFiles $question_files
36 ) {
37 if (!strlen($this->getImageMaterial()->getRawContent())) {
38 throw new ilQtiException('cannot import image without content');
39 }
40
42 $this->determineMimeType($this->getImageMaterial()->getRawContent())
43 );
44 }
45
47 {
48 return $this->image_material;
49 }
50
51 public function setImageMaterial(ilQTIMatimage $image_material): void
52 {
53 $this->image_material = $image_material;
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() && !$this->question_files->isAllowedImageMimeType($this->getImageMaterial()->getImagetype())) {
82 return false;
83 }
84
85 if (!$this->question_files->isAllowedImageMimeType($this->getDetectedMimeType())) {
86 return false;
87 }
88
89 if ($this->getImageMaterial()->getImagetype()) {
90 $declaredMimeType = current(explode(';', $this->getImageMaterial()->getImagetype()));
91 $detectedMimeType = current(explode(';', $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
121 return $this->question_files->isAllowedImageFileExtension($this->getDetectedMimeType(), $extension);
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 getASCIIFilename(string $a_filename)
setImageMaterial(ilQTIMatimage $image_material)
__construct(protected ilQTIMatimage $image_material, private QuestionFiles $question_files)
setDetectedMimeType(string $detectedMimeType)
static stripSlashes(string $a_str, bool $a_strip_html=true, string $a_allow="")
$log
Definition: ltiresult.php:34
$GLOBALS["DIC"]
Definition: wac.php:54