Sources for file plugins/config/file.php in version 2.0
Click on a comment to hide it. Click here to show all comments.
<?PHP
/*
* Project: Onyx: PHP Application Backend
* File: config/file.php
* Method to parse configuration files
*
* Version: 2.0
* CVS tag: $Id: file.php,v 1.12 2003/05/31 08:29:39 bok Exp $
* Author: Robert Amos <bok[at]ausmac.net>
* Andrew Wellington <proton[at]wiretapped.net>
* Copyright: 2001,2002,2003 odynia.org.
*/
class config_file_plugin {
/**
* Method: parse
* Description: Parse Configuration Files
* Arguments: string - file name
* Returns: an array of the config file (empty if error)
**/
function parse($file=NULL) {
global $onyx_parent;
// check first
if (is_null($file)) {
$onyx_parent->trigger_error('No configuration file specified.', WARNING);
return array();
}
// check it exists and we can read it
if (!file_exists($file) || !is_readable($file)) {
$onyx_parent->trigger_error('File ($file) not found or isnt readable.', WARNING);
return array();
}
// well what else is there to do now but start it off?
$fp = fopen($file, "r");
// keep a line counter for debugging and errors
$line = 0;
// for dealing with variables in config values for later
global $onyx_parent;
if (is_object($GLOBALS['smarty']))
$vars = $GLOBALS['smarty']->get_template_vars();
// loop the file
while (!feof($fp)) {
// read line in
$linebuf = chop(fgets($fp, 1024));
// deal with comments
if (preg_match("/^[^a-zA-Z0-9_]/", $linebuf)) {
$line++;
continue;
}
// split!!
if (!preg_match("/^([\S]+?)\s+(.*?)$/", $linebuf, $m)) {
$line++;
continue; // invalid line ey
}
// allow for variables in the files
if (preg_match_all('/\{\$([^}]*?)}/', $m[2], $variables, PREG_SET_ORDER)) {
foreach ($variables as $v) {
if (!empty($vars[$v[1]])) {
$match[] = $v[0];
$replace[] = $vars[$v[1]];
} elseif (!empty($onyx_parent->config[$v[1]])) {
$match[] = $v[0];
$replace[] = $onyx_parent->config[$v[1]];
} elseif (!empty($onyx_parent->page[$v[1]])) {
$match[] = $v[0];
$replace[] = $onyx_parent->page[$v[1]];
} elseif (!empty($GLOBALS[$v[1]])) {
// only other source, global vars i guess
$match[] = $v[0];
$replace[] = $GLOBALS[$v[1]];
} else {
$match[] = $v[0];
$replace[] = str_replace('$', '\$', $v[0]);
}
}
}
if (is_array($match)) {
$evaluated = str_replace($match, $replace, $m[2]);
} else {
$evaluated = $m[2];
}
// there seriously has to be a better way of doing this
$varline = "['".preg_replace("/\./", "']['", $m[1])."']";
$varline = "return".$varline;
eval("\$$varline = \"$evaluated\";");
}
fclose($fp);
$line++;
// phew
return $return;
}
}
?>
