Added a PHP5 module for the configuration class
[gnucomo.git] / src / phpclasses / configuration.class.php5
1 <?php
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 ***************************************************************************/
7
8
9 /*
10 ///////////////////////////////////////////////////////////////////////////
11 //  NAME           : configuration
12 //  BASECLASS      :
13 //  MEMBERS        : 
14 //  OPERATORS      :
15 //  METHODS        : read
16 //
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.
26 //
27 //  RELATIONS      : 
28 //  SEE ALSO       :
29 //  LAST MODIFIED  : Nov 19, 2007
30 ///////////////////////////////////////////////////////////////////////////
31 */
32
33
34 class configuration
35 {
36    var   $system, $user;
37
38    function configuration()
39    {
40       $this->system = false;
41       $this->user   = false;
42    }
43
44    function xmlFindTag($node, $tag)
45    {
46       $element = false;
47       $i = 0;
48
49    // Assume we have PHP5
50       while (!$element && $i < $node->length)
51       {
52          if ($node->item($i)->nodeType == XML_ELEMENT_NODE && $node->item($i)->nodeName == $tag)
53          {
54             $element = $node->item($i);
55          }
56          $i++;
57       }
58
59
60       return $element;
61    }
62
63    function read($app_name)
64    {
65       $this->system = false;
66       $this->user = false;
67       $filename = "/etc/" . $app_name . ".conf";
68       if (!is_readable($filename))
69       {
70          $filename = "/usr/local/etc/" . $app_name . ".conf";
71       }
72       if (is_readable($filename))
73       {
74
75          $this->system = new DOMDocument();
76
77          if ($this->system->load($filename))
78          {
79             $root = $this->system->documentElement;
80
81             if ($root->tagName != $app_name)
82             {
83                print("Configuration error: Wrong configuration file.<br>");
84                $this->system = false;
85             }
86          }
87       }
88
89       if (isset($_ENV['HOME']) && $_ENV['HOME'] != '/')
90       {
91          //  Read configurations from the user's homedir.
92
93          $filename = $_ENV['HOME'] . "/." . $app_name . ".conf";
94          if (is_readable($filename))
95          {
96
97             $this->user = new DOMDocument();
98
99             if ($this->user->load($filename))
100             {
101                $root = $this->user->documentElement;
102
103                if ($root->tagName != $app_name)
104                {
105                   print("Configuration error: Wrong configuration file.<br>");
106                   $this->user = false;
107                }
108             }
109          }
110          
111       }
112
113       if ($this->system == false && $this->user == false)
114       {
115          print("Configuration error: Configuration file for $app_name not found.<br>\n");
116       }
117
118       return $this->system != false || $this->user != false;
119    }
120
121    function find_parameter($section, $parameter)
122    {
123       $param_value = "";
124
125       if ($this->system)
126       {
127          $root_node = $this->system->documentElement;
128          $section_node = $this->xmlFindTag($root_node->childNodes, $section);
129          if ($section_node)
130          {
131             $param_node = $this->xmlFindTag($section_node->childNodes, $parameter);
132             if ($param_node)
133             {
134                $param_value = $param_node->nodeValue;
135             }
136          }
137       }
138
139       //  If the parameter is also defined in the user config, it will override
140       //  the system-wide value.
141
142       if ($this->user)
143       {
144          $root_node = $this->user->documentElement;
145          $section_node = $this->xmlFindTag($root_node->childNodes, $section);
146          if ($section_node)
147          {
148             $param_node = $this->xmlFindTag($section_node->childNodes, $parameter);
149             if ($param_node)
150             {
151                $param_value = $param_node->nodeValue;
152             }
153          }
154       }
155
156       return $param_value;
157    }
158 }
159
160 ?>