refactoring

This commit is contained in:
Roland Gruber 2024-12-12 18:50:11 +01:00
parent 3729b3639f
commit f8313d826e
3 changed files with 66 additions and 77 deletions

View file

@ -56,7 +56,9 @@ enforceUserIsLoggedIn();
checkIfToolIsActive('toolFileUpload'); checkIfToolIsActive('toolFileUpload');
// die if no write access // die if no write access
if (!checkIfWriteAccessIsAllowed()) die(); if (!checkIfWriteAccessIsAllowed()) {
die();
}
// Redirect to startpage if user is not logged in // Redirect to startpage if user is not logged in
if (!isLoggedIn()) { if (!isLoggedIn()) {
@ -196,7 +198,7 @@ if ($_FILES['inputfile'] && ($_FILES['inputfile']['size'] > 0)) {
$values_given[] = $dataRow[$colNumber]; $values_given[] = $dataRow[$colNumber];
} }
$values_unique = array_unique($values_given); $values_unique = array_unique($values_given);
if (sizeof($values_given) != sizeof($values_unique)) { if (count($values_given) !== count($values_unique)) {
$duplicates = []; $duplicates = [];
foreach ($values_given as $key => $value) { foreach ($values_given as $key => $value) {
if (!isset($values_unique[$key]) && ($value !== null)) { if (!isset($values_unique[$key]) && ($value !== null)) {
@ -211,7 +213,7 @@ if ($_FILES['inputfile'] && ($_FILES['inputfile']['size'] > 0)) {
logNewMessage(LOG_DEBUG, 'End of generic checks'); logNewMessage(LOG_DEBUG, 'End of generic checks');
// if input data is invalid just display error messages (max 50) // if input data is invalid just display error messages (max 50)
if (sizeof($errors) > 0) { if ($errors !== []) {
foreach ($errors as $error) { foreach ($errors as $error) {
$container->add(new htmlStatusMessage("ERROR", $error[0], $error[1]), 12); $container->add(new htmlStatusMessage("ERROR", $error[0], $error[1]), 12);
} }
@ -237,12 +239,7 @@ if ($_FILES['inputfile'] && ($_FILES['inputfile']['size'] > 0)) {
$rdnValue = $rdnValue[0]; $rdnValue = $rdnValue[0];
} }
$account_dn = $data[$i][$ids['dn_rdn']] . "=" . ldap_escape($rdnValue, '', LDAP_ESCAPE_DN) . ","; $account_dn = $data[$i][$ids['dn_rdn']] . "=" . ldap_escape($rdnValue, '', LDAP_ESCAPE_DN) . ",";
if ($data[$i][$ids['dn_suffix']] == "") { $account_dn = ($data[$i][$ids['dn_suffix']] == "") ? $account_dn . $suffix : $account_dn . $data[$i][$ids['dn_suffix']];
$account_dn = $account_dn . $suffix;
}
else {
$account_dn = $account_dn . $data[$i][$ids['dn_suffix']];
}
$accounts[$i]['dn'] = $account_dn; $accounts[$i]['dn'] = $account_dn;
} }
// set overwrite // set overwrite
@ -251,7 +248,7 @@ if ($_FILES['inputfile'] && ($_FILES['inputfile']['size'] > 0)) {
} }
} }
// print errors if DN could not be built // print errors if DN could not be built
if (sizeof($errors) > 0) { if ($errors !== []) {
foreach ($errors as $error) { foreach ($errors as $error) {
$container->add(new htmlStatusMessage("ERROR", $error[0], $error[1], $error[2]), 12); $container->add(new htmlStatusMessage("ERROR", $error[0], $error[1], $error[2]), 12);
} }

View file

@ -54,7 +54,9 @@ enforceUserIsLoggedIn();
checkIfToolIsActive('toolFileUpload'); checkIfToolIsActive('toolFileUpload');
// die if no write access // die if no write access
if (!checkIfWriteAccessIsAllowed()) die(); if (!checkIfWriteAccessIsAllowed()) {
die();
}
// Redirect to startpage if user is not logged in // Redirect to startpage if user is not logged in
if (!isLoggedIn()) { if (!isLoggedIn()) {

View file

@ -1,5 +1,7 @@
<?php <?php
namespace LAM\UPLOAD; namespace LAM\UPLOAD;
use htmlInputCheckbox; use htmlInputCheckbox;
use htmlLabel; use htmlLabel;
use \htmlResponsiveTable; use \htmlResponsiveTable;
@ -20,6 +22,7 @@ use \htmlResponsiveSelect;
use \htmlSpacer; use \htmlSpacer;
use LAM\PDF\PdfStructurePersistenceManager; use LAM\PDF\PdfStructurePersistenceManager;
use \moduleCache; use \moduleCache;
/* /*
This code is part of LDAP Account Manager (http://www.ldap-account-manager.org/) This code is part of LDAP Account Manager (http://www.ldap-account-manager.org/)
@ -42,11 +45,11 @@ use \moduleCache;
*/ */
/** /**
* Start page of file upload * Start page of file upload
* *
* @author Roland Gruber * @author Roland Gruber
* @package tools * @package tools
*/ */
/** security functions */ /** security functions */
include_once(__DIR__ . "/../../lib/security.inc"); include_once(__DIR__ . "/../../lib/security.inc");
@ -69,7 +72,9 @@ enforceUserIsLoggedIn();
checkIfToolIsActive('toolFileUpload'); checkIfToolIsActive('toolFileUpload');
// die if no write access // die if no write access
if (!checkIfWriteAccessIsAllowed()) die(); if (!checkIfWriteAccessIsAllowed()) {
die();
}
checkIfToolIsActive('toolFileUpload'); checkIfToolIsActive('toolFileUpload');
@ -102,11 +107,11 @@ include __DIR__ . '/../../lib/adminHeader.inc';
// get possible types and remove those which do not support file upload // get possible types and remove those which do not support file upload
$typeManager = new \LAM\TYPES\TypeManager(); $typeManager = new \LAM\TYPES\TypeManager();
$types = $typeManager->getConfiguredTypes(); $types = $typeManager->getConfiguredTypes();
$count = sizeof($types); $count = count($types);
for ($i = 0; $i < $count; $i++) { for ($i = 0; $i < $count; $i++) {
$myType = $types[$i]; $myType = $types[$i];
if (!$myType->getBaseType()->supportsFileUpload() || $myType->isHidden() if (!$myType->getBaseType()->supportsFileUpload() || $myType->isHidden()
|| !checkIfNewEntriesAreAllowed($myType->getId()) || !checkIfWriteAccessIsAllowed($myType->getId())) { || !checkIfNewEntriesAreAllowed($myType->getId()) || !checkIfWriteAccessIsAllowed($myType->getId())) {
unset($types[$i]); unset($types[$i]);
} }
} }
@ -127,11 +132,11 @@ if (isset($_POST['type'])) {
} }
$deps = getModulesDependencies($type->getScope()); $deps = getModulesDependencies($type->getScope());
$depErrors = check_module_depends($selectedModules, $deps); $depErrors = check_module_depends($selectedModules, $deps);
if (is_array($depErrors) && (sizeof($depErrors) > 0)) { if (is_array($depErrors) && ($depErrors !== [])) {
foreach ($depErrors as $depError) { foreach ($depErrors as $depError) {
StatusMessage('ERROR', _("Unsolved dependency:") . ' ' . StatusMessage('ERROR', _("Unsolved dependency:") . ' ' .
getModuleAlias($depError[0], $type->getScope()) . " (" . getModuleAlias($depError[0], $type->getScope()) . " (" .
getModuleAlias($depError[1], $type->getScope()) . ")"); getModuleAlias($depError[1], $type->getScope()) . ")");
} }
} }
else { else {
@ -214,14 +219,14 @@ foreach ($types as $type) {
$moduleGroup->addElement(new htmlLabel($type->getId() . '___' . $moduleName, getModuleAlias($moduleName, $type->getScope()))); $moduleGroup->addElement(new htmlLabel($type->getId() . '___' . $moduleName, getModuleAlias($moduleName, $type->getScope())));
$innerRow->add($moduleGroup, 12, 6, 4); $innerRow->add($moduleGroup, 12, 6, 4);
} }
$moduleCount = sizeof($modules); $moduleCount = count($modules);
if ($moduleCount%3 == 2) { if ($moduleCount % 3 == 2) {
$innerRow->add(new htmlOutputText('&nbsp;', false), 0, 0, 4); $innerRow->add(new htmlOutputText('&nbsp;', false), 0, 0, 4);
} }
if ($moduleCount%3 == 1) { if ($moduleCount % 3 == 1) {
$innerRow->add(new htmlOutputText('&nbsp;', false), 0, 0, 4); $innerRow->add(new htmlOutputText('&nbsp;', false), 0, 0, 4);
} }
if ($moduleCount%2 == 1) { if ($moduleCount % 2 == 1) {
$innerRow->add(new htmlOutputText('&nbsp;', false), 0, 6, 0); $innerRow->add(new htmlOutputText('&nbsp;', false), 0, 6, 0);
} }
$typeDiv = new htmlDiv($type->getId(), $innerRow); $typeDiv = new htmlDiv($type->getId(), $innerRow);
@ -232,8 +237,8 @@ foreach ($types as $type) {
// ok button // ok button
$row->addVerticalSpacer('3rem'); $row->addVerticalSpacer('3rem');
if (!empty($types)) { if (!empty($types)) {
$okButton = new htmlButton('submit', _('Ok')); $okButton = new htmlButton('submit', _('Ok'));
$okButton->setCSSClasses(['lam-primary']); $okButton->setCSSClasses(['lam-primary']);
$row->add($okButton); $row->add($okButton);
} }
@ -246,11 +251,11 @@ echo '</div>';
include __DIR__ . '/../../lib/adminFooter.inc'; include __DIR__ . '/../../lib/adminFooter.inc';
/** /**
* Displays the account type specific main page of the upload. * Displays the account type specific main page of the upload.
* *
* @param \LAM\TYPES\ConfiguredType $type account type * @param \LAM\TYPES\ConfiguredType $type account type
* @param string[] $selectedModules list of selected account modules * @param string[] $selectedModules list of selected account modules
*/ */
function showMainPage(\LAM\TYPES\ConfiguredType $type, array $selectedModules): void { function showMainPage(\LAM\TYPES\ConfiguredType $type, array $selectedModules): void {
$scope = $type->getScope(); $scope = $type->getScope();
echo '<div class="smallPaddingContent">'; echo '<div class="smallPaddingContent">';
@ -297,7 +302,7 @@ function showMainPage(\LAM\TYPES\ConfiguredType $type, array $selectedModules):
if (isset($_POST['pdfStructure'])) { if (isset($_POST['pdfStructure'])) {
$pdfSelected = [$_POST['pdfStructure']]; $pdfSelected = [$_POST['pdfStructure']];
} }
else if (in_array('default', $pdfStructures)) { elseif (in_array('default', $pdfStructures)) {
$pdfSelected = ['default']; $pdfSelected = ['default'];
} }
$row->add(new htmlResponsiveSelect('pdfStructure', $pdfStructures, $pdfSelected, _('PDF structure'))); $row->add(new htmlResponsiveSelect('pdfStructure', $pdfStructures, $pdfSelected, _('PDF structure')));
@ -386,7 +391,7 @@ function showMainPage(\LAM\TYPES\ConfiguredType $type, array $selectedModules):
// module options // module options
foreach ($modules as $moduleName) { foreach ($modules as $moduleName) {
// skip modules without upload columns // skip modules without upload columns
if (sizeof($columns[$moduleName]) < 1) { if (count($columns[$moduleName]) < 1) {
continue; continue;
} }
$data = []; $data = [];
@ -423,18 +428,8 @@ function showMainPage(\LAM\TYPES\ConfiguredType $type, array $selectedModules):
$example = $column['example']; $example = $column['example'];
} }
$rowCells[] = new htmlOutputText($example); $rowCells[] = new htmlOutputText($example);
if (isset($column['default'])) { $rowCells[] = isset($column['default']) ? new htmlOutputText($column['default']) : new htmlOutputText('');
$rowCells[] = new htmlOutputText($column['default']); $rowCells[] = isset($column['values']) ? new htmlOutputText($column['values']) : new htmlOutputText('');
}
else {
$rowCells[] = new htmlOutputText('');
}
if (isset($column['values'])) {
$rowCells[] = new htmlOutputText($column['values']);
}
else {
$rowCells[] = new htmlOutputText('');
}
$data[] = $rowCells; $data[] = $rowCells;
} }
$table = new htmlResponsiveTable($titles, $data); $table = new htmlResponsiveTable($titles, $data);
@ -450,38 +445,33 @@ function showMainPage(\LAM\TYPES\ConfiguredType $type, array $selectedModules):
// build sample CSV // build sample CSV
$sampleCSV_head = []; $sampleCSV_head = [];
$sampleCSV_row = []; $sampleCSV_row = [];
// DN attributes // DN attributes
$sampleCSV_head[] = "\"dn_suffix\""; $sampleCSV_head[] = "\"dn_suffix\"";
$sampleCSV_head[] = "\"dn_rdn\""; $sampleCSV_head[] = "\"dn_rdn\"";
$sampleCSV_head[] = "\"overwrite\""; $sampleCSV_head[] = "\"overwrite\"";
// module attributes // module attributes
foreach ($modules as $moduleName) { foreach ($modules as $moduleName) {
if (sizeof($columns[$moduleName]) < 1) { if (count($columns[$moduleName]) < 1) {
continue; continue;
}
foreach ($columns[$moduleName] as $column) {
$sampleCSV_head[] = "\"" . $column['name'] . "\"";
}
} }
$RDNs = getRDNAttributes($type->getId(), $selectedModules); foreach ($columns[$moduleName] as $column) {
// DN attributes $sampleCSV_head[] = "\"" . $column['name'] . "\"";
$sampleCSV_row[] = "\"" . $type->getSuffix() . "\"";
$sampleCSV_row[] = "\"" . $RDNs[0] . "\"";
$sampleCSV_row[] = "\"false\"";
// module attributes
foreach ($modules as $moduleName) {
if (sizeof($columns[$moduleName]) < 1) {
continue;
}
foreach ($columns[$moduleName] as $column) {
if (isset($column['example'])) {
$sampleCSV_row[] = '"' . $column['example'] . '"';
}
else {
$sampleCSV_row[] = '""';
}
}
} }
}
$RDNs = getRDNAttributes($type->getId(), $selectedModules);
// DN attributes
$sampleCSV_row[] = "\"" . $type->getSuffix() . "\"";
$sampleCSV_row[] = "\"" . $RDNs[0] . "\"";
$sampleCSV_row[] = "\"false\"";
// module attributes
foreach ($modules as $moduleName) {
if (count($columns[$moduleName]) < 1) {
continue;
}
foreach ($columns[$moduleName] as $column) {
$sampleCSV_row[] = isset($column['example']) ? '"' . $column['example'] . '"' : '""';
}
}
$sampleCSV = implode(",", $sampleCSV_head) . "\n" . implode(",", $sampleCSV_row) . "\n"; $sampleCSV = implode(",", $sampleCSV_head) . "\n" . implode(",", $sampleCSV_row) . "\n";
$_SESSION['mass_csv'] = $sampleCSV; $_SESSION['mass_csv'] = $sampleCSV;