From 1934e89b53ccb50baf7524a00443ccdc2c63f709 Mon Sep 17 00:00:00 2001 From: Roland Gruber Date: Fri, 9 Sep 2022 08:02:32 +0200 Subject: [PATCH] refactoring --- lam/templates/tools/multiEdit.php | 29 ++++++---- lam/templates/tools/ou_edit.php | 67 ++++++++++++---------- lam/templates/tools/treeView.php | 12 ++-- lam/templates/upload/massBuildAccounts.php | 15 +++-- phpstan.neon | 3 + 5 files changed, 72 insertions(+), 54 deletions(-) diff --git a/lam/templates/tools/multiEdit.php b/lam/templates/tools/multiEdit.php index 9ab83c01f..d27ad7182 100644 --- a/lam/templates/tools/multiEdit.php +++ b/lam/templates/tools/multiEdit.php @@ -476,16 +476,18 @@ function dryRun(): array { // store LDIF $filename = 'ldif' . getRandomNumber() . '.ldif'; $out = @fopen(dirname(__FILE__) . '/../../tmp/' . $filename, "wb"); - fwrite($out, $ldif); - $container->addElement(new htmlOutputText(_('LDIF file')), true); - $ldifLink = new htmlLink($filename, '../../tmp/' . $filename); - $ldifLink->setTargetWindow('_blank'); - $container->addElement($ldifLink, true); - $container->addVerticalSpace('20px'); - $container->addElement(new htmlOutputText(_('Log output')), true); - $container->addElement(new htmlInputTextarea('log', $log, 100, 30), true); - // generate HTML - fclose ($out); + if ($out !== false) { + fwrite($out, $ldif); + $container->addElement(new htmlOutputText(_('LDIF file')), true); + $ldifLink = new htmlLink($filename, '../../tmp/' . $filename); + $ldifLink->setTargetWindow('_blank'); + $container->addElement($ldifLink, true); + $container->addVerticalSpace('20px'); + $container->addElement(new htmlOutputText(_('Log output')), true); + $container->addElement(new htmlInputTextarea('log', $log, 100, 30), true); + // generate HTML + fclose ($out); + } ob_start(); $tabindex = 1; parseHtml(null, $container, array(), true, $tabindex, 'user'); @@ -587,13 +589,16 @@ function doModify(): array { * Returns the HTML code for a htmlStatusMessage * * @param htmlStatusMessage $msg message - * @return String HTML code + * @return string HTML code */ -function getMessageHTML($msg) { +function getMessageHTML(htmlStatusMessage $msg): string { $tabindex = 0; ob_start(); parseHtml(null, $msg, array(), true, $tabindex, 'user'); $content = ob_get_contents(); ob_end_clean(); + if ($content === false) { + return ''; + } return $content; } diff --git a/lam/templates/tools/ou_edit.php b/lam/templates/tools/ou_edit.php index 733624f7d..79714adcc 100644 --- a/lam/templates/tools/ou_edit.php +++ b/lam/templates/tools/ou_edit.php @@ -14,7 +14,7 @@ use \htmlGroup; /* This code is part of LDAP Account Manager (http://www.ldap-account-manager.org/) - Copyright (C) 2003 - 2021 Roland Gruber + Copyright (C) 2003 - 2022 Roland Gruber 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 @@ -115,38 +115,43 @@ if (isset($_POST['createOU']) || isset($_POST['deleteOU'])) { elseif (isset($_POST['deleteOU'])) { // check for sub entries $sr = ldap_list($_SESSION['ldap']->server(), $_POST['deleteableOU'], "ObjectClass=*", array("")); - $info = ldap_get_entries($_SESSION['ldap']->server(), $sr); - if ($sr && ($info['count'] === 0)) { - // print header - include '../../lib/adminHeader.inc'; - echo '
'; - echo "
\n"; - $tabindex = 1; - $container = new htmlResponsiveRow(); - $label = new htmlOutputText(_("Do you really want to delete this OU?")); - $label->colspan = 5; - $container->add($label, 12); - $container->addVerticalSpacer('1rem'); - $dnLabel = new htmlOutputText(getAbstractDN($_POST['deleteableOU'])); - $dnLabel->colspan = 5; - $container->add($dnLabel, 12); - $container->addVerticalSpacer('1rem'); - $buttonGroup = new htmlGroup(); - $buttonGroup->addElement(new htmlButton('sure', _("Delete"))); - $buttonGroup->addElement(new htmlSpacer('0.5rem', null)); - $buttonGroup->addElement(new htmlButton('abort', _("Cancel"))); - $container->add($buttonGroup, 12); - $container->add(new htmlHiddenInput('deleteOU', 'submit'), 12); - $container->add(new htmlHiddenInput('deletename', $_POST['deleteableOU']), 12); - addSecurityTokenToMetaHTML($container); - parseHtml(null, $container, array(), false, $tabindex, 'user'); - echo "
"; - echo '
'; - include '../../lib/adminFooter.inc'; - exit(); + if ($sr === false) { + $error = _("OU is not empty or invalid!"); } else { - $error = _("OU is not empty or invalid!"); + $info = ldap_get_entries($_SESSION['ldap']->server(), $sr); + if (($info !== false) && ($info['count'] === 0)) { + // print header + include '../../lib/adminHeader.inc'; + echo '
'; + echo "
\n"; + $tabindex = 1; + $container = new htmlResponsiveRow(); + $label = new htmlOutputText(_("Do you really want to delete this OU?")); + $label->colspan = 5; + $container->add($label, 12); + $container->addVerticalSpacer('1rem'); + $dnLabel = new htmlOutputText(getAbstractDN($_POST['deleteableOU'])); + $dnLabel->colspan = 5; + $container->add($dnLabel, 12); + $container->addVerticalSpacer('1rem'); + $buttonGroup = new htmlGroup(); + $buttonGroup->addElement(new htmlButton('sure', _("Delete"))); + $buttonGroup->addElement(new htmlSpacer('0.5rem', null)); + $buttonGroup->addElement(new htmlButton('abort', _("Cancel"))); + $container->add($buttonGroup, 12); + $container->add(new htmlHiddenInput('deleteOU', 'submit'), 12); + $container->add(new htmlHiddenInput('deletename', $_POST['deleteableOU']), 12); + addSecurityTokenToMetaHTML($container); + parseHtml(null, $container, array(), false, $tabindex, 'user'); + echo "
"; + echo '
'; + include '../../lib/adminFooter.inc'; + exit(); + } + else { + $error = _("OU is not empty or invalid!"); + } } } } diff --git a/lam/templates/tools/treeView.php b/lam/templates/tools/treeView.php index f275995a5..59d7246c4 100644 --- a/lam/templates/tools/treeView.php +++ b/lam/templates/tools/treeView.php @@ -75,11 +75,13 @@ function showTree(): void { if ((strlen($initialDn) > strlen($rootDn)) && substr($initialDn, -1 * strlen($rootDn)) === $rootDn) { $extraDnPart = substr($initialDn, 0, (-1 * strlen($rootDn)) - 1); $dnParts = ldap_explode_dn($extraDnPart, 0); - unset($dnParts['count']); - $dnPartsCount = sizeof($dnParts); - for ($i = 0; $i < $dnPartsCount; $i++) { - $currentParts = array_slice($dnParts, $dnPartsCount - ($i + 1)); - $openInitial[] = '"' . base64_encode(implode(',', $currentParts) . ',' . $rootDn) . '"'; + if ($dnParts !== false) { + unset($dnParts['count']); + $dnPartsCount = sizeof($dnParts); + for ($i = 0; $i < $dnPartsCount; $i++) { + $currentParts = array_slice($dnParts, $dnPartsCount - ($i + 1)); + $openInitial[] = '"' . base64_encode(implode(',', $currentParts) . ',' . $rootDn) . '"'; + } } } } diff --git a/lam/templates/upload/massBuildAccounts.php b/lam/templates/upload/massBuildAccounts.php index 64f77026d..06efc8668 100644 --- a/lam/templates/upload/massBuildAccounts.php +++ b/lam/templates/upload/massBuildAccounts.php @@ -126,13 +126,16 @@ if ($_FILES['inputfile'] && ($_FILES['inputfile']['size'] > 0)) { $uploadColumns = getUploadColumns($type, $selectedModules); // read input file $handle = fopen ($_FILES['inputfile']['tmp_name'], "r"); - if (($head = fgetcsv($handle, 2000)) !== false ) { // head row - foreach ($head as $i => $headItem) { - $ids[$headItem] = $i; + if ($handle !== false) { + if (($head = fgetcsv($handle, 2000)) !== false ) { // head row + foreach ($head as $i => $headItem) { + $ids[$headItem] = $i; + } } - } - while (($line = fgetcsv($handle, 2000)) !== false ) { // account rows - $data[] = $line; + while (($line = fgetcsv($handle, 2000)) !== false ) { // account rows + $data[] = $line; + } + fclose($handle); } $errors = array(); diff --git a/phpstan.neon b/phpstan.neon index 6de3dbc88..067235067 100644 --- a/phpstan.neon +++ b/phpstan.neon @@ -28,3 +28,6 @@ parameters: - '#Cannot assign new offset to array\|string.#' - '#Parameter \#1 \$result of function ldap_free_result expects LDAP\\Result, array\|LDAP\\Result given.#' - '#Cannot access offset .* on array\|int.#' + - '#Parameter \#1 \$haystack of function strpos expects string, int\|string given.#' + - '#Argument of an invalid type array\|int supplied for foreach, only iterables are supported.#' + - '#Cannot access offset mixed on non-empty-array\|int.#'