The method configuration::read() will also read
authorarjen <arjen>
Thu, 28 Aug 2003 12:16:33 +0000 (12:16 +0000)
committerarjen <arjen>
Thu, 28 Aug 2003 12:16:33 +0000 (12:16 +0000)
a user-specific configuration from the home directory if a
PHP script is not run through the web server.

src/phpclasses/configuration.class.php

index 30a1446..1aaaa66 100644 (file)
@@ -37,8 +37,8 @@ class configuration
 
    function configuration()
    {
-      $system = false;
-      $user   = false;
+      $this->system = false;
+      $this->user   = false;
    }
 
    function xmlFindTag($node, $tag)
@@ -60,6 +60,7 @@ class configuration
 
    function read($app_name)
    {
+      $this->system = false;
       $filename = "/etc/" . $app_name . ".conf";
       if (!is_readable($filename))
       {
@@ -81,10 +82,38 @@ class configuration
             }
          }
       }
-      else
+
+      $this->user = false;
+      if (isset($_ENV['HOME']) && $_ENV['HOME'] != '/')
       {
-         print("Configuration error: Configuration file for $app_name not found.<br>");
+         //  Read configurations from the user's homedir.
+
+         $filename = $_ENV['HOME'] . "/." . $app_name . ".conf";
+         if (is_readable($filename))
+         {
+
+            $this->user = xmldocfile($filename);
+
+            if ($this->user)
+            {
+               $root = $this->user->root();
+
+               if ($root->tagname != $app_name)
+               {
+                  print("Configuration error: Wrong configuration file.<br>");
+                  $this->user = false;
+               }
+            }
+         }
+         
+      }
+
+      if ($this->system == false && $this->user == false)
+      {
+         print("Configuration error: Configuration file for $app_name not found.<br>\n");
       }
+
+      return $this->system != false || $this->user != false;
    }
 
    function find_parameter($section, $parameter)
@@ -109,6 +138,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;
    }
 }