Text processor module
| > Module written by Matt Mecham
| > Official Version: 2.0 - Number of changes to date 3 billion (estimated)
| > DBA Checked: Mon 24th May 2004
|
+--------------------------------------------------------------------------
*/
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;
var $bypass_badwords = 0;
var $load_custom_tags = 0;
var $pp_do_html = 0;
var $pp_nl2br = 1;
var $pp_wordwrap = 0;
var $max_embed_quotes = 10;
/*-------------------------------------------------------------------------*/
// CONSTRUCTOR
/*-------------------------------------------------------------------------*/
function post_parser($load=0)
{
global $ibforums, $DB;
$this->strip_quotes = $ibforums->vars['strip_quotes'];
if ( $load )
{
$this->check_caches($load);
}
}
/*-------------------------------------------------------------------------*/
// CHECK (AND LOAD) CACHES
/*-------------------------------------------------------------------------*/
function check_caches($load=0)
{
global $ibforums, $std, $DB;
if ( ! is_array( $ibforums->cache['emoticons'] ) )
{
$ibforums->cache['emoticons'] = array();
$DB->simple_construct( array( 'select' => 'typed,image,clickable,emo_set', 'from' => 'emoticons' ) );
$DB->simple_exec();
while ( $r = $DB->fetch_row() )
{
$ibforums->cache['emoticons'][] = $r;
}
if ( $load )
{
$std->update_cache( array( 'name' => 'emoticons', 'array' => 1, 'deletefirst' => 1 ) );
}
}
if ( ! is_array( $ibforums->cache['bbcode'] ) )
{
$ibforums->cache['bbcode'] = array();
$DB->simple_construct( array( 'select' => 'bbcode_id, bbcode_tag, bbcode_replace, bbcode_useoption', 'from' => 'custom_bbcode' ) );
$bbcode = $DB->simple_exec();
while ( $r = $DB->fetch_row($bbcode) )
{
$ibforums->cache['bbcode'][] = $r;
}
if ( $load )
{
$std->update_cache( array( 'name' => 'bbcode', 'array' => 1, 'deletefirst' => 1 ) );
}
}
if ( ! is_array( $ibforums->cache['badwords'] ) )
{
$ibforums->cache['badwords'] = array();
$DB->simple_construct( array( 'select' => 'type,swop,m_exact', 'from' => 'badwords' ) );
$bbcode = $DB->simple_exec();
while ( $r = $DB->fetch_row($bbcode) )
{
$ibforums->cache['badwords'][] = $r;
}
if ( $load )
{
$std->update_cache( array( 'name' => 'badwords', 'array' => 1, 'deletefirst' => 1 ) );
}
}
}
/*-------------------------------------------------------------------------*/
// Strip quote tags
/*-------------------------------------------------------------------------*/
function strip_quote_tags( $txt="" )
{
return preg_replace( "#\[QUOTE(=.+?,.+?)?\].+?\[/QUOTE\]#is", "", $txt );
}
/*-------------------------------------------------------------------------*/
// strip all tags
/*-------------------------------------------------------------------------*/
function strip_all_tags( $txt="" )
{
$txt = $this->strip_quote_tags( $this->unconvert( $txt ) );
$txt = preg_replace( "#\[.+?\](.+?)\[/.+?\]#is", "\\1", $txt );
return $txt;
}
/*-------------------------------------------------------------------------*/
// strip all tags to formatted HTML
/*-------------------------------------------------------------------------*/
function strip_all_tags_to_formatted( $txt="" )
{
//$txt = $this->strip_quote_tags( $this->unconvert( $txt ) );
//$txt = preg_replace( "#\[CODE\](.+?)\[/CODE\]#is", "
\\1
", $txt );
//$txt = preg_replace( "#\[LIST\](.+?)\[/LIST\]#eis", "'' .str_replace( '[*]', '- ', nl2br('\\1') ).'
';", $txt );
//$txt = preg_replace( "#\[LIST=.+?\](.+?)\[/LIST\]#eis", "'' .str_replace( '[*]', '- ', nl2br('\\1') ).'
';", $txt );
//$txt = preg_replace( "#\[.+?\](.+?)\[/.+?\]#is", "\\1", $txt );
return $txt;
}
/*-------------------------------------------------------------------------*/
// 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->check_caches();
$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.' ';
usort( $ibforums->cache['emoticons'] , array( 'post_parser', 'smilie_length_sort' ) );
if ( count( $ibforums->cache['emoticons'] ) > 0 )
{
foreach( $ibforums->cache['emoticons'] as $a_id => $row)
{
if ( $row['emo_set'] != $ibforums->skin['_emodir'] )
{
continue;
}
$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 );
}
}
$txt = trim($txt);
if ( $ibforums->vars['max_emos'] )
{
if ($this->emoticon_count > $ibforums->vars['max_emos'])
{
$this->error = 'too_many_emoticons';
}
}
}
$txt = $this->bad_words($txt);
$txt = $this->bbcode_check($txt);
return $txt;
}
//-----------------------------------------
// Checks opening and closing bbtags - doesn't parse at this point
//-----------------------------------------
function bbcode_check($t="")
{
global $ibforums, $DB, $std;
$count = array();
foreach( $ibforums->cache['bbcode'] as $i => $r )
{
if ( $r['bbcode_useoption'] )
{
$count[ $r['bbcode_id'] ]['open'] = substr_count( strtolower($t), '['.strtolower($r['bbcode_tag']).'=' );
$count[ $r['bbcode_id'] ]['wrongopen'] = substr_count( strtolower($t), '['.strtolower($r['bbcode_tag']).']' );
}
else
{
$count[ $r['bbcode_id'] ]['open'] = substr_count( strtolower($t), '['.strtolower($r['bbcode_tag']).']' );
$count[ $r['bbcode_id'] ]['wrongopen'] = substr_count( strtolower($t), '['.strtolower($r['bbcode_tag']).'=' );
}
$count[ $r['bbcode_id'] ]['closed'] = substr_count( strtolower($t), '[/'.strtolower($r['bbcode_tag']).']' );
//-----------------------------------------
// check...
//-----------------------------------------
if ( $count[ $r['bbcode_id'] ]['open'] != $count[ $r['bbcode_id'] ]['closed'] )
{
if ( $count[ $r['bbcode_id'] ]['wrongopen'] == $count[ $r['bbcode_id'] ]['closed'] )
{
$this->error = 'custom_tags_incorrect2';
}
else
{
$this->error = 'custom_tags_incorrect';
}
}
}
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;
}
//-----------------------------------------
// Post DB parse tags
//-----------------------------------------
function post_db_parse($t="")
{
global $ibforums, $DB;
if ( $this->pp_do_html )
{
$t = $this->post_db_parse_html( $t );
}
else
{
//$t = $this->my_strip_tags( $t );
}
if ( $this->pp_wordwrap > 0 )
{
$t = $this->my_wordwrap( $t, $this->pp_wordwrap );
}
//-----------------------------------------
// Custom BB code
//-----------------------------------------
if ( strstr( $t, '[/' ) )
{
$t = $this->post_db_parse_bbcode($t);
}
return $t;
}
//-----------------------------------------
// Post DB parse BBCode
//-----------------------------------------
function post_db_parse_bbcode($t="")
{
global $ibforums, $DB, $std;
if ( is_array( $ibforums->cache['bbcode'] ) and count( $ibforums->cache['bbcode'] ) )
{
foreach( $ibforums->cache['bbcode'] as $i => $row )
{
if ( substr_count( $row['bbcode_replace'], '{content}' ) > 1 )
{
//-----------------------------------------
// Slightly slower
//-----------------------------------------
if ( $row['bbcode_useoption'] )
{
preg_match_all( "#(\[".$row['bbcode_tag']."=(?:"|&\#39;)?(.+?)(?:"|&\#39;)?\])(.+?)(\[/".$row['bbcode_tag']."\])#si", $t, $match );
for ($i=0; $i < count($match[0]); $i++)
{
$tmp = $row['bbcode_replace'];
$tmp = str_replace( '{option}' , $match[2][$i], $tmp );
$tmp = str_replace( '{content}', $match[3][$i], $tmp );
$t = str_replace( $match[0][$i], $tmp, $t );
}
}
else
{
preg_match_all( "#(\[".$row['bbcode_tag']."\])(.+?)(\[/".$row['bbcode_tag']."\])#si", $t, $match );
for ($i=0; $i < count($match[0]); $i++)
{
$tmp = $row['bbcode_replace'];
$tmp = str_replace( '{content}', $match[2][$i], $tmp );
$t = str_replace( $match[0][$i], $tmp, $t );
}
}
}
else
{
$replace = explode( '{content}', $row['bbcode_replace'] );
if ( $row['bbcode_useoption'] )
{
$t = preg_replace( "#\[".$row['bbcode_tag']."=(?:"|&\#39;)?(.+?)(?:"|&\#39;)?\]#si", str_replace( '{option}', "\\1", $replace[0] ), $t );
}
else
{
$t = preg_replace( '#\['.$row['bbcode_tag'].'\]#i' , $replace[0], $t );
}
$t = preg_replace( '#\[/'.$row['bbcode_tag'].'\]#i', $replace[1], $t );
}
}
}
return $t;
}
//-----------------------------------------
// parse_html
// Converts the doHTML tag
//-----------------------------------------
function post_db_parse_html($t="")
{
if ( $t == "" )
{
return $t;
}
//-----------------------------------------
// Remove
s 'cos we know they can't
// be user inputted, 'cos they are still
// <br> at this point :)
//-----------------------------------------
if ( $this->pp_nl2br != 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( "/javascript/i" , "javascript", $t );
$t = preg_replace( "/alert/i" , "alert" , $t );
$t = preg_replace( "/about:/i" , "about:" , $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->bypass_badwords == 1 )
{
return $text;
}
//-----------------------------------------
if ( is_array( $ibforums->cache['badwords'] ) )
{
usort( $ibforums->cache['badwords'] , array( 'post_parser', 'word_length_sort' ) );
if ( count($ibforums->cache['badwords']) > 0 )
{
foreach($ibforums->cache['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,}#" , "\\1\[list\]" , $txt );
$txt = preg_replace( "#(\n){0,}#" , "\\1\[list=\\2\]\n" , $txt );
$txt = preg_replace( "#(\n){0,}- #" , "\n\[*\]" , $txt );
$txt = preg_replace( "#(\n){0,}
(\n){0,}#", "\n\[/list\]\\2" , $txt );
$txt = preg_replace( "#(\n){0,}(\n){0,}#", "\n\[/list\]\\2" , $txt );
while ( preg_match( "#(.+?)#is", $txt ) )
{
$txt = preg_replace( "#(.+?)#ise" , "\$this->unconvert_size('\\1', '\\2')", $txt );
}
while ( preg_match( "#(.+?)#is", $txt ) )
{
$txt = preg_replace( "#(.+?)#is" , "\[color=\\1\]\\2\[/color\]", $txt );
}
while ( preg_match( "#(.+?)#is", $txt ) )
{
$txt = preg_replace( "#(.+?)#is", "\[font=\\1\]\\2\[/font\]", $txt );
}
// Tidy up the end quote stuff
$txt = preg_replace( "#(\[/QUOTE\])\s*?
\s*#si", "\\1\n", $txt );
$txt = preg_replace( "#(\[/QUOTE\])\s*?
\s*#si" , "\\1\n", $txt );
$txt = preg_replace( "##" , "" , $txt );
$txt = str_replace( "", "", $txt );
$txt = str_replace( "", "(tm)", $txt );
}
if ($html == 1)
{
$txt = str_replace( "'", "'", $txt);
}
$txt = str_replace( "
" , "\n", $txt );
$txt = str_replace( "
", "\n", $txt );
return trim(stripslashes($txt));
}
//-----------------------------------------
//+-----------------------------------------------------------------------------------------
// UNCONVERT FUNCTIONS
//+-----------------------------------------------------------------------------------------
//+-----------------------------------------------------------------------------------------
function unconvert_size($size="", $text="")
{
$size -= 7;
return '[size='.$size.']'.$text.'[/size]';
}
function unconvert_flash($flash="")
{
$f_arr = explode( "+", $flash );
return '[flash='.$f_arr[0].','.$f_arr[1].']'.$f_arr[2].'[/flash]';
}
function unconvert_sql($sql="")
{
$sql = stripslashes($sql);
while ( preg_match( "#(.+?)#is", $sql ) )
{
$sql = preg_replace( "#(.+?)#is", "\\1", $sql );
}
$sql = preg_replace( "#\s*$#" , "", $sql );
return '[sql]'.$sql.'[/sql]';
}
function unconvert_htm($html="")
{
$html = stripslashes($html);
while ( preg_match( "#(.+?)#is", $html ) )
{
$html = preg_replace( "#(.+?)#is", "\\1", $html );
}
$html = preg_replace( "#\s*$#" , "", $html );
return '[html]'.$html.'[/html]';
}
//-----------------------------------------
//+-----------------------------------------------------------------------------------------
// CONVERT FUNCTIONS
//+-----------------------------------------------------------------------------------------
//+-----------------------------------------------------------------------------------------
/*-------------------------------------------------------------------------*/
// convert_emoticon:
// replaces the text with the emoticon image
/*-------------------------------------------------------------------------*/
function convert_emoticon($code="", $image="")
{
global $ibforums;
if (!$code or !$image) return;
//-----------------------------------------
// Remove slashes added by preg_quote
//-----------------------------------------
$code = stripslashes($code);
$this->emoticon_count++;
return "
";
}
/*-------------------------------------------------------------------------*/
// wrap style:
// code and quote table HTML generator
/*-------------------------------------------------------------------------*/
function wrap_style( $type='quote', $extra="" )
{
global $ibforums;
$used = array(
'quote' => array( 'title' => 'QUOTE', 'css_top' => 'quotetop' , 'css_main' => 'quotemain' ),
'code' => array( 'title' => 'CODE' , 'css_top' => 'codetop' , 'css_main' => 'codemain' ),
'sql' => array( 'title' => 'SQL' , 'css_top' => 'sqltop' , 'css_main' => 'sqlmain' ),
'html' => array( 'title' => 'HTML' , 'css_top' => 'htmltop' , 'css_main' => 'htmlmain' )
);
return array( 'START' => "{$used[ $type ]['title']}{$extra}
",
'END' => "
"
);
}
/*-------------------------------------------------------------------------*/
// regex_list: List generation
//
/*-------------------------------------------------------------------------*/
function regex_list( $txt="", $type="" )
{
if ($txt == "")
{
return;
}
if ( $type == "" )
{
// Unordered list.
return "".$this->regex_list_item($txt)."
";
}
else
{
return "".$this->regex_list_item($txt)."
";
}
}
function regex_list_item($txt)
{
$txt = preg_replace( "#\[\*\]#", "" , trim($txt) );
$txt = preg_replace( "#^?li>#" , "", $txt );
return str_replace( "\n", "", $txt."" );
}
/*-------------------------------------------------------------------------*/
// regex_html_tag: HTML syntax highlighting
//
/*-------------------------------------------------------------------------*/
function regex_html_tag($html="") {
if ($html == "") return;
//-----------------------------------------
// Too many embedded code/quote/html/sql tags can crash Opera and Moz
//-----------------------------------------
if (preg_match( "/\[(quote|code|html|sql)\].+?\[(quote|code|html|sql)\].+?\[(quote|code|html|sql)\].+?\[(quote|code|html|sql)\].+?\[(quote|code|html|sql)\]/i", $html) )
{
return $default;
}
//-----------------------------------------
// Take a stab at removing most of the common
// smilie characters.
//-----------------------------------------
$html = preg_replace( "#:#" , ":", $html );
$html = preg_replace( "#\[#" , "[", $html );
$html = preg_replace( "#\]#" , "]", $html );
$html = preg_replace( "#\)#" , ")", $html );
$html = preg_replace( "#\(#" , "(", $html );
$html = preg_replace( "/^
/" , "", $html );
$html = preg_replace( "#^
#", "", $html );
$html = preg_replace( "/^\s+/" , "", $html );
$html = preg_replace( "#<([^&<>]+)>#" , "<\\1>" , $html ); //Matches
$html = preg_replace( "#<([^&<>]+)=#" , "<\\1=" , $html ); //Matches \\1>" , $html ); //Matches
$html = preg_replace( "!=("|')(.+?)?("|')(\s|>)!" , "=\\1\\2\\3\\4" , $html ); //Matches ='this'
$html = preg_replace( "!<!--(.+?)-->!" , "<!--\\1-->", $html );
$wrap = $this->wrap_style( 'html' );
return "{$wrap['START']}$html{$wrap['END']}";
}
/*-------------------------------------------------------------------------*/
// regex_sql_tag: SQL syntax highlighting
//
/*-------------------------------------------------------------------------*/
function regex_sql_tag($sql="") {
if ($sql == "") return;
//-----------------------------------------
// Too many embedded code/quote/html/sql tags can crash Opera and Moz
//-----------------------------------------
if (preg_match( "/\[(quote|code|html|sql)\].+?\[(quote|code|html|sql)\].+?\[(quote|code|html|sql)\].+?\[(quote|code|html|sql)\].+?\[(quote|code|html|sql)\].+?\[(quote|code|html|sql)\].+?\[(quote|code|html|sql)\].+?\[(quote|code|html|sql)\].+?\[(quote|code|html|sql)\]/i", $sql) ) {
return $default;
}
//-----------------------------------------
// Knock off any preceeding newlines (which have
// since been converted into
)
//-----------------------------------------
$sql = preg_replace( "/^
/" , "", $sql );
$sql = preg_replace( "#^
#", "", $sql );
$sql = preg_replace( "/^\s+/" , "", $sql );
//-----------------------------------------
// Make certain regex work..
//-----------------------------------------
if (! preg_match( "/\s+$/" , $sql) )
{
$sql = $sql.' ';
}
$sql = preg_replace( "#(=|\+|\-|>|<|~|==|\!=|LIKE|NOT LIKE|REGEXP)#i" , "\\1", $sql );
$sql = preg_replace( "#(MAX|AVG|SUM|COUNT|MIN)\(#i" , "\\1(" , $sql );
$sql = preg_replace( "!("|'|')(.+?)("|'|')!i" , "\\1\\2\\3" , $sql );
$sql = preg_replace( "#\s{1,}(AND|OR)\s{1,}#i" , " \\1 " , $sql );
$sql = preg_replace( "#(LEFT|JOIN|WHERE|MODIFY|CHANGE|AS|DISTINCT|IN|ASC|DESC|ORDER BY)\s{1,}#i" , "\\1 " , $sql );
$sql = preg_replace( "#LIMIT\s*(\d+)\s*,\s*(\d+)#i" , "LIMIT \\1, \\2" , $sql );
$sql = preg_replace( "#(FROM|INTO)\s{1,}(\S+?)\s{1,}#i" , "\\1 \\2 ", $sql );
$sql = preg_replace( "#(SELECT|INSERT|UPDATE|DELETE|ALTER TABLE|DROP)#i" , "\\1" , $sql );
$html = $this->wrap_style( 'sql' );
return "{$html['START']}{$sql}{$html['END']}";
}
/*-------------------------------------------------------------------------*/
// regex_code_tag: Builds this code tag HTML
//
/*-------------------------------------------------------------------------*/
function regex_code_tag($txt="")
{
global $ibforums;
$default = "\[code\]$txt\[/code\]";
if ($txt == "") return;
//-----------------------------------------
// Too many embedded code/quote/html/sql tags can crash Opera and Moz
//-----------------------------------------
if (preg_match( "/\[(quote|code|html|sql)\].+?\[(quote|code|html|sql)\].+?\[(quote|code|html|sql)\].+?\[(quote|code|html|sql)\].+?\[(quote|code|html|sql)\].+?\[(quote|code|html|sql)\].+?\[(quote|code|html|sql)\]/i", $txt) ) {
return $default;
}
//-----------------------------------------
// Take a stab at removing most of the common
// smilie characters.
//-----------------------------------------
//$txt = str_replace( "&" , "&", $txt );
$txt = preg_replace( "#<#" , "<", $txt );
$txt = preg_replace( "#>#" , ">", $txt );
$txt = preg_replace( "#"#" , """, $txt );
$txt = preg_replace( "#:#" , ":", $txt );
$txt = preg_replace( "#\[#" , "[", $txt );
$txt = preg_replace( "#\]#" , "]", $txt );
$txt = preg_replace( "#\)#" , ")", $txt );
$txt = preg_replace( "#\(#" , "(", $txt );
$txt = preg_replace( "#\r#" , "
", $txt );
$txt = preg_replace( "#\n#" , "
", $txt );
$txt = preg_replace( "#\s{1};#" , ";", $txt );
//-----------------------------------------
// Ensure that spacing is preserved
//-----------------------------------------
$txt = preg_replace( "#\s{2}#", " ", $txt );
$html = $this->wrap_style( 'code' );
return "{$html['START']}$txt{$html['END']}";
}
/*-------------------------------------------------------------------------*/
// regex_parse_quotes: Builds this quote tag HTML
// [QUOTE] .. [/QUOTE] - allows for embedded quotes
/*-------------------------------------------------------------------------*/
function regex_parse_quotes($the_txt="") {
if ($the_txt == "") return;
$txt = $the_txt;
if ( substr_count( strtolower($txt), '[quote' ) > $this->max_embed_quotes )
{
return $txt;
}
$txt = str_replace( chr(173).']', ']', $txt );
$this->quote_html = $this->wrap_style('quote');
$txt = preg_replace( "#\[quote\]#ie" , "\$this->regex_simple_quote_tag()" , $txt );
$txt = preg_replace( "#\[quote=([^\],]+?),([^\]]+?)\]#ie" , "\$this->regex_quote_tag('\\1', '\\2')" , $txt );
$txt = preg_replace( "#\[quote=([^\]]+?)\]#ie" , "\$this->regex_quote_tag('\\1', '')" , $txt );
$txt = preg_replace( "#\[/quote\]#ie" , "\$this->regex_close_quote()" , $txt );
$txt = str_replace( "\n", "
", $txt );
if ( ($this->quote_open == $this->quote_closed) and ($this->quote_error == 0) )
{
$txt = preg_replace( "#(.+?)#es", "\$this->regex_preserve_spacing('\\1')", trim($txt) );
return $txt;
}
else
{
return $the_txt;
}
}
/*-------------------------------------------------------------------------*/
// regex_preserve_spacing: keeps double spaces
// without CSS killing tags
/*-------------------------------------------------------------------------*/
function regex_preserve_spacing($txt="")
{
$txt = preg_replace( "#^(?:
|
)#", "", trim($txt) );
$txt = preg_replace( "#\s{2}#", " ", $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( 'quote', "($name)");
}
else
{
$html = $this->wrap_style( 'quote', "($name @ $date)" );
}
$extra = "-".$name.'+'.$date;
return "{$html['START']}";
}
/*-------------------------------------------------------------------------*/
// regex_check_flash: Checks, and builds the