From 8774583532b744799961fa3cf5ee01db62d805ba Mon Sep 17 00:00:00 2001 From: Roland Gruber Date: Thu, 5 Jan 2017 21:05:17 +0100 Subject: [PATCH] new type API for PDF --- lam/lib/lists.inc | 2 +- lam/lib/pdf.inc | 2 +- lam/lib/pdfstruct.inc | 133 ++++++++++++++----------- lam/templates/pdfedit/pdfmain.php | 6 +- lam/templates/pdfedit/pdfpage.php | 6 +- lam/templates/profedit/profilemain.php | 24 ++--- lam/templates/upload/masscreate.php | 2 +- 7 files changed, 97 insertions(+), 78 deletions(-) diff --git a/lam/lib/lists.inc b/lam/lib/lists.inc index f64771c3..0e174adb 100644 --- a/lam/lib/lists.inc +++ b/lam/lib/lists.inc @@ -690,7 +690,7 @@ class lamList { $selAccounts[] = $id; } // get possible PDF structures - $pdf_structures = \LAM\PDF\getPDFStructureDefinitions($this->type->getId()); + $pdf_structures = \LAM\PDF\getPDFStructures($this->type->getId()); $this->listPrintHeader(); diff --git a/lam/lib/pdf.inc b/lam/lib/pdf.inc index ec902d13..80d82f21 100644 --- a/lam/lib/pdf.inc +++ b/lam/lib/pdf.inc @@ -67,7 +67,7 @@ function createModulePDF($accounts, $pdf_structure="default", $returnAsString = $account_type = $accounts[0]->get_type(); // Get PDF structure from xml file - $load = \LAM\PDF\loadPDFStructureDefinitions($account_type->getId(), $pdf_structure); + $load = \LAM\PDF\loadPDFStructure($account_type->getId(), $pdf_structure); $structure = $load['structure']; // get list of PDF keys $pdfKeys = array(); diff --git a/lam/lib/pdfstruct.inc b/lam/lib/pdfstruct.inc index 2fae7541..2c7132bb 100644 --- a/lam/lib/pdfstruct.inc +++ b/lam/lib/pdfstruct.inc @@ -2,6 +2,7 @@ namespace LAM\PDF; use \htmlStatusMessage; +use \LAMException; /* $Id$ @@ -49,7 +50,7 @@ include_once("ldap.inc"); * scope. Each entry is a string being the filename that may be passed to the * createModulePDF() function as second argument. */ -function getPDFStructureDefinitions($scope = "user", $profile = null) { +function getPDFStructures($scope = "user", $profile = null) { $return = array(); if (!isset($profile)) { @@ -70,17 +71,19 @@ function getPDFStructureDefinitions($scope = "user", $profile = null) { } /** - * This function is used to get pdf structure from xml file. - * Used in createModulePDF. + * This function is used to get the PDF structure from XML file. * - * @param string $scope The account scope for wich the PDF structure should be returned. - * @param string $pdf_structure Structure name of selected scope wich should be returned. + * @param string $typeId the account type + * @param string $name structure name * * @return array PDF structure */ -function loadPDFStructureDefinitions($scope='user', $pdf_structure='default') { +function loadPDFStructure($typeId, $name='default') { + if (!isValidPDFStructureName($name) || !preg_match('/[a-zA-Z]+/', $typeId)) { + return null; + } $parser = new xmlParser(); - $file = dirname(__FILE__) . '/../config/pdf/' . $_SESSION['config']->getName() . '/' . $pdf_structure . '.' . $scope . '.xml'; + $file = dirname(__FILE__) . '/../config/pdf/' . $_SESSION['config']->getName() . '/' . $name . '.' . $typeId . '.xml'; $xml = $parser->parse($file); $border = array(); $structure = array(); @@ -100,16 +103,17 @@ function loadPDFStructureDefinitions($scope='user', $pdf_structure='default') { /** - * Saves PDF structure definitions to XML file in format: ..xml + * Saves PDF structure to XML file in format: ..xml * - * @param string $scope account type - * @param string $definition Name of definition + * @param string $typeId account type + * @param string $name name of structure * @return string "no perms" if access denied or "ok". */ -function savePDFStructureDefinitions($scope, $definition) { - if (!preg_match('/[a-zA-Z0-9\-\_]+/',$definition)) return 'no perms'; - if (!preg_match('/[a-zA-Z]+/',$scope)) return 'no perms'; - $struct_file = dirname(__FILE__) . '/../config/pdf/' . $_SESSION['config']->getName() . '/' . $definition . '.' . $scope . '.xml'; +function savePDFStructure($typeId, $name) { + if (!isValidPDFStructureName($name) || !preg_match('/[a-zA-Z]+/', $typeId)) { + return 'no perms'; + } + $struct_file = dirname(__FILE__) . '/../config/pdf/' . $_SESSION['config']->getName() . '/' . $name . '.' . $typeId . '.xml'; if(!is_writable(dirname(__FILE__) . '/../config/pdf/' . $_SESSION['config']->getName())) { return 'no perms'; } @@ -157,15 +161,16 @@ function savePDFStructureDefinitions($scope, $definition) { /** * Deletes XML file with PDF structure definitions. * - * @param string $scope account type - * @param string $definition Name of definition to delete + * @param string $typeId account type + * @param string $name Name of definition to delete * * @return boolean True if file was deleted or false if a problem occured. */ -function deletePDFStructureDefinition($scope, $definition) { - if (!preg_match('/[a-zA-Z0-9\-\_]+/',$definition)) return false; - if (!preg_match('/[a-zA-Z]+/',$scope)) return false; - $file = dirname(__FILE__) . '/../config/pdf/' . $_SESSION['config']->getName() . '/' . $definition . '.' . $scope . '.xml'; +function deletePDFStructure($typeId, $name) { + if (!isValidPDFStructureName($name) || !preg_match('/[a-zA-Z]+/',$typeId)) { + return false; + } + $file = dirname(__FILE__) . '/../config/pdf/' . $_SESSION['config']->getName() . '/' . $name . '.' . $typeId . '.xml'; if(is_file($file) && is_writable($file)) { return unlink($file); } @@ -196,46 +201,50 @@ function getAvailableLogos() { return $return; } +/** + * Copies a PDF structure from the given source to target. + * + * @param \LAM\TYPES\ConfiguredType $sourceType source type + * @param string $sourceStructureName structure name + * @param \LAM\TYPES\ConfiguredType $targetType target type + * @throws Exception + */ +function copyStructure($sourceType, $sourceStructureName, $targetType) { + if (!isValidPDFStructureName($sourceStructureName)) { + throw new LAMException(_('Failed to copy')); + } + $sourceConfig = $sourceType->getTypeManager()->getConfig()->getName(); + $sourceTypeId = $sourceType->getId(); + $targetConfig = $targetType->getTypeManager()->getConfig()->getName(); + $targetTypeId = $targetType->getId(); + $basePath = dirname(__FILE__) . '/../config/pdf/'; + $src = $basePath . $sourceConfig . '/' . $sourceStructureName . '.' . $sourceTypeId; + $dst = $basePath . $targetConfig . '/' . $sourceStructureName . '.' . $targetTypeId; + if (!@copy($src, $dst)) { + throw new LAMException(_('Failed to copy'), $sourceConfig . ': ' . $sourceStructureName); + } +} /** - * Copies PDF profiles to other server profiles. + * Copies a PDF structure from the given source to global templates. * - * @param array $pdfProfiles PDF profile names - * @param String $scope account scope - * @param array $dests destinations - * - * @return boolean operation succeeded + * @param \LAM\TYPES\ConfiguredType $sourceType source type + * @param string $sourceName structure name + * @throws Exception */ -function copyPdfProfiles($pdfProfiles, $scope, $dests = array()) { - $state = true; - $profilePath = substr(__FILE__, 0, strlen(__FILE__) - 17) . 'config/pdf/'; - foreach ($pdfProfiles as $profile) { - //part 1: server profile - //part 2: account profile - $tmpArr = explode('##', $profile); - $src = $profilePath . $tmpArr[0] . '/' . $tmpArr[1] . '.' . $scope . '.xml'; - if (!empty($dests)) { - foreach ($dests as $dest) { - if ($dest == 'templates*') { - $dst = substr(__FILE__, 0, strlen(__FILE__) - 17) . 'config/templates/pdf/' . $tmpArr[1] . '.' . $scope . '.xml'; - } else { - $dst = $profilePath . $dest . '/' . $tmpArr[1] . '.' . $scope . '.xml'; - } - if (!@copy($src, $dst)) { - StatusMessage('ERROR', _('Failed to export!'), $tmpArr[1] . '.' . $scope . '.xml'); - $state = false; - } - } - } else { - $dst = $profilePath . $_SESSION['config']->getName() . '/' . $tmpArr[1] . '.' . $scope . '.xml'; - if (!@copy($src, $dst)) { - StatusMessage('ERROR', _('Failed to import!'), $tmpArr[1] . '.' . $scope . '.xml'); - $state = false; - } - } +function copyStructureToTemplates($sourceType, $sourceName) { + if (!isValidPDFStructureName($sourceName)) { + throw new LAMException(_('Failed to copy')); + } + $sourceConfig = $sourceType->getTypeManager()->getConfig()->getName(); + $sourceTypeId = $sourceType->getId(); + $basePath = dirname(__FILE__) . '/../config/pdf/'; + $templatePath = dirname(__FILE__) . '/../config/templates/pdf/'; + $src = $basePath . $sourceConfig . '/' . $sourceName . '.' . $sourceTypeId; + $dst = $templatePath . $sourceName . '.' . $sourceType->getScope(); + if (!@copy($src, $dst)) { + throw new LAMException(_('Failed to copy'), $sourceConfig . ': ' . $sourceName); } - - return $state; } /** @@ -285,9 +294,9 @@ function deletePDFLogo($name) { // check if still in use $activeTypes = $_SESSION['config']->get_ActiveTypes(); foreach ($activeTypes as $type) { - $structures = getPDFStructureDefinitions($type); + $structures = getPDFStructures($type); foreach ($structures as $structure) { - $data = loadPDFStructureDefinitions($type, $structure); + $data = loadPDFStructure($type, $structure); if ($data['page_definitions']['filename'] == $name) { return new htmlStatusMessage('ERROR', _('Unable to delete logo file.'), sprintf(_('Logo is still in use by PDF structure "%s" in account type "%s".'), $structure, \LAM\TYPES\getTypeAlias($type))); @@ -305,4 +314,14 @@ function deletePDFLogo($name) { } } +/** + * Returns if the give structure name is valid. + * + * @param string $name structure name + * @return boolean is valid + */ +function isValidPDFStructureName($name) { + return preg_match('/[a-zA-Z0-9\-\_]+/',$name) === 1; +} + ?> diff --git a/lam/templates/pdfedit/pdfmain.php b/lam/templates/pdfedit/pdfmain.php index 39a640f5..91164844 100644 --- a/lam/templates/pdfedit/pdfmain.php +++ b/lam/templates/pdfedit/pdfmain.php @@ -106,7 +106,7 @@ $container->addElement(new htmlTitle(_('PDF editor')), true); if (isset($_POST['deleteProfile']) && ($_POST['deleteProfile'] == 'true')) { // delete structure - if (\LAM\PDF\deletePDFStructureDefinition($_POST['profileDeleteType'], $_POST['profileDeleteName'])) { + if (\LAM\PDF\deletePDFStructure($_POST['profileDeleteType'], $_POST['profileDeleteName'])) { $message = new htmlStatusMessage('INFO', _('Deleted PDF structure.'), \LAM\TYPES\getTypeAlias($_POST['profileDeleteType']) . ': ' . htmlspecialchars($_POST['profileDeleteName'])); $message->colspan = 10; $container->addElement($message, true); @@ -171,7 +171,7 @@ foreach ($sortedTypes as $typeId => $title) { } // get list of templates for each account type for ($i = 0; $i < sizeof($templateClasses); $i++) { - $templateClasses[$i]['templates'] = \LAM\PDF\getPDFStructureDefinitions($templateClasses[$i]['typeId']); + $templateClasses[$i]['templates'] = \LAM\PDF\getPDFStructures($templateClasses[$i]['typeId']); } // check if a template should be edited @@ -286,7 +286,7 @@ include '../main_header.php'; $tmpArr = array(); foreach ($configProfiles as $profile) { if ($profile != $_SESSION['config']->getName()) { - $accountProfiles = \LAM\PDF\getPDFStructureDefinitions($typeId, $profile); + $accountProfiles = \LAM\PDF\getPDFStructures($typeId, $profile); for ($p = 0; $p < sizeof($accountProfiles); $p++) { $tmpArr[$profile][$accountProfiles[$p]] = $profile . '##' . $accountProfiles[$p]; } diff --git a/lam/templates/pdfedit/pdfpage.php b/lam/templates/pdfedit/pdfpage.php index b264a5a6..e1c0f225 100644 --- a/lam/templates/pdfedit/pdfpage.php +++ b/lam/templates/pdfedit/pdfpage.php @@ -129,7 +129,7 @@ if(isset($_GET['submit'])) { $saveErrors[] = array('ERROR', _('PDF structure name not valid'), _('The name for that PDF-structure you submitted is not valid. A valid name must consist of the following characters: \'a-z\',\'A-Z\',\'0-9\',\'_\',\'-\'.')); } else { - $return = \LAM\PDF\savePDFStructureDefinitions($type->getId(), $_GET['pdfname']); + $return = \LAM\PDF\savePDFStructure($type->getId(), $_GET['pdfname']); if($return == 'ok') { metaRefresh('pdfmain.php?savedSuccessfully=' . $_GET['pdfname']); exit; @@ -356,14 +356,14 @@ foreach ($_GET as $key => $value) { if(!isset($_SESSION['currentPDFStructure'])) { // Load structure file to be edit if(isset($_GET['edit'])) { - $load = \LAM\PDF\loadPDFStructureDefinitions($type->getId(), $_GET['edit']); + $load = \LAM\PDF\loadPDFStructure($type->getId(), $_GET['edit']); $_SESSION['currentPDFStructure'] = $load['structure']; $_SESSION['currentPageDefinitions'] = $load['page_definitions']; $_GET['pdfname'] = $_GET['edit']; } // Load default structure file when creating a new one else { - $load = \LAM\PDF\loadPDFStructureDefinitions($type->getId()); + $load = \LAM\PDF\loadPDFStructure($type->getId()); $_SESSION['currentPDFStructure'] = $load['structure']; $_SESSION['currentPageDefinitions'] = $load['page_definitions']; } diff --git a/lam/templates/profedit/profilemain.php b/lam/templates/profedit/profilemain.php index 5701ae8f..9b580d95 100644 --- a/lam/templates/profedit/profilemain.php +++ b/lam/templates/profedit/profilemain.php @@ -145,9 +145,9 @@ foreach ($configProfiles as $profileName) { if (!empty($_POST['import'])) { $cfg = new LAMCfgMain(); // check master password - $impMessage = null; + $errMessage = null; if (!$cfg->checkPassword($_POST['passwd_i_' . $_POST['typeId']])) { - $impMessage = new htmlStatusMessage('ERROR', _('Master password is wrong!')); + $errMessage = new htmlStatusMessage('ERROR', _('Master password is wrong!')); } elseif (!empty($_POST['importProfiles'])) { $options = array(); @@ -155,20 +155,20 @@ if (!empty($_POST['import'])) { $parts = explode('##', $importProfiles); $options[] = array('conf' => $parts[0], 'typeId' => $parts[1], 'name' => $parts[2]); } - $impMessage = importProfiles($_POST['typeId'], $options, $serverProfiles, $typeManager); + $errMessage = importProfiles($_POST['typeId'], $options, $serverProfiles, $typeManager); } - if ($impMessage != null) { - $impMessage->colspan = 10; - $container->addElement($impMessage, true); + if ($errMessage != null) { + $errMessage->colspan = 10; + $container->addElement($errMessage, true); } } // export profiles if (!empty($_POST['export'])) { $cfg = new LAMCfgMain(); // check master password - $impMessage = null; + $errMessage = null; if (!$cfg->checkPassword($_POST['passwd_e_' . $_POST['typeId']])) { - $impMessage = new htmlStatusMessage('ERROR', _('Master password is wrong!')); + $errMessage = new htmlStatusMessage('ERROR', _('Master password is wrong!')); } elseif (!empty($_POST['exportProfiles'])) { $options = array(); @@ -178,11 +178,11 @@ if (!empty($_POST['export'])) { } $typeId = $_POST['typeId']; $name = $_POST['name_' . $typeId]; - $impMessage = exportProfiles($typeId, $name, $options, $serverProfiles, $typeManager); + $errMessage = exportProfiles($typeId, $name, $options, $serverProfiles, $typeManager); } - if ($impMessage != null) { - $impMessage->colspan = 10; - $container->addElement($impMessage, true); + if ($errMessage != null) { + $errMessage->colspan = 10; + $container->addElement($errMessage, true); } } diff --git a/lam/templates/upload/masscreate.php b/lam/templates/upload/masscreate.php index 3cba31c9..9fad8e51 100644 --- a/lam/templates/upload/masscreate.php +++ b/lam/templates/upload/masscreate.php @@ -267,7 +267,7 @@ function showMainPage($scope, $selectedModules) { $pdfCheckbox = new htmlTableExtendedInputCheckbox('createPDF', $createPDF, _('Create PDF files')); $pdfCheckbox->setTableRowsToShow(array('pdfStructure')); $inputContainer->addElement($pdfCheckbox, true); - $pdfStructures = \LAM\PDF\getPDFStructureDefinitions($scope); + $pdfStructures = \LAM\PDF\getPDFStructures($scope); $pdfSelected = array(); if (isset($_POST['pdfStructure'])) { $pdfSelected = array($_POST['pdfStructure']);