PageRenderTime 66ms CodeModel.GetById 27ms RepoModel.GetById 1ms app.codeStats 0ms

/ext/bbcode/main.php

https://github.com/dgz/shimmie2
PHP | 155 lines | 110 code | 17 blank | 28 comment | 6 complexity | caffd2abbf9de7d763e5bcdc97c65074 MD5 | raw file
  1. <?php
  2. /**
  3. * Name: BBCode
  4. * Author: Shish <webmaster@shishnet.org>
  5. * Link: http://code.shishnet.org/shimmie2/
  6. * License: GPLv2
  7. * Description: Turns BBCode into HTML
  8. * Documentation:
  9. * Supported tags:
  10. * <ul>
  11. * <li>[img]url[/img]
  12. * <li>[url]<a href="http://code.shishnet.org/shimmie2/">http://code.shishnet.org/</a>[/url]
  13. * <li>[email]<a href="mailto:webmaster@shishnet.org">webmaster@shishnet.org</a>[/email]
  14. * <li>[b]<b>bold</b>[/b]
  15. * <li>[i]<i>italic</i>[/i]
  16. * <li>[u]<u>underline</u>[/u]
  17. * <li>[s]<s>strikethrough</s>[/s]
  18. * <li>[sup]<sup>superscript</sup>[/sup]
  19. * <li>[sub]<sub>subscript</sub>[/sub]
  20. * <li>[[wiki article]]
  21. * <li>[[wiki article|with some text]]
  22. * <li>[quote]text[/quote]
  23. * <li>[quote=Username]text[/quote]
  24. * <li>&gt;&gt;123 (link to image #123)
  25. * </ul>
  26. */
  27. class BBCode extends FormatterExtension {
  28. public function format(/*string*/ $text) {
  29. $text = $this->extract_code($text);
  30. foreach(array(
  31. "b", "i", "u", "s", "sup", "sub", "h1", "h2", "h3", "h4",
  32. ) as $el) {
  33. $text = preg_replace("!\[$el\](.*?)\[/$el\]!s", "<$el>$1</$el>", $text);
  34. }
  35. $text = preg_replace('!^&gt;&gt;([^\d].+)!', '<blockquote><small>$1</small></blockquote>', $text);
  36. $text = preg_replace('!&gt;&gt;(\d+)(#c?\d+)?!s', '<a class="shm-clink" data-clink-sel="$2" href="'.make_link('post/view/$1$2').'">&gt;&gt;$1$2</a>', $text);
  37. $text = preg_replace('!\[url=site://(.*?)(#c\d+)?\](.*?)\[/url\]!s', '<a class="shm-clink" data-clink-sel="$2" href="'.make_link('$1$2').'">$3</a>', $text);
  38. $text = preg_replace('!\[url\]site://(.*?)(#c\d+)?\[/url\]!s', '<a class="shm-clink" data-clink-sel="$2" href="'.make_link('$1$2').'">$1$2</a>', $text);
  39. $text = preg_replace('!\[url=((?:https?|ftp|irc|mailto)://.*?)\](.*?)\[/url\]!s', '<a href="$1">$2</a>', $text);
  40. $text = preg_replace('!\[url\]((?:https?|ftp|irc|mailto)://.*?)\[/url\]!s', '<a href="$1">$1</a>', $text);
  41. $text = preg_replace('!\[email\](.*?)\[/email\]!s', '<a href="mailto:$1">$1</a>', $text);
  42. $text = preg_replace('!\[img\](https?:\/\/.*?)\[/img\]!s', '<img src="$1">', $text);
  43. $text = preg_replace('!\[\[([^\|\]]+)\|([^\]]+)\]\]!s', '<a href="'.make_link('wiki/$1').'">$2</a>', $text);
  44. $text = preg_replace('!\[\[([^\]]+)\]\]!s', '<a href="'.make_link('wiki/$1').'">$1</a>', $text);
  45. $text = preg_replace("!\n\s*\n!", "\n\n", $text);
  46. $text = str_replace("\n", "\n<br>", $text);
  47. $text = preg_replace("/\[quote\](.*?)\[\/quote\]/s", "<blockquote><small>\\1</small></blockquote>", $text);
  48. $text = preg_replace("/\[quote=(.*?)\](.*?)\[\/quote\]/s", "<blockquote><em>\\1 said:</em><br><small>\\2</small></blockquote>", $text);
  49. while(preg_match("/\[list\](.*?)\[\/list\]/s", $text))
  50. $text = preg_replace("/\[list\](.*?)\[\/list\]/s", "<ul>\\1</ul>", $text);
  51. while(preg_match("/\[ul\](.*?)\[\/ul\]/s", $text))
  52. $text = preg_replace("/\[ul\](.*?)\[\/ul\]/s", "<ul>\\1</ul>", $text);
  53. while(preg_match("/\[ol\](.*?)\[\/ol\]/s", $text))
  54. $text = preg_replace("/\[ol\](.*?)\[\/ol\]/s", "<ol>\\1</ol>", $text);
  55. $text = preg_replace("/\[li\](.*?)\[\/li\]/s", "<li>\\1</li>", $text);
  56. $text = preg_replace("#\[\*\]#s", "<li>", $text);
  57. $text = preg_replace("#<br><(li|ul|ol|/ul|/ol)>#s", "<\\1>", $text);
  58. $text = preg_replace("#\[align=(left|center|right)\](.*?)\[\/align\]#s", "<div style='text-align:\\1;'>\\2</div>", $text);
  59. $text = $this->filter_spoiler($text);
  60. $text = $this->insert_code($text);
  61. return $text;
  62. }
  63. public function strip(/*string*/ $text) {
  64. foreach(array(
  65. "b", "i", "u", "s", "sup", "sub", "h1", "h2", "h3", "h4",
  66. "code", "url", "email", "li",
  67. ) as $el) {
  68. $text = preg_replace("!\[$el\](.*?)\[/$el\]!s", '$1', $text);
  69. }
  70. $text = preg_replace("!\[url=(.*?)\](.*?)\[/url\]!s", '$2', $text);
  71. $text = preg_replace("!\[img\](.*?)\[/img\]!s", "", $text);
  72. $text = preg_replace("!\[\[([^\|\]]+)\|([^\]]+)\]\]!s", '$2', $text);
  73. $text = preg_replace("!\[\[([^\]]+)\]\]!s", '$1', $text);
  74. $text = preg_replace("!\[quote\](.*?)\[/quote\]!s", "", $text);
  75. $text = preg_replace("!\[quote=(.*?)\](.*?)\[/quote\]!s", "", $text);
  76. $text = preg_replace("!\[/?(list|ul|ol)\]!", "", $text);
  77. $text = preg_replace("!\[\*\](.*?)!s", '$1', $text);
  78. $text = $this->strip_spoiler($text);
  79. return $text;
  80. }
  81. private function filter_spoiler(/*string*/ $text) {
  82. return str_replace(
  83. array("[spoiler]","[/spoiler]"),
  84. array("<span style=\"background-color:#000; color:#000;\">","</span>"),
  85. $text);
  86. }
  87. private function strip_spoiler(/*string*/ $text) {
  88. $l1 = strlen("[spoiler]");
  89. $l2 = strlen("[/spoiler]");
  90. while(true) {
  91. $start = strpos($text, "[spoiler]");
  92. if($start === false) break;
  93. $end = strpos($text, "[/spoiler]");
  94. if($end === false) break;
  95. $beginning = substr($text, 0, $start);
  96. $middle = str_rot13(substr($text, $start+$l1, ($end-$start-$l1)));
  97. $ending = substr($text, $end + $l2, (strlen($text)-$end+$l2));
  98. $text = $beginning . $middle . $ending;
  99. }
  100. return $text;
  101. }
  102. private function extract_code(/*string*/ $text) {
  103. # at the end of this function, the only code! blocks should be
  104. # the ones we've added -- others may contain malicious content,
  105. # which would only appear after decoding
  106. $text = str_replace("[code!]", "[code]", $text);
  107. $text = str_replace("[/code!]", "[/code]", $text);
  108. $l1 = strlen("[code]");
  109. $l2 = strlen("[/code]");
  110. while(true) {
  111. $start = strpos($text, "[code]");
  112. if($start === false) break;
  113. $end = strpos($text, "[/code]", $start);
  114. if($end === false) break;
  115. $beginning = substr($text, 0, $start);
  116. $middle = base64_encode(substr($text, $start+$l1, ($end-$start-$l1)));
  117. $ending = substr($text, $end + $l2, (strlen($text)-$end+$l2));
  118. $text = $beginning . "[code!]" . $middle . "[/code!]" . $ending;
  119. }
  120. return $text;
  121. }
  122. private function insert_code(/*string*/ $text) {
  123. $l1 = strlen("[code!]");
  124. $l2 = strlen("[/code!]");
  125. while(true) {
  126. $start = strpos($text, "[code!]");
  127. if($start === false) break;
  128. $end = strpos($text, "[/code!]");
  129. if($end === false) break;
  130. $beginning = substr($text, 0, $start);
  131. $middle = base64_decode(substr($text, $start+$l1, ($end-$start-$l1)));
  132. $ending = substr($text, $end + $l2, (strlen($text)-$end+$l2));
  133. $text = $beginning . "<pre>" . $middle . "</pre>" . $ending;
  134. }
  135. return $text;
  136. }
  137. }
  138. ?>