Source for file Settings.php

Documentation is available at Settings.php

  1. <?php
  2. /**
  3.  * Settings.php
  4.  *
  5.  * PHP version 5
  6.  *
  7.  * LICENSE: This program is free software; you can redistribute it and/or
  8.  * modify it under the terms of the GNU General Public License as published
  9.  * by the Free Software Foundation.
  10.  *
  11.  * @package                     FreeNAC
  12.  * @author                      Hector Ortiz (FreeNAC Core Team)
  13.  * @copyright                   2007 FreeNAC
  14.  * @license                     http://www.gnu.org/copyleft/gpl.html   GNU Public License Version 2
  15.  * @version                     SVN: $Id$
  16.  * @link                        http://www.freenac.net
  17.  *
  18.  */
  19.  
  20. /**
  21.  * Define the Settings class which is a Singleton
  22.  * used to hold global variables stored in the configuration files and running script.
  23. */
  24. class Settings
  25. {
  26.    private $props=array();
  27.    private static $instance;
  28.    
  29.    /**
  30.    * Set our internal array to contain all variabled defined in the config table
  31.    */
  32.    private function __construct()
  33.    {
  34.       db_connect();
  35.       $query="select * from config";                        # Query 
  36.       $res=mysql_query($query);
  37.       if ($res)
  38.       {
  39.          while ($result=mysql_fetch_array($resMYSQL_ASSOC))            # Iterate over all information found
  40.          {
  41.             switch ($result['type'])                        # Now doing casts according to the field 'type'
  42.             {                                    # And store them in an internal array
  43.                case 'integer':$temp[$result['name']]=(integer)$result['value'];
  44.                   break;
  45.                case 'boolean':
  46.                   {
  47.                      if ((strcasecmp($result['value'],'true')==0|| (strcasecmp($result['value'],'1')==0))
  48.                         $temp[$result['name']]=(boolean)true;
  49.                      else
  50.                         $temp[$result['name']]=(boolean)false;
  51.                   }
  52.                   break;
  53.                case 'float':$temp[$result['name']]=(float)$result['value'];
  54.                   break;
  55.                case 'double':$temp[$result['name']]=(double)$result['value'];
  56.                   break;
  57.                case 'string':$temp[$result['name']]=(string)$result['value'];
  58.                   break;
  59.                case 'array':$temp[$result['name']][]=$result['value'];
  60.                   break;
  61.             }
  62.          }
  63.          $this->props=$temp;                            # Et voila, copy it to our internal array
  64.       }
  65.       else
  66.          return;                                # Nothing to return
  67.    }
  68.  
  69.    /**
  70.    * Get instance of this class
  71.    * @return object     Current instance
  72.    */
  73.    public static function getInstance()
  74.    {
  75.       if (empty(self::$instance))                        # Is there an instance of this class?
  76.       {
  77.          self::$instance=new Settings();                    # No, then create it
  78.       }
  79.       return self::$instance;                            # Yes, return it
  80.    }
  81.  
  82.    /**
  83.    * Get the value of one property if it exists
  84.    * @param mixed $key          Property to lookup
  85.    * @return mixed              The value of the wanted property, or false if such a property doesn't exist
  86.    */
  87.    public function __get($key)                            # Get the value of one var
  88.    {
  89.       if (isset($this->props[$key]))
  90.          return $this->props[$key];
  91.       else
  92.          return false;
  93.    }
  94.  
  95.    /**
  96.    * Return all properties defined. This method is here only for debugging purposes, please delete it after
  97.    * @return array      All properties present
  98.    */
  99.    public function getAllProps()                        # Get our inner array
  100.    {
  101.       return $this->props;
  102.    }
  103. }
  104.  
  105. ?>

Documentation generated on Mon, 17 Nov 2008 01:10:47 +0100 by phpDocumentor 1.4.0