From 81587a9b00a3c463d35597150bfc64bb8c1a521d Mon Sep 17 00:00:00 2001 From: Roland Gruber Date: Sun, 3 May 2020 10:59:35 +0200 Subject: [PATCH] refactoring --- lam/lib/lists.inc | 2 +- lam/lib/pdfstruct.inc | 32 ++++++++++++++--------------- lam/templates/pdfedit/pdfmain.php | 12 +++++------ lam/templates/pdfedit/pdfpage.php | 4 ++-- lam/templates/upload/masscreate.php | 2 +- 5 files changed, 26 insertions(+), 26 deletions(-) diff --git a/lam/lib/lists.inc b/lam/lib/lists.inc index 7ad71fc6..d8aa0feb 100644 --- a/lam/lib/lists.inc +++ b/lam/lib/lists.inc @@ -759,7 +759,7 @@ class lamList { $selAccounts[] = $id; } // get possible PDF structures - $pdf_structures = \LAM\PDF\getPDFStructures($this->type->getId()); + $pdf_structures = \LAM\PDF\getPDFStructures($this->type->getId(), $_SESSION['config']->getName()); $this->printHeader(); diff --git a/lam/lib/pdfstruct.inc b/lam/lib/pdfstruct.inc index c6b7635d..3a17cb5b 100644 --- a/lam/lib/pdfstruct.inc +++ b/lam/lib/pdfstruct.inc @@ -44,18 +44,15 @@ include_once(__DIR__ . "/ldap.inc"); * @param string $typeId the account type * @param string $profile server profile name * - * @return array All available PDF structure definitions for the submitted account + * @return string[] All available PDF structure definitions for the submitted account * scope. Each entry is a string being the filename that may be passed to the * createModulePDF() function as second argument. */ -function getPDFStructures($typeId, $profile = null) { +function getPDFStructures($typeId, $profile) { $return = array(); if (!preg_match('/[a-zA-Z]+/', $typeId)) { return null; } - if (!isset($profile)) { - $profile = $_SESSION['config']->getName(); - } $path = dirname(__FILE__) . '/../config/pdf/' . $profile; if(is_dir($path)) { $dirHandle = opendir($path); @@ -75,14 +72,14 @@ function getPDFStructures($typeId, $profile = null) { * * @param string $typeId account type * @param string $name Name of definition to delete - * + * @param string $serverProfileName server profile name * @return boolean True if file was deleted or false if a problem occurred. */ -function deletePDFStructure($typeId, $name) { +function deletePDFStructure($typeId, $name, $serverProfileName) { if (!isValidPDFStructureName($name) || !preg_match('/[a-zA-Z]+/',$typeId)) { return false; } - $file = dirname(__FILE__) . '/../config/pdf/' . $_SESSION['config']->getName() . '/' . $name . '.' . $typeId . '.xml'; + $file = dirname(__FILE__) . '/../config/pdf/' . $serverProfileName . '/' . $name . '.' . $typeId . '.xml'; if(is_file($file) && is_writable($file)) { return unlink($file); } @@ -95,11 +92,12 @@ function deletePDFStructure($typeId, $name) { /** * This function returns an array with all aviliable logo images. * + * @param string $serverProfileName server profile name * @return array list of logo files */ -function getAvailableLogos() { +function getAvailableLogos($serverProfileName) { $return = array(); - $dirPath = dirname(__FILE__) . '/../config/pdf/' . $_SESSION['config']->getName() . '/logos/'; + $dirPath = dirname(__FILE__) . '/../config/pdf/' . $serverProfileName . '/logos/'; $dirHandle = opendir($dirPath); while($file = readdir($dirHandle)) { if(!is_dir($file) && $file != '.' && $file != '..' && preg_match('/\\.(jpg|png)$/i',$file)) { @@ -165,13 +163,14 @@ function copyStructureToTemplates($sourceType, $sourceName) { * * @param String $file full path of temporary file * @param String $name file name + * @param string $serverProfileName server profile name * @return StatusMessage status message to display */ -function uploadPDFLogo($file, $name) { +function uploadPDFLogo($file, $name, $serverProfileName) { if (!preg_match('/[a-zA-Z0-9_-]+\\.(png)|(jpg)/i', $name)) { return new htmlStatusMessage('ERROR', _('Unable to upload logo file.'), _('The file name must end with ".png" or ".jpg".')); } - $dirPath = dirname(__FILE__) . '/../config/pdf/' . $_SESSION['config']->getName() . '/logos/'; + $dirPath = dirname(__FILE__) . '/../config/pdf/' . $serverProfileName . '/logos/'; $success = copy($file, $dirPath . '/' . $name); if ($success) { return new htmlStatusMessage('INFO', _('Uploaded logo file.'), $name); @@ -185,12 +184,13 @@ function uploadPDFLogo($file, $name) { * Deletes a PDF logo file. * * @param String $name file name + * @param string $serverProfileName server profile name * @return StatusMessage status message to display */ -function deletePDFLogo($name) { +function deletePDFLogo($name, $serverProfileName) { // check if valid file $found = false; - $logos = getAvailableLogos(); + $logos = getAvailableLogos($serverProfileName); foreach ($logos as $logo) { if ($logo['filename'] === $name) { $found = true; @@ -205,7 +205,7 @@ function deletePDFLogo($name) { $activeTypes = $typeManager->getConfiguredTypes(); $reader = new PDFStructureReader(); foreach ($activeTypes as $type) { - $structures = getPDFStructures($type->getId()); + $structures = getPDFStructures($type->getId(), $serverProfileName); foreach ($structures as $structure) { try { $data = $reader->read($type->getId(), $structure); @@ -220,7 +220,7 @@ function deletePDFLogo($name) { } } // delete file - $dirPath = dirname(__FILE__) . '/../config/pdf/' . $_SESSION['config']->getName() . '/logos/'; + $dirPath = dirname(__FILE__) . '/../config/pdf/' . $serverProfileName . '/logos/'; $success = @unlink($dirPath . '/' . $name); if ($success) { return new htmlStatusMessage('INFO', _('Logo file deleted.'), $name); diff --git a/lam/templates/pdfedit/pdfmain.php b/lam/templates/pdfedit/pdfmain.php index 12c3b779..6a65ec6c 100644 --- a/lam/templates/pdfedit/pdfmain.php +++ b/lam/templates/pdfedit/pdfmain.php @@ -21,7 +21,7 @@ use \LAM\TYPES\TypeManager; This code is part of LDAP Account Manager (http://www.ldap-account-manager.org/) Copyright (C) 2003 - 2006 Michael Duergner - 2005 - 2018 Roland Gruber + 2005 - 2020 Roland Gruber This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by @@ -107,7 +107,7 @@ $container->add(new htmlTitle(_('PDF editor')), 12); if (isset($_POST['deleteProfile']) && ($_POST['deleteProfile'] == 'true')) { $typeToDelete = $typeManager->getConfiguredType($_POST['profileDeleteType']); // delete structure - if (\LAM\PDF\deletePDFStructure($_POST['profileDeleteType'], $_POST['profileDeleteName'])) { + if (\LAM\PDF\deletePDFStructure($_POST['profileDeleteType'], $_POST['profileDeleteName'], $_SESSION['config']->getName())) { $message = new htmlStatusMessage('INFO', _('Deleted PDF structure.'), $typeToDelete->getAlias() . ': ' . htmlspecialchars($_POST['profileDeleteName'])); $container->add($message, 12); } @@ -173,13 +173,13 @@ if (!empty($_POST['export'])) { if (isset($_POST['uploadLogo']) && !empty($_FILES['logoUpload']) && !empty($_FILES['logoUpload']['size'])) { $file = $_FILES['logoUpload']['tmp_name']; $filename = $_FILES['logoUpload']['name']; - $container->add(\LAM\PDF\uploadPDFLogo($file, $filename), 12); + $container->add(\LAM\PDF\uploadPDFLogo($file, $filename, $_SESSION['config']->getName()), 12); } // delete logo file if (isset($_POST['delLogo'])) { $toDel = $_POST['logo']; - $container->add(\LAM\PDF\deletePDFLogo($toDel), 12); + $container->add(\LAM\PDF\deletePDFLogo($toDel, $_SESSION['config']->getName()), 12); } // get list of account types @@ -192,7 +192,7 @@ foreach ($sortedTypes as $typeId => $title) { 'scope' => $type->getScope(), 'title' => $title, 'icon' => $type->getIcon(), - 'templates' => \LAM\PDF\getPDFStructures($type->getId())); + 'templates' => \LAM\PDF\getPDFStructures($type->getId(), $_SESSION['config']->getName())); $availableTypes[$title] = $type->getId(); } // check if a template should be edited @@ -275,7 +275,7 @@ include __DIR__ . '/../../lib/adminHeader.inc'; // manage logos $container->addVerticalSpacer('4rem'); $container->add(new htmlSubTitle(_('Manage logos')), 12); - $logos = \LAM\PDF\getAvailableLogos(); + $logos = \LAM\PDF\getAvailableLogos($_SESSION['config']->getName()); $logoOptions = array(); foreach ($logos as $logo) { $file = $logo['filename']; diff --git a/lam/templates/pdfedit/pdfpage.php b/lam/templates/pdfedit/pdfpage.php index db105c77..9d715473 100644 --- a/lam/templates/pdfedit/pdfpage.php +++ b/lam/templates/pdfedit/pdfpage.php @@ -22,7 +22,7 @@ use LAM\PDF\PDFStructureWriter; /* This code is part of LDAP Account Manager (http://www.ldap-account-manager.org/) Copyright (C) 2003 - 2006 Michael Duergner - 2007 - 2019 Roland Gruber + 2007 - 2020 Roland Gruber This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by @@ -218,7 +218,7 @@ else if (isset($_POST['pdfname'])) { // headline $headline = $_SESSION['currentPDFStructure']->getTitle(); // logo -$logoFiles = \LAM\PDF\getAvailableLogos(); +$logoFiles = \LAM\PDF\getAvailableLogos($_SESSION['config']->getName()); $logos = array(_('No logo') => 'none'); foreach($logoFiles as $logoFile) { $logos[$logoFile['filename'] . ' (' . $logoFile['infos'][0] . ' x ' . $logoFile['infos'][1] . ")"] = $logoFile['filename']; diff --git a/lam/templates/upload/masscreate.php b/lam/templates/upload/masscreate.php index 95c4196d..1f76e2a1 100644 --- a/lam/templates/upload/masscreate.php +++ b/lam/templates/upload/masscreate.php @@ -294,7 +294,7 @@ function showMainPage(\LAM\TYPES\ConfiguredType $type, $selectedModules) { $pdfCheckbox = new htmlResponsiveInputCheckbox('createPDF', $createPDF, _('Create PDF files')); $pdfCheckbox->setTableRowsToShow(array('pdfStructure', 'pdf_font')); $row->add($pdfCheckbox, 12); - $pdfStructures = \LAM\PDF\getPDFStructures($type->getId()); + $pdfStructures = \LAM\PDF\getPDFStructures($type->getId(), $_SESSION['config']->getName()); $pdfSelected = array(); if (isset($_POST['pdfStructure'])) { $pdfSelected = array($_POST['pdfStructure']);