refactoring

This commit is contained in:
Roland Gruber 2025-07-27 10:47:43 +02:00
parent 9f74afa310
commit 68b928be12

View file

@ -1,5 +1,7 @@
<?php <?php
use LAM\PDF\PDFEntry;
use LAM\PDF\PDFLabelValue;
use LAM\PDF\PdfStructurePersistenceManager; use LAM\PDF\PdfStructurePersistenceManager;
use LAM\PROFILES\AccountProfilePersistenceManager; use LAM\PROFILES\AccountProfilePersistenceManager;
use LAM\TYPES\ConfiguredType; use LAM\TYPES\ConfiguredType;
@ -204,6 +206,7 @@ function getRDNAttributes($typeId, $selectedModules = null) {
* @return array dependencies * @return array dependencies
*/ */
function getModulesDependencies($scope) { function getModulesDependencies($scope) {
$return = [];
$mods = getAvailableModules($scope); $mods = getAvailableModules($scope);
for ($i = 0; $i < count($mods); $i++) { for ($i = 0; $i < count($mods); $i++) {
$module = moduleCache::getModule($mods[$i], $scope); $module = moduleCache::getModule($mods[$i], $scope);
@ -394,7 +397,7 @@ function checkConfigOptions($moduleToTypeIds, &$options) {
foreach ($moduleToTypeIds as $module => $typeIds) { foreach ($moduleToTypeIds as $module => $typeIds) {
$m = moduleCache::getModule($module, getScopeFromTypeId($typeIds[0])); $m = moduleCache::getModule($module, getScopeFromTypeId($typeIds[0]));
$errors = $m->check_configOptions($typeIds, $options); $errors = $m->check_configOptions($typeIds, $options);
if (isset($errors) && is_array($errors)) { if (is_array($errors)) {
$return = array_merge($return, $errors); $return = array_merge($return, $errors);
} }
} }
@ -412,12 +415,8 @@ function checkConfigOptions($moduleToTypeIds, &$options) {
function getHelp($module, $helpID, $scope) { function getHelp($module, $helpID, $scope) {
global $helpArray; global $helpArray;
if (!isset($module) || ($module == '') || ($module == 'main')) { if (!isset($module) || ($module == '') || ($module == 'main')) {
$helpPath = "../help/help.inc";
if (is_file("../../help/help.inc")) {
$helpPath = "../../help/help.inc";
}
if (!isset($helpArray)) { if (!isset($helpArray)) {
include_once($helpPath); include_once(__DIR__ . '/../help/help.inc');
} }
return $helpArray[$helpID]; return $helpArray[$helpID];
} }
@ -696,22 +695,6 @@ function parseHtml($module, $input, $values, $restricted, $scope) {
return []; return [];
} }
/**
* Helper function to sort descriptive options in parseHTML().
* It compares the second entries of two arrays.
*
* @param array $a first array
* @param array $b second array
* @return integer compare result
*/
function lamCompareDescriptiveOptions(&$a, &$b) {
// check parameters
if (!is_array($a) || !isset($a[1]) || !is_array($b) || !isset($b[1])) {
return 0;
}
return strnatcasecmp($a[1], $b[1]);
}
/** /**
* Prints a LAM help link. * Prints a LAM help link.
* *
@ -758,13 +741,7 @@ class accountContainer {
* @param ConfiguredType $type account type * @param ConfiguredType $type account type
* @param string $base key in $_SESSION where this object is saved * @param string $base key in $_SESSION where this object is saved
*/ */
function __construct($type, $base) { public function __construct(ConfiguredType $type, string $base) {
if (!($type instanceof ConfiguredType)) {
die('Argument of accountContainer must be ConfiguredType.');
}
if (!is_string($base)) {
die('Argument base of accountContainer must be string: ' . $base);
}
$this->type = $type; $this->type = $type;
$this->base = $base; $this->base = $base;
// Set start page // Set start page
@ -1056,14 +1033,12 @@ class accountContainer {
* *
* @param array $result list of messages * @param array $result list of messages
*/ */
private function printModuleContent($result) { private function printModuleContent(array $result): void {
$this->printPageHeader(); $this->printPageHeader();
$this->printPasswordPromt(); $this->printPasswordPromt();
// display error messages // display error messages
if (is_array($result)) { for ($i = 0; $i < count($result); $i++) {
for ($i = 0; $i < count($result); $i++) { call_user_func_array(StatusMessage(...), $result[$i]);
call_user_func_array(StatusMessage(...), $result[$i]);
}
} }
echo '<div id="passwordMessageArea"></div>'; echo '<div id="passwordMessageArea"></div>';
echo "<table class=\"lam-account-edit-table\">\n"; echo "<table class=\"lam-account-edit-table\">\n";
@ -1710,7 +1685,7 @@ class accountContainer {
/** /**
* Fixes spelling errors in the attribute names. * Fixes spelling errors in the attribute names.
* *
* @param array $attributes LDAP attributes * @param array<string, string[]|string>|null $attributes LDAP attributes
* @param array $modules list of active modules * @param array $modules list of active modules
* @return array fixed attributes * @return array fixed attributes
*/ */
@ -2055,7 +2030,7 @@ class accountContainer {
if (!$stopprocessing) { if (!$stopprocessing) {
logNewMessage(LOG_DEBUG, 'Attribute changes for ' . $DNs[$i] . ":\n" . print_r($attributes[$DNs[$i]], true)); logNewMessage(LOG_DEBUG, 'Attribute changes for ' . $DNs[$i] . ":\n" . print_r($attributes[$DNs[$i]], true));
// modify attributes // modify attributes
if (!empty($attributes[$DNs[$i]]['modify']) && !$stopprocessing) { if (!empty($attributes[$DNs[$i]]['modify'])) {
$success = @ldap_mod_replace($_SESSION['ldap']->server(), $DNs[$i], $attributes[$DNs[$i]]['modify']); $success = @ldap_mod_replace($_SESSION['ldap']->server(), $DNs[$i], $attributes[$DNs[$i]]['modify']);
if (!$success) { if (!$success) {
logNewMessage(LOG_ERR, 'Unable to modify attributes of DN: ' . $DNs[$i] . ' (' . ldap_error($_SESSION['ldap']->server()) . '). ' logNewMessage(LOG_ERR, 'Unable to modify attributes of DN: ' . $DNs[$i] . ' (' . ldap_error($_SESSION['ldap']->server()) . '). '
@ -2136,9 +2111,9 @@ class accountContainer {
/** /**
* Defines if the LDAP entry has only virtual child entries. This is the case for e.g. LDAP views. * Defines if the LDAP entry has only virtual child entries. This is the case for e.g. LDAP views.
* *
* @return boolean has only virtual children * @return bool has only virtual children
*/ */
public function hasOnlyVirtualChildren() { public function hasOnlyVirtualChildren(): bool {
foreach ($this->module as $module) { foreach ($this->module as $module) {
if ($module->hasOnlyVirtualChildren()) { if ($module->hasOnlyVirtualChildren()) {
return true; return true;
@ -2154,7 +2129,7 @@ class accountContainer {
* @param string $typeId type id (user, group, host) * @param string $typeId type id (user, group, host)
* @return PDFEntry[] list of key => PDFEntry * @return PDFEntry[] list of key => PDFEntry
*/ */
function get_pdfEntries($pdfKeys, $typeId) { function get_pdfEntries($pdfKeys, $typeId): array {
$return = []; $return = [];
while (($current = current($this->module)) != null) { while (($current = current($this->module)) != null) {
$return = array_merge($return, $current->get_pdfEntries($pdfKeys, $typeId)); $return = array_merge($return, $current->get_pdfEntries($pdfKeys, $typeId));
@ -2164,7 +2139,7 @@ class accountContainer {
if ($this->finalDN !== null) { if ($this->finalDN !== null) {
$dn = $this->finalDN; $dn = $this->finalDN;
} }
return array_merge($return, ['main_dn' => [new \LAM\PDF\PDFLabelValue(_('DN'), $dn)]]); return array_merge($return, ['main_dn' => [new PDFLabelValue(_('DN'), $dn)]]);
} }
/** /**
@ -2263,11 +2238,11 @@ class accountContainer {
/** /**
* Returns the RDN part of a given DN. * Returns the RDN part of a given DN.
* *
* @param String $dn DN * @param string|null $dn DN
* @return String RDN * @return string RDN
*/ */
function getRDN($dn) { function getRDN(?string $dn): string {
if (($dn == "") || ($dn == null)) { if (($dn === "") || ($dn === null)) {
return ""; return "";
} }
return substr($dn, 0, strpos($dn, ",")); return substr($dn, 0, strpos($dn, ","));
@ -2276,11 +2251,11 @@ class accountContainer {
/** /**
* Returns the parent DN of a given DN. * Returns the parent DN of a given DN.
* *
* @param String $dn DN * @param string|null $dn DN
* @return String DN * @return string DN
*/ */
function getParentDN($dn) { function getParentDN(?string $dn): string {
if (($dn == "") || ($dn == null)) { if (($dn === "") || ($dn === null)) {
return ""; return "";
} }
return substr($dn, strpos($dn, ",") + 1); return substr($dn, strpos($dn, ",") + 1);
@ -2299,21 +2274,6 @@ class accountContainer {
return $this->cachedOUs; return $this->cachedOUs;
} }
/**
* Returns the account status.
*
* @return AccountStatus status
*/
public function getAccountStatus(): AccountStatus {
$details = [];
foreach ($this->module as $module) {
if ($module instanceof AccountStatusProvider) {
$details = array_merge($details, $module->getAccountStatusDetails());
}
}
return new AccountStatus($details);
}
/** /**
* Replaces POST data with wildcard values from modules. * Replaces POST data with wildcard values from modules.
* *
@ -2613,11 +2573,10 @@ class AccountStatus {
$modules = $_SESSION['config']->get_AccountModules($type->getId()); $modules = $_SESSION['config']->get_AccountModules($type->getId());
$details = []; $details = [];
foreach ($modules as $module) { foreach ($modules as $module) {
$interfaces = class_implements($module); $moduleObject = moduleCache::getModule($module, $type->getScope());
if (!in_array('AccountStatusProvider', $interfaces)) { if (!($moduleObject instanceof AccountStatusProvider)) {
continue; continue;
} }
$moduleObject = moduleCache::getModule($module, $type->getScope());
$details = array_merge($details, $moduleObject->getAccountStatusDetails($type, $attributes)); $details = array_merge($details, $moduleObject->getAccountStatusDetails($type, $attributes));
} }
return new AccountStatus($details); return new AccountStatus($details);
@ -2634,11 +2593,10 @@ class AccountStatus {
$modules = $_SESSION['config']->get_AccountModules($type->getId()); $modules = $_SESSION['config']->get_AccountModules($type->getId());
$details = []; $details = [];
foreach ($modules as $module) { foreach ($modules as $module) {
$interfaces = class_implements($module); $moduleObject = moduleCache::getModule($module, $type->getScope());
if (!in_array('AccountStatusProvider', $interfaces)) { if (!($moduleObject instanceof AccountStatusProvider)) {
continue; continue;
} }
$moduleObject = moduleCache::getModule($module, $type->getScope());
$details = array_merge($details, $moduleObject->getAccountStatusPossibleLockOptions($type, $attributes)); $details = array_merge($details, $moduleObject->getAccountStatusPossibleLockOptions($type, $attributes));
} }
return new AccountStatus($details); return new AccountStatus($details);