📄 post_parser.php
字号:
<?php//******************************************************//// ///////// //// /////// // // // // //// // ///// ////// //// ////// // // //// // // // //// ///////// ///// ////// ///// ///////******************************************************//// icebb.net // 1.0//******************************************************//// post parser class// $Id: post_parser.php 763 2007-02-16 21:59:29Z mutantmonkey0 $//******************************************************///** * A post parser class * * @package IceBB * @version 1.0 * @date $Date$ */class post_parser{ var $smilies_list = array(); var $word_filters = array(); var $parse_quotes = 1; var $quote_open = 0; var $quote_closed = 0; var $wysiwyg = 0; var $url_regex_protocol = "(ftp|http|https|ed2k|imap|irc|news|pop|sftp|ssh|telnet|ventrilo|webcal)://"; var $url_regex_main = "([\w\.\?/&-:;,\-@%\+\=#~]+)"; var $url_regex = ""; /** * Constructor */ function post_parser() { global $icebb,$db,$config,$std; $this->url_regex = $this->url_regex_protocol . $this->url_regex_main; $set = $icebb->skin_data['smiley_set']; if(is_array($icebb->cache['smilies'][$set])) { foreach($icebb->cache['smilies'][$set] as $s) { $this->smilies_list[]= $s; } } if(is_array($icebb->cache['word_filters'])) { foreach($icebb->cache['word_filters'] as $bw) { $this->word_filters[]= $bw; } } $db->query("SELECT * FROM icebb_uploads"); while($u = $db->fetch_row()) { $this->uploads[$u['uid']]= $u; } if(!class_exists('skin_global')) { require(PATH_TO_ICEBB."skins/{$icebb->skin->skin_id}/global.php"); } $this->html_global = new skin_global; } /** * Parses BBCode * * @param string String to parse * @return string Parsed string */ function bbcode($t) { global $icebb,$db,$config,$std; // this is to keep a few things from breaking, shouldn't break anything $t = str_replace('$','$',$t); // get rid of empty BBCode, is there a point in having excess markup? $t = preg_replace("`\[(b|i|u|url|mail|img|quote|code|php|tt)\]\[/(b|i|u|url|mail|img|quote|code|php|tt)\]`",'',$t); // first handle defaults, as much as I'd like to make everything customizable I can't >_< $t = preg_replace("`\[b\](.*)\[/b\]`sUi","<b>\\1</b>",$t); $t = preg_replace("`\[i\](.*)\[/i\]`sUi","<i>\\1</i>",$t); $t = preg_replace("`\[u\](.*)\[/u\]`sUi","<u>\\1</u>",$t); $t = preg_replace("`\[tt\](.*)\[/tt\]`sUi","<tt class='bbcode'>\\1</tt>",$t); $t = preg_replace("`\[color=(#[a-z0-9]*|[a-z]*)\](.*)\[/color\]`sUi","<span style='color:\\1'>\\2</span>",$t); $t = preg_replace("`\[size=([0-9]*)\](.*)\[/size\]`sUie","\$this->bbcode_handle_size('\\1','\\2')",$t); $t = preg_replace("`\[font=([a-z0-9@, ]*)\](.*)\[/font\]`sUi","<span style='font-family:\\1'>\\2</span>",$t); $t = preg_replace("`\[url\]{$this->url_regex}\[/url\]`sUi","<a href='$1://$2'>$1://$2</a>",$t); $t = preg_replace("`\[url\={$this->url_regex}\](.*)\[/url\]`sUi","<a href='$1://$2'>$3</a>",$t); $t = preg_replace("`\[mail\](.*)\[/mail\]`eUi","\$this->bbcode_handle_mail('\\1')",$t); $t = preg_replace("`\[img\]([\w]*[:\/\/]*[\w\.\?\/&=\;, \-@%\?]+)\[/img\]`isU","<img src='\\1' alt='Attached Image' />",$t); //$t = preg_replace("`\[quote\](.*)\[/quote\]`sie","\$this->bbcode_handle_quote('\\1')",$t); //$t = preg_replace("`\[quote\=([\w]*[:\/\/]*[\w\.\?\/&=\;, -@]+)\](.*)\[/quote\]`sie","\$this->bbcode_handle_quote('\\2','\\1')",$t); if(preg_match("`\[quote(.+?)?\](.*)\[/quote\]`si",$t)) { $t = $this->bbcode_handle_quote_do($t); } // code tags $t = preg_replace_callback("`\[code(\=([a-z]+))?\]((?:[^[]|\[(?!/?code\])|(?R))+)\[/code\]`i",array(&$this,'bbcode_handle_code'),$t); // left, right, center $t = preg_replace("`\[left\](.*)\[/left\]`siU","<div style='text-align:left'>\\1</div>",$t); $t = preg_replace("`\[center\](.*)\[/center\]`siU","<div style='text-align:center'>\\1</div>",$t); $t = preg_replace("`\[right\](.*)\[/right\]`siU","<div style='text-align:right'>\\1</div>",$t); $t = preg_replace("`\[noparse\](.*)\[/noparse\]`seiU","\$this->bbcode_handle_noparse('\\1')",$t); return $t; } /** * Undoes the parsing of BBCode * * @param string String to parse * @return string Parsed string */ function bbcode_undo($t) { global $icebb,$db,$config,$std; // this is to keep a few things from breaking, shouldn't break anything $t = str_replace('$','$',$t); // first handle defaults, as much as I'd like to make everything customizable I can't >_< $t = preg_replace("`<b>(.*)</b>`sUi","[b]\\1[/b]",$t); $t = preg_replace("`<i>(.*)</i>`sUi","[i]\\1[/i]",$t); $t = preg_replace("`<u>(.*)</u>`sUi","[u]\\1[/u]",$t); $t = preg_replace("`<img src='(.*)' alt='Attached Image' />`isU","[img]\\1[/img]",$t); return $t; } function bbcode_handle_size($size,$t) { $size = intval($size)+7; if($size > 30) { $size = 30; } $t = "<span style='font-size:{$size}pt'>{$t}</span>"; return $t; } function bbcode_handle_mail($q) { $encoded = ''; for($i=0;$i<strlen($q);$i++) { $encoded .= "&#".ord($q{$i}).";"; } $q = "<a href='mailto:{$encoded}'>{$encoded}</a>"; return $q; } function bbcode_handle_noparse($t) { $t = $this->smilies_undo($t); $t = $this->bbcode_undo($t); return $t; } function bbcode_handle_php($q) { $q = $this->smilies_undo($q); $q = str_replace('?',htmlspecialchars('?'),$q); $q = str_replace('$','$',$q); $q = stripslashes(html_entity_decode(html_entity_decode(html_entity_decode($q)))); $q = highlight_string($q,1); // /me wubs php.net $q = preg_replace('#<font color="([^\']*)">([^\']*)</font>#', '<span style="color: \\1">\\2</span>', $q); $q = preg_replace('#<font color="([^\']*)">([^\']*)</font>#U', '<span style="color: \\1">\\2</span>', $q); $q = str_replace('<br />','',$q); //$q = str_replace('&(amp;?)(amp;?)#36;','$',$q); $t = "<div class='code_tag'><div class='ctop'>PHP:</div><div class='code'>"; $t .= $q; $t .= "</div></div>"; return $t; } function bbcode_handle_code($tb) { $type = $tb[2]; $tb = $tb[3]; $tb = $this->smilies_undo($tb); $tb = str_replace("<br />","",$tb); switch($type) { default: case 'php': if(version_compare(phpversion(),"5.0.0","<")) break; // decode the string so highlight_string() will work $tb = html_entity_decode($tb); $tb = htmlspecialchars_decode($tb); // okay, let's highlight the string... $tb = highlight_string($tb,true); $tb = str_replace("<br />","",$tb); $tb = str_replace("<code>","",$tb); $tb = str_replace("</code>","",$tb); $tb = preg_replace("`<span style=\"color: #000000\">(.*)</span>\n`is","$1",$tb); break; case 'xml': $tb = explode("\n",$tb); foreach($tb as $on => $piece) { $piece = preg_replace("/('(.+?)'|"(.+?)"|\"(.+?)\")/","<span style='color:#DD0000'>\\0</span>",$piece); $piece = preg_replace('/\<(.+?)\>/i',"<span style='color:#007700'>\\0</span>",$piece); $tb[$on] = $piece; } $tb = implode($tb); break; } $t = $this->html_global->code_tag($tb,$type); return $t; } function bbcode_handle_xml($source) { $source = $this->smilies_undo($source); $source = explode("\n",$source); foreach($source as $on => $piece) { //$piece = htmlspecialchars($piece); $piece = preg_replace("/'.*?'/", "<span style='color:#0000CC;'>\\0</span>", $piece); $piece = preg_replace('/".*?"/', "<span style='color:#0000CC;'>\\0</span>", $piece); $piece = preg_replace('/<.*?>/', "<span style='color:#008800;'>\\0</span>", $piece); $source[$on]= $piece; } $output = implode($source); $output = str_replace('<br />','',$output); $output2 = $this->html_global->code_tag($output,'xml'); return $output2; } function bbcode_handle_quote($q,$title='') { if(!empty($title)) { $title = str_replace("]",']',$title); } $t = $this->bbcode_quote_top($title); $t .= $this->bbcode_handle_quote_do($q); $t .= $this->bbcode_quote_bottom(); return $t; } function bbcode_handle_quote_do($t) { if(!$this->parse_quotes) { return $t; } $t = preg_replace("`\[quote\]`sie","\$this->bbcode_handle_quote_start_old()",$t); $t = preg_replace("`\[quote\=([\w]*[:\/\/]*[\w\.\?\/&=\;, -@]+)\]`sie","\$this->bbcode_handle_quote_start_old('\\1')",$t); $t = preg_replace("`\[quote pid\=([0-9]*) author\=([\w]*[:\/\/]*[\w\.\?\/&=\;, -@]+) date\=([0-9]+)\]`sie","\$this->bbcode_handle_quote_start('\\1','\\2','\\3')",$t); $t = preg_replace("`\[/quote\]`sie","\$this->bbcode_handle_quote_end()",$t); if(preg_match("`\[quote(.?)\](.*)\[/quote\]`si",$t)) { //$t = $this->bbcode_handle_quote_do($t); } return $t; } function bbcode_handle_quote_start($pid='',$title='',$date='') { global $icebb,$std; $this->quotes_open++; $date = gmdate($icebb->user['date_format'],$date+$std->get_offset()); $t = $this->bbcode_quote_top($pid,$title,$date); return $t; } function bbcode_handle_quote_start_old($title='') { global $icebb,$std; $this->quotes_open++; $title = preg_replace("`,time=([0-9]+)`e","\", \".gmdate(\$icebb->user['date_format'],\\1+\$std->get_offset())",$title); $t = $this->bbcode_quote_top('',$title,''); return $t; } function bbcode_quote_top($pid='',$title='',$date='') { global $icebb; if(!empty($pid)) { $link = " <a href='{$icebb->base_url}act=search&findpost={$pid}'>(view in context)</a>"; } if(!empty($date)) { $date = ", {$date}"; } $t = $this->html_global->quote_tag_top($title,$date,$link); return $t; } function bbcode_handle_quote_end() { if($this->quotes_open>=1) { $this->quotes_closed++; $t = $this->html_global->quote_tag_bottom(); } return $t; } function bbcode_quote_bottom() { $t = $this->html_global->quote_tag_bottom(); return $t; } /** * Parses smilies * * @param string String to parse * @return string Parsed string */ function smilies($t) { global $icebb,$db,$config,$std; if(is_array($this->smilies_list)) { foreach($this->smilies_list as $s) { $s['code'] = $this->xss_is_bad($s['code']); $smiley_code = preg_quote($s['code'],"`"); $t = preg_replace("`(?<=^|[\n ]|\.){$smiley_code}`","<img src='{$icebb->settings['board_url']}smilies/{$s['smiley_set']}/{$s['image']}' border='0' alt='{$s['code']}' />",$t); } } return $t; }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -