<?php
 
require_once 'PEAR.php';
 
 
/**
 
 * Class parses ini-like files using  PHP's parse_ini_file function
 
 * Also you can make new config and then write it to file.
 
 *
 
 * Ini-file consist of Sections and Variables.
 
 *
 
 * Sections are obligatory.
 
 *
 
 * Comments starts with ";"
 
 * Each section should be placed in square brackets,
 
 * for example:
 
 *     [Test section].
 
 * Each variable should have a name and value divided by "=",
 
 * for example:
 
 *     myvar = my value
 
 *
 
 * Don't forget to include IniConfigDefines.php - file
 
 * with error codes.
 
 *
 
 * @author   Eugene Panin <varenich@yahoo.com>
 
 * @home     http://www.tortuga.pp.ru
 
 * @package  IniConfig
 
 * @version  1.0
 
 * @access   public
 
 */
 
class IniConfig extends PEAR {
 
 
  /**
 
   * Output window name
 
   *
 
   * @var      string
 
   * @access   private
 
   */
 
  var $_fileName = '';
 
 
  /**
 
   * Parsed data
 
   *
 
   * @var      array
 
   * @access   private
 
   */
 
  var $_data = array();
 
 
  /**
 
   * Did data parsed
 
   *
 
   * @var      boolean
 
   * @access   private
 
   */
 
  var $_parsed = false;
 
 
  /**
 
   * Parses file
 
   *
 
   * @return object PEAR_Error
 
   * @access private
 
   */
 
  function _parse() {
 
    $this->_data = @parse_ini_file ($this->_fileName, true);
 
    if (!$this->_data) return $this->raiseError("Can't parse configuration file at ".get_class($this).' on line '.__LINE__,CANT_PARSE,'');
 
    $this->_parsed = true;
 
    return true;
 
  }
 
 
  /**
 
   * Selects configuration file and parses it
 
   *
 
   * @return object PEAR_Error
 
   * @access public
 
   */
 
  function selectFile($fileName) {
 
    $this->_parsed = false;
 
    if (!file_exists($fileName)) return $this->raiseError("Configuration file doesn't exist at ".get_class($this).' on line '.__LINE__,FILE_NOT_EXISTS,'');
 
    $this->_fileName = $fileName;
 
    $err = $this->_parse();
 
    if (PEAR::isError($err)) return $err;
 
    return true;
 
  }
 
 
  /**
 
   * Return specified cariable form specified section
 
   *
 
   * @param string $section Section name
 
   * @param string $key Variable name
 
   * @return string Variable value or PEAR::Error in case of error
 
   * @access public
 
   */
 
  function getVar($section,$key) {
 
    if (!$this->_parsed) return $this->raiseError("Data not parsed at ".get_class($this).' on line '.__LINE__,DATA_NOT_PARSED,'');
 
    if ($this->_data["$section"]["$key"]) {
 
      return $this->_data["$section"]["$key"];
 
    }
 
    else {
 
      if (!$this->_data["$section"]) return $this->raiseError("Section doesn't exist at ".get_class($this).' on line '.__LINE__,SECTION_NOT_EXISTS,'');
 
      return $this->raiseError("Variable doesn't exist at ".get_class($this).' on line '.__LINE__,VARIABLE_NOT_EXISTS,'');
 
    }
 
  }
 
 
  /**
 
   * Return all variables in section
 
   *
 
   * @param string $section Section name
 
   * @return array Variables or PEAR::Error in case of error
 
   * @access public
 
   */
 
  function getSectionVars($section) {
 
    if (!$this->_parsed) return $this->raiseError("Data not parsed at ".get_class($this).' on line '.__LINE__,DATA_NOT_PARSED,'');
 
    if ($this->_data["$section"]) {
 
      return $this->_data["$section"];
 
    }
 
    else {
 
      return $this->raiseError("Section doesn't exist at ".get_class($this).' on line '.__LINE__,SECTION_NOT_EXISTS,'');
 
    }
 
  }
 
 
  /**
 
   * Return all section names
 
   *
 
   * @return array Section names or PEAR::Error in case of error
 
   * @access public
 
   */
 
  function getSections() {
 
    if (!$this->_parsed) return $this->raiseError("Data not parsed at ".get_class($this).' on line '.__LINE__,DATA_NOT_PARSED,'');
 
    return array_keys($this->_data);
 
  }
 
 
  /**
 
   * Writes data to file
 
   *
 
   * @param string $fileName File name
 
   * @return object PEAR::Error Error
 
   * @access public
 
   */
 
  function writeToFile($fileName){
 
    $st="";
 
    foreach(array_keys($this->_data) as $sn){
 
      $st.="[".$sn."]"."\n";
 
      foreach($this->_data["$sn"] as $key => $value){
 
    $st.="$key = $value\n";
 
      }
 
    }
 
    $f = fopen($fileName,"w");
 
    if (!$f) return $this->raiseError("Can't open file for writing at ".get_class($this).' on line '.__LINE__,CANT_WRITE,'');
 
    fwrite($f,$st);
 
    fclose($f);
 
  }
 
 
  /**
 
   * Adds new section
 
   *
 
   * @param string $section Section name
 
   * @return object PEAR::Error Error
 
   * @access public
 
   */
 
  function addSection($section) {
 
    if ($this->_data["$section"]) return $this->raiseError("Section already exists at ".get_class($this).' on line '.__LINE__,SECTION_EXISTS,'');
 
    $this->_data["$section"] = array();
 
  }
 
 
  /**
 
   * Sets value for variable. Creates new variable if it doesn't exist
 
   *
 
   * @param string $section Section name
 
   * @param string $key Variable name
 
   * @param string $val Variable value. Default is ''
 
   * @return object PEAR::Error Error
 
   * @access public
 
   */
 
  function setVar($section,$key,$val='') {
 
    if (!$this->_data["$section"]) return $this->raiseError("Section doesn't exist at ".get_class($this).' on line '.__LINE__,SECTION_NOT_EXISTS,'');
 
    $this->_data["$section"]["$key"] = $val;
 
  }
 
 
  /**
 
   * Sets section variables
 
   *
 
   * @param string $section Section name
 
   * @param array $vars Section variables (hash). Default is empty hash
 
   * @return object PEAR::Error Error
 
   * @access public
 
   */
 
  function setSectionVars($section,$vars=array()) {
 
    if (!$this->_data["$section"]) return $this->raiseError("Section doesn't exist at ".get_class($this).' on line '.__LINE__,SECTION_NOT_EXISTS,'');
 
    $this->_data["$section"] = $vars;
 
  }
 
 
   /**
 
   * Singleton returns an existing object or creates a new one
 
   *
 
   * @param string $ident Config identation
 
   * @return object IniConfig Resulting config
 
   * @access  public
 
   */
 
  function &singleton ($ident='') {
 
        static $instances;
 
        if (!isset($instances)) $instances = array();
 
        
 
        $signature = md5($ident);
 
        if (!isset($instances[$signature])) {
 
      $instances[$signature] = new IniConfig;
 
        }
 
        return $instances[$signature];
 
  }
 
  
 
} // class
 
?>
 
 |