uccode.class.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. <?php
  2. /*
  3. [UCenter] (C)2001-2099 Comsenz Inc.
  4. This is NOT a freeware, use is subject to license terms
  5. $Id: uccode.class.php 1166 2014-11-03 01:49:32Z hypowang $
  6. */
  7. class uccode {
  8. var $uccodes;
  9. function __construct() {
  10. $this->uccode();
  11. }
  12. function uccode() {
  13. $this->uccode = array(
  14. 'pcodecount' => -1,
  15. 'codecount' => 0,
  16. 'codehtml' => ''
  17. );
  18. }
  19. function codedisp($code) {
  20. $this->uccode['pcodecount']++;
  21. $code = str_replace('\\"', '"', preg_replace("/^[\n\r]*(.+?)[\n\r]*$/is", "\\1", $code));
  22. $this->uccode['codehtml'][$this->uccode['pcodecount']] = $this->tpl_codedisp($code);
  23. $this->uccode['codecount']++;
  24. return "[\tUCENTER_CODE_".$this->uccode[pcodecount]."\t]";
  25. }
  26. function complie($message) {
  27. $message = dhtmlspecialchars($message);
  28. if(strpos($message, '[/code]') !== FALSE) {
  29. $message = preg_replace_callback("/\s*\[code\](.+?)\[\/code\]\s*/is", array($this, 'complie_callback_codedisp_1'), $message);
  30. }
  31. if(strpos($message, '[/url]') !== FALSE) {
  32. $message = preg_replace_callback("/\[url(=((https?|ftp|gopher|news|telnet|rtsp|mms|callto|bctp|ed2k|thunder|synacast){1}:\/\/|www\.)([^\[\"']+?))?\](.+?)\[\/url\]/is", array($this, 'complie_callback_parseurl_15'), $message);
  33. }
  34. if(strpos($message, '[/email]') !== FALSE) {
  35. $message = preg_replace_callback("/\[email(=([a-z0-9\-_.+]+)@([a-z0-9\-_]+[.][a-z0-9\-_.]+))?\](.+?)\[\/email\]/is", array($this, 'complie_callback_parseemail_14'), $message);
  36. }
  37. $message = str_replace(array(
  38. '[/color]', '[/size]', '[/font]', '[/align]', '[b]', '[/b]',
  39. '[i]', '[/i]', '[u]', '[/u]', '[list]', '[list=1]', '[list=a]',
  40. '[list=A]', '[*]', '[/list]', '[indent]', '[/indent]', '[/float]'
  41. ), array(
  42. '</font>', '</font>', '</font>', '</p>', '<strong>', '</strong>', '<i>',
  43. '</i>', '<u>', '</u>', '<ul>', '<ul type="1">', '<ul type="a">',
  44. '<ul type="A">', '<li>', '</ul>', '<blockquote>', '</blockquote>', '</span>'
  45. ), preg_replace(array(
  46. "/\[color=([#\w]+?)\]/i",
  47. "/\[size=(\d+?)\]/i",
  48. "/\[size=(\d+(\.\d+)?(px|pt|in|cm|mm|pc|em|ex|%)+?)\]/i",
  49. "/\[font=([^\[\<]+?)\]/i",
  50. "/\[align=(left|center|right)\]/i",
  51. "/\[float=(left|right)\]/i"
  52. ), array(
  53. "<font color=\"\\1\">",
  54. "<font size=\"\\1\">",
  55. "<font style=\"font-size: \\1\">",
  56. "<font face=\"\\1 \">",
  57. "<p align=\"\\1\">",
  58. "<span style=\"float: \\1;\">"
  59. ), $message));
  60. if(strpos($message, '[/quote]') !== FALSE) {
  61. $message = preg_replace("/\s*\[quote\][\n\r]*(.+?)[\n\r]*\[\/quote\]\s*/is", $this->tpl_quote(), $message);
  62. }
  63. if(strpos($message, '[/img]') !== FALSE) {
  64. $message = preg_replace_callback("/\[img\]\s*([^\[\<\r\n]+?)\s*\[\/img\]/is", array($this, 'complie_callback_bbcodeurl_1'), $message);
  65. $message = preg_replace_callback("/\[img=(\d{1,4})[x|\,](\d{1,4})\]\s*([^\[\<\r\n]+?)\s*\[\/img\]/is", array($this, 'complie_callback_bbcodeurl_312'), $message);
  66. }
  67. for($i = 0; $i <= $this->uccode['pcodecount']; $i++) {
  68. $message = str_replace("[\tUCENTER_CODE_$i\t]", $this->uccode['codehtml'][$i], $message);
  69. }
  70. return nl2br(str_replace(array("\t", ' ', ' '), array('&nbsp; &nbsp; &nbsp; &nbsp; ', '&nbsp; &nbsp;', '&nbsp;&nbsp;'), $message));
  71. }
  72. function complie_callback_codedisp_1($matches) {
  73. return $this->codedisp($matches[1]);
  74. }
  75. function complie_callback_parseurl_15($matches) {
  76. return $this->parseurl($matches[1], $matches[5]);
  77. }
  78. function complie_callback_parseemail_14($matches) {
  79. return $this->parseemail($matches[1], $matches[4]);
  80. }
  81. function complie_callback_bbcodeurl_1($matches) {
  82. return $this->bbcodeurl($matches[1], '<img src="%s" border="0" alt="" />');
  83. }
  84. function complie_callback_bbcodeurl_312($matches) {
  85. return $this->bbcodeurl($matches[3], '<img width="'.$matches[1].'" height="'.$matches[2].'" src="%s" border="0" alt="" />');
  86. }
  87. function parseurl($url, $text) {
  88. if(!$url && preg_match("/((https?|ftp|gopher|news|telnet|rtsp|mms|callto|bctp|ed2k|thunder|synacast){1}:\/\/|www\.)[^\[\"']+/i", trim($text), $matches)) {
  89. $url = $matches[0];
  90. $length = 65;
  91. if(strlen($url) > $length) {
  92. $text = substr($url, 0, intval($length * 0.5)).' ... '.substr($url, - intval($length * 0.3));
  93. }
  94. return '<a href="'.(substr(strtolower($url), 0, 4) == 'www.' ? 'http://'.$url : $url).'" target="_blank">'.$text.'</a>';
  95. } else {
  96. $url = substr($url, 1);
  97. if(substr(strtolower($url), 0, 4) == 'www.') {
  98. $url = 'http://'.$url;
  99. }
  100. return '<a href="'.$url.'" target="_blank">'.$text.'</a>';
  101. }
  102. }
  103. function parseemail($email, $text) {
  104. $text = str_replace('\"', '"', $text);
  105. if(!$email && preg_match("/\s*([a-z0-9\-_.+]+)@([a-z0-9\-_]+[.][a-z0-9\-_.]+)\s*/i", $text, $matches)) {
  106. $email = trim($matches[0]);
  107. return '<a href="mailto:'.$email.'">'.$email.'</a>';
  108. } else {
  109. return '<a href="mailto:'.substr($email, 1).'">'.$text.'</a>';
  110. }
  111. }
  112. function bbcodeurl($url, $tags) {
  113. if(!preg_match("/<.+?>/s", $url)) {
  114. if(!in_array(strtolower(substr($url, 0, 6)), array('http:/', 'https:', 'ftp://', 'rtsp:/', 'mms://'))) {
  115. $url = 'http://'.$url;
  116. }
  117. return str_replace(array('submit', 'logging.php'), array('', ''), sprintf($tags, $url, addslashes($url)));
  118. } else {
  119. return '&nbsp;'.$url;
  120. }
  121. }
  122. function tpl_codedisp($code) {
  123. return '<div class="blockcode"><code id="code'.$this->uccodes['codecount'].'">'.$code.'</code></div>';
  124. }
  125. function tpl_quote() {
  126. return '<div class="quote"><blockquote>\\1</blockquote></div>';
  127. }
  128. }
  129. ?>