LDAPAccountManager/lam/templates/upload/massBuildAccounts.php

304 lines
10 KiB
PHP
Raw Normal View History

2004-09-21 18:39:11 +00:00
<?php
2017-01-07 17:23:04 +00:00
namespace LAM\UPLOAD;
use \htmlStatusMessage;
use \htmlLink;
2018-04-18 17:08:51 +00:00
use \htmlOutputText;
2017-01-07 17:23:04 +00:00
use \htmlButton;
use \htmlHiddenInput;
2018-04-18 17:08:51 +00:00
use \htmlResponsiveRow;
2004-09-21 18:39:11 +00:00
/*
2009-10-27 18:47:12 +00:00
This code is part of LDAP Account Manager (http://www.ldap-account-manager.org/)
2018-01-07 16:19:28 +00:00
Copyright (C) 2004 - 2018 Roland Gruber
2004-09-21 18:39:11 +00:00
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
*/
/**
* Creates the accounts by parsing the uploaded file.
*
* @author Roland Gruber
* @package tools
*/
2006-03-26 17:51:25 +00:00
/** security functions */
2016-12-07 20:18:06 +00:00
include_once("../../lib/security.inc");
2004-09-21 18:39:11 +00:00
/** access to configuration */
2016-12-07 20:18:06 +00:00
include_once('../../lib/config.inc');
2004-09-21 18:39:11 +00:00
/** status messages */
2016-12-07 20:18:06 +00:00
include_once('../../lib/status.inc');
2004-09-21 18:39:11 +00:00
/** account modules */
2016-12-07 20:18:06 +00:00
include_once('../../lib/modules.inc');
2004-09-21 18:39:11 +00:00
// Start session
2006-03-26 17:51:25 +00:00
startSecureSession();
2017-02-11 16:11:37 +00:00
enforceUserIsLoggedIn();
2004-09-21 18:39:11 +00:00
2012-08-13 17:43:48 +00:00
// check if this tool may be run
checkIfToolIsActive('toolFileUpload');
2007-12-30 13:15:39 +00:00
// die if no write access
if (!checkIfWriteAccessIsAllowed()) die();
2004-09-21 18:39:11 +00:00
// Redirect to startpage if user is not loged in
2014-10-25 19:17:53 +00:00
if (!isLoggedIn()) {
2016-12-07 20:18:06 +00:00
metaRefresh("../login.php");
2004-09-21 18:39:11 +00:00
exit;
}
// Set correct language, codepages, ....
setlanguage();
2015-05-15 19:16:46 +00:00
if (!empty($_POST)) {
validateSecurityToken();
}
2004-09-21 18:39:11 +00:00
// show LDIF if requested
if (isset($_GET['showldif'])) {
//download file
2016-07-14 19:00:47 +00:00
header('Content-Type: text/plain');
header('Content-disposition: attachment; filename=lam.ldif');
$accounts = unserialize(lamDecrypt($_SESSION['mass_accounts']));
2018-01-07 16:19:28 +00:00
foreach ($accounts as $account) {
echo "DN: " . $account['dn'] . "\n";
unset($account['dn']);
$keys = array_keys($account);
2018-01-07 16:33:46 +00:00
foreach ($keys as $key) {
if (strpos($key, 'INFO.') === 0) {
2012-01-15 14:15:56 +00:00
continue;
}
2018-01-07 16:33:46 +00:00
if (is_array($account[$key])) {
foreach ($account[$key] as $value) {
echo $key . ": " . $value . "\n";
2004-09-21 18:39:11 +00:00
}
}
else {
2018-01-07 16:33:46 +00:00
echo $key . ": " . $account[$key] . "\n";
2004-09-21 18:39:11 +00:00
}
}
echo "\n";
}
exit;
}
2018-04-18 17:08:51 +00:00
include '../../lib/adminHeader.inc';
2017-01-07 17:23:04 +00:00
$typeId = htmlspecialchars($_POST['typeId']);
$typeManager = new \LAM\TYPES\TypeManager();
$type = $typeManager->getConfiguredType($typeId);
// check if account type is ok
2017-01-07 17:23:04 +00:00
if ($type->isHidden()) {
logNewMessage(LOG_ERR, 'User tried to access hidden upload: ' . $type->getId());
die();
}
2017-01-07 17:23:04 +00:00
if (!checkIfNewEntriesAreAllowed($type->getId()) || !checkIfWriteAccessIsAllowed($type->getId())) {
logNewMessage(LOG_ERR, 'User tried to access forbidden upload: ' . $type->getId());
die();
}
2013-02-10 19:21:56 +00:00
echo '<form enctype="multipart/form-data" action="masscreate.php" method="post">';
2017-01-07 17:23:04 +00:00
echo '<div class="' . $type->getScope() . '-bright smallPaddingContent">';
2018-04-18 17:08:51 +00:00
$container = new htmlResponsiveRow();
2004-09-21 18:39:11 +00:00
2010-11-28 13:29:05 +00:00
$selectedModules = explode(',', $_POST['selectedModules']);
2004-09-21 18:39:11 +00:00
if ($_FILES['inputfile'] && ($_FILES['inputfile']['size'] > 0)) {
// check if input file is well formated
$data = array(); // input values without first row
$ids = array(); // <column name> => <column number for $data>
2004-09-21 18:39:11 +00:00
// get input fields from modules
2017-05-20 08:04:09 +00:00
$columns = getUploadColumns($type, $selectedModules);
2004-09-21 18:39:11 +00:00
// read input file
$handle = fopen ($_FILES['inputfile']['tmp_name'], "r");
if (($head = fgetcsv($handle, 2000)) !== false ) { // head row
2018-01-07 16:33:46 +00:00
foreach ($head as $i => $headItem) {
$ids[$headItem] = $i;
2004-09-21 18:39:11 +00:00
}
}
while (($line = fgetcsv($handle, 2000)) !== false ) { // account rows
$data[] = $line;
}
2016-07-14 19:00:47 +00:00
2004-09-21 18:39:11 +00:00
$errors = array();
2016-07-14 19:00:47 +00:00
// check if all required columns are present
2004-09-21 18:39:11 +00:00
$checkcolumns = array();
$columns = call_user_func_array('array_merge', $columns);
2018-01-07 16:33:46 +00:00
foreach ($columns as $column) {
if (isset($column['required']) && ($column['required'] === true)) {
if (isset($ids[$column['name']])) $checkcolumns[] = $ids[$column['name']];
else $errors[] = array(_("A required column is missing in your CSV file."), $column['name']);
2004-09-21 18:39:11 +00:00
}
}
2016-07-14 19:00:47 +00:00
// check if all required attributes are given
2004-09-21 18:39:11 +00:00
$invalidColumns = array();
$id_names = array_keys($ids);
2014-09-21 15:06:11 +00:00
foreach ($checkcolumns as $checkcolumn) {
foreach ($data as $dataRow) {
if (empty($dataRow[$checkcolumn])) {
$invalidColumns[] = $id_names[$checkcolumn];
2004-09-21 18:39:11 +00:00
break;
}
}
}
2014-09-21 15:06:11 +00:00
foreach ($data as $dataRow) {
if (empty($dataRow[$ids['dn_rdn']])) {
2004-09-21 18:39:11 +00:00
$invalidColumns[] = 'dn_rdn';
break;
}
}
2018-01-07 16:33:46 +00:00
foreach ($invalidColumns as $invalidColumn) {
$errors[] = array(_("One or more values of the required column \"$invalidColumn\" are missing."), "");
2004-09-21 18:39:11 +00:00
}
2016-07-14 19:00:47 +00:00
// check if values in unique columns are correct
2018-01-07 16:33:46 +00:00
foreach ($columns as $column) {
if (isset($column['unique']) && ($column['unique'] === true) && isset($ids[$column['name']])) {
$colNumber = $ids[$column['name']];
$values_given = array();
2014-09-21 15:06:11 +00:00
foreach ($data as $dataRow) {
$values_given[] = $dataRow[$colNumber];
}
$values_unique = array_unique($values_given);
if (sizeof($values_given) != sizeof($values_unique)) {
$duplicates = array();
foreach ($values_given as $key => $value) {
if (!isset($values_unique[$key])) {
$duplicates[] = htmlspecialchars($value);
}
}
$duplicates = array_values(array_unique($duplicates));
2018-01-07 16:33:46 +00:00
$errors[] = array(_("This column is defined to include unique entries but duplicates were found:") . ' ' . $column['name'], implode(', ', $duplicates));
}
}
}
2016-07-14 19:00:47 +00:00
2004-09-27 19:13:06 +00:00
// if input data is invalid just display error messages (max 50)
if (sizeof($errors) > 0) {
2018-01-07 16:33:46 +00:00
foreach ($errors as $error) {
2018-04-18 17:08:51 +00:00
$container->add(new htmlStatusMessage("ERROR", $error[0], $error[1]), 12);
2013-02-10 19:21:56 +00:00
}
2018-04-18 17:08:51 +00:00
$container->addVerticalSpacer('2rem');
2017-01-07 17:23:04 +00:00
massPrintBackButton($type->getId(), $selectedModules, $container);
2004-09-27 19:13:06 +00:00
}
2016-07-14 19:00:47 +00:00
2004-09-21 18:39:11 +00:00
// let modules build accounts
2004-09-27 19:13:06 +00:00
else {
2018-04-18 17:08:51 +00:00
$accounts = buildUploadAccounts($type, $data, $ids, $selectedModules, $container);
2017-09-18 17:47:27 +00:00
if ($accounts !== false) {
2017-01-07 17:23:04 +00:00
$rdnList = getRDNAttributes($type->getId(), $selectedModules);
$suffix = $type->getSuffix();
2004-09-21 18:39:11 +00:00
// set DN
2014-09-21 15:06:11 +00:00
foreach ($accounts as $i => $account) {
// check against list of possible RDN attributes
2009-11-27 18:49:56 +00:00
if (!in_array($data[$i][$ids['dn_rdn']], $rdnList)) {
2014-09-21 15:06:11 +00:00
$errors[] = array(_('Account %s:') . ' dn_rdn ' . $account[$data[$i][$ids['dn_rdn']]], _("Invalid RDN attribute!"), array($i));
}
2004-09-21 18:39:11 +00:00
else {
2014-09-21 15:06:11 +00:00
$account_dn = $data[$i][$ids['dn_rdn']] . "=" . $account[$data[$i][$ids['dn_rdn']]] . ",";
2009-11-27 18:49:56 +00:00
if ($data[$i][$ids['dn_suffix']] == "") $account_dn = $account_dn . $suffix;
2004-09-21 18:39:11 +00:00
else $account_dn = $account_dn . $data[$i][$ids['dn_suffix']];
2014-09-21 18:09:44 +00:00
$accounts[$i]['dn'] = $account_dn;
2004-09-21 18:39:11 +00:00
}
}
// print errors if DN could not be built
if (sizeof($errors) > 0) {
2018-01-07 16:33:46 +00:00
foreach ($errors as $error) {
2018-04-18 17:08:51 +00:00
$container->add(new htmlStatusMessage("ERROR", $error[0], $error[1], $error[2]), 12);
2013-02-10 19:21:56 +00:00
}
}
else {
// store accounts in session
$_SESSION['mass_accounts'] = lamEncrypt(serialize($accounts));
$_SESSION['mass_errors'] = array();
2004-10-19 18:18:46 +00:00
$_SESSION['mass_failed'] = array();
$_SESSION['mass_postActions'] = array();
$_SESSION['mass_data'] = lamEncrypt(serialize($data));
2004-10-19 18:18:46 +00:00
$_SESSION['mass_ids'] = $ids;
2017-01-07 17:23:04 +00:00
$_SESSION['mass_typeId'] = $type->getId();
$_SESSION['mass_selectedModules'] = $selectedModules;
2012-02-05 10:38:59 +00:00
if (isset($_SESSION['mass_pdf'])) {
unset($_SESSION['mass_pdf']);
}
if (isset($_POST['createPDF']) && ($_POST['createPDF'] == 'on')) {
$_SESSION['mass_pdf']['structure'] = $_POST['pdfStructure'];
2017-10-23 15:58:08 +00:00
$_SESSION['mass_pdf']['font'] = $_POST['pdf_font'];
2012-02-05 10:38:59 +00:00
$_SESSION['mass_pdf']['counter'] = 0;
2016-12-07 20:18:06 +00:00
$_SESSION['mass_pdf']['file'] = '../../tmp/lam_pdf' . getRandomNumber() . '.zip';
2012-02-05 10:38:59 +00:00
}
else {
$_SESSION['mass_pdf']['structure'] = null;
}
// show links for upload and LDIF export
2018-04-18 17:08:51 +00:00
$container->addVerticalSpacer('2rem');
$container->add(new htmlOutputText(_("LAM has checked your input and is now ready to create the accounts.")), 12);
$container->addVerticalSpacer('4rem');
$link = new htmlLink(_("Upload accounts to LDAP"), 'massDoUpload.php', '../../graphics/up.gif', true);
$link->setCSSClasses(array('margin3'));
$container->addLabel($link);
$link = new htmlLink(_("Show LDIF file"), 'massBuildAccounts.php?showldif=true', '../../graphics/edit.png', true);
$link->setCSSClasses(array('margin3'));
$container->addField($link);
$container->addVerticalSpacer('2rem');
massPrintBackButton($type->getId(), $selectedModules, $container);
}
2004-09-21 18:39:11 +00:00
}
2010-11-28 13:29:05 +00:00
else {
2018-04-18 17:08:51 +00:00
$container->addVerticalSpacer('2rem');
2017-01-07 17:23:04 +00:00
massPrintBackButton($type->getId(), $selectedModules, $container);
2010-11-28 13:29:05 +00:00
}
2004-09-21 18:39:11 +00:00
}
}
else {
2018-04-18 17:08:51 +00:00
$container->add(new htmlStatusMessage('ERROR', _('Please provide a file to upload.')), 12);
$container->addVerticalSpacer('2rem');
2017-01-07 17:23:04 +00:00
massPrintBackButton($type->getId(), $selectedModules, $container);
}
2004-09-21 18:39:11 +00:00
2015-05-15 19:16:46 +00:00
addSecurityTokenToMetaHTML($container);
2013-02-10 19:21:56 +00:00
$tabindex = 1;
2017-01-07 17:23:04 +00:00
parseHtml(null, $container, array(), false, $tabindex, $type->getScope());
2013-02-10 19:21:56 +00:00
2010-10-24 13:53:44 +00:00
echo '</div>';
2013-02-10 19:21:56 +00:00
echo '</form>';
2018-04-18 17:08:51 +00:00
include '../../lib/adminFooter.inc';
2004-09-21 18:39:11 +00:00
2010-11-28 13:29:05 +00:00
/**
* Prints a back button to the page where the user enters a file to upload.
*
2017-01-07 17:23:04 +00:00
* @param String $typeId account type (e.g. user)
2010-11-28 13:29:05 +00:00
* @param array $selectedModules selected modules for upload
2018-04-18 17:08:51 +00:00
* @param htmlResponsiveRow $container table container
2010-11-28 13:29:05 +00:00
*/
2018-04-18 17:08:51 +00:00
function massPrintBackButton($typeId, $selectedModules, htmlResponsiveRow &$container) {
2013-02-10 19:21:56 +00:00
$backButton = new htmlButton('submit', _('Back'));
$backButton->setIconClass('backButton');
2018-04-18 17:08:51 +00:00
$container->add($backButton, 12);
$container->add(new htmlHiddenInput('type', $typeId), 12);
2012-02-05 10:38:59 +00:00
$createPDF = 0;
if (isset($_POST['createPDF']) && ($_POST['createPDF'] == 'on')) {
$createPDF = 1;
}
2018-04-18 17:08:51 +00:00
$container->add(new htmlHiddenInput('createPDF', $createPDF), 12);
$container->add(new htmlHiddenInput('pdfStructure', $_POST['pdfStructure']), 12);
2018-01-07 16:33:46 +00:00
foreach ($selectedModules as $selectedModule) {
2018-04-18 17:08:51 +00:00
$container->add(new htmlHiddenInput($typeId . '___' . $selectedModule, 'on'), 12);
2010-11-28 13:29:05 +00:00
}
}
2017-09-16 20:08:57 +00:00
?>