From cacf62f70a6af511fc5573210e309bc47f8eb1a8 Mon Sep 17 00:00:00 2001 From: arjen Date: Thu, 28 Aug 2003 12:16:33 +0000 Subject: [PATCH] The method configuration::read() will also read a user-specific configuration from the home directory if a PHP script is not run through the web server. --- src/phpclasses/configuration.class.php | 58 +++++++++++++++++++++++++++++++--- 1 file changed, 54 insertions(+), 4 deletions(-) diff --git a/src/phpclasses/configuration.class.php b/src/phpclasses/configuration.class.php index 30a1446..1aaaa66 100644 --- a/src/phpclasses/configuration.class.php +++ b/src/phpclasses/configuration.class.php @@ -37,8 +37,8 @@ class configuration function configuration() { - $system = false; - $user = false; + $this->system = false; + $this->user = false; } function xmlFindTag($node, $tag) @@ -60,6 +60,7 @@ class configuration function read($app_name) { + $this->system = false; $filename = "/etc/" . $app_name . ".conf"; if (!is_readable($filename)) { @@ -81,10 +82,38 @@ class configuration } } } - else + + $this->user = false; + if (isset($_ENV['HOME']) && $_ENV['HOME'] != '/') { - print("Configuration error: Configuration file for $app_name not found.
"); + // Read configurations from the user's homedir. + + $filename = $_ENV['HOME'] . "/." . $app_name . ".conf"; + if (is_readable($filename)) + { + + $this->user = xmldocfile($filename); + + if ($this->user) + { + $root = $this->user->root(); + + if ($root->tagname != $app_name) + { + print("Configuration error: Wrong configuration file.
"); + $this->user = false; + } + } + } + + } + + if ($this->system == false && $this->user == false) + { + print("Configuration error: Configuration file for $app_name not found.
\n"); } + + return $this->system != false || $this->user != false; } function find_parameter($section, $parameter) @@ -109,6 +138,27 @@ class configuration } } + // If the parameter is also defined in the user config, it will override + // the system-wide value. + + if ($this->user) + { + $root_node = $this->user->root(); + $section_node = $this->xmlFindTag($root_node->children(), $section); + if ($section_node) + { + $param_node = $this->xmlFindTag($section_node->children(), $parameter); + if ($param_node) + { + $param_node = $param_node->children(); + } + if ($param_node && $param_node[0]->type == XML_TEXT_NODE) + { + $param_value = $param_node[0]->content; + } + } + } + return $param_value; } } -- 2.11.0