inc/base.php - index































































































































































































































































































































































































































<?php

/* just a few goofy functions that do odd things */

// OB callbacks

function obentities($buffer) {
    return 
htmlentities($buffer);
}

function 
obcomments($buffer) {
    return 
str_replace('--','-',str_replace('-->','',$buffer));
}


function 
array_delete(&$array$value) {

    foreach (
$array as $a => $b) {
        if (
$b == $value) {
            unset(
$array[$a]);
            break;
        }
    }
}

function 
my_glob($dir$ext '') {

    
$files glob($dir);
    
$files str_replace(dirname($dir).'/','',$files);
    if (
$ext$files str_replace($ext,'',$files);
    return 
$files;
}

function 
print_e($a$c ', ') {

    
$n count($a)-1;
    foreach (
$a as $b)
        echo 
$b.(($n--) ? $c '');
}
//    $s = '';
//    foreach ($a as $b)
//        $s .= "$b$c";
//    echo rtrim($s,$c);
//
//    echo rtrim(implode($c,$a),$c);

function collapse($a$no 40) {

    
$s '';
    foreach (
$a as $k => $v) {
        
$v htmlentities((!$no)?$v:substr($v,0,$no));
        
$s .= "<b>$k=</b>'$v' ";
    }
    return 
$s;
}

function 
dump($var$s ''$p 0$h 0) {

    
$d config('dumptype');

    if (!
$p) print '<pre>';
    print 
$s;

    
ob_start();

    if (!
$d) {
            if (
is_array($var))
            
print_r($var);
            else
                    
var_dump($var);
    }
    else
    if (
$d == 1)
        
var_dump($var);
    else
    if (
$d == 2)
        
my_var_dump($var);
    else
            if (
is_array($var))
            
parray($var);
            else
            print 
$var;

    
$out ob_get_contents();
    
ob_end_clean();
    if (
$h)
        
$out htmlentities($out);
    echo 
$out;
    if (!
$p) print '</pre>';
}

function 
my_var_dump($var$ret 0) {
    
ob_start();
    
var_dump($var);
    
$out ob_get_contents();
    
ob_end_clean();
    
$out str_replace("=>\n "," =>",$out);
    if (
$ret)
        return 
$out;
    echo 
$out;
}

function 
parray($a) {

    if (isset(
$a[0]))
        echo 
implode('<br>',$a);
    else
    foreach (
$a as $k => $v)
        echo 
"$k = $v<br>";
}


/* get who called the caller */

// list($file,$line,$func) = caller();
// print "file: $file, line: $line, function: $func";

// this is a real handy function for helping track execution flow
// (sometimes the file and line numbers are not known to PHP; like 
// via call_user_func())

// best use is:
//     list($file,$line,$func) = caller(1);
//     $func = caller(2,'function');
// which gets the correct function that called

// but if you want a function to return who called use this:

function getcaller(&$file, &$line, &$func) {

    list(
$file,$line,$func) = caller(2);
    
$func caller(3,'function');
}

function 
caller($depth 1$elem NULL) {

    if (
defined('DEBUG_BACKTRACE_IGNORE_ARGS'))
        
$bt debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS);
    else
        
$bt debug_backtrace();

    if (
$elem) {
        
$s = (@$bt[$depth][$elem]) ? $bt[$depth][$elem] : '';
        if (
$elem == 'file')
            
$s basename($s);
        return 
$s;
    }

    if (!isset(
$bt[$depth]))
        return(array(
'noindex:',$depth,''));

    
$bt $bt[$depth];
    
$file = (@$bt['file']) ? basename($bt['file']) : '';
    
$line = (@$bt['line']) ? $bt['line'] : '';
    
$func $bt['function'];

    if (
$elem === '')
        return 
"$file,$line,$func";

    return array(
$file,$line,$func);
}

function 
splitntrim($string$delim ',') {

    
$a = array();
        foreach (
explode($delim,$string) as $s)
        
$a[] = trim($s);
        return 
$a;
}

// is_empty - true if var is "empty"

// this is because PHP's loose tests treat "0" as empty and is_null() 
// does not think "" is null (which it ain't of course), but we want to 
// detect "" as not correct and "0" as okay

function is_empty($v) {

        return (
$v === "" || is_null($v));
}


// identify - useful for learning how PHP does things

function identify($var) {

        print 
"<tt>";
        print 
"$var<br>";
        if (!
$var) print "!<br>";
        if (!isset(
$var)) print "!"; print "isset<br>";
        if (!
is_null($var)) print "!"; print "is_null<br>";
        if (!
is_empty($var)) print "!"; print "is_empty<br>";
        if (!
is_numeric($var)) print "!"; print "is_numeric<br>";
        if (!
is_array($var)) print "!"; print "is_array<br>";
        print (
$var === 0) ? "===" "!=="; print " 0<br>";
        print (
$var === "") ? "===" "!=="; print " \"\"<br>";
        print (
$var === NULL) ? "===" "!=="; print " NULL<br>";
        print (
$var === TRUE) ? "===" "!=="; print " TRUE<br>";
        print (
$var === FALSE) ? "===" "!=="; print " FALSE<br>";
        print (
$var === array()) ? "===" "!=="; print " array()<br>";

        print 
"<br>";
        print 
"<br>";
        print 
"</tt>";
}

function 
datafile($file) {

    if (!(
$file = @file($file)))
        return 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)) {
                        if (
function_exists('debug'))
                
debug("'$file' not found");
                        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);
        
$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);
}

?>