class ini {
/* public settings */
public $parse_constants = TRUE;
public $use_defaults = TRUE;
/* 'private' settings */
private $_ini_file = "";
private $_ini_loaded = FALSE;
private $_default_loaded = FALSE;
private $_error_pattern = '[ini] %s';
private $_settings = array (
'ini' => array (),
'default' => array ()
)
;
/**
* constructor ini ( [ string ini_file = NULL [, string default_file = NULL] ] )
*/
function ini ( $ini_file = NULL, $default_file = NULL ) {
if ( !is_null ( $ini_file ) ) {
$this->_ini_file = $ini_file;
$this->read_ini ( $ini_file, FALSE );
if ( !is_null ( $default_file ) ) {
$this->read_defaults ( $default_file );
} // if
} // if
} // func ini
/**
* mixed get ( string key [, string section = NULL] )
*/
function get ( $key, $section = NULL ) {
/**
* if a value for given key (and section) exists -> return it
* else if $use_defaults is TRUE return the default setting ( if any )
*/
$ini_value = $this->_get_value ( $key, 'ini', $section );
$def_value = $this->_get_value ( $key, 'default', $section );
if ( is_null ( $ini_value ) && $this->use_defaults ) {
return $def_value;
} // if
else {
return $ini_value;
} // else
} // func get
/**
* private mixed _get_value ( string key, string type [, string section = NULL] )
* (needs to be improved!)
*/
function _get_value ( $key, $type, $section = NULL ) {
/**
* return the value; if it does not exist return NULL
*/
if ( is_null ( $section ) ) {
if ( isset ( $this->_settings[ $type ][ $key ] ) ) {
return $this->_settings[ $type ][ $key ];
} // if
else {
return NULL;
} // else
} // if
else {
if ( isset ( $this->_settings[ $type ][ $section ][ $key ] ) ) {
return $this->_settings[ $type ][ $section ][ $key ];
} // if
else {
return NULL;
} // else
} // else
} // func _get_value
function set ($key, $value="", $section = NULL){
$this->_set_value($key,$value,'ini',$section);
$this->_save_file();
return $this->_settings['ini'];
}
function _set_value($key, $value="", $type,$section = NULL){
if (is_null ($section)){
$this->_settings[$type][$key] = ($value?$value:'""');
}else{
$this->_settings[$type][$section][$key] = ($value?$value:'""');
}
//var_dump($this->_settings);
}
/**
* mixed read_ini ( string filename [, boolean return_ini = FALSE ] )
*/
function read_ini ( $filename, $return_ini = FALSE ) {
if ( $content = $this->_read_file ( $filename ) ) {
$this->_ini_file = $filename;
$this->_settings[ 'ini' ] = $this->_parse ( $content );
$this->_ini_loaded = TRUE;
if ( $return_ini ) {
return $this->_settings[ 'ini' ];
} // if
else {
return TRUE;
} // else
} // if
else {
return FALSE;
} // else
} // func read_ini
/**
* read_mem
*
* nacteni z promene
*
*/
function read_mem($content){
$this->_settings[ 'ini' ] = $this->_parse ( $content );
$this->_ini_loaded = TRUE;
return $this->_settings[ 'ini' ];
}
/**
* boolean read_defaults ( string filename )
*/
function read_defaults ( $filename ) {
if ( $content = $this->_read_file ( $filename ) ) {
$this->_settings[ 'default' ] = $this->_parse ( $content );
$this->default_loaded = TRUE;
return TRUE;
} // if
else {
return FALSE;
} // else
} // func read_defaults
/**
* private string _read_file ( string filename )
*/
function _read_file ( $filename ) {
if ( !file_exists ( $filename ) ) {
trigger_error ( $this->_errstr ( 'File does not exist: "' . $filename . '"' ), E_USER_ERROR );
return FALSE;
} // if
$content = @file ( $filename );
if ( !is_array ( $content ) ) {
trigger_error ( $this->_errstr ( 'an error occured while reading "' . $filename . '"' ), E_USER_ERROR );
return FALSE;
} // if
return join ( '', $content );
} // func _read_file
function save(){
global $_POST;
//var_dump($_POST);
foreach($_POST as $key=>$val){
$k = explode("_",$key);
$pos = strpos($key,"_");
$sekce = substr($key,0,$pos);
$klic = substr($key,$pos+1);
//echo "$sekce $klic
";
$this->_settings['ini'][$sekce][$klic] = $val;
}
$this->_save_file();
return $this->_settings['ini'];
}
/**
* private mixed _save_file ()
*
*/
function _save_file (){
if ($f = fopen($this->_ini_file,"w+")){
$file_body = "";
foreach($this->_settings['ini'] as $key=>$val){
if (is_array($val)){
$file_body .= "[$key]\n";
foreach($val as $key1=>$val1){
$file_body .= " $key1=".($val1?$val1:'""')."\n";
}
}
}
fwrite($f,$file_body);
fclose($f);
}
}
/**
* private mixed _parse ( & string content )
*/
function _parse ( & $str_content ) {
/**
* required vars
*/
$comments = array ( "/#[^\n]*/", "/'[^\n]*/", "/\/\*[^\n]*/");
$ini = array ();
$arr_content = array ();
$current_section = FALSE;
$line = '';
$key = '';
$value = '';
// replace microsoft-style-newlines
$str_content = str_replace ( "\r\n", "\n", $str_content );
// remove comments...
$str_content = preg_replace ( $comments, '', $str_content );
// create array from $content separate by newline
$str_content = explode ( "\n", $str_content );
// no we do the real parsing...
foreach ( $str_content as $key => $line ) {
$line = trim ( $line );
if ( !empty ( $line ) ) {
// if the line contains a section we set it as the current one
if ( preg_match ( '/(?:\[)(.*)(?=])/', $line, $matches ) ) {
$current_section = $matches[ 1 ];
} // if
// if it has a definition like foo = bar we set this
elseif ( preg_match ( '/(.+)(?:=)(.+)/', $line, $matches ) ) {
$key = trim ( $matches[ 1 ] );
$value = trim ( $matches[ 2 ] );
// if value is enclosed in quotes we just remove them;
if ( preg_match ( '/(^".*"$)|(^\'.*\'$)/', $value ) ) {
$value = substr ( $value, 1, -1 );
} // if
// otherwise if parse_constants is TRUE we look if
// there is a matching constant for value
elseif ( $this->parse_constants ) {
if ( defined ( $value ) ) {
$value = constant ( $value );
} // if
} // elseif
// adding key = value to the ini array; if cuurent section
// is set we put it into that branch
if ( $current_section === FALSE ) {
$ini[ $key ] = $value;
} // if
else {
$ini[ $current_section ][ $key ] = $value;
} // else
} // elseif
// if line contains anything not matching the above patterns
// we do:
else {
// NOTHING
} // else
} // if
} // foreach
return $ini;
} // funct _parse
/**
* private string _errstr ( string message )
*/
function _errstr ( $message ) {
return sprintf (
$this->_error_pattern
, $message
);
} // func _errstr
function extract(){
global $_SERVER;
extract($_SERVER);
foreach($this->_settings[ 'ini' ] as $key=>$val){
//echo "$key => $val
";
if (is_array($val)){
foreach($val as $key1=>$val1){
//echo strpos($val1,'$')."
";
if (strpos($val1,'$') > -1){ extract($extract); eval("\$val1 = \"$val1\";"); }
$extract["$key"."_"."$key1"] = $val1;
//echo " $key1 => $val1 : ".${$key1}."
";
}
}
}
return $extract;
}
} // class ini
?>