refactoring

This commit is contained in:
Roland Gruber 2020-05-03 10:59:35 +02:00
parent 9c6e30a03b
commit 81587a9b00
5 changed files with 26 additions and 26 deletions

View File

@ -759,7 +759,7 @@ class lamList {
$selAccounts[] = $id; $selAccounts[] = $id;
} }
// get possible PDF structures // 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(); $this->printHeader();

View File

@ -44,18 +44,15 @@ include_once(__DIR__ . "/ldap.inc");
* @param string $typeId the account type * @param string $typeId the account type
* @param string $profile server profile name * @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 * scope. Each entry is a string being the filename that may be passed to the
* createModulePDF() function as second argument. * createModulePDF() function as second argument.
*/ */
function getPDFStructures($typeId, $profile = null) { function getPDFStructures($typeId, $profile) {
$return = array(); $return = array();
if (!preg_match('/[a-zA-Z]+/', $typeId)) { if (!preg_match('/[a-zA-Z]+/', $typeId)) {
return null; return null;
} }
if (!isset($profile)) {
$profile = $_SESSION['config']->getName();
}
$path = dirname(__FILE__) . '/../config/pdf/' . $profile; $path = dirname(__FILE__) . '/../config/pdf/' . $profile;
if(is_dir($path)) { if(is_dir($path)) {
$dirHandle = opendir($path); $dirHandle = opendir($path);
@ -75,14 +72,14 @@ function getPDFStructures($typeId, $profile = null) {
* *
* @param string $typeId account type * @param string $typeId account type
* @param string $name Name of definition to delete * @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. * @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)) { if (!isValidPDFStructureName($name) || !preg_match('/[a-zA-Z]+/',$typeId)) {
return false; 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)) { if(is_file($file) && is_writable($file)) {
return unlink($file); return unlink($file);
} }
@ -95,11 +92,12 @@ function deletePDFStructure($typeId, $name) {
/** /**
* This function returns an array with all aviliable logo images. * This function returns an array with all aviliable logo images.
* *
* @param string $serverProfileName server profile name
* @return array list of logo files * @return array list of logo files
*/ */
function getAvailableLogos() { function getAvailableLogos($serverProfileName) {
$return = array(); $return = array();
$dirPath = dirname(__FILE__) . '/../config/pdf/' . $_SESSION['config']->getName() . '/logos/'; $dirPath = dirname(__FILE__) . '/../config/pdf/' . $serverProfileName . '/logos/';
$dirHandle = opendir($dirPath); $dirHandle = opendir($dirPath);
while($file = readdir($dirHandle)) { while($file = readdir($dirHandle)) {
if(!is_dir($file) && $file != '.' && $file != '..' && preg_match('/\\.(jpg|png)$/i',$file)) { 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 $file full path of temporary file
* @param String $name file name * @param String $name file name
* @param string $serverProfileName server profile name
* @return StatusMessage status message to display * @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)) { 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".')); 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); $success = copy($file, $dirPath . '/' . $name);
if ($success) { if ($success) {
return new htmlStatusMessage('INFO', _('Uploaded logo file.'), $name); return new htmlStatusMessage('INFO', _('Uploaded logo file.'), $name);
@ -185,12 +184,13 @@ function uploadPDFLogo($file, $name) {
* Deletes a PDF logo file. * Deletes a PDF logo file.
* *
* @param String $name file name * @param String $name file name
* @param string $serverProfileName server profile name
* @return StatusMessage status message to display * @return StatusMessage status message to display
*/ */
function deletePDFLogo($name) { function deletePDFLogo($name, $serverProfileName) {
// check if valid file // check if valid file
$found = false; $found = false;
$logos = getAvailableLogos(); $logos = getAvailableLogos($serverProfileName);
foreach ($logos as $logo) { foreach ($logos as $logo) {
if ($logo['filename'] === $name) { if ($logo['filename'] === $name) {
$found = true; $found = true;
@ -205,7 +205,7 @@ function deletePDFLogo($name) {
$activeTypes = $typeManager->getConfiguredTypes(); $activeTypes = $typeManager->getConfiguredTypes();
$reader = new PDFStructureReader(); $reader = new PDFStructureReader();
foreach ($activeTypes as $type) { foreach ($activeTypes as $type) {
$structures = getPDFStructures($type->getId()); $structures = getPDFStructures($type->getId(), $serverProfileName);
foreach ($structures as $structure) { foreach ($structures as $structure) {
try { try {
$data = $reader->read($type->getId(), $structure); $data = $reader->read($type->getId(), $structure);
@ -220,7 +220,7 @@ function deletePDFLogo($name) {
} }
} }
// delete file // delete file
$dirPath = dirname(__FILE__) . '/../config/pdf/' . $_SESSION['config']->getName() . '/logos/'; $dirPath = dirname(__FILE__) . '/../config/pdf/' . $serverProfileName . '/logos/';
$success = @unlink($dirPath . '/' . $name); $success = @unlink($dirPath . '/' . $name);
if ($success) { if ($success) {
return new htmlStatusMessage('INFO', _('Logo file deleted.'), $name); return new htmlStatusMessage('INFO', _('Logo file deleted.'), $name);

View File

@ -21,7 +21,7 @@ use \LAM\TYPES\TypeManager;
This code is part of LDAP Account Manager (http://www.ldap-account-manager.org/) This code is part of LDAP Account Manager (http://www.ldap-account-manager.org/)
Copyright (C) 2003 - 2006 Michael Duergner 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 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 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')) { if (isset($_POST['deleteProfile']) && ($_POST['deleteProfile'] == 'true')) {
$typeToDelete = $typeManager->getConfiguredType($_POST['profileDeleteType']); $typeToDelete = $typeManager->getConfiguredType($_POST['profileDeleteType']);
// delete structure // 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'])); $message = new htmlStatusMessage('INFO', _('Deleted PDF structure.'), $typeToDelete->getAlias() . ': ' . htmlspecialchars($_POST['profileDeleteName']));
$container->add($message, 12); $container->add($message, 12);
} }
@ -173,13 +173,13 @@ if (!empty($_POST['export'])) {
if (isset($_POST['uploadLogo']) && !empty($_FILES['logoUpload']) && !empty($_FILES['logoUpload']['size'])) { if (isset($_POST['uploadLogo']) && !empty($_FILES['logoUpload']) && !empty($_FILES['logoUpload']['size'])) {
$file = $_FILES['logoUpload']['tmp_name']; $file = $_FILES['logoUpload']['tmp_name'];
$filename = $_FILES['logoUpload']['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 // delete logo file
if (isset($_POST['delLogo'])) { if (isset($_POST['delLogo'])) {
$toDel = $_POST['logo']; $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 // get list of account types
@ -192,7 +192,7 @@ foreach ($sortedTypes as $typeId => $title) {
'scope' => $type->getScope(), 'scope' => $type->getScope(),
'title' => $title, 'title' => $title,
'icon' => $type->getIcon(), 'icon' => $type->getIcon(),
'templates' => \LAM\PDF\getPDFStructures($type->getId())); 'templates' => \LAM\PDF\getPDFStructures($type->getId(), $_SESSION['config']->getName()));
$availableTypes[$title] = $type->getId(); $availableTypes[$title] = $type->getId();
} }
// check if a template should be edited // check if a template should be edited
@ -275,7 +275,7 @@ include __DIR__ . '/../../lib/adminHeader.inc';
// manage logos // manage logos
$container->addVerticalSpacer('4rem'); $container->addVerticalSpacer('4rem');
$container->add(new htmlSubTitle(_('Manage logos')), 12); $container->add(new htmlSubTitle(_('Manage logos')), 12);
$logos = \LAM\PDF\getAvailableLogos(); $logos = \LAM\PDF\getAvailableLogos($_SESSION['config']->getName());
$logoOptions = array(); $logoOptions = array();
foreach ($logos as $logo) { foreach ($logos as $logo) {
$file = $logo['filename']; $file = $logo['filename'];

View File

@ -22,7 +22,7 @@ use LAM\PDF\PDFStructureWriter;
/* /*
This code is part of LDAP Account Manager (http://www.ldap-account-manager.org/) This code is part of LDAP Account Manager (http://www.ldap-account-manager.org/)
Copyright (C) 2003 - 2006 Michael Duergner 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 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 it under the terms of the GNU General Public License as published by
@ -218,7 +218,7 @@ else if (isset($_POST['pdfname'])) {
// headline // headline
$headline = $_SESSION['currentPDFStructure']->getTitle(); $headline = $_SESSION['currentPDFStructure']->getTitle();
// logo // logo
$logoFiles = \LAM\PDF\getAvailableLogos(); $logoFiles = \LAM\PDF\getAvailableLogos($_SESSION['config']->getName());
$logos = array(_('No logo') => 'none'); $logos = array(_('No logo') => 'none');
foreach($logoFiles as $logoFile) { foreach($logoFiles as $logoFile) {
$logos[$logoFile['filename'] . ' (' . $logoFile['infos'][0] . ' x ' . $logoFile['infos'][1] . ")"] = $logoFile['filename']; $logos[$logoFile['filename'] . ' (' . $logoFile['infos'][0] . ' x ' . $logoFile['infos'][1] . ")"] = $logoFile['filename'];

View File

@ -294,7 +294,7 @@ function showMainPage(\LAM\TYPES\ConfiguredType $type, $selectedModules) {
$pdfCheckbox = new htmlResponsiveInputCheckbox('createPDF', $createPDF, _('Create PDF files')); $pdfCheckbox = new htmlResponsiveInputCheckbox('createPDF', $createPDF, _('Create PDF files'));
$pdfCheckbox->setTableRowsToShow(array('pdfStructure', 'pdf_font')); $pdfCheckbox->setTableRowsToShow(array('pdfStructure', 'pdf_font'));
$row->add($pdfCheckbox, 12); $row->add($pdfCheckbox, 12);
$pdfStructures = \LAM\PDF\getPDFStructures($type->getId()); $pdfStructures = \LAM\PDF\getPDFStructures($type->getId(), $_SESSION['config']->getName());
$pdfSelected = array(); $pdfSelected = array();
if (isset($_POST['pdfStructure'])) { if (isset($_POST['pdfStructure'])) {
$pdfSelected = array($_POST['pdfStructure']); $pdfSelected = array($_POST['pdfStructure']);