<?php
/* $Id: IniFile.php,v 1.5 2006/08/30 22:00:58 robertbienert Exp $
*
* easy ini file access
*
* Copyright (C) 2005, 2006 Robert Bienert
* <robertbienert at sourceforge dot net>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library 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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
/* easy ini file access
*
* This class maps a ini file into memory and provides a simple access
* to the file.
*/
class IniFile {
var $ini; // the array containing our key-value pairs
var $file; // a filename for loading and saving
// constructor
function IniFile(& $file) {
$this->setFile($file);
$this->ini = @parse_ini_file($this->file);
}
// checks if the following key exists
function hasKey($key) {
return @array_key_exists($key, $this->ini);
}
// returns the value for key
function value($key, $def = FALSE) {
return ($this->hasKey($key)) ? $this->ini[$key] : $def;
}
// sets a key-value pair
function setValue($key, $value) {
$this->ini[$key] = $value;
}
// sets a new filename for writing
function setFile($file) {
$this->file = $file;
}
// saves the file
function save($file = FALSE) {
if ($file !== FALSE)
$this->setFile($file);
if (! ($fh = @fopen($this->file, 'wb')))
return FALSE;
foreach ($this->ini as $key => $val)
if (@fwrite($fh, "$key=$val\n") === FALSE)
return FALSE;
return @fclose($fh) == TRUE;
}
}
?>