2 /**************************************************************************
3 ** (c) Copyright 2002, Andromeda Technology & Automation
4 ** This is free software; you can redistribute it and/or modify it under the
5 ** terms of the GNU General Public License, see the file COPYING.
6 ***************************************************************************/
10 ///////////////////////////////////////////////////////////////////////////
11 // NAME : configuration
17 // DESCRIPTION : Handle configurational parameters for the application.
18 // Many applications need some permanently stored configurational
19 // data. The information is usually stored in two places: A system-
20 // wide configuration file and a configuration file per user.
21 // The content of the configuration file is in XML format.
22 // The configuration base class takes care of finding the configuration
23 // files, e.g. in /etc/app.conf or in /usr/loca/etc/app.conf
24 // The config files are parsed with the gnome XML parser and a
25 // framework is provided to find configurational items.
29 // LAST MODIFIED : Nov 19, 2007
30 ///////////////////////////////////////////////////////////////////////////
38 function configuration()
40 $this->system = false;
44 function xmlFindTag($node, $tag)
49 // Assume we have PHP5
50 while (!$element && $i < $node->length)
52 if ($node->item($i)->nodeType == XML_ELEMENT_NODE && $node->item($i)->nodeName == $tag)
54 $element = $node->item($i);
63 function read($app_name)
65 $this->system = false;
67 $filename = "/etc/" . $app_name . ".conf";
68 if (!is_readable($filename))
70 $filename = "/usr/local/etc/" . $app_name . ".conf";
72 if (is_readable($filename))
75 $this->system = new DOMDocument();
77 if ($this->system->load($filename))
79 $root = $this->system->documentElement;
81 if ($root->tagName != $app_name)
83 print("Configuration error: Wrong configuration file.<br>");
84 $this->system = false;
89 // First try the user's config in the current dir.
90 $filename = $app_name . ".conf";
91 if (!is_readable($filename))
93 print("Can not read $filename");
94 if (isset($_ENV['HOME']) && $_ENV['HOME'] != '/')
96 // Read configurations from the user's homedir.
98 $filename = $_ENV['HOME'] . "/." . $app_name . ".conf";
101 if (is_readable($filename))
104 $this->user = new DOMDocument();
105 if ($this->user->load($filename))
107 $root = $this->user->documentElement;
109 if ($root->tagName != $app_name)
111 print("Configuration error: Wrong configuration file.<br>");
118 if ($this->system == false && $this->user == false)
120 print("Configuration error: Configuration file for $app_name not found.<br>\n");
123 return $this->system != false || $this->user != false;
126 function find_parameter($section, $parameter)
132 $root_node = $this->system->documentElement;
133 $section_node = $this->xmlFindTag($root_node->childNodes, $section);
136 $param_node = $this->xmlFindTag($section_node->childNodes, $parameter);
139 $param_value = $param_node->nodeValue;
144 // If the parameter is also defined in the user config, it will override
145 // the system-wide value.
149 $root_node = $this->user->documentElement;
150 $section_node = $this->xmlFindTag($root_node->childNodes, $section);
153 $param_node = $this->xmlFindTag($section_node->childNodes, $parameter);
156 $param_value = $param_node->nodeValue;