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 - read/write configuration data */
function config($var = NULL, $val = NULL) {
static $config = array();
if ($config == array()) {
if (!is_file($f='config.ini'))
user_error("'$f' was not found",E_USER_ERROR);
$config = parse_ini_file($f);
if ($config == FALSE)
user_error("cannot continue with error in '$f'",E_USER_ERROR);
if (is_file($f='sections.ini')) {
$sections = parse_ini_file($f,TRUE);
if ($sections == FALSE)
user_error("cannot continue with error in '$f'",E_USER_ERROR);
}
$config['sections']['']['name'] =
isset($sections['name']) ?
$sections['name'] : 'Index';
$config['sections']['']['subtitle'] =
isset($sections['subtitle']) ?
$sections['subtitle'] : 'The THIS Project';
foreach ($sections as $k => $v)
if (!is_array($v))
unset($sections[$k]);
if (isset($sections))
foreach ($sections as $k => $v)
$config['sections'][$k] = $v;
$config['version'] = $GLOBALS['VERSION'];
if (!config('testing') && !defined('DB_NAME')) // known unknown
$config['testing'] = 1; // force testing
// unlike other GET inputs, 'arg' is checked here as it is needed to set
// some configuration variables (see also RULES.PHP)...
$arg = getvar('arg');
if ($arg && !isset($config['sections'][$arg])) {
if (isset($config['httperrors']))
notfound();
$arg = $_GET['arg'] = '';
}
// this overrides settings by section (some settings won't make sense in a
// section; see file SECTIONS)
$section =& $config['sections'][$arg];
foreach ($section as $name => $value)
$config[$name] = $value;
// sets 'debug' only if logged in as Admin or running Admin, but with a
// special value of 100 even if not logged in as Admin (to debug Admin code
// for example; see file DEBUGGING)
if ($_SERVER['SERVER_NAME'] == 'localhost')
$config['localhost'] = 1;
if ($config['debug'] == 100)
errorlog(100);
else
if (userstate() || isset($config['admin']) || isset($config['localhost'])) {
if (($t = getvar('debug')) !== "")
$config['debug'] = $t;
if ($config['debug'])
errorlog($config['debug']);
}
debug('cookies: '.cookie_str());
// make sure directory variables end appropriately
foreach($config as $k => $v)
if (!is_array($v) && preg_match('/dir$/',$k) && $v)
$config[$k] = rtrim($v,'/') . '/';
if ($arg == '') $arg = 'root';
$config['section'] = $arg;
$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 reduce some inline string literals
/* 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);
}
/* the initialization function */
function _config() {
config();
}