system = false;
$this->user = false;
}
function xmlFindTag($node, $tag)
{
$element = false;
$i = 0;
while (!$element && $i < sizeof($node))
{
if ($node[$i]->type == XML_ELEMENT_NODE && $node[$i]->tagname == $tag)
{
$element = $node[$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 = domxml_open_file($filename);
if ($this->system)
{
$root = $this->system->root();
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 = domxml_open_file($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)
{
$param_value = "";
if ($this->system)
{
$root_node = $this->system->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;
}
}
}
// 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;
}
}
?>