ILIAS  release_8 Revision v8.24
class.ilCertificateTemplateImportAction.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
28
33{
34 private int $objectId;
35 private string $certificatePath;
42 private string $installationID;
45
46 public function __construct(
47 int $objectId,
48 string $certificatePath,
55 ?ilDBInterface $database = null,
58 ) {
59 $this->objectId = $objectId;
60 $this->certificatePath = $certificatePath;
61
62 $this->logger = $logger;
63 if (null === $database) {
64 global $DIC;
65 $database = $DIC->database();
66 }
67
68 $this->filesystem = $filesystem;
69
70 $this->placeholderDescriptionObject = $placeholderDescriptionObject;
71
72 if (null === $templateRepository) {
74 }
75 $this->templateRepository = $templateRepository;
76
77 if (null === $objectHelper) {
79 }
80 $this->objectHelper = $objectHelper;
81
82 if (null === $utilHelper) {
84 }
85 $this->utilHelper = $utilHelper;
86
87 if (null === $fileService) {
91 );
92 }
93 $this->fileService = $fileService;
94 if (null === $svg_blacklist_processor) {
96 }
97 $this->svg_blacklist_processor = $svg_blacklist_processor;
98 }
99
113 public function import(
114 string $zipFile,
115 string $filename,
116 string $rootDir = CLIENT_WEB_DIR,
117 string $iliasVerision = ILIAS_VERSION_NUMERIC,
119 ): bool {
120 $importPath = $this->createArchiveDirectory($installationID);
121
122 try {
123 $result = $this->utilHelper->moveUploadedFile($zipFile, $filename, $rootDir . $importPath . $filename);
124
125 if (!$result) {
126 return false;
127 }
128
129 $this->utilHelper->unzip(
130 $rootDir . $importPath . $filename,
131 true
132 );
133
134 $subDirectoryName = str_replace('.zip', '', strtolower($filename)) . '/';
135 $subDirectoryAbsolutePath = $rootDir . $importPath . $subDirectoryName;
136
137 $copyDirectory = $rootDir . $importPath;
138 if (is_dir($subDirectoryAbsolutePath)) {
139 $copyDirectory = $subDirectoryAbsolutePath;
140 }
141
142 $directoryInformation = $this->utilHelper->getDir($copyDirectory);
143
144 $xmlFiles = 0;
145 foreach ($directoryInformation as $file) {
146 if (strcmp($file['type'], 'file') === 0) {
147 if (strpos($file['entry'], '.xml') !== false) {
148 $xmlFiles++;
149 }
150
151 if (strpos($file['entry'], '.svg') !== false) {
152 $filePath = $importPath . $file['entry'];
153
154 $stream = $this->filesystem->readStream($filePath);
155 $file_metadata = $stream->getMetadata();
156 $absolute_file_path = $file_metadata['uri'];
157
158 $metadata = new Metadata(
159 pathinfo($absolute_file_path)['basename'],
160 filesize($absolute_file_path),
161 mime_content_type($absolute_file_path)
162 );
163
164 $processing_result = $this->svg_blacklist_processor->process($stream, $metadata);
165 if ($processing_result->getCode() !== ProcessingStatus::OK) {
166 return false;
167 }
168 }
169 }
170 }
171
172 if (0 === $xmlFiles) {
173 return false;
174 }
175
176 $certificate = $this->templateRepository->fetchCurrentlyUsedCertificate($this->objectId);
177
178 $currentVersion = $certificate->getVersion();
179 $newVersion = $currentVersion + 1;
180 $backgroundImagePath = $certificate->getBackgroundImagePath();
181 $cardThumbnailImagePath = $certificate->getThumbnailImagePath();
182
183 $xsl = $certificate->getCertificateContent();
184
185 foreach ($directoryInformation as $file) {
186 if (strcmp($file['type'], 'file') === 0) {
187 $filePath = $importPath . $file['entry'];
188 if (strpos($file['entry'], '.xml') !== false) {
189 $xsl = $this->filesystem->read($filePath);
190 // as long as we cannot make RPC calls in a given directory, we have
191 // to add the complete path to every url
192 $xsl = preg_replace_callback(
193 "/url\‍([']{0,1}(.*?)[']{0,1}\‍)/",
194 function (array $matches) use ($rootDir): string {
195 $basePath = rtrim(
196 dirname($this->fileService->getBackgroundImageDirectory($rootDir)),
197 '/'
198 );
199 $fileName = basename($matches[1]);
200
201 if ('[BACKGROUND_IMAGE]' === $fileName) {
202 $basePath = '';
203 } elseif ($basePath !== '') {
204 $basePath .= '/';
205 }
206
207 return 'url(' . $basePath . $fileName . ')';
208 },
209 $xsl
210 );
211 } elseif (strpos($file['entry'], '.jpg') !== false) {
212 $newBackgroundImageName = 'background_' . $newVersion . '.jpg';
213 $newPath = $this->certificatePath . $newBackgroundImageName;
214 $this->filesystem->copy($filePath, $newPath);
215
216 $backgroundImagePath = $this->certificatePath . $newBackgroundImageName;
217 // upload of the background image, create a thumbnail
218
219 $backgroundImageThumbPath = $this->getBackgroundImageThumbnailPath();
220
221 $thumbnailImagePath = $rootDir . $backgroundImageThumbPath;
222
223 $originalImagePath = $rootDir . $newPath;
224 $this->utilHelper->convertImage(
225 $originalImagePath,
226 $thumbnailImagePath,
227 'JPEG',
228 "100"
229 );
230 } elseif (strpos($file['entry'], '.svg') !== false) {
231 $newCardThumbnailName = 'thumbnail_' . $newVersion . '.svg';
232 $newPath = $this->certificatePath . $newCardThumbnailName;
233
234 $this->filesystem->copy($filePath, $newPath);
235
236 $cardThumbnailImagePath = $this->certificatePath . $newCardThumbnailName;
237 }
238 }
239 }
240
241 $jsonEncodedTemplateValues = json_encode(
242 $this->placeholderDescriptionObject->getPlaceholderDescriptions(),
243 JSON_THROW_ON_ERROR
244 );
245
246 $newHashValue = hash(
247 'sha256',
248 implode('', [
249 $xsl,
250 $backgroundImagePath,
251 $jsonEncodedTemplateValues,
252 $cardThumbnailImagePath
253 ])
254 );
255
256 $template = new ilCertificateTemplate(
257 $this->objectId,
258 $this->objectHelper->lookupType($this->objectId),
259 $xsl,
260 $newHashValue,
261 $jsonEncodedTemplateValues,
262 $newVersion,
263 $iliasVerision,
264 time(),
265 true,
266 $backgroundImagePath,
267 $cardThumbnailImagePath
268 );
269
270 $this->templateRepository->save($template);
271
272 return true;
273 } catch (Throwable $e) {
274 $this->logger->error(sprintf('Error during certificate import: %s', $e->getMessage()));
275 $this->logger->error($e->getTraceAsString());
276
277 return false;
278 } finally {
279 $this->filesystem->deleteDir($importPath);
280 }
281 }
282
289 private function createArchiveDirectory(string $installationID): string
290 {
291 $type = $this->objectHelper->lookupType($this->objectId);
292 $certificateId = $this->objectId;
293
294 $dir = $this->certificatePath . time() . '__' . $installationID . '__' . $type . '__' . $certificateId . '__certificate/';
295 $this->filesystem->createDir($dir);
296
297 return $dir;
298 }
299
300 private function getBackgroundImageThumbnailPath(): string
301 {
302 return $this->certificatePath . 'background.jpg.thumb.jpg';
303 }
304}
$filename
Definition: buildRTE.php:78
ilCertificateBackgroundImageFileService $fileService
ilCertificatePlaceholderDescription $placeholderDescriptionObject
__construct(int $objectId, string $certificatePath, ilCertificatePlaceholderDescription $placeholderDescriptionObject, ilLogger $logger, Filesystem $filesystem, ?ilCertificateTemplateRepository $templateRepository=null, ?ilCertificateObjectHelper $objectHelper=null, ?ilCertificateUtilHelper $utilHelper=null, ?ilDBInterface $database=null, ?ilCertificateBackgroundImageFileService $fileService=null, ?SVGBlacklistPreProcessor $svg_blacklist_processor=null)
createArchiveDirectory(string $installationID)
Creates a directory for a zip archive containing multiple certificates.
Just a wrapper class to create Unit Test for other classes.
Component logger with individual log levels by component id.
const CLIENT_WEB_DIR
Definition: constants.php:47
const IL_INST_ID
Definition: constants.php:40
global $DIC
Definition: feed.php:28
const ILIAS_VERSION_NUMERIC
Interface Filesystem.
Definition: Filesystem.php:40
Interface ilDBInterface.
Class FlySystemFileAccessTest \Provider\FlySystem @runTestsInSeparateProcesses @preserveGlobalState d...
$type