Friendica Communications Platform (please note that this is a clone of the repository at github, issues are handled there) https://friendi.ca
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

290 lines
9.5 KiB

  1. <?php
  2. /*
  3. html2bbcode.php
  4. Converter for HTML to BBCode
  5. Made by: ike@piratenpartei.de
  6. Originally made for the syncom project: http://wiki.piratenpartei.de/Syncom
  7. https://github.com/annando/Syncom
  8. */
  9. function node2bbcode(&$doc, $oldnode, $attributes, $startbb, $endbb)
  10. {
  11. do {
  12. $done = node2bbcodesub($doc, $oldnode, $attributes, $startbb, $endbb);
  13. } while ($done);
  14. }
  15. function node2bbcodesub(&$doc, $oldnode, $attributes, $startbb, $endbb)
  16. {
  17. $savestart = str_replace('$', '%', $startbb);
  18. $replace = false;
  19. $xpath = new DomXPath($doc);
  20. $list = $xpath->query("//".$oldnode);
  21. foreach ($list as $oldNode) {
  22. $attr = array();
  23. if ($oldNode->attributes->length)
  24. foreach ($oldNode->attributes as $attribute)
  25. $attr[$attribute->name] = $attribute->value;
  26. $replace = true;
  27. $startbb = $savestart;
  28. $i = 0;
  29. foreach ($attributes as $attribute => $value) {
  30. $startbb = str_replace('%'.++$i, '$1', $startbb);
  31. if (strpos('*'.$startbb, '$1') > 0) {
  32. if ($replace and (@$attr[$attribute] != '')) {
  33. $startbb = preg_replace($value, $startbb, $attr[$attribute], -1, $count);
  34. // If nothing could be changed
  35. if ($count == 0)
  36. $replace = false;
  37. } else
  38. $replace = false;
  39. } else {
  40. if (@$attr[$attribute] != $value)
  41. $replace = false;
  42. }
  43. }
  44. if ($replace) {
  45. $StartCode = $oldNode->ownerDocument->createTextNode($startbb);
  46. $EndCode = $oldNode->ownerDocument->createTextNode($endbb);
  47. $oldNode->parentNode->insertBefore($StartCode, $oldNode);
  48. if ($oldNode->hasChildNodes()) {
  49. foreach ($oldNode->childNodes as $child) {
  50. $newNode = $child->cloneNode(true);
  51. $oldNode->parentNode->insertBefore($newNode, $oldNode);
  52. }
  53. }
  54. $oldNode->parentNode->insertBefore($EndCode, $oldNode);
  55. $oldNode->parentNode->removeChild($oldNode);
  56. }
  57. }
  58. return($replace);
  59. }
  60. function deletenode(&$doc, $node)
  61. {
  62. $xpath = new DomXPath($doc);
  63. $list = $xpath->query("//".$node);
  64. foreach ($list as $child)
  65. $child->parentNode->removeChild($child);
  66. }
  67. function html2bbcode($message)
  68. {
  69. //$file = tempnam("/tmp/", "html");
  70. //file_put_contents($file, $message);
  71. $message = str_replace("\r", "", $message);
  72. $message = str_replace(array(
  73. "<li><p>",
  74. "</p></li>"),
  75. array(
  76. "<li>",
  77. "</li>"),
  78. $message);
  79. // remove namespaces
  80. $message = preg_replace('=<(\w+):(.+?)>=', '<removeme>', $message);
  81. $message = preg_replace('=</(\w+):(.+?)>=', '</removeme>', $message);
  82. $doc = new DOMDocument();
  83. $doc->preserveWhiteSpace = false;
  84. $message = mb_convert_encoding($message, 'HTML-ENTITIES', "UTF-8");
  85. @$doc->loadHTML($message);
  86. deletenode($doc, 'style');
  87. deletenode($doc, 'head');
  88. deletenode($doc, 'title');
  89. deletenode($doc, 'meta');
  90. deletenode($doc, 'xml');
  91. deletenode($doc, 'removeme');
  92. $xpath = new DomXPath($doc);
  93. $list = $xpath->query("//pre");
  94. foreach ($list as $node)
  95. $node->nodeValue = str_replace("\n", "\r", $node->nodeValue);
  96. $message = $doc->saveHTML();
  97. $message = str_replace(array("\n<", ">\n", "\r", "\n", "\xC3\x82\xC2\xA0"), array("<", ">", "<br>", " ", ""), $message);
  98. $message = preg_replace('= [\s]*=i', " ", $message);
  99. @$doc->loadHTML($message);
  100. node2bbcode($doc, 'html', array(), "", "");
  101. node2bbcode($doc, 'body', array(), "", "");
  102. // Outlook-Quote - Variant 1
  103. node2bbcode($doc, 'p', array('class'=>'MsoNormal', 'style'=>'margin-left:35.4pt'), '[quote]', '[/quote]');
  104. // Outlook-Quote - Variant 2
  105. node2bbcode($doc, 'div', array('style'=>'border:none;border-left:solid blue 1.5pt;padding:0cm 0cm 0cm 4.0pt'), '[quote]', '[/quote]');
  106. // MyBB-Stuff
  107. node2bbcode($doc, 'span', array('style'=>'text-decoration: underline;'), '[u]', '[/u]');
  108. node2bbcode($doc, 'span', array('style'=>'font-style: italic;'), '[i]', '[/i]');
  109. node2bbcode($doc, 'span', array('style'=>'font-weight: bold;'), '[b]', '[/b]');
  110. /*node2bbcode($doc, 'font', array('face'=>'/([\w ]+)/', 'size'=>'/(\d+)/', 'color'=>'/(.+)/'), '[font=$1][size=$2][color=$3]', '[/color][/size][/font]');
  111. node2bbcode($doc, 'font', array('size'=>'/(\d+)/', 'color'=>'/(.+)/'), '[size=$1][color=$2]', '[/color][/size]');
  112. node2bbcode($doc, 'font', array('face'=>'/([\w ]+)/', 'size'=>'/(.+)/'), '[font=$1][size=$2]', '[/size][/font]');
  113. node2bbcode($doc, 'font', array('face'=>'/([\w ]+)/', 'color'=>'/(.+)/'), '[font=$1][color=$3]', '[/color][/font]');
  114. node2bbcode($doc, 'font', array('face'=>'/([\w ]+)/'), '[font=$1]', '[/font]');
  115. node2bbcode($doc, 'font', array('size'=>'/(\d+)/'), '[size=$1]', '[/size]');
  116. node2bbcode($doc, 'font', array('color'=>'/(.+)/'), '[color=$1]', '[/color]');
  117. */
  118. // Untested
  119. //node2bbcode($doc, 'span', array('style'=>'/.*font-size:\s*(.+?)[,;].*font-family:\s*(.+?)[,;].*color:\s*(.+?)[,;].*/'), '[size=$1][font=$2][color=$3]', '[/color][/font][/size]');
  120. //node2bbcode($doc, 'span', array('style'=>'/.*font-size:\s*(\d+)[,;].*/'), '[size=$1]', '[/size]');
  121. //node2bbcode($doc, 'span', array('style'=>'/.*font-size:\s*(.+?)[,;].*/'), '[size=$1]', '[/size]');
  122. node2bbcode($doc, 'span', array('style'=>'/.*color:\s*(.+?)[,;].*/'), '[color="$1"]', '[/color]');
  123. //node2bbcode($doc, 'span', array('style'=>'/.*font-family:\s*(.+?)[,;].*/'), '[font=$1]', '[/font]');
  124. //node2bbcode($doc, 'div', array('style'=>'/.*font-family:\s*(.+?)[,;].*font-size:\s*(\d+?)pt.*/'), '[font=$1][size=$2]', '[/size][/font]');
  125. //node2bbcode($doc, 'div', array('style'=>'/.*font-family:\s*(.+?)[,;].*font-size:\s*(\d+?)px.*/'), '[font=$1][size=$2]', '[/size][/font]');
  126. //node2bbcode($doc, 'div', array('style'=>'/.*font-family:\s*(.+?)[,;].*/'), '[font=$1]', '[/font]');
  127. node2bbcode($doc, 'strong', array(), '[b]', '[/b]');
  128. node2bbcode($doc, 'em', array(), '[i]', '[/i]');
  129. node2bbcode($doc, 'b', array(), '[b]', '[/b]');
  130. node2bbcode($doc, 'i', array(), '[i]', '[/i]');
  131. node2bbcode($doc, 'u', array(), '[u]', '[/u]');
  132. node2bbcode($doc, 'big', array(), "[size=large]", "[/size]");
  133. node2bbcode($doc, 'small', array(), "[size=small]", "[/size]");
  134. node2bbcode($doc, 'blockquote', array(), '[quote]', '[/quote]');
  135. node2bbcode($doc, 'br', array(), "\n", '');
  136. node2bbcode($doc, 'p', array('class'=>'MsoNormal'), "\n", "");
  137. node2bbcode($doc, 'div', array('class'=>'MsoNormal'), "\r", "");
  138. node2bbcode($doc, 'span', array(), "", "");
  139. node2bbcode($doc, 'span', array(), "", "");
  140. node2bbcode($doc, 'pre', array(), "", "");
  141. node2bbcode($doc, 'div', array(), "\r", "\r");
  142. node2bbcode($doc, 'p', array(), "\n", "\n");
  143. node2bbcode($doc, 'ul', array(), "[list]", "[/list]");
  144. node2bbcode($doc, 'ol', array(), "[list=1]", "[/list]");
  145. node2bbcode($doc, 'li', array(), "[*]", "");
  146. node2bbcode($doc, 'hr', array(), "[hr]", "");
  147. node2bbcode($doc, 'table', array(), "", "");
  148. node2bbcode($doc, 'tr', array(), "\n", "");
  149. node2bbcode($doc, 'td', array(), "\t", "");
  150. //node2bbcode($doc, 'table', array(), "[table]", "[/table]");
  151. //node2bbcode($doc, 'th', array(), "[th]", "[/th]");
  152. //node2bbcode($doc, 'tr', array(), "[tr]", "[/tr]");
  153. //node2bbcode($doc, 'td', array(), "[td]", "[/td]");
  154. node2bbcode($doc, 'h1', array(), "\n\n[size=xx-large][b]", "[/b][/size]\n");
  155. node2bbcode($doc, 'h2', array(), "\n\n[size=x-large][b]", "[/b][/size]\n");
  156. node2bbcode($doc, 'h3', array(), "\n\n[size=large][b]", "[/b][/size]\n");
  157. node2bbcode($doc, 'h4', array(), "\n\n[size=medium][b]", "[/b][/size]\n");
  158. node2bbcode($doc, 'h5', array(), "\n\n[size=small][b]", "[/b][/size]\n");
  159. node2bbcode($doc, 'h6', array(), "\n\n[size=x-small][b]", "[/b][/size]\n");
  160. node2bbcode($doc, 'a', array('href'=>'/(.+)/'), '[url=$1]', '[/url]');
  161. node2bbcode($doc, 'img', array('src'=>'/(.+)/', 'width'=>'/(\d+)/', 'height'=>'/(\d+)/'), '[img=$2x$3]$1', '[/img]');
  162. node2bbcode($doc, 'img', array('src'=>'/(.+)/'), '[img]$1', '[/img]');
  163. node2bbcode($doc, 'video', array('src'=>'/(.+)/'), '[video]$1', '[/video]');
  164. node2bbcode($doc, 'audio', array('src'=>'/(.+)/'), '[audio]$1', '[/audio]');
  165. node2bbcode($doc, 'iframe', array('src'=>'/(.+)/'), '[iframe]$1', '[/iframe]');
  166. node2bbcode($doc, 'code', array(), '[code]', '[/code]');
  167. $message = $doc->saveHTML();
  168. // I'm removing something really disturbing
  169. // Don't know exactly what it is
  170. $message = str_replace(chr(194).chr(160), ' ', $message);
  171. $message = str_replace("&nbsp;", " ", $message);
  172. // removing multiple DIVs
  173. $message = preg_replace('=\r *\r=i', "\n", $message);
  174. $message = str_replace("\r", "\n", $message);
  175. call_hooks('html2bbcode', $message);
  176. $message = strip_tags($message);
  177. $message = html_entity_decode($message, ENT_QUOTES, 'UTF-8');
  178. $message = str_replace(array("<"), array("&lt;"), $message);
  179. // remove quotes if they don't make sense
  180. $message = preg_replace('=\[/quote\][\s]*\[quote\]=i', "\n", $message);
  181. $message = preg_replace('=\[quote\]\s*=i', "[quote]", $message);
  182. $message = preg_replace('=\s*\[/quote\]=i', "[/quote]", $message);
  183. do {
  184. $oldmessage = $message;
  185. $message = str_replace("\n \n", "\n\n", $message);
  186. } while ($oldmessage != $message);
  187. do {
  188. $oldmessage = $message;
  189. $message = str_replace("\n\n\n", "\n\n", $message);
  190. } while ($oldmessage != $message);
  191. do {
  192. $oldmessage = $message;
  193. $message = str_replace(array(
  194. "[/size]\n\n",
  195. "\n[hr]",
  196. "[hr]\n",
  197. "\n[list",
  198. "[/list]\n",
  199. "\n[/",
  200. "[list]\n",
  201. "[list=1]\n",
  202. "\n[*]"),
  203. array(
  204. "[/size]\n",
  205. "[hr]",
  206. "[hr]",
  207. "[list",
  208. "[/list]",
  209. "[/",
  210. "[list]",
  211. "[list=1]",
  212. "[*]"),
  213. $message);
  214. } while ($message != $oldmessage);
  215. $message = str_replace(array('[b][b]', '[/b][/b]', '[i][i]', '[/i][/i]'),
  216. array('[b]', '[/b]', '[i]', '[/i]'), $message);
  217. // Handling Yahoo style of mails
  218. $message = str_replace('[hr][b]From:[/b]', '[quote][b]From:[/b]', $message);
  219. return(trim($message));
  220. }
  221. ?>