index





















































































































































































































































































































































































































































<?php

/* CONVERT.PHP - post and comment text are "translated" through here 

The post texts are converted after they are read from the database, comments 
are converted before they are written to the database (posts are editable, 
comments are not).

The following public functions are defined:

convert($body);                // convert post text
convertchars($str);            // " post title and subject
convert_user($body);            // " comments

The functions starting with "gmlp" are a spin-off of the GMLP API and we 
thought no need to rename them all. The initialization function here is a bit 
different than for other modules -- the function _gmlp_open() is alled by 
the initialize function in DISPLAY.PHP.
*/


/* convert - convert post text accoring to TRANSLATE.INI */

function convert($data$record) {

    if (!
is_array($data))
        
$data explode(THIS_EOL,$data);

    
$p 0;                    // the one state flag
    
$body '';                         // return value

    
while (($_ array_shift($data)) !== NULL) {
        if (
$_ == '')
            continue;

        if (isset(
$record['notranslate']))
            goto 
skip;

        if (
gmlp_skip($_))
            goto 
skip;

        if (
gmlp_lines($_))
            goto 
skip2;

        if (
gmlp_convert_block($data,$_))
            goto 
skip2;

        
gmlp_line($_);
        
gmlp_codes($_);
        
gmlp_inlines($_);

        if (
config('entitytranslate'))
            
gmlp_entities($_);

        
gmlp_chars($_);

skip:        if ($p == 0)
            
$_ '<p>'.$_;
        else
            
$_ '<br>'.$_;

        
$p 1;

        if (isset(
$data[0]) && $data[0] == '') {
            
$_ .= '</p>';
skip2:            $p 0;
        }

        
$body .= $_.THIS_EOL;
    }

    if (
$p
        
$body .= '</p>';

    return 
$body;
}


/* convertchars - character translations */

function convertchars($str) {

    if (
config('entitytranslate'))
        
gmlp_entities($str);
    
gmlp_chars($str);

    return 
$str;
}


/* convert_user - conversions on comment body */

// there are fewer translations for comments
// this code needs updating to allow more formatting

function convert_user($data) {

    
$filter config('commentfilter');
    
$tags config('commenttags');
    
$atags explode_tags($tags);        // array of tags

    
if (config('logcommentdata'))        // this is experimental and
        
$commentdata $data;        //  is for some analysis
                        //  we are conducting
    
$data explode(THIS_EOL,$data);

    
// we do this line by line to get the paragraphing (with <p></p>)
    // right, but it is inefficient - this is all to be re-designed at
    // some point as some filtering is done in DISPLAY.PHP as well and 
    // that just adds to inefficiency (and complexity)

    
$body '';
    while (list( ,
$_) = each($data)) {
        if (
$_ == '') continue;

        if (
$filter == 'striptags')
            
$_ our_strip_tags($_,$tags,$atags);
        else
        if (
$filter == 'specialchars')
            
$_ htmlspecialchars($_,ENT_NOQUOTES);
        else
            
$_ convertchars($_);            // ' * ^ | _

        
$body .= '<p>'.$_.'</p>'.THIS_EOL;
    }

    if (isset(
$commentdata)) {
        
$commentdata str_replace('--','-',$commentdata);
        
$body .= '<!-- '.$commentdata.' -->';
    }

    return 
rtrim($body);
}


/* our_strip_tags - fix un-closed tags per line */

// loose end tags don't seem to be a problem with most web browsers

function our_strip_tags($_$tags$tagsarray) {

    
$_ preg_replace('/<\s+/','<',$_);    // strip_tags() errs if ' '
    
$_ strip_tags($_,$tags);        // can leave behind errant '>'s

    
if ($tags) {
        foreach (
$tagsarray as $a => $b) {
            
$A substr_count($_,$a);    // count and
            
$B substr_count($_,$b);
            while (
$A $B++)        //  append if missing
                
$_ .= $b;
        }
    }

    return 
$_;
}


/* explode_tags -  "<a><b>" to array("<a>"=>"</a>","<b>"=>"</b>") */

function explode_tags($tags) {
static 
$atags = array();

    if (!
$tags)
        return array();
    
    if (
$atags !== array())
        return 
$atags;

    
$a explode('>',$tags);
    
array_pop($a);
    foreach (
$a as $t)
        
$atags[$t.'>'] = preg_replace('/<(.*)/','</$1>',$t);

    return 
$atags;
}


/* the GMLP functions - see file GMLP */


/* gmlp_convert_str - do all the line conversions at once */

function gmlp_convert_str($_) {

    
gmlp_line($_);
    
gmlp_codes($_);
    
gmlp_inlines($_);

    if (
config('entitytranslate'))
        
gmlp_entities($_);

    
gmlp_chars($_);

    return 
$_;
}

function 
gmlp_skip(&$_) {

    foreach (
_gmlp('skip') as $s)
        if (
preg_match($s,$_,$res)) {
            if (isset(
$res[1]))
                
$_ $res[1];
            return 
TRUE;
        }

    return 
FALSE;
}

function 
gmlp_lines(&$_) {

    foreach (
_gmlp('lines') as $re => $repl)
        if (
preg_match($re,$_,$res)) {
            if (
$repl == '') {
                if (isset(
$res[1]))
                    
$_ $res[1];
                return 
TRUE;
            }
            if (
function_exists($repl)) {
                if (isset(
$res[1]))
                    
$_ $res[1];
                
$_ $repl($_);
                return 
TRUE;
            }
            if (isset(
$res[1]))
                
$_ gmlp_convert_str($_);
            
$_ preg_replace($re,$repl,$_);
            return 
TRUE;
        }

    return 
FALSE;
}

function 
gmlp_line(&$_) {

    foreach (
_gmlp('line') as $preg => $repl)
        
$_ preg_replace($preg,$repl,$_);
}

function 
gmlp_codes(&$_) {

    foreach (
_gmlp('codes') as $re => $func)
        if (
preg_match_all($re,$_,$res))
            foreach (
$res[0] as $k => $v)
                
$_ str_replace($v,$func($res[1][$k]),$_);
}

function 
gmlp_inlines(&$_) {

    foreach (
_gmlp('inlines') as $preg => $repl)
        
$_ preg_replace($preg,$repl,$_);
}

function 
gmlp_entities(&$_) {

    foreach (
_gmlp('entities') as $str => $replace)
        
$_ str_replace($str,$replace,$_);
}

function 
gmlp_chars(&$_) {

    foreach (
_gmlp('chars') as $k => $v)
        
$_ _gmlp_chars($_,'\\'.$k,$v);
    return 
$_;
}

// every time I try to merge this function in above I lose a brain cell...

function _gmlp_chars($str$char$tag) {

    
$repl '/([^a-zA-Z=;\/])'.$char.'(.*)'.$char.'([^a-zA-Z\/\]])/U';
    
$tags '$1<'.$tag.'>$2</'.$tag.'>$3';
    
$str ' '.$str.' ';                // put in two spaces!
    
$s preg_replace($repl,$tags,$str);

    
$s substr_replace($s,'',-1);            // get out the spaces!
    
$s substr_replace($s,'',0,1);

    return 
$s;
}

function 
gmlp_convert_block(&$data, &$s) {

    if (!(
$block _gmlp_lookup_block($s)))
        return 
FALSE;

    
$end $block['end'];        // use of these just eliminates
    
$pre = @$block['pre'];        //  some syntax
    
$post = @$block['post'];
    
$first = @$block['first'];
    
$last = @$block['last'];
    
$repl = @$block['replace'];
    
$convert = @$block['convert'];

    
$_ $s;

    if (
$first) {
        if (!
$convert)
            
gmlp_line($_);
        else
            
$_ gmlp_convert_str($_);

        if (
function_exists($repl))
            
$_ $repl($_);
        
$s $pre.$_.THIS_EOL;
    }
    else
        
$s $pre;

    while ((
$_ array_shift($data)) !== NULL) {
        if (
preg_match($end,$_))
            break;

        if (!
$convert)
            
gmlp_line($_);
        else
            
$_ gmlp_convert_str($_);

        if (
function_exists($repl))
            
$_ $repl($_);
        else
        if (
$repl)
            
$_ $_.$repl;

        
$s .= $_.THIS_EOL;
    }

    if (@
$block['continue'])
        
array_unshift($data,$_);

    if (
$last === 0)            // eat last newline
        
$s substr_replace($s,'',-1);

    if (
$last) {
        if (!
$convert)
            
gmlp_line($_);
        else
            
$_ gmlp_convert_str($_);
        
$s .= $_;
    }

    if (
function_exists($post))        // 'post' is either a function
        
$s $post($s);            //  or string to append
    
else                    //  might want to do more
        
$s .= $post;

    return 
TRUE;
}

function 
gmlp_open($data) {

    
_gmlp($data);
}


/* _gmlp - initialize (on first call) then return data array */

// should be initialized with an array (such as 'translate.ini'); trys to 
// not fail with errors if that does not happen; see also gmlp_add();
// if $var (data array member) is not defined return array() so that the 
// foreach blocks "do nothing"

function _gmlp($var ''$data NULL) {
static 
$init 0;
static 
$gmlp_translate = array();

    if (!
$init) {
        
$init 1;
        if (!
is_array($var))
            
debug("inavlid '$var' passed to GMLP");
        else
            
$gmlp_translate $var;
        return;
    }

    if (
$data) {
        foreach (
$data as $key => $value)
            
$gmlp_translate[$var][$key] = $value;
        return;
    }

    return (
$t = @$gmlp_translate[$var]) ? $t : array();
}

function 
_gmlp_lookup_block($_) {

    
$blocks _gmlp('blocks');
    foreach (
$blocks as $b)
        if (
preg_match($b['begin'],$_))
            return 
$b;
    return 
NULL;
}


// translation data support functions - called by entries in TRANSLATE.INI


function gmlp_convertcase($s) {
    
$s ucwords(strtolower($s));
    return 
'<p><b>'.$s.'</b></p>';        // note the Ps
}

function 
gmlp_highlightstr($s) {
    return 
highlight_string($s,TRUE);
}

/* highlighed block without the "<?php"/"?>" thingys */

function gmlp_highlightstr_php($s) {
    
$s "<?php\n$s\n?>";
    
$s highlight_string($s,TRUE);
    
$s str_replace('&lt;?php<br />','',$s);

// difference in highlight_string() when text ends in heredoc

    
if (preg_match('/#0000BB">\?&gt;<\/span>\n/',$s))
        
$replace "<span style=\"color: #0000BB\">?&gt;</span>\n";
    else
        
$replace "<br />?&gt;</span>\n";

    
$s str_replace($replace,'',$s);
    return 
$s;
}

function 
gmlp_list($s) {
    return 
'<li>'.$s;
}

function 
gmlp_dlist($s) {
    return 
preg_replace('/(\S+)(\s+)(.*)/','<dt>$1<dd>$3</dd>',$s);
}

function 
gmlp_pre($s) {
    return 
'<pre>'.$s.'</pre>';
}

?>