Source for file Config.php

Documentation is available at Config.php

  1. <?php
  2.  
  3. /**
  4.  * Load and save configuration files
  5.  *
  6.  * This class currently supports the following config file formats:
  7.  * - .INI
  8.  *
  9.  * This class can be used by any module as follows:-
  10.  * - $this->exec_module_action('Config', 'read_ini_file', $filename);
  11.  * - $this->exec_module_action('Config', 'write_ini_file', $filename, $config);
  12.  */
  13. class Config extends Module {
  14.     /**
  15.      * This method reads configuration values in the specified filename.
  16.      *
  17.      * This method uses the PHP function parse_ini_file() to load the configuration.
  18.      * It always processes sections.
  19.      *
  20.      * @param string Name of the file
  21.      *
  22.      * @return array An associative array with the configuration
  23.      */
  24.     function read_ini_file($filename{
  25.         return parse_ini_file($filenametrue);
  26.     }
  27.  
  28.     /**
  29.      * This method writes the configuration values specified to the filename.
  30.      *
  31.      * The file data is overwritten if the file already exists. Implementation
  32.      * obtained from the comments under the parse_ini_file() page on PHP.net.
  33.      *
  34.      * @param string Name of the file
  35.      * @param array Associative array with the configuration
  36.      *
  37.      * @return bool Returns true on success, false otherwise
  38.      */
  39.     function write_ini_file($filename$config{
  40.         $content '';
  41.         $sections '';
  42.  
  43.         foreach ($config as $key => $item{
  44.             if (is_array($item)) {
  45.                 $sections .= "\n[{$key}]\n";
  46.                 foreach ($item as $key2 => $item2{
  47.                     if (is_numeric($item2|| is_bool($item2))
  48.                         $sections .= "{$key2} = {$item2}\n";
  49.                     else
  50.                         $sections .= "{$key2} = \"{$item2}\"\n";
  51.                 }
  52.             else {
  53.                 if(is_numeric($item|| is_bool($item))
  54.                     $content .= "{$key} = {$item}\n";
  55.                 else
  56.                     $content .= "{$key} = \"{$item}\"\n";
  57.             }
  58.         }
  59.  
  60.         $content .= $sections;
  61.  
  62.         if (!$handle fopen($filename'w'))
  63.             return false;
  64.  
  65.         if (!fwrite($handle$content))
  66.             return false;
  67.  
  68.         fclose($handle);
  69.         return true;
  70.     }
  71. }
  72.  
  73. ?>

Documentation generated on Sat, 23 Jun 2007 21:28:18 -0500 by phpDocumentor 1.3.2