LDAPAccountManager/lam/lib/pdfstruct.inc

328 lines
11 KiB
PHP
Raw Normal View History

<?php
2017-01-04 19:52:51 +00:00
namespace LAM\PDF;
use \htmlStatusMessage;
2017-01-05 20:05:17 +00:00
use \LAMException;
/*
$Id$
2009-10-27 18:47:12 +00:00
This code is part of LDAP Account Manager (http://www.ldap-account-manager.org/)
2011-11-27 14:49:20 +00:00
Copyright (C) 2003 - 2006 Michael Duergner
2016-12-19 20:32:08 +00:00
2011 - 2016 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
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
2004-10-30 16:46:06 +00:00
/**
* Functions to manage the PDF structures.
2016-12-19 20:32:08 +00:00
*
2011-11-27 14:49:20 +00:00
* @author Michael Duergner
2004-10-30 16:46:06 +00:00
* @package PDF
*/
/** LAM configuration */
include_once("config.inc");
2004-10-30 16:46:06 +00:00
/** LDAP object */
include_once("ldap.inc");
2004-10-30 16:46:06 +00:00
/**
* This function will return all available PDF structure definitions for the submitted
* account scope.
2012-10-28 14:37:54 +00:00
*
* @param string $scope The account scope the PDF structure definitions should be
* returned.
2012-10-28 14:37:54 +00:00
* @param string $profile server profile name
*
* @return array $scope All available PDF structure definitions for the submitted account
2012-10-28 14:37:54 +00:00
* scope. Each entry is a string being the filename that may be passed to the
* createModulePDF() function as second argument.
2004-10-30 16:46:06 +00:00
*/
2017-01-05 20:05:17 +00:00
function getPDFStructures($scope = "user", $profile = null) {
$return = array();
2012-10-28 14:37:54 +00:00
if (!isset($profile)) {
$profile = $_SESSION['config']->getName();
}
$path = dirname(__FILE__) . '/../config/pdf/' . $profile;
2004-09-08 14:40:25 +00:00
if(is_dir($path)) {
$dirHandle = opendir($path);
while($file = readdir($dirHandle)) {
$struct_file = explode('.',$file);
2012-10-28 14:37:54 +00:00
if(!is_dir($path.$file) && ($file != '.') && ($file != '..') && (sizeof($struct_file) == 3) && ($struct_file[1] == $scope) && ($struct_file[2] == 'xml')) {
array_push($return, $struct_file[0]);
2004-09-08 14:40:25 +00:00
}
}
2004-09-08 14:40:25 +00:00
sort($return);
}
return $return;
}
2004-10-30 16:46:06 +00:00
/**
2017-01-05 20:05:17 +00:00
* This function is used to get the PDF structure from XML file.
*
2017-01-05 20:05:17 +00:00
* @param string $typeId the account type
* @param string $name structure name
2012-10-28 14:37:54 +00:00
*
* @return array PDF structure
2004-10-30 16:46:06 +00:00
*/
2017-01-05 20:05:17 +00:00
function loadPDFStructure($typeId, $name='default') {
if (!isValidPDFStructureName($name) || !preg_match('/[a-zA-Z]+/', $typeId)) {
return null;
}
$parser = new xmlParser();
2017-01-05 20:05:17 +00:00
$file = dirname(__FILE__) . '/../config/pdf/' . $_SESSION['config']->getName() . '/' . $name . '.' . $typeId . '.xml';
2004-09-08 14:40:25 +00:00
$xml = $parser->parse($file);
$border = array();
$structure = array();
2008-03-16 10:52:50 +00:00
$complete_page_definitions = array('filename' => 'printLogo.jpg', 'headline' => 'LDAP Account Manager');
2017-01-04 19:52:51 +00:00
if (!empty($xml)) {
$border['start'] = $xml[1]['PDF'][0];
$page_definitions = $xml[0][$xml[1]['PDF'][0]]['attributes'];
foreach($page_definitions as $key => $value) {
$complete_page_definitions[strtolower($key)] = $value;
unset($page_definitions[$key]);
}
$border['end'] = $xml[1]['PDF'][1];
2017-01-04 19:52:51 +00:00
$structure = array_slice($xml[0],$border['start'] + 1,$border['end'] - ($border['start'] + 1));
}
return array('structure' => $structure, 'page_definitions' => $complete_page_definitions);
}
2004-10-30 16:46:06 +00:00
/**
2017-01-05 20:05:17 +00:00
* Saves PDF structure to XML file in format: <name>.<typeId>.xml
2016-12-19 20:32:08 +00:00
*
2017-01-05 20:05:17 +00:00
* @param string $typeId account type
* @param string $name name of structure
* @return string "no perms" if access denied or "ok".
2004-10-30 16:46:06 +00:00
*/
2017-01-05 20:05:17 +00:00
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';
2012-10-28 14:37:54 +00:00
if(!is_writable(dirname(__FILE__) . '/../config/pdf/' . $_SESSION['config']->getName())) {
return 'no perms';
}
else {
2005-09-27 12:47:22 +00:00
$handle = @fopen($struct_file,'w');
if (!$handle) return 'no perms';
$pdf_attributes = '';
foreach($_SESSION['currentPageDefinitions'] as $key => $value) {
2017-01-04 19:52:51 +00:00
$pdf_attributes .= ' ' . $key . '="' . $value . '"';
}
2017-01-04 19:52:51 +00:00
$file = '<pdf' . $pdf_attributes . ">\n";
foreach($_SESSION['currentPDFStructure'] as $entry) {
$ident = '';
for($i=0;$i<$entry['level'] -1;$i++) {
$ident .= "\t";
}
$attributes = '';
2008-04-28 17:59:08 +00:00
if(isset($entry['attributes']) && is_array($entry['attributes'])) {
foreach($entry['attributes'] as $key => $value) {
$attributes .= ' ' . strtolower($key) . '="' . $value . '"';
}
}
if($entry['type'] == 'open') {
2016-12-19 20:32:08 +00:00
$file .= $ident . '<' . strtolower($entry['tag']) . $attributes . ">\n";
}
elseif($entry['type'] == 'close') {
$file .= $ident . '</' . strtolower($entry['tag']) . ">\n";
}
elseif($entry['type'] == 'complete') {
if(isset($entry['value'])) {
$file .= $ident . '<' . strtolower($entry['tag']) . $attributes . '>' . $entry['value'] . '</' . strtolower($entry['tag']) . ">\n";
}
else {
$file .= $ident . '<' . strtolower($entry['tag']) . $attributes . " />\n";
}
}
}
$file .= "</pdf>";
fwrite($handle,$file);
fclose($handle);
return 'ok';
}
}
2004-10-30 16:46:06 +00:00
/**
* Deletes XML file with PDF structure definitions.
2012-10-28 14:37:54 +00:00
*
2017-01-05 20:05:17 +00:00
* @param string $typeId account type
* @param string $name Name of definition to delete
2012-10-28 14:37:54 +00:00
*
* @return boolean True if file was deleted or false if a problem occured.
2004-10-30 16:46:06 +00:00
*/
2017-01-05 20:05:17 +00:00
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);
}
else {
return false;
}
2012-10-28 14:37:54 +00:00
}
2004-10-30 16:46:06 +00:00
/**
* This function returns an array with all aviliable logo images.
2012-10-28 14:37:54 +00:00
*
* @return array list of logo files
2004-10-30 16:46:06 +00:00
*/
function getAvailableLogos() {
$return = array();
2012-10-28 14:37:54 +00:00
$dirPath = dirname(__FILE__) . '/../config/pdf/' . $_SESSION['config']->getName() . '/logos/';
$dirHandle = opendir($dirPath);
while($file = readdir($dirHandle)) {
2016-12-19 20:32:08 +00:00
if(!is_dir($file) && $file != '.' && $file != '..' && preg_match('/\\.(jpg|png)$/i',$file)) {
$infos = getimagesize($dirPath . $file);
2010-12-19 14:08:38 +00:00
if($infos[0] <= 2000 && $infos[1] <= 300) {
array_push($return, array('filename' => $file, 'infos' => $infos));
}
}
}
sort($return);
return $return;
}
2012-10-28 14:37:54 +00:00
/**
2017-01-05 20:05:17 +00:00
* Copies a PDF structure from the given source to target.
2012-10-28 14:37:54 +00:00
*
2017-01-05 20:05:17 +00:00
* @param \LAM\TYPES\ConfiguredType $sourceType source type
* @param string $sourceStructureName structure name
* @param \LAM\TYPES\ConfiguredType $targetType target type
* @throws Exception
2012-10-28 14:37:54 +00:00
*/
2017-01-05 20:05:17 +00:00
function copyStructure($sourceType, $sourceStructureName, $targetType) {
if (!isValidPDFStructureName($sourceStructureName)) {
throw new LAMException(_('Failed to copy'));
2012-10-28 14:37:54 +00:00
}
2017-01-05 20:05:17 +00:00
$sourceConfig = $sourceType->getTypeManager()->getConfig()->getName();
$sourceTypeId = $sourceType->getId();
$targetConfig = $targetType->getTypeManager()->getConfig()->getName();
$targetTypeId = $targetType->getId();
$basePath = dirname(__FILE__) . '/../config/pdf/';
2017-01-05 20:40:48 +00:00
$src = $basePath . $sourceConfig . '/' . $sourceStructureName . '.' . $sourceTypeId . '.xml';
$dst = $basePath . $targetConfig . '/' . $sourceStructureName . '.' . $targetTypeId . '.xml';
2017-01-05 20:05:17 +00:00
if (!@copy($src, $dst)) {
throw new LAMException(_('Failed to copy'), $sourceConfig . ': ' . $sourceStructureName);
}
}
2012-10-28 14:37:54 +00:00
2017-01-05 20:05:17 +00:00
/**
* Copies a PDF structure from the given source to global templates.
*
* @param \LAM\TYPES\ConfiguredType $sourceType source type
* @param string $sourceName structure name
* @throws Exception
*/
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/';
2017-01-05 20:40:48 +00:00
$src = $basePath . $sourceConfig . '/' . $sourceName . '.' . $sourceTypeId . '.xml';
$dst = $templatePath . $sourceName . '.' . $sourceType->getScope() . '.xml';
2017-01-05 20:05:17 +00:00
if (!@copy($src, $dst)) {
throw new LAMException(_('Failed to copy'), $sourceConfig . ': ' . $sourceName);
}
2012-10-28 14:37:54 +00:00
}
2013-10-08 19:11:01 +00:00
/**
* Uploads a PDF logo file for the current server profile.
2016-12-19 20:32:08 +00:00
*
2013-10-08 19:11:01 +00:00
* @param String $file full path of temporary file
* @param String $name file name
* @return StatusMessage status message to display
*/
function uploadPDFLogo($file, $name) {
2016-12-19 20:32:08 +00:00
if (!preg_match('/[a-zA-Z0-9_-]+\\.(png)|(jpg)/i', $name)) {
2013-10-08 19:11:01 +00:00
return new htmlStatusMessage('ERROR', _('Unable to upload logo file.'), _('The file name must end with ".png" or ".jpg".'));
}
$infos = getimagesize($file);
if ($infos[0] <= 2000 && $infos[1] <= 300) {
$dirPath = dirname(__FILE__) . '/../config/pdf/' . $_SESSION['config']->getName() . '/logos/';
$success = copy($file, $dirPath . '/' . $name);
if ($success) {
return new htmlStatusMessage('INFO', _('Uploaded logo file.'), $name);
}
else {
return new htmlStatusMessage('ERROR', _('Unable to upload logo file.'), $name);
}
}
return new htmlStatusMessage('ERROR', _('Unable to upload logo file.'), _('The file must not exeed 2000x300px.'));
}
/**
* Deletes a PDF logo file.
2016-12-19 20:32:08 +00:00
*
2013-10-08 19:11:01 +00:00
* @param String $name file name
* @return StatusMessage status message to display
*/
function deletePDFLogo($name) {
// check if valid file
$found = false;
$logos = getAvailableLogos();
foreach ($logos as $logo) {
if ($logo['filename'] === $name) {
$found = true;
break;
}
}
if (!$found) {
return new htmlStatusMessage('ERROR', _('File does not exist.'), htmlspecialchars($name));
}
// check if still in use
$activeTypes = $_SESSION['config']->get_ActiveTypes();
foreach ($activeTypes as $type) {
2017-01-05 20:05:17 +00:00
$structures = getPDFStructures($type);
2013-10-08 19:11:01 +00:00
foreach ($structures as $structure) {
2017-01-05 20:05:17 +00:00
$data = loadPDFStructure($type, $structure);
2013-10-08 19:11:01 +00:00
if ($data['page_definitions']['filename'] == $name) {
return new htmlStatusMessage('ERROR', _('Unable to delete logo file.'),
2017-01-04 19:52:51 +00:00
sprintf(_('Logo is still in use by PDF structure "%s" in account type "%s".'), $structure, \LAM\TYPES\getTypeAlias($type)));
2013-10-08 19:11:01 +00:00
}
}
}
// delete file
$dirPath = dirname(__FILE__) . '/../config/pdf/' . $_SESSION['config']->getName() . '/logos/';
$success = @unlink($dirPath . '/' . $name);
if ($success) {
return new htmlStatusMessage('INFO', _('Logo file deleted.'), $name);
}
else {
return new htmlStatusMessage('ERROR', _('Unable to delete logo file.'), $name);
}
}
2017-01-05 20:05:17 +00:00
/**
* 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;
}
2012-10-28 14:37:54 +00:00
?>