| 1 |
<?php |
| 2 |
namespace LAM\UPLOAD; |
| 3 |
use htmlForm; |
| 4 |
use htmlStatusMessage; |
| 5 |
use htmlOutputText; |
| 6 |
use htmlButton; |
| 7 |
use htmlHiddenInput; |
| 8 |
use htmlResponsiveRow; |
| 9 |
use LAM\PDF\PdfStructurePersistenceManager; |
| 10 |
use LAM\TYPES\TypeManager; |
| 11 |
use LamTemporaryFilesManager; |
| 12 |
use function LAM\PDF\getPdfFonts; |
| 13 |
|
| 14 |
/* |
| 15 |
|
| 16 |
This code is part of LDAP Account Manager (http://www.ldap-account-manager.org/) |
| 17 |
Copyright (C) 2004 - 2026 Roland Gruber |
| 18 |
|
| 19 |
This program is free software; you can redistribute it and/or modify |
| 20 |
it under the terms of the GNU General Public License as published by |
| 21 |
the Free Software Foundation; either version 2 of the License, or |
| 22 |
(at your option) any later version. |
| 23 |
|
| 24 |
This program is distributed in the hope that it will be useful, |
| 25 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 26 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 27 |
GNU General Public License for more details. |
| 28 |
|
| 29 |
You should have received a copy of the GNU General Public License |
| 30 |
along with this program; if not, write to the Free Software |
| 31 |
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 32 |
|
| 33 |
*/ |
| 34 |
|
| 35 |
/** |
| 36 |
* Creates the accounts by parsing the uploaded file. |
| 37 |
* |
| 38 |
* @author Roland Gruber |
| 39 |
* @package tools |
| 40 |
*/ |
| 41 |
|
| 42 |
/** security functions */ |
| 43 |
include_once(__DIR__ . "/../../lib/security.inc"); |
| 44 |
/** access to configuration */ |
| 45 |
include_once(__DIR__ . '/../../lib/config.inc'); |
| 46 |
/** status messages */ |
| 47 |
include_once(__DIR__ . '/../../lib/status.inc'); |
| 48 |
/** account modules */ |
| 49 |
include_once(__DIR__ . '/../../lib/modules.inc'); |
| 50 |
|
| 51 |
|
| 52 |
// Start session |
| 53 |
startSecureSession(); |
| 54 |
enforceUserIsLoggedIn(); |
| 55 |
|
| 56 |
// check if this tool may be run |
| 57 |
checkIfToolIsActive('toolFileUpload'); |
| 58 |
|
| 59 |
// die if no write access |
| 60 |
if (!checkIfWriteAccessIsAllowed()) { |
| 61 |
die(); |
| 62 |
} |
| 63 |
|
| 64 |
// Redirect to startpage if user is not logged in |
| 65 |
if (!isLoggedIn()) { |
| 66 |
metaRefresh("../login.php"); |
| 67 |
exit; |
| 68 |
} |
| 69 |
|
| 70 |
// Set correct language, codepages, .... |
| 71 |
setlanguage(); |
| 72 |
|
| 73 |
if (!empty($_POST)) { |
| 74 |
validateSecurityToken(); |
| 75 |
} |
| 76 |
|
| 77 |
// show LDIF if requested |
| 78 |
if (isset($_POST['showldif'])) { |
| 79 |
//download file |
| 80 |
header('Content-Type: text/plain'); |
| 81 |
header('Content-disposition: attachment; filename=lam.ldif'); |
| 82 |
$accounts = json_decode(lamDecrypt($_SESSION['mass_accounts']), true); |
| 83 |
if ($accounts === false) { |
| 84 |
exit; |
| 85 |
} |
| 86 |
foreach ($accounts as $account) { |
| 87 |
echo "DN: " . $account['dn'] . "\n"; |
| 88 |
unset($account['dn']); |
| 89 |
$keys = array_keys($account); |
| 90 |
foreach ($keys as $key) { |
| 91 |
if (str_starts_with($key, 'INFO.')) { |
| 92 |
continue; |
| 93 |
} |
| 94 |
if (is_array($account[$key])) { |
| 95 |
foreach ($account[$key] as $value) { |
| 96 |
echo $key . ": " . $value . "\n"; |
| 97 |
} |
| 98 |
} |
| 99 |
else { |
| 100 |
echo $key . ": " . $account[$key] . "\n"; |
| 101 |
} |
| 102 |
} |
| 103 |
echo "\n"; |
| 104 |
} |
| 105 |
exit; |
| 106 |
} |
| 107 |
elseif (isset($_POST['upload'])) { |
| 108 |
metaRefresh('massDoUpload.php'); |
| 109 |
exit(); |
| 110 |
} |
| 111 |
|
| 112 |
include __DIR__ . '/../../lib/adminHeader.inc'; |
| 113 |
$typeId = htmlspecialchars($_POST['typeId']); |
| 114 |
$typeManager = new TypeManager(); |
| 115 |
$type = $typeManager->getConfiguredType($typeId); |
| 116 |
if ($type === null) { |
| 117 |
logNewMessage(LOG_ERR, 'User tried to access invalid upload type: ' . $typeId); |
| 118 |
die(); |
| 119 |
} |
| 120 |
|
| 121 |
// check if account type is ok |
| 122 |
if ($type->isHidden()) { |
| 123 |
logNewMessage(LOG_ERR, 'User tried to access hidden upload: ' . $type->getId()); |
| 124 |
die(); |
| 125 |
} |
| 126 |
if (!checkIfNewEntriesAreAllowed($type->getId()) || !checkIfWriteAccessIsAllowed($type->getId())) { |
| 127 |
logNewMessage(LOG_ERR, 'User tried to access forbidden upload: ' . $type->getId()); |
| 128 |
die(); |
| 129 |
} |
| 130 |
|
| 131 |
echo '<div class="smallPaddingContent">'; |
| 132 |
$container = new htmlResponsiveRow(); |
| 133 |
|
| 134 |
$selectedModules = explode(',', $_POST['selectedModules']); |
| 135 |
if ($_FILES['inputfile'] && ($_FILES['inputfile']['size'] > 0)) { |
| 136 |
// check if input file is well formatted |
| 137 |
$data = []; // input values without first row |
| 138 |
$ids = []; // <column name> => <column number for $data> |
| 139 |
// get input fields from modules |
| 140 |
$uploadColumns = getUploadColumns($type, $selectedModules); |
| 141 |
// read input file |
| 142 |
logNewMessage(LOG_DEBUG, 'Reading CSV file'); |
| 143 |
$handle = fopen($_FILES['inputfile']['tmp_name'], "r"); |
| 144 |
if ($handle !== false) { |
| 145 |
$head = fgetcsv($handle, 2000, escape: ''); |
| 146 |
if ($head !== false ) { // head row |
| 147 |
foreach ($head as $i => $headItem) { |
| 148 |
$ids[$headItem] = $i; |
| 149 |
} |
| 150 |
} |
| 151 |
while (($line = fgetcsv($handle, 2000, escape: '')) !== false ) { // account rows |
| 152 |
$data[] = $line; |
| 153 |
} |
| 154 |
fclose($handle); |
| 155 |
} |
| 156 |
|
| 157 |
$errors = []; |
| 158 |
|
| 159 |
// check if all required columns are present |
| 160 |
$checkcolumns = []; |
| 161 |
$columns = []; |
| 162 |
foreach ($uploadColumns as $uploadColumn) { |
| 163 |
$columns = array_merge($columns, $uploadColumn); |
| 164 |
} |
| 165 |
foreach ($columns as $column) { |
| 166 |
if (isset($column['required']) && ($column['required'] === true)) { |
| 167 |
if (isset($ids[$column['name']])) { |
| 168 |
$checkcolumns[] = $ids[$column['name']]; |
| 169 |
} |
| 170 |
else { |
| 171 |
$errors[] = [_("A required column is missing in your CSV file."), $column['name']]; |
| 172 |
} |
| 173 |
} |
| 174 |
} |
| 175 |
|
| 176 |
// check if all required attributes are given |
| 177 |
$invalidColumns = []; |
| 178 |
$id_names = array_keys($ids); |
| 179 |
foreach ($checkcolumns as $checkcolumn) { |
| 180 |
foreach ($data as $dataRow) { |
| 181 |
if (empty($dataRow[$checkcolumn])) { |
| 182 |
$invalidColumns[] = $id_names[$checkcolumn]; |
| 183 |
break; |
| 184 |
} |
| 185 |
} |
| 186 |
} |
| 187 |
foreach ($data as $dataRow) { |
| 188 |
if (empty($dataRow[$ids['dn_rdn']])) { |
| 189 |
$invalidColumns[] = 'dn_rdn'; |
| 190 |
break; |
| 191 |
} |
| 192 |
} |
| 193 |
foreach ($invalidColumns as $invalidColumn) { |
| 194 |
$errors[] = [_("One or more values of the required column \"$invalidColumn\" are missing."), ""]; |
| 195 |
} |
| 196 |
|
| 197 |
// check if values in unique columns are correct |
| 198 |
foreach ($columns as $column) { |
| 199 |
if (isset($column['unique']) && ($column['unique'] === true) && isset($ids[$column['name']])) { |
| 200 |
$colNumber = $ids[$column['name']]; |
| 201 |
$values_given = []; |
| 202 |
foreach ($data as $dataRow) { |
| 203 |
$values_given[] = $dataRow[$colNumber]; |
| 204 |
} |
| 205 |
$values_unique = array_unique($values_given); |
| 206 |
if (count($values_given) !== count($values_unique)) { |
| 207 |
$duplicates = []; |
| 208 |
foreach ($values_given as $key => $value) { |
| 209 |
if (!isset($values_unique[$key]) && ($value !== null) && ($value !== '')) { |
| 210 |
$duplicates[] = htmlspecialchars($value); |
| 211 |
} |
| 212 |
} |
| 213 |
if (!empty($duplicates)) { |
| 214 |
$duplicates = array_values(array_unique($duplicates)); |
| 215 |
$errors[] = [_("This column is defined to include unique entries but duplicates were found:") . ' ' . $column['name'], implode(', ', $duplicates)]; |
| 216 |
} |
| 217 |
} |
| 218 |
} |
| 219 |
} |
| 220 |
logNewMessage(LOG_DEBUG, 'End of generic checks'); |
| 221 |
|
| 222 |
// if input data is invalid just display error messages (max 50) |
| 223 |
if ($errors !== []) { |
| 224 |
foreach ($errors as $error) { |
| 225 |
$container->add(new htmlStatusMessage("ERROR", $error[0], $error[1])); |
| 226 |
} |
| 227 |
$container->addVerticalSpacer('2rem'); |
| 228 |
massPrintBackButton($type->getId(), $selectedModules, $container); |
| 229 |
} |
| 230 |
|
| 231 |
// let modules build accounts |
| 232 |
else { |
| 233 |
$accounts = buildUploadAccounts($type, $data, $ids, $selectedModules, $container); |
| 234 |
if ($accounts !== false) { |
| 235 |
$rdnList = getRDNAttributes($type->getId(), $selectedModules); |
| 236 |
$suffix = $type->getSuffix(); |
| 237 |
foreach ($accounts as $i => $account) { |
| 238 |
// set DN |
| 239 |
// check against list of possible RDN attributes |
| 240 |
if (!isset($account[$data[$i][$ids['dn_rdn']]])) { |
| 241 |
$errors[] = [_('Account %s:') . ' dn_rdn', _("Invalid RDN attribute!"), [$i]]; |
| 242 |
} |
| 243 |
elseif (!in_array($data[$i][$ids['dn_rdn']], $rdnList)) { |
| 244 |
$errors[] = [_('Account %s:') . ' dn_rdn ' . $account[$data[$i][$ids['dn_rdn']]], _("Invalid RDN attribute!"), [$i]]; |
| 245 |
} |
| 246 |
else { |
| 247 |
$rdnValue = $account[$data[$i][$ids['dn_rdn']]]; |
| 248 |
if (is_array($rdnValue)) { |
| 249 |
$rdnValue = $rdnValue[0]; |
| 250 |
} |
| 251 |
$account_dn = $data[$i][$ids['dn_rdn']] . "=" . ldap_escape($rdnValue, '', LDAP_ESCAPE_DN) . ","; |
| 252 |
$account_dn = ($data[$i][$ids['dn_suffix']] == "") ? $account_dn . $suffix : $account_dn . $data[$i][$ids['dn_suffix']]; |
| 253 |
$accounts[$i]['dn'] = $account_dn; |
| 254 |
} |
| 255 |
// set overwrite |
| 256 |
if (isset($ids['overwrite']) && ($data[$i][$ids['overwrite']] === 'true')) { |
| 257 |
$accounts[$i]['INFO.overwrite'] = 'true'; |
| 258 |
} |
| 259 |
} |
| 260 |
// print errors if DN could not be built |
| 261 |
if ($errors !== []) { |
| 262 |
foreach ($errors as $error) { |
| 263 |
$container->add(new htmlStatusMessage("ERROR", $error[0], $error[1], $error[2])); |
| 264 |
} |
| 265 |
} |
| 266 |
else { |
| 267 |
// store accounts in session |
| 268 |
$_SESSION['mass_accounts'] = lamEncrypt(json_encode($accounts)); |
| 269 |
$_SESSION['mass_errors'] = []; |
| 270 |
$_SESSION['mass_failed'] = []; |
| 271 |
$_SESSION['mass_postActions'] = []; |
| 272 |
$_SESSION['mass_data'] = lamEncrypt(json_encode($data)); |
| 273 |
$_SESSION['mass_ids'] = $ids; |
| 274 |
$_SESSION['mass_typeId'] = $type->getId(); |
| 275 |
$_SESSION['mass_selectedModules'] = $selectedModules; |
| 276 |
if (isset($_SESSION['mass_pdf'])) { |
| 277 |
unset($_SESSION['mass_pdf']); |
| 278 |
} |
| 279 |
if (isset($_POST['createPDF']) && ($_POST['createPDF'] == 'on')) { |
| 280 |
$pdfStructurePersistenceManager = new PdfStructurePersistenceManager(); |
| 281 |
$pdfStructures = $pdfStructurePersistenceManager->getPDFStructures($_SESSION['config']->getName(), $type->getId()); |
| 282 |
$pdfStructureName = (string) $_POST['pdfStructure']; |
| 283 |
if (!in_array($pdfStructureName, $pdfStructures)) { |
| 284 |
logNewMessage(LOG_ERR, 'Invalid PDF structure: ' . $pdfStructureName); |
| 285 |
die(); |
| 286 |
} |
| 287 |
$_SESSION['mass_pdf']['structure'] = $pdfStructureName; |
| 288 |
$fontName = (string) $_POST['pdf_font']; |
| 289 |
$fonts = getPdfFonts(); |
| 290 |
if (!in_array($fontName, $fonts)) { |
| 291 |
logNewMessage(LOG_ERR, 'Invalid font name: ' . $fontName); |
| 292 |
die(); |
| 293 |
} |
| 294 |
$_SESSION['mass_pdf']['font'] = $fontName; |
| 295 |
$_SESSION['mass_pdf']['counter'] = 0; |
| 296 |
$tempFilesManager = new LamTemporaryFilesManager(); |
| 297 |
$_SESSION['mass_pdf']['file'] = $tempFilesManager->registerTemporaryFile('.zip'); |
| 298 |
} |
| 299 |
else { |
| 300 |
$_SESSION['mass_pdf']['structure'] = null; |
| 301 |
} |
| 302 |
// show links for upload and LDIF export |
| 303 |
$container->addVerticalSpacer('2rem'); |
| 304 |
$container->add(new htmlOutputText(_("LAM has checked your input and is now ready to create the accounts."))); |
| 305 |
$container->addVerticalSpacer('3rem'); |
| 306 |
$formRow = new htmlResponsiveRow(); |
| 307 |
$uploadButton = new htmlButton('upload', _("Upload accounts to LDAP")); |
| 308 |
$uploadButton->setCSSClasses(['lam-primary']); |
| 309 |
$formRow->addLabel($uploadButton); |
| 310 |
$ldifButton = new htmlButton('showldif', _("Show LDIF file")); |
| 311 |
$ldifButton->setCSSClasses(['lam-secondary']); |
| 312 |
$formRow->addField($ldifButton); |
| 313 |
addSecurityTokenToMetaHTML($formRow); |
| 314 |
$container->add(new htmlForm('uploadform', 'massBuildAccounts.php', $formRow)); |
| 315 |
|
| 316 |
$container->addVerticalSpacer('2rem'); |
| 317 |
massPrintBackButton($type->getId(), $selectedModules, $container); |
| 318 |
} |
| 319 |
} |
| 320 |
else { |
| 321 |
$container->addVerticalSpacer('2rem'); |
| 322 |
massPrintBackButton($type->getId(), $selectedModules, $container); |
| 323 |
} |
| 324 |
} |
| 325 |
} |
| 326 |
else { |
| 327 |
$container->add(new htmlStatusMessage('ERROR', _('Please provide a file to upload.'))); |
| 328 |
$container->addVerticalSpacer('2rem'); |
| 329 |
massPrintBackButton($type->getId(), $selectedModules, $container); |
| 330 |
} |
| 331 |
|
| 332 |
parseHtml(null, $container, [], false, $type->getScope()); |
| 333 |
|
| 334 |
echo '</div>'; |
| 335 |
include __DIR__ . '/../../lib/adminFooter.inc'; |
| 336 |
|
| 337 |
/** |
| 338 |
* Prints a back button to the page where the user enters a file to upload. |
| 339 |
* |
| 340 |
* @param string $typeId account type (e.g. user) |
| 341 |
* @param string[] $selectedModules selected modules for upload |
| 342 |
* @param htmlResponsiveRow $container table container |
| 343 |
*/ |
| 344 |
function massPrintBackButton(string $typeId, array $selectedModules, htmlResponsiveRow $container): void { |
| 345 |
$row = new htmlResponsiveRow(); |
| 346 |
$backButton = new htmlButton('submit', _('Back')); |
| 347 |
$row->add($backButton); |
| 348 |
$row->add(new htmlHiddenInput('type', $typeId)); |
| 349 |
$createPDF = '0'; |
| 350 |
if (isset($_POST['createPDF']) && ($_POST['createPDF'] == 'on')) { |
| 351 |
$createPDF = '1'; |
| 352 |
} |
| 353 |
$row->add(new htmlHiddenInput('createPDF', $createPDF)); |
| 354 |
$row->add(new htmlHiddenInput('pdfStructure', $_POST['pdfStructure'])); |
| 355 |
foreach ($selectedModules as $selectedModule) { |
| 356 |
$row->add(new htmlHiddenInput($typeId . '___' . $selectedModule, 'on')); |
| 357 |
} |
| 358 |
addSecurityTokenToMetaHTML($row); |
| 359 |
$container->add(new htmlForm('backform', 'masscreate.php', $row)); |
| 360 |
} |