<?php
class INI {
    private $file;

    static private $instances;

    private $data;

    private function __construct($file) {
    	$this->file = $file;
        $this->data = parse_ini_file($file, true);
    }

    static public function & getInstance($file) {
        if(! isset(self::$instances[$file])) {
            self::$instances[$file] = new INI($file);
        }

        return self::$instances[$file];
    }

    public function getValue($p_section,$p_key) {
        if ((is_array($this->data)) &&
                (array_key_exists($p_section, $this->data)) &&
                (array_key_exists($p_key, $this->data[$p_section]))) {
            return $this->data[$p_section][$p_key];
        }
    }

    public function getSections() {
        return array_keys($this->data);
    }
    
    public function getKeys($p_section) {
    	if(array_key_exists($p_section,$this->data)) {
    	   return array_keys($this->data[$p_section]);
    	}
    }

    public function setValue($sSection, $sKey, $sValue) {
        $sFileHandle = fopen($this->file, "r");
        if (!$sFileHandle) {
            Log::error(__FILE__,__LINE__,"Unable to open ".$this->file.".");
        }
        if (filesize($this->file) > 0) {
            $sFileContents = fread($sFileHandle, filesize($this->file));
        }
        if ($sFileContents === FALSE) {
            Log::error(__FILE__,__LINE__,"Unable to read data from ".$this->file.".");
        }
        $sFileData = preg_split("/\r?\n/", $sFileContents);
        $sectionStart = -1;
        $sectionEnd = -1;
        $returnLine = -1;
        for ($i = 0; $i < count($sFileData); $i++) {
            $x = strpos($sFileData[$i], "[".$sSection."]");
            $y = strpos($sFileData[$i], "[");
            if ($y == 0 && $y !== FALSE && $sectionStart >= 0 && $sectionEnd == -1) {
                $sectionEnd = ($i - 1);
            }
            $z = explode("=", $sFileData[$i]);
            if (count($z) >= 2) {
                if ($z[0] == $sKey && $sectionStart >= 0 && $sectionEnd == -1 && substr($sFileData[$i], 0, 1) != ";") {
                    $returnLine = $i;
                }
            }
            if ($x == 0 && $x !== FALSE && $sectionStart == -1) {
                $sectionStart = ($i + 1);
            }
        }
        if ($sectionEnd == -1) {
            $sectionEnd = (count($sFileData)-1);
        }
        $newfile = "";
        if ($returnLine > -1) {
            $sFileData[$returnLine] = $sKey."=".$sValue;
            for ($i = 0; $i < count($sFileData); $i++) {
                $newfile .= $sFileData[$i]."\r\n";
            }
        }
        else {
            if ($sectionStart > -1) {
                for ($i = 0; $i < count($sFileData); $i++) {
                    if ($i == $sectionEnd) {
                        $newfile .= $sKey."=".$sValue."\r\n";
                    }
                    $newfile .= $sFileData[$i]."\r\n";
                }
            }
            else {
                for ($i = 0; $i < count($sFileData); $i++) {
                    $newfile .= $sFileData[$i]."\r\n";
                }
                $newfile .= "[".$sSection."]\r\n".$sKey."=".$sValue."\r\n";
            }
        }
        fclose($sFileHandle);
        $sFileHandle = fopen($this->file, "w");
        if (!$sFileHandle) {
            Log::error(__FILE__,__LINE__,"Unable to open file ".$this->file." with write access.");
        }
        $sFileWrite = fwrite($sFileHandle, trim($newfile));
        if ($sFileWrite === FALSE) {
            Log::error(__FILE__,__LINE__,"Unable to write data to file ".$this->file.".");
        }
        fclose($sFileHandle);
        $this->data[$sSection][$sKey] = $sValue;
    }
}
?>