123 : bool {
125 $abs_tmp_directory = rtrim($storage_directory, '/') . '/temp/';
127
128 $clean_up_import_dir = function () use (&$rel_tmp_import_path, &$rel_import_path): void {
129 try {
130 if ($this->tmp_fs->hasDir($rel_tmp_import_path)) {
131 $this->tmp_fs->deleteDir($rel_tmp_import_path);
132 }
133 }
catch (Throwable
$e) {
134 $this->
logger->error(sprintf(
"Can't clean up temporary import directory: %s",
$e->getMessage()));
135 $this->
logger->error($e->getTraceAsString());
136 }
137
138 try {
139 if ($this->web_fs->hasDir($rel_import_path)) {
140 $this->web_fs->deleteDir($rel_import_path);
141 }
142 }
catch (Throwable
$e) {
143 $this->
logger->error(sprintf(
"Can't clean up import directory: %s",
$e->getMessage()));
144 $this->
logger->error($e->getTraceAsString());
145 }
146 };
147
148 try {
149 $abs_zip_path = $abs_tmp_directory . $rel_tmp_import_path .
$filename;
150 $result = $this->utilHelper->moveUploadedFile(
151 $zipFile,
153 $abs_zip_path
154 );
155
156 if (!$result) {
157 return false;
158 }
159
160 $this->utilHelper->unzip(
161 $abs_tmp_directory . $rel_tmp_import_path .
$filename,
162 true
163 );
164
165 $abs_unzip_destination_dir = $abs_tmp_directory . $rel_tmp_import_path;
166 $sub_directory = str_replace(
'.zip',
'', strtolower(
$filename)) .
'/';
167 $abs_sub_directory_path = $abs_tmp_directory . $rel_tmp_import_path . $sub_directory;
168 if (is_dir($abs_sub_directory_path)) {
169 $abs_target_directory = $abs_sub_directory_path;
170 }
171
172 $this->utilHelper->renameExecutables($abs_sub_directory_path);
173
174 if ($this->tmp_fs->has($rel_tmp_import_path .
$filename)) {
175 $this->tmp_fs->delete($rel_tmp_import_path .
$filename);
176 }
177
178 $tmp_contents = $this->tmp_fs->listContents($rel_tmp_import_path, true);
179 foreach ($tmp_contents as $file) {
180 if (!$file->isFile()) {
181 continue;
182 }
183
184 if (!$this->web_fs->has($rel_import_path . basename($file->getPath()))) {
185 $this->web_fs->writeStream(
186 $rel_import_path . basename($file->getPath()),
187 $this->tmp_fs->readStream($file->getPath())
188 );
189 }
190 }
191
192 $num_background_images = 0;
193 $num_tile_images = 0;
194 $num_xml_files = 0;
195 $contents = $this->web_fs->listContents($rel_import_path);
196 foreach ($contents as $file) {
197 if (!$file->isFile()) {
198 continue;
199 }
200
201 if (strpos($file->getPath(), '.xml') !== false) {
202 ++$num_xml_files;
203 }
204
205 if (strpos($file->getPath(), '.svg') !== false) {
206 $stream = $this->web_fs->readStream($file->getPath());
207 $file_metadata = $stream->getMetadata();
208 $absolute_file_path = $file_metadata['uri'];
209
211 pathinfo($absolute_file_path)['basename'],
212 filesize($absolute_file_path),
213 mime_content_type($absolute_file_path)
214 );
215
216 ++$num_tile_images;
217
218 $processing_result = $this->svg_blacklist_processor->process($stream, $metadata);
219 if ($processing_result->getCode() !== ProcessingStatus::OK) {
220 return false;
221 }
222 }
223
224 if (str_contains($file->getPath(), '.jpg')) {
225 ++$num_background_images;
226 }
227 }
228
229 if (0 === $num_xml_files) {
230 $this->
logger->error(
'No XML file found in the imported zip file');
231 return false;
232 }
233 if ($num_background_images > 1) {
234 $this->
logger->error(
'More than one background image found in the imported zip file');
235 return false;
236 }
237 if ($num_tile_images > 1) {
238 $this->
logger->error(
'More than one tile image found in the imported zip file');
239 return false;
240 }
241
242 $certificate = $this->templateRepository->fetchCurrentlyUsedCertificate($this->objectId);
243
244 $currentVersion = $certificate->getVersion();
245 $newVersion = $currentVersion + 1;
246 $backgroundImagePath = $certificate->getBackgroundImagePath();
247 $cardThumbnailImagePath = $certificate->getThumbnailImagePath();
248
249 $xsl = $certificate->getCertificateContent();
250
251 foreach ($contents as $file) {
252 if (!$file->isFile()) {
253 continue;
254 }
255
256 if (strpos($file->getPath(), '.xml') !== false) {
257 $xsl = $this->web_fs->read($file->getPath());
258
259
260 $xsl = preg_replace_callback(
261 "/url\([']{0,1}(.*?)[']{0,1}\)/",
262 function (array $matches) use ($web_directory): string {
263 $basePath = rtrim(
264 dirname($this->fileService->getBackgroundImageDirectory($web_directory)),
265 '/'
266 );
267 $fileName = basename($matches[1]);
268
269 if ('[BACKGROUND_IMAGE]' === $fileName) {
270 $basePath = '';
271 } elseif ($basePath !== '') {
272 $basePath .= '/';
273 }
274
275 return 'url(' . $basePath . $fileName . ')';
276 },
277 $xsl
278 );
279 } elseif (strpos($file->getPath(), '.jpg') !== false) {
280 $newBackgroundImageName = 'background_' . $newVersion . '.jpg';
281 $newPath = $this->certificatePath . $newBackgroundImageName;
282 $this->web_fs->copy($file->getPath(), $newPath);
283
284 $backgroundImagePath = $this->certificatePath . $newBackgroundImageName;
285
286
288
289 $thumbnailImagePath = $web_directory . $backgroundImageThumbPath;
290
291 $originalImagePath = $web_directory . $newPath;
292 $this->utilHelper->convertImage(
293 $originalImagePath,
294 $thumbnailImagePath,
295 'JPEG',
296 "100"
297 );
298 } elseif (strpos($file->getPath(), '.svg') !== false) {
299 $newCardThumbnailName = 'thumbnail_' . $newVersion . '.svg';
300 $newPath = $this->certificatePath . $newCardThumbnailName;
301
302 $this->web_fs->copy($file->getPath(), $newPath);
303
304 $cardThumbnailImagePath = $this->certificatePath . $newCardThumbnailName;
305 }
306 }
307
308 $jsonEncodedTemplateValues = json_encode(
309 $this->placeholderDescriptionObject->getPlaceholderDescriptions(),
310 JSON_THROW_ON_ERROR
311 );
312
313 $newHashValue = hash(
314 'sha256',
315 implode('', [
316 $xsl,
317 $backgroundImagePath,
318 $jsonEncodedTemplateValues,
319 $cardThumbnailImagePath
320 ])
321 );
322
324 $this->objectId,
325 $this->objectHelper->lookupType($this->objectId),
326 $xsl,
327 $newHashValue,
328 $jsonEncodedTemplateValues,
329 $newVersion,
330 $iliasVerision,
331 time(),
332 true,
333 $backgroundImagePath,
334 $cardThumbnailImagePath
335 );
336
337 $this->templateRepository->save($template);
338
339 return true;
340 }
catch (Throwable
$e) {
341 $this->
logger->error(sprintf(
'Error during certificate import: %s',
$e->getMessage()));
342 $this->
logger->error($e->getTraceAsString());
343
344 return false;
345 } finally {
346 $clean_up_import_dir();
347 }
348 }
createTemporaryArchiveDirectory(string $installationId)
getBackgroundImageThumbnailPath()
createArchiveDirectory(string $installationId)