ILIAS  release_8 Revision v8.19
All Data Structures Namespaces Files Functions Variables Modules Pages
class.ilCertificateTemplateImportAction.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
25 
30 {
31  private int $objectId;
32  private string $certificatePath;
35  private ilLogger $logger;
39  private string $installationID;
41 
42  public function __construct(
43  int $objectId,
44  string $certificatePath,
45  ilCertificatePlaceholderDescription $placeholderDescriptionObject,
46  ilLogger $logger,
47  Filesystem $filesystem,
48  ?ilCertificateTemplateRepository $templateRepository = null,
49  ?ilCertificateObjectHelper $objectHelper = null,
50  ?ilCertificateUtilHelper $utilHelper = null,
51  ?ilDBInterface $database = null,
52  ?ilCertificateBackgroundImageFileService $fileService = null
53  ) {
54  $this->objectId = $objectId;
55  $this->certificatePath = $certificatePath;
56 
57  $this->logger = $logger;
58  if (null === $database) {
59  global $DIC;
60  $database = $DIC->database();
61  }
62 
63  $this->filesystem = $filesystem;
64 
65  $this->placeholderDescriptionObject = $placeholderDescriptionObject;
66 
67  if (null === $templateRepository) {
68  $templateRepository = new ilCertificateTemplateDatabaseRepository($database, $logger);
69  }
70  $this->templateRepository = $templateRepository;
71 
72  if (null === $objectHelper) {
73  $objectHelper = new ilCertificateObjectHelper();
74  }
75  $this->objectHelper = $objectHelper;
76 
77  if (null === $utilHelper) {
78  $utilHelper = new ilCertificateUtilHelper();
79  }
80  $this->utilHelper = $utilHelper;
81 
82  if (null === $fileService) {
83  $fileService = new ilCertificateBackgroundImageFileService(
84  $certificatePath,
85  $filesystem
86  );
87  }
88  $this->fileService = $fileService;
89  }
90 
104  public function import(
105  string $zipFile,
106  string $filename,
107  string $rootDir = CLIENT_WEB_DIR,
108  string $iliasVerision = ILIAS_VERSION_NUMERIC,
109  string $installationID = IL_INST_ID
110  ): bool {
111  $importPath = $this->createArchiveDirectory($installationID);
112 
113  $result = $this->utilHelper->moveUploadedFile($zipFile, $filename, $rootDir . $importPath . $filename);
114 
115  if (!$result) {
116  $this->filesystem->deleteDir($importPath);
117  return false;
118  }
119 
120  $this->utilHelper->unzip(
121  $rootDir . $importPath . $filename,
122  true
123  );
124 
125  $subDirectoryName = str_replace('.zip', '', strtolower($filename)) . '/';
126  $subDirectoryAbsolutePath = $rootDir . $importPath . $subDirectoryName;
127 
128  $copyDirectory = $importPath;
129  if (is_dir($subDirectoryAbsolutePath)) {
130  $copyDirectory = $subDirectoryAbsolutePath;
131  }
132 
133  $directoryInformation = $this->utilHelper->getDir($copyDirectory);
134 
135  $xmlFiles = 0;
136  foreach ($directoryInformation as $file) {
137  if (strcmp($file['type'], 'file') === 0 && strpos($file['entry'], '.xml') !== false) {
138  $xmlFiles++;
139  }
140  }
141 
142  if (0 === $xmlFiles) {
143  $this->filesystem->deleteDir($importPath);
144  return false;
145  }
146 
147  $certificate = $this->templateRepository->fetchCurrentlyUsedCertificate($this->objectId);
148 
149  $currentVersion = $certificate->getVersion();
150  $newVersion = $currentVersion + 1;
151  $backgroundImagePath = $certificate->getBackgroundImagePath();
152  $cardThumbnailImagePath = $certificate->getThumbnailImagePath();
153 
154  $xsl = $certificate->getCertificateContent();
155 
156  foreach ($directoryInformation as $file) {
157  if (strcmp($file['type'], 'file') === 0) {
158  $filePath = $importPath . $subDirectoryName . $file['entry'];
159  if (strpos($file['entry'], '.xml') !== false) {
160  $xsl = $this->filesystem->read($filePath);
161  // as long as we cannot make RPC calls in a given directory, we have
162  // to add the complete path to every url
163  $xsl = preg_replace_callback(
164  "/url\([']{0,1}(.*?)[']{0,1}\)/",
165  function (array $matches) use ($rootDir): string {
166  $basePath = rtrim(dirname($this->fileService->getBackgroundImageDirectory($rootDir)), '/');
167  $fileName = basename($matches[1]);
168 
169  if ('[BACKGROUND_IMAGE]' === $fileName) {
170  $basePath = '';
171  } elseif ($basePath !== '') {
172  $basePath .= '/';
173  }
174 
175  return 'url(' . $basePath . $fileName . ')';
176  },
177  $xsl
178  );
179  } elseif (strpos($file['entry'], '.jpg') !== false) {
180  $newBackgroundImageName = 'background_' . $newVersion . '.jpg';
181  $newPath = $this->certificatePath . $newBackgroundImageName;
182  $this->filesystem->copy($filePath, $newPath);
183 
184  $backgroundImagePath = $this->certificatePath . $newBackgroundImageName;
185  // upload of the background image, create a thumbnail
186 
187  $backgroundImageThumbPath = $this->getBackgroundImageThumbnailPath();
188 
189  $thumbnailImagePath = $rootDir . $backgroundImageThumbPath;
190 
191  $originalImagePath = $rootDir . $newPath;
192  $this->utilHelper->convertImage(
193  $originalImagePath,
194  $thumbnailImagePath,
195  'JPEG',
196  "100"
197  );
198  } elseif (strpos($file['entry'], '.svg') !== false) {
199  $newCardThumbnailName = 'thumbnail_' . $newVersion . '.svg';
200  $newPath = $this->certificatePath . $newCardThumbnailName;
201 
202  $this->filesystem->copy($filePath, $newPath);
203 
204  $cardThumbnailImagePath = $this->certificatePath . $newCardThumbnailName;
205  }
206  }
207  }
208 
209  $jsonEncodedTemplateValues = json_encode(
210  $this->placeholderDescriptionObject->getPlaceholderDescriptions(),
211  JSON_THROW_ON_ERROR
212  );
213 
214  $newHashValue = hash(
215  'sha256',
216  implode('', [
217  $xsl,
218  $backgroundImagePath,
219  $jsonEncodedTemplateValues,
220  $cardThumbnailImagePath
221  ])
222  );
223 
224  $template = new ilCertificateTemplate(
225  $this->objectId,
226  $this->objectHelper->lookupType($this->objectId),
227  $xsl,
228  $newHashValue,
229  $jsonEncodedTemplateValues,
230  $newVersion,
231  $iliasVerision,
232  time(),
233  true,
234  $backgroundImagePath,
235  $cardThumbnailImagePath
236  );
237 
238  $this->templateRepository->save($template);
239 
240  $this->filesystem->deleteDir($importPath);
241 
242  return true;
243  }
244 
251  private function createArchiveDirectory(string $installationID): string
252  {
253  $type = $this->objectHelper->lookupType($this->objectId);
254  $certificateId = $this->objectId;
255 
256  $dir = $this->certificatePath . time() . '__' . $installationID . '__' . $type . '__' . $certificateId . '__certificate/';
257  $this->filesystem->createDir($dir);
258 
259  return $dir;
260  }
261 
262  private function getBackgroundImageThumbnailPath(): string
263  {
264  return $this->certificatePath . 'background.jpg.thumb.jpg';
265  }
266 }
const IL_INST_ID
Definition: constants.php:40
const ILIAS_VERSION_NUMERIC
$type
global $DIC
Definition: feed.php:28
const CLIENT_WEB_DIR
Definition: constants.php:47
ilCertificateBackgroundImageFileService $fileService
$filename
Definition: buildRTE.php:78
Just a wrapper class to create Unit Test for other classes.
__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)
ilCertificatePlaceholderDescription $placeholderDescriptionObject
Class FlySystemFileAccessTest disabled disabled disabled.
createArchiveDirectory(string $installationID)
Creates a directory for a zip archive containing multiple certificates.