| 1 |
<?php |
| 2 |
|
| 3 |
namespace LAM\PDF; |
| 4 |
|
| 5 |
use LAM\PERSISTENCE\ConfigurationDatabase; |
| 6 |
use LAM\TYPES\TypeManager; |
| 7 |
use LAMCfgMain; |
| 8 |
use LAMConfig; |
| 9 |
use LAMException; |
| 10 |
use LAM\ImageUtils\ImageManipulationFactory; |
| 11 |
use PDO; |
| 12 |
use ServerProfilePersistenceManager; |
| 13 |
use XMLReader; |
| 14 |
use XMLWriter; |
| 15 |
use function LAM\PERSISTENCE\dbTableExists; |
| 16 |
|
| 17 |
/* |
| 18 |
This code is part of LDAP Account Manager (http://www.ldap-account-manager.org/) |
| 19 |
Copyright (C) 2003 - 2006 Michael Duergner |
| 20 |
2011 - 2026 Roland Gruber |
| 21 |
|
| 22 |
This program is free software; you can redistribute it and/or modify |
| 23 |
it under the terms of the GNU General Public License as published by |
| 24 |
the Free Software Foundation; either version 2 of the License, or |
| 25 |
(at your option) any later version. |
| 26 |
|
| 27 |
This program is distributed in the hope that it will be useful, |
| 28 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 29 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 30 |
GNU General Public License for more details. |
| 31 |
|
| 32 |
You should have received a copy of the GNU General Public License |
| 33 |
along with this program; if not, write to the Free Software |
| 34 |
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 35 |
*/ |
| 36 |
|
| 37 |
/** |
| 38 |
* Functions to manage the PDF structures. |
| 39 |
* |
| 40 |
* @author Michael Duergner |
| 41 |
* @package PDF |
| 42 |
*/ |
| 43 |
|
| 44 |
/** LAM configuration */ |
| 45 |
include_once(__DIR__ . "/config.inc"); |
| 46 |
|
| 47 |
/** |
| 48 |
* Use as server profile name to manage global templates. |
| 49 |
*/ |
| 50 |
const GLOBAL_PROFILE = '__GLOBAL__'; |
| 51 |
|
| 52 |
/** LDAP object */ |
| 53 |
include_once(__DIR__ . "/ldap.inc"); |
| 54 |
|
| 55 |
/** |
| 56 |
* Manages the persistence of PDF structures. |
| 57 |
* |
| 58 |
* @package LAM\PDF |
| 59 |
*/ |
| 60 |
class PdfStructurePersistenceManager { |
| 61 |
|
| 62 |
/** |
| 63 |
* @var PdfStructurePersistenceStrategy |
| 64 |
*/ |
| 65 |
private $strategy; |
| 66 |
|
| 67 |
/** |
| 68 |
* Constructor |
| 69 |
*/ |
| 70 |
public function __construct() { |
| 71 |
$configDb = new ConfigurationDatabase(new LAMCfgMain()); |
| 72 |
if ($configDb->useRemoteDb()) { |
| 73 |
$this->strategy = new PdfStructurePersistenceStrategyPdo($configDb->getPdo()); |
| 74 |
} |
| 75 |
else { |
| 76 |
$this->strategy = new PdfStructurePersistenceStrategyFiles(); |
| 77 |
} |
| 78 |
} |
| 79 |
|
| 80 |
/** |
| 81 |
* Returns the names of existing PDF structure templates. |
| 82 |
* |
| 83 |
* @return array<string, string[]> scope => names (e.g. array('user' => array('default'))) |
| 84 |
* @throws LAMException error reading templates |
| 85 |
*/ |
| 86 |
public function getPdfStructureTemplateNames(): array { |
| 87 |
return $this->strategy->getPdfStructureTemplateNames(); |
| 88 |
} |
| 89 |
|
| 90 |
/** |
| 91 |
* Deletes an PDF structure template. |
| 92 |
* |
| 93 |
* @param string $scope user/group/host |
| 94 |
* @param string $name PDF structure name |
| 95 |
* @throws LAMException error deleting template |
| 96 |
*/ |
| 97 |
public function deletePdfStructureTemplate(string $scope, string $name): void { |
| 98 |
if (!$this->isValidPdfStructureName($name) || !TypeManager::isValidTypeId($scope) || ($name === 'default')) { |
| 99 |
logNewMessage(LOG_NOTICE, "Invalid account profile name: $name:$scope"); |
| 100 |
throw new LAMException(_("Unable to delete profile!")); |
| 101 |
} |
| 102 |
$this->strategy->deletePdfStructureTemplate($scope, $name); |
| 103 |
} |
| 104 |
|
| 105 |
/** |
| 106 |
* Reads a PDF structure template. |
| 107 |
* |
| 108 |
* @param string $scope user/group/host |
| 109 |
* @param string $name structure name |
| 110 |
* @return PDFStructure PDF structure |
| 111 |
* @throws LAMException error deleting structure |
| 112 |
*/ |
| 113 |
public function readPdfStructureTemplate(string $scope, string $name): PDFStructure { |
| 114 |
if (!TypeManager::isValidTypeId($scope) || !$this->isValidPDFStructureName($name)) { |
| 115 |
throw new LAMException(_('Unable to read PDF structure.')); |
| 116 |
} |
| 117 |
return $this->strategy->readPdfStructureTemplate($scope, $name); |
| 118 |
} |
| 119 |
|
| 120 |
/** |
| 121 |
* Saves the PDF structure. |
| 122 |
* |
| 123 |
* @param string $scope user/group/host |
| 124 |
* @param string $name structure name |
| 125 |
* @param PDFStructure $structure structure |
| 126 |
* @throws LAMException error saving structure |
| 127 |
*/ |
| 128 |
public function savePdfStructureTemplate(string $scope, string $name, PDFStructure $structure): void { |
| 129 |
if (!TypeManager::isValidTypeId($scope) || !$this->isValidPDFStructureName($name)) { |
| 130 |
throw new LAMException(_('PDF structure name not valid')); |
| 131 |
} |
| 132 |
$this->strategy->savePdfStructureTemplate($scope, $name, $structure); |
| 133 |
} |
| 134 |
|
| 135 |
/** |
| 136 |
* Returns a list of template logo file names. |
| 137 |
* |
| 138 |
* @return string[] logo file names |
| 139 |
*/ |
| 140 |
public function getPdfTemplateLogoNames(): array { |
| 141 |
return $this->strategy->getPdfTemplateLogoNames(); |
| 142 |
} |
| 143 |
|
| 144 |
/** |
| 145 |
* Returns the binary data for the given template logo. |
| 146 |
* |
| 147 |
* @param string $name file name |
| 148 |
* @return string binary |
| 149 |
* @throws LAMException error reading file |
| 150 |
*/ |
| 151 |
public function getPdfTemplateLogoBinary(string $name): string { |
| 152 |
if (!PDFStructure::isValidLogoFileName($name)) { |
| 153 |
throw new LAMException(_('Unable to read logo file.')); |
| 154 |
} |
| 155 |
return $this->strategy->getPdfTemplateLogoBinary($name); |
| 156 |
} |
| 157 |
|
| 158 |
/** |
| 159 |
* Deletes a logo in global templates. |
| 160 |
* |
| 161 |
* @param string $name logo name |
| 162 |
* @throws LAMException error during deletion |
| 163 |
*/ |
| 164 |
public function deletePdfTemplateLogo(string $name): void { |
| 165 |
if (!PDFStructure::isValidLogoFileName($name)) { |
| 166 |
throw new LAMException(_('Unable to delete logo file.')); |
| 167 |
} |
| 168 |
$this->strategy->deletePdfTemplateLogo($name); |
| 169 |
} |
| 170 |
|
| 171 |
/** |
| 172 |
* Saves the template logo. |
| 173 |
* |
| 174 |
* @param string $name file name |
| 175 |
* @param string $data binary data |
| 176 |
* @throws LAMException error during save |
| 177 |
*/ |
| 178 |
public function savePdfTemplateLogo(string $name, string $data): void { |
| 179 |
if (!PDFStructure::isValidLogoFileName($name)) { |
| 180 |
throw new LAMException(_('Unable to upload logo file.')); |
| 181 |
} |
| 182 |
$this->strategy->savePdfTemplateLogo($name, $data); |
| 183 |
} |
| 184 |
|
| 185 |
/** |
| 186 |
* Returns the list of available PDF logos. |
| 187 |
* |
| 188 |
* @param string $confName server profile name |
| 189 |
* @param bool $readDimensions reads the image dimensions |
| 190 |
* @return PdfLogo[] logos |
| 191 |
* @throws LAMException error reading logos |
| 192 |
*/ |
| 193 |
public function getPdfLogos(string $confName, bool $readDimensions = false): array { |
| 194 |
if (!LAMConfig::isValidName($confName)) { |
| 195 |
throw new LAMException(_('Unable to read logos.')); |
| 196 |
} |
| 197 |
$logoNames = $this->strategy->getPdfLogoNames($confName); |
| 198 |
sort($logoNames); |
| 199 |
$result = []; |
| 200 |
if ($readDimensions) { |
| 201 |
include_once __DIR__ . '/imageutils.inc'; |
| 202 |
} |
| 203 |
foreach ($logoNames as $logoName) { |
| 204 |
if ($readDimensions) { |
| 205 |
$binary = $this->getPdfLogoBinary($confName, $logoName); |
| 206 |
$imageManipulator = ImageManipulationFactory::getImageManipulator($binary); |
| 207 |
$result[] = new PdfLogo($logoName, $imageManipulator->getHeight(), $imageManipulator->getWidth()); |
| 208 |
$imageManipulator = null; |
| 209 |
} |
| 210 |
else { |
| 211 |
$result[] = new PdfLogo($logoName); |
| 212 |
} |
| 213 |
} |
| 214 |
return $result; |
| 215 |
} |
| 216 |
|
| 217 |
/** |
| 218 |
* Reads a PDF logo. |
| 219 |
* |
| 220 |
* @param string $confName server profile name |
| 221 |
* @param string $name file name |
| 222 |
* @return string binary data |
| 223 |
* @throws LAMException error reading logo |
| 224 |
*/ |
| 225 |
public function getPdfLogoBinary(string $confName, string $name): string { |
| 226 |
if (!LAMConfig::isValidName($confName) || !PDFStructure::isValidLogoFileName($name)) { |
| 227 |
logNewMessage(LOG_ERR, "Unable to read PDF logo " . $name); |
| 228 |
throw new LAMException(_('Unable to read logo file.')); |
| 229 |
} |
| 230 |
return $this->strategy->getPdfLogoBinary($confName, $name); |
| 231 |
} |
| 232 |
|
| 233 |
/** |
| 234 |
* Deletes a PDF logo. |
| 235 |
* |
| 236 |
* @param string $confName server profile name |
| 237 |
* @param string $name logo name |
| 238 |
* @throws LAMException error deleting logo |
| 239 |
*/ |
| 240 |
public function deletePdfLogo(string $confName, string $name): void { |
| 241 |
if (!LAMConfig::isValidName($confName) || !PDFStructure::isValidLogoFileName($name)) { |
| 242 |
throw new LAMException(_('Unable to delete logo file.')); |
| 243 |
} |
| 244 |
// check if existing |
| 245 |
$found = false; |
| 246 |
$logos = $this->getPdfLogos($confName); |
| 247 |
foreach ($logos as $logo) { |
| 248 |
if ($logo->getName() === $name) { |
| 249 |
$found = true; |
| 250 |
break; |
| 251 |
} |
| 252 |
} |
| 253 |
if (!$found) { |
| 254 |
throw new LAMException(_('File does not exist.'), htmlspecialchars($name)); |
| 255 |
} |
| 256 |
// check if still in use |
| 257 |
$typeManager = new TypeManager(); |
| 258 |
$activeTypes = $typeManager->getConfiguredTypes(); |
| 259 |
foreach ($activeTypes as $type) { |
| 260 |
$structures = $this->getPDFStructures($confName, $type->getId()); |
| 261 |
foreach ($structures as $structure) { |
| 262 |
$data = $this->readPdfStructure($confName, $type->getId(), $structure); |
| 263 |
if ($data->getLogo() == $name) { |
| 264 |
throw new LAMException(_('Unable to delete logo file.'), |
| 265 |
sprintf(_('Logo is still in use by PDF structure "%s" in account type "%s".'), $structure, $type->getAlias())); |
| 266 |
} |
| 267 |
} |
| 268 |
} |
| 269 |
$this->strategy->deletePdfLogo($confName, $name); |
| 270 |
} |
| 271 |
|
| 272 |
/** |
| 273 |
* Saves a PDF logo. |
| 274 |
* |
| 275 |
* @param string $confName server profile name |
| 276 |
* @param string $name logo name |
| 277 |
* @param string $data binary |
| 278 |
* @throws LAMException error saving logo |
| 279 |
*/ |
| 280 |
public function savePdfLogo(string $confName, string $name, string $data): void { |
| 281 |
if (!LAMConfig::isValidName($confName)) { |
| 282 |
throw new LAMException(_('Unable to upload logo file.')); |
| 283 |
} |
| 284 |
if (!PDFStructure::isValidLogoFileName($name)) { |
| 285 |
throw new LAMException(_('Unable to upload logo file.'), _('The file name must end with ".png" or ".jpg".')); |
| 286 |
} |
| 287 |
$this->strategy->savePdfLogo($confName, $name, $data); |
| 288 |
} |
| 289 |
|
| 290 |
/** |
| 291 |
* Returns all available PDF structure definitions for the submitted account type. |
| 292 |
* |
| 293 |
* @param string $confName server profile name |
| 294 |
* @param string $typeId the account type |
| 295 |
* |
| 296 |
* @return string[] structure names |
| 297 |
* @throws LAMException error reading structures |
| 298 |
*/ |
| 299 |
public function getPDFStructures(string $confName, string $typeId): array { |
| 300 |
if (!TypeManager::isValidTypeId($typeId)) { |
| 301 |
throw new LAMException(_('Unable to read PDF structures.')); |
| 302 |
} |
| 303 |
$return = $this->strategy->getPDFStructures($confName, $typeId); |
| 304 |
sort($return); |
| 305 |
return $return; |
| 306 |
} |
| 307 |
|
| 308 |
/** |
| 309 |
* Deletes a PDF structure. |
| 310 |
* |
| 311 |
* @param string $confName server profile name |
| 312 |
* @param string $typeId user/group/host |
| 313 |
* @param string $name structure name |
| 314 |
* @throws LAMException error deleting structure |
| 315 |
*/ |
| 316 |
public function deletePdfStructure(string $confName, string $typeId, string $name): void { |
| 317 |
if (!LAMConfig::isValidName($confName) || !TypeManager::isValidTypeId($typeId) || !$this->isValidPDFStructureName($name)) { |
| 318 |
logNewMessage(LOG_ERR, 'Invalid data: ' . $confName . ' ' . $typeId . ' ' . $name); |
| 319 |
throw new LAMException(_('Unable to delete PDF structure!')); |
| 320 |
} |
| 321 |
$this->strategy->deletePdfStructure($confName, $typeId, $name); |
| 322 |
} |
| 323 |
|
| 324 |
/** |
| 325 |
* Reads a PDF structure. |
| 326 |
* |
| 327 |
* @param string $confName server profile name |
| 328 |
* @param string $typeId user/group/host |
| 329 |
* @param string $name structure name |
| 330 |
* @return PDFStructure PDF structure |
| 331 |
* @throws LAMException error deleting structure |
| 332 |
*/ |
| 333 |
public function readPdfStructure(string $confName, string $typeId, string $name): PDFStructure { |
| 334 |
if (!LAMConfig::isValidName($confName) || !TypeManager::isValidTypeId($typeId) || !$this->isValidPDFStructureName($name)) { |
| 335 |
throw new LAMException(_('Unable to read PDF structure.')); |
| 336 |
} |
| 337 |
return $this->strategy->readPdfStructure($confName, $typeId, $name); |
| 338 |
} |
| 339 |
|
| 340 |
/** |
| 341 |
* Saves the PDF structure. |
| 342 |
* |
| 343 |
* @param string $confName server profile name |
| 344 |
* @param string $typeId user/group/host |
| 345 |
* @param string $name structure name |
| 346 |
* @param PDFStructure $structure structure |
| 347 |
* @throws LAMException error saving structure |
| 348 |
*/ |
| 349 |
public function savePdfStructure(string $confName, string $typeId, string $name, PDFStructure $structure): void { |
| 350 |
if (!LAMConfig::isValidName($confName) || !TypeManager::isValidTypeId($typeId) || !$this->isValidPDFStructureName($name)) { |
| 351 |
throw new LAMException(_('PDF structure name not valid')); |
| 352 |
} |
| 353 |
$this->strategy->savePdfStructure($confName, $typeId, $name, $structure); |
| 354 |
} |
| 355 |
|
| 356 |
/** |
| 357 |
* Returns if the give structure name is valid. |
| 358 |
* |
| 359 |
* @param string $name structure name |
| 360 |
* @return boolean is valid |
| 361 |
*/ |
| 362 |
private function isValidPDFStructureName(string $name): bool { |
| 363 |
return preg_match('/^[a-z0-9_-]+$/i', $name) === 1; |
| 364 |
} |
| 365 |
|
| 366 |
/** |
| 367 |
* Installs template structures to the given server profile. |
| 368 |
* |
| 369 |
* @param string $confName server profile name |
| 370 |
* @throws LAMException error during installation |
| 371 |
*/ |
| 372 |
public function installPDFTemplates(string $confName): void { |
| 373 |
if (!LAMConfig::isValidName($confName)) { |
| 374 |
throw new LAMException(_("Profile name is invalid!")); |
| 375 |
} |
| 376 |
$serverProfilesPersistenceManager = new ServerProfilePersistenceManager(); |
| 377 |
$config = $serverProfilesPersistenceManager->loadProfile($confName); |
| 378 |
$typeManager = new TypeManager($config); |
| 379 |
$allTemplates = $this->getPdfStructureTemplateNames(); |
| 380 |
foreach ($typeManager->getConfiguredTypes() as $type) { |
| 381 |
if (empty($allTemplates[$type->getScope()])) { |
| 382 |
continue; |
| 383 |
} |
| 384 |
$existingStructures = $this->getPDFStructures($confName, $type->getId()); |
| 385 |
foreach ($allTemplates[$type->getScope()] as $templateName) { |
| 386 |
if (!in_array($templateName, $existingStructures)) { |
| 387 |
$structure = $this->readPdfStructureTemplate($type->getScope(), $templateName); |
| 388 |
$this->savePdfStructure($confName, $type->getId(), $templateName, $structure); |
| 389 |
} |
| 390 |
} |
| 391 |
} |
| 392 |
$logos = $this->getPdfTemplateLogoNames(); |
| 393 |
$existingLogos = $this->getPdfLogos($confName); |
| 394 |
$existingLogoNames = []; |
| 395 |
foreach ($existingLogos as $existingLogo) { |
| 396 |
$existingLogoNames[] = $existingLogo->getName(); |
| 397 |
} |
| 398 |
foreach ($logos as $logo) { |
| 399 |
if (!in_array($logo, $existingLogoNames)) { |
| 400 |
$binary = $this->getPdfTemplateLogoBinary($logo); |
| 401 |
$this->savePdfLogo($confName, $logo, $binary); |
| 402 |
} |
| 403 |
} |
| 404 |
} |
| 405 |
|
| 406 |
} |
| 407 |
|
| 408 |
/** |
| 409 |
* Logo for PDF structures. |
| 410 |
* |
| 411 |
* @package LAM\PDF |
| 412 |
*/ |
| 413 |
class PdfLogo { |
| 414 |
|
| 415 |
private string $name; |
| 416 |
|
| 417 |
private int $height; |
| 418 |
|
| 419 |
private int $width; |
| 420 |
|
| 421 |
/** |
| 422 |
* Constructor |
| 423 |
* |
| 424 |
* @param string $name file name |
| 425 |
* @param int $height height |
| 426 |
* @param int $width width |
| 427 |
*/ |
| 428 |
public function __construct(string $name, int $height = 0, int $width = 0) { |
| 429 |
$this->name = $name; |
| 430 |
$this->height = $height; |
| 431 |
$this->width = $width; |
| 432 |
} |
| 433 |
|
| 434 |
/** |
| 435 |
* Returns the file name. |
| 436 |
* |
| 437 |
* @return string file name |
| 438 |
*/ |
| 439 |
public function getName(): string { |
| 440 |
return $this->name; |
| 441 |
} |
| 442 |
|
| 443 |
/** |
| 444 |
* Returns the height. |
| 445 |
* |
| 446 |
* @return int height |
| 447 |
*/ |
| 448 |
public function getHeight(): int { |
| 449 |
return $this->height; |
| 450 |
} |
| 451 |
|
| 452 |
/** |
| 453 |
* Returns the width. |
| 454 |
* |
| 455 |
* @return int width |
| 456 |
*/ |
| 457 |
public function getWidth(): int { |
| 458 |
return $this->width; |
| 459 |
} |
| 460 |
|
| 461 |
} |
| 462 |
|
| 463 |
/** |
| 464 |
* Interface for PDF structure persistence. |
| 465 |
* |
| 466 |
* @package LAM\PDF |
| 467 |
*/ |
| 468 |
interface PdfStructurePersistenceStrategy { |
| 469 |
|
| 470 |
/** |
| 471 |
* Returns the names of existing PDF structure templates. |
| 472 |
* |
| 473 |
* @return array<string, string[]> scope => names (e.g. array('user' => array('default'))) |
| 474 |
* @throws LAMException error reading templates |
| 475 |
*/ |
| 476 |
public function getPdfStructureTemplateNames(): array; |
| 477 |
|
| 478 |
/** |
| 479 |
* Deletes a PDF structure template, |
| 480 |
* |
| 481 |
* @param string $scope user/group/host |
| 482 |
* @param string $name template name |
| 483 |
* @throws LAMException error deleting template |
| 484 |
*/ |
| 485 |
public function deletePdfStructureTemplate(string $scope, string $name): void; |
| 486 |
|
| 487 |
/** |
| 488 |
* Reads a PDF structure template. |
| 489 |
* |
| 490 |
* @param string $scope user/group/host |
| 491 |
* @param string $name structure name |
| 492 |
* @return PDFStructure PDF structure |
| 493 |
* @throws LAMException error deleting structure |
| 494 |
*/ |
| 495 |
public function readPdfStructureTemplate(string $scope, string $name): PDFStructure; |
| 496 |
|
| 497 |
/** |
| 498 |
* Saves the PDF structure. |
| 499 |
* |
| 500 |
* @param string $scope user/group/host |
| 501 |
* @param string $name structure name |
| 502 |
* @param PDFStructure $structure structure |
| 503 |
* @throws LAMException error saving structure |
| 504 |
*/ |
| 505 |
public function savePdfStructureTemplate(string $scope, string $name, PDFStructure $structure): void; |
| 506 |
|
| 507 |
/** |
| 508 |
* Returns a list of template logo file names. |
| 509 |
* |
| 510 |
* @return string[] logo file names |
| 511 |
*/ |
| 512 |
public function getPdfTemplateLogoNames(): array; |
| 513 |
|
| 514 |
/** |
| 515 |
* Returns the binary data for the given template logo. |
| 516 |
* |
| 517 |
* @param string $name file name |
| 518 |
* @return string binary |
| 519 |
* @throws LAMException error reading file |
| 520 |
*/ |
| 521 |
public function getPdfTemplateLogoBinary(string $name): string; |
| 522 |
|
| 523 |
/** |
| 524 |
* Deletes a logo in global templates. |
| 525 |
* |
| 526 |
* @param string $name logo name |
| 527 |
* @throws LAMException error during deletion |
| 528 |
*/ |
| 529 |
public function deletePdfTemplateLogo(string $name): void; |
| 530 |
|
| 531 |
/** |
| 532 |
* Saves the template logo. |
| 533 |
* |
| 534 |
* @param string $name file name |
| 535 |
* @param string $data binary data |
| 536 |
* @throws LAMException error during save |
| 537 |
*/ |
| 538 |
public function savePdfTemplateLogo(string $name, string $data): void; |
| 539 |
|
| 540 |
/** |
| 541 |
* Returns the list of available PDF logos. |
| 542 |
* |
| 543 |
* @param string $confName server profile name |
| 544 |
* @return string[] logos |
| 545 |
* @throws LAMException error reading logos |
| 546 |
*/ |
| 547 |
public function getPdfLogoNames(string $confName): array; |
| 548 |
|
| 549 |
/** |
| 550 |
* Reads a PDF logo. |
| 551 |
* |
| 552 |
* @param string $confName server profile name |
| 553 |
* @param string $name file name |
| 554 |
* @return string binary data |
| 555 |
* @throws LAMException error reading logo |
| 556 |
*/ |
| 557 |
public function getPdfLogoBinary(string $confName, string $name): string; |
| 558 |
|
| 559 |
/** |
| 560 |
* Deletes a PDF logo. |
| 561 |
* |
| 562 |
* @param string $confName server profile name |
| 563 |
* @param string $name logo name |
| 564 |
* @throws LAMException error deleting logo |
| 565 |
*/ |
| 566 |
public function deletePdfLogo(string $confName, string $name): void; |
| 567 |
|
| 568 |
/** |
| 569 |
* Saves a PDF logo. |
| 570 |
* |
| 571 |
* @param string $confName server profile name |
| 572 |
* @param string $name logo name |
| 573 |
* @param string $data binary |
| 574 |
* @throws LAMException error saving logo |
| 575 |
*/ |
| 576 |
public function savePdfLogo(string $confName, string $name, string $data): void; |
| 577 |
|
| 578 |
/** |
| 579 |
* Returns all available PDF structure definitions for the submitted account type. |
| 580 |
* |
| 581 |
* @param string $confName server profile name |
| 582 |
* @param string $typeId the account type |
| 583 |
* |
| 584 |
* @return string[] structure names |
| 585 |
* @throws LAMException error reading structures |
| 586 |
*/ |
| 587 |
public function getPDFStructures(string $confName, string $typeId): array; |
| 588 |
|
| 589 |
/** |
| 590 |
* Deletes a PDF structure. |
| 591 |
* |
| 592 |
* @param string $confName server profile name |
| 593 |
* @param string $typeId user/group/host |
| 594 |
* @param string $name structure name |
| 595 |
* @throws LAMException error deleting structure |
| 596 |
*/ |
| 597 |
public function deletePdfStructure(string $confName, string $typeId, string $name): void; |
| 598 |
|
| 599 |
/** |
| 600 |
* Reads a PDF structure. |
| 601 |
* |
| 602 |
* @param string $confName server profile name |
| 603 |
* @param string $typeId user/group/host |
| 604 |
* @param string $name structure name |
| 605 |
* @return PDFStructure PDF structure |
| 606 |
* @throws LAMException error deleting structure |
| 607 |
*/ |
| 608 |
public function readPdfStructure(string $confName, string $typeId, string $name): PDFStructure; |
| 609 |
|
| 610 |
/** |
| 611 |
* Saves the PDF structure. |
| 612 |
* |
| 613 |
* @param string $confName server profile name |
| 614 |
* @param string $typeId user/group/host |
| 615 |
* @param string $name structure name |
| 616 |
* @param PDFStructure $structure structure |
| 617 |
* @throws LAMException error saving structure |
| 618 |
*/ |
| 619 |
public function savePdfStructure(string $confName, string $typeId, string $name, PDFStructure $structure): void; |
| 620 |
|
| 621 |
} |
| 622 |
|
| 623 |
/** |
| 624 |
* Manages PDF structures on file system. |
| 625 |
* |
| 626 |
* @package LAM\PDF |
| 627 |
*/ |
| 628 |
class PdfStructurePersistenceStrategyFiles implements PdfStructurePersistenceStrategy { |
| 629 |
|
| 630 |
/** |
| 631 |
* @inheritDoc |
| 632 |
*/ |
| 633 |
public function getPdfStructureTemplateNames(): array { |
| 634 |
$templatePath = __DIR__ . '/../config/templates/pdf'; |
| 635 |
$templateDir = @dir($templatePath); |
| 636 |
$allTemplates = []; |
| 637 |
if ($templateDir) { |
| 638 |
$entry = $templateDir->read(); |
| 639 |
while ($entry) { |
| 640 |
$parts = explode('.', $entry); |
| 641 |
if ((strlen($entry) > 3) && (count($parts) === 3)) { |
| 642 |
$name = $parts[0]; |
| 643 |
$scope = $parts[1]; |
| 644 |
$allTemplates[$scope][] = $name; |
| 645 |
} |
| 646 |
$entry = $templateDir->read(); |
| 647 |
} |
| 648 |
} |
| 649 |
return $allTemplates; |
| 650 |
} |
| 651 |
|
| 652 |
/** |
| 653 |
* @inheritDoc |
| 654 |
*/ |
| 655 |
public function deletePdfStructureTemplate(string $scope, string $name): void { |
| 656 |
$fileName = $this->getPdfStructureTemplateFileName($scope, $name); |
| 657 |
$deleted = @unlink($fileName); |
| 658 |
if (!$deleted) { |
| 659 |
throw new LAMException(_("Unable to delete PDF structure!")); |
| 660 |
} |
| 661 |
} |
| 662 |
|
| 663 |
/** |
| 664 |
* @inheritDoc |
| 665 |
*/ |
| 666 |
public function readPdfStructureTemplate(string $scope, string $name): PDFStructure { |
| 667 |
$fileName = $this->getPdfStructureTemplateFileName($scope, $name); |
| 668 |
if (!is_file($fileName) || !is_readable($fileName)) { |
| 669 |
throw new LAMException(_('Unable to read PDF structure.')); |
| 670 |
} |
| 671 |
$handle = fopen($fileName, 'r'); |
| 672 |
$xmlData = fread($handle, 100000000); |
| 673 |
fclose($handle); |
| 674 |
$reader = new PDFStructureReader(); |
| 675 |
return $reader->read($xmlData); |
| 676 |
} |
| 677 |
|
| 678 |
/** |
| 679 |
* @inheritDoc |
| 680 |
*/ |
| 681 |
public function savePdfStructureTemplate(string $scope, string $name, PDFStructure $structure): void { |
| 682 |
$fileName = $this->getPdfStructureTemplateFileName($scope, $name); |
| 683 |
$writer = new PDFStructureWriter(); |
| 684 |
$xml = $writer->getXML($structure); |
| 685 |
$file = @fopen($fileName, "w"); |
| 686 |
if (!$file) { |
| 687 |
logNewMessage(LOG_ERR, 'Unable to write ' . $fileName); |
| 688 |
throw new LAMException(_('Unable to save PDF structure.')); |
| 689 |
} |
| 690 |
fwrite($file, $xml); |
| 691 |
fclose($file); |
| 692 |
} |
| 693 |
|
| 694 |
/** |
| 695 |
* Returns the file name for a PDF structure. |
| 696 |
* |
| 697 |
* @param string $scope user/group/host |
| 698 |
* @param string $name structure name |
| 699 |
* @return string file name |
| 700 |
*/ |
| 701 |
private function getPdfStructureTemplateFileName(string $scope, string $name): string { |
| 702 |
return __DIR__ . '/../config/templates/pdf/' . $name . '.' . $scope . '.xml'; |
| 703 |
} |
| 704 |
|
| 705 |
/** |
| 706 |
* @inheritDoc |
| 707 |
*/ |
| 708 |
public function getPdfTemplateLogoNames(): array { |
| 709 |
$templatePath = __DIR__ . '/../config/templates/pdf/logos'; |
| 710 |
$templateDir = @dir($templatePath); |
| 711 |
$logos = []; |
| 712 |
if ($templateDir) { |
| 713 |
$entry = $templateDir->read(); |
| 714 |
while ($entry) { |
| 715 |
if ((!str_starts_with($entry, '.')) && is_file($templatePath . '/' . $entry)) { |
| 716 |
$logos[] = $entry; |
| 717 |
} |
| 718 |
$entry = $templateDir->read(); |
| 719 |
} |
| 720 |
} |
| 721 |
return $logos; |
| 722 |
} |
| 723 |
|
| 724 |
/** |
| 725 |
* @inheritDoc |
| 726 |
*/ |
| 727 |
public function getPdfTemplateLogoBinary(string $name): string { |
| 728 |
$fileName = $this->getPdfTemplateLogoFileName($name); |
| 729 |
$handle = fopen($fileName, 'r'); |
| 730 |
$logoBinary = fread($handle, 100000000); |
| 731 |
fclose($handle); |
| 732 |
return $logoBinary; |
| 733 |
} |
| 734 |
|
| 735 |
/** |
| 736 |
* Returns the file name of a given logo. |
| 737 |
* |
| 738 |
* @param string $name logo name |
| 739 |
* @return string file name |
| 740 |
*/ |
| 741 |
private function getPdfTemplateLogoFileName(string $name): string { |
| 742 |
return __DIR__ . '/../config/templates/pdf/logos/' . $name; |
| 743 |
} |
| 744 |
|
| 745 |
/** |
| 746 |
* @inheritDoc |
| 747 |
*/ |
| 748 |
public function deletePdfTemplateLogo(string $name): void { |
| 749 |
$fileName = $this->getPdfTemplateLogoFileName($name); |
| 750 |
$deleted = @unlink($fileName); |
| 751 |
if (!$deleted) { |
| 752 |
throw new LAMException(_('Unable to delete logo file.')); |
| 753 |
} |
| 754 |
} |
| 755 |
|
| 756 |
/** |
| 757 |
* @inheritDoc |
| 758 |
*/ |
| 759 |
public function savePdfTemplateLogo(string $name, string $data): void { |
| 760 |
$fileName = $this->getPdfTemplateLogoFileName($name); |
| 761 |
$file = @fopen($fileName, "w"); |
| 762 |
if (!$file) { |
| 763 |
logNewMessage(LOG_ERR, 'Unable to write ' . $fileName); |
| 764 |
throw new LAMException(_('Unable to upload logo file.')); |
| 765 |
} |
| 766 |
fwrite($file, $data); |
| 767 |
fclose($file); |
| 768 |
} |
| 769 |
|
| 770 |
/** |
| 771 |
* Returns the file name of a given logo. |
| 772 |
* |
| 773 |
* @param string $confName server profile name |
| 774 |
* @param string $name logo name |
| 775 |
* @return string file name |
| 776 |
*/ |
| 777 |
private function getPdfLogoFileName(string $confName, string $name): string { |
| 778 |
return __DIR__ . '/../config/pdf/' . $confName . '/logos/' . $name; |
| 779 |
} |
| 780 |
|
| 781 |
/** |
| 782 |
* @inheritDoc |
| 783 |
*/ |
| 784 |
public function getPdfLogoNames(string $confName): array { |
| 785 |
$return = []; |
| 786 |
$dirPath = __DIR__ . '/../config/pdf/' . $confName . '/logos/'; |
| 787 |
if (!is_dir($dirPath)) { |
| 788 |
mkdir($dirPath, 0700, true); |
| 789 |
} |
| 790 |
$dirHandle = opendir($dirPath); |
| 791 |
if ($dirHandle === false) { |
| 792 |
throw new LAMException(_('Unable to read logos.')); |
| 793 |
} |
| 794 |
while ($file = readdir($dirHandle)) { |
| 795 |
if (!is_dir($file) && $file !== '.' && $file !== '..' && preg_match('/\\.(jpg|png)$/i', $file)) { |
| 796 |
$return[] = $file; |
| 797 |
} |
| 798 |
} |
| 799 |
return $return; |
| 800 |
} |
| 801 |
|
| 802 |
/** |
| 803 |
* @inheritDoc |
| 804 |
*/ |
| 805 |
public function getPdfLogoBinary(string $confName, string $name): string { |
| 806 |
$fileName = $this->getPdfLogoFileName($confName, $name); |
| 807 |
$handle = fopen($fileName, 'r'); |
| 808 |
$logoBinary = fread($handle, 100000000); |
| 809 |
fclose($handle); |
| 810 |
return $logoBinary; |
| 811 |
} |
| 812 |
|
| 813 |
/** |
| 814 |
* @inheritDoc |
| 815 |
*/ |
| 816 |
public function deletePdfLogo(string $confName, string $name): void { |
| 817 |
// delete file |
| 818 |
$success = @unlink($this->getPdfLogoFileName($confName, $name)); |
| 819 |
if (!$success) { |
| 820 |
throw new LAMException(_('Unable to delete logo file.'), $name); |
| 821 |
} |
| 822 |
} |
| 823 |
|
| 824 |
/** |
| 825 |
* @inheritDoc |
| 826 |
*/ |
| 827 |
public function savePdfLogo(string $confName, string $name, string $data): void { |
| 828 |
$fileName = $this->getPdfLogoFileName($confName, $name); |
| 829 |
$basePath = dirname($fileName); |
| 830 |
if (!file_exists($basePath)) { |
| 831 |
mkdir($basePath, 0700, true); |
| 832 |
} |
| 833 |
$file = @fopen($fileName, "w"); |
| 834 |
if (!$file) { |
| 835 |
logNewMessage(LOG_ERR, 'Unable to write ' . $fileName); |
| 836 |
throw new LAMException(_('Unable to upload logo file.')); |
| 837 |
} |
| 838 |
fwrite($file, $data); |
| 839 |
fclose($file); |
| 840 |
} |
| 841 |
|
| 842 |
/** |
| 843 |
* @inheritDoc |
| 844 |
*/ |
| 845 |
public function getPDFStructures(string $confName, string $typeId): array { |
| 846 |
$return = []; |
| 847 |
$path = __DIR__ . '/../config/pdf/' . $confName; |
| 848 |
if (is_dir($path) && is_readable($path)) { |
| 849 |
$dirHandle = opendir($path); |
| 850 |
while ($file = readdir($dirHandle)) { |
| 851 |
$struct_file = explode('.', $file); |
| 852 |
if (!is_dir($path . $file) |
| 853 |
&& ($file !== '.') |
| 854 |
&& ($file !== '..') |
| 855 |
&& (count($struct_file) === 3) |
| 856 |
&& ($struct_file[1] === $typeId) |
| 857 |
&& ($struct_file[2] === 'xml')) { |
| 858 |
$return[] = $struct_file[0]; |
| 859 |
} |
| 860 |
} |
| 861 |
} |
| 862 |
return $return; |
| 863 |
} |
| 864 |
|
| 865 |
/** |
| 866 |
* @inheritDoc |
| 867 |
*/ |
| 868 |
public function deletePdfStructure(string $confName, string $typeId, string $name): void { |
| 869 |
$fileName = $this->getPdfStructureFileName($confName, $typeId, $name); |
| 870 |
if (!is_file($fileName) || !is_writable($fileName)) { |
| 871 |
logNewMessage(LOG_ERR, 'PDF structure does not exist or is not writable: ' . $fileName); |
| 872 |
throw new LAMException(_('Unable to delete PDF structure!')); |
| 873 |
} |
| 874 |
$deleteOk = @unlink($fileName); |
| 875 |
if (!$deleteOk) { |
| 876 |
logNewMessage(LOG_ERR, 'PDF structure delete failed: ' . $fileName); |
| 877 |
throw new LAMException(_('Unable to delete PDF structure!')); |
| 878 |
} |
| 879 |
} |
| 880 |
|
| 881 |
/** |
| 882 |
* @inheritDoc |
| 883 |
*/ |
| 884 |
public function readPdfStructure(string $confName, string $typeId, string $name): PDFStructure { |
| 885 |
$fileName = $this->getPdfStructureFileName($confName, $typeId, $name); |
| 886 |
if (!is_file($fileName) || !is_readable($fileName)) { |
| 887 |
throw new LAMException(_('Unable to read PDF structure.')); |
| 888 |
} |
| 889 |
$handle = fopen($fileName, 'r'); |
| 890 |
$xmlData = fread($handle, 100000000); |
| 891 |
fclose($handle); |
| 892 |
$reader = new PDFStructureReader(); |
| 893 |
return $reader->read($xmlData); |
| 894 |
} |
| 895 |
|
| 896 |
/** |
| 897 |
* @inheritDoc |
| 898 |
*/ |
| 899 |
public function savePdfStructure(string $confName, string $typeId, string $name, PDFStructure $structure): void { |
| 900 |
$fileName = $this->getPdfStructureFileName($confName, $typeId, $name); |
| 901 |
$basePath = dirname($fileName); |
| 902 |
if (!file_exists($basePath)) { |
| 903 |
mkdir($basePath, 0700, true); |
| 904 |
} |
| 905 |
$writer = new PDFStructureWriter(); |
| 906 |
$xml = $writer->getXML($structure); |
| 907 |
$file = @fopen($fileName, "w"); |
| 908 |
if (!$file) { |
| 909 |
logNewMessage(LOG_ERR, 'Unable to write ' . $fileName); |
| 910 |
throw new LAMException(_('Unable to save PDF structure.')); |
| 911 |
} |
| 912 |
fwrite($file, $xml); |
| 913 |
fclose($file); |
| 914 |
} |
| 915 |
|
| 916 |
/** |
| 917 |
* Returns the file name of the structure. |
| 918 |
* |
| 919 |
* @param string $confName server profile name |
| 920 |
* @param string $typeId user/group/host |
| 921 |
* @param string $name structure name |
| 922 |
* @return string file name |
| 923 |
*/ |
| 924 |
private function getPdfStructureFileName(string $confName, string $typeId, string $name): string { |
| 925 |
return __DIR__ . '/../config/pdf/' . $confName . '/' . $name . '.' . $typeId . '.xml'; |
| 926 |
} |
| 927 |
|
| 928 |
} |
| 929 |
|
| 930 |
/** |
| 931 |
* Manages PDF structures on file system. |
| 932 |
* |
| 933 |
* @package LAM\PDF |
| 934 |
*/ |
| 935 |
class PdfStructurePersistenceStrategyPdo implements PdfStructurePersistenceStrategy { |
| 936 |
|
| 937 |
private const TABLE_NAME = 'pdf_structures'; |
| 938 |
private const TABLE_NAME_LOGOS = 'pdf_logos'; |
| 939 |
private const TABLE_NAME_TEMPLATES = 'pdf_structures_templates'; |
| 940 |
private const TABLE_NAME_TEMPLATES_LOGOS = 'pdf_logos_templates'; |
| 941 |
|
| 942 |
/** |
| 943 |
* @var PDO |
| 944 |
*/ |
| 945 |
private $pdo; |
| 946 |
|
| 947 |
/** |
| 948 |
* Constructor |
| 949 |
* |
| 950 |
* @param PDO $pdo PDO |
| 951 |
*/ |
| 952 |
public function __construct(PDO $pdo) { |
| 953 |
$this->pdo = $pdo; |
| 954 |
$this->checkSchema(); |
| 955 |
} |
| 956 |
|
| 957 |
/** |
| 958 |
* Checks if the schema has latest version. |
| 959 |
*/ |
| 960 |
private function checkSchema(): void { |
| 961 |
if (!dbTableExists($this->pdo, self::TABLE_NAME)) { |
| 962 |
$this->createInitialSchema(); |
| 963 |
} |
| 964 |
} |
| 965 |
|
| 966 |
/** |
| 967 |
* Creates the initial schema. |
| 968 |
*/ |
| 969 |
public function createInitialSchema(): void { |
| 970 |
logNewMessage(LOG_DEBUG, 'Creating database table ' . self::TABLE_NAME); |
| 971 |
$sql = 'create table ' . self::TABLE_NAME . '(' |
| 972 |
. 'position int NOT NULL,' |
| 973 |
. 'confname VARCHAR(300) NOT NULL,' |
| 974 |
. 'typeid VARCHAR(300) NOT NULL,' |
| 975 |
. 'name VARCHAR(300) NOT NULL,' |
| 976 |
. 'data TEXT NOT NULL,' |
| 977 |
. 'PRIMARY KEY(position)' |
| 978 |
. ');'; |
| 979 |
$this->pdo->exec($sql); |
| 980 |
logNewMessage(LOG_DEBUG, 'Creating database table ' . self::TABLE_NAME_TEMPLATES); |
| 981 |
$sql = 'create table ' . self::TABLE_NAME_TEMPLATES . '(' |
| 982 |
. 'scope VARCHAR(100) NOT NULL,' |
| 983 |
. 'name VARCHAR(300) NOT NULL,' |
| 984 |
. 'data TEXT NOT NULL,' |
| 985 |
. 'PRIMARY KEY(scope,name)' |
| 986 |
. ');'; |
| 987 |
$this->pdo->exec($sql); |
| 988 |
logNewMessage(LOG_DEBUG, 'Creating database table ' . self::TABLE_NAME_LOGOS); |
| 989 |
$sql = 'create table ' . self::TABLE_NAME_LOGOS . '(' |
| 990 |
. 'confname VARCHAR(300) NOT NULL,' |
| 991 |
. 'name VARCHAR(300) NOT NULL,' |
| 992 |
. 'data LONGBLOB NOT NULL,' |
| 993 |
. 'PRIMARY KEY(confname,name)' |
| 994 |
. ');'; |
| 995 |
$this->pdo->exec($sql); |
| 996 |
logNewMessage(LOG_DEBUG, 'Creating database table ' . self::TABLE_NAME_TEMPLATES_LOGOS); |
| 997 |
$sql = 'create table ' . self::TABLE_NAME_TEMPLATES_LOGOS . '(' |
| 998 |
. 'name VARCHAR(300) NOT NULL,' |
| 999 |
. 'data LONGBLOB NOT NULL,' |
| 1000 |
. 'PRIMARY KEY(name)' |
| 1001 |
. ');'; |
| 1002 |
$this->pdo->exec($sql); |
| 1003 |
$sql = 'insert into ' . ConfigurationDatabase::TABLE_SCHEMA_VERSIONS . ' (name, version) VALUES ("pdf_structures", 1);'; |
| 1004 |
$this->pdo->exec($sql); |
| 1005 |
} |
| 1006 |
|
| 1007 |
/** |
| 1008 |
* @inheritDoc |
| 1009 |
*/ |
| 1010 |
public function getPdfStructureTemplateNames(): array { |
| 1011 |
$statement = $this->pdo->prepare("SELECT scope, name FROM " . self::TABLE_NAME_TEMPLATES); |
| 1012 |
$statement->execute(); |
| 1013 |
$results = $statement->fetchAll(); |
| 1014 |
$profiles = []; |
| 1015 |
foreach ($results as $result) { |
| 1016 |
$profiles[$result['scope']][] = $result['name']; |
| 1017 |
} |
| 1018 |
return $profiles; |
| 1019 |
} |
| 1020 |
|
| 1021 |
/** |
| 1022 |
* @inheritDoc |
| 1023 |
*/ |
| 1024 |
public function deletePdfStructureTemplate(string $scope, string $name): void { |
| 1025 |
$statement = $this->pdo->prepare("DELETE FROM " . self::TABLE_NAME_TEMPLATES . " WHERE scope = ? AND name = ?"); |
| 1026 |
$statement->execute([$scope, $name]); |
| 1027 |
} |
| 1028 |
|
| 1029 |
/** |
| 1030 |
* @inheritDoc |
| 1031 |
*/ |
| 1032 |
public function readPdfStructureTemplate(string $scope, string $name): PDFStructure { |
| 1033 |
$statement = $this->pdo->prepare("SELECT data FROM " . self::TABLE_NAME_TEMPLATES . ' WHERE scope = ? AND name = ?'); |
| 1034 |
$statement->execute([$scope, $name]); |
| 1035 |
$results = $statement->fetchAll(); |
| 1036 |
if (empty($results)) { |
| 1037 |
throw new LAMException(_('Unable to read PDF structure.')); |
| 1038 |
} |
| 1039 |
$structure = new PDFStructure(); |
| 1040 |
$structure->import(json_decode($results[0]['data'], true)); |
| 1041 |
return $structure; |
| 1042 |
} |
| 1043 |
|
| 1044 |
/** |
| 1045 |
* @inheritDoc |
| 1046 |
*/ |
| 1047 |
public function savePdfStructureTemplate(string $scope, string $name, PDFStructure $structure): void { |
| 1048 |
$json = json_encode($structure->export()); |
| 1049 |
$statement = $this->pdo->prepare("SELECT name FROM " . self::TABLE_NAME_TEMPLATES . " WHERE scope = ? AND name = ?"); |
| 1050 |
$statement->execute([$scope, $name]); |
| 1051 |
$results = $statement->fetchAll(); |
| 1052 |
$isExisting = !empty($results); |
| 1053 |
if ($isExisting) { |
| 1054 |
$statement = $this->pdo->prepare("UPDATE " . self::TABLE_NAME_TEMPLATES . " SET data = ? WHERE scope = ? AND name = ?"); |
| 1055 |
$statement->execute([$json, $scope, $name]); |
| 1056 |
} |
| 1057 |
else { |
| 1058 |
$statement = $this->pdo->prepare("INSERT INTO " . self::TABLE_NAME_TEMPLATES . " (scope, name, data) VALUES (?, ?, ?)"); |
| 1059 |
$statement->execute([$scope, $name, $json]); |
| 1060 |
} |
| 1061 |
} |
| 1062 |
|
| 1063 |
/** |
| 1064 |
* @inheritDoc |
| 1065 |
*/ |
| 1066 |
public function getPdfTemplateLogoNames(): array { |
| 1067 |
$statement = $this->pdo->prepare("SELECT name FROM " . self::TABLE_NAME_TEMPLATES_LOGOS); |
| 1068 |
$statement->execute(); |
| 1069 |
$results = $statement->fetchAll(); |
| 1070 |
$logos = []; |
| 1071 |
foreach ($results as $result) { |
| 1072 |
$logos[] = $result['name']; |
| 1073 |
} |
| 1074 |
return $logos; |
| 1075 |
} |
| 1076 |
|
| 1077 |
/** |
| 1078 |
* @inheritDoc |
| 1079 |
*/ |
| 1080 |
public function getPdfTemplateLogoBinary(string $name): string { |
| 1081 |
$statement = $this->pdo->prepare("SELECT data FROM " . self::TABLE_NAME_TEMPLATES_LOGOS . ' WHERE name = ?'); |
| 1082 |
$statement->execute([$name]); |
| 1083 |
$results = $statement->fetchAll(); |
| 1084 |
if (empty($results)) { |
| 1085 |
throw new LAMException(_('Unable to read logo file.')); |
| 1086 |
} |
| 1087 |
return $results[0]['data']; |
| 1088 |
} |
| 1089 |
|
| 1090 |
/** |
| 1091 |
* @inheritDoc |
| 1092 |
*/ |
| 1093 |
public function deletePdfTemplateLogo(string $name): void { |
| 1094 |
$statement = $this->pdo->prepare("DELETE FROM " . self::TABLE_NAME_TEMPLATES_LOGOS . " WHERE name = ?"); |
| 1095 |
$statement->execute([$name]); |
| 1096 |
} |
| 1097 |
|
| 1098 |
/** |
| 1099 |
* @inheritDoc |
| 1100 |
*/ |
| 1101 |
public function savePdfTemplateLogo(string $name, string $data): void { |
| 1102 |
$statement = $this->pdo->prepare("SELECT name FROM " . self::TABLE_NAME_TEMPLATES_LOGOS . " WHERE name = ?"); |
| 1103 |
$statement->execute([$name]); |
| 1104 |
$results = $statement->fetchAll(); |
| 1105 |
$isExisting = !empty($results); |
| 1106 |
if ($isExisting) { |
| 1107 |
$statement = $this->pdo->prepare("UPDATE " . self::TABLE_NAME_TEMPLATES_LOGOS . " SET data = ? WHERE name = ?"); |
| 1108 |
$statement->execute([$data, $name]); |
| 1109 |
} |
| 1110 |
else { |
| 1111 |
$statement = $this->pdo->prepare("INSERT INTO " . self::TABLE_NAME_TEMPLATES_LOGOS . " (name, data) VALUES (?, ?)"); |
| 1112 |
$statement->execute([$name, $data]); |
| 1113 |
} |
| 1114 |
} |
| 1115 |
|
| 1116 |
/** |
| 1117 |
* @inheritDoc |
| 1118 |
*/ |
| 1119 |
public function getPdfLogoNames(string $confName): array { |
| 1120 |
$statement = $this->pdo->prepare("SELECT name FROM " . self::TABLE_NAME_LOGOS . ' WHERE confname = ?'); |
| 1121 |
$statement->execute([$confName]); |
| 1122 |
$results = $statement->fetchAll(); |
| 1123 |
$logos = []; |
| 1124 |
foreach ($results as $result) { |
| 1125 |
$logos[] = $result['name']; |
| 1126 |
} |
| 1127 |
return $logos; |
| 1128 |
} |
| 1129 |
|
| 1130 |
/** |
| 1131 |
* @inheritDoc |
| 1132 |
*/ |
| 1133 |
public function getPdfLogoBinary(string $confName, string $name): string { |
| 1134 |
$statement = $this->pdo->prepare("SELECT data FROM " . self::TABLE_NAME_LOGOS . ' WHERE confname = ? AND name = ?'); |
| 1135 |
$statement->execute([$confName, $name]); |
| 1136 |
$results = $statement->fetchAll(); |
| 1137 |
if (empty($results)) { |
| 1138 |
throw new LAMException(_('Unable to read logo file.')); |
| 1139 |
} |
| 1140 |
return $results[0]['data']; |
| 1141 |
} |
| 1142 |
|
| 1143 |
/** |
| 1144 |
* @inheritDoc |
| 1145 |
*/ |
| 1146 |
public function deletePdfLogo(string $confName, string $name): void { |
| 1147 |
$statement = $this->pdo->prepare("DELETE FROM " . self::TABLE_NAME_LOGOS . " WHERE confname = ? AND name = ?"); |
| 1148 |
$statement->execute([$confName, $name]); |
| 1149 |
} |
| 1150 |
|
| 1151 |
/** |
| 1152 |
* @inheritDoc |
| 1153 |
*/ |
| 1154 |
public function savePdfLogo(string $confName, string $name, string $data): void { |
| 1155 |
$statement = $this->pdo->prepare("SELECT name FROM " . self::TABLE_NAME_LOGOS . " WHERE confname = ? AND name = ?"); |
| 1156 |
$statement->execute([$confName, $name]); |
| 1157 |
$results = $statement->fetchAll(); |
| 1158 |
$isExisting = !empty($results); |
| 1159 |
if ($isExisting) { |
| 1160 |
$statement = $this->pdo->prepare("UPDATE " . self::TABLE_NAME_LOGOS . " SET data = ? WHERE confname = ? AND name = ?"); |
| 1161 |
$statement->execute([$data, $confName, $name]); |
| 1162 |
} |
| 1163 |
else { |
| 1164 |
$statement = $this->pdo->prepare("INSERT INTO " . self::TABLE_NAME_LOGOS . " (confname, name, data) VALUES (?, ?, ?)"); |
| 1165 |
$statement->execute([$confName, $name, $data]); |
| 1166 |
} |
| 1167 |
} |
| 1168 |
|
| 1169 |
/** |
| 1170 |
* @inheritDoc |
| 1171 |
*/ |
| 1172 |
public function getPDFStructures(string $confName, string $typeId): array { |
| 1173 |
$statement = $this->pdo->prepare("SELECT name FROM " . self::TABLE_NAME . ' WHERE confname = ? AND typeid = ?'); |
| 1174 |
$statement->execute([$confName, $typeId]); |
| 1175 |
$results = $statement->fetchAll(); |
| 1176 |
$names = []; |
| 1177 |
foreach ($results as $result) { |
| 1178 |
$names[] = $result['name']; |
| 1179 |
} |
| 1180 |
return $names; |
| 1181 |
} |
| 1182 |
|
| 1183 |
/** |
| 1184 |
* @inheritDoc |
| 1185 |
*/ |
| 1186 |
public function deletePdfStructure(string $confName, string $typeId, string $name): void { |
| 1187 |
$statement = $this->pdo->prepare("DELETE FROM " . self::TABLE_NAME . " WHERE confname = ? AND typeid = ? AND name = ?"); |
| 1188 |
$statement->execute([$confName, $typeId, $name]); |
| 1189 |
} |
| 1190 |
|
| 1191 |
/** |
| 1192 |
* @inheritDoc |
| 1193 |
*/ |
| 1194 |
public function readPdfStructure(string $confName, string $typeId, string $name): PDFStructure { |
| 1195 |
$statement = $this->pdo->prepare("SELECT data FROM " . self::TABLE_NAME . ' WHERE confname = ? AND typeid = ? AND name = ?'); |
| 1196 |
$statement->execute([$confName, $typeId, $name]); |
| 1197 |
$results = $statement->fetchAll(); |
| 1198 |
if (empty($results)) { |
| 1199 |
throw new LAMException(_('Unable to read PDF structure.')); |
| 1200 |
} |
| 1201 |
$structure = new PDFStructure(); |
| 1202 |
$structure->import(json_decode($results[0]['data'], true)); |
| 1203 |
return $structure; |
| 1204 |
} |
| 1205 |
|
| 1206 |
/** |
| 1207 |
* @inheritDoc |
| 1208 |
*/ |
| 1209 |
public function savePdfStructure(string $confName, string $typeId, string $name, PDFStructure $structure): void { |
| 1210 |
$json = json_encode($structure->export()); |
| 1211 |
$statement = $this->pdo->prepare("SELECT name FROM " . self::TABLE_NAME . " WHERE confname = ? AND typeid = ? AND name = ?"); |
| 1212 |
$statement->execute([$confName, $typeId, $name]); |
| 1213 |
$results = $statement->fetchAll(); |
| 1214 |
$isExisting = !empty($results); |
| 1215 |
if ($isExisting) { |
| 1216 |
$statement = $this->pdo->prepare("UPDATE " . self::TABLE_NAME . " SET data = ? WHERE confname = ? AND typeid = ? AND name = ?"); |
| 1217 |
$statement->execute([$json, $confName, $typeId, $name]); |
| 1218 |
} |
| 1219 |
else { |
| 1220 |
$positionStatement = $this->pdo->prepare("SELECT MAX(position) AS position FROM " . self::TABLE_NAME); |
| 1221 |
$positionStatement->execute(); |
| 1222 |
$positionResult = $positionStatement->fetchAll(); |
| 1223 |
$position = $positionResult[0]['position'] + 1; |
| 1224 |
$statement = $this->pdo->prepare("INSERT INTO " . self::TABLE_NAME . " (position, confname, typeid, name, data) VALUES (?, ?, ?, ?, ?)"); |
| 1225 |
$statement->execute([$position, $confName, $typeId, $name, $json]); |
| 1226 |
} |
| 1227 |
} |
| 1228 |
|
| 1229 |
} |
| 1230 |
|
| 1231 |
/** |
| 1232 |
* Reads a PDF structure. |
| 1233 |
* |
| 1234 |
* @author Roland Gruber |
| 1235 |
*/ |
| 1236 |
class PDFStructureReader { |
| 1237 |
|
| 1238 |
/** |
| 1239 |
* Reads a PDF structure file. |
| 1240 |
* |
| 1241 |
* @param string $data XML data |
| 1242 |
* @return PDFStructure structure |
| 1243 |
* @throws LAMException error reading structure |
| 1244 |
*/ |
| 1245 |
public function read(string $data): PDFStructure { |
| 1246 |
$xml = new XMLReader(); |
| 1247 |
$xml->XML($data); |
| 1248 |
$structure = new PDFStructure(); |
| 1249 |
// open <pdf> |
| 1250 |
@$xml->read(); |
| 1251 |
if (!$xml->name == 'pdf') { |
| 1252 |
logNewMessage(LOG_ERR, 'Unknown tag name: ' . $xml->name); |
| 1253 |
throw new LAMException(_('Unable to read PDF structure.')); |
| 1254 |
} |
| 1255 |
$structure->setLogo($xml->getAttribute('filename')); |
| 1256 |
$structure->setTitle($xml->getAttribute('headline')); |
| 1257 |
$structure->setFoldingMarks($xml->getAttribute('foldingmarks')); |
| 1258 |
$sections = []; |
| 1259 |
while ($xml->read()) { |
| 1260 |
if (($xml->nodeType === XMLReader::SIGNIFICANT_WHITESPACE) |
| 1261 |
|| (($xml->name === 'pdf') && ($xml->nodeType == XMLReader::END_ELEMENT))) { |
| 1262 |
continue; |
| 1263 |
} |
| 1264 |
elseif ($xml->name === 'text') { |
| 1265 |
$xml->read(); |
| 1266 |
$sections[] = new PDFTextSection($xml->value); |
| 1267 |
$xml->read(); |
| 1268 |
if ($xml->name !== 'text') { |
| 1269 |
logNewMessage(LOG_ERR, 'Unexpected tag name: ' . $xml->name); |
| 1270 |
throw new LAMException(_('Unable to read PDF structure.')); |
| 1271 |
} |
| 1272 |
} |
| 1273 |
elseif ($xml->name === 'section') { |
| 1274 |
$sections[] = $this->readSection($xml); |
| 1275 |
} |
| 1276 |
else { |
| 1277 |
logNewMessage(LOG_ERR, 'Unexpected tag name: ' . $xml->name); |
| 1278 |
throw new LAMException(_('Unable to read PDF structure.')); |
| 1279 |
} |
| 1280 |
} |
| 1281 |
$xml->close(); |
| 1282 |
$structure->setSections($sections); |
| 1283 |
return $structure; |
| 1284 |
} |
| 1285 |
|
| 1286 |
/** |
| 1287 |
* Reads a single section from XML. |
| 1288 |
* |
| 1289 |
* @param XMLReader $xml reader |
| 1290 |
* @return PDFEntrySection section |
| 1291 |
* @throws LAMException unable to parse section |
| 1292 |
*/ |
| 1293 |
private function readSection(XMLReader $xml): PDFEntrySection { |
| 1294 |
$section = new PDFEntrySection($xml->getAttribute('name')); |
| 1295 |
$entries = []; |
| 1296 |
if (!$xml->isEmptyElement) { |
| 1297 |
while ($xml->read()) { |
| 1298 |
if (($xml->name === 'section') && ($xml->nodeType == XMLReader::END_ELEMENT)) { |
| 1299 |
break; |
| 1300 |
} |
| 1301 |
elseif (($xml->nodeType === XMLReader::END_ELEMENT) |
| 1302 |
|| ($xml->nodeType === XMLReader::SIGNIFICANT_WHITESPACE)) { |
| 1303 |
continue; |
| 1304 |
} |
| 1305 |
elseif ($xml->name === 'entry') { |
| 1306 |
$entries[] = new PDFSectionEntry($xml->getAttribute('name')); |
| 1307 |
} |
| 1308 |
else { |
| 1309 |
logNewMessage(LOG_ERR, 'Unexpected tag name inside section: ' . $xml->name); |
| 1310 |
throw new LAMException(_('Unable to read PDF structure.')); |
| 1311 |
} |
| 1312 |
} |
| 1313 |
} |
| 1314 |
$section->setEntries($entries); |
| 1315 |
return $section; |
| 1316 |
} |
| 1317 |
|
| 1318 |
} |
| 1319 |
|
| 1320 |
/** |
| 1321 |
* Writes PDF structures to files. |
| 1322 |
* |
| 1323 |
* @author Roland Gruber |
| 1324 |
*/ |
| 1325 |
class PDFStructureWriter { |
| 1326 |
|
| 1327 |
/** |
| 1328 |
* Returns the generated XML. |
| 1329 |
* |
| 1330 |
* @param PDFStructure $structure structure |
| 1331 |
* @return string XML |
| 1332 |
*/ |
| 1333 |
public function getXML(PDFStructure $structure): string { |
| 1334 |
$writer = new XMLWriter(); |
| 1335 |
$writer->openMemory(); |
| 1336 |
$writer->setIndent(true); |
| 1337 |
$writer->setIndentString("\t"); |
| 1338 |
$writer->startElement('pdf'); |
| 1339 |
$writer->writeAttribute('filename', $structure->getLogo()); |
| 1340 |
$writer->writeAttribute('headline', $structure->getTitle()); |
| 1341 |
$writer->writeAttribute('foldingmarks', $structure->getFoldingMarks()); |
| 1342 |
foreach ($structure->getSections() as $section) { |
| 1343 |
if ($section instanceof PDFTextSection) { |
| 1344 |
$writer->startElement('text'); |
| 1345 |
$writer->text($section->getText()); |
| 1346 |
} |
| 1347 |
else { |
| 1348 |
$writer->startElement('section'); |
| 1349 |
if ($section->isAttributeTitle()) { |
| 1350 |
$writer->writeAttribute('name', '_' . $section->getPdfKey()); |
| 1351 |
} |
| 1352 |
else { |
| 1353 |
$writer->writeAttribute('name', $section->getTitle()); |
| 1354 |
} |
| 1355 |
foreach ($section->getEntries() as $entry) { |
| 1356 |
$writer->startElement('entry'); |
| 1357 |
$writer->writeAttribute('name', $entry->getKey()); |
| 1358 |
$writer->endElement(); |
| 1359 |
} |
| 1360 |
} |
| 1361 |
$writer->endElement(); |
| 1362 |
} |
| 1363 |
$writer->endElement(); |
| 1364 |
return $writer->outputMemory(); |
| 1365 |
} |
| 1366 |
|
| 1367 |
} |
| 1368 |
|
| 1369 |
/** |
| 1370 |
* PDF structure |
| 1371 |
* |
| 1372 |
* @author Roland Gruber |
| 1373 |
*/ |
| 1374 |
class PDFStructure { |
| 1375 |
|
| 1376 |
/** no folding marks */ |
| 1377 |
public const FOLDING_NONE = 'no'; |
| 1378 |
/** standard folding marks */ |
| 1379 |
public const FOLDING_STANDARD = 'standard'; |
| 1380 |
public const NO_LOGO = 'none'; |
| 1381 |
|
| 1382 |
private string $logo = self::NO_LOGO; |
| 1383 |
|
| 1384 |
private string $title = 'LDAP Account Manager'; |
| 1385 |
|
| 1386 |
private string $foldingMarks = 'no'; |
| 1387 |
|
| 1388 |
/** @var PDFTextSection[]|PDFEntrySection[] $sections sections */ |
| 1389 |
private array $sections = []; |
| 1390 |
|
| 1391 |
/** |
| 1392 |
* Returns an array representation of the structure. |
| 1393 |
* |
| 1394 |
* @return array<string, string|string[]|array<string, string|string[]>[]> export data |
| 1395 |
*/ |
| 1396 |
public function export(): array { |
| 1397 |
$data = []; |
| 1398 |
$data['title'] = $this->title; |
| 1399 |
$data['foldingMarks'] = $this->foldingMarks; |
| 1400 |
$data['logo'] = $this->logo; |
| 1401 |
$data['sections'] = []; |
| 1402 |
foreach ($this->sections as $section) { |
| 1403 |
$type = ($section instanceof PDFTextSection) ? 'text' : 'entry'; |
| 1404 |
$sectionData = $section->export(); |
| 1405 |
$data['sections'][] = [ |
| 1406 |
'type' => $type, |
| 1407 |
'data' => $sectionData |
| 1408 |
]; |
| 1409 |
} |
| 1410 |
return $data; |
| 1411 |
} |
| 1412 |
|
| 1413 |
/** |
| 1414 |
* Imports an array representation of the structure. |
| 1415 |
* |
| 1416 |
* @param array<string, string|string[]|array<string, string|string[]>[]> $data import data |
| 1417 |
*/ |
| 1418 |
public function import(array $data): void { |
| 1419 |
if (isset($data['title'])) { |
| 1420 |
$this->setTitle($data['title']); |
| 1421 |
} |
| 1422 |
if (isset($data['foldingMarks'])) { |
| 1423 |
$this->setFoldingMarks($data['foldingMarks']); |
| 1424 |
} |
| 1425 |
if (isset($data['logo'])) { |
| 1426 |
$this->setLogo($data['logo']); |
| 1427 |
} |
| 1428 |
if (isset($data['sections'])) { |
| 1429 |
foreach ($data['sections'] as $section) { |
| 1430 |
if ($section['type'] === 'text') { |
| 1431 |
$this->sections[] = new PDFTextSection($section['data']); |
| 1432 |
} |
| 1433 |
else { |
| 1434 |
$entrySection = new PDFEntrySection(''); |
| 1435 |
$entrySection->import($section['data']); |
| 1436 |
$this->sections[] = $entrySection; |
| 1437 |
} |
| 1438 |
} |
| 1439 |
} |
| 1440 |
} |
| 1441 |
|
| 1442 |
/** |
| 1443 |
* Returns the logo file path. |
| 1444 |
* |
| 1445 |
* @return string logo |
| 1446 |
*/ |
| 1447 |
public function getLogo(): string { |
| 1448 |
return $this->logo; |
| 1449 |
} |
| 1450 |
|
| 1451 |
/** |
| 1452 |
* Sets the logo file path. |
| 1453 |
* |
| 1454 |
* @param string|null $logo logo |
| 1455 |
* @throws LAMException invalid logo |
| 1456 |
*/ |
| 1457 |
public function setLogo(?string $logo): void { |
| 1458 |
if (empty($logo)) { |
| 1459 |
$logo = self::NO_LOGO; |
| 1460 |
} |
| 1461 |
if (!self::isValidLogoFileName($logo)) { |
| 1462 |
throw new LAMException('Invalid logo name: ' . $logo); |
| 1463 |
} |
| 1464 |
$this->logo = $logo; |
| 1465 |
} |
| 1466 |
|
| 1467 |
/** |
| 1468 |
* Returns the title. |
| 1469 |
* |
| 1470 |
* @return string title |
| 1471 |
*/ |
| 1472 |
public function getTitle(): string { |
| 1473 |
return $this->title; |
| 1474 |
} |
| 1475 |
|
| 1476 |
/** |
| 1477 |
* Sets the title. |
| 1478 |
* |
| 1479 |
* @param string $title title |
| 1480 |
*/ |
| 1481 |
public function setTitle(string $title): void { |
| 1482 |
$this->title = $title; |
| 1483 |
} |
| 1484 |
|
| 1485 |
/** |
| 1486 |
* Returns if to print folding marks. |
| 1487 |
* |
| 1488 |
* @return string print folding marks |
| 1489 |
*/ |
| 1490 |
public function getFoldingMarks(): string { |
| 1491 |
return $this->foldingMarks; |
| 1492 |
} |
| 1493 |
|
| 1494 |
/** |
| 1495 |
* Sets if to print folding marks. |
| 1496 |
* |
| 1497 |
* @param string|null $foldingMarks print folding marks |
| 1498 |
* @throws LAMException invalid folding marks |
| 1499 |
*/ |
| 1500 |
public function setFoldingMarks(?string $foldingMarks): void { |
| 1501 |
if (empty($foldingMarks)) { |
| 1502 |
$foldingMarks = self::FOLDING_NONE; |
| 1503 |
} |
| 1504 |
if (!in_array($foldingMarks, [self::FOLDING_NONE, self::FOLDING_STANDARD], true)) { |
| 1505 |
throw new LAMException('Invalid folding marks specified'); |
| 1506 |
} |
| 1507 |
$this->foldingMarks = $foldingMarks; |
| 1508 |
} |
| 1509 |
|
| 1510 |
/** |
| 1511 |
* Returns the sections. |
| 1512 |
* |
| 1513 |
* @return PDFTextSection[]|PDFEntrySection[] $sections |
| 1514 |
*/ |
| 1515 |
public function getSections(): array { |
| 1516 |
return $this->sections; |
| 1517 |
} |
| 1518 |
|
| 1519 |
/** |
| 1520 |
* Sets the sections. |
| 1521 |
* |
| 1522 |
* @param PDFEntrySection[]|PDFTextSection[] $sections sections |
| 1523 |
*/ |
| 1524 |
public function setSections(array $sections): void { |
| 1525 |
$this->sections = $sections; |
| 1526 |
} |
| 1527 |
|
| 1528 |
/** |
| 1529 |
* Returns if the given logo file name is valid. |
| 1530 |
* |
| 1531 |
* @param string $fileName file name |
| 1532 |
* @return bool valid |
| 1533 |
*/ |
| 1534 |
public static function isValidLogoFileName(string $fileName): bool { |
| 1535 |
return ($fileName === self::NO_LOGO) || preg_match('/^[a-z0-9_-]+\\.(png|jpg)$/i', $fileName) === 1; |
| 1536 |
} |
| 1537 |
|
| 1538 |
} |
| 1539 |
|
| 1540 |
/** |
| 1541 |
* Section for static text. |
| 1542 |
* |
| 1543 |
* @author Roland Gruber |
| 1544 |
*/ |
| 1545 |
class PDFTextSection { |
| 1546 |
|
| 1547 |
private string $text; |
| 1548 |
|
| 1549 |
/** |
| 1550 |
* Constructor. |
| 1551 |
* |
| 1552 |
* @param string $text text |
| 1553 |
*/ |
| 1554 |
public function __construct(string $text) { |
| 1555 |
$this->text = $text; |
| 1556 |
} |
| 1557 |
|
| 1558 |
/** |
| 1559 |
* Exports the section. |
| 1560 |
* |
| 1561 |
* @return string text |
| 1562 |
*/ |
| 1563 |
public function export(): string { |
| 1564 |
return $this->getText(); |
| 1565 |
} |
| 1566 |
|
| 1567 |
/** |
| 1568 |
* Returns the text. |
| 1569 |
* |
| 1570 |
* @return string text |
| 1571 |
*/ |
| 1572 |
public function getText(): string { |
| 1573 |
return $this->text; |
| 1574 |
} |
| 1575 |
|
| 1576 |
} |
| 1577 |
|
| 1578 |
/** |
| 1579 |
* PDF section that contains LDAP data entries. |
| 1580 |
* |
| 1581 |
* @author Roland Gruber |
| 1582 |
*/ |
| 1583 |
class PDFEntrySection { |
| 1584 |
|
| 1585 |
private string $title; |
| 1586 |
/** @var PDFSectionEntry[] $entries entries */ |
| 1587 |
private array $entries = []; |
| 1588 |
|
| 1589 |
/** |
| 1590 |
* Constructor |
| 1591 |
* |
| 1592 |
* @param string $title title |
| 1593 |
*/ |
| 1594 |
public function __construct(string $title) { |
| 1595 |
$this->title = $title; |
| 1596 |
} |
| 1597 |
|
| 1598 |
/** |
| 1599 |
* Exports the section. |
| 1600 |
* |
| 1601 |
* @return array<string, string|string[]> export data |
| 1602 |
*/ |
| 1603 |
public function export(): array { |
| 1604 |
$data = []; |
| 1605 |
$data['title'] = $this->title; |
| 1606 |
$data['entries'] = []; |
| 1607 |
foreach ($this->getEntries() as $entry) { |
| 1608 |
$data['entries'][] = $entry->getKey(); |
| 1609 |
} |
| 1610 |
return $data; |
| 1611 |
} |
| 1612 |
|
| 1613 |
/** |
| 1614 |
* Imports the section. |
| 1615 |
* |
| 1616 |
* @param array<string, string|string[]> $data import data |
| 1617 |
*/ |
| 1618 |
public function import(array $data): void { |
| 1619 |
if (isset($data['title'])) { |
| 1620 |
$this->title = $data['title']; |
| 1621 |
} |
| 1622 |
if ($data['entries']) { |
| 1623 |
foreach ($data['entries'] as $entry) { |
| 1624 |
$this->entries[] = new PDFSectionEntry($entry); |
| 1625 |
} |
| 1626 |
} |
| 1627 |
} |
| 1628 |
|
| 1629 |
/** |
| 1630 |
* Returns if the title is an attribute value. |
| 1631 |
* |
| 1632 |
* @return bool is attribute |
| 1633 |
*/ |
| 1634 |
public function isAttributeTitle(): bool { |
| 1635 |
return (bool) preg_match('/^_([a-zA-Z0-9_-])+$/', $this->title); |
| 1636 |
} |
| 1637 |
|
| 1638 |
/** |
| 1639 |
* Returns the PDF key name. |
| 1640 |
* |
| 1641 |
* @return string PDF key name |
| 1642 |
*/ |
| 1643 |
public function getPdfKey(): string { |
| 1644 |
return substr($this->title, 1); |
| 1645 |
} |
| 1646 |
|
| 1647 |
/** |
| 1648 |
* Returns the text title. |
| 1649 |
* |
| 1650 |
* @return string title |
| 1651 |
*/ |
| 1652 |
public function getTitle(): string { |
| 1653 |
return $this->title; |
| 1654 |
} |
| 1655 |
|
| 1656 |
/** |
| 1657 |
* Sets the title text. |
| 1658 |
* |
| 1659 |
* @param string $title title |
| 1660 |
*/ |
| 1661 |
public function setTitle(string $title): void { |
| 1662 |
$this->title = $title; |
| 1663 |
} |
| 1664 |
|
| 1665 |
/** |
| 1666 |
* Returns the entries. |
| 1667 |
* |
| 1668 |
* @return PDFSectionEntry[] entries |
| 1669 |
*/ |
| 1670 |
public function getEntries(): array { |
| 1671 |
return $this->entries; |
| 1672 |
} |
| 1673 |
|
| 1674 |
/** |
| 1675 |
* Sets the entries. |
| 1676 |
* |
| 1677 |
* @param PDFSectionEntry[] $entries entries |
| 1678 |
*/ |
| 1679 |
public function setEntries(array $entries): void { |
| 1680 |
$this->entries = $entries; |
| 1681 |
} |
| 1682 |
|
| 1683 |
} |
| 1684 |
|
| 1685 |
/** |
| 1686 |
* Single PDF entry. |
| 1687 |
* |
| 1688 |
* @author Roland Gruber |
| 1689 |
*/ |
| 1690 |
class PDFSectionEntry { |
| 1691 |
|
| 1692 |
private string $key; |
| 1693 |
|
| 1694 |
/** |
| 1695 |
* Constructor |
| 1696 |
* |
| 1697 |
* @param string $key key |
| 1698 |
*/ |
| 1699 |
public function __construct(string $key) { |
| 1700 |
$this->key = $key; |
| 1701 |
} |
| 1702 |
|
| 1703 |
/** |
| 1704 |
* Returns the PDF key. |
| 1705 |
* |
| 1706 |
* @return string $key key |
| 1707 |
*/ |
| 1708 |
public function getKey(): string { |
| 1709 |
return $this->key; |
| 1710 |
} |
| 1711 |
|
| 1712 |
} |
| 1713 |
|
| 1714 |
/** |
| 1715 |
* Returns a list of possible fonts. |
| 1716 |
* |
| 1717 |
* @return array<string, string> list of fonts (description => font name) |
| 1718 |
*/ |
| 1719 |
function getPdfFonts(): array { |
| 1720 |
return [ |
| 1721 |
'DejaVu' => 'DejaVuSerif', |
| 1722 |
_('Chinese Traditional') => 'cid0ct', |
| 1723 |
_('Chinese Simplified') => 'cid0cs', |
| 1724 |
_('Japanese') => 'cid0jp', |
| 1725 |
_('Korean') => 'cid0kr', |
| 1726 |
]; |
| 1727 |
} |