Creation of gcm_input and a first approach to a web interface
[gnucomo.git] / src / phpclasses / configuration.class.php
1 <?php
2
3
4 /*
5 ///////////////////////////////////////////////////////////////////////////
6 //  NAME           : configuration
7 //  BASECLASS      :
8 //  MEMBERS        : 
9 //  OPERATORS      :
10 //  METHODS        : read
11 //
12 //  DESCRIPTION    : Handle configurational parameters for the application.
13 //                   Many applications need some permanently stored configurational
14 //                   data. The information is usually stored in two places: A system-
15 //                   wide configuration file and a configuration file per user.
16 //                   The content of the configuration file is in XML format.
17 //                   The configuration base class takes care of finding the configuration
18 //                   files, e.g. in /etc/app.conf or in /usr/loca/etc/app.conf
19 //                   The config files are parsed with the gnome XML parser and a
20 //                   framework is provided to find configurational items.
21 //
22 //  RELATIONS      : 
23 //  SEE ALSO       :
24 //  LAST MODIFIED  : Jul 29, 2002
25 ///////////////////////////////////////////////////////////////////////////
26 */
27
28
29 class configuration
30 {
31    var   $system, $user;
32
33    function configuration()
34    {
35       $system = false;
36       $user   = false;
37    }
38
39    function xmlFindTag($node, $tag)
40    {
41       $element = false;
42       $i = 0;
43
44       while (!$element && $i < sizeof($node))
45       {
46          if ($node[$i]->type == XML_ELEMENT_NODE && $node[$i]->tagname == $tag)
47          {
48             $element = $node[$i];
49          }
50          $i++;
51       }
52
53       return $element;
54    }
55
56    function read($app_name)
57    {
58       $filename = "/etc/" . $app_name . ".conf";
59       $this->system = xmldocfile($filename);
60
61       if (!$this->system)
62       {
63          $filename = "/usr/local/etc/" . $app_name . ".conf";
64          $this->system = xmldocfile($filename);
65       }
66
67       if ($this->system)
68       {
69          $root = $this->system->root();
70
71          if ($root->tagname != $app_name)
72          {
73             print("Configuration error: Wrong configuration file.<br>");
74             $this->system = false;
75          }
76       }
77       else
78       {
79          print("Configuration error: Configuration file for $app_name not found.<br>");
80       }
81    }
82
83    function find_parameter($section, $parameter)
84    {
85       $param_value = "";
86
87       if ($this->system)
88       {
89          $root_node = $this->system->root();
90          $section_node = $this->xmlFindTag($root_node->children(), $section);
91          if ($section_node)
92          {
93             $param_node = $this->xmlFindTag($section_node->children(), $parameter);
94             if ($param_node)
95             {
96                $param_node = $param_node->children();
97             }
98             if ($param_node && $param_node[0]->type == XML_TEXT_NODE)
99             {
100                $param_value = $param_node[0]->content;
101             }
102          }
103       }
104
105       return $param_value;
106    }
107 }
108
109 ?>