Spam scanning investigation
[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/local/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       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       $this->user = false;
65       $filename = "/etc/" . $app_name . ".conf";
66       if (!is_readable($filename))
67       {
68          $filename = "/usr/local/etc/" . $app_name . ".conf";
69       }
70       if (is_readable($filename))
71       {
72
73          $this->system = domxml_open_file($filename);
74
75          if ($this->system)
76          {
77             $root = $this->system->root();
78
79             if ($root->tagname != $app_name)
80             {
81                print("Configuration error: Wrong configuration file.<br>");
82                $this->system = false;
83             }
84          }
85       }
86
87       // First try the user's config in the current dir.
88       $filename = $app_name . ".conf";
89       if (!is_readable($filename))
90       {
91          print("Can not read $filename");
92          if (isset($_ENV['HOME']) && $_ENV['HOME'] != '/')
93          {
94             //  Read configurations from the user's homedir.
95
96             $filename = $_ENV['HOME'] . "/." . $app_name . ".conf";
97          }
98       }
99       if (is_readable($filename))
100       {
101
102          $this->user = domxml_open_file($filename);
103
104          if ($this->user)
105          {
106             $root = $this->user->root();
107
108             if ($root->tagname != $app_name)
109             {
110                print("Configuration error: Wrong configuration file.<br>");
111                $this->user = false;
112             }
113          }
114       }
115          
116
117       if ($this->system == false && $this->user == false)
118       {
119          print("Configuration error: Configuration file for $app_name is not found.<br>\n");
120       }
121
122       return $this->system != false || $this->user != false;
123    }
124
125    function find_parameter($section, $parameter)
126    {
127       $param_value = "";
128
129       if ($this->system)
130       {
131          $root_node = $this->system->root();
132          $section_node = $this->xmlFindTag($root_node->children(), $section);
133          if ($section_node)
134          {
135             $param_node = $this->xmlFindTag($section_node->children(), $parameter);
136             if ($param_node)
137             {
138                $param_node = $param_node->children();
139             }
140             if ($param_node && $param_node[0]->type == XML_TEXT_NODE)
141             {
142                $param_value = $param_node[0]->content;
143             }
144          }
145       }
146
147       //  If the parameter is also defined in the user config, it will override
148       //  the system-wide value.
149
150       if ($this->user)
151       {
152          $root_node = $this->user->root();
153          $section_node = $this->xmlFindTag($root_node->children(), $section);
154          if ($section_node)
155          {
156             $param_node = $this->xmlFindTag($section_node->children(), $parameter);
157             if ($param_node)
158             {
159                $param_node = $param_node->children();
160             }
161             if ($param_node && $param_node[0]->type == XML_TEXT_NODE)
162             {
163                $param_value = $param_node[0]->content;
164             }
165          }
166       }
167
168       return $param_value;
169    }
170 }
171
172 ?>