ILIAS  release_6 Revision v6.24-5-g0c8bfefb3b8
All Data Structures Namespaces Files Functions Variables Modules Pages
class.ilCertificateTemplateImportAction.php
Go to the documentation of this file.
1 <?php
2 
3 /* Copyright (c) 1998-2018 ILIAS open source, Extended GPL, see docs/LICENSE */
4 
6 
11 {
15  private $objectId;
16 
21 
26 
31 
35  private $logger;
36 
40  private $filesystem;
41 
45  private $objectHelper;
46 
50  private $utilHelper;
51 
55  private $installationID;
56 
60  private $fileService;
61 
74  public function __construct(
75  int $objectId,
76  string $certificatePath,
83  ilDBInterface $database = null,
85  ) {
86  $this->objectId = $objectId;
87  $this->certificatePath = $certificatePath;
88 
89  $this->logger = $logger;
90  if (null === $database) {
91  global $DIC;
92  $database = $DIC->database();
93  }
94 
95  $this->filesystem = $filesystem;
96 
97  $this->placeholderDescriptionObject = $placeholderDescriptionObject;
98 
99  if (null === $templateRepository) {
100  $templateRepository = new ilCertificateTemplateRepository($database, $logger);
101  }
102  $this->templateRepository = $templateRepository;
103 
104  if (null === $objectHelper) {
106  }
107  $this->objectHelper = $objectHelper;
108 
109  if (null === $utilHelper) {
111  }
112  $this->utilHelper = $utilHelper;
113 
114  if (null === $fileService) {
116  $certificatePath,
117  $filesystem
118  );
119  }
120  $this->fileService = $fileService;
121  }
122 
136  public function import(
137  string $zipFile,
138  string $filename,
139  string $rootDir = CLIENT_WEB_DIR,
140  string $iliasVerision = ILIAS_VERSION_NUMERIC,
141  string $installationID = IL_INST_ID
142  ) {
143  $importPath = $this->createArchiveDirectory($installationID);
144 
145  $result = $this->utilHelper->moveUploadedFile($zipFile, $filename, $rootDir . $importPath . $filename);
146 
147  if (!$result) {
148  $this->filesystem->deleteDir($importPath);
149  return false;
150  }
151 
152  $this->utilHelper->unzip(
153  $rootDir . $importPath . $filename,
154  true
155  );
156 
157  $subDirectoryName = str_replace('.zip', '', strtolower($filename)) . '/';
158  $subDirectoryAbsolutePath = $rootDir . $importPath . $subDirectoryName;
159 
160  $copyDirectory = $importPath;
161  if (is_dir($subDirectoryAbsolutePath)) {
162  $copyDirectory = $subDirectoryAbsolutePath;
163  }
164 
165  $directoryInformation = $this->utilHelper->getDir($copyDirectory);
166 
167  $xmlFiles = 0;
168  foreach ($directoryInformation as $file) {
169  if (strcmp($file['type'], 'file') == 0) {
170  if (strpos($file['entry'], '.xml') !== false) {
171  $xmlFiles++;
172  }
173  }
174  }
175 
176  if (0 === $xmlFiles) {
177  $this->filesystem->deleteDir($importPath);
178  return false;
179  }
180 
181  $certificate = $this->templateRepository->fetchCurrentlyUsedCertificate($this->objectId);
182 
183  $currentVersion = (int) $certificate->getVersion();
184  $newVersion = $currentVersion + 1;
185  $backgroundImagePath = $certificate->getBackgroundImagePath();
186  $cardThumbnailImagePath = $certificate->getThumbnailImagePath();
187 
188  $xsl = $certificate->getCertificateContent();
189 
190  foreach ($directoryInformation as $file) {
191  if (strcmp($file['type'], 'file') == 0) {
192  $filePath = $importPath . $subDirectoryName . $file['entry'];
193  if (strpos($file['entry'], '.xml') !== false) {
194  $xsl = $this->filesystem->read($filePath);
195  // as long as we cannot make RPC calls in a given directory, we have
196  // to add the complete path to every url
197  $xsl = preg_replace_callback("/url\([']{0,1}(.*?)[']{0,1}\)/", function (array $matches) use ($rootDir) {
198  $basePath = rtrim(dirname($this->fileService->getBackgroundImageDirectory($rootDir)), '/');
199  $fileName = basename($matches[1]);
200 
201  if ('[BACKGROUND_IMAGE]' === $fileName) {
202  $basePath = '';
203  } elseif (strlen($basePath) > 0) {
204  $basePath .= '/';
205  }
206 
207  return 'url(' . $basePath . $fileName . ')';
208  }, $xsl);
209  } elseif (strpos($file['entry'], '.jpg') !== false) {
210  $newBackgroundImageName = 'background_' . $newVersion . '.jpg';
211  $newPath = $this->certificatePath . $newBackgroundImageName;
212  $this->filesystem->copy($filePath, $newPath);
213 
214  $backgroundImagePath = $this->certificatePath . $newBackgroundImageName;
215  // upload of the background image, create a thumbnail
216 
217  $backgroundImageThumbPath = $this->getBackgroundImageThumbnailPath();
218 
219  $thumbnailImagePath = $rootDir . $backgroundImageThumbPath;
220 
221  $originalImagePath = $rootDir . $newPath;
222  $this->utilHelper->convertImage(
223  $originalImagePath,
224  $thumbnailImagePath,
225  'JPEG',
226  100
227  );
228  } elseif (strpos($file['entry'], '.svg') !== false) {
229  $newCardThumbnailName = 'thumbnail_' . $newVersion . '.svg';
230  $newPath = $this->certificatePath . $newCardThumbnailName;
231 
232  $this->filesystem->copy($filePath, $newPath);
233 
234  $cardThumbnailImagePath = $this->certificatePath . $newCardThumbnailName;
235  }
236  }
237  }
238 
239  $jsonEncodedTemplateValues = json_encode($this->placeholderDescriptionObject->getPlaceholderDescriptions());
240 
241  $newHashValue = hash(
242  'sha256',
243  implode('', array(
244  $xsl,
245  $backgroundImagePath,
246  $jsonEncodedTemplateValues,
247  $cardThumbnailImagePath
248  ))
249  );
250 
251  $template = new ilCertificateTemplate(
252  $this->objectId,
253  $this->objectHelper->lookupType($this->objectId),
254  $xsl,
255  $newHashValue,
256  $jsonEncodedTemplateValues,
257  $newVersion,
258  $iliasVerision,
259  time(),
260  true,
261  $backgroundImagePath,
262  $cardThumbnailImagePath
263  );
264 
265  $this->templateRepository->save($template);
266 
267  $this->utilHelper->delDir($importPath);
268 
269  return true;
270  }
271 
279  private function createArchiveDirectory(string $installationID) : string
280  {
281  $type = $this->objectHelper->lookupType($this->objectId);
282  $certificateId = $this->objectId;
283 
284  $dir = $this->certificatePath . time() . '__' . $installationID . '__' . $type . '__' . $certificateId . '__certificate/';
285  $this->filesystem->createDir($dir);
286 
287  return $dir;
288  }
289 
290 
294  private function getBackgroundImageThumbnailPath() : string
295  {
296  return $this->certificatePath . 'background.jpg.thumb.jpg';
297  }
298 }
__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)
$result
const ILIAS_VERSION_NUMERIC
$type
Interface ilDBInterface.
$filename
Definition: buildRTE.php:89
Just a wrapper class to create Unit Test for other classes.
$DIC
Definition: xapitoken.php:46
Component logger with individual log levels by component id.
Class FlySystemFileAccessTest.
createArchiveDirectory(string $installationID)
Creates a directory for a zip archive containing multiple certificates.