From cf5cc2f5192dfc97293db6a7237ad22b9c4edd6b Mon Sep 17 00:00:00 2001 From: Roland Gruber Date: Sun, 18 Jul 2004 10:18:25 +0000 Subject: [PATCH] added dynamic settings for modules --- lam/lib/config.inc | 135 +++++++++++++++++++++++++++++----------- lam/tests/conf-test.php | 9 ++- 2 files changed, 105 insertions(+), 39 deletions(-) diff --git a/lam/lib/config.inc b/lam/lib/config.inc index 78ce5001..eebab9eb 100644 --- a/lam/lib/config.inc +++ b/lam/lib/config.inc @@ -145,6 +145,9 @@ class Config { /** Default language */ var $defaultLanguage; + /** module settings */ + var $moduleSettings = array(); + /** * Path to external lamdaemon script on server where it is executed * @@ -183,7 +186,7 @@ class Config { "domainsuffix", "MinUID", "MaxUID", "MinGID", "MaxGID", "MinMachine", "MaxMachine", "userlistAttributes", "grouplistAttributes", "hostlistAttributes", "maxlistentries", "defaultLanguage", "scriptPath", "scriptServer", "cachetimeout", "pwdhash", - "usermodules", "groupmodules", "hostmodules"); + "usermodules", "groupmodules", "hostmodules", "modules"); /** @@ -201,11 +204,16 @@ class Config { $this->reload(); } - /** Reloads preferences from config file */ + /** + * Reloads preferences from config file + * + * @return boolean true if file was readable + */ function reload() { $conffile = substr(__FILE__, 0, strlen(__FILE__) - 15) . "/config/" . $this->file . ".conf"; if (is_file($conffile) == True) { - $file = fopen($conffile, "r"); + $file = @fopen($conffile, "r"); + if (!$file) return false; // abort if file is not readable while (!feof($file)) { $line = fgets($file, 1024); $line = trim($line); // remove spaces at the beginning and end @@ -215,16 +223,23 @@ class Config { $keyword = $this->settings[$i]; $keylen = strlen($keyword); if (strtolower(substr($line, 0, $keylen + 2)) == strtolower($keyword . ": ")) { - $this->$keyword = substr($line, $keylen + 2, strlen($line) - $keylen -2); + // 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)] = substr($option, $pos + 2, strlen($option) - $pos - 2); + } + // general settings + else { + $this->$keyword = substr($line, $keylen + 2, strlen($line) - $keylen - 2); + } break; } } } fclose($file); } - else { - StatusMessage("ERROR", "", _("Unable to load configuration!") . " (" . $conffile . ")"); - } + return true; } /** Saves preferences to config file */ @@ -240,6 +255,7 @@ class Config { fclose($file); // generate new configuration file $saved = array(); // includes all settings which have been saved + $mod_saved = array(); // includes all module settings which have been saved for ($i = 0; $i < sizeof($file_array); $i++) { $line = trim($file_array[$i]); if (($line == "")||($line[0] == "#")) continue; // ignore comments and empty lines @@ -248,8 +264,19 @@ class Config { $keyword = $this->settings[$k]; $keylen = strlen($keyword); if (strtolower(substr($line, 0, $keylen + 1)) == strtolower($keyword . ":")) { - $file_array[$i] = $keyword . ": " . $this->$keyword . "\n"; - $saved[] = $keyword; // mark keyword as saved + // module settings + if (strtolower(substr($line, 0, $keylen + 2)) == "modules: ") { + $option = substr($line, $keylen + 2, strlen($line) - $keylen - 2); + $pos = strpos($option, ":"); + $name = substr($option, 0, $pos); + $file_array[$i] = "modules: " . $name . ": " . $this->moduleSettings[$name] . "\n"; + $mod_saved[] = $name; // mark keyword as saved + } + // general settings + else { + $file_array[$i] = $keyword . ": " . $this->$keyword . "\n"; + $saved[] = $keyword; // mark keyword as saved + } break; } } @@ -289,6 +316,13 @@ class Config { if (!in_array("usermodules", $saved)) array_push($file_array, "\n\n# List of used user modules\n" . "usermodules: " . $this->usermodules . "\n"); if (!in_array("groupmodules", $saved)) array_push($file_array, "\n\n# List of used group modules\n" . "groupmodules: " . $this->groupmodules . "\n"); if (!in_array("hostmodules", $saved)) array_push($file_array, "\n\n# List of used host modules\n" . "hostmodules: " . $this->hostmodules . "\n"); + // check if all module settings were added + $m_settings = array_keys($this->moduleSettings); + for ($i = 0; $i < sizeof($m_settings); $i++) { + if (!in_array($m_settings[$i], $mod_saved)) { + array_push($file_array, "modules: " . $m_settings[$i] . ": " . $this->moduleSettings[$m_settings[$i]] . "\n"); + } + } $file = fopen($conffile, "w"); if ($file) { for ($i = 0; $i < sizeof($file_array); $i++) fputs($file, $file_array[$i]); @@ -304,30 +338,35 @@ class Config { /** Prints current preferences */ function printconf() { - echo "" . _("Server address") . ": " . $this->ServerURL . "
"; - echo "" . _("Password hash type") . ": " . $this->pwdhash . "
"; - echo "" . _("Cache timeout") . ": " . $this->cachetimeout . "
"; - echo "" . _("UserSuffix") . ": " . $this->usersuffix . "
"; - echo "" . _("GroupSuffix") . ": " . $this->groupsuffix . "
"; - echo "" . _("HostSuffix") . ": " . $this->hostsuffix . "
"; - echo "" . _("DomainSuffix") . ": " . $this->domainsuffix . "
"; - echo "" . _("Minimum UID number") . ": " . $this->MinUID . "
"; - echo "" . _("Maximum UID number") . ": " . $this->MaxUID . "
"; - echo "" . _("Minimum GID number") . ": " . $this->MinGID . "
"; - echo "" . _("Maximum GID number") . ": " . $this->MaxGID . "
"; - echo "" . _("Minimum Machine number") . ": " . $this->MinMachine . "
"; - echo "" . _("Maximum Machine number") . ": " . $this->MaxMachine . "
"; - echo "" . _("Attributes in User List") . ": " . $this->userlistAttributes . "
"; - echo "" . _("Attributes in Group List") . ": " . $this->grouplistAttributes . "
"; - echo "" . _("Attributes in Host List") . ": " . $this->hostlistAttributes . "
"; - echo "" . _("Maximum list entries") . ": " . $this->maxlistentries . "
"; - echo "" . _("Default language") . ": " . $this->defaultLanguage . "
"; - echo "" . _("Path to external script") . ": " . $this->scriptPath . "
"; - echo "" . _("Server of external script") . ": " . $this->scriptServer . "
"; - echo "" . _("List of valid users") . ": " . $this->Admins . "
"; - echo "" . _("User modules") . ": " . $this->usermodules . "
"; - echo "" . _("Group modules") . ": " . $this->groupmodules . "
"; - echo "" . _("Host modules") . ": " . $this->hostmodules . "

"; + echo "" . _("Server address") . ": " . $this->ServerURL . "
\n"; + echo "" . _("Password hash type") . ": " . $this->pwdhash . "
\n"; + echo "" . _("Cache timeout") . ": " . $this->cachetimeout . "
\n"; + echo "" . _("UserSuffix") . ": " . $this->usersuffix . "
\n"; + echo "" . _("GroupSuffix") . ": " . $this->groupsuffix . "
\n"; + echo "" . _("HostSuffix") . ": " . $this->hostsuffix . "
\n"; + echo "" . _("DomainSuffix") . ": " . $this->domainsuffix . "
\n"; + echo "" . _("Minimum UID number") . ": " . $this->MinUID . "
\n"; + echo "" . _("Maximum UID number") . ": " . $this->MaxUID . "
\n"; + echo "" . _("Minimum GID number") . ": " . $this->MinGID . "
\n"; + echo "" . _("Maximum GID number") . ": " . $this->MaxGID . "
\n"; + echo "" . _("Minimum Machine number") . ": " . $this->MinMachine . "
\n"; + echo "" . _("Maximum Machine number") . ": " . $this->MaxMachine . "
\n"; + echo "" . _("Attributes in User List") . ": " . $this->userlistAttributes . "
\n"; + echo "" . _("Attributes in Group List") . ": " . $this->grouplistAttributes . "
\n"; + echo "" . _("Attributes in Host List") . ": " . $this->hostlistAttributes . "
\n"; + echo "" . _("Maximum list entries") . ": " . $this->maxlistentries . "
\n"; + echo "" . _("Default language") . ": " . $this->defaultLanguage . "
\n"; + echo "" . _("Path to external script") . ": " . $this->scriptPath . "
\n"; + echo "" . _("Server of external script") . ": " . $this->scriptServer . "
\n"; + echo "" . _("List of valid users") . ": " . $this->Admins . "
\n"; + echo "" . _("User modules") . ": " . $this->usermodules . "
\n"; + echo "" . _("Group modules") . ": " . $this->groupmodules . "
\n"; + echo "" . _("Host modules") . ": " . $this->hostmodules . "

\n"; + echo "" . _("Module settings") . ":
\n"; + echo "\n"; } // functions to read/write preferences @@ -946,6 +985,27 @@ class Config { return true; } + /** + * Sets the settings for the account modules. + * + * @param array $settings list of module setting array(name => value) + * @return true if $settings has correct format + */ + function set_moduleSettings($settings) { + if (!is_array($settings)) return false; + $this->moduleSettings = $settings; + return true; + } + + /** + * Returns a list of saved module settings + * + * @return array list of settings: array(name => value) + */ + function get_moduleSettings() { + return $this->moduleSettings; + } + } @@ -971,11 +1031,14 @@ class CfgMain { /** * Reloads preferences from config file config.cfg + * + * @return boolean true if file was readable */ function reload() { $conffile = substr(__FILE__, 0, strlen(__FILE__) - 15) . "/config/config.cfg"; if (is_file($conffile) == True) { - $file = fopen($conffile, "r"); + $file = @fopen($conffile, "r"); + if (!$file) return false; // abort if file is not readable while (!feof($file)) { $line = fgets($file, 1024); $line = trim($line); // remove spaces at the beginning and end @@ -992,9 +1055,7 @@ class CfgMain { } fclose($file); } - else { - StatusMessage("ERROR", "", _("Unable to load configuration!") . " (" . $conffile . ")"); - } + return true; } /** diff --git a/lam/tests/conf-test.php b/lam/tests/conf-test.php index b422e248..49cab02f 100644 --- a/lam/tests/conf-test.php +++ b/lam/tests/conf-test.php @@ -29,7 +29,7 @@ echo " Current Config

"); $conf->printconf(); echo ("

Starting Test...

"); -// now all prferences are loaded +// now all preferences are loaded echo ("Loading preferences..."); $ServerURL = $conf->get_ServerURL(); $cachetimeout = $conf->get_cacheTimeout(); @@ -53,6 +53,7 @@ $defaultlanguage = $conf->get_defaultlanguage(); $scriptpath = $conf->get_scriptPath(); $scriptServer = $conf->get_scriptServer(); $pwdhash = $conf->get_pwdhash(); +$moduleSettings = $conf->get_moduleSettings(); echo ("done
"); // next we modify them and save lam.conf echo ("Changing preferences..."); @@ -78,6 +79,7 @@ $conf->set_defaultlanguage("de_AT:iso639_de:Deutsch (Oesterreich)"); $conf->set_scriptPath("/var/www/lam/lib/script"); $conf->set_scriptServer("127.0.0.1"); $conf->set_pwdhash("SMD5"); +$conf->set_moduleSettings(array("test1" => 11, "test2" => "abc", 'test3' => 3)); $conf->save(); echo ("done
"); // at last all preferences are read from lam.conf and compared @@ -105,6 +107,8 @@ if ($conf2->get_defaultlanguage() != "de_AT:iso639_de:Deutsch (Oesterreich)") ec if ($conf2->get_scriptPath() != "/var/www/lam/lib/script") echo ("
Saving script path failed!
"); if ($conf2->get_scriptServer() != "127.0.0.1") echo ("
Saving script server failed!
"); if ($conf2->get_pwdhash() != "SMD5") echo ("
Saving pwdhash failed!
"); +$msettings = $conf2->get_moduleSettings(); +if (($msettings['test1'] != 11) || ($msettings['test2'] != 'abc') || ($msettings['test3'] != '3')) echo ("
Saving module settings failed!
"); echo ("done
"); // restore old values echo ("Restoring old preferences..."); @@ -128,8 +132,9 @@ $conf2->set_hostlistAttributes($hostlistAttributes); $conf2->set_maxlistentries($maxlistentries); $conf2->set_defaultLanguage($defaultlanguage); $conf2->set_scriptPath($scriptpath); -$conf2->set_scriptServer($scriptserver); +$conf2->set_scriptServer($scriptServer); $conf2->set_pwdhash($pwdhash); +$conf2->set_moduleSettings($moduleSettings); $conf2->save(); echo ("done
"); // finished