diff --git a/lam/lib/config.inc b/lam/lib/config.inc index c956984b..5a97ced8 100644 --- a/lam/lib/config.inc +++ b/lam/lib/config.inc @@ -614,49 +614,56 @@ class LAMConfig { $line = trim($line); // remove spaces at the beginning and end if (($line == "")||($line[0] == "#")) continue; // ignore comments and empty lines // search keywords - for ($i = 0; $i < sizeof($this->settings); $i++) { - $keyword = $this->settings[$i]; - $keylen = strlen($keyword); - if (strtolower(substr($line, 0, $keylen + 2)) == strtolower($keyword . ": ")) { - // module settings - if (strtolower(substr($line, 0, $keylen + 2)) == "modules: ") { - $option = substr($line, $keylen + 2, strlen($line) - $keylen - 2); - $pos = strpos($option, ":"); - $this->moduleSettings[substr($option, 0, $pos)] = explode(LAMConfig::LINE_SEPARATOR, substr($option, $pos + 2)); - } - // type settings - elseif (strtolower(substr($line, 0, $keylen + 2)) == "types: ") { - $option = substr($line, $keylen + 2, strlen($line) - $keylen - 2); - $pos = strpos($option, ":"); - $this->typeSettings[substr($option, 0, $pos)] = substr($option, $pos + 2); - } - // tool settings - elseif (strtolower(substr($line, 0, $keylen + 2)) == "tools: ") { - $option = substr($line, $keylen + 2, strlen($line) - $keylen - 2); - $pos = strpos($option, ":"); - $this->toolSettings[substr($option, 0, $pos)] = substr($option, $pos + 2); - } - // job settings - elseif (strtolower(substr($line, 0, $keylen + 2)) == "jobs: ") { - $option = substr($line, $keylen + 2, strlen($line) - $keylen - 2); - $pos = strpos($option, ":"); - $this->jobSettings[substr($option, 0, $pos)] = explode(LAMConfig::LINE_SEPARATOR, substr($option, $pos + 2)); - } - // general settings - else { - $this->$keyword = substr($line, $keylen + 2, strlen($line) - $keylen - 2); - } - break; + $parts = explode(': ', $line); + $keyword = $parts[0]; + if (!in_array($keyword, $this->settings)) { + continue; + } + $startIndex = strlen($keyword) + 2; + if (sizeof($parts) == 1) { + // empty global settings + $this->$keyword = ''; + } + elseif (sizeof($parts) == 2) { + // global setting with value + $this->$keyword = substr($line, $startIndex); + } + else { + $subKeyword = $parts[1]; + $startIndex = $startIndex + strlen($subKeyword) + 2; + // module settings + if ($keyword == 'modules') { + $option = substr($line, $startIndex); + $this->moduleSettings[$subKeyword] = explode(LAMConfig::LINE_SEPARATOR, $option); } - elseif (strtolower($line) == strtolower($keyword . ":")) { - // set empty options - $this->$keyword = ''; + // type settings + if ($keyword == 'types') { + $option = substr($line, $startIndex); + $this->typeSettings[$subKeyword] = $option; + } + // tool settings + if ($keyword == 'tools') { + $option = substr($line, $startIndex); + $this->toolSettings[$subKeyword] = $option; + } + // job settings + if ($keyword == 'jobs') { + $option = substr($line, $startIndex); + $this->jobSettings[$subKeyword] = explode(LAMConfig::LINE_SEPARATOR, $option); } } } fclose($file); } - // check types + $this->removeInvalidTypes(); + $this->removeInvalidModules(); + return true; + } + + /** + * Removes any non-existing types from the configuration. + */ + private function removeInvalidTypes() { $allTypes = LAM\TYPES\getTypes(); $activeTypes = $this->get_ActiveTypes(); for ($i = 0; $i < sizeof($activeTypes); $i++) { @@ -666,14 +673,23 @@ class LAMConfig { } $activeTypes = array_values($activeTypes); $this->set_ActiveTypes($activeTypes); - // check modules + } + + /** + * Removes any non-existing modules from the configuration. + */ + private function removeInvalidModules() { $types = $this->get_ActiveTypes(); + $availableByScope = array(); foreach ($types as $type) { $scope = \LAM\TYPES\getScopeFromTypeId($type); $moduleVar = "modules_" . $type; if (isset($this->typeSettings[$moduleVar])){ $modules = explode(",", $this->typeSettings[$moduleVar]); - $available = getAvailableModules($scope); + if (empty($availableByScope[$scope])) { + $availableByScope[$scope] = getAvailableModules($scope); + } + $available = $availableByScope[$scope]; // only return available modules $ret = array(); for ($i = 0; $i < sizeof($modules); $i++) { @@ -682,7 +698,6 @@ class LAMConfig { $this->typeSettings[$moduleVar] = implode(",", $ret); } } - return true; } /** Saves preferences to config file */ diff --git a/lam/lib/types.inc b/lam/lib/types.inc index f1a045bc..53905e1a 100644 --- a/lam/lib/types.inc +++ b/lam/lib/types.inc @@ -160,15 +160,15 @@ class ConfiguredType { private $id; - private $suffix; + private $suffix = null; - private $attributes; + private $attributes = null; - private $alias; + private $alias = null; - private $additionalLdapFilter; + private $additionalLdapFilter = null; - private $hidden; + private $hidden = null; private $baseType; @@ -180,22 +180,11 @@ class ConfiguredType { * @param TypeManager $typeManager type manager * @param string $scope account type * @param string $id unique ID for this configuration - * @param string $suffix LDAP base suffix - * @param array $attributes list of ListAttribute - * @param string $alias alias name for display - * @param string $ldapFilter additional LDAP filter - * @param boolean $hidden hidden in GUI */ - public function __construct(&$typeManager, $scope, $id, $suffix, $attributes, $alias, - $ldapFilter, $hidden) { + public function __construct(&$typeManager, $scope, $id) { $this->typeManager = &$typeManager; $this->scope = $scope; $this->id = $id; - $this->suffix = $suffix; - $this->attributes = $attributes; - $this->alias = $alias; - $this->additionalLdapFilter = $ldapFilter; - $this->hidden = $hidden; } /** @@ -231,6 +220,10 @@ class ConfiguredType { * @return string LDAP suffix */ public function getSuffix() { + if ($this->suffix !== null) { + return $this->suffix; + } + $this->suffix = $this->typeManager->getConfig()->get_Suffix($this->id); return $this->suffix; } @@ -240,6 +233,16 @@ class ConfiguredType { * @return ListAttribute[] list of ListAttribute */ public function getAttributes() { + if ($this->attributes !== null) { + return $this->attributes; + } + $attributeString = $this->typeManager->getConfig()->get_listAttributes($this->id); + $attributeSpecs = explode(';', $attributeString); + $attributes = array(); + foreach ($attributeSpecs as $attributeSpec) { + $attributes[] = new ListAttribute($attributeSpec, $this->scope); + } + $this->attributes = $attributes; return $this->attributes; } @@ -249,6 +252,10 @@ class ConfiguredType { * @return string alias name */ public function getAlias() { + if ($this->alias !== null) { + return $this->alias; + } + $this->alias = getTypeAlias($this->id, $this->typeManager->getConfig()); return $this->alias; } @@ -258,6 +265,10 @@ class ConfiguredType { * @return string LDAP filter */ public function getAdditionalLdapFilter() { + if ($this->additionalLdapFilter !== null) { + return $this->additionalLdapFilter; + } + $this->additionalLdapFilter = $this->typeManager->getConfig()->get_Suffix($typeId); return $this->additionalLdapFilter; } @@ -267,6 +278,10 @@ class ConfiguredType { * @return boolean hidden */ public function isHidden() { + if ($this->hidden !== null) { + return $this->hidden; + } + $this->hidden = isAccountTypeHidden($this->id); return $this->hidden; } @@ -293,7 +308,7 @@ class ConfiguredType { $connection = $_SESSION["ldap"]->server(); $ret = array(); $filter = $this->getBaseType()->getSuffixFilter(); - $sr = @ldap_search($connection, escapeDN($this->suffix), $filter, array('dn', 'objectClass'), 0, 0, 0, LDAP_DEREF_NEVER); + $sr = @ldap_search($connection, escapeDN($this->getSuffix()), $filter, array('dn', 'objectClass'), 0, 0, 0, LDAP_DEREF_NEVER); if ($sr) { $units = ldap_get_entries($connection, $sr); cleanLDAPResult($units); @@ -312,13 +327,13 @@ class ConfiguredType { // add root suffix if needed $found = false; for ($i = 0; $i < sizeof($ret); $i++) { // search suffix case-intensitive - if (strtolower($this->suffix) == strtolower($ret[$i])) { + if (strtolower($this->getSuffix()) == strtolower($ret[$i])) { $found = true; break; } } if (!$found) { - $ret[] = $this->suffix; + $ret[] = $this->getSuffix(); } usort($ret, 'compareDN'); return $ret; @@ -466,29 +481,7 @@ class TypeManager { */ private function buildConfiguredType($typeId) { $scope = getScopeFromTypeId($typeId); - $suffix = $this->config->get_Suffix($typeId); - $attributes = $this->getAttributes($typeId, $scope); - $alias = getTypeAlias($typeId, $this->config); - $ldapFilter = $this->config->get_Suffix($typeId); - $hidden = isAccountTypeHidden($typeId); - return new ConfiguredType($this, $scope, $typeId, $suffix, $attributes, $alias, $ldapFilter, $hidden); - } - - /** - * Builds the list of account list attributes. - * - * @param string $typeId type id - * @param string $scope account type - * @return \LAM\TYPES\ListAttribute[] list attributes - */ - private function getAttributes($typeId, $scope) { - $attributeString = $this->config->get_listAttributes($typeId); - $attributeSpecs = explode(';', $attributeString); - $attributes = array(); - foreach ($attributeSpecs as $attributeSpec) { - $attributes[] = new ListAttribute($attributeSpec, $scope); - } - return $attributes; + return new ConfiguredType($this, $scope, $typeId); } /** diff --git a/lam/tests/lib/LAMConfigTest.php b/lam/tests/lib/LAMConfigTest.php index c4790bde..5328ef7a 100644 --- a/lam/tests/lib/LAMConfigTest.php +++ b/lam/tests/lib/LAMConfigTest.php @@ -252,6 +252,12 @@ class LAMConfigTest extends PHPUnit_Framework_TestCase { $this->assertEquals($val, $this->lAMConfig->get_scriptPath()); $this->doSave(); $this->assertEquals($val, $this->lAMConfig->get_scriptPath()); + // empty script + $val = ''; + $this->lAMConfig->set_scriptPath($val); + $this->assertEquals($val, $this->lAMConfig->get_scriptPath()); + $this->doSave(); + $this->assertEquals($val, $this->lAMConfig->get_scriptPath()); } /**