ILIAS  release_10 Revision v10.1-43-ga1241a92c2f
class.ilCertificateTemplateImportAction.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
27 
32 {
37 
38  public function __construct(
39  private readonly int $objectId,
40  private readonly string $certificatePath,
41  private readonly ilCertificatePlaceholderDescription $placeholderDescriptionObject,
42  private readonly ilLogger $logger,
43  private readonly Filesystem $filesystem,
44  private readonly IRSS $irss,
45  ?ilCertificateTemplateRepository $templateRepository = null,
46  ?ilCertificateObjectHelper $objectHelper = null,
47  ?ilCertificateUtilHelper $utilHelper = null,
48  ?ilDBInterface $database = null,
49  ) {
50  global $DIC;
51  if (null === $database) {
52  $database = $DIC->database();
53  }
54 
55  if (null === $templateRepository) {
56  $templateRepository = new ilCertificateTemplateDatabaseRepository($database, $logger);
57  }
58  $this->templateRepository = $templateRepository;
59 
60  if (null === $objectHelper) {
61  $objectHelper = new ilCertificateObjectHelper();
62  }
63  $this->objectHelper = $objectHelper;
64 
65  if (null === $utilHelper) {
66  $utilHelper = new ilCertificateUtilHelper();
67  }
68  $this->utilHelper = $utilHelper;
69  $this->stakeholder = new ilCertificateTemplateStakeholder();
70  }
71 
79  public function import(
80  string $zipFile,
81  string $filename,
82  string $rootDir = CLIENT_WEB_DIR,
83  string $iliasVersion = ILIAS_VERSION_NUMERIC,
84  string $installationID = IL_INST_ID
85  ): bool {
86  $importPath = $this->createArchiveDirectory($installationID);
87 
88  $clean_up_import_dir = function () use (&$importPath) {
89  try {
90  if ($this->filesystem->hasDir($importPath)) {
91  $this->filesystem->deleteDir($importPath);
92  }
93  } catch (Throwable $e) {
94  $this->logger->error(sprintf("Can't clean up import directory: %s", $e->getMessage()));
95  $this->logger->error($e->getTraceAsString());
96  }
97  };
98 
99  $result = $this->utilHelper->moveUploadedFile($zipFile, $filename, $rootDir . $importPath . $filename);
100  if (!$result) {
101  $clean_up_import_dir();
102 
103  return false;
104  }
105 
106  $destination_dir = $rootDir . $importPath;
107  $unzip = $this->utilHelper->unzip(
108  $rootDir . $importPath . $filename,
109  $destination_dir,
110  true
111  );
112 
113  $unzipped = $unzip->extract();
114 
115  // Cleanup memory, otherwise there will be issues with NFS-based file systems after `listContents` has been called
116  unset($unzip);
117 
118  if (!$unzipped) {
119  $clean_up_import_dir();
120 
121  return false;
122  }
123 
124  if ($this->filesystem->has($importPath . $filename)) {
125  $this->filesystem->delete($importPath . $filename);
126  }
127 
128  $xmlFiles = 0;
129  $contents = $this->filesystem->listContents($importPath);
130  foreach ($contents as $file) {
131  if ($file->isFile() && str_contains($file->getPath(), '.xml')) {
132  $xmlFiles++;
133  }
134  }
135 
136  if (0 === $xmlFiles) {
137  return false;
138  }
139 
140  $certificate = $this->templateRepository->fetchCurrentlyUsedCertificate($this->objectId);
141 
142  $currentVersion = $certificate->getVersion();
143  $newVersion = $currentVersion + 1;
144  $xsl = $certificate->getCertificateContent();
145 
146  foreach ($contents as $file) {
147  if (!$file->isFile()) {
148  continue;
149  }
150 
151  if (str_contains($file->getPath(), '.xml')) {
152  $xsl = $this->filesystem->read($file->getPath());
153  // TODO: wth??
154  } elseif (str_contains($file->getPath(), '.jpg')) {
155  $background_rid = $this->irss->manage()->stream(
156  $this->filesystem->readStream($file->getPath()),
157  $this->stakeholder
158  );
159  } elseif (str_contains($file->getPath(), '.svg')) {
160  $card_thumbnail_rid = $this->irss->manage()->stream(
161  $this->filesystem->readStream($file->getPath()),
162  $this->stakeholder
163  );
164  }
165  }
166 
167  $jsonEncodedTemplateValues = json_encode(
168  $this->placeholderDescriptionObject->getPlaceholderDescriptions(),
169  JSON_THROW_ON_ERROR
170  );
171 
172  $newHashValue = hash(
173  'sha256',
174  implode('', [
175  $xsl,
176  isset($background_rid) ? $this->irss->manage()->getResource(
177  $background_rid
178  )->getStorageID() : '',
179  $jsonEncodedTemplateValues,
180  isset($card_thumbnail_rid) ? $this->irss->manage()->getResource(
181  $card_thumbnail_rid
182  )->getStorageID() : ''
183  ])
184  );
185 
186  $template = new ilCertificateTemplate(
187  $this->objectId,
188  $this->objectHelper->lookupType($this->objectId),
189  $xsl,
190  $newHashValue,
191  $jsonEncodedTemplateValues,
192  $newVersion,
193  $iliasVersion,
194  time(),
195  false,
196  '',
197  '',
198  isset($background_rid) ? $background_rid->serialize() : '',
199  isset($card_thumbnail_rid) ? $card_thumbnail_rid->serialize() : ''
200  );
201 
202  $this->templateRepository->save($template);
203 
204  $clean_up_import_dir();
205 
206  return true;
207  }
208 
214  private function createArchiveDirectory(string $installationID): string
215  {
216  $type = $this->objectHelper->lookupType($this->objectId);
217  $certificateId = $this->objectId;
218 
219  $dir = $this->certificatePath . time() . '__' . $installationID . '__' . $type . '__' . $certificateId . '__certificate/';
220  if ($this->filesystem->hasDir($dir)) {
221  $this->filesystem->deleteDir($dir);
222  }
223  $this->filesystem->createDir($dir);
224 
225  return $dir;
226  }
227 }
readonly ilCertificateTemplateStakeholder $stakeholder
__construct(private readonly int $objectId, private readonly string $certificatePath, private readonly ilCertificatePlaceholderDescription $placeholderDescriptionObject, private readonly ilLogger $logger, private readonly Filesystem $filesystem, private readonly IRSS $irss, ?ilCertificateTemplateRepository $templateRepository=null, ?ilCertificateObjectHelper $objectHelper=null, ?ilCertificateUtilHelper $utilHelper=null, ?ilDBInterface $database=null,)
const IL_INST_ID
Definition: constants.php:40
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
const ILIAS_VERSION_NUMERIC
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
global $DIC
Definition: shib_login.php:25
const CLIENT_WEB_DIR
Definition: constants.php:47
$filename
Definition: buildRTE.php:78
readonly ilCertificateTemplateRepository $templateRepository
createArchiveDirectory(string $installationID)
Creates a directory for a zip archive containing multiple certificates.