index
























































































































<?php

/* USERCODE.PHP - manage "user" added code

"usercode" is code attached to a record and is 'eval'ed during the record 
(post) display process. You will probably not understand how it works by 
just looking at this code. Trust me. Yet it does work.

See file USERCODE.
*/


/* usercode - apply usercode to a post */

// can change $record; called solely by displayrecord() in DISPLAY.PHP

function usercode(&$record) {

    if (isset(
$record['usercode']))
        
_usercode_call($record['usercode'],$record);

    if (
_usercode('allposts') && !isset($record['noall']))
        
_usercode_call('allposts',$record);
}


/* _usercode_call - use external code to post-process a record */

// this runs fairly deep before it may realize that there is no user code,
// not too bad though...

function _usercode_call($codeid, &$record) {

    
debug("'$codeid'");

    if (
strstr($codeid,',')) {
        
$user_args splitntrim($codeid);
        
$codeid $user_args[0];
    }
    else
        
$user_args[0] = $codeid;

    
$uc _usercode($codeid);            // get the usercode
    
if ($uc === FALSE) {
        
$umsg "usercode:'$codeid' not found";
        if (
config('showusercodeerr'))
            
$record['body'] .= "<div class='note'>$umsg</div>";
        else
            
$record['body'] .= "<!-- $umsg -->";
        return;
    }

    
$r $uc($user_args,$record);            // call usercode

    
if ($r)                        // okay, use result
        
$record['body'] = $r;
}


/* _usercode_global - apply any '[global]' usercode */

// called once when this module is included

function _usercode_global() {

    if (
config('admin'))
        return;

    
$uc _usercode('global');

    if (
$uc) {
        
$r $uc();
        
debug('global:'.$r);
    }
}


/* _usercode - set/get user code code string */

function _usercode($codeid '') {
static 
$init 0;
static 
$user_code = array();

    if (!
$init) {
// the use of $init is that $user_code can be left uninitialized if there is 
// no usercode
        
$init 1;
        
$file themedir('usercode.ini');
// doesn't check for existence, returns array() if not found:
        
$code datafile($file);
 
        
$record createrecord('test','test');
        
$args '$user_args,$record';
        foreach (
$code as $name => $code) {
            if (eval(
$code) === FALSE) {
                
debug("'$name' parse error");
                continue;
                
// eval exits if fatal error (like function
            
}    //  not found)

            
debug("'$name'");
            if (
$name == 'global')
                
$f create_function('',$code);
            else
                
$f create_function($args,$code);
            
$user_code[$name] = $f;
        }

        
_usercode_global();
    }

    if (!isset(
$user_code[$codeid]))
        return 
FALSE;

    return 
$user_code[$codeid];
}


?>