<?php

 class iniParser {
    
    var $_iniFilename = '';
    var $_iniParsedArray = array();

    function iniParser( $filename ) {
        $this->_iniFilename = $filename;
        $this->_iniParsedArray = parse_ini_file( $filename, FALSE );
    }

    function getMasterSection() {
        $match = '';
        $MSfdescriptor = fopen( $this->_iniFilename, "r" );
        $fdata = fread( $MSfdescriptor, filesize( $this->_iniFilename ) );
        preg_match( '/\[(.*)\]/', $fdata, $match);
        fclose( $MSfdescriptor );
        return $match[1];
    }

    function getValue( $key ) {
        return $this->_iniParsedArray[$key];
    }

    function setValue( $key, $value ) {
        if( $this->_iniParsedArray[$key] = $value ) return TRUE;
    }

    function saveFile( $filename = null ) {
        if( $filename == null ) $filename = $this->_iniFilename;
        $masterSection = $this->getMasterSection();
        if( is_writeable( $filename ) ) {
            $SFfdescriptor = fopen( $filename, "w" );
            fwrite( $SFfdescriptor, "[" . $masterSection . "]\n" );
            foreach( $this->_iniParsedArray as $key => $value ) {
                fwrite( $SFfdescriptor, "$key=$value\n" );
            }
            fclose( $SFfdescriptor );
            return TRUE;
        }
    }
 }
?>
