$Date: 2005-10-10 14:08:54 +0100 (Mon, 10 Oct 2005) $
| > $Revision: 23 $
| > $Author: matt $
+---------------------------------------------------------------------------
|
| > 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)
{
$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;
$load=0;
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", "
$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