uccode.class.php 6.1 KB

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