<?
/*
ConfigReader v 1.3 1999/04/03
Copyright (c) 1999 CDI, hide@address.com, All Rights Reserved
This was a quickie that took all of 2 hours to write
and debug. A very simple, yet robust, config file reader for PHP3
1.1 Added booleanize(), this version was never released.
1.2 Added AUTOBOOL and associated logic
ConfigKeys that match ^b[A-Z] will automatically
be interpreted as boolean vars. If set to anything other than
true or yes (case insensitive) the ConfigKey will be unset.
1.3 Added the ability to assign array values dynamically.
Duplicate keys will create an array, each associated value
for that key will become an array element.
*/
Class Config
{
var $param = array();
var $ERROR = "";
var $AUTOBOOL = false;
// ************************************************************
function Config ( $ConfigFile = "" )
{
if(!empty($ConfigFile))
{
$Return = $this->get_config($ConfigFile);
}
// If you don't call new() with the file path and name,
// you MUST call get_config() with it.
}
// ************************************************************
function get_config ($ConfigFile)
{
global $php_errormsg;
if(empty($ConfigFile))
{
$this->error("No ConfigFile to read",0);
return;
}
if(!file_exists($ConfigFile))
{
$this->error("[$ConfigFile] does not exist",0);
return;
}
$fd = @fopen($ConfigFile,"r");
if( (!$fd) or (empty($fd)) )
{
$this->error("File error: [$php_errormsg]",0);
return false;
}
$contents = fread( $fd, filesize( $ConfigFile ) );
fclose($fd);
if(empty($contents))
{
$this->error("Config file has no data",0);
return false;
}
$this->parse_config($contents);
unset($contents);
return;
}
// ************************************************************
function parse_config ($ConfigContents)
{
$ContentsArray = split("\n",$ConfigContents);
while ( list ($key,$line) = each ($ContentsArray) )
{
if (eregi("^[A-Z]",$line))
{
$this->set_config($line);
}
}
}
// ************************************************************
function set_config ( $ConfigLine )
{
list ($ConfigKey,$ConfigVal) = split(" = ",$ConfigLine);
$Key = ereg_replace("[ ]", "",$ConfigKey); // Remove spaces and tabs from the Key
// $ConfigVal = ereg_replace("^[ ]+","",$ConfigVal); // Remove whitespace leading up to the Val
// What was I thinking?
$ConfigVal = trim($ConfigVal); // *heh*
if (!ereg("^'",$ConfigVal))
{
// Remove whitespace in $ConfigVal if no ' at beginning of Val
$Val = ereg_replace("[ ]", "",$ConfigVal);
}
else
{
// Strip the leading ' and trailing '
$Val = ereg_replace("^'","",$ConfigVal);
$Val = ereg_replace("'$","",$Val);
}
if(!$this->$Key)
{
$this->$Key = $Val;
$this->param[$Key] = $Val;
}
else
{
$thisKey = $this->$Key;
if(!is_array($thisKey))
{
settype($thisKey,"array");
}
$count = count($thisKey);
$thisKey[$count] = $Val;
$this->$Key = $thisKey;
$this->param[$Key] = $thisKey;
}
if ($this->AUTOBOOL)
{
if (ereg("^b[A-Z]",$Key))
{
$this->booleanize($Key);
}
}
return;
}
// ************************************************************
function error ( $errorMsg, $die = 0 )
{
$this->ERROR = "[ConfigReader]: $errorMsg";
@error_log($this->ERROR,0);
return;
}
// ************************************************************
function booleanize ( $ConfigKey )
{
$ConfigVal = $this->$ConfigKey;
if(eregi("(true|yes)",$ConfigVal))
{
$this->$ConfigKey = true;
$this->param[$ConfigKey] = true;
$Return = true;
}
else
{
unset($this->param[$ConfigKey]);
unset($this->$ConfigKey);
$Return = false;
}
return($Return);
}
// ************************************************************
}
?>