//
// RELATIONS :
// SEE ALSO :
-// LAST MODIFIED : Jul 29, 2002
+// LAST MODIFIED : Nov 19, 2007
///////////////////////////////////////////////////////////////////////////
*/
function read($app_name)
{
$this->system = false;
+ $this->user = false;
$filename = "/etc/" . $app_name . ".conf";
if (!is_readable($filename))
{
if (is_readable($filename))
{
- $this->system = xmldocfile($filename);
+ $this->system = domxml_open_file($filename);
if ($this->system)
{
}
}
- $this->user = false;
if (isset($_ENV['HOME']) && $_ENV['HOME'] != '/')
{
// Read configurations from the user's homedir.
if (is_readable($filename))
{
- $this->user = xmldocfile($filename);
+ $this->user = domxml_open_file($filename);
if ($this->user)
{
--- /dev/null
+<?php
+/**************************************************************************
+** (c) Copyright 2002, Andromeda Technology & Automation
+** This is free software; you can redistribute it and/or modify it under the
+** terms of the GNU General Public License, see the file COPYING.
+***************************************************************************/
+
+
+/*
+///////////////////////////////////////////////////////////////////////////
+// NAME : configuration
+// BASECLASS :
+// MEMBERS :
+// OPERATORS :
+// METHODS : read
+//
+// DESCRIPTION : Handle configurational parameters for the application.
+// Many applications need some permanently stored configurational
+// data. The information is usually stored in two places: A system-
+// wide configuration file and a configuration file per user.
+// The content of the configuration file is in XML format.
+// The configuration base class takes care of finding the configuration
+// files, e.g. in /etc/app.conf or in /usr/loca/etc/app.conf
+// The config files are parsed with the gnome XML parser and a
+// framework is provided to find configurational items.
+//
+// RELATIONS :
+// SEE ALSO :
+// LAST MODIFIED : Nov 19, 2007
+///////////////////////////////////////////////////////////////////////////
+*/
+
+
+class configuration
+{
+ var $system, $user;
+
+ function configuration()
+ {
+ $this->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.<br>");
+ $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.<br>");
+ $this->user = false;
+ }
+ }
+ }
+
+ }
+
+ if ($this->system == false && $this->user == false)
+ {
+ print("Configuration error: Configuration file for $app_name not found.<br>\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;
+ }
+}
+
+?>