Added a PHP5 module for the configuration class
authorarjen <arjen>
Wed, 21 Nov 2007 15:27:48 +0000 (15:27 +0000)
committerarjen <arjen>
Wed, 21 Nov 2007 15:27:48 +0000 (15:27 +0000)
src/phpclasses/configuration.class.php
src/phpclasses/configuration.class.php5 [new file with mode: 0644]
src/phpclasses/gnucomo_config.php

index 1aaaa66..4f0c2a5 100644 (file)
@@ -26,7 +26,7 @@
 //
 //  RELATIONS      : 
 //  SEE ALSO       :
-//  LAST MODIFIED  : Jul 29, 2002
+//  LAST MODIFIED  : Nov 19, 2007
 ///////////////////////////////////////////////////////////////////////////
 */
 
@@ -61,6 +61,7 @@ class configuration
    function read($app_name)
    {
       $this->system = false;
+      $this->user = false;
       $filename = "/etc/" . $app_name . ".conf";
       if (!is_readable($filename))
       {
@@ -69,7 +70,7 @@ class configuration
       if (is_readable($filename))
       {
 
-         $this->system = xmldocfile($filename);
+         $this->system = domxml_open_file($filename);
 
          if ($this->system)
          {
@@ -83,7 +84,6 @@ class configuration
          }
       }
 
-      $this->user = false;
       if (isset($_ENV['HOME']) && $_ENV['HOME'] != '/')
       {
          //  Read configurations from the user's homedir.
@@ -92,7 +92,7 @@ class configuration
          if (is_readable($filename))
          {
 
-            $this->user = xmldocfile($filename);
+            $this->user = domxml_open_file($filename);
 
             if ($this->user)
             {
diff --git a/src/phpclasses/configuration.class.php5 b/src/phpclasses/configuration.class.php5
new file mode 100644 (file)
index 0000000..68c529e
--- /dev/null
@@ -0,0 +1,160 @@
+<?php
+/**************************************************************************
+**  (c) Copyright 2002, Andromeda Technology & Automation
+** This is free software; you can redistribute it and/or modify it under the
+** terms of the GNU General Public License, see the file COPYING.
+***************************************************************************/
+
+
+/*
+///////////////////////////////////////////////////////////////////////////
+//  NAME           : configuration
+//  BASECLASS      :
+//  MEMBERS        : 
+//  OPERATORS      :
+//  METHODS        : read
+//
+//  DESCRIPTION    : Handle configurational parameters for the application.
+//                   Many applications need some permanently stored configurational
+//                   data. The information is usually stored in two places: A system-
+//                   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
+//                   The config files are parsed with the gnome XML parser and a
+//                   framework is provided to find configurational items.
+//
+//  RELATIONS      : 
+//  SEE ALSO       :
+//  LAST MODIFIED  : Nov 19, 2007
+///////////////////////////////////////////////////////////////////////////
+*/
+
+
+class configuration
+{
+   var   $system, $user;
+
+   function configuration()
+   {
+      $this->system = false;
+      $this->user   = false;
+   }
+
+   function xmlFindTag($node, $tag)
+   {
+      $element = false;
+      $i = 0;
+
+   // Assume we have PHP5
+      while (!$element && $i < $node->length)
+      {
+         if ($node->item($i)->nodeType == XML_ELEMENT_NODE && $node->item($i)->nodeName == $tag)
+         {
+            $element = $node->item($i);
+         }
+         $i++;
+      }
+
+
+      return $element;
+   }
+
+   function read($app_name)
+   {
+      $this->system = false;
+      $this->user = false;
+      $filename = "/etc/" . $app_name . ".conf";
+      if (!is_readable($filename))
+      {
+         $filename = "/usr/local/etc/" . $app_name . ".conf";
+      }
+      if (is_readable($filename))
+      {
+
+         $this->system = new DOMDocument();
+
+         if ($this->system->load($filename))
+         {
+            $root = $this->system->documentElement;
+
+            if ($root->tagName != $app_name)
+            {
+               print("Configuration error: Wrong configuration file.<br>");
+               $this->system = false;
+            }
+         }
+      }
+
+      if (isset($_ENV['HOME']) && $_ENV['HOME'] != '/')
+      {
+         //  Read configurations from the user's homedir.
+
+         $filename = $_ENV['HOME'] . "/." . $app_name . ".conf";
+         if (is_readable($filename))
+         {
+
+            $this->user = new DOMDocument();
+
+            if ($this->user->load($filename))
+            {
+               $root = $this->user->documentElement;
+
+               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)
+   {
+      $param_value = "";
+
+      if ($this->system)
+      {
+         $root_node = $this->system->documentElement;
+         $section_node = $this->xmlFindTag($root_node->childNodes, $section);
+         if ($section_node)
+         {
+            $param_node = $this->xmlFindTag($section_node->childNodes, $parameter);
+            if ($param_node)
+            {
+               $param_value = $param_node->nodeValue;
+            }
+         }
+      }
+
+      //  If the parameter is also defined in the user config, it will override
+      //  the system-wide value.
+
+      if ($this->user)
+      {
+         $root_node = $this->user->documentElement;
+         $section_node = $this->xmlFindTag($root_node->childNodes, $section);
+         if ($section_node)
+         {
+            $param_node = $this->xmlFindTag($section_node->childNodes, $parameter);
+            if ($param_node)
+            {
+               $param_value = $param_node->nodeValue;
+            }
+         }
+      }
+
+      return $param_value;
+   }
+}
+
+?>
index 507a462..139daa7 100644 (file)
@@ -5,7 +5,14 @@
 ** terms of the GNU General Public License, see the file COPYING.
 ***************************************************************************/
 
-require_once('configuration.class.php');
+if (substr(phpversion(), 0, 1) == "4")
+{
+   require_once('configuration.class.php');
+}
+else
+{
+   require_once('configuration.class.php5');
+}
 
 /*
 ///////////////////////////////////////////////////////////////////////////