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.

328 lines
11KB

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