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;
}
}
}
// First try the user's config in the current dir.
$filename = $app_name . ".conf";
if (!is_readable($filename))
{
print("Can not read $filename");
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;
}
}
?>