ILIAS  trunk Revision v11.0_alpha-1715-g7fc467680fb
All Data Structures Namespaces Files Functions Variables Enumerations Enumerator Modules Pages
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  } elseif (str_contains($file->getPath(), '.jpg')) {
154  $background_rid = $this->irss->manage()->stream(
155  $this->filesystem->readStream($file->getPath()),
156  $this->stakeholder
157  );
158  } elseif (str_contains($file->getPath(), '.svg')) {
159  $tile_image_rid = $this->irss->manage()->stream(
160  $this->filesystem->readStream($file->getPath()),
161  $this->stakeholder
162  );
163  }
164  }
165 
166  $jsonEncodedTemplateValues = json_encode(
167  $this->placeholderDescriptionObject->getPlaceholderDescriptions(),
168  JSON_THROW_ON_ERROR
169  );
170 
171  $newHashValue = hash(
172  'sha256',
173  implode('', [
174  $xsl,
175  isset($background_rid) ? $this->irss->manage()->getResource(
176  $background_rid
177  )->getStorageID() : '',
178  $jsonEncodedTemplateValues,
179  isset($tile_image_rid) ? $this->irss->manage()->getResource(
180  $tile_image_rid
181  )->getStorageID() : ''
182  ])
183  );
184 
185  $template = new ilCertificateTemplate(
186  $this->objectId,
187  $this->objectHelper->lookupType($this->objectId),
188  $xsl,
189  $newHashValue,
190  $jsonEncodedTemplateValues,
191  $newVersion,
192  $iliasVersion,
193  time(),
194  false,
195  '',
196  '',
197  isset($background_rid) ? $background_rid->serialize() : '',
198  isset($tile_image_rid) ? $tile_image_rid->serialize() : ''
199  );
200 
201  $this->templateRepository->save($template);
202 
203  $clean_up_import_dir();
204 
205  return true;
206  }
207 
213  private function createArchiveDirectory(string $installationID): string
214  {
215  $type = $this->objectHelper->lookupType($this->objectId);
216  $certificateId = $this->objectId;
217 
218  $dir = $this->certificatePath . time() . '__' . $installationID . '__' . $type . '__' . $certificateId . '__certificate/';
219  if ($this->filesystem->hasDir($dir)) {
220  $this->filesystem->deleteDir($dir);
221  }
222  $this->filesystem->createDir($dir);
223 
224  return $dir;
225  }
226 }
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...
while($session_entry=$r->fetchRow(ilDBConstants::FETCHMODE_ASSOC)) return null
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:22
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.