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 */

function do_rules($rule) {

    
debug("'$rule'");

    
$rules _rules();

    if (
$rules == NULL)
        return 
$rules;

    if (!isset(
$rules[$rule])) {
        
debug("rule '$rule' not found");
        return;
    }

    foreach (
$rules[$rule] 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");
    }
}


/* _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) {
            
$t = @$_GET[$res[1][$k]];
            
$_ str_replace($v,$t,$_);
        }
    }

    return 
$_;
}


/* _rules - initialize the rules data and/or return it */

function _rules() {
static 
$rules = array();

    if (
$rules == array()) {

        
$rules _loadrules('rules.ini');
        if (
$rules == array())
            
user_error("'rules.ini' was not found",E_USER_ERROR);

        
// check for invalid 'op'

        
if (!isset($_GET['op']) || !isset($rules[$_GET['op']]))
            
$_GET['op'] = $rules['default'];

        if (
$_GET['op'] == 'preview')            // internal use
            
$_GET['op'] = $rules['default'];

        
// 'arg' is checked by the CONFIG.PHP code

        // these just check for invalid data and not for the
        //  existence of objects

        
$op $_GET['op'];
        if (
$op == 'grok' || $op == 'submit') {
            if (!isset(
$_GET['id']) || !is_numeric($_GET['id']))
                
$_GET['op'] = $rules['default'];
        }

        if (
$_GET['op'] == 'page') {
            if (!isset(
$_GET['id']) || preg_match('/[^a-zA-Z0-9]+/',$_GET['id']))
                
$_GET['op'] = $rules['default'];
        }

        
// we cannot check for object existence here right now, they
        // are checked at the function level - see displaypage() for
        // example how it displays a "Page not found" message; there
        // will eventually be rules created for such things, further
        // reducing code complexity (and possibly size)
    
}

    return 
$rules;
}


/* _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;
}

?>