Text processor module
| > Module written by Matt Mecham
| > Official Version: 1.2 - Number of changes to date 3 billion (estimated)
|
+--------------------------------------------------------------------------
*/
class post_parser {
var $error = "";
var $image_count = 0;
var $emoticon_count = 0;
var $quote_html = array();
var $quote_open = 0;
var $quote_closed = 0;
var $quote_error = 0;
var $emoticons = "";
var $badwords = "";
var $strip_quotes = "";
var $in_sig = "";
var $allow_unicode = 1;
function smilie_length_sort($a, $b)
{
if ( strlen($a['typed']) == strlen($b['typed']) )
{
return 0;
}
return ( strlen($a['typed']) > strlen($b['typed']) ) ? -1 : 1;
}
function word_length_sort($a, $b)
{
if ( strlen($a['type']) == strlen($b['type']) )
{
return 0;
}
return ( strlen($a['type']) > strlen($b['type']) ) ? -1 : 1;
}
function post_parser($load=0)
{
global $ibforums, $DB;
$this->strip_quotes = $ibforums->vars['strip_quotes'];
if ($load != 0)
{
// Pre-load the bad words
$DB->query("SELECT * from ibf_badwords");
if ( $DB->get_num_rows() )
{
while ( $r = $DB->fetch_row() )
{
$this->badwords[] = array( 'type' => stripslashes($r['type']),
'swop' => stripslashes($r['swop']),
'm_exact' => $r['m_exact'],
);
}
}
else
{
$this->no_bad_words = 1;
}
// Pre-load the smilies
$this->emoticons = array();
$DB->query("SELECT typed, image from ibf_emoticons");
if ( $DB->get_num_rows() )
{
while ( $r = $DB->fetch_row() )
{
$this->emoticons[] = array( 'typed' => stripslashes($r['typed']),
'image' => stripslashes($r['image']),
'clickable' => $r['clickable'],
);
}
}
}
}
/**************************************************/
// PARSE POLL TAGS
// Converts certain code tags for polling
/**************************************************/
function parse_poll_tags($txt)
{
// if you want to parse more tags for polls, simply cut n' paste from the "convert" routine
// anywhere here.
$txt = preg_replace( "#\[img\](.+?)\[/img\]#ie" , "\$this->regex_check_image('\\1')", $txt );
$txt = preg_replace( "#\[url\](\S+?)\[/url\]#ie" , "\$this->regex_build_url(array('html' => '\\1', 'show' => '\\1'))", $txt );
$txt = preg_replace( "#\[url\s*=\s*\"\;\s*(\S+?)\s*\"\;\s*\](.*?)\[\/url\]#ie" , "\$this->regex_build_url(array('html' => '\\1', 'show' => '\\2'))", $txt );
$txt = preg_replace( "#\[url\s*=\s*(\S+?)\s*\](.*?)\[\/url\]#ie" , "\$this->regex_build_url(array('html' => '\\1', 'show' => '\\2'))", $txt );
return $txt;
}
/**************************************************/
// convert:
// Parses raw text into smilies, HTML and iB CODE
/**************************************************/
function convert($in=array( 'TEXT' => "", 'SMILIES' => 0, 'CODE' => 0, 'SIGNATURE' => 0, 'HTML' => 0)) {
global $ibforums, $DB;
$this->in_sig = $in['SIGNATURE'];
$txt = $in['TEXT'];
//--------------------------------------
// Returns any errors as $this->error
//--------------------------------------
// Remove session id's from any post
$txt = preg_replace( "#(\?|&|;|&)s=([0-9a-zA-Z]){32}(&|;|&|$)?#e", "\$this->regex_bash_session('\\1', '\\3')", $txt );
//--------------------------------------
// convert
to \n
//--------------------------------------
$txt = preg_replace( "/
|
/", "\n", $txt );
//--------------------------------------
// Are we parsing iB_CODE and do we have either '[' or ']' in the
// text we are processing?
//--------------------------------------
if ( $in['CODE'] == 1 ) {
//---------------------------------
// Do [CODE] tag
//---------------------------------
$txt = preg_replace( "#\[code\](.+?)\[/code\]#ies", "\$this->regex_code_tag('\\1')", $txt );
//--------------------------------------
// Auto parse URLs
//--------------------------------------
$txt = preg_replace( "#(^|\s)((http|https|news|ftp)://\w+[^\s\[\]]+)#ie" , "\$this->regex_build_url(array('html' => '\\2', 'show' => '\\2', 'st' => '\\1'))", $txt );
//---------------------------------
// Do [QUOTE(name,date)] tags
//---------------------------------
// Find the first, and last quote tag (greedy match)...
$txt = preg_replace( "#(\[quote(.+?)?\].*\[/quote\])#ies" , "\$this->regex_parse_quotes('\\1')" , $txt );
/***********************************************/
// If we are not parsing a siggie, lets have a bash
// at the [PHP] [SQL] and [HTML] tags.
/***********************************************/
if ($in['SIGNATURE'] != 1) {
$txt = preg_replace( "#\[sql\](.+?)\[/sql\]#ies" , "\$this->regex_sql_tag('\\1')" , $txt );
$txt = preg_replace( "#\[html\](.+?)\[/html\]#ies" , "\$this->regex_html_tag('\\1')" , $txt );
//-------------------------
// [LIST] [*] [/LIST]
//-------------------------
while( preg_match( "#\n?\[list\](.+?)\[/list\]\n?#ies" , $txt ) )
{
$txt = preg_replace( "#\n?\[list\](.+?)\[/list\]\n?#ies", "\$this->regex_list('\\1')" , $txt );
}
while( preg_match( "#\n?\[list=(a|A|i|I|1)\](.+?)\[/list\]\n?#ies" , $txt ) )
{
$txt = preg_replace( "#\n?\[list=(a|A|i|I|1)\](.+?)\[/list\]\n?#ies", "\$this->regex_list('\\2','\\1')" , $txt );
}
}
//---------------------------------
// Do [IMG] [FLASH] tags
//---------------------------------
if ($ibforums->vars['allow_images'])
{
$txt = preg_replace( "#\[img\](.+?)\[/img\]#ie" , "\$this->regex_check_image('\\1')" , $txt );
$txt = preg_replace( "#(\[flash=)(\S+?)(\,)(\S+?)(\])(\S+?)(\[\/flash\])#ie", "\$this->regex_check_flash('\\2','\\4','\\6')", $txt );
}
// Start off with the easy stuff
$txt = preg_replace( "#\[b\](.+?)\[/b\]#is", "\\1", $txt );
$txt = preg_replace( "#\[i\](.+?)\[/i\]#is", "\\1", $txt );
$txt = preg_replace( "#\[u\](.+?)\[/u\]#is", "\\1", $txt );
$txt = preg_replace( "#\[s\](.+?)\[/s\]#is", "\\1", $txt );
// (c) (r) and (tm)
$txt = preg_replace( "#\(c\)#i" , "©" , $txt );
$txt = preg_replace( "#\(tm\)#i" , "" , $txt );
$txt = preg_replace( "#\(r\)#i" , "®" , $txt );
// email tags
// [email]matt@index.com[/email] [email=matt@index.com]Email me[/email]
$txt = preg_replace( "#\[email\](\S+?)\[/email\]#i" , "\\1", $txt );
$txt = preg_replace( "#\[email\s*=\s*\"\;([\.\w\-]+\@[\.\w\-]+\.[\.\w\-]+)\s*\"\;\s*\](.*?)\[\/email\]#i" , "\\2", $txt );
$txt = preg_replace( "#\[email\s*=\s*([\.\w\-]+\@[\.\w\-]+\.[\w\-]+)\s*\](.*?)\[\/email\]#i" , "\\2", $txt );
// url tags
// [url]http://www.index.com[/url] [url=http://www.index.com]ibforums![/url]
$txt = preg_replace( "#\[url\](\S+?)\[/url\]#ie" , "\$this->regex_build_url(array('html' => '\\1', 'show' => '\\1'))", $txt );
$txt = preg_replace( "#\[url\s*=\s*\"\;\s*(\S+?)\s*\"\;\s*\](.*?)\[\/url\]#ie" , "\$this->regex_build_url(array('html' => '\\1', 'show' => '\\2'))", $txt );
$txt = preg_replace( "#\[url\s*=\s*(\S+?)\s*\](.*?)\[\/url\]#ie" , "\$this->regex_build_url(array('html' => '\\1', 'show' => '\\2'))", $txt );
// font size, colour and font style
// [font=courier]Text here[/font] [size=6]Text here[/size] [color=red]Text here[/color]
while ( preg_match( "#\[size=([^\]]+)\](.+?)\[/size\]#ies", $txt ) )
{
$txt = preg_replace( "#\[size=([^\]]+)\](.+?)\[/size\]#ies" , "\$this->regex_font_attr(array('s'=>'size','1'=>'\\1','2'=>'\\2'))", $txt );
}
while ( preg_match( "#\[font=([^\]]+)\](.*?)\[/font\]#ies", $txt ) )
{
$txt = preg_replace( "#\[font=([^\]]+)\](.*?)\[/font\]#ies" , "\$this->regex_font_attr(array('s'=>'font','1'=>'\\1','2'=>'\\2'))", $txt );
}
while( preg_match( "#\[color=([^\]]+)\](.+?)\[/color\]#ies", $txt ) )
{
$txt = preg_replace( "#\[color=([^\]]+)\](.+?)\[/color\]#ies" , "\$this->regex_font_attr(array('s'=>'col' ,'1'=>'\\1','2'=>'\\2'))", $txt );
}
}
// Swop \n back to
$txt = preg_replace( "/\n/", "
", $txt );
// Unicode?
if ( $this->allow_unicode )
{
$txt = preg_replace("/&#([0-9]+);/s", "\\1;", $txt );
}
//+---------------------------------------------------------------------------------------------------
// Parse smilies (disallow smilies in siggies, or we'll have to query the DB for each post
// and each signature when viewing a topic, not something that we really want to do.
//+---------------------------------------------------------------------------------------------------
if ($in['SMILIES'] != 0 and $in['SIGNATURE'] == 0) {
$txt = ' '.$txt.' ';
if ( ! is_array($this->emoticons) )
{
$DB->query("SELECT typed, image from ibf_emoticons");
$this->emoticons = array();
if ( $DB->get_num_rows() )
{
while ( $r = $DB->fetch_row() )
{
$this->emoticons[] = array( 'typed' => stripslashes($r['typed']),
'image' => stripslashes($r['image']),
'clickable' => $r['clickable'],
);
}
}
}
usort($this->emoticons, array( 'post_parser', 'smilie_length_sort' ) );
if ( count($this->emoticons) > 0 )
{
foreach($this->emoticons as $a_id => $row)
{
$code = $row['typed'];
$image = $row['image'];
// Make safe for regex
$code = preg_quote($code, "/");
$txt = preg_replace( "!(?<=[^\w&;/])$code(?=.\W|\W.|\W$)!ei", "\$this->convert_emoticon('$code', '$image')", $txt );
}
}
if ($ibforums->vars['max_emos'])
{
if ($this->emoticon_count > $ibforums->vars['max_emos'])
{
$this->error = 'too_many_emoticons';
// Uncovert the little yellow chappies
//$txt = preg_replace( "#.+?#", "\\1" , $txt );
}
}
}
$txt = $this->bad_words($txt);
return $txt;
}
//--------------------------------------------------------------
// Post DB parse tags
// ...................
//--------------------------------------------------------------
function post_db_parse($t="", $use_html=0)
{
global $ibforums, $DB;
if ( $use_html )
{
$t = preg_replace( "#\[dohtml\](.+?)\[/dohtml\]#ies", "\$this->parse_html('\\1')", $t );
}
else
{
$t = preg_replace( "#(\[dohtml\])(.+?)(\[/dohtml\])#ies", "\$this->my_strip_tags('\\2')", $t );
}
return $t;
}
//---------------------------------------------------------------
// My strip-tags. Converts HTML entities back before strippin' em
//---------------------------------------------------------------
function my_strip_tags($t="")
{
$t = str_replace( '>', '>', $t );
$t = str_replace( '<', '<', $t );
$t = strip_tags($t);
// Make sure nothing naughty is left...
$t = str_replace( '<', '<', $t );
$t = str_replace( '>', '>', $t );
return $t;
}
//--------------------------------------------------------------
// Word wrap, wraps 'da word innit
//--------------------------------------------------------------
function my_wordwrap($t="", $chrs=0, $replace="
")
{
if ( $t == "" )
{
return $t;
}
if ( $chrs < 1 )
{
return $t;
}
$t = preg_replace("#([^\s<>'\"/\.\\-\?&\n\r\%]{".$chrs."})#i", " \\1".$replace ,$t);
return $t;
}
//--------------------------------------------------------------
// parse_html
// Converts the doHTML tag
//--------------------------------------------------------------
function parse_html($t="", $do_br=1)
{
if ( $t == "" )
{
return $t;
}
// Remove
s 'cos we know they can't
// be user inputted, 'cos they are still
// <br> at this point :)
if ( $do_br == 1 )
{
$t = str_replace( "
" , "\n" , $t );
$t = str_replace( "
" , "\n" , $t );
}
$t = str_replace( "'" , "'", $t );
$t = str_replace( "!" , "!", $t );
$t = str_replace( "$" , "$", $t );
$t = str_replace( "|" , "|", $t );
$t = str_replace( "&" , "&", $t );
$t = str_replace( ">" , ">", $t );
$t = str_replace( "<" , "<", $t );
$t = str_replace( """ , '"', $t );
// Take a crack at parsing some of the nasties
// NOTE: THIS IS NOT DESIGNED AS A FOOLPROOF METHOD
// AND SHOULD NOT BE RELIED UPON!
$t = preg_replace( "/alert/i" , "alert" , $t );
$t = preg_replace( "/onmouseover/i", "onmouseover", $t );
$t = preg_replace( "/onclick/i" , "onclick" , $t );
$t = preg_replace( "/onload/i" , "onload" , $t );
$t = preg_replace( "/onsubmit/i" , "onsubmit" , $t );
return $t;
}
//--------------------------------------------------------------
// Badwords:
// Swops naughty, naugty words and stuff
//--------------------------------------------------------------
function bad_words($text = "")
{
global $DB, $ibforums;
if ($text == "")
{
return "";
}
if ( $this->no_bad_words == 1 )
{
return $text;
}
//--------------------------------
if ( ! is_array($this->badwords) )
{
$DB->query("SELECT * from ibf_badwords");
$this->badwords = array();
if ( $DB->get_num_rows() )
{
while ( $r = $DB->fetch_row() )
{
$this->badwords[] = array( 'type' => stripslashes($r['type']),
'swop' => stripslashes($r['swop']),
'm_exact' => $r['m_exact'],
);
}
}
}
usort($this->badwords, array( 'post_parser', 'word_length_sort' ) );
if ( count($this->badwords) > 0 )
{
foreach($this->badwords as $idx => $r)
{
if ($r['swop'] == "")
{
$replace = '######';
}
else
{
$replace = $r['swop'];
}
//---------------------------
$r['type'] = preg_quote($r['type'], "/");
//---------------------------
if ($r['m_exact'] == 1)
{
$text = preg_replace( "/(^|\b)".$r['type']."(\b|!|\?|\.|,|$)/i", "$replace", $text );
}
else
{
$text = preg_replace( "/".$r['type']."/i", "$replace", $text );
}
}
}
return $text;
}
/**************************************************/
// unconvert:
// Parses the HTML back into plain text
/**************************************************/
function unconvert($txt="", $code=1, $html=0) {
$txt = preg_replace( "#.+?#", "\\1" , $txt );
if ($code == 1)
{
$txt = preg_replace( "#(.+?)(.+?)(.+?)#eis" , "\$this->unconvert_sql(\"\\2\")", $txt);
$txt = preg_replace( "#(.+?)(.+?)(.+?)#e", "\$this->unconvert_htm(\"\\2\")", $txt);
$txt = preg_replace( "#.+?#e" , "\$this->unconvert_flash('\\1')", $txt );
$txt = preg_replace( "##" , "\[IMG\]\\1\[/IMG\]" , $txt );
$txt = preg_replace( "#(.+?)#" , "\[EMAIL=\\1\]\\2\[/EMAIL\]" , $txt );
$txt = preg_replace( "#(.+?)#" , "\[URL=\\1\\2\]\\3\[/URL\]" , $txt );
$txt = preg_replace( "#(.+?)#" , '[QUOTE]' , $txt );
$txt = preg_replace( "#(.+?)#" , "[QUOTE=\\1,\\2]" , $txt );
$txt = preg_replace( "#(.+?)#" , "[QUOTE=\\1]" , $txt );
$txt = preg_replace( "#(.+?)#" , '[/QUOTE]' , $txt );
$txt = preg_replace( "#(.+?)#", '[CODE]' , $txt );
$txt = preg_replace( "#(.+?)#", '[/CODE]' , $txt );
$txt = preg_replace( "#(.+?)#is" , "\[i\]\\1\[/i\]" , $txt );
$txt = preg_replace( "#(.+?)#is" , "\[b\]\\1\[/b\]" , $txt );
$txt = preg_replace( "#
(.+?)#is" , "\[s\]\\1\[/s\]" , $txt );
$txt = preg_replace( "#(.+?)#is" , "\[u\]\\1\[/u\]" , $txt );
$txt = preg_replace( "#(\n){0,}
| {$possible_use[$in[STYLE]][1]} {$in[EXTRA]} |
| ", 'END' => " |
tags
/**************************************************/
function regex_preserve_spacing($txt="")
{
$txt = preg_replace( "#\s{2}#", " ", trim($txt) );
return $txt;
}
/**************************************************/
// regex_simple_quote_tag: Builds this quote tag HTML
// [QUOTE] .. [/QUOTE]
/**************************************************/
function regex_simple_quote_tag() {
global $ibforums;
$this->quote_open++;
return "{$this->quote_html['START']}";
}
/**************************************************/
// regex_close_quote: closes a quote tag
//
/**************************************************/
function regex_close_quote() {
if ($this->quote_open == 0)
{
$this->quote_error++;
return;
}
$this->quote_closed++;
return "{$this->quote_html['END']}";
}
/**************************************************/
// regex_quote_tag: Builds this quote tag HTML
// [QUOTE=Matthew,14 February 2002]
/**************************************************/
function regex_quote_tag($name="", $date="")
{
global $ibforums;
if ( $date != "" )
{
$default = "\[quote=$name,$date\]";
}
else
{
$default = "\[quote=$name\]";
}
if ( strstr( $name, '' ) or strstr( $date, '' ) )
{
// Code tag detected...
$this->quote_error++;
return $default;
}
$name = str_replace( "+", "+", $name );
$name = str_replace( "-", "-", $name );
$name = str_replace( '[', "[", $name );
$name = str_replace( ']', "]", $name );
$this->quote_open++;
if ($date == "")
{
$html = $this->wrap_style( array( 'STYLE' => 'QUOTE', 'EXTRA' => "($name)" ) );
}
else
{
$html = $this->wrap_style( array( 'STYLE' => 'QUOTE', 'EXTRA' => "($name @ $date)" ) );
}
$extra = "-".$name.'+'.$date;
return "{$html['START']}";
}
/****************************************************************************************************/
// regex_check_flash: Checks, and builds the