#native_company# #native_desc#
#native_cta#

Class for reading INI files

By Victor M Varela
on October 30, 2000

Version: 1.03

Type: Class

Category: File Management

License: GNU Library Public License

Description: Simple class that supports reading sections, single values, and arrays from INI files.

<?
//  filename: class.inifile.php
//  author:   victor m varela ([email protected])
//  comment:  *.ini file reader for PHP
//  license:  LGPL
//  date:     mi oct 25 10:20:10 CEST 2000
//
//  vie oct 27 12:25:48 CEST 2000
//     1.01  recognize some bool values (true, false, yes, no, on, off)
//
//  vie oct 27 16:00:36 CEST 2000
//     1.02  bugfix: false value was returning default value
//
//  lun oct 30 11:55:38 CET 2000
//     1.03  cached engine added
//
//  valid formats:
//
//     [my-section]
//     # single values
//     var1 = value1
//     var2=value 2
//     var3 = 'value 3'
//     ; if we use values between ' or ", they are not trimmed
//     var4="  value 4"
//
//     ; setting an array
//     var_array1 = this is var_array1[0]
//     var_array1 = this is var_array1[1]
//     var_array1 = this is var_array1[2]
//

// cached values
$iniCachedValues = array();
$iniCachedSections = array();
$iniCachedFilenames = array();

class IniFile {

   // private: do not use;
   var $filename;

   // constructor
   function IniFile ($filename) 
   {
      global $iniCachedValues, $iniCachedSections, $iniCachedFilenames;
      $section = "";
      $this->filename = $filename;
      if (file_exists($filename) and ! isset($iniCachedFilenames[$filename])) {
         $iniCachedFilenames[$filename] = $filename;
         $lines = file($filename);
         for ($i = 0; $i < count($lines); $i++) {
            $lines[$i] = trim($lines[$i]);
            if ($lines[$i][0] == ';' || $lines[$i][0] == '#') {
               // it's a commment
               continue;
            } elseif (ereg("^[(.*)]", $lines[$i], $regs)) {
               // new section
               $section = strtolower(trim($regs[1]));
               $iniCachedSections[$filename][] = trim($regs[1]);
            } elseif (ereg("^(.*) = (.*)", $lines[$i], $regs) || ereg("^(.*)=(.*)", $lines[$i], $regs)) {
               // setting variable
               $variable = strtolower(trim($regs[1]));
               $value = trim($regs[2]);
               if (ereg("^["'](.*)["']$", $value, $regs)) {
                  $value = $regs[1];
               }
               $lower_value = strtolower($value);
               if (in_array($lower_value, array('true', 'yes', 'on'))) {
                  $value = 1;
               }
               elseif (in_array($lower_value, array('false', 'no', 'off'))) {
                  $value = 0;
               }
               if (! isset($iniCachedValues[$filename][$section][$variable])) {
                  // new variable
                  $iniCachedValues[$filename][$section][$variable] = $value;
               }
               elseif (gettype($iniCachedValues[$filename][$section][$variable]) == "array") {
                  // append new element to array
                  $iniCachedValues[$filename][$section][$variable][] = $value;
               }
               else {
                  // two values for a single variable, convert to array
                  $ovalue = $iniCachedValues[$filename][$section][$variable];
                  $iniCachedValues[$filename][$section][$variable] = array($ovalue,$value);
               }
            }
         }
      }
   }

   // returns file name
   function fileName () { return $this->filename;  }

   // returns all sections
   function readSections () { 
       global $iniCachedSections;
       echo serialize($iniCachedSections);
       return $iniCachedSections[$this->filename];  
   }

   // returns all (variable,value) pairs for a section
   function readSection ($section) {
       global $iniCachedValues;
       return $iniCachedValues[$this->filename][strtolower($section)]; 
   }

   // returns a value
   function value($section, $variable, $default)
   {
      global $iniCachedValues;
      $section = strtolower($section);
      $variable = strtolower($variable);
      if (isset($iniCachedValues[$this->filename][$section][$variable])) {
         return $iniCachedValues[$this->filename][$section][$variable];
      }
      else {
         return $default;
      }
   }
}

?>