Location: PHPKode > scripts > ABG_IniParser > abg_iniparser/ABG_IniParse.inc.php
<?
  ##############################################################################
  #  ABG_IniParser                                                             #
  #  V1.0.0 04/04/2006  : Initial                                              #
  #  V1.1.0 07/04/2006  : Improvement of class and test script                 #
  #  V1.2.0 09/04/2006  : Debug mode                                           #
  #                       Minor bug fixes                                      #
  #                                                                            #
  #  (cy)  G. BENABOU / ABG Soft PARIS FRANCE                                  #
  #                                                                            #
  #  A PHP 4 script to handle INI type files                                   #
  #  - Multi-dimensional array to hold  structure                              #
  #  - Full check of file access (existence & writability)                     #
  #  - Check of Section/Item existance with optionnal bypass                   #
  #  - Create Section / Item if not existant                                   #
  #  - Comprehensive disgnostic of errors with optional print (Debug mode)     #
  #                                                                            #
  #  Object Properties                                                         #
  #  Name           Description                                                #
  #--------------------------------------------------------------------------- #
  #  $ov_Debug     Enables online printing of errors                   {1.1.2} #
  #  $ov_Error     HTML string when error                                      #
  #  $ov_File      Ini file name                                               #
  #  $ov_Status    Boolean result of last action                               #
  #  $ov_Tbl       Array to hold the ini structure                             #
  #  $ov_Write     Flag TRUE if file is writeable                              #
  #                                                                            #
  #  Object Methods                                                            #
  #  ABG_IniParser($_IniFile,            // Ini Filename                       #
  #                $_Debug=FALSE)        // Online printingof errors           #
  #    - Object constructor                                                    #
  #    - Reads IniFile into ov_Tbl                                             #
  #    - Flags file to be Read only                                            #
  #  om_WriteFile()                                                            #
  #    - Checks file writeability                                              #
  #    - Writes ov_Tbl back to the disk                                        #
  #*******************   IMPORTANT ! DON'T FORGET *****************************#
  #  om_GetItem( $_Section,      // Ini section (betwen brackets)              #
  #              $_Item,         // Item = Value                               #
  #              $_Check=FALSE)  // Check existance of Section/Item (Optional) #
  #    - Get value of Item in Section                                          #
  #    - Return Value or NULL if not accessible                                #
  #  om_SetItem($_Section, $_Item, $_Value)                                    #
  #    - Create Section / Item if not already existant                         #
  #    - Set Value to Item in Section                                          #
  #                                                                            #
  ##############################################################################

  class ABG_IniParser {
    //...  Internal variables
    var $ov_Debug  = FALSE;
    var $ov_Error  = "";
    var $ov_File;
    var $ov_Tbl    = array();
    var $ov_Status = FALSE;
    var $ov_Write  = FALSE;
    var $ov_Errors = array( "File \$Var does not exist",        // 0
                            "Table not available",              // 1
                            "Data not available",               // 2
                            "File \$Var not writeable",         // 3 {1.12}
                            "Unable to open file \$Var",        // 4 {1.12}
                            "Item \$Var does not exist");       // 5 {1.12}

    /*** Initialize object ***/
    function ABG_IniParser($_IniFile, $_Debug=FALSE)                 //  {1.1.2}
    {
      $this->ov_Debug  = $_Debug;
      if(file_exists($_IniFile)) {
        $this->ov_File   = $_IniFile;
        $this->ov_Write  = is_writable($this->ov_File);
        $this->ov_Tbl    = parse_ini_file($this->ov_File, TRUE);
        $this->ov_Status = empty($this->ov_Tbl) ? $this->om_HandleError(1, null) : TRUE;
      }
      else
        $this->om_HandleError(0, $_IniFile);
    }
    /*** Handle error ***/
    function om_HandleError($_No, $Var) {
      $T_ = $this->ov_Errors[$_No];
      $T_ = eval("return(\"$T_\");");
      $this->ov_Error  = "<pre>*** Error ***\n". htmlentities($T_)."\n</pre>";
      if ($this->ov_Debug) print($this->ov_Error);
      return(FALSE);
    }
    /*** Write structure to disk  ***/
    function om_WriteFile(){
      if (!$this->ov_Status)
        return($this->om_HandleError(2, null));
      if (!$this->ov_Write)
        return($this->om_HandleError(3, $this->ov_File));
      $FileRes_ = fopen($this->ov_File, 'w');
      if(!$FileRes_ )
        return($this->om_HandleError(4, $this->ov_File));            //  {1.1.2}
      foreach($this->ov_Tbl as $SecName_=>$Section_){
        fwrite($FileRes_, "[$SecName_]\n");
        foreach($Section_ as $_Item=>$Value_)
          fwrite($FileRes_, "$_Item=$Value_\n");
      }
      fclose($FileRes_);
    }
    /*** Retrieve Item in Section from the structure ***/            //  {1.1.2}
    function om_GetItem($_Section, $_Item, $_Check=FALSE){
      if(!$this->ov_Status)
        return($this->om_HandleError(2, null));
      $Val_ = $this->ov_Tbl[$_Section][$_Item] ;
      if (!isset($Val_) and $_Check)
        $this->om_HandleError(5, "$_Section/$_Item");
      return($Val_);
    }
    /*** Set Item in Section to Value into the structure             //  {1.1.2}
         Creates Section and Item if not existant ***/
    function om_SetItem($_Section, $_Item, $_Value){
      $Sec_ = $this->ov_Tbl[$_Section];
      $Sec_ = isset($Sec_) ? $Sec_ : array($_Item=>$_Value);
      $Sec_[$_Item] = $_Value;
      $this->ov_Tbl[$_Section] = $Sec_;
    }
  }
?>
Return current item: ABG_IniParser