2003-08-20 16:09:41 +00:00
|
|
|
<?php
|
2003-05-31 10:52:15 +00:00
|
|
|
/*
|
|
|
|
$Id$
|
|
|
|
|
|
|
|
This code is part of LDAP Account Manager (http://www.sourceforge.net/projects/lam)
|
2003-11-03 16:33:16 +00:00
|
|
|
Copyright (C) 2003 Michael Drgner
|
2003-05-31 10:52:15 +00:00
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
|
2003-08-20 16:09:41 +00:00
|
|
|
LDAP Account Manager lamPDF class and some pdf functions
|
2003-05-31 10:52:15 +00:00
|
|
|
*/
|
|
|
|
|
2004-05-23 15:23:00 +00:00
|
|
|
define('FPDF_FONTPATH','font/');
|
2004-05-29 19:20:28 +00:00
|
|
|
define('LAMPDF_LINEWIDTH',190);
|
2003-08-20 16:09:41 +00:00
|
|
|
include_once("fpdf.php");
|
2004-05-23 15:23:00 +00:00
|
|
|
include_once('xml_parser.inc');
|
2003-05-31 10:52:15 +00:00
|
|
|
|
2004-05-23 15:23:00 +00:00
|
|
|
$key = false;
|
|
|
|
$line_width = LAMPDF_LINEWIDTH;
|
|
|
|
|
2004-06-03 14:26:48 +00:00
|
|
|
function getAvailablePDFStructures($scope='user') {
|
|
|
|
$return = array();
|
|
|
|
$dirHandle = opendir($_SESSION['lampath'] . '/config/pdf/' . $scope . '/');
|
|
|
|
while($file = readdir($dirHandle)) {
|
|
|
|
if(!is_dir($file) && $file != '.' && $file != '..') {
|
|
|
|
array_push($return,$file);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
sort($return);
|
|
|
|
return $return;
|
|
|
|
}
|
|
|
|
|
|
|
|
function createModulePDF($accounts,$pdf_structure="default.xml") {
|
2004-05-23 15:23:00 +00:00
|
|
|
|
|
|
|
global $key;
|
|
|
|
|
2004-05-27 18:13:16 +00:00
|
|
|
// Get account type from account container if none was specified or
|
|
|
|
// if it is different to the submitted or previous stored
|
|
|
|
if($account_type == "" || $account_type != $accounts[0]->get_type()) {
|
|
|
|
$account_type = $accounts[0]->get_type();
|
|
|
|
}
|
|
|
|
|
2004-06-03 14:26:48 +00:00
|
|
|
// Get PDF structure from xml file
|
|
|
|
$structure = getStructure($account_type,$pdf_structure);
|
|
|
|
|
2004-05-23 15:23:00 +00:00
|
|
|
// The decimal separator must be a dot in order to write pdf-files
|
|
|
|
setlocale(LC_NUMERIC, "C");
|
|
|
|
|
|
|
|
// Create a new PDF file acording to the account type
|
|
|
|
$pdf = new LamPDF($account_type);
|
|
|
|
|
|
|
|
// Loop over each account and add a new page in the PDF file for it
|
|
|
|
foreach($accounts as $account) {
|
2004-05-27 17:42:19 +00:00
|
|
|
|
2004-05-23 15:23:00 +00:00
|
|
|
// Start a new page for each account
|
|
|
|
$pdf->AddPage();
|
|
|
|
|
|
|
|
// Get PDF entries for the current account
|
|
|
|
$entries = $account->get_pdfEntries($account_type);
|
|
|
|
|
|
|
|
// Now create the PDF file acording to the structure with the submitted values
|
|
|
|
foreach($structure as $entry) {
|
|
|
|
// We have a new section to start
|
|
|
|
if($entry['tag'] == "SECTION" && $entry['type'] == "open") {
|
|
|
|
$name = $entry['attributes']['NAME'];
|
2004-05-29 19:20:28 +00:00
|
|
|
if(preg_match("/^\_[a-zA-Z\_]+/",$name)) {
|
2004-06-03 14:26:48 +00:00
|
|
|
$section_headline = getSectionHeadline($entries[substr($name,1)][0]);
|
2004-05-23 15:23:00 +00:00
|
|
|
}
|
|
|
|
else {
|
2004-05-29 19:20:28 +00:00
|
|
|
$section_headline = '- ' . _($name);
|
2004-05-23 15:23:00 +00:00
|
|
|
}
|
|
|
|
$pdf->setFont("arial","B",12);
|
2004-05-29 19:20:28 +00:00
|
|
|
$pdf->Write(5,$section_headline . ":");
|
2004-05-23 15:23:00 +00:00
|
|
|
$pdf->Ln(6);
|
|
|
|
}
|
|
|
|
// We have a section to end
|
|
|
|
elseif($entry['tag'] == "SECTION" && $entry['type'] == "close") {
|
|
|
|
$pdf->Ln(9);
|
|
|
|
}
|
|
|
|
// We have to include a static text.
|
|
|
|
elseif($entry['tag'] == "TEXT") {
|
2004-07-16 06:33:37 +00:00
|
|
|
// Load PDF text from structure array
|
|
|
|
$info_string = $entry['value'];
|
2004-05-23 15:23:00 +00:00
|
|
|
// Get all allowed vairables from account-object
|
|
|
|
$values = get_object_vars($account);
|
|
|
|
$values = array_keys($values);
|
|
|
|
// Replace $varstring in string with variable
|
|
|
|
foreach ($values as $value) {
|
|
|
|
// replace string
|
|
|
|
if (is_string($account->$value)) {
|
|
|
|
$info_string = str_replace('$'.$value, $account->$value, $info_string);
|
|
|
|
}
|
|
|
|
// replace object
|
|
|
|
elseif (is_object($account->$value)) {
|
|
|
|
$values2 = get_object_vars($account->$value);
|
|
|
|
$values2 = array_keys($values2);
|
|
|
|
foreach ($values2 as $value2) {
|
|
|
|
$info_string = str_replace('$'.$value.'->'.$value2, $account->$value->$value2, $info_string);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// replace array
|
|
|
|
elseif (is_array($account->$value)) {
|
|
|
|
foreach ($account->$value as $sub_array2) {
|
|
|
|
$sub_array .= $sub_array2.", ";
|
|
|
|
}
|
|
|
|
$sub_array = substr($sub_array, 0, -2);
|
|
|
|
$info_string = str_replace('$'.$value, $sub_array, $info_string);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Split string in array
|
|
|
|
$info_array = explode("\n", $info_string);
|
|
|
|
// Set font for text
|
|
|
|
$pdf->setFont("times","",10);
|
|
|
|
// Print each entry in array (each line in the text) as new line
|
|
|
|
foreach ($info_array as $info) {
|
|
|
|
$pdf->Cell(50,5,$info,0,1,"L",0);
|
|
|
|
}
|
|
|
|
// Print linebreak afterwards
|
|
|
|
$pdf->Ln(6);
|
|
|
|
}
|
|
|
|
// We have to include an entry from the account
|
|
|
|
elseif($entry['tag'] == "ENTRY") {
|
2004-05-27 17:42:19 +00:00
|
|
|
// Get name of current entry
|
|
|
|
$name = $entry['attributes']['NAME'];
|
|
|
|
|
2004-05-23 15:23:00 +00:00
|
|
|
// Get current entry
|
2004-05-27 17:42:19 +00:00
|
|
|
$value_entry = $entries[$name];
|
2004-05-23 15:23:00 +00:00
|
|
|
|
2004-05-27 19:07:50 +00:00
|
|
|
// Print entry only when module sumitted values for it
|
|
|
|
if(is_array($value_entry)) {
|
|
|
|
// Loop over all rows of this entry (most of the time this will be just one)
|
|
|
|
foreach($value_entry as $line) {
|
|
|
|
// Substitue XML syntax with valid FPDF methods
|
|
|
|
$methods = processLine($line);
|
|
|
|
// Call every method
|
|
|
|
foreach($methods as $method) {
|
|
|
|
call_user_func_array(array(&$pdf,$method[0]),$method[1]);
|
|
|
|
}
|
2004-05-23 15:23:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
$key = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Close PDF
|
2004-05-27 17:42:19 +00:00
|
|
|
$pdf->Close();
|
2004-05-23 15:23:00 +00:00
|
|
|
// Get relative url path
|
|
|
|
$fullpath = realpath('.');
|
|
|
|
$subdirs = explode('/', str_replace($_SESSION['lampath'], '', $fullpath));
|
|
|
|
for ($i=0; $i<count($subdirs); $i++ ) $filename .= '../';
|
|
|
|
// use timestamp and random number from ldap.inc as filename so it should be unique.
|
|
|
|
$filename .= 'tmp/' . $_SESSION['ldap']->rand . time() .'.pdf';
|
|
|
|
// Save PDF
|
2004-05-27 17:42:19 +00:00
|
|
|
$pdf->Output($filename);
|
2004-05-23 15:23:00 +00:00
|
|
|
// Output meta refresh to pdf-file
|
2004-06-03 14:26:48 +00:00
|
|
|
metaRefresh($filename);
|
2004-05-23 15:23:00 +00:00
|
|
|
// Return relative path of pdf-file
|
|
|
|
return $filename;
|
|
|
|
}
|
|
|
|
|
2004-06-03 14:26:48 +00:00
|
|
|
function getStructure($scope,$pdf_structure) {
|
2004-05-23 15:23:00 +00:00
|
|
|
$parser = new xmlParser();
|
2004-06-03 14:26:48 +00:00
|
|
|
$xml = $parser->parse($_SESSION['lampath'] . '/config/pdf/' . $scope . '/' . $pdf_structure);
|
2004-05-28 09:43:17 +00:00
|
|
|
|
2004-05-23 15:23:00 +00:00
|
|
|
$border = array();
|
|
|
|
$structure = array();
|
2004-06-03 14:26:48 +00:00
|
|
|
$pdf_entries = $xml[1]['PDF'];
|
|
|
|
while(($index = current($pdf_entries)) != null) {
|
|
|
|
$border['start'] = $index;
|
|
|
|
next($pdf_entries);
|
|
|
|
$border['end'] = current($pdf_entries);
|
|
|
|
next($pdf_entries);
|
|
|
|
}
|
|
|
|
return array_slice($xml[0],$border['start'] + 1,$border['end'] - ($border['start'] + 1));
|
|
|
|
}
|
|
|
|
|
|
|
|
function getSectionHeadline($line) {
|
|
|
|
$headline_pattern = '/<block>.*<value>(.*)<\/value><\/block>/';
|
|
|
|
if(preg_match($headline_pattern,$line,$matches)) {
|
|
|
|
$valueStyle = processFormatTags($matches[1],'');
|
|
|
|
return $valueStyle[1];
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return '';
|
2004-05-23 15:23:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function processLine($line,$first_td = true) {
|
|
|
|
global $key, $line_width;
|
|
|
|
|
|
|
|
// PCRE matching <block> tag
|
|
|
|
$block_pattern = '/<block><\/block>/';
|
|
|
|
// PCRE matching a <key> tag
|
|
|
|
$key_pattern = '/(<block>)<key>(.+)<\/key>(.*<\/block>)/';
|
|
|
|
// PCRE matching a <value> tag
|
2004-05-27 17:42:19 +00:00
|
|
|
// !!FIXME!! value must contain at least one character
|
|
|
|
$value_pattern = '/(<block>.*)<value>(.*)<\/value>(<\/block>)/';
|
2004-05-23 15:23:00 +00:00
|
|
|
// PCRE matching a <td> tag
|
|
|
|
$td_pattern = '/(<block>.*?)<td(.*?)>(.+?)<\/td>(.*<\/block>)/';
|
|
|
|
// PCRE matching a <p> tag
|
|
|
|
$p_pattern = '/(<block>.*)<p>(.+)<\/p>(.*<\/block>)/';
|
|
|
|
// PCRE matching a <br> tag
|
|
|
|
$br_pattern = '/<br \/>/';
|
|
|
|
|
|
|
|
$return = array();
|
|
|
|
if(preg_match($key_pattern,$line,$matches)) {
|
|
|
|
$key = true;
|
|
|
|
$line_width = $line_width - 50;
|
|
|
|
$format = processFormatTags($matches[2],'B');
|
|
|
|
$return[] = array('setFont',array('times',$format[0],10));
|
|
|
|
$return[] = array('Cell',array(50,5,$format[1] . ':',0,0,'R',0));
|
|
|
|
$return[] = array('setFont',array('times','',10));
|
|
|
|
return array_merge($return,processLine($matches[1] . $matches[3],false));
|
|
|
|
}
|
|
|
|
elseif(preg_match($value_pattern,$line,$matches)) {
|
|
|
|
$format = processFormatTags($matches[2],'');
|
|
|
|
$return[] = array('setFont',array('times',$format[0],10));
|
|
|
|
$return[] = array('Cell',array(50,5,$format[1],0,0,'L',0));
|
|
|
|
$return[] = array('setFont',array('times','',10));
|
|
|
|
return array_merge($return,processLine($matches[1] . $matches[3]));
|
|
|
|
}
|
|
|
|
elseif(preg_match($p_pattern,$line,$matches)) {
|
|
|
|
$format = processFormatTags($matches[2],'');
|
|
|
|
$return[] = array('setFont',array('times',$format[0],10));
|
|
|
|
$return[] = array('Write',array(5,$format[1]));
|
|
|
|
$return[] = array('setFont',array('times','',10));
|
|
|
|
return array_merge($return,processLine($matches[1] . $matches[3]));
|
|
|
|
}
|
|
|
|
elseif(preg_match($td_pattern,$line,$matches)) {
|
|
|
|
if($first_td && $key) {
|
|
|
|
$first_td = !$first_td;
|
|
|
|
$return[] = array('Cell',array(50,5,'',0,0,'L',0));
|
|
|
|
}
|
|
|
|
$format = processFormatTags($matches[3],'');
|
|
|
|
$attrs = processAttributes($matches[2],array('width' => $line_width,'height' => 5,'align' => 'L'));
|
|
|
|
$return[] = array('setFont',array('times',$format[0],10));
|
|
|
|
$return[] = array('Cell',array($attrs['width'],$attrs['height'],$format[1],0,0,$attrs['align'],0));
|
|
|
|
$return[] = array('setFont',array('times','',10));
|
|
|
|
return array_merge($return,processLine($matches[1] . $matches[4],$first_td));
|
|
|
|
}
|
|
|
|
elseif(preg_match($br_pattern,$line,$matches)) {
|
|
|
|
return array(array('Ln',array(5)));
|
|
|
|
}
|
|
|
|
elseif(preg_match($block_pattern,$line,$matches)) {
|
|
|
|
$line_width = LAMPDF_LINEWIDTH;
|
|
|
|
return array(array('Ln',array(5)));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function processFormatTags($line,$style) {
|
|
|
|
// PCRE matching a <i> tag
|
|
|
|
$i_pattern = '/(.*)<i>(.+)<\/i>(.*)/';
|
|
|
|
// PCRE matching a <b> tag
|
|
|
|
$b_pattern = '/(.*)<b>(.+)<\/b>(.*)/';
|
|
|
|
// PCRE matching a <u> tag
|
|
|
|
$u_pattern = '/(.*)<u>(.+)<\/u>(.*)/';
|
|
|
|
// Replacement pattern when one of the above pattern matched
|
2004-05-29 19:20:28 +00:00
|
|
|
$replace = "\$1\$2\$3";
|
2004-05-23 15:23:00 +00:00
|
|
|
|
|
|
|
if(preg_match($i_pattern,$line,$matches)) {
|
|
|
|
$style .= "I";
|
|
|
|
$line = preg_replace($i_pattern,$replace,$line);
|
|
|
|
}
|
|
|
|
if(preg_match($b_pattern,$line,$matches)) {
|
|
|
|
$style .= "B";
|
|
|
|
$line = preg_replace($b_pattern,$replace,$line);
|
|
|
|
}
|
|
|
|
if(preg_match($u_pattern,$line,$matches)) {
|
|
|
|
$style .= "U";
|
|
|
|
$line = preg_replace($u_pattern,$replace,$line);
|
|
|
|
}
|
|
|
|
return array($style,$line);
|
|
|
|
}
|
|
|
|
|
|
|
|
function processAttributes($attrs,$return = array()) {
|
|
|
|
global $line_width;
|
|
|
|
|
|
|
|
// PCRE matching width attribute
|
|
|
|
$width_pattern = '/(.*)width\=\"(\\d+)(\%?)\"(.*)/';
|
|
|
|
// PCRE matching height attribute
|
|
|
|
$height_pattern = '/(.*)height\=\"(\\d+)\"(.*)/';
|
|
|
|
// PCRE matching align attribute
|
|
|
|
$align_pattern = '/(.*)align\=\"(L|R|C)\"(.*)/';
|
|
|
|
|
|
|
|
// Remove leading and trailing whitespaces
|
|
|
|
$attrs = trim($attrs);
|
|
|
|
|
|
|
|
if(preg_match($width_pattern,$attrs,$matches)) {
|
|
|
|
if($matches[3] == '%') {
|
|
|
|
$return['width'] = ceil($line_width * $matches[2] / 100);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
$return['width'] = ceil($matches[2]);
|
|
|
|
}
|
|
|
|
return processAttributes($matches[1] . $matches[4],$return);
|
|
|
|
}
|
|
|
|
elseif(preg_match($height_pattern,$attrs,$matches)) {
|
|
|
|
$return['height'] = $matches[2];
|
|
|
|
return processAttributes($matches[1] . $matches[3],$return);
|
|
|
|
}
|
|
|
|
elseif(preg_match($align_pattern,$attrs,$matches)) {
|
|
|
|
$return['align'] = $matches[2];
|
|
|
|
return processAttributes($matches[1] . $matches[3],$return);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return $return;
|
|
|
|
}
|
|
|
|
}
|
2003-09-28 16:38:54 +00:00
|
|
|
|
2003-10-18 17:23:07 +00:00
|
|
|
|
2003-08-21 15:27:42 +00:00
|
|
|
// lamPDF class || For defining own a Header and Footer
|
2003-08-20 16:09:41 +00:00
|
|
|
class lamPDF extends FPDF {
|
2004-05-23 15:23:00 +00:00
|
|
|
|
|
|
|
function lamPDF($account_type = "User") {
|
|
|
|
// Call constructor of superclass
|
|
|
|
$this->FPDF('P','mm','A4');
|
|
|
|
|
|
|
|
// Decide which PDF file type we shall use
|
|
|
|
switch($account_type) {
|
2004-05-27 18:13:16 +00:00
|
|
|
case "user":
|
2004-05-23 15:23:00 +00:00
|
|
|
$subject = _("User information page");
|
|
|
|
break;
|
2004-05-27 18:13:16 +00:00
|
|
|
case "group":
|
2004-05-23 15:23:00 +00:00
|
|
|
$subject = _("Group information page");
|
|
|
|
break;
|
2004-05-27 18:13:16 +00:00
|
|
|
case "host":
|
2004-05-23 15:23:00 +00:00
|
|
|
$subject = _("Samba-Host information page");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Open PDF file and write some basic information
|
|
|
|
$this->Open();
|
|
|
|
$this->setFont("arial","",12);
|
|
|
|
$this->setTitle("LDAP Account Manager");
|
|
|
|
$this->setSubject($subject);
|
|
|
|
$this->setAuthor("LDAP Account Manager Devel-Team -Michael Duergner-");
|
|
|
|
$this->setCreator("LDAP Account Manager (pdf.inc)");
|
|
|
|
}
|
|
|
|
|
2003-08-21 15:27:42 +00:00
|
|
|
// Print page header
|
2003-08-20 16:09:41 +00:00
|
|
|
function header() {
|
2003-08-26 20:48:20 +00:00
|
|
|
$imageFile = substr(__FILE__,0,strlen(__FILE__)- 11) . "graphics/printLogo.jpg";
|
2003-08-26 20:29:47 +00:00
|
|
|
$this->Image($imageFile,10,10,50,20,"JPG");
|
2003-08-20 16:09:41 +00:00
|
|
|
$this->SetFont("arial","B",22);
|
|
|
|
$this->Cell(170,5,"LDAP Account Manager",0,1,"R",0);
|
|
|
|
$this->Ln(3);
|
|
|
|
$this->SetFont("times","",14);
|
2004-05-23 15:23:00 +00:00
|
|
|
$this->Cell(170,5,"- " . $this->subject . " -",0,0,"R",0);
|
2003-08-20 16:09:41 +00:00
|
|
|
$this->SetLineWidth(0.8);
|
|
|
|
$this->Line(10,40,200,40);
|
|
|
|
$this->Line(10,42,200,42);
|
2004-01-15 16:19:10 +00:00
|
|
|
$this->SetY(50);
|
2003-08-20 16:09:41 +00:00
|
|
|
}
|
2003-05-31 10:52:15 +00:00
|
|
|
|
2003-08-21 15:27:42 +00:00
|
|
|
// Print page footer
|
2003-08-20 16:09:41 +00:00
|
|
|
function footer() {
|
|
|
|
$this->SetLineWidth(0.8);
|
|
|
|
$this->Line(10,280,200,280);
|
|
|
|
$this->Line(10,282,200,282);
|
|
|
|
$this->SetY(285);
|
2003-09-10 17:49:46 +00:00
|
|
|
$this->Cell(0,5,_("This document was automatically created by LDAP Account Manager"),0,0,"C",0);
|
2003-08-20 16:09:41 +00:00
|
|
|
}
|
2004-05-23 15:23:00 +00:00
|
|
|
}
|