<?php
class CSIniFile
{
private $file;
public function getFile() { return $this->file; }
private $nodes;
public function __construct($file)
{
$this->file = $file;
$this->nodes = false;
try {
$this->Read();
} catch (Exception $e) {
throw $e;
}
}
public function __destruct() { }
public function getKey($sect, $key, $throwOnUnknown = TRUE)
{
if (array_key_exists($sect, $this->nodes) && array_key_exists($key, $this->nodes[$sect]))
return $this->nodes[$sect][$key];
if ($throwOnUnknown)
throw new Exception('Configuration error: '.$sect.'::'.$key.': not defined');
return false;
}
public function setKey($sect, $key, $value)
{
$this->addSection($sect);
$this->nodes[$sect][$key] = $value;
}
public function delKey($sect, $key)
{
if (array_key_exists($sect, $this->nodes) && array_key_exists($key, $this->nodes[$sect]))
unset($this->nodes[$sect][$key]);
}
public function addSection($sect)
{
if (!array_key_exists($sect, $this->nodes))
$this->nodes[$sect] = array();
}
public function delSection($sect)
{
if (array_key_exists($sect, $this->nodes))
{
// We need to delete all keys in this section
foreach($this->nodes[$sect] as $key => $value)
unset($this->nodes[$sect][$key]);
// Unset the section array
unset($this->nodes[$sect]);
}
}
public function Read()
{
// #####################################################################################
// PHP's inbuilt parse_ini_file() cannot handle non-alphanumeric characters within
// key values unless they are double quoted. The backend Config::IniFiles has no
// option to encapsulate key values in quotes when rewriting a file.
//
// As such, this function is used as an alternative to PHP's native function. It was
// originally written by Justin Frim (hide@address.com) and was copied
// from http://uk.php.net/manual/en/function.parse-ini-file.php#48989 - (w/copyright)
// #####################################################################################
try {
$FileData = CS::GetFileContents($this->file);
} catch (Exception $e) {
throw $e;
}
$this->nodes = array();
$Sect = '';
foreach($FileData as $FileLine)
{
$Line = trim($FileLine);
$FirstChar = substr($Line, 0, 1);
// Skip any commented or empty lines
if ($FirstChar == ';' || $FirstChar == '#' || empty($Line))
continue;
//
// Is this a section?
if ($FirstChar == '[' && substr($Line, -1, 1) == ']')
{
$Sect = strtolower(substr($Line, 1, -1));
continue;
}
//
// This is a key...
if (($DelimPos = strpos($Line, '=')) > 0)
{
$Key = strtolower(trim(substr($Line, 0, $DelimPos)));
$Value = trim(substr($Line, $DelimPos + 1));
if (substr($Value, 1, 1) == '"' && substr($Value, -1, 1) == '"')
$Value = substr($Value, 1, -1);
// Add it in
$this->nodes[$Sect][$Key] = stripcslashes($Value);
}
else
{
// We just have a key, no parameter, so use this with no value.
$this->nodes[$Sect][strtolower($trim($Line))] = '';
}
}
}
public function Write($AlternateFilePath = FALSE)
{
if (!is_array($this->nodes))
throw new Exception('CSIniFile::Write() with no nodes');
$FileBuf = array(
';',
'; '.CS_TITLE.' Settings File',
'; Written '.date('r'),
';'
);
// We need to build a data buffer to write
foreach($this->nodes as $SectName => $SectKeys)
{
$FileBuf[] = '';
$FileBuf[] = '['.$SectName.']';
foreach($SectKeys as $Key => $Value)
$FileBuf[] = $Key.'='.$Value;
}
$FileBuf[] = '';
$FileBuf[] = '; End of Settings File';
foreach($FileBuf as &$Line) // Kludge!!
$Line .= "\r\n";
//
// Now we write this to disk...
if ($AlternateFilePath)
@$WriteLen = file_put_contents($AlternateFilePath, $FileBuf);
else
@$WriteLen = file_put_contents($this->file, $FileBuf);
if (!$WriteLen)
throw new CSFileWriteException($AlternateFilePath ? $AlternateFilePath : $this->file);
return $WriteLen;
}
}
?>