Configuration from current directory
[gnucomo.git] / src / phpclasses / configuration.class.php
index fa25a7d..84ca56b 100644 (file)
 //                   wide configuration file and a configuration file per user.
 //                   The content of the configuration file is in XML format.
 //                   The configuration base class takes care of finding the configuration
-//                   files, e.g. in /etc/app.conf or in /usr/loca/etc/app.conf
+//                   files, e.g. in /etc/app.conf or in /usr/local/etc/app.conf
 //                   The config files are parsed with the gnome XML parser and a
 //                   framework is provided to find configurational items.
 //
 //  RELATIONS      : 
 //  SEE ALSO       :
-//  LAST MODIFIED  : Jul 29, 2002
+//  LAST MODIFIED  : Nov 19, 2007
 ///////////////////////////////////////////////////////////////////////////
 */
 
@@ -37,8 +37,8 @@ class configuration
 
    function configuration()
    {
-      $system = false;
-      $user   = false;
+      $this->system = false;
+      $this->user   = false;
    }
 
    function xmlFindTag($node, $tag)
@@ -60,29 +60,66 @@ class configuration
 
    function read($app_name)
    {
+      $this->system = false;
+      $this->user = false;
       $filename = "/etc/" . $app_name . ".conf";
-      $this->system = xmldocfile($filename);
-
-      if (!$this->system)
+      if (!is_readable($filename))
       {
          $filename = "/usr/local/etc/" . $app_name . ".conf";
-         $this->system = xmldocfile($filename);
       }
+      if (is_readable($filename))
+      {
 
-      if ($this->system)
+         $this->system = domxml_open_file($filename);
+
+         if ($this->system)
+         {
+            $root = $this->system->root();
+
+            if ($root->tagname != $app_name)
+            {
+               print("Configuration error: Wrong configuration file.<br>");
+               $this->system = false;
+            }
+         }
+      }
+
+      // First try the user's config in the current dir.
+      $filename = $app_name . ".conf";
+      if (!is_readable($filename))
+      {
+         print("Can not read $filename");
+         if (isset($_ENV['HOME']) && $_ENV['HOME'] != '/')
+         {
+            //  Read configurations from the user's homedir.
+
+            $filename = $_ENV['HOME'] . "/." . $app_name . ".conf";
+         }
+      }
+      if (is_readable($filename))
       {
-         $root = $this->system->root();
 
-         if ($root->tagname != $app_name)
+         $this->user = domxml_open_file($filename);
+
+         if ($this->user)
          {
-            print("Configuration error: Wrong configuration file.<br>");
-            $this->system = false;
+            $root = $this->user->root();
+
+            if ($root->tagname != $app_name)
+            {
+               print("Configuration error: Wrong configuration file.<br>");
+               $this->user = false;
+            }
          }
       }
-      else
+         
+
+      if ($this->system == false && $this->user == false)
       {
-         print("Configuration error: Configuration file for $app_name not found.<br>");
+         print("Configuration error: Configuration file for $app_name is not found.<br>\n");
       }
+
+      return $this->system != false || $this->user != false;
    }
 
    function find_parameter($section, $parameter)
@@ -107,6 +144,27 @@ class configuration
          }
       }
 
+      //  If the parameter is also defined in the user config, it will override
+      //  the system-wide value.
+
+      if ($this->user)
+      {
+         $root_node = $this->user->root();
+         $section_node = $this->xmlFindTag($root_node->children(), $section);
+         if ($section_node)
+         {
+            $param_node = $this->xmlFindTag($section_node->children(), $parameter);
+            if ($param_node)
+            {
+               $param_node = $param_node->children();
+            }
+            if ($param_node && $param_node[0]->type == XML_TEXT_NODE)
+            {
+               $param_value = $param_node[0]->content;
+            }
+         }
+      }
+
       return $param_value;
    }
 }