LDAPAccountManager/lam/templates/tools/importexport.php

400 lines
13 KiB
PHP
Raw Normal View History

2018-08-31 18:59:05 +00:00
<?php
namespace LAM\TOOLS\IMPORT_EXPORT;
use \htmlTitle;
use \htmlResponsiveRadio;
use \htmlResponsiveRow;
use \htmlResponsiveInputFileUpload;
use \htmlResponsiveInputTextarea;
use \htmlButton;
use \htmlStatusMessage;
use \htmlDiv;
use \htmlOutputText;
use \htmlJavaScript;
2018-09-01 11:36:04 +00:00
use \LAMException;
use \htmlLink;
2018-09-15 18:13:57 +00:00
use \htmlResponsiveInputCheckbox;
use \htmlResponsiveSelect;
use \htmlResponsiveInputField;
2018-09-23 18:12:27 +00:00
use \htmlHiddenInput;
2018-09-15 18:13:57 +00:00
use LAM\TYPES\TypeManager;
2018-08-31 18:59:05 +00:00
/*
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
*/
/**
* Multi edit tool that allows LDAP operations on multiple entries.
*
* @author Roland Gruber
* @package tools
*/
/** security functions */
2018-12-23 16:32:17 +00:00
include_once(__DIR__ . "/../../lib/security.inc");
2018-08-31 18:59:05 +00:00
/** access to configuration data */
2018-12-23 16:32:17 +00:00
include_once(__DIR__ . "/../../lib/config.inc");
2018-08-31 18:59:05 +00:00
/** access LDAP server */
2018-12-23 16:32:17 +00:00
include_once(__DIR__ . "/../../lib/ldap.inc");
2018-08-31 18:59:05 +00:00
/** used to print status messages */
2018-12-23 16:32:17 +00:00
include_once(__DIR__ . "/../../lib/status.inc");
2018-08-31 18:59:05 +00:00
/** import class */
2018-12-23 16:32:17 +00:00
include_once(__DIR__ . "/../../lib/import.inc");
2018-08-31 18:59:05 +00:00
// start session
startSecureSession();
enforceUserIsLoggedIn();
// die if no write access
2018-12-23 10:01:29 +00:00
if (!checkIfWriteAccessIsAllowed()) {
die();
}
2018-08-31 18:59:05 +00:00
checkIfToolIsActive('ImportExport');
setlanguage();
if (!empty($_POST)) {
validateSecurityToken();
}
// clean old data
2018-09-01 11:36:04 +00:00
if (isset($_SESSION[Importer::SESSION_KEY_TASKS])) {
unset($_SESSION[Importer::SESSION_KEY_TASKS]);
2018-08-31 18:59:05 +00:00
}
if (isset($_SESSION[Importer::SESSION_KEY_COUNT])) {
unset($_SESSION[Importer::SESSION_KEY_COUNT]);
}
2018-09-15 16:25:53 +00:00
if (isset($_SESSION[Importer::SESSION_KEY_STOP_ON_ERROR])) {
unset($_SESSION[Importer::SESSION_KEY_STOP_ON_ERROR]);
}
2018-08-31 18:59:05 +00:00
2018-12-23 16:32:17 +00:00
include __DIR__ . '/../../lib/adminHeader.inc';
2018-09-23 08:05:06 +00:00
$tabindex = 1;
$activeTab = 0;
if (!empty($_GET['tab']) && ($_GET['tab'] === 'export')) {
$activeTab = 1;
}
2018-08-31 18:59:05 +00:00
?>
<script>
$(function() {
2018-09-23 08:05:06 +00:00
jQuery("#tabs").tabs({
active: <?php echo $activeTab; ?>
});
jQuery(".inputForm").validationEngine();
2018-08-31 18:59:05 +00:00
});
</script>
2018-11-02 16:51:12 +00:00
<div class="smallPaddingContent">
<div id="tabs" class="user-bright">
2018-08-31 18:59:05 +00:00
<ul>
<li id="tab_import">
<a href="#tab-import"><img alt="import" src="../../graphics/import.png"> <?php echo _('Import') ?> </a>
</li>
<li id="tab_export">
<a href="#tab-export"><img alt="export" src="../../graphics/export.png"> <?php echo _('Export') ?> </a>
</li>
</ul>
<div id="tab-import">
<?php
if (isset($_POST['submitImport'])) {
printImportTabProcessing($tabindex);
}
else {
printImportTabContent($tabindex);
}
?>
</div>
<div id="tab-export">
2018-09-15 18:13:57 +00:00
<?php
if (isset($_POST['submitExport'])) {
printExportTabProcessing($tabindex);
}
else {
printExportTabContent($tabindex);
}
?>
2018-08-31 18:59:05 +00:00
</div>
</div>
</div>
<?php
/**
* Prints the content area for the import tab.
*
* @param int $tabindex tabindex
*/
function printImportTabContent(&$tabindex) {
2018-09-23 08:05:06 +00:00
echo "<form class=\"inputForm\" enctype=\"multipart/form-data\" action=\"importexport.php\" method=\"post\">\n";
2018-08-31 18:59:05 +00:00
$container = new htmlResponsiveRow();
$container->add(new htmlTitle(_("Import")), 12);
$sources = array(
2018-10-14 07:44:10 +00:00
_('Text input') => 'text',
2018-08-31 18:59:05 +00:00
_('File') => 'file',
);
$sourceRadio = new htmlResponsiveRadio(_('Source'), 'source', $sources, 'text');
$sourceRadio->setTableRowsToHide(
array(
'file' => array('text'),
'text' => array('file')
)
);
$sourceRadio->setTableRowsToShow(
array(
'text' => array('text'),
'file' => array('file')
)
);
$container->add($sourceRadio, 12);
$container->addVerticalSpacer('1rem');
$container->add(new htmlResponsiveInputFileUpload('file', _('File'), '750'), 12);
$container->add(new htmlResponsiveInputTextarea('text', '', '60', '20', _('LDIF data'), '750'), 12);
2018-09-15 18:13:57 +00:00
$container->add(new htmlResponsiveInputCheckbox('noStop', false, _('Don\'t stop on errors')), 12);
2018-08-31 18:59:05 +00:00
$container->addVerticalSpacer('3rem');
$button = new htmlButton('submitImport', _('Submit'));
$container->add($button, 12, 12, 12, 'text-center');
addSecurityTokenToMetaHTML($container);
parseHtml(null, $container, array(), false, $tabindex, 'user');
2018-12-23 10:01:29 +00:00
echo "</form>\n";
2018-08-31 18:59:05 +00:00
}
/**
* Prints the content area for the import tab during processing state.
*
* @param int $tabindex tabindex
*/
function printImportTabProcessing(&$tabindex) {
2018-09-01 11:36:04 +00:00
try {
checkImportData();
}
catch (LAMException $e) {
2018-08-31 18:59:05 +00:00
$container = new htmlResponsiveRow();
2018-09-01 11:36:04 +00:00
$container->add(new htmlStatusMessage('ERROR', $e->getTitle(), $e->getMessage()), 12);
2018-08-31 18:59:05 +00:00
parseHtml(null, $container, array(), false, $tabindex, 'user');
printImportTabContent($tabindex);
2018-09-15 13:10:50 +00:00
return;
2018-08-31 18:59:05 +00:00
}
2018-09-23 08:05:06 +00:00
echo "<form class=\"inputForm\" enctype=\"multipart/form-data\" action=\"importexport.php\" method=\"post\">\n";
2018-08-31 18:59:05 +00:00
$container = new htmlResponsiveRow();
$container->add(new htmlTitle(_("Import")), 12);
$container->add(new htmlDiv('statusImportInprogress', new htmlOutputText(_('Status') . ': ' . _('in progress'))), 12);
$container->add(new htmlDiv('statusImportDone', new htmlOutputText(_('Status') . ': ' . _('done')), array('hidden')), 12);
2018-09-01 11:36:04 +00:00
$container->add(new htmlDiv('statusImportFailed', new htmlOutputText(_('Status') . ': ' . _('failed')), array('hidden')), 12);
$container->addVerticalSpacer('1rem');
2018-08-31 18:59:05 +00:00
$container->add(new htmlDiv('progressbarImport', new htmlOutputText('')), 12);
2018-09-01 11:36:04 +00:00
$container->addVerticalSpacer('3rem');
$button = new htmlButton('submitImportCancel', _('Cancel'));
$container->add($button, 12, 12, 12, 'text-center');
$newImportButton = new htmlLink(_('New import'), null, null, true);
$container->add($newImportButton, 12, 12, 12, 'text-center hidden newimport');
$container->addVerticalSpacer('3rem');
2018-08-31 18:59:05 +00:00
$container->add(new htmlDiv('importResults', new htmlOutputText('')), 12);
$container->add(new htmlJavaScript(
2018-09-23 18:12:27 +00:00
'window.lam.importexport.startImport(\'' . getSecurityTokenName() . '\', \'' . getSecurityTokenValue() . '\');'
2018-08-31 18:59:05 +00:00
), 12);
addSecurityTokenToMetaHTML($container);
parseHtml(null, $container, array(), false, $tabindex, 'user');
2018-12-23 10:01:29 +00:00
echo "</form>\n";
2018-08-31 18:59:05 +00:00
}
/**
* Checks if the import data is ok.
*
2018-09-01 11:36:04 +00:00
* @throws LAMException error message if not valid
2018-08-31 18:59:05 +00:00
*/
function checkImportData() {
$source = $_POST['source'];
$ldif = '';
if ($source == 'text') {
$ldif = $_POST['text'];
}
else {
$handle = fopen($_FILES['file']['tmp_name'], "r");
$ldif = fread($handle, 100000000);
fclose($handle);
}
if (empty($ldif)) {
2018-09-01 11:36:04 +00:00
throw new LAMException(_('You must either upload a file or provide an import in the text box.'));
2018-08-31 18:59:05 +00:00
}
$lines = preg_split("/\n|\r\n|\r/", $ldif);
2018-09-01 11:36:04 +00:00
$importer = new Importer();
$tasks = $importer->getTasks($lines);
$_SESSION[Importer::SESSION_KEY_TASKS] = $tasks;
$_SESSION[Importer::SESSION_KEY_COUNT] = sizeof($tasks);
2018-09-15 16:25:53 +00:00
$_SESSION[Importer::SESSION_KEY_STOP_ON_ERROR] = (!isset($_POST['noStop']) || ($_POST['noStop'] != 'on'));
2018-08-31 18:59:05 +00:00
}
2018-09-15 18:13:57 +00:00
/**
* Prints the content area for the export tab.
*
* @param int $tabindex tabindex
*/
function printExportTabContent(&$tabindex) {
2018-09-23 08:05:06 +00:00
echo "<form class=\"inputForm\" enctype=\"multipart/form-data\" action=\"importexport.php?tab=export\" method=\"post\">\n";
2018-09-15 18:13:57 +00:00
$container = new htmlResponsiveRow();
$container->add(new htmlTitle(_("Export")), 12);
2018-10-06 17:47:33 +00:00
$baseDnField = new htmlResponsiveInputField(_('Base DN'), 'baseDn', getDefaultBaseDn(), '751', true);
$baseDnField->showDnSelection();
$container->add($baseDnField, 12);
2018-09-15 18:13:57 +00:00
$searchScopes = array(
_('Base (base dn only)') => 'base',
_('One (one level beneath base)') => 'one',
_('Sub (entire subtree)') => 'sub'
);
2018-09-23 18:12:27 +00:00
$searchScopeSelect = new htmlResponsiveSelect('searchScope', $searchScopes, array('sub'), _('Search scope'));
2018-09-15 18:13:57 +00:00
$searchScopeSelect->setHasDescriptiveElements(true);
$searchScopeSelect->setSortElements(false);
$container->add($searchScopeSelect, 12);
2018-10-04 19:07:55 +00:00
$container->add(new htmlResponsiveInputField(_('Search filter'), 'filter', '(objectClass=*)', '752'), 12);
$container->add(new htmlResponsiveInputField(_('Attributes'), 'attributes', '*', '753'), 12);
$container->add(new htmlResponsiveInputCheckbox('includeSystem', false, _('Include system attributes'), '754'), 12);
2018-09-15 18:13:57 +00:00
$container->add(new htmlResponsiveInputCheckbox('saveAsFile', false, _('Save as file')), 12);
$formats = array(
'CSV' => 'csv',
'LDIF' => 'ldif'
);
$formatSelect = new htmlResponsiveSelect('format', $formats, array('ldif'), _('Export format'));
$formatSelect->setHasDescriptiveElements(true);
$formatSelect->setSortElements(false);
$container->add($formatSelect, 12);
$endings = array(
'Windows' => 'windows',
'Unix' => 'unix'
);
$endingsSelect = new htmlResponsiveSelect('ending', $endings, array('unix'), _('End of line'));
$endingsSelect->setHasDescriptiveElements(true);
$endingsSelect->setSortElements(false);
$container->add($endingsSelect, 12);
$container->addVerticalSpacer('3rem');
$button = new htmlButton('submitExport', _('Submit'));
$container->add($button, 12, 12, 12, 'text-center');
addSecurityTokenToMetaHTML($container);
parseHtml(null, $container, array(), false, $tabindex, 'user');
2018-12-23 10:01:29 +00:00
echo "</form>\n";
2018-09-15 18:13:57 +00:00
}
/**
* Returns the default base DN.
*
* @return string base DN
*/
function getDefaultBaseDn() {
$typeManager = new TypeManager();
$baseDn = '';
foreach ($typeManager->getConfiguredTypes() as $type) {
$suffix = $type->getSuffix();
if (empty($baseDn) || (!empty($suffix) && (strlen($suffix) < strlen($baseDn)))) {
$baseDn = $suffix;
}
}
$treeSuffix = $_SESSION['config']->get_Suffix('tree');
if (empty($baseDn) || (!empty($treeSuffix) && (strlen($treeSuffix) < strlen($baseDn)))) {
$baseDn = $treeSuffix;
}
return $baseDn;
}
2018-09-23 08:05:06 +00:00
/**
* Prints the content area for the export tab during processing state.
*
* @param int $tabindex tabindex
*/
function printExportTabProcessing(&$tabindex) {
try {
checkExportData();
}
catch (LAMException $e) {
$container = new htmlResponsiveRow();
$container->add(new htmlStatusMessage('ERROR', $e->getTitle(), $e->getMessage()), 12);
parseHtml(null, $container, array(), false, $tabindex, 'user');
printExportTabContent($tabindex);
return;
}
echo "<form class=\"inputForm\" enctype=\"multipart/form-data\" action=\"importexport.php?tab=export\" method=\"post\">\n";
$container = new htmlResponsiveRow();
$container->add(new htmlTitle(_("Export")), 12);
2018-09-23 18:12:27 +00:00
$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);
2018-09-23 08:05:06 +00:00
$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);
$container->addVerticalSpacer('1rem');
$container->add(new htmlDiv('progressbarExport', new htmlOutputText('')), 12);
$container->addVerticalSpacer('3rem');
$button = new htmlButton('submitExportCancel', _('Cancel'));
$container->add($button, 12, 12, 12, 'text-center');
$newExportButton = new htmlLink(_('New export'), null, null, true);
$container->add($newExportButton, 12, 12, 12, 'text-center hidden newexport');
$container->addVerticalSpacer('3rem');
2018-10-04 19:07:55 +00:00
$exportText = new htmlOutputText('');
$exportText->setPreformatted(true);
$container->add(new htmlDiv('exportResults', $exportText), 12);
2018-09-23 08:05:06 +00:00
$container->add(new htmlJavaScript(
2018-09-23 18:12:27 +00:00
'window.lam.importexport.startExport(\'' . getSecurityTokenName() . '\', \'' . getSecurityTokenValue() . '\');'
2018-09-23 08:05:06 +00:00
), 12);
addSecurityTokenToMetaHTML($container);
parseHtml(null, $container, array(), false, $tabindex, 'user');
2018-12-23 10:01:29 +00:00
echo "</form>\n";
2018-09-23 08:05:06 +00:00
}
/**
* Checks if the export data is ok.
*
* @throws LAMException error message if not valid
*/
function checkExportData() {
if (empty($_POST['baseDn'])) {
throw new LAMException(_('This field is required.'), _('Base DN'));
}
}
2018-12-23 16:32:17 +00:00
include __DIR__ . '/../../lib/adminFooter.inc';