From 1513ea546732c56d683f9f0c0ebb254dfbf1e473 Mon Sep 17 00:00:00 2001 From: arjen Date: Wed, 21 Nov 2007 15:27:48 +0000 Subject: [PATCH] Added a PHP5 module for the configuration class --- src/phpclasses/configuration.class.php | 8 +- src/phpclasses/configuration.class.php5 | 160 ++++++++++++++++++++++++++++++++ src/phpclasses/gnucomo_config.php | 9 +- 3 files changed, 172 insertions(+), 5 deletions(-) create mode 100644 src/phpclasses/configuration.class.php5 diff --git a/src/phpclasses/configuration.class.php b/src/phpclasses/configuration.class.php index 1aaaa66..4f0c2a5 100644 --- a/src/phpclasses/configuration.class.php +++ b/src/phpclasses/configuration.class.php @@ -26,7 +26,7 @@ // // RELATIONS : // SEE ALSO : -// LAST MODIFIED : Jul 29, 2002 +// LAST MODIFIED : Nov 19, 2007 /////////////////////////////////////////////////////////////////////////// */ @@ -61,6 +61,7 @@ class configuration function read($app_name) { $this->system = false; + $this->user = false; $filename = "/etc/" . $app_name . ".conf"; if (!is_readable($filename)) { @@ -69,7 +70,7 @@ class configuration if (is_readable($filename)) { - $this->system = xmldocfile($filename); + $this->system = domxml_open_file($filename); if ($this->system) { @@ -83,7 +84,6 @@ class configuration } } - $this->user = false; if (isset($_ENV['HOME']) && $_ENV['HOME'] != '/') { // Read configurations from the user's homedir. @@ -92,7 +92,7 @@ class configuration if (is_readable($filename)) { - $this->user = xmldocfile($filename); + $this->user = domxml_open_file($filename); if ($this->user) { diff --git a/src/phpclasses/configuration.class.php5 b/src/phpclasses/configuration.class.php5 new file mode 100644 index 0000000..68c529e --- /dev/null +++ b/src/phpclasses/configuration.class.php5 @@ -0,0 +1,160 @@ +system = false; + $this->user = false; + } + + function xmlFindTag($node, $tag) + { + $element = false; + $i = 0; + + // Assume we have PHP5 + while (!$element && $i < $node->length) + { + if ($node->item($i)->nodeType == XML_ELEMENT_NODE && $node->item($i)->nodeName == $tag) + { + $element = $node->item($i); + } + $i++; + } + + + return $element; + } + + function read($app_name) + { + $this->system = false; + $this->user = false; + $filename = "/etc/" . $app_name . ".conf"; + if (!is_readable($filename)) + { + $filename = "/usr/local/etc/" . $app_name . ".conf"; + } + if (is_readable($filename)) + { + + $this->system = new DOMDocument(); + + if ($this->system->load($filename)) + { + $root = $this->system->documentElement; + + if ($root->tagName != $app_name) + { + print("Configuration error: Wrong configuration file.
"); + $this->system = false; + } + } + } + + if (isset($_ENV['HOME']) && $_ENV['HOME'] != '/') + { + // Read configurations from the user's homedir. + + $filename = $_ENV['HOME'] . "/." . $app_name . ".conf"; + if (is_readable($filename)) + { + + $this->user = new DOMDocument(); + + if ($this->user->load($filename)) + { + $root = $this->user->documentElement; + + 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) + { + $param_value = ""; + + if ($this->system) + { + $root_node = $this->system->documentElement; + $section_node = $this->xmlFindTag($root_node->childNodes, $section); + if ($section_node) + { + $param_node = $this->xmlFindTag($section_node->childNodes, $parameter); + if ($param_node) + { + $param_value = $param_node->nodeValue; + } + } + } + + // If the parameter is also defined in the user config, it will override + // the system-wide value. + + if ($this->user) + { + $root_node = $this->user->documentElement; + $section_node = $this->xmlFindTag($root_node->childNodes, $section); + if ($section_node) + { + $param_node = $this->xmlFindTag($section_node->childNodes, $parameter); + if ($param_node) + { + $param_value = $param_node->nodeValue; + } + } + } + + return $param_value; + } +} + +?> diff --git a/src/phpclasses/gnucomo_config.php b/src/phpclasses/gnucomo_config.php index 507a462..139daa7 100644 --- a/src/phpclasses/gnucomo_config.php +++ b/src/phpclasses/gnucomo_config.php @@ -5,7 +5,14 @@ ** terms of the GNU General Public License, see the file COPYING. ***************************************************************************/ -require_once('configuration.class.php'); +if (substr(phpversion(), 0, 1) == "4") +{ + require_once('configuration.class.php'); +} +else +{ + require_once('configuration.class.php5'); +} /* /////////////////////////////////////////////////////////////////////////// -- 2.11.0