<?php
/*
 *
 * sflog!
 * Copyright (C) 2005 Alberto Sfolcini <a.sfolcini@gmail.com>
 *
 * This file is a part of sflog!.
 *
 * sflog! is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * sflog! is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 *
 */
 
/**
 * @title               : Ini       
 * @description 		: Manages INI files, Read&Write 
 * @date                : 22-Oct-2005   
 * @author              : Alberto Sfolcini <a.sfolcini@nikasistemi.com>
 */
class Ini {

    var $fileName;

	/**
	 * Constructor
	 * @param $filename string
	 */
    function Ini($fileName) {
    	$this->fileName = $fileName;
    }

	/**
	 * Gets section's count
	 * @return int
	 */
    function getSectionsCount(){
	   if (file_exists($this->fileName))
		   $temp= parse_ini_file($this->fileName, true);
		else
			$temp = 0;
    	return count($temp);
   }

	/**
	 * Gets array of full ini
	 * @return array that contains full ini
	 */
    function getIniAsArray(){
		return parse_ini_file($this->fileName, true);
    }


	/**
	 * Gets given key's value at given section
	 * @param $section string
	 * @param $key string
	 * @return string
	 */
	function getSectionValue($section,$key){
    	$temp= parse_ini_file($this->fileName, true);
        return $temp[$section][$key];
    }

	/**
	 * Gets value of given key,
	 * Use this function only if your INI file has not 
	 * any sections.
	 * @param $key
	 * @return string value
	 */
    function getValue($key){
        $temp= parse_ini_file($this->fileName);
        return $temp[$key];       
	}


	/**
	 * check if a given section exists
	 * @param $section string
	 * @return boolean
	 */
	function sectionExists($section){
		error_reporting(E_USER_ERROR);
    	$temp= parse_ini_file($this->fileName, true);
       	if (!is_null($temp[$section]))
       		return true;
       	else
       		return false;       	
	}

	/**
	 * Gets array of section (string format)
	 * @return array of string
	 */
	function getSections(){
    	return array_keys(parse_ini_file($this->fileName, true));	
	}
	
	
	/**
	 * Sets new value for a given key and section
	 * @param $section string
	 * @param $key string
	 * @param $value string
	 */
	function setSectionValue($section,$key,$value){
		$currentINI = $this->getIniAsArray();
		$currentINI[$section][$key] = $value;
		$this->write_ini_file($this->fileName,$currentINI);
	}

	/**
	 * Deletes section
	 * @param $section
	 */
	function deleteSection($section){
		$currentINI = $this->getIniAsArray();
		unSet($currentINI[$section]);
		$this->write_ini_file($this->fileName,$currentINI);		
	}


	/**
	 * Sets new value for a given key
	 * Use this function only if your ini file has not
	 * any sections
	 * @param $section string
	 * @param $key string
	 * @param $value string
	 */
	function setValue($key,$value){
		$currentINI = $this->getIniAsArray();
		$currentINI[$key] = $value;
		$this->write_ini_file($this->fileName,$currentINI);
	}


	/**
	 * write ini file 
	 * code ripped from php.net forum 
	 * wrote by wickedfather@hotmail.com
	 * 
	 * this function should be considered as private
	 * but is declared as public to keep compability
	 * with php4
	 */
	function write_ini_file($path, $assoc_array){
   	$content = '';
	   $sections = '';

   	foreach ($assoc_array as $key => $item){
			if (is_array($item)){
				$sections .= "\n[{$key}]\n";
           	foreach ($item as $key2 => $item2){
					if (is_numeric($item2) || is_bool($item2))
                   $sections .= "{$key2} = {$item2}\n";
               else
                   $sections .= "{$key2} = \"{$item2}\"\n";
           	}     
       	}else{
           if(is_numeric($item) || is_bool($item))
               $content .= "{$key} = {$item}\n";
           else
               $content .= "{$key} = \"{$item}\"\n";
       	}
   	}     

   	$content .= $sections;

	   if (!$handle = fopen($path, 'w')){
			return false;
		}
  
	   if (!fwrite($handle, $content)){
			return false;
		}
  
	   fclose($handle);
	   return true;
	}


}
?>
