<?php
/*
* Developer : Dirk Herling
* Contact : hide@address.com
* Version : 1.0 Beta
* Date : 2002-12-17
* Classname : iniRW
* Filename : inirw.class.php
* License : GNU General Public License (GPL)
*
* Public functions:
* Void iniRW ( [String $sys]); // contructor
* Array read_all ( String $file); // return array with all values
* Array read_section ( String $file, String $section); // return array of values of selected section
* String read_item ( String $file, String $section, String $item); // return value of selected item
* Void write ( String $file, String $section, String $item, String $value); // insert or update an item value
* String error ( ); // return error message
*
* Private functions:
* Void _Import ( String $file); // read and compare ini-file
* Void _Export ( String $file); // write ini-file
*
* Example:
* <?php
* require ( "inirw.class.php");
* $ini = new iniRW ();
*
* $ini->write ( "example.ini", "example", "testitem" , "This is an example!!!");
* $ini->write ( "example.ini", "example", "testitem2", "This is an other example!!!");
* $ini->write ( "example.ini", "test" , "testitem" , "TEST example");
*
* $all = $ini->read_all ( "example.ini", "example");
* print_r($all);
* // $all["EXAMPLE"]["testitem"] = "This is an example!!!";
* // $all["EXAMPLE"]["testitem2"] = "This is an other example!!!";
* // $all["TEST"]["testitem"] = "TEST example";
*
* $section = $ini->read_section ( "example.ini", "example"); // get section values
* print_r($section);
* // $section["testitem"] = "This is an example!!!";
* // $section["testitem2"] = "This is an other example!!!";
*
* $item = $ini->read_item ( "example.ini", "test", "testitem"); // get item value
* echo $item;
* // $item = "TEST example";
* ?>
*/
class iniRW
{
//--- Private variables -----------------------------------------------------
var $_iniData;
var $_lineBreak;
//--- Public functions ------------------------------------------------------
function iniRW($sys = "UNIX") // VOID
{
switch(strtoupper($sys))
{
case "UNIX":
$this->_lineBreak = "\n";
break;
case "WIN":
$this->_lineBreak = "\r\n";
break;
case "MAC":
$this->_lineBreak = "\r";
break;
default:
$this->_lineBreak = "\n";
}
}
function read_all($file) // ARRAY
{
if($this->_iniData[$file] == null) $this->_Import($file);
return $this->_iniData[$file];
}
function read_section($file, $section) // ARRAY
{
if($this->_iniData[$file] == null) $this->_Import($file);
return $this->_iniData[$file][strtoupper($section)];
}
function read_item($file, $section, $item) // STRING
{
if($this->_iniData[$file] == null) $this->_Import($file);
return $this->_iniData[$file][strtoupper($section)][strtolower($item)];
}
function write($file, $section, $item, $value) // VOID
{
if($this->_iniData[$file] == null) $this->_Import($file);
if($this->iniErrorId != 403)
{
$this->_iniData[$file][strtoupper($section)][strtolower($item)] = preg_replace("/[\r\n]+/", " ", $value);
$this->_Export($file);
}
$this->_iniData[$file] = null;
}
function error() // STRING
{
return $this->iniErrorMsg;
}
//--- Private functions -----------------------------------------------------
function _Import($file) // VOID
{
if(file_exists($file))
{
if($fp = fopen($file, "r"))
{
while($line = fgets($fp, 4192))
{
if(preg_match("/^\[([\w\d]+)\][\r\n]*$/", $line, $matches))
{
$section = strtoupper($matches[1]);
continue;
}
elseif(preg_match("/^([\w\d]+)=([\w\W\d\D]+)[\r\n]*$/", $line, $matches) && $section != null)
{
$item = strtolower($matches[1]);
$this->_iniData[$file][$section][$item] = trim($matches[2]);
}
}
fclose($fp);
$this->iniErrorId = 200;
$this->iniErrorMsg = null;
}
else
{
$this->iniErrorId = 403;
$this->iniErrorMsg = 'Could not read INI-File "'.$file.'"!!!';
}
}
else
{
$this->iniErrorId = 404;
$this->iniErrorMsg = 'File not exists "'.$file.'"';
}
}
function _Export($file) // VOID
{
if($fp = fopen($file, "w"))
{
while(list($section, $data) = each($this->_iniData[$file]))
{
fwrite($fp, "[$section]" . $this->_lineBreak);
while(list($item, $value) = each($data))
{
if($value != null)
{
fwrite($fp, "$item=$value" . $this->_lineBreak);
}
}
fwrite($fp, $this->_lineBreak);
}
fclose($fp);
$this->iniErrorId = 200;
$this->iniErrorMsg = null;
}
else
{
$this->iniErrorId = 403;
$this->iniErrorMsg = 'Could not write INI-File "'.$file.'"!!!';
}
}
}
?>