mirror of
https://github.com/jimeh/dlist.git
synced 2026-02-18 23:46:39 +00:00
- other general fixes to things a bit all over the place git-svn-id: file:///Users/jimeh/Desktop/dlist/trunk@12 a5845835-ea0f-0410-a762-dd0bfe9bfde8
74 lines
1.8 KiB
PHP
74 lines
1.8 KiB
PHP
<?php
|
|
|
|
class config {
|
|
|
|
/*
|
|
|
|
Class: config v0.2 beta
|
|
|
|
Copyright © 2006 Jim Myhrberg. All rights reserved.
|
|
zynode@gmail.com
|
|
|
|
*/
|
|
|
|
|
|
function config ($input=false) {
|
|
if ( !empty($input) ) $this->parse($input);
|
|
}
|
|
|
|
// Main function
|
|
function parse ($input, $overwrite=true, $pad=false) {
|
|
if ( !empty($pad) ) $this->_config_pad = $pad;
|
|
if ( is_array($input) ) {
|
|
$this->parse_array($input, $overwrite);
|
|
} elseif ( is_string($input) ) {
|
|
if ( preg_match("/.*\.php$/", $input) ) {
|
|
$this->parse_php_file($input, 'config', $overwrite);
|
|
} else {
|
|
$this->parse_ini_file($input);
|
|
}
|
|
}
|
|
unset($this->_config_pad);
|
|
}
|
|
|
|
// Parse settings from an array
|
|
function parse_array ($input, $overwrite=true) {
|
|
$pad = '';
|
|
if ( is_array($input) ) {
|
|
foreach( $input as $key => $value ) {
|
|
if ( is_array($value) ) {
|
|
if ( !empty($this->_config_pad) ) $key = $this->_config_pad.$key;
|
|
foreach( $value as $k => $v ) {
|
|
if ( ($empty = empty($this->$key)) || $overwrite ) {
|
|
$this->$key = ( $empty ) ? array($k=>$v) : array_merge($this->$key, array($k=>$v)) ;
|
|
}
|
|
}
|
|
} else {
|
|
if ( !empty($this->_config_pad) ) $key = $this->_config_pad.$key;
|
|
if ( $overwrite || empty($this->$key) ) $this->$key = $value;
|
|
}
|
|
}
|
|
return true;
|
|
} else return false;
|
|
}
|
|
|
|
// Parse settings from a defined array inside a php file,
|
|
// $var is the name of the variable inside the php to parse.
|
|
function parse_php_file ($file, $var='config', $overwrite=true) {
|
|
if ( is_readable($file) ) {
|
|
include($file);
|
|
$this->parse_array($$var, $overwrite);
|
|
return true;
|
|
} else return false;
|
|
}
|
|
|
|
// Parse settings from an ini file.
|
|
function parse_ini_file ($file, $overwrite=true, $parse_sections=true) {
|
|
if ( is_readable($file) ) {
|
|
$this->parse_array(parse_ini_file($file, $parse_sections), $overwrite);
|
|
}
|
|
}
|
|
|
|
}
|
|
|
|
?>
|