Source Code Index
<?php
 
/* RULES.PHP - website "rules" manager/handler
 
See file RULES for an explanation of the how and the why of this code.
 
Basically, INDEX.PHP calls here to "do" the site based on the GET variables
with what "to do" as a list of functions found in the RULES.INI file.
 
*/
 
_rules();					// self initialize
 
 
/* do_rules - this function runs the site */
 
// it it initially called by INDEX.PHP but can be called explicitly later
 
function do_rules($rule) {
 
	debug("rule '$rule'");
 
	$rules = _rules($rule);
 
	foreach ($rules as $rulestr) {
 
		// substitute any '$vars' with their $_GET values
 
		$rulestr = _getargs($rulestr);
 
		// convert any super globals
 
		if (preg_match('/{\$[_A-Z]*\[/',$rulestr)) {
			$rulestr = "\$rulestr = \"$rulestr\";";
			eval($rulestr);
		}
 
		// get function name and any argument(s)
 
		$args = explode(" ",$rulestr);
		$name = $args[0];
		array_shift($args);
 
		// look for "special" arguments
 
		foreach ($args as &$a)
			if ($a == "''")
				$a = '';
		unset($a);
 
		debug("'$name'");
		debug($args);
 
		// and call the function
 
		if (function_exists($name))
			call_user_func_array($name,$args);
		else
			debug("in rule '$rule', function '$name' not found");
	}
}
 
 
/* data_check - checks for valid objects */
 
function data_check() {
 
	$op = getvar('op');
	$id = getvar('id');
 
	if ($op == 'grok') {
		if (!is_numeric($id) || !dblistrecords($id))
			displaynotfound();			// exits
	}
 
	else
	if ($op == 'page') {
		if (preg_match('/[^a-zA-Z0-9]+/',$id) || !dblistpages($id))
			displaynotfound();
	}
}
 
 
/* data_adjust - adjust some HTML according to the object beinf viewed */
 
function data_adjust() {
 
	$op = getvar('op');
	$section = config('section');
	$arg = config('arg');
 
	if ($op == 'grok') {
		displaysetup();			// "caches" the post
		$record = html('record');
		$title = $section['name'].html('titlesep').$record['title'];
		if (config('arg') == 'root')
			$title = $record['title'];
	}
	elseif ($op == 'help') {
		$title = 'HELP'.html('titlesep').config('subtitle');
	}
 
	elseif (config('arg') == 'root')
		$title = config('subtitle');
	else {
		$title = $section['name'];
		if (isset($section['subtitle']))
			$title .= html('titlesep').$section['subtitle'];
	}
 
	html('title',html('title').html('titlesep').$title);
	html('metadescription',$section['description']);
}
 
 
function _validate() {
 
	// 'arg' is checked by the CONFIG.PHP code
 
	$default = _rules('default');
 
	if (!isset($_GET['op']) || !_rules($_GET['op']))
		$_GET['op'] = $default;
 
	if ($_GET['op'] == 'preview')		// internal use only
		$_GET['op'] = $default;
 
	$op = $_GET['op'];
	$id = (isset($_GET['id'])) ? $_GET['id'] : '';
 
	if ($op == 'grok' && !$id)
		$_GET['op'] = $default;
 
	if ($op == 'page' && !$id)
		$_GET['op'] = $default;
 
	if ($op == 'submit' && (!$id || !is_numeric($id)))
		$_GET['op'] = $default;
}
 
 
/* _getargs - convert arguments */
 
// replace '$var' with "$_GET['var']"
 
function _getargs($_) {
 
	$re = '/\$([a-zA-Z]+)/';
	if (preg_match_all($re,$_,$res))
		foreach ($res[0] as $k => $v)
			if (isset($_GET[$res[1][$k]]))
				$_ = str_replace($v,$_GET[$res[1][$k]],$_);
	return $_;
}
 
 
/* _rules - initialize the rules data and/or return it */
 
function _rules($rule = NULL) {
static $rules = array();
 
	if ($rules == array()) {
 
		$rules = _loadrules('rules.ini');
		if ($rules == array())
			user_error("'rules.ini' was not found",E_USER_ERROR);
 
		_validate();		// validate all URL data
		return;
	}
 
	if ($rule == NULL)
		return $rules;
 
	if (!isset($rules[$rule])) {
		debug("'$rule' not found");
		return $rules['default'];
	}
	return $rules[$rule];
}
 
 
/* _loadrules - the rules array is an associative array of arrays */
 
// see RULES.INI
 
function _loadrules($file) {
 
	$data = @file($file);
	if (!$data)
		return array();
 
	$data = _parserules($data);
 
	foreach ($data as $rule => $value) {
 
		if (!is_array($value))
			$rules[$rule] = $value;
		else
		foreach ($value as $_)
			$rules[$rule][] = $_;
	}
 
	return $rules;
}
 
 
/* _parserules - parse the RULES data file */
 
function _parserules($file) {
 
	$section = "";
 
	while (list (, $l) = each ($file)) {
		$l = chop($l);
		if (preg_match('/^;/',$l) || $l == '')
			continue;
 
		if (preg_match('/^\[(.*)\]/',$l,$res)) {
			$section = $res[1];
			continue;
		}
 
		if (!$section) {
			preg_match('/(.*)\s*=\s*(.*)/',$l,$res);
			$data[trim($res[1])] = $res[2];
		}
		else
			$data[$section][] = $l;
	}
 
	return $data;
}
 
?>
 
THIS source compiler