export
This commit is contained in:
		
							parent
							
								
									23e58208cf
								
							
						
					
					
						commit
						f2d77dc851
					
				|  | @ -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')); | ||||
| 		} | ||||
| 	} | ||||
| 
 | ||||
| } | ||||
|  | @ -34,7 +34,7 @@ use \LAMException; | |||
| include_once('ldap.inc'); | ||||
| 
 | ||||
| /** | ||||
|  * Creates LDAP accounts for file upload. | ||||
|  * Imports LDIF files. | ||||
|  * | ||||
|  * @author Roland Gruber | ||||
|  * @package tools | ||||
|  |  | |||
|  | @ -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. | ||||
|  | @ -909,7 +909,7 @@ window.lam.import = window.lam.import || {}; | |||
|  * @param tokenName name 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('#progressbarImport').progressbar(); | ||||
| 		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() { | ||||
| 	window.lam.gui.equalHeight(); | ||||
| 	window.lam.form.autoTrim(); | ||||
|  |  | |||
|  | @ -1,6 +1,7 @@ | |||
| <?php | ||||
| namespace LAM\AJAX; | ||||
| 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/) | ||||
|  | @ -102,6 +103,22 @@ class Ajax { | |||
| 			ob_end_clean(); | ||||
| 			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') { | ||||
| 			include_once('../../lib/upload.inc'); | ||||
| 			$typeManager = new \LAM\TYPES\TypeManager(); | ||||
|  |  | |||
|  | @ -17,6 +17,7 @@ use \htmlResponsiveSelect; | |||
| use \htmlResponsiveInputField; | ||||
| use \htmlGroup; | ||||
| use \htmlInputField; | ||||
| use \htmlHiddenInput; | ||||
| use LAM\TYPES\TypeManager; | ||||
| 
 | ||||
| /* | ||||
|  | @ -215,7 +216,7 @@ function printImportTabProcessing(&$tabindex) { | |||
| 
 | ||||
| 	$container->add(new htmlDiv('importResults', new htmlOutputText('')), 12); | ||||
| 	$container->add(new htmlJavaScript( | ||||
| 			'window.lam.import.startImport(\'' . getSecurityTokenName() . '\', \'' . getSecurityTokenValue() . '\');' | ||||
| 			'window.lam.importexport.startImport(\'' . getSecurityTokenName() . '\', \'' . getSecurityTokenValue() . '\');' | ||||
| 		), 12); | ||||
| 
 | ||||
| 	addSecurityTokenToMetaHTML($container); | ||||
|  | @ -273,7 +274,7 @@ function printExportTabContent(&$tabindex) { | |||
| 		_('One (one level beneath base)') => 'one', | ||||
| 		_('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->setSortElements(false); | ||||
| 	$container->add($searchScopeSelect, 12); | ||||
|  | @ -351,6 +352,15 @@ function printExportTabProcessing(&$tabindex) { | |||
| 	$container = new htmlResponsiveRow(); | ||||
| 	$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('statusExportDone', new htmlOutputText(_('Status') . ': ' . _('done')), 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 htmlJavaScript( | ||||
| 			'window.lam.export.startExport(\'' . getSecurityTokenName() . '\', \'' . getSecurityTokenValue() . '\');' | ||||
| 			'window.lam.importexport.startExport(\'' . getSecurityTokenName() . '\', \'' . getSecurityTokenValue() . '\');' | ||||
| 		), 12); | ||||
| 
 | ||||
| 	addSecurityTokenToMetaHTML($container); | ||||
|  |  | |||
		Loading…
	
		Reference in New Issue