From b12ba2369e37302982d1eb4b81c8efec4f4761d3 Mon Sep 17 00:00:00 2001 From: Roland Gruber Date: Thu, 19 Mar 2020 20:42:36 +0100 Subject: [PATCH] refactoring --- lam/lib/2factor.inc | 10 +++---- lam/lib/checkEnvironment.inc | 11 +++---- lam/lib/ldap.inc | 10 +++---- lam/lib/modules.inc | 6 ++-- lam/lib/modules/authorizedServiceObject.inc | 10 +++---- lam/lib/modules/ddns.inc | 12 +++----- lam/lib/modules/dhcp_settings.inc | 32 ++++++++++----------- lam/lib/modules/eduPerson.inc | 28 +++++++----------- lam/lib/modules/freeRadius.inc | 26 ++++++++--------- 9 files changed, 65 insertions(+), 80 deletions(-) diff --git a/lam/lib/2factor.inc b/lam/lib/2factor.inc index c04bf665..f65b9a90 100644 --- a/lam/lib/2factor.inc +++ b/lam/lib/2factor.inc @@ -560,12 +560,10 @@ class WebauthnProvider extends BaseProvider { $row->add($loginButton, 12); $errorMessage = new htmlStatusMessage('ERROR', '', _('This service requires a browser with "WebAuthn" support.')); $row->add(new htmlDiv(null, $errorMessage, array('hidden webauthn-error')), 12); - if ($this->config->twoFactorAuthenticationOptional === true) { - if (!$hasTokens) { - $skipButton = new htmlButton('skip_webauthn', _('Skip')); - $skipButton->setCSSClasses(array('fullwidth')); - $row->add($skipButton, 12); - } + if (($this->config->twoFactorAuthenticationOptional === true) && !$hasTokens) { + $skipButton = new htmlButton('skip_webauthn', _('Skip')); + $skipButton->setCSSClasses(array('fullwidth')); + $row->add($skipButton, 12); } $errorMessageDiv = new htmlDiv('generic-webauthn-error', new htmlOutputText('')); $errorMessageDiv->addDataAttribute('button', _('Ok')); diff --git a/lam/lib/checkEnvironment.inc b/lam/lib/checkEnvironment.inc index 4bbb89db..bb6a32bb 100644 --- a/lam/lib/checkEnvironment.inc +++ b/lam/lib/checkEnvironment.inc @@ -110,11 +110,12 @@ if (ini_get("session.auto_start") == "1") { } // check memory limit $memLimit = ini_get('memory_limit'); -if (isset($memLimit) && ($memLimit != '') && (substr(strtoupper($memLimit), strlen($memLimit) - 1) == 'M')) { - if (intval(substr($memLimit, 0, strlen($memLimit) - 1)) < 128) { - $criticalErrors[] = array("ERROR", "Please increase the \"memory_limit\" parameter in your php.ini to at least \"128M\".", - "Your current memory limit is $memLimit."); - } +if (isset($memLimit) + && ($memLimit != '') + && (substr(strtoupper($memLimit), strlen($memLimit) - 1) == 'M') + && (intval(substr($memLimit, 0, strlen($memLimit) - 1)) < 128)) { + $criticalErrors[] = array("ERROR", "Please increase the \"memory_limit\" parameter in your php.ini to at least \"128M\".", + "Your current memory limit is $memLimit."); } // check PCRE regex system if (!@preg_match('/^\p{L}+$/u', "abc")) { diff --git a/lam/lib/ldap.inc b/lam/lib/ldap.inc index c2bbc238..db99d3a9 100644 --- a/lam/lib/ldap.inc +++ b/lam/lib/ldap.inc @@ -2,7 +2,7 @@ /* This code is part of LDAP Account Manager (http://www.ldap-account-manager.org/) - Copyright (C) 2003 - 2019 Roland Gruber + Copyright (C) 2003 - 2020 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 @@ -144,10 +144,10 @@ class Ldap{ $file = @readdir($dir); while ($file) { $path = $tmpDir . $file; - if ((substr($file, 0, 1) != '.') && !is_dir($path)) { - if ($time - filemtime($path) > 900) { - @unlink($path); - } + if ((substr($file, 0, 1) != '.') + && !is_dir($path) + && ($time - filemtime($path) > 900)) { + @unlink($path); } $file = @readdir($dir); } diff --git a/lam/lib/modules.inc b/lam/lib/modules.inc index 73fa32d4..aacba5a4 100644 --- a/lam/lib/modules.inc +++ b/lam/lib/modules.inc @@ -1779,10 +1779,8 @@ class accountContainer { // pass profile to each module $modules = array_keys($this->module); foreach ($modules as $module) $this->module[$module]->load_profile($profile); - if (isset($profile['ldap_rdn'][0])) { - if (in_array($profile['ldap_rdn'][0], getRDNAttributes($this->type->getId()))) { - $this->rdn = $profile['ldap_rdn'][0]; - } + if (isset($profile['ldap_rdn'][0]) && in_array($profile['ldap_rdn'][0], getRDNAttributes($this->type->getId()))) { + $this->rdn = $profile['ldap_rdn'][0]; } if (isset($profile['ldap_suffix'][0]) && ($profile['ldap_suffix'][0] != '-')) { $this->dnSuffix = $profile['ldap_suffix'][0]; diff --git a/lam/lib/modules/authorizedServiceObject.inc b/lam/lib/modules/authorizedServiceObject.inc index 0162a6b7..8073b6c2 100644 --- a/lam/lib/modules/authorizedServiceObject.inc +++ b/lam/lib/modules/authorizedServiceObject.inc @@ -3,7 +3,7 @@ This code is not yet part of LDAP Account Manager (http://www.ldap-account-manager.org/) Copyright (C) 2011 J de Jong - 2012 - 2019 Roland Gruber + 2012 - 2020 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 @@ -230,10 +230,10 @@ class authorizedServiceObject extends baseModule { // profile mappings in meta data parent::load_profile($profile); // add extension - if (isset($profile['authorizedServiceObject_addExt'][0]) && ($profile['authorizedServiceObject_addExt'][0] == "true")) { - if (!in_array('authorizedServiceObject', $this->attributes['objectClass'])) { - $this->attributes['objectClass'][] = 'authorizedServiceObject'; - } + if (isset($profile['authorizedServiceObject_addExt'][0]) + && ($profile['authorizedServiceObject_addExt'][0] == "true") + && !in_array('authorizedServiceObject', $this->attributes['objectClass'])) { + $this->attributes['objectClass'][] = 'authorizedServiceObject'; } // add ASs if (isset ($profile['authorizedServiceObject_services'][0]) && ($profile['authorizedServiceObject_services'][0] != "")) { diff --git a/lam/lib/modules/ddns.inc b/lam/lib/modules/ddns.inc index 6a127402..0160976f 100644 --- a/lam/lib/modules/ddns.inc +++ b/lam/lib/modules/ddns.inc @@ -281,10 +281,8 @@ class ddns extends baseModule { $this->setIgnoreClientUpdates(($client_insert == 'on')); $this->setUpdateKey($key_path); - if (!empty($key_path)) { - if (str_replace("\"","",$_POST['key_path']) != $key_path) { - $errors[] = $this->messages['key_path'][1]; - } + if (!empty($key_path) && (str_replace("\"","",$_POST['key_path']) != $key_path)) { + $errors[] = $this->messages['key_path'][1]; } return $errors; @@ -301,10 +299,8 @@ class ddns extends baseModule { $zone_reverse = trim($_POST['zone_reverse']); // ip correct??? - if (!empty($ip)) { - if (!check_ip($ip)) { - $errors[] = $this->messages['ip'][0]; - } + if (!empty($ip) && !check_ip($ip)) { + $errors[] = $this->messages['ip'][0]; } for ($i = 0; $i < sizeof($this->attributes['dhcpStatements']); $i++) { diff --git a/lam/lib/modules/dhcp_settings.inc b/lam/lib/modules/dhcp_settings.inc index c9a062d0..7a4e0f2c 100644 --- a/lam/lib/modules/dhcp_settings.inc +++ b/lam/lib/modules/dhcp_settings.inc @@ -370,10 +370,10 @@ By default, the nodes are configured as H-Nodes which fits for small networks. I */ public function save_attributes() { // remove dhcpSubnet object class if only the DHCP settings were changed - if ($this->isRootNode()) { - if (!in_array_ignore_case("dhcpSubnet", $this->orig['objectClass']) && in_array_ignore_case("dhcpSubnet", $this->attributes['objectClass'])) { - $this->attributes['objectClass'] = array_delete(array("dhcpSubnet"), $this->attributes['objectClass']); - } + if ($this->isRootNode() + && !in_array_ignore_case("dhcpSubnet", $this->orig['objectClass']) + && in_array_ignore_case("dhcpSubnet", $this->attributes['objectClass'])) { + $this->attributes['objectClass'] = array_delete(array("dhcpSubnet"), $this->attributes['objectClass']); } return parent::save_attributes(); } @@ -386,10 +386,10 @@ By default, the nodes are configured as H-Nodes which fits for small networks. I */ public function process_attributes() { // check if DHCP main settings and valid DHCP entry - if ($this->isRootNode()) { - if (!in_array_ignore_case('dhcpService', $this->attributes['objectClass']) && !in_array_ignore_case('dhcpServer', $this->attributes['objectClass'])) { - return array(); - } + if ($this->isRootNode() + && !in_array_ignore_case('dhcpService', $this->attributes['objectClass']) + && !in_array_ignore_case('dhcpServer', $this->attributes['objectClass'])) { + return array(); } $errors = array(); @@ -420,10 +420,8 @@ By default, the nodes are configured as H-Nodes which fits for small networks. I // Check domainname: if (!empty($_POST['domainname'])) $_POST['domainname'] = trim($_POST['domainname']); - if (!empty($_POST['domainname'])) { - if (!preg_match("/^[A-Za-z0-9\\._-]*$/", $_POST['domainname'])) { - $errors[] = $this->messages['domainname'][2]; - } + if (!empty($_POST['domainname']) && !preg_match("/^[A-Za-z0-9\\._-]*$/", $_POST['domainname'])) { + $errors[] = $this->messages['domainname'][2]; } $this->setDHCPOption('domain-name', '"' . $_POST['domainname'] . '"'); @@ -562,11 +560,11 @@ By default, the nodes are configured as H-Nodes which fits for small networks. I public function display_html_attributes() { $return = new htmlResponsiveRow(); // check if DHCP main settings and valid DHCP entry - if ($this->isRootNode()) { - if (!in_array_ignore_case('dhcpService', $this->attributes['objectClass']) && !in_array_ignore_case('dhcpServer', $this->attributes['objectClass'])) { - $return->add(new htmlStatusMessage('ERROR', _('Please set your LDAP suffix to an LDAP entry with object class "dhcpService" or "dhcpServer".')), 12); - return $return; - } + if ($this->isRootNode() + && !in_array_ignore_case('dhcpService', $this->attributes['objectClass']) + && !in_array_ignore_case('dhcpServer', $this->attributes['objectClass'])) { + $return->add(new htmlStatusMessage('ERROR', _('Please set your LDAP suffix to an LDAP entry with object class "dhcpService" or "dhcpServer".')), 12); + return $return; } // Subnet name if (!$this->isRootNode()) { diff --git a/lam/lib/modules/eduPerson.inc b/lam/lib/modules/eduPerson.inc index b24b3405..f0624ce0 100644 --- a/lam/lib/modules/eduPerson.inc +++ b/lam/lib/modules/eduPerson.inc @@ -2,7 +2,7 @@ /* This code is part of LDAP Account Manager (http://www.ldap-account-manager.org/) - Copyright (C) 2009 - 2019 Roland Gruber + Copyright (C) 2009 - 2020 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 @@ -398,10 +398,8 @@ class eduPerson extends baseModule { $this->attributes['eduPersonScopedAffiliation'] = array_values(array_unique($this->attributes['eduPersonScopedAffiliation'])); // principal name $this->attributes['eduPersonPrincipalName'][0] = $_POST['eduPersonPrincipalName']; - if ($_POST['eduPersonPrincipalName'] != '') { - if (!preg_match('/^[0-9a-z_\\.@-]+$/i', $_POST['eduPersonPrincipalName'])) { - $errors[] = $this->messages['eduPersonPrincipalName'][0]; - } + if (($_POST['eduPersonPrincipalName'] != '') && !preg_match('/^[0-9a-z_\\.@-]+$/i', $_POST['eduPersonPrincipalName'])) { + $errors[] = $this->messages['eduPersonPrincipalName'][0]; } // affiliations $this->attributes['eduPersonAffiliation'] = array(); @@ -422,17 +420,13 @@ class eduPerson extends baseModule { $this->processMultiValueInputTextField('eduPersonEntitlement', $errors); // org DN $this->attributes['eduPersonOrgDN'][0] = $_POST['eduPersonOrgDN']; - if ($_POST['eduPersonOrgDN'] != '') { - if (!get_preg($_POST['eduPersonOrgDN'], 'dn')) { - $errors[] = $this->messages['eduPersonOrgDN'][0]; - } + if (($_POST['eduPersonOrgDN'] != '') && !get_preg($_POST['eduPersonOrgDN'], 'dn')) { + $errors[] = $this->messages['eduPersonOrgDN'][0]; } // primary OU $this->attributes['eduPersonPrimaryOrgUnitDN'][0] = $_POST['eduPersonPrimaryOrgUnitDN']; - if ($_POST['eduPersonPrimaryOrgUnitDN'] != '') { - if (!get_preg($_POST['eduPersonPrimaryOrgUnitDN'], 'dn')) { - $errors[] = $this->messages['eduPersonPrimaryOrgUnitDN'][0]; - } + if (($_POST['eduPersonPrimaryOrgUnitDN'] != '') && !get_preg($_POST['eduPersonPrimaryOrgUnitDN'], 'dn')) { + $errors[] = $this->messages['eduPersonPrimaryOrgUnitDN'][0]; } // OUs $this->processMultiValueInputTextField('eduPersonOrgUnitDN', $errors, 'dn'); @@ -449,10 +443,10 @@ class eduPerson extends baseModule { function load_profile($profile) { parent::load_profile($profile); // add extension - if (isset($profile['eduPerson_addExt'][0]) && ($profile['eduPerson_addExt'][0] == "true")) { - if (!in_array('eduPerson', $this->attributes['objectClass'])) { - $this->attributes['objectClass'][] = 'eduPerson'; - } + if (isset($profile['eduPerson_addExt'][0]) + && ($profile['eduPerson_addExt'][0] == "true") + && !in_array('eduPerson', $this->attributes['objectClass'])) { + $this->attributes['objectClass'][] = 'eduPerson'; } } diff --git a/lam/lib/modules/freeRadius.inc b/lam/lib/modules/freeRadius.inc index 11a085f5..c27287d2 100644 --- a/lam/lib/modules/freeRadius.inc +++ b/lam/lib/modules/freeRadius.inc @@ -711,14 +711,14 @@ class freeRadius extends baseModule { function check_profileOptions($options, $typeId) { $messages = parent::check_profileOptions($options, $typeId); // group names - if (!$this->isBooleanConfigOptionSet('freeRadius_hideRadiusGroupName')) { - if (isset($options['freeRadius_radiusGroupName'][0]) && ($options['freeRadius_radiusGroupName'][0] != '')) { - $list = preg_split('/;[ ]*/', $options['freeRadius_radiusGroupName'][0]); - for ($i = 0; $i < sizeof($list); $i++) { - if (!get_preg($list[$i], 'groupname')) { - $messages[] = $this->messages['radiusGroupName'][0]; - break; - } + if (!$this->isBooleanConfigOptionSet('freeRadius_hideRadiusGroupName') + && isset($options['freeRadius_radiusGroupName'][0]) + && ($options['freeRadius_radiusGroupName'][0] != '')) { + $list = preg_split('/;[ ]*/', $options['freeRadius_radiusGroupName'][0]); + for ($i = 0; $i < sizeof($list); $i++) { + if (!get_preg($list[$i], 'groupname')) { + $messages[] = $this->messages['radiusGroupName'][0]; + break; } } } @@ -733,11 +733,11 @@ class freeRadius extends baseModule { function load_profile($profile) { // profile mappings in meta data parent::load_profile($profile); - if (!$this->isBooleanConfigOptionSet('freeRadius_hideRadiusGroupName')) { - // group names - if (isset($profile['freeRadius_radiusGroupName'][0]) && $profile['freeRadius_radiusGroupName'][0] != '') { - $this->attributes['radiusGroupName'] = preg_split('/;[ ]*/', $profile['freeRadius_radiusGroupName'][0]); - } + // group names + if (!$this->isBooleanConfigOptionSet('freeRadius_hideRadiusGroupName') + && isset($profile['freeRadius_radiusGroupName'][0]) + && ($profile['freeRadius_radiusGroupName'][0] != '')) { + $this->attributes['radiusGroupName'] = preg_split('/;[ ]*/', $profile['freeRadius_radiusGroupName'][0]); } }