allow to create PDF file on upload

This commit is contained in:
Roland Gruber 2012-02-05 10:38:59 +00:00
parent b4613adce2
commit 15b33ea717
9 changed files with 136 additions and 20 deletions

View File

@ -1,7 +1,9 @@
March 2012 3.7
- Login: support bind user for login search
- Personal: added labeledURI and cosmetic changes, description is now multi-valued (RFE 3446363)
- File upload: support custom scripts postCreate (LAM Pro)
- File upload:
-> support custom scripts postCreate (LAM Pro)
-> PDF export
- New translation: Slovakian
- removed phpGroupWare support (project no longer exists)
- LAM Pro:

View File

@ -103,6 +103,10 @@ if (isset($memLimit) && ($memLimit != '') && (substr(strtoupper($memLimit), strl
if (!@preg_match('/^\p{L}+$/u', "abc")) {
$criticalErrors[] = array("ERROR", "Your PCRE library has no complete Unicode support. Please upgrade libpcre or compile with \"--enable-unicode-properties\".");
}
// check ZIP support for PDF files in file upload
if (!extension_loaded('zip')) {
$criticalErrors[] = array("ERROR", "Your PHP has no ZIP support.", "Please install the ZIP extension for PHP.");
}
// stop login if critical errors occured
if (sizeof($criticalErrors) > 0) {
echo "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\" \"http://www.w3.org/TR/html4/loose.dtd\">\n\n";

View File

@ -4,7 +4,7 @@ $Id$
This code is part of LDAP Account Manager (http://www.ldap-account-manager.org/)
Copyright (C) 2003 - 2006 Michael Duergner
2011 Roland Gruber
2007 - 2012 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
@ -27,6 +27,7 @@ $Id$
* and furthermore some helper functions.
*
* @author Michael Duergner
* @author Roland Gruber
* @package PDF
*/
@ -35,6 +36,7 @@ $Id$
*
* @package PDF
* @author Michael Duergner
* @author Roland Gruber
*/
class lamPDF extends UFPDF {
@ -64,7 +66,9 @@ class lamPDF extends UFPDF {
*/
function __construct($page_definitions = array(),$fontName) {
$this->fontName = $fontName;
define('FPDF_FONTPATH', dirname(__FILE__) . '/font/');
if (!defined('FPDF_FONTPATH')) {
define('FPDF_FONTPATH', dirname(__FILE__) . '/font/');
}
// Call constructor of superclass
$this->FPDF('P','mm','A4');

View File

@ -122,7 +122,9 @@ class Ldap{
/** Closes connection to server */
function close() {
@ldap_close($this->server);
if ($this->server != null) {
@ldap_close($this->server);
}
}
/**
@ -158,7 +160,7 @@ class Ldap{
$dir = @opendir($tmpDir);
$file = @readdir($dir);
while ($file) {
if ((substr($file, -4) == '.pdf') || (substr($file, -4) == '.jpg')) {
if ((substr($file, -4) == '.pdf') || (substr($file, -4) == '.jpg') || (substr($file, -4) == '.zip')) {
$path = $tmpDir . $file;
if ($time - filemtime($path) > 600) {
@unlink($path);

View File

@ -923,6 +923,9 @@ class lamList {
}
elseif (isset($_GET['uploadAllOk'])) {
StatusMessage('INFO', _("Upload has finished"));
if (isset($_SESSION['mass_pdf']['file'])) {
StatusMessage('INFO', sprintf(_('You can download your PDF files {link=%s}here{endlink}.'), '../' . $_SESSION['mass_pdf']['file']));
}
}
elseif (isset($_SESSION['listRedirectMessages'])) {
for ($i = 0; $i < sizeof($_SESSION['listRedirectMessages']); $i++) {

View File

@ -3,7 +3,8 @@
$Id$
This code is part of LDAP Account Manager (http://www.ldap-account-manager.org/)
Copyright (C) 2003 - 2006 Michael Duergner
Copyright (C) 2003 - 2004 Michael Duergner
2003 - 2012 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
@ -26,6 +27,7 @@ $Id$
* and furthermore some helper functions.
*
* @author Michael Duergner
* @author Roland Gruber
* @package PDF
*/
@ -49,9 +51,10 @@ $line_width = LAMPDF_LINEWIDTH;
* @param string $pdf_structure The filename of the structure definition that should be used
* to create the PDF page. If not submitted the 'default.user' structure definition
* for the appropriate account type.
* @param $returnAsString returns the PDF output as String value instead of writing it to a file
* @return String PDF file name
*/
function createModulePDF($accounts,$pdf_structure="default") {
function createModulePDF($accounts, $pdf_structure="default", $returnAsString = false) {
/** PDF generator class */
include_once("fpdf.php");
@ -142,12 +145,17 @@ function createModulePDF($accounts,$pdf_structure="default") {
// Close PDF
$pdf->Close();
// use timestamp and random number from ldap.inc as filename so it should be unique.
$filename = '../../tmp/' . $_SESSION['ldap']->new_rand() . time() .'.pdf';
// Save PDF
$pdf->Output($filename);
// return PDF file name
return $filename;
if (!$returnAsString) {
// use timestamp and random number from ldap.inc as filename so it should be unique.
$filename = '../../tmp/' . $_SESSION['ldap']->new_rand() . time() .'.pdf';
// Save PDF
$pdf->Output($filename);
// return PDF file name
return $filename;
}
else {
return $pdf->Output('', 'S');
}
}
/**

View File

@ -3,7 +3,7 @@
$Id$
This code is part of LDAP Account Manager (http://www.ldap-account-manager.org/)
Copyright (C) 2004 - 2011 Roland Gruber
Copyright (C) 2004 - 2012 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
@ -195,6 +195,17 @@ if ($_FILES['inputfile'] && ($_FILES['inputfile']['size'] > 0)) {
$_SESSION['mass_ids'] = $ids;
$_SESSION['mass_scope'] = $_POST['scope'];
$_SESSION['mass_selectedModules'] = $selectedModules;
if (isset($_SESSION['mass_pdf'])) {
unset($_SESSION['mass_pdf']);
}
if (isset($_POST['createPDF']) && ($_POST['createPDF'] == 'on')) {
$_SESSION['mass_pdf']['structure'] = $_POST['pdfStructure'];
$_SESSION['mass_pdf']['counter'] = 0;
$_SESSION['mass_pdf']['file'] = '../tmp/lam_pdf' . $_SESSION['ldap']->new_rand() . '.zip';
}
else {
$_SESSION['mass_pdf']['structure'] = null;
}
// show links for upload and LDIF export
echo "<div class=\"title\">\n";
echo "<h2 class=\"titleText\">" . _("LAM has checked your input and is now ready to create the accounts.") . "</h2>\n";
@ -238,6 +249,12 @@ function massPrintBackButton($scope, $selectedModules) {
$container->addElement(new htmlSpacer(null, '10px'), true);
$container->addElement(new htmlButton('submit', _('Back')));
$container->addElement(new htmlHiddenInput('type', $scope));
$createPDF = 0;
if (isset($_POST['createPDF']) && ($_POST['createPDF'] == 'on')) {
$createPDF = 1;
}
$container->addElement(new htmlHiddenInput('createPDF', $createPDF));
$container->addElement(new htmlHiddenInput('pdfStructure', $_POST['pdfStructure']));
for ($i = 0; $i < sizeof($selectedModules); $i++) {
$container->addElement(new htmlHiddenInput($scope . '_' . $selectedModules[$i], 'on'));
}

View File

@ -3,7 +3,7 @@
$Id$
This code is part of LDAP Account Manager (http://www.ldap-account-manager.org/)
Copyright (C) 2004 - 2011 Roland Gruber
Copyright (C) 2004 - 2012 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
@ -38,6 +38,8 @@ include_once('../lib/ldap.inc');
include_once('../lib/status.inc');
/** account modules */
include_once('../lib/modules.inc');
/** PDF */
include_once('../lib/pdf.inc');
// Start session
@ -60,12 +62,11 @@ echo '<div class="' . $_SESSION['mass_scope'] . 'list-bright smallPaddingContent
// create accounts
$accounts = unserialize($_SESSION['ldap']->decrypt($_SESSION['mass_accounts']));
if (($_SESSION['mass_counter'] < sizeof($accounts)) || !isset($_SESSION['mass_postActions']['finished'])) {
if (($_SESSION['mass_counter'] < sizeof($accounts)) || !isset($_SESSION['mass_postActions']['finished']) || !isset($_SESSION['mass_pdf']['finished'])) {
$startTime = time();
$maxTime = get_cfg_var('max_execution_time') - 5;
if ($maxTime > 60) $maxTime = 60;
if ($maxTime <= 0) $maxTime = 60;
$refreshTime = $maxTime + 7;
echo "<div class=\"title\">\n";
echo "<h2 class=\"titleText\">" . _("LDAP upload in progress. Please wait.") . "</h2>\n";
echo "</div>";
@ -116,7 +117,7 @@ if (($_SESSION['mass_counter'] < sizeof($accounts)) || !isset($_SESSION['mass_po
</script>
<?php
flush(); // send HTML to browser
// do post upload actions
// do post upload actions after all accounts are created
if ($_SESSION['mass_counter'] >= sizeof($accounts)) {
$data = unserialize($_SESSION['ldap']->decrypt($_SESSION['mass_data']));
$return = doUploadPostActions($_SESSION['mass_scope'], $data, $_SESSION['mass_ids'], $_SESSION['mass_failed'], $_SESSION['mass_selectedModules'], $accounts);
@ -148,6 +149,61 @@ if (($_SESSION['mass_counter'] < sizeof($accounts)) || !isset($_SESSION['mass_po
}
}
}
// create PDF when upload post actions are done
if (isset($_SESSION['mass_postActions']['finished'])) {
if (($_SESSION['mass_pdf']['structure'] != null) && !isset($_SESSION['mass_pdf']['finished'])) {
$file = $_SESSION['mass_pdf']['file'];
$pdfStructure = $_SESSION['mass_pdf']['structure'];
$pdfZip = new ZipArchive();
if ($_SESSION['mass_pdf']['counter'] == 0) {
$pdfZipResult = @$pdfZip->open($_SESSION['mass_pdf']['file'], ZipArchive::CREATE);
if (!$pdfZipResult === true) {
$_SESSION['mass_errors'][] = array('ERROR', _('Unable to create ZIP file for PDF export.'), $file);
$_SESSION['mass_pdf']['finished'] = true;
}
}
else {
@$pdfZip->open($_SESSION['mass_pdf']['file']);
}
// show progress bar
$progress = ($_SESSION['mass_pdf']['counter'] * 100) / sizeof($accounts);
echo "<h1>" . _('Create PDF files') . "</h1>\n";
?>
<div id="progressbarPDF"></div>
<script type="text/javascript">
$(function() {
$( "#progressbarPDF" ).progressbar({
value: <?php echo $progress; ?>
});
});
</script>
<?php
flush();
while (!isset($_SESSION['mass_pdf']['finished']) && (($startTime + $maxTime) > time())) {
// load account
$attrs = $accounts[$_SESSION['mass_pdf']['counter']];
$dn = $attrs['dn'];
$_SESSION['pdfAccount'] = new accountContainer($_SESSION['mass_scope'], 'pdfAccount');
$pdfErrors = $_SESSION['pdfAccount']->load_account($dn);
if (sizeof($pdfErrors) > 0) {
$_SESSION['mass_errors'] = array_merge($_SESSION['mass_errors'], $pdfErrors);
$_SESSION['mass_pdf']['finished'] = true;
break;
}
// create and save PDF
$pdfContent = createModulePDF(array($_SESSION['pdfAccount']), $pdfStructure, true);
$pdfZip->addFromString($dn, $pdfContent);
$_SESSION['mass_pdf']['counter'] ++;
if ($_SESSION['mass_pdf']['counter'] >= sizeof($accounts)) {
$_SESSION['mass_pdf']['finished'] = true;
}
}
@$pdfZip->close();
}
else {
$_SESSION['mass_pdf']['finished'] = true;
}
}
// refresh with JavaScript
echo "<script type=\"text/javascript\">\n";
echo "top.location.href = \"massDoUpload.php\";\n";

View File

@ -3,7 +3,7 @@
$Id$
This code is part of LDAP Account Manager (http://www.ldap-account-manager.org/)
Copyright (C) 2004 - 2011 Roland Gruber
Copyright (C) 2004 - 2012 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
@ -36,6 +36,8 @@ include_once('../lib/config.inc');
include_once('../lib/status.inc');
/** account modules */
include_once('../lib/modules.inc');
/** Used to get PDF information. */
include_once('../lib/pdfstruct.inc');
// Start session
@ -238,11 +240,29 @@ function showMainPage($scope, $selectedModules) {
$inputContainer = new htmlTable();
$inputContainer->addElement(new htmlOutputText(_("CSV file")));
$inputContainer->addElement(new htmlInputFileUpload('inputfile'));
$inputContainer->addElement(new htmlButton('submitfile', _('Upload file and create accounts')));
$inputContainer->addElement(new htmlSpacer('10px', null));
$inputContainer->addElement(new htmlLink(_("Download sample CSV file"), 'masscreate.php?getCSV=1'));
$inputContainer->addElement(new htmlHiddenInput('scope', $scope));
$inputContainer->addElement(new htmlHiddenInput('selectedModules', implode(',', $selectedModules)), true);
// PDF
$createPDF = false;
if (isset($_POST['createPDF']) && ($_POST['createPDF'] === '1')) {
$createPDF = true;
}
$pdfCheckbox = new htmlTableExtendedInputCheckbox('createPDF', $createPDF, _('Create PDF files'));
$pdfCheckbox->setTableRowsToShow(array('pdfStructure'));
$inputContainer->addElement($pdfCheckbox, true);
$pdfStructures = getPDFStructureDefinitions($scope);
$pdfSelected = array();
if (isset($_POST['pdfStructure'])) {
$pdfSelected = array($_POST['pdfStructure']);
}
else if (in_array('default', $pdfStructures)) {
$pdfSelected = array('default');
}
$inputContainer->addElement(new htmlTableExtendedSelect('pdfStructure', $pdfStructures, $pdfSelected, _('PDF structure')), true);
$inputContainer->addElement(new htmlSpacer(null, '5px'), true);
$inputContainer->addElement(new htmlButton('submitfile', _('Upload file and create accounts')));
$container->addElement($inputContainer, true);
$container->addElement(new htmlSpacer(null, '10px'), true);
// column list