ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
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 
69  public function __construct(
70  int $objectId,
71  string $certificatePath,
78  ilDBInterface $database = null
79  ) {
80  $this->objectId = $objectId;
81  $this->certificatePath = $certificatePath;
82 
83  $this->logger = $logger;
84  if (null === $database) {
85  global $DIC;
86  $database = $DIC->database();
87  }
88 
89  $this->filesystem = $filesystem;
90 
91  $this->placeholderDescriptionObject = $placeholderDescriptionObject;
92 
93  if (null === $templateRepository) {
94  $templateRepository = new ilCertificateTemplateRepository($database, $logger);
95  }
96  $this->templateRepository = $templateRepository;
97 
98  if (null === $objectHelper) {
100  }
101  $this->objectHelper = $objectHelper;
102 
103  if (null === $utilHelper) {
105  }
106  $this->utilHelper = $utilHelper;
107  }
108 
122  public function import(
123  string $zipFile,
124  string $filename,
125  string $rootDir = CLIENT_WEB_DIR,
126  string $iliasVerision = ILIAS_VERSION_NUMERIC,
127  string $installationID = IL_INST_ID
128  ) {
129  $importPath = $this->createArchiveDirectory($installationID);
130 
131  $result = $this->utilHelper->moveUploadedFile($zipFile, $filename, $rootDir . $importPath . $filename);
132 
133  if (!$result) {
134  $this->filesystem->deleteDir($importPath);
135  return false;
136  }
137 
138  $this->utilHelper->unzip(
139  $rootDir . $importPath . $filename,
140  true
141  );
142 
143  $subDirectoryName = str_replace('.zip', '', strtolower($filename)) . '/';
144  $subDirectoryAbsolutePath = $rootDir . $importPath . $subDirectoryName;
145 
146  $copyDirectory = $importPath;
147  if (is_dir($subDirectoryAbsolutePath)) {
148  $copyDirectory = $subDirectoryAbsolutePath;
149  }
150 
151  $directoryInformation = $this->utilHelper->getDir($copyDirectory);
152 
153  $xmlFiles = 0;
154  foreach ($directoryInformation as $file) {
155  if (strcmp($file['type'], 'file') == 0) {
156  if (strpos($file['entry'], '.xml') !== false) {
157  $xmlFiles++;
158  }
159  }
160  }
161 
162  if (0 === $xmlFiles) {
163  $this->filesystem->deleteDir($importPath);
164  return false;
165  }
166 
167  $certificate = $this->templateRepository->fetchCurrentlyUsedCertificate($this->objectId);
168 
169  $currentVersion = (int) $certificate->getVersion();
170  $newVersion = $currentVersion + 1;
171  $backgroundImagePath = $certificate->getBackgroundImagePath();
172  $cardThumbnailImagePath = $certificate->getThumbnailImagePath();
173 
174  $xsl = $certificate->getCertificateContent();
175 
176  foreach ($directoryInformation as $file) {
177  if (strcmp($file['type'], 'file') == 0) {
178  $filePath = $importPath . $subDirectoryName . $file['entry'];
179  if (strpos($file['entry'], '.xml') !== false) {
180  $xsl = $this->filesystem->read($filePath);
181  // as long as we cannot make RPC calls in a given directory, we have
182  // to add the complete path to every url
183  $xsl = preg_replace_callback("/url\([']{0,1}(.*?)[']{0,1}\)/", function (array $matches) use ($rootDir) {
184  $basePath = rtrim(dirname($this->getBackgroundImageDirectory($rootDir)), '/');
185  $fileName = basename($matches[1]);
186 
187  if ('[BACKGROUND_IMAGE]' === $fileName) {
188  $basePath = '';
189  } elseif (strlen($basePath) > 0) {
190  $basePath .= '/';
191  }
192 
193  return 'url(' . $basePath . $fileName . ')';
194  }, $xsl);
195  } elseif (strpos($file['entry'], '.jpg') !== false) {
196  $newBackgroundImageName = 'background_' . $newVersion . '.jpg';
197  $newPath = $this->certificatePath . $newBackgroundImageName;
198  $this->filesystem->copy($filePath, $newPath);
199 
200  $backgroundImagePath = $this->certificatePath . $newBackgroundImageName;
201  // upload of the background image, create a thumbnail
202 
203  $backgroundImageThumbPath = $this->getBackgroundImageThumbnailPath();
204 
205  $thumbnailImagePath = $rootDir . $backgroundImageThumbPath;
206 
207  $originalImagePath = $rootDir . $newPath;
208  $this->utilHelper->convertImage(
209  $originalImagePath,
210  $thumbnailImagePath,
211  'JPEG',
212  100
213  );
214  } elseif (strpos($file['entry'], '.svg') !== false) {
215  $newCardThumbnailName = 'thumbnail_' . $newVersion . '.svg';
216  $newPath = $this->certificatePath . $newCardThumbnailName;
217 
218  $this->filesystem->copy($filePath, $newPath);
219 
220  $cardThumbnailImagePath = $this->certificatePath . $newCardThumbnailName;
221  }
222  }
223  }
224 
225  $jsonEncodedTemplateValues = json_encode($this->placeholderDescriptionObject->getPlaceholderDescriptions());
226 
227  $newHashValue = hash(
228  'sha256',
229  implode('', array(
230  $xsl,
231  $backgroundImagePath,
232  $jsonEncodedTemplateValues,
233  $cardThumbnailImagePath
234  ))
235  );
236 
238  $this->objectId,
239  $this->objectHelper->lookupType($this->objectId),
240  $xsl,
241  $newHashValue,
242  $jsonEncodedTemplateValues,
243  $newVersion,
244  $iliasVerision,
245  time(),
246  true,
247  $backgroundImagePath,
248  $cardThumbnailImagePath
249  );
250 
251  $this->templateRepository->save($template);
252 
253  $this->utilHelper->delDir($importPath);
254 
255  return true;
256  }
257 
265  private function createArchiveDirectory(string $installationID) : string
266  {
267  $type = $this->objectHelper->lookupType($this->objectId);
268  $certificateId = $this->objectId;
269 
270  $dir = $this->certificatePath . time() . '__' . $installationID . '__' . $type . '__' . $certificateId . '__certificate/';
271  $this->filesystem->createDir($dir);
272 
273  return $dir;
274  }
275 
276 
281  private function getBackgroundImageDirectory(string $rootDir) : string
282  {
283  return str_replace(
284  array($rootDir, '//'),
285  array('[CLIENT_WEB_DIR]', '/'),
286  ''
287  );
288  }
289 
293  private function getBackgroundImageThumbnailPath() : string
294  {
295  return $this->certificatePath . 'background.jpg.thumb.jpg';
296  }
297 }
$result
const ILIAS_VERSION_NUMERIC
$template
$type
global $DIC
Definition: saml.php:7
__construct(int $objectId, string $certificatePath, ilCertificatePlaceholderDescription $placeholderDescriptionObject, ilLogger $logger, Filesystem $filesystem, ilCertificateTemplateRepository $templateRepository=null, ilCertificateObjectHelper $objectHelper=null, ilCertificateUtilHelper $utilHelper=null, ilDBInterface $database=null)
if(@file_exists(dirname(__FILE__).'/lang/eng.php')) $certificate
Definition: example_052.php:77
$filename
Definition: buildRTE.php:89
Just a wrapper class to create Unit Test for other classes.
Component logger with individual log levels by component id.
hash(StreamInterface $stream, $algo, $rawOutput=false)
Calculate a hash of a Stream.
Definition: functions.php:406
Class FlySystemFileAccessTest.
createArchiveDirectory(string $installationID)
Creates a directory for a zip archive containing multiple certificates.