uccode.class.php 4.8 KB

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