index
















































































































































































































































































<?php

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

function if_strip_tags($s) {
// this one is temporary
    
if (!config('commentfilter'))
        
$s my_strip_tags($s);
    return 
$s;
}


function 
my_strip_tags($s) {

    
$s preg_replace('/<\s+/','<',$s);
    
$s strip_tags($s);
    return 
str_replace('>','',$s);
}

function 
strip_errtags($s) {

    
$s str_replace(array('<br />','<b>','</b>',"\n"),'',$s);
    return 
str_replace($_SERVER['DOCUMENT_ROOT'],'',$s);
}

function 
php_error(&$file NULL, &$line NULL) {

    
$e error_get_last();
    
$s preg_replace('/ \[.*\]/','',$e['message']);

    
$file basename($e['file']);
    
$line $e['line'];

    return 
$s;
}

// OB callbacks

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

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

function 
strip_htmlcomments($s) {
    return 
preg_replace('/<!--.*-->/','',$s);
}

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 ', ') {

    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;
}

/* flatten - an implode() that handles associative arrays */

function flatten($a) {

    if (isset(
$a[0]) && !is_array($a[0]))
        return 
implode(',',$a);

    
$s '';
    foreach (
$a as $k => $v) {
        if (
is_array($v))
            
$s .= "$k=".flatten($v);
        else
            
$s .= "$k='$v',";
    }

    return 
rtrim($s,',');
}


// very helpful for a quick debug
//
//    $s is a "title", $p is wrap in '<pre></pre>', $h is use htmlenties()

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

    
$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 (
$d == 3)
        
var_export($var);
    else
        if (
is_array($var))
            
parray($var);
        else
            print 
$var;

    
$out ob_get_contents();
    
ob_end_clean();
    if (
$h)
        
$out htmlentities($out,ENT_NOQUOTES);
    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;
}

// we keep doing something like this so let's make it a function

function ob_run($func$a$b, &$out) {

    
ob_start();
    
$r $func($a,$b);
    
$out ob_get_contents();
    
ob_end_clean();
    return 
$r;
}

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>";
}

?>