\n";
$container = new htmlResponsiveRow();
$container->add(new htmlTitle(_("Import")));
$sources = [
_('Text input') => 'text',
_('File') => 'file',
];
$sourceRadio = new htmlResponsiveRadio(_('Source'), 'source', $sources, 'text');
$sourceRadio->setTableRowsToHide(
[
'file' => ['text'],
'text' => ['file']
]
);
$sourceRadio->setTableRowsToShow(
[
'text' => ['text'],
'file' => ['file']
]
);
$container->add($sourceRadio);
$container->addVerticalSpacer('1rem');
$container->add(new htmlResponsiveInputFileUpload('file', _('File'), '750'));
$ldifValue = $_POST['text'] ?? '';
$container->add(new htmlResponsiveInputTextarea('text', $ldifValue, 60, 20, _('LDIF data'), '750'));
$container->add(new htmlResponsiveInputCheckbox('noStop', false, _('Don\'t stop on errors')));
$container->addVerticalSpacer('3rem');
$button = new htmlButton('submitImport', _('Submit'));
$container->add($button, 12, 12, 12, 'text-center');
addSecurityTokenToMetaHTML($container);
parseHtml(null, $container, [], false, 'user');
echo "\n";
}
/**
* Prints the content area for the import tab during processing state.
*/
function printImportTabProcessing(): void {
try {
checkImportData();
}
catch (LAMException $e) {
$container = new htmlResponsiveRow();
$container->add(new htmlStatusMessage('ERROR', $e->getTitle(), $e->getMessage()));
parseHtml(null, $container, [], false, 'user');
printImportTabContent();
return;
}
echo "\n";
}
/**
* Checks if the import data is ok.
*
* @throws LAMException error message if not valid
*/
function checkImportData(): void {
$source = $_POST['source'];
if ($source == 'text') {
$ldif = $_POST['text'];
}
else {
$handle = fopen($_FILES['file']['tmp_name'], "r");
if ($handle === false) {
throw new LAMException(_('Unable to create temporary file.'));
}
$ldif = fread($handle, 100_000_000);
if ($ldif === false) {
throw new LAMException(_('Unable to create temporary file.'));
}
fclose($handle);
}
if (empty($ldif)) {
throw new LAMException(_('You must either upload a file or provide an import in the text box.'));
}
$lines = preg_split("/\n|\r\n|\r/", $ldif);
if ($lines === false) {
throw new LAMException(_('You must either upload a file or provide an import in the text box.'));
}
$importer = new Importer();
$tasks = $importer->getTasks($lines);
$_SESSION[Importer::SESSION_KEY_TASKS] = $tasks;
$_SESSION[Importer::SESSION_KEY_COUNT] = count($tasks);
$_SESSION[Importer::SESSION_KEY_STOP_ON_ERROR] = (!isset($_POST['noStop']) || ($_POST['noStop'] != 'on'));
}
/**
* Prints the content area for the export tab.
*/
function printExportTabContent(): void {
echo "\n";
}
/**
* Returns the default base DN.
*
* @return string base DN
*/
function getDefaultBaseDn(): string {
$typeManager = new TypeManager();
$baseDn = '';
foreach ($typeManager->getConfiguredTypes() as $type) {
$suffix = $type->getSuffix();
if (empty($baseDn) || (!empty($suffix) && (strlen($suffix) < strlen($baseDn)))) {
$baseDn = $suffix;
}
}
if ($_SESSION['config']->isToolActive('TreeViewTool')) {
$treeSuffixes = TreeViewTool::getRootDns();
if (empty($baseDn) || (!empty($treeSuffixes) && (strlen($treeSuffixes[0]) < strlen($baseDn)))) {
$baseDn = $treeSuffixes[0];
}
}
return $baseDn;
}
/**
* Checks if the given DN is valid for exporting.
*
* @param string $dn DN
* @return bool valid
*/
function isValidExportDn(string $dn): bool {
$dn = strtolower($dn);
$typeManager = new TypeManager();
foreach ($typeManager->getConfiguredTypes() as $type) {
$suffix = strtolower($type->getSuffix());
if (str_ends_with($dn, $suffix)) {
return true;
}
}
if ($_SESSION['config']->isToolActive('TreeViewTool')) {
$treeSuffixes = TreeViewTool::getRootDns();
foreach ($treeSuffixes as $treeSuffix) {
$treeSuffix = strtolower($treeSuffix);
if (str_ends_with($dn, $treeSuffix)) {
return true;
}
}
}
return false;
}
/**
* Prints the content area for the export tab during processing state.
*/
function printExportTabProcessing(): void {
try {
checkExportData();
}
catch (LAMException $e) {
$container = new htmlResponsiveRow();
$container->add(new htmlStatusMessage('ERROR', $e->getTitle(), $e->getMessage()));
parseHtml(null, $container, [], false, 'user');
printExportTabContent();
return;
}
echo "\n";
}
/**
* Checks if the export data is ok.
*
* @throws LAMException error message if not valid
*/
function checkExportData(): void {
if (empty($_POST['baseDn'])) {
throw new LAMException(_('This field is required.'), _('Base DN'));
}
}
include __DIR__ . '/../../lib/adminFooter.inc';