ILIAS  release_7 Revision v7.30-3-g800a261c036
class.ilQtiMatImageSecurity.php
Go to the documentation of this file.
1<?php
2/* Copyright (c) 1998-2013 ILIAS open source, Extended GPL, see docs/LICENSE */
3
4require_once 'Modules/TestQuestionPool/classes/class.assQuestion.php';
5require_once 'Services/Utilities/classes/class.ilFileUtils.php';
6require_once 'Services/QTI/exceptions/class.ilQtiException.php';
7
15{
19 protected $imageMaterial;
20
25
27 {
28 $this->setImageMaterial($imageMaterial);
29
30 if (!strlen($this->getImageMaterial()->getRawContent())) {
31 throw new ilQtiException('cannot import image without content');
32 }
33
35 $this->determineMimeType($this->getImageMaterial()->getRawContent())
36 );
37 }
38
42 public function getImageMaterial()
43 {
45 }
46
51 {
52 $this->imageMaterial = $imageMaterial;
53 }
54
58 protected function getDetectedMimeType()
59 {
61 }
62
67 {
68 $this->detectedMimeType = $detectedMimeType;
69 }
70
71 public function validate()
72 {
73 if (!$this->validateLabel()) {
74 return false;
75 }
76
77 if (!$this->validateContent()) {
78 return false;
79 }
80
81 return true;
82 }
83
84 protected function validateContent()
85 {
86 if ($this->getImageMaterial()->getImagetype() && !assQuestion::isAllowedImageMimeType($this->getImageMaterial()->getImagetype())) {
87 return false;
88 }
89
91 return false;
92 }
93
94 if ($this->getImageMaterial()->getImagetype()) {
95 $declaredMimeType = assQuestion::fetchMimeTypeIdentifier($this->getImageMaterial()->getImagetype());
97
98 if ($declaredMimeType != $detectedMimeType) {
99 // since ilias exports jpeg declared pngs itself, we skip this validation ^^
100 // return false;
101
102 /* @var ilComponentLogger $log */
103 $log = $GLOBALS['DIC'] ? $GLOBALS['DIC']['ilLog'] : $GLOBALS['ilLog'];
104 $log->log(
105 'QPL: imported image with declared mime (' . $declaredMimeType . ') '
106 . 'and detected mime (' . $detectedMimeType . ')'
107 );
108 }
109 }
110
111 return true;
112 }
113
114 protected function validateLabel()
115 {
116 if ($this->getImageMaterial()->getUri()) {
117 if (!$this->hasFileExtension($this->getImageMaterial()->getUri())) {
118 return true;
119 }
120
121 $extension = $this->determineFileExtension($this->getImageMaterial()->getUri());
122 } else {
123 $extension = $this->determineFileExtension($this->getImageMaterial()->getLabel());
124 }
125
127 }
128
129 public function sanitizeLabel()
130 {
131 $label = $this->getImageMaterial()->getLabel();
132
133 $label = basename($label);
134 $label = ilUtil::stripSlashes($label);
135 $label = ilUtil::getASCIIFilename($label);
136
137 $this->getImageMaterial()->setLabel($label);
138 }
139
140 protected function determineMimeType($content)
141 {
142 return ilFileUtils::lookupContentMimeType($content);
143 }
144
150 protected function determineFileExtension($label)
151 {
152 $pathInfo = pathinfo($label);
153
154 if (isset($pathInfo['extension'])) {
155 return $pathInfo['extension'];
156 }
157
158 return null;
159 }
160
166 protected function hasFileExtension($label)
167 {
168 $pathInfo = pathinfo($label);
169
170 return array_key_exists('extension', $pathInfo);
171 }
172}
if(!defined('PATH_SEPARATOR')) $GLOBALS['_PEAR_default_error_mode']
Definition: PEAR.php:64
An exception for terminatinating execution or to throw for unit testing.
static isAllowedImageFileExtension($mimeType, $fileExtension)
static fetchMimeTypeIdentifier($contentTypeString)
static isAllowedImageMimeType($mimeType)
static lookupContentMimeType($content)
__construct(ilQTIMatimage $imageMaterial)
hasFileExtension($label)
Returns whether or not the passed label contains a file extension.
determineFileExtension($label)
Returns the determine file extension.
setDetectedMimeType($detectedMimeType)
static getASCIIFilename($a_filename)
convert utf8 to ascii filename
static stripSlashes($a_str, $a_strip_html=true, $a_allow="")
strip slashes if magic qoutes is enabled
$log
Definition: result.php:15