The method configuration::read() will also read
[gnucomo.git] / src / phpclasses / configuration.class.php
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  : Jul 29, 2002
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       while (!$element && $i < sizeof($node))
50       {
51          if ($node[$i]->type == XML_ELEMENT_NODE && $node[$i]->tagname == $tag)
52          {
53             $element = $node[$i];
54          }
55          $i++;
56       }
57
58       return $element;
59    }
60
61    function read($app_name)
62    {
63       $this->system = false;
64       $filename = "/etc/" . $app_name . ".conf";
65       if (!is_readable($filename))
66       {
67          $filename = "/usr/local/etc/" . $app_name . ".conf";
68       }
69       if (is_readable($filename))
70       {
71
72          $this->system = xmldocfile($filename);
73
74          if ($this->system)
75          {
76             $root = $this->system->root();
77
78             if ($root->tagname != $app_name)
79             {
80                print("Configuration error: Wrong configuration file.<br>");
81                $this->system = false;
82             }
83          }
84       }
85
86       $this->user = false;
87       if (isset($_ENV['HOME']) && $_ENV['HOME'] != '/')
88       {
89          //  Read configurations from the user's homedir.
90
91          $filename = $_ENV['HOME'] . "/." . $app_name . ".conf";
92          if (is_readable($filename))
93          {
94
95             $this->user = xmldocfile($filename);
96
97             if ($this->user)
98             {
99                $root = $this->user->root();
100
101                if ($root->tagname != $app_name)
102                {
103                   print("Configuration error: Wrong configuration file.<br>");
104                   $this->user = false;
105                }
106             }
107          }
108          
109       }
110
111       if ($this->system == false && $this->user == false)
112       {
113          print("Configuration error: Configuration file for $app_name not found.<br>\n");
114       }
115
116       return $this->system != false || $this->user != false;
117    }
118
119    function find_parameter($section, $parameter)
120    {
121       $param_value = "";
122
123       if ($this->system)
124       {
125          $root_node = $this->system->root();
126          $section_node = $this->xmlFindTag($root_node->children(), $section);
127          if ($section_node)
128          {
129             $param_node = $this->xmlFindTag($section_node->children(), $parameter);
130             if ($param_node)
131             {
132                $param_node = $param_node->children();
133             }
134             if ($param_node && $param_node[0]->type == XML_TEXT_NODE)
135             {
136                $param_value = $param_node[0]->content;
137             }
138          }
139       }
140
141       //  If the parameter is also defined in the user config, it will override
142       //  the system-wide value.
143
144       if ($this->user)
145       {
146          $root_node = $this->user->root();
147          $section_node = $this->xmlFindTag($root_node->children(), $section);
148          if ($section_node)
149          {
150             $param_node = $this->xmlFindTag($section_node->children(), $parameter);
151             if ($param_node)
152             {
153                $param_node = $param_node->children();
154             }
155             if ($param_node && $param_node[0]->type == XML_TEXT_NODE)
156             {
157                $param_value = $param_node[0]->content;
158             }
159          }
160       }
161
162       return $param_value;
163    }
164 }
165
166 ?>