index

















































































































































































































































<?php

/* DATA.PHP - misc. functions to load data */


/* load_php_file - safely load an INI file in PHP format */

// this loads files like TRANSLATE.INI and TEMPLATES.PHP

function load_php_file($file) {

    
debug("'$file'");

    
$data = @file_get_contents($file);
    if (
$data === FALSE) {
        if (
config('failnotemplate'))
            
user_error("'$file' was not found",E_USER_ERROR);
        
debug("'$file' was not found");
        return 
NULL;
    }

    
ob_start();
    
$data = eval($data);
    
$out ob_get_contents();
    
ob_end_clean();
    if (
$data === FALSE) {
        if (
config('failnotemplate'))
            exit(
$out);
        if (
config('showerrors'))
            
error($out);
        
debug(strip_errtags($out));
    }

    return 
$data;
}


function 
datafile($file) {

    if (!(
$file = @file($file)))
        return array();

    
$data = array();
    
$section '';

    foreach (
$file as $l) {
        if (
trim($l) == '' || ltrim($l[0]) == ';')
            continue;

        if (
preg_match('/^\[\s*(.*)\s*\]/',$l,$res)) {
            
$section $res[1];
            
$data[$section] = '';
            continue;
        }

        if (!
$section)
            continue;

        
$data[$section] .= $l;
    }

    return 
$data;
}


// configfile - read configuration data into an associative array
//
// $file        file to parse
// $thisconfig  optional array to use
// $translate   optional boolean to ignore case (set all vars lower case)
//
// Returns: array (empty if file not found or file has no data)
//
// NOTES:
//
// This works just like typical INI parsing, with exceptions; quotes
// are never needed to wrap values, values can contain equal signs (=),
// if keys do not have equal signs and values it is assumed that a
// "section" is simply an array, if a section is blank ([]) increasing
// numbers are used, superglobals (in {}s) in values are evaluated.
//
// This was written a long time ago -- I think we were using DOS boxes
// back then -- and is like the longest function ever written... It's
// okay though, and it works. (It was recently enhanced.)

// NOTE there is a certain part of this function that looks really bad
// (you'll know it when you see it); not much can be done to fix it
// without just removing the consistency checks and the diagnostics

// NOTE NOTE This function is going to go away as it is an affront to
// humanity... (April, 2013)

function configfile($file$thisconfig NULL$translate FALSE) {

    if (
$thisconfig)
        
$config $thisconfig;
    else
        
$config = array();

    if (
is_array($file)) {
        
$f $file;
    }
    else {
        if (!
is_file($file))
            return 
$config;
        
$f file($file);
    }

    
$line 0;            // file line number
    
$secount 0;            // section counter
    
$section "";            // section name
    
$insection "";        // subsection name

    
foreach ($f as $_) {
        
$line++;
        
$_ chop($_);
        if (
$_ == '' || $_[0] == ';')
            continue;

        if (
preg_match('/^\s*\[(.*)\]/',$_,$res)) {
            
$res[1] = trim($res[1]);

/* the following (really horrible) block is just to support [:name] as an
array within a section; the two awful tests are just to report overwrites
like:
    [section]
    a = foo
    [:a]        <= overwrite of 'a'
    b = bar

*/
            
if ($res[1] && $res[1][0] == ':') {
                
$insection ltrim($res[1],': ');
                if (
$section === "") {
                    
$section $insection;
                    
$insection "";
                    
_configerr(0,$section,$line,$f);
                    if (isset(
$config[$section]) && !is_array($config[$section])) {
                        
_configerr(1,$section,$line,$f);
                        unset(
$config[$section]);
                    }
                }
                if (isset(
$config[$section][$insection]) && !is_array($config[$section][$insection])) {
                    
_configerr(2,$insection,$line,$f);
                    unset(
$config[$section][$insection]);
                }
                continue;
            }
/* end (really horrible) block */

            // NOTE section can be int(0) if "[]" seen first
            
$section = ($res[1]) ? $res[1] : $secount++;

            if (isset(
$config[$section]) && !is_array($config[$section])) {
                
_configerr(3,$section,$line,$f);
                unset(
$config[$section]);
            }

            
$insection "";
            continue;
        }

        
$aa explode('=',$_);
        
$noe = (count($aa) == 1);
        
$a array_shift($aa);
        
$a trim($a);
        
$b implode('=',$aa);        // $b can be '', let slide
        
$b str_replace("\n",'\n',$b);
        
$b trim($b);

        if (
is_empty($a))
            continue;

        if (
$a[strlen($a)-1] == '.') {
            
$dote 1;
            
$a rtrim($a,' .');
        }

        
// this hooks up superglobals; so as to not let them mess up
        // the display if there is an error, turn off the "Undefined
        // variable" notice

        
error_reporting(E_ALL E_NOTICE);

        if (
preg_match('/{\$[_A-Z]*\[/',$b)) {
            
$b "\$b = \"$b\";";
            eval(
$b);
        }

        
error_reporting(E_ALL);

        if (
defined($b))
            
$b constant($b);

        if (
$translate === TRUE)
            
$a strtolower($a);

        if (
$insection) {
            if (
is_empty($b) && $noe)
                
$config[$section][$insection][] = $a;
            else
                
$config[$section][$insection][$a] = $b;
            continue;
        }

        if (
$section !== "") {
            if (
is_empty($b) && $noe)
                
$config[$section][] = $a;
            else
                
$config[$section][$a] = $b;
            continue;
        }

        if (isset(
$dote)) {
            if (isset(
$config[$a])) {
                
$config[$a] .= $b;
                continue;
            }
            
// fall through
        
}

        
$config[$a] = $b;
    }

    return 
$config;
}

function 
_configerr($err$var$line$file) {

    
$e[0] = "sub section '$var' without section; making section";
    
$e[1] = "'$var' already defined";
    
$e[2] = $e[1];
    
$e[3] = $e[1];

    
$s "ERR$err {$e[$err]}, line: $line ({$file[$line-1]})";
    if (!
function_exists('debug')) echo $s;
    else 
debug($s);
}

?>