post_parser.php
来自「sabreipb 2.1.6 utf-8中文版本!」· PHP 代码 · 共 1,500 行 · 第 1/4 页
PHP
1,500 行
<?php/*+--------------------------------------------------------------------------| Invision Power Board v2.1.5| =============================================| by Matthew Mecham| (c) 2001 - 2005 Invision Power Services, Inc.| | =============================================| Web: | Time: Wed, 01 Mar 2006 19:11:29 GMT| Release: | Licence Info: +---------------------------------------------------------------------------| > $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", "<pre>\\1</pre>", $txt ); //$txt = preg_replace( "#\[LIST\](.+?)\[/LIST\]#eis", "'<ul>' .str_replace( '[*]', '<li>', nl2br('\\1') ).'</ul>';", $txt ); //$txt = preg_replace( "#\[LIST=.+?\](.+?)\[/LIST\]#eis", "'<ul>' .str_replace( '[*]', '<li>', nl2br('\\1') ).'</ul>';", $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 <br> to \n //----------------------------------------- $txt = preg_replace( "/<br>|<br \/>/", "\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", "<b>\\1</b>", $txt ); $txt = preg_replace( "#\[i\](.+?)\[/i\]#is", "<i>\\1</i>", $txt ); $txt = preg_replace( "#\[u\](.+?)\[/u\]#is", "<u>\\1</u>", $txt ); $txt = preg_replace( "#\[s\](.+?)\[/s\]#is", "<s>\\1</s>", $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" , "<a href='mailto:\\1'>\\1</a>", $txt ); $txt = preg_replace( "#\[email\s*=\s*\"\;([\.\w\-]+\@[\.\w\-]+\.[\.\w\-]+)\s*\"\;\s*\](.*?)\[\/email\]#i" , "<a href='mailto:\\1'>\\2</a>", $txt ); $txt = preg_replace( "#\[email\s*=\s*([\.\w\-]+\@[\.\w\-]+\.[\w\-]+)\s*\](.*?)\[\/email\]#i" , "<a href='mailto:\\1'>\\2</a>", $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 <br> $txt = preg_replace( "/\n/", "<br />", $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'] != $this->ipsclass->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);
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?