class_bbcode.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. <?php
  2. /**
  3. * [Discuz!] (C)2001-2099 Comsenz Inc.
  4. * This is NOT a freeware, use is subject to license terms
  5. *
  6. * $Id: class_bbcode.php 36278 2016-12-09 07:52:35Z nemohou $
  7. */
  8. if(!defined('IN_DISCUZ')) {
  9. exit('Access Denied');
  10. }
  11. class bbcode {
  12. var $search_exp = array();
  13. var $replace_exp = array();
  14. var $search_str = array();
  15. var $replace_str = array();
  16. var $html_s_exp = array();
  17. var $html_r_exp = array();
  18. var $html_s_str = array();
  19. var $html_r_str = array();
  20. function &instance() {
  21. static $object;
  22. if(empty($object)) {
  23. $object = new bbcode();
  24. }
  25. return $object;
  26. }
  27. function bbcode() {
  28. }
  29. function bbcode2html($message, $parseurl=0) {
  30. if(empty($this->search_exp)) {
  31. $this->search_exp = array(
  32. "/\s*\[quote\][\n\r]*(.+?)[\n\r]*\[\/quote\]\s*/is",
  33. "/\[url\]\s*(https?:\/\/|ftp:\/\/|gopher:\/\/|news:\/\/|telnet:\/\/|rtsp:\/\/|mms:\/\/|callto:\/\/|ed2k:\/\/){1}([^\[\"']+?)\s*\[\/url\]/i",
  34. "/\[em:([0-9]+):\]/i",
  35. );
  36. $this->replace_exp = array(
  37. "<div class=\"quote\"><blockquote>\\1</blockquote></div>",
  38. "<a href=\"\\1\\2\" target=\"_blank\">\\1\\2</a>",
  39. " <img src=\"".STATICURL."image/smiley/comcom/\\1.gif\" class=\"vm\"> "
  40. );
  41. $this->replace_exp[] = '$this->bb_img(\'\\1\')';
  42. $this->search_str = array('[b]', '[/b]','[i]', '[/i]', '[u]', '[/u]');
  43. $this->replace_str = array('<b>', '</b>', '<i>','</i>', '<u>', '</u>');
  44. }
  45. if($parseurl==2) {
  46. $message = bbcode::parseurl($message);
  47. }
  48. @$message = preg_replace($this->search_exp, $this->replace_exp, $message, 20);
  49. if($parseurl==2) {
  50. @$message = preg_replace_callback("/\[img\]\s*([^\[\<\r\n]+?)\s*\[\/img\]/is", array($this, 'bbcode2html_callback_bb_img_1'), $message, 20);
  51. }
  52. @$message = str_replace($this->search_str, $this->replace_str, $message);
  53. return nl2br(str_replace(array("\t", ' ', ' '), array('&nbsp; &nbsp; &nbsp; &nbsp; ', '&nbsp; &nbsp;', '&nbsp;&nbsp;'), $message));
  54. }
  55. function bbcode2html_callback_bb_img_1($matches) {
  56. return $this->bb_img($matches[1]);
  57. }
  58. function parseurl($message) {
  59. return preg_replace("/(?<=[^\]a-z0-9-=\"'\\/])((https?|ftp|gopher|news|telnet|mms|rtsp):\/\/)([a-z0-9\/\-_+=.~!%@?#%&;:$\\()|]+)/i", "[url]\\1\\3[/url]", ' '.$message);
  60. }
  61. function html2bbcode($message) {
  62. if(empty($this->html_s_exp)) {
  63. $this->html_s_exp = array(
  64. "/\<div class=\"quote\"\>\<blockquote\>(.*?)\<\/blockquote\>\<\/div\>/is",
  65. "/\<a href=\"(.+?)\".*?\<\/a\>/is",
  66. "/(\r\n|\n|\r)/",
  67. "/<br.*>/siU",
  68. "/[ \t]*\<img src=\"static\/image\/smiley\/comcom\/(.+?).gif\".*?\>[ \t]*/is",
  69. "/\s*\<img src=\"(.+?)\".*?\>\s*/is"
  70. );
  71. $this->html_r_exp = array(
  72. "[quote]\\1[/quote]",
  73. "\\1",
  74. '',
  75. "\n",
  76. "[em:\\1:]",
  77. "\n[img]\\1[/img]\n"
  78. );
  79. $this->html_s_str = array('<b>', '</b>', '<i>','</i>', '<u>', '</u>', '&nbsp; &nbsp; &nbsp; &nbsp; ', '&nbsp; &nbsp;', '&nbsp;&nbsp;', '&lt;', '&gt;', '&amp;');
  80. $this->html_r_str = array('[b]', '[/b]','[i]', '[/i]', '[u]', '[/u]', "\t", ' ', ' ', '<', '>', '&');
  81. }
  82. @$message = str_replace($this->html_s_str, $this->html_r_str,
  83. preg_replace($this->html_s_exp, $this->html_r_exp, $message));
  84. $message = dhtmlspecialchars($message);
  85. return trim($message);
  86. }
  87. function bb_img($url) {
  88. global $_G;
  89. if(!in_array(strtolower(substr($url, 0, 6)), array('http:/', 'https:', 'ftp://', 'rtsp:/', 'mms://'))) {
  90. $url = isset($_G['siteurl']) && !empty($_G['siteurl']) ? $_G['siteurl'].$url : 'http://'.$url;
  91. }
  92. $url = addslashes($url);
  93. return "<img src=\"$url\" class=\"vm\">";
  94. }
  95. }
  96. ?>