ILIAS  release_8 Revision v8.25-1-g13de6a5eca6
ilCertificateTemplateImportAction Class Reference
+ Collaboration diagram for ilCertificateTemplateImportAction:

Public Member Functions

 __construct (int $objectId, string $certificatePath, ilCertificatePlaceholderDescription $placeholderDescriptionObject, ilLogger $logger, Filesystem $filesystem, Filesystem $tmp_fs, ?ilCertificateTemplateRepository $templateRepository=null, ?ilCertificateObjectHelper $objectHelper=null, ?ilCertificateUtilHelper $utilHelper=null, ?ilDBInterface $database=null, ?ilCertificateBackgroundImageFileService $fileService=null, ?SVGBlacklistPreProcessor $svg_blacklist_processor=null)
 
 import (string $zipFile, string $filename, string $web_directory=CLIENT_WEB_DIR, string $storage_directory=CLIENT_DATA_DIR, string $iliasVerision=ILIAS_VERSION_NUMERIC, string $installationID=IL_INST_ID)
 

Private Member Functions

 createArchiveDirectory (string $installationId)
 
 createTemporaryArchiveDirectory (string $installationId)
 
 buildArchivePath (string $installationId)
 
 getBackgroundImageThumbnailPath ()
 

Private Attributes

int $objectId
 
string $certificatePath
 
ilCertificateTemplateRepository $templateRepository
 
ilCertificatePlaceholderDescription $placeholderDescriptionObject
 
ilLogger $logger
 
Filesystem $web_fs
 
Filesystem $tmp_fs
 
ilCertificateObjectHelper $objectHelper
 
ilCertificateUtilHelper $utilHelper
 
string $installationID
 
ilCertificateBackgroundImageFileService $fileService
 
SVGBlacklistPreProcessor $svg_blacklist_processor
 

Detailed Description

Constructor & Destructor Documentation

◆ __construct()

ilCertificateTemplateImportAction::__construct ( int  $objectId,
string  $certificatePath,
ilCertificatePlaceholderDescription  $placeholderDescriptionObject,
ilLogger  $logger,
Filesystem  $filesystem,
Filesystem  $tmp_fs,
?ilCertificateTemplateRepository  $templateRepository = null,
?ilCertificateObjectHelper  $objectHelper = null,
?ilCertificateUtilHelper  $utilHelper = null,
?ilDBInterface  $database = null,
?ilCertificateBackgroundImageFileService  $fileService = null,
?SVGBlacklistPreProcessor  $svg_blacklist_processor = null 
)

Definition at line 47 of file class.ilCertificateTemplateImportAction.php.

60 {
61 $this->objectId = $objectId;
62 $this->certificatePath = $certificatePath;
63
64 $this->logger = $logger;
65 if (null === $database) {
66 global $DIC;
67 $database = $DIC->database();
68 }
69
70 $this->web_fs = $filesystem;
71 $this->tmp_fs = $tmp_fs;
72
73 $this->placeholderDescriptionObject = $placeholderDescriptionObject;
74
75 if (null === $templateRepository) {
77 }
78 $this->templateRepository = $templateRepository;
79
80 if (null === $objectHelper) {
82 }
83 $this->objectHelper = $objectHelper;
84
85 if (null === $utilHelper) {
87 }
88 $this->utilHelper = $utilHelper;
89
90 if (null === $fileService) {
93 $filesystem
94 );
95 }
96 $this->fileService = $fileService;
97 if (null === $svg_blacklist_processor) {
99 }
100 $this->svg_blacklist_processor = $svg_blacklist_processor;
101 }
ilCertificateBackgroundImageFileService $fileService
ilCertificatePlaceholderDescription $placeholderDescriptionObject
Just a wrapper class to create Unit Test for other classes.
global $DIC
Definition: feed.php:28

References $certificatePath, $DIC, $fileService, $logger, $objectHelper, $objectId, $placeholderDescriptionObject, $svg_blacklist_processor, $templateRepository, $tmp_fs, $utilHelper, and ILIAS\Repository\logger().

+ Here is the call graph for this function:

Member Function Documentation

◆ buildArchivePath()

ilCertificateTemplateImportAction::buildArchivePath ( string  $installationId)
private

Definition at line 380 of file class.ilCertificateTemplateImportAction.php.

380 : string
381 {
382 $seperator = '__';
383 $type = $this->objectHelper->lookupType($this->objectId);
384
385 return implode($seperator, [
386 $this->certificatePath . time(),
387 $installationId,
388 $type,
389 $this->objectId,
390 'certificate/'
391 ]);
392 }
$type

References $type.

Referenced by createArchiveDirectory(), and createTemporaryArchiveDirectory().

+ Here is the caller graph for this function:

◆ createArchiveDirectory()

ilCertificateTemplateImportAction::createArchiveDirectory ( string  $installationId)
private
Exceptions
IOException

Definition at line 353 of file class.ilCertificateTemplateImportAction.php.

353 : string
354 {
355 $dir = $this->buildArchivePath($installationId);
356
357 if ($this->web_fs->hasDir($dir)) {
358 $this->web_fs->deleteDir($dir);
359 }
360 $this->web_fs->createDir($dir);
361
362 return $dir;
363 }

References buildArchivePath().

Referenced by import().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ createTemporaryArchiveDirectory()

ilCertificateTemplateImportAction::createTemporaryArchiveDirectory ( string  $installationId)
private
Exceptions
IOException

Definition at line 368 of file class.ilCertificateTemplateImportAction.php.

368 : string
369 {
370 $dir = $this->buildArchivePath($installationId);
371
372 if ($this->tmp_fs->hasDir($dir)) {
373 $this->tmp_fs->deleteDir($dir);
374 }
375 $this->tmp_fs->createDir($dir);
376
377 return $dir;
378 }

References buildArchivePath().

Referenced by import().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ getBackgroundImageThumbnailPath()

ilCertificateTemplateImportAction::getBackgroundImageThumbnailPath ( )
private

Definition at line 394 of file class.ilCertificateTemplateImportAction.php.

394 : string
395 {
396 return $this->certificatePath . 'background.jpg.thumb.jpg';
397 }

Referenced by import().

+ Here is the caller graph for this function:

◆ import()

ilCertificateTemplateImportAction::import ( string  $zipFile,
string  $filename,
string  $web_directory = CLIENT_WEB_DIR,
string  $storage_directory = CLIENT_DATA_DIR,
string  $iliasVerision = ILIAS_VERSION_NUMERIC,
string  $installationID = IL_INST_ID 
)
Parameters
string$zipFile
string$filename
string$web_directory
string$iliasVerision
string | false$installationID
Returns
bool
Exceptions
FileAlreadyExistsException
FileNotFoundException
IOException
ilDatabaseException
ilException

Definition at line 116 of file class.ilCertificateTemplateImportAction.php.

123 : bool {
124 $rel_tmp_import_path = $this->createTemporaryArchiveDirectory($installationID);
125 $abs_tmp_directory = rtrim($storage_directory, '/') . '/temp/';
126 $rel_import_path = $this->createArchiveDirectory($installationID);
127
128 $clean_up_import_dir = function () use (&$rel_tmp_import_path, &$rel_import_path): void {
129 try {
130 if ($this->tmp_fs->hasDir($rel_tmp_import_path)) {
131 $this->tmp_fs->deleteDir($rel_tmp_import_path);
132 }
133 } catch (Throwable $e) {
134 $this->logger->error(sprintf("Can't clean up temporary import directory: %s", $e->getMessage()));
135 $this->logger->error($e->getTraceAsString());
136 }
137
138 try {
139 if ($this->web_fs->hasDir($rel_import_path)) {
140 $this->web_fs->deleteDir($rel_import_path);
141 }
142 } catch (Throwable $e) {
143 $this->logger->error(sprintf("Can't clean up import directory: %s", $e->getMessage()));
144 $this->logger->error($e->getTraceAsString());
145 }
146 };
147
148 try {
149 $abs_zip_path = $abs_tmp_directory . $rel_tmp_import_path . $filename;
150 $result = $this->utilHelper->moveUploadedFile(
151 $zipFile,
152 $filename,
153 $abs_zip_path
154 );
155
156 if (!$result) {
157 return false;
158 }
159
160 $this->utilHelper->unzip(
161 $abs_tmp_directory . $rel_tmp_import_path . $filename,
162 true
163 );
164
165 $abs_unzip_destination_dir = $abs_tmp_directory . $rel_tmp_import_path;
166 $sub_directory = str_replace('.zip', '', strtolower($filename)) . '/';
167 $abs_sub_directory_path = $abs_tmp_directory . $rel_tmp_import_path . $sub_directory;
168 if (is_dir($abs_sub_directory_path)) {
169 $abs_target_directory = $abs_sub_directory_path;
170 }
171
172 $this->utilHelper->renameExecutables($abs_sub_directory_path);
173
174 if ($this->tmp_fs->has($rel_tmp_import_path . $filename)) {
175 $this->tmp_fs->delete($rel_tmp_import_path . $filename);
176 }
177
178 $tmp_contents = $this->tmp_fs->listContents($rel_tmp_import_path, true);
179 foreach ($tmp_contents as $file) {
180 if (!$file->isFile()) {
181 continue;
182 }
183
184 if (!$this->web_fs->has($rel_import_path . basename($file->getPath()))) {
185 $this->web_fs->writeStream(
186 $rel_import_path . basename($file->getPath()),
187 $this->tmp_fs->readStream($file->getPath())
188 );
189 }
190 }
191
192 $num_background_images = 0;
193 $num_tile_images = 0;
194 $num_xml_files = 0;
195 $contents = $this->web_fs->listContents($rel_import_path);
196 foreach ($contents as $file) {
197 if (!$file->isFile()) {
198 continue;
199 }
200
201 if (strpos($file->getPath(), '.xml') !== false) {
202 ++$num_xml_files;
203 }
204
205 if (strpos($file->getPath(), '.svg') !== false) {
206 $stream = $this->web_fs->readStream($file->getPath());
207 $file_metadata = $stream->getMetadata();
208 $absolute_file_path = $file_metadata['uri'];
209
210 $metadata = new Metadata(
211 pathinfo($absolute_file_path)['basename'],
212 filesize($absolute_file_path),
213 mime_content_type($absolute_file_path)
214 );
215
216 ++$num_tile_images;
217
218 $processing_result = $this->svg_blacklist_processor->process($stream, $metadata);
219 if ($processing_result->getCode() !== ProcessingStatus::OK) {
220 return false;
221 }
222 }
223
224 if (str_contains($file->getPath(), '.jpg')) {
225 ++$num_background_images;
226 }
227 }
228
229 if (0 === $num_xml_files) {
230 $this->logger->error('No XML file found in the imported zip file');
231 return false;
232 }
233 if ($num_background_images > 1) {
234 $this->logger->error('More than one background image found in the imported zip file');
235 return false;
236 }
237 if ($num_tile_images > 1) {
238 $this->logger->error('More than one tile image found in the imported zip file');
239 return false;
240 }
241
242 $certificate = $this->templateRepository->fetchCurrentlyUsedCertificate($this->objectId);
243
244 $currentVersion = $certificate->getVersion();
245 $newVersion = $currentVersion + 1;
246 $backgroundImagePath = $certificate->getBackgroundImagePath();
247 $cardThumbnailImagePath = $certificate->getThumbnailImagePath();
248
249 $xsl = $certificate->getCertificateContent();
250
251 foreach ($contents as $file) {
252 if (!$file->isFile()) {
253 continue;
254 }
255
256 if (strpos($file->getPath(), '.xml') !== false) {
257 $xsl = $this->web_fs->read($file->getPath());
258 // as long as we cannot make RPC calls in a given directory, we have
259 // to add the complete path to every url
260 $xsl = preg_replace_callback(
261 "/url\‍([']{0,1}(.*?)[']{0,1}\‍)/",
262 function (array $matches) use ($web_directory): string {
263 $basePath = rtrim(
264 dirname($this->fileService->getBackgroundImageDirectory($web_directory)),
265 '/'
266 );
267 $fileName = basename($matches[1]);
268
269 if ('[BACKGROUND_IMAGE]' === $fileName) {
270 $basePath = '';
271 } elseif ($basePath !== '') {
272 $basePath .= '/';
273 }
274
275 return 'url(' . $basePath . $fileName . ')';
276 },
277 $xsl
278 );
279 } elseif (strpos($file->getPath(), '.jpg') !== false) {
280 $newBackgroundImageName = 'background_' . $newVersion . '.jpg';
281 $newPath = $this->certificatePath . $newBackgroundImageName;
282 $this->web_fs->copy($file->getPath(), $newPath);
283
284 $backgroundImagePath = $this->certificatePath . $newBackgroundImageName;
285 // upload of the background image, create a thumbnail
286
287 $backgroundImageThumbPath = $this->getBackgroundImageThumbnailPath();
288
289 $thumbnailImagePath = $web_directory . $backgroundImageThumbPath;
290
291 $originalImagePath = $web_directory . $newPath;
292 $this->utilHelper->convertImage(
293 $originalImagePath,
294 $thumbnailImagePath,
295 'JPEG',
296 "100"
297 );
298 } elseif (strpos($file->getPath(), '.svg') !== false) {
299 $newCardThumbnailName = 'thumbnail_' . $newVersion . '.svg';
300 $newPath = $this->certificatePath . $newCardThumbnailName;
301
302 $this->web_fs->copy($file->getPath(), $newPath);
303
304 $cardThumbnailImagePath = $this->certificatePath . $newCardThumbnailName;
305 }
306 }
307
308 $jsonEncodedTemplateValues = json_encode(
309 $this->placeholderDescriptionObject->getPlaceholderDescriptions(),
310 JSON_THROW_ON_ERROR
311 );
312
313 $newHashValue = hash(
314 'sha256',
315 implode('', [
316 $xsl,
317 $backgroundImagePath,
318 $jsonEncodedTemplateValues,
319 $cardThumbnailImagePath
320 ])
321 );
322
323 $template = new ilCertificateTemplate(
324 $this->objectId,
325 $this->objectHelper->lookupType($this->objectId),
326 $xsl,
327 $newHashValue,
328 $jsonEncodedTemplateValues,
329 $newVersion,
330 $iliasVerision,
331 time(),
332 true,
333 $backgroundImagePath,
334 $cardThumbnailImagePath
335 );
336
337 $this->templateRepository->save($template);
338
339 return true;
340 } catch (Throwable $e) {
341 $this->logger->error(sprintf('Error during certificate import: %s', $e->getMessage()));
342 $this->logger->error($e->getTraceAsString());
343
344 return false;
345 } finally {
346 $clean_up_import_dir();
347 }
348 }
$filename
Definition: buildRTE.php:78

References Vendor\Package\$e, $filename, createArchiveDirectory(), createTemporaryArchiveDirectory(), getBackgroundImageThumbnailPath(), and ILIAS\Repository\logger().

+ Here is the call graph for this function:

Field Documentation

◆ $certificatePath

string ilCertificateTemplateImportAction::$certificatePath
private

Definition at line 35 of file class.ilCertificateTemplateImportAction.php.

Referenced by __construct().

◆ $fileService

ilCertificateBackgroundImageFileService ilCertificateTemplateImportAction::$fileService
private

Definition at line 44 of file class.ilCertificateTemplateImportAction.php.

Referenced by __construct().

◆ $installationID

string ilCertificateTemplateImportAction::$installationID
private

Definition at line 43 of file class.ilCertificateTemplateImportAction.php.

◆ $logger

ilLogger ilCertificateTemplateImportAction::$logger
private

Definition at line 38 of file class.ilCertificateTemplateImportAction.php.

Referenced by __construct().

◆ $objectHelper

ilCertificateObjectHelper ilCertificateTemplateImportAction::$objectHelper
private

Definition at line 41 of file class.ilCertificateTemplateImportAction.php.

Referenced by __construct().

◆ $objectId

int ilCertificateTemplateImportAction::$objectId
private

Definition at line 34 of file class.ilCertificateTemplateImportAction.php.

Referenced by __construct().

◆ $placeholderDescriptionObject

ilCertificatePlaceholderDescription ilCertificateTemplateImportAction::$placeholderDescriptionObject
private

Definition at line 37 of file class.ilCertificateTemplateImportAction.php.

Referenced by __construct().

◆ $svg_blacklist_processor

SVGBlacklistPreProcessor ilCertificateTemplateImportAction::$svg_blacklist_processor
private

Definition at line 45 of file class.ilCertificateTemplateImportAction.php.

Referenced by __construct().

◆ $templateRepository

ilCertificateTemplateRepository ilCertificateTemplateImportAction::$templateRepository
private

Definition at line 36 of file class.ilCertificateTemplateImportAction.php.

Referenced by __construct().

◆ $tmp_fs

Filesystem ilCertificateTemplateImportAction::$tmp_fs
private

Definition at line 40 of file class.ilCertificateTemplateImportAction.php.

Referenced by __construct().

◆ $utilHelper

ilCertificateUtilHelper ilCertificateTemplateImportAction::$utilHelper
private

Definition at line 42 of file class.ilCertificateTemplateImportAction.php.

Referenced by __construct().

◆ $web_fs

Filesystem ilCertificateTemplateImportAction::$web_fs
private

Definition at line 39 of file class.ilCertificateTemplateImportAction.php.


The documentation for this class was generated from the following file: