config.php -
index
<?php
/*
"...we know that the initialization of a program can't be written
until the program itself takes shape."
-- Donald Knuth
*/
/* CONFIG.PHP - This configures the core of the application.
This contains all "sitewide" configuration data as an associative array as a
static variable in the config() function that is initialized on first call.
All data access is through that function (with a few "helper" functions).
The odd things here are how "sections" are read (and we need more documentation
about that), how 'debug' is handled and how $arg is checked (which should be
checked in RULES.PHP, but...).
*/
config(); // config() initializes on first use
function config($var = NULL, $val = NULL) {
static $config = array();
if ($config == array()) {
$config = parse_ini_file('config.ini');
$sections = @parse_ini_file('sections.ini',TRUE);
$config['sections']['']['name'] = ($sections['name']) ?
$sections['name'] : 'Index';
$config['sections']['']['subtitle'] = $sections['subtitle'] ?
$sections['subtitle'] : 'The THIS Project';
unset($sections['name'],$sections['subtitle']);
if ($sections) {
foreach ($sections as $k => $v)
$config['sections'][$k] = $v;
unset($sections);
}
$config['version'] = $GLOBALS['VERSION'];
// sets 'debug' only if loggin as Admin but with a special value of 9 even if
// not logged in as Admin (to debug Admin code)
if ($config['debug'] == 9)
errorlog($config['debug']);
else
if (userstate() || isset($config['admin'])) {
if (($t = getvar('debug')) !== "")
$config['debug'] = $t;
if ($config['debug'])
errorlog($config['debug']);
}
debug('cookies: '.cookie_str(),-1);
if (!config('testing') && !defined('DB_NAME')) // known unknown
$config['testing'] = 1; // force testing
// it's kind of a dilema checking arg here as it should be done in RULES.PHP
// but it is needed here to set some configuration variables! grrrr!
$arg = getvar('arg');
if ($arg && !isset($config['sections'][$arg]))
$arg = $_GET['arg'] = '';
$section =& $config['sections'][$arg];
foreach ($section as $name => $value)
$config[$name] = $value;
foreach($config as $k => $v)
if (!is_array($v) && preg_match('/dir$/',$k) && $v)
$config[$k] = rtrim($v,'/') . '/';
$config['section'] = $arg;
if ($arg == '') $arg = 'root';
$config['pagedir'] .= $arg.'/';
$config['importdir'] .= $arg.'/';
}
if ($var === NULL)
return $config;
if ($val !== NULL)
return $config[$var] = $val;
if (isset($config[$var]))
return $config[$var];
return '';
}
/*
A few "support" functions - these simply and only reduce several calls like:
$htmdir = config('htmdir');
to:
$htmdir = htmdir();
thereby eliminating inline string literals ('htmdir' in this example).
*/
/* htmdir - return html directory with possible filename */
function htmdir($file = '') {
return config('htmdir').$file;
}
/* themedir - return theme directory with possible filename */
function themedir($file = '') {
return config('htmdir').config('themedir').$file;
}
function imgdir($file = '') {
return config('imgdir').$file;
}
/* datestrcmt - return date string formated to comment date format */
function datestrcmt($time = "") {
if ($time == "") $time = time();
return date(config('datestrcmt'),$time);
}