This commit is contained in:
Roland Gruber 2018-09-23 20:12:27 +02:00
parent 23e58208cf
commit f2d77dc851
5 changed files with 209 additions and 6 deletions

127
lam/lib/export.inc Normal file
View File

@ -0,0 +1,127 @@
<?php
namespace LAM\TOOLS\IMPORT_EXPORT;
use \htmlStatusMessage;
use \LAMException;
/*
This code is part of LDAP Account Manager (http://www.ldap-account-manager.org/)
Copyright (C) 2018 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
*/
/**
* LDIF export.
*
* @author Roland Gruber
* @package tools
*/
/** LDAP handle */
include_once('ldap.inc');
/**
* Creates LDAP accounts for file upload.
*
* @author Roland Gruber
* @package tools
*/
class Exporter {
const DATA = 'data';
const STATUS = 'status';
private $baseDn = null;
private $searchScope = null;
private $filter = null;
private $attributes = null;
private $includeSystem = false;
private $saveAsFile = false;
private $format = null;
private $ending = null;
/**
* Constructor.
*
* @param string $baseDn base DN
* @param string $searchScope search scope (base, one, sub)
* @param string $filter search filter
* @param string $attributes attributes to return
* @param bool $includeSystem include system attributes
* @param bool $saveAsFile return as file
* @param string $format output format (LDIF, CSV)
* @param string $ending line endings (windows, unix)
*/
public function __construct($baseDn, $searchScope, $filter, $attributes, $includeSystem, $saveAsFile, $format, $ending) {
$this->baseDn = $baseDn;
$this->searchScope = $searchScope;
$this->filter = $filter;
$this->attributes = $attributes;
$this->includeSystem = $includeSystem;
$this->saveAsFile = $saveAsFile;
$this->format = $format;
$this->ending = $ending;
}
/**
* Starts the export process.
*
* @return string JSON result
*/
public function doExport() {
try {
$this->checkParameters();
}
catch (LAMException $e) {
$data = Exporter::formatMessage('ERROR', $e->getTitle(), $e->getMessage());
$status = array(
Exporter::STATUS => 'failed',
Exporter::DATA => $data
);
return json_encode($status);
}
}
/**
* Returns the HTML for an error message.
*
* @param string $type message type (e.g. INFO)
* @param string $title title
* @param string $message message
* @return string HTML
*/
public static function formatMessage($type, $title, $message) {
$msg = new htmlStatusMessage($type, $title, $message);
$tabindex = 0;
ob_start();
$msg->generateHTML(null, array($msg), array(), true, $tabindex, 'user');
$data = ob_get_contents();
ob_clean();
return $data;
}
/**
* Checks the input parameters for validity.
*
* @throws LAMException in case of errors
*/
private function checkParameters() {
if (!get_preg($this->baseDn, 'dn')) {
throw new LAMException(_('Please enter a valid DN in the field:'), _('Base DN'));
}
}
}

View File

@ -34,7 +34,7 @@ use \LAMException;
include_once('ldap.inc'); include_once('ldap.inc');
/** /**
* Creates LDAP accounts for file upload. * Imports LDIF files.
* *
* @author Roland Gruber * @author Roland Gruber
* @package tools * @package tools

View File

@ -901,7 +901,7 @@ window.lam.tools.schema.select = function() {
}); });
}; };
window.lam.import = window.lam.import || {}; window.lam.importexport = window.lam.importexport || {};
/** /**
* Starts the import process. * Starts the import process.
@ -909,7 +909,7 @@ window.lam.import = window.lam.import || {};
* @param tokenName name of CSRF token * @param tokenName name of CSRF token
* @param tokenValue value of CSRF token * @param tokenValue value of CSRF token
*/ */
window.lam.import.startImport = function(tokenName, tokenValue) { window.lam.importexport.startImport = function(tokenName, tokenValue) {
jQuery(document).ready(function() { jQuery(document).ready(function() {
jQuery('#progressbarImport').progressbar(); jQuery('#progressbarImport').progressbar();
var output = jQuery('#importResults'); var output = jQuery('#importResults');
@ -949,6 +949,55 @@ window.lam.import.startImport = function(tokenName, tokenValue) {
}); });
}; };
/**
* Starts the export process.
*
* @param tokenName name of CSRF token
* @param tokenValue value of CSRF token
*/
window.lam.importexport.startExport = function(tokenName, tokenValue) {
jQuery(document).ready(function() {
jQuery('#progressbarExport').progressbar({value: 50});
var output = jQuery('#exportResults');
var data = {
jsonInput: ''
};
data[tokenName] = tokenValue;
data['baseDn'] = jQuery('#baseDn').val();
data['searchScope'] = jQuery('#searchScope').val();
data['filter'] = jQuery('#filter').val();
data['attributes'] = jQuery('#attributes').val();
data['format'] = jQuery('#format').val();
data['ending'] = jQuery('#ending').val();
data['includeSystem'] = jQuery('#includeSystem').val();
data['saveAsFile'] = jQuery('#saveAsFile').val();
jQuery.ajax({
url: '../misc/ajax.php?function=export',
method: 'POST',
data: data
})
.done(function(jsonData){
if (jsonData.data && (jsonData.data != '')) {
output.append(jsonData.data);
}
if (jsonData.status == 'done') {
jQuery('#progressbarExport').hide();
jQuery('#btn_submitExportCancel').hide();
jQuery('#statusExportInprogress').hide();
jQuery('#statusExportDone').show();
jQuery('.newexport').show();
}
else {
jQuery('#progressbarExport').hide();
jQuery('#btn_submitExportCancel').hide();
jQuery('#statusExportInprogress').hide();
jQuery('#statusExportFailed').show();
jQuery('.newexport').show();
}
});
});
};
jQuery(document).ready(function() { jQuery(document).ready(function() {
window.lam.gui.equalHeight(); window.lam.gui.equalHeight();
window.lam.form.autoTrim(); window.lam.form.autoTrim();

View File

@ -1,6 +1,7 @@
<?php <?php
namespace LAM\AJAX; namespace LAM\AJAX;
use \LAM\TOOLS\IMPORT_EXPORT\Importer; use \LAM\TOOLS\IMPORT_EXPORT\Importer;
use \LAM\TOOLS\IMPORT_EXPORT\Exporter;
/* /*
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/)
@ -102,6 +103,22 @@ class Ajax {
ob_end_clean(); ob_end_clean();
echo $jsonOut; echo $jsonOut;
} }
elseif ($function === 'export') {
include_once('../../lib/export.inc');
$attributes = $_POST['attributes'];
$baseDn = $_POST['baseDn'];
$ending = $_POST['ending'];
$filter = $_POST['filter'];
$format = $_POST['format'];
$includeSystem = ($_POST['includeSystem'] === 'true');
$saveAsFile = ($_POST['saveAsFile'] === 'true');
$searchScope = $_POST['searchScope'];
$exporter = new Exporter($baseDn, $searchScope, $filter, $attributes, $includeSystem, $saveAsFile, $format, $ending);
ob_start();
$jsonOut = $exporter->doExport();
ob_end_clean();
echo $jsonOut;
}
elseif ($function === 'upload') { elseif ($function === 'upload') {
include_once('../../lib/upload.inc'); include_once('../../lib/upload.inc');
$typeManager = new \LAM\TYPES\TypeManager(); $typeManager = new \LAM\TYPES\TypeManager();

View File

@ -17,6 +17,7 @@ use \htmlResponsiveSelect;
use \htmlResponsiveInputField; use \htmlResponsiveInputField;
use \htmlGroup; use \htmlGroup;
use \htmlInputField; use \htmlInputField;
use \htmlHiddenInput;
use LAM\TYPES\TypeManager; use LAM\TYPES\TypeManager;
/* /*
@ -215,7 +216,7 @@ function printImportTabProcessing(&$tabindex) {
$container->add(new htmlDiv('importResults', new htmlOutputText('')), 12); $container->add(new htmlDiv('importResults', new htmlOutputText('')), 12);
$container->add(new htmlJavaScript( $container->add(new htmlJavaScript(
'window.lam.import.startImport(\'' . getSecurityTokenName() . '\', \'' . getSecurityTokenValue() . '\');' 'window.lam.importexport.startImport(\'' . getSecurityTokenName() . '\', \'' . getSecurityTokenValue() . '\');'
), 12); ), 12);
addSecurityTokenToMetaHTML($container); addSecurityTokenToMetaHTML($container);
@ -273,7 +274,7 @@ function printExportTabContent(&$tabindex) {
_('One (one level beneath base)') => 'one', _('One (one level beneath base)') => 'one',
_('Sub (entire subtree)') => 'sub' _('Sub (entire subtree)') => 'sub'
); );
$searchScopeSelect = new htmlResponsiveSelect('searchscope', $searchScopes, array('sub'), _('Search scope')); $searchScopeSelect = new htmlResponsiveSelect('searchScope', $searchScopes, array('sub'), _('Search scope'));
$searchScopeSelect->setHasDescriptiveElements(true); $searchScopeSelect->setHasDescriptiveElements(true);
$searchScopeSelect->setSortElements(false); $searchScopeSelect->setSortElements(false);
$container->add($searchScopeSelect, 12); $container->add($searchScopeSelect, 12);
@ -351,6 +352,15 @@ function printExportTabProcessing(&$tabindex) {
$container = new htmlResponsiveRow(); $container = new htmlResponsiveRow();
$container->add(new htmlTitle(_("Export")), 12); $container->add(new htmlTitle(_("Export")), 12);
$container->add(new htmlHiddenInput('baseDn', $_POST['baseDn']), 12);
$container->add(new htmlHiddenInput('searchScope', $_POST['searchScope']), 12);
$container->add(new htmlHiddenInput('filter', $_POST['filter']), 12);
$container->add(new htmlHiddenInput('attributes', $_POST['attributes']), 12);
$container->add(new htmlHiddenInput('format', $_POST['format']), 12);
$container->add(new htmlHiddenInput('ending', $_POST['ending']), 12);
$container->add(new htmlHiddenInput('includeSystem', isset($_POST['includeSystem']) && ($_POST['includeSystem'] === 'on') ? 'true' : 'false'), 12);
$container->add(new htmlHiddenInput('saveAsFile', isset($_POST['saveAsFile']) && ($_POST['saveAsFile'] === 'on') ? 'true' : 'false'), 12);
$container->add(new htmlDiv('statusExportInprogress', new htmlOutputText(_('Status') . ': ' . _('in progress'))), 12); $container->add(new htmlDiv('statusExportInprogress', new htmlOutputText(_('Status') . ': ' . _('in progress'))), 12);
$container->add(new htmlDiv('statusExportDone', new htmlOutputText(_('Status') . ': ' . _('done')), array('hidden')), 12); $container->add(new htmlDiv('statusExportDone', new htmlOutputText(_('Status') . ': ' . _('done')), array('hidden')), 12);
$container->add(new htmlDiv('statusExportFailed', new htmlOutputText(_('Status') . ': ' . _('failed')), array('hidden')), 12); $container->add(new htmlDiv('statusExportFailed', new htmlOutputText(_('Status') . ': ' . _('failed')), array('hidden')), 12);
@ -367,7 +377,7 @@ function printExportTabProcessing(&$tabindex) {
$container->add(new htmlDiv('exportResults', new htmlOutputText('')), 12); $container->add(new htmlDiv('exportResults', new htmlOutputText('')), 12);
$container->add(new htmlJavaScript( $container->add(new htmlJavaScript(
'window.lam.export.startExport(\'' . getSecurityTokenName() . '\', \'' . getSecurityTokenValue() . '\');' 'window.lam.importexport.startExport(\'' . getSecurityTokenName() . '\', \'' . getSecurityTokenValue() . '\');'
), 12); ), 12);
addSecurityTokenToMetaHTML($container); addSecurityTokenToMetaHTML($container);