Debian Codegraph @

templates/upload/masscreate.php

raw
1 <?php
2
3 namespace LAM\UPLOAD;
4
5 use htmlInputCheckbox;
6 use htmlLabel;
7 use htmlResponsiveTable;
8 use htmlOutputText;
9 use htmlGroup;
10 use htmlImage;
11 use htmlResponsiveInputCheckbox;
12 use htmlDiv;
13 use htmlHiddenInput;
14 use htmlButton;
15 use htmlTitle;
16 use htmlResponsiveInputFileUpload;
17 use htmlLink;
18 use htmlSubTitle;
19 use htmlHelpLink;
20 use htmlResponsiveRow;
21 use htmlResponsiveSelect;
22 use htmlSpacer;
23 use LAM\PDF\PdfStructurePersistenceManager;
24 use LAM\TYPES\ConfiguredType;
25 use LAM\TYPES\TypeManager;
26 use moduleCache;
27 use function LAM\PDF\getPdfFonts;
28 use function LAM\TYPES\getScopeFromTypeId;
29
30 /*
31
32 This code is part of LDAP Account Manager (http://www.ldap-account-manager.org/)
33 Copyright (C) 2004 - 2026 Roland Gruber
34
35 This program is free software; you can redistribute it and/or modify
36 it under the terms of the GNU General Public License as published by
37 the Free Software Foundation; either version 2 of the License, or
38 (at your option) any later version.
39
40 This program is distributed in the hope that it will be useful,
41 but WITHOUT ANY WARRANTY; without even the implied warranty of
42 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
43 GNU General Public License for more details.
44
45 You should have received a copy of the GNU General Public License
46 along with this program; if not, write to the Free Software
47 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
48
49 */
50
51 /**
52 * Start page of file upload
53 *
54 * @author Roland Gruber
55 * @package tools
56 */
57
58 /** security functions */
59 include_once(__DIR__ . "/../../lib/security.inc");
60 /** access to configuration */
61 include_once(__DIR__ . '/../../lib/config.inc');
62 /** status messages */
63 include_once(__DIR__ . '/../../lib/status.inc');
64 /** account modules */
65 include_once(__DIR__ . '/../../lib/modules.inc');
66 /** Used to get PDF information. */
67 include_once(__DIR__ . '/../../lib/pdfstruct.inc');
68 /** upload functions */
69 include_once(__DIR__ . '/../../lib/upload.inc');
70
71 // Start session
72 startSecureSession();
73 enforceUserIsLoggedIn();
74
75 // check if this tool may be run
76 checkIfToolIsActive('toolFileUpload');
77
78 // die if no write access
79 if (!checkIfWriteAccessIsAllowed()) {
80 die();
81 }
82
83 checkIfToolIsActive('toolFileUpload');
84
85 // Redirect to startpage if user is not logged in
86 if (!isLoggedIn()) {
87 metaRefresh("../login.php");
88 exit;
89 }
90
91 // Set correct language, codepages, ....
92 setlanguage();
93
94 if (!empty($_POST)) {
95 validateSecurityToken();
96 }
97
98 // show CSV if requested
99 if (isset($_GET['getCSV'])) {
100 //download file
101 header('Content-Type: application/msexcel');
102 header('Content-disposition: attachment; filename=lam.csv');
103 echo $_SESSION['mass_csv'];
104 exit;
105 }
106
107 Uploader::cleanSession();
108
109 include __DIR__ . '/../../lib/adminHeader.inc';
110
111 // get possible types and remove those which do not support file upload
112 $typeManager = new TypeManager();
113 $types = $typeManager->getConfiguredTypes();
114 $count = count($types);
115 for ($i = 0; $i < $count; $i++) {
116 $myType = $types[$i];
117 if (!$myType->getBaseType()->supportsFileUpload() || $myType->isHidden()
118 || !checkIfNewEntriesAreAllowed($myType->getId()) || !checkIfWriteAccessIsAllowed($myType->getId())) {
119 unset($types[$i]);
120 }
121 }
122 $types = array_values($types);
123
124 // check if account specific page should be shown
125 if (isset($_POST['type'])) {
126 // get selected type
127 $typeId = htmlspecialchars((string) $_POST['type']);
128 $type = $typeManager->getConfiguredType($typeId);
129 if ($type === null) {
130 logNewMessage(LOG_ERR, 'Invalid type: ' . $typeId);
131 die();
132 }
133 // get selected modules
134 $selectedModules = [];
135 $checkedBoxes = array_keys($_POST, 'on');
136 foreach ($checkedBoxes as $checkedBox) {
137 if (str_starts_with($checkedBox, $typeId . '___')) {
138 $selectedModules[] = substr($checkedBox, strlen($typeId) + strlen('___'));
139 }
140 }
141 $deps = getModulesDependencies($type->getScope());
142 $depErrors = check_module_depends($selectedModules, $deps);
143 if (is_array($depErrors) && ($depErrors !== [])) {
144 foreach ($depErrors as $depError) {
145 StatusMessage('ERROR', _("Unsolved dependency:") . ' ' .
146 getModuleAlias($depError[0], $type->getScope()) . " (" .
147 getModuleAlias($depError[1], $type->getScope()) . ")");
148 }
149 }
150 else {
151 showMainPage($type, $selectedModules);
152 exit;
153 }
154 }
155
156 // show start page
157 $divClass = 'user';
158 if (isset($_REQUEST['type'])) {
159 $divClass = htmlspecialchars(getScopeFromTypeId($_REQUEST['type']));
160 }
161 echo '<div class="smallPaddingContent">';
162 echo "<form enctype=\"multipart/form-data\" action=\"masscreate.php\" method=\"post\">\n";
163
164 $row = new htmlResponsiveRow();
165 $row->add(new htmlTitle(_("Account creation via file upload")));
166 $row->add(new htmlOutputText(_("Here you can create multiple accounts by providing a CSV file.")));
167 $row->addVerticalSpacer('4rem');
168
169 // account type
170 $typeList = [];
171 foreach ($types as $type) {
172 $typeList[$type->getAlias()] = $type->getId();
173 }
174 $selectedType = null;
175 if (isset($_REQUEST['type'])) {
176 $selectedType = $_REQUEST['type'];
177 }
178 elseif (!empty($types)) {
179 $selectedType = $types[0]->getId();
180 }
181 $typeSelect = new htmlResponsiveSelect('type', $typeList, [$selectedType], _("Account type"));
182 $typeSelect->setHasDescriptiveElements(true);
183 $typeSelect->setOnchangeEvent('window.lam.upload.changeVisibleModules(this);');
184 $row->add($typeSelect);
185 $row->addVerticalSpacer('1rem');
186
187 $row->add(new htmlSubTitle(_('Selected modules')));
188
189 // module selection
190 foreach ($types as $type) {
191 $divClasses = ['typeOptions'];
192 if ($selectedType != $type->getId()) {
193 $divClasses[] = 'hidden';
194 }
195 $innerRow = new htmlResponsiveRow();
196 $modules = $_SESSION['config']->get_AccountModules($type->getId());
197 foreach ($modules as $moduleName) {
198 $moduleGroup = new htmlGroup();
199 $module = moduleCache::getModule($moduleName, $type->getScope());
200 if ($module === null) {
201 continue;
202 }
203 $iconImage = $module->getIcon();
204 if (!(str_starts_with($iconImage, 'http')) && !(str_starts_with($iconImage, '/'))) {
205 $iconImage = '../../graphics/' . $iconImage;
206 }
207 $image = new htmlImage($iconImage, '32px', '32px');
208 $image->setCSSClasses(['margin3']);
209 $moduleGroup->addElement($image);
210 $enabled = true;
211 if (is_base_module($moduleName, $type->getScope())) {
212 $enabled = false;
213 }
214 $checked = true;
215 if (isset($_POST['submit']) && !isset($_POST[$type->getId() . '___' . $moduleName])) {
216 $checked = false;
217 }
218 $checkbox = new htmlInputCheckbox($type->getId() . '___' . $moduleName, $checked);
219 $checkbox->setIsEnabled($enabled);
220 if ($enabled) {
221 $moduleGroup->addElement($checkbox);
222 }
223 else {
224 $boxGroup = new htmlGroup();
225 $boxGroup->addElement($checkbox);
226 // add hidden field to fake disabled checkbox value
227 $boxGroup->addElement(new htmlHiddenInput($type->getId() . '___' . $moduleName, 'on'));
228 $moduleGroup->addElement($boxGroup);
229 }
230 $moduleGroup->addElement(new htmlLabel($type->getId() . '___' . $moduleName, $module->get_alias()));
231 $innerRow->add($moduleGroup, 12, 6, 4);
232 }
233 $moduleCount = count($modules);
234 if ($moduleCount % 3 === 2) {
235 $innerRow->add(new htmlOutputText('&nbsp;', false), 0, 0, 4);
236 }
237 if ($moduleCount % 3 === 1) {
238 $innerRow->add(new htmlOutputText('&nbsp;', false), 0, 0, 4);
239 }
240 if ($moduleCount % 2 === 1) {
241 $innerRow->add(new htmlOutputText('&nbsp;', false), 0, 6, 0);
242 }
243 $typeDiv = new htmlDiv($type->getId(), $innerRow);
244 $typeDiv->setCSSClasses($divClasses);
245 $row->add($typeDiv);
246 }
247
248 // ok button
249 $row->addVerticalSpacer('3rem');
250 if (!empty($types)) {
251 $okButton = new htmlButton('submit', _('Ok'));
252 $okButton->setCSSClasses(['lam-primary']);
253 $row->add($okButton);
254 }
255
256 addSecurityTokenToMetaHTML($row);
257 parseHtml(null, $row, [], false, 'user');
258
259 echo "</form>\n";
260
261 echo '</div>';
262 include __DIR__ . '/../../lib/adminFooter.inc';
263
264 /**
265 * Displays the account type specific main page of the upload.
266 *
267 * @param ConfiguredType $type account type
268 * @param string[] $selectedModules list of selected account modules
269 */
270 function showMainPage(ConfiguredType $type, array $selectedModules): void {
271 $scope = $type->getScope();
272 echo '<div class="smallPaddingContent">';
273 // get input fields from modules
274 $columns = getUploadColumns($type, $selectedModules);
275 $modules = array_keys($columns);
276
277 echo "<form enctype=\"multipart/form-data\" action=\"massBuildAccounts.php\" method=\"post\">\n";
278 $row = new htmlResponsiveRow();
279 $row->setCSSClasses(['maxrow']);
280
281 // title
282 $row->add(new htmlTitle(_("File upload")));
283
284 // instructions
285 $row->add(new htmlOutputText(_("Please provide a CSV formatted file with your account data. The cells in the first row must be filled with the column identifiers. The following rows represent one account for each row.")));
286 $row->add(new htmlOutputText(_("Check your input carefully. LAM will only do some basic checks on the upload data.")));
287 $row->addVerticalSpacer('1rem');
288 $row->add(new htmlOutputText(_("Hint: Format all cells as text in your spreadsheet program and turn off auto correction.")));
289 $row->addVerticalSpacer('1rem');
290
291 // upload elements
292 $row->addLabel(new htmlOutputText(_("Download sample CSV file")));
293 $saveLink = new htmlLink('', 'masscreate.php?getCSV=1', '../../graphics/save.svg');
294 $saveLink->setCSSClasses(['icon']);
295 $row->addField($saveLink);
296 $row->addVerticalSpacer('3rem');
297 $row->add(new htmlResponsiveInputFileUpload('inputfile', _("CSV file"), null, true));
298 $row->add(new htmlHiddenInput('typeId', $type->getId()));
299 $row->add(new htmlHiddenInput('selectedModules', implode(',', $selectedModules)));
300
301 // PDF
302 $pdfStructurePersistenceManager = new PdfStructurePersistenceManager();
303 $pdfStructures = $pdfStructurePersistenceManager->getPDFStructures($_SESSION['config']->getName(), $type->getId());
304 if (!empty($pdfStructures)) {
305 $createPDF = false;
306 if (isset($_POST['createPDF']) && ($_POST['createPDF'] === '1')) {
307 $createPDF = true;
308 }
309 $pdfCheckbox = new htmlResponsiveInputCheckbox('createPDF', $createPDF, _('Create PDF files'));
310 $pdfCheckbox->setTableRowsToShow(['pdfStructure', 'pdf_font']);
311 $row->add($pdfCheckbox);
312 $pdfSelected = [];
313 if (isset($_POST['pdfStructure'])) {
314 $pdfSelected = [$_POST['pdfStructure']];
315 }
316 elseif (in_array('default', $pdfStructures)) {
317 $pdfSelected = ['default'];
318 }
319 $row->add(new htmlResponsiveSelect('pdfStructure', $pdfStructures, $pdfSelected, _('PDF structure')));
320 $fonts = getPdfFonts();
321 $fontSelection = new htmlResponsiveSelect('pdf_font', $fonts, [], _('Font'), '411');
322 $fontSelection->setCSSClasses(['lam-save-selection']);
323 $fontSelection->setHasDescriptiveElements(true);
324 $fontSelection->setSortElements(false);
325 $row->add($fontSelection);
326 }
327 $row->addVerticalSpacer('1rem');
328
329 $uploadButton = new htmlButton('submitfile', _('Upload file and create accounts'));
330 $uploadButton->setCSSClasses(['lam-primary']);
331 $row->addLabel($uploadButton);
332 $row->addField(new htmlOutputText('&nbsp;', false));
333 $row->addVerticalSpacer('2rem');
334
335 $row->add(new htmlTitle(_("Columns")));
336
337 // DN options
338 $dnTitle = new htmlSubTitle(_("DN settings"), '../../graphics/logo.svg');
339 $row->add($dnTitle);
340 $titles = [_('Name'), _("Identifier"), _("Example value"), _("Default value"), _("Possible values")];
341 $data = [];
342 // DN suffix
343 $dnSuffixRowCells = [];
344 $nameGroup = new htmlGroup();
345 $help = new htmlHelpLink('361');
346 $help->setCSSClasses(['hide-on-mobile']);
347 $nameGroup->addElement($help);
348 $nameGroup->addElement(new htmlSpacer('0.25rem', '16px'));
349 $nameGroup->addElement(new htmlOutputText(_("DN suffix")));
350 $help = new htmlHelpLink('361');
351 $help->setCSSClasses(['hide-on-tablet']);
352 $nameGroup->addElement($help);
353 $dnSuffixRowCells[] = $nameGroup;
354 $dnSuffixRowCells[] = new htmlOutputText('dn_suffix');
355 $dnSuffixRowCells[] = new htmlOutputText($type->getSuffix());
356 $dnSuffixRowCells[] = new htmlOutputText($type->getSuffix());
357 $dnSuffixRowCells[] = new htmlOutputText('');
358 $data[] = $dnSuffixRowCells;
359 // RDN
360 $dnRDNRowCells = [];
361 $rdnText = new htmlOutputText(_("RDN identifier"));
362 $rdnText->setMarkAsRequired(true);
363 $nameGroup = new htmlGroup();
364 $help = new htmlHelpLink('301');
365 $help->setCSSClasses(['hide-on-mobile']);
366 $nameGroup->addElement($help);
367 $nameGroup->addElement(new htmlSpacer('0.25rem', '16px'));
368 $nameGroup->addElement($rdnText);
369 $help = new htmlHelpLink('301');
370 $help->setCSSClasses(['hide-on-tablet']);
371 $nameGroup->addElement($help);
372 $dnRDNRowCells[] = $nameGroup;
373 $dnRDNRowCells[] = new htmlOutputText('dn_rdn');
374 $rdnAttributes = getRDNAttributes($type->getId(), $selectedModules);
375 $dnRDNRowCells[] = new htmlOutputText($rdnAttributes[0]);
376 $dnRDNRowCells[] = new htmlOutputText('');
377 $dnRDNRowCells[] = new htmlOutputText(implode(", ", $rdnAttributes));
378 $dnRDNRowCells[] = new htmlHelpLink('301');
379 $data[] = $dnRDNRowCells;
380 // replace existing
381 $replaceRowCells = [];
382 $nameGroup = new htmlGroup();
383 $help = new htmlHelpLink('302');
384 $help->setCSSClasses(['hide-on-mobile']);
385 $nameGroup->addElement($help);
386 $nameGroup->addElement(new htmlSpacer('0.25rem', '16px'));
387 $nameGroup->addElement(new htmlOutputText(_("Overwrite")));
388 $help = new htmlHelpLink('302');
389 $help->setCSSClasses(['hide-on-tablet']);
390 $nameGroup->addElement($help);
391 $replaceRowCells[] = $nameGroup;
392 $replaceRowCells[] = new htmlOutputText('overwrite');
393 $replaceRowCells[] = new htmlOutputText('false');
394 $replaceRowCells[] = new htmlOutputText('false');
395 $replaceRowCells[] = new htmlOutputText('true, false');
396 $data[] = $replaceRowCells;
397
398 $table = new htmlResponsiveTable($titles, $data);
399 $table->setCSSClasses(['alternating-color']);
400 $row->add($table);
401
402 // module options
403 foreach ($modules as $moduleName) {
404 // skip modules without upload columns
405 if (count($columns[$moduleName]) < 1) {
406 continue;
407 }
408 $data = [];
409 $row->addVerticalSpacer('2rem');
410 $module = moduleCache::getModule($moduleName, $scope);
411 if ($module === null) {
412 continue;
413 }
414 $icon = $module->getIcon();
415 if (!(str_starts_with($icon, 'http')) && !(str_starts_with($icon, '/'))) {
416 $icon = '../../graphics/' . $icon;
417 }
418 $moduleTitle = new htmlSubTitle($module->get_alias(), $icon);
419 $moduleTitle->colspan = 20;
420 $row->add($moduleTitle);
421 foreach ($columns[$moduleName] as $column) {
422 $required = false;
423 if (isset($column['required']) && ($column['required'] === true)) {
424 $required = true;
425 }
426 $rowCells = [];
427 $descriptionText = new htmlOutputText($column['description']);
428 $descriptionText->setMarkAsRequired($required);
429 $nameGroup = new htmlGroup();
430 $help = new htmlHelpLink($column['help'], $moduleName, $scope);
431 $help->setCSSClasses(['hide-on-mobile']);
432 $nameGroup->addElement($help);
433 $nameGroup->addElement(new htmlSpacer('0.25rem', '16px'));
434 $nameGroup->addElement($descriptionText);
435 $help = new htmlHelpLink($column['help'], $moduleName, $scope);
436 $help->setCSSClasses(['hide-on-tablet']);
437 $nameGroup->addElement($help);
438 $rowCells[] = $nameGroup;
439 $rowCells[] = new htmlOutputText($column['name']);
440 $example = '';
441 if (isset($column['example'])) {
442 $example = $column['example'];
443 }
444 $rowCells[] = new htmlOutputText($example);
445 $rowCells[] = isset($column['default']) ? new htmlOutputText($column['default']) : new htmlOutputText('');
446 $rowCells[] = isset($column['values']) ? new htmlOutputText($column['values']) : new htmlOutputText('');
447 $data[] = $rowCells;
448 }
449 $table = new htmlResponsiveTable($titles, $data);
450 $table->setCSSClasses(['alternating-color']);
451 $row->add($table);
452 }
453
454 addSecurityTokenToMetaHTML($row);
455 parseHtml(null, $row, [], false, $scope);
456
457 echo "</form>\n";
458
459 // build sample CSV
460 $sampleCSV_head = [];
461 $sampleCSV_row = [];
462 // DN attributes
463 $sampleCSV_head[] = "\"dn_suffix\"";
464 $sampleCSV_head[] = "\"dn_rdn\"";
465 $sampleCSV_head[] = "\"overwrite\"";
466 // module attributes
467 foreach ($modules as $moduleName) {
468 if (count($columns[$moduleName]) < 1) {
469 continue;
470 }
471 foreach ($columns[$moduleName] as $column) {
472 $sampleCSV_head[] = "\"" . $column['name'] . "\"";
473 }
474 }
475 $RDNs = getRDNAttributes($type->getId(), $selectedModules);
476 // DN attributes
477 $sampleCSV_row[] = "\"" . $type->getSuffix() . "\"";
478 $sampleCSV_row[] = "\"" . $RDNs[0] . "\"";
479 $sampleCSV_row[] = "\"false\"";
480 // module attributes
481 foreach ($modules as $moduleName) {
482 if (count($columns[$moduleName]) < 1) {
483 continue;
484 }
485 foreach ($columns[$moduleName] as $column) {
486 $sampleCSV_row[] = isset($column['example']) ? '"' . $column['example'] . '"' : '""';
487 }
488 }
489 $sampleCSV = implode(",", $sampleCSV_head) . "\n" . implode(",", $sampleCSV_row) . "\n";
490 $_SESSION['mass_csv'] = $sampleCSV;
491
492 echo '</div>';
493 include __DIR__ . '/../../lib/adminFooter.inc';
494 die;
495 }