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.

313 lines
11 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('$', '\x01', $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('\x01'.++$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. if(!function_exists('deletenode')) {
  61. function deletenode(&$doc, $node)
  62. {
  63. $xpath = new DomXPath($doc);
  64. $list = $xpath->query("//".$node);
  65. foreach ($list as $child)
  66. $child->parentNode->removeChild($child);
  67. }}
  68. function _replace_code_cb($m){
  69. return "<code>".str_replace("\n","<br>\n",$m[1]). "</code>";
  70. }
  71. function html2bbcode($message)
  72. {
  73. $message = str_replace("\r", "", $message);
  74. $message = preg_replace_callback("|<pre><code>([^<]*)</code></pre>|ism", "_replace_code_cb", $message);
  75. $message = str_replace(array(
  76. "<li><p>",
  77. "</p></li>",
  78. ),
  79. array(
  80. "<li>",
  81. "</li>",
  82. ),
  83. $message);
  84. // remove namespaces
  85. $message = preg_replace('=<(\w+):(.+?)>=', '<removeme>', $message);
  86. $message = preg_replace('=</(\w+):(.+?)>=', '</removeme>', $message);
  87. $doc = new DOMDocument();
  88. $doc->preserveWhiteSpace = false;
  89. $message = mb_convert_encoding($message, 'HTML-ENTITIES', "UTF-8");
  90. @$doc->loadHTML($message);
  91. deletenode($doc, 'style');
  92. deletenode($doc, 'head');
  93. deletenode($doc, 'title');
  94. deletenode($doc, 'meta');
  95. deletenode($doc, 'xml');
  96. deletenode($doc, 'removeme');
  97. $xpath = new DomXPath($doc);
  98. $list = $xpath->query("//pre");
  99. foreach ($list as $node)
  100. $node->nodeValue = str_replace("\n", "\r", $node->nodeValue);
  101. $message = $doc->saveHTML();
  102. $message = str_replace(array("\n<", ">\n", "\r", "\n", "\xC3\x82\xC2\xA0"), array("<", ">", "<br />", " ", ""), $message);
  103. $message = preg_replace('= [\s]*=i', " ", $message);
  104. @$doc->loadHTML($message);
  105. node2bbcode($doc, 'html', array(), "", "");
  106. node2bbcode($doc, 'body', array(), "", "");
  107. // Outlook-Quote - Variant 1
  108. node2bbcode($doc, 'p', array('class'=>'MsoNormal', 'style'=>'margin-left:35.4pt'), '[quote]', '[/quote]');
  109. // Outlook-Quote - Variant 2
  110. node2bbcode($doc, 'div', array('style'=>'border:none;border-left:solid blue 1.5pt;padding:0cm 0cm 0cm 4.0pt'), '[quote]', '[/quote]');
  111. // MyBB-Stuff
  112. node2bbcode($doc, 'span', array('style'=>'text-decoration: underline;'), '[u]', '[/u]');
  113. node2bbcode($doc, 'span', array('style'=>'font-style: italic;'), '[i]', '[/i]');
  114. node2bbcode($doc, 'span', array('style'=>'font-weight: bold;'), '[b]', '[/b]');
  115. /*node2bbcode($doc, 'font', array('face'=>'/([\w ]+)/', 'size'=>'/(\d+)/', 'color'=>'/(.+)/'), '[font=$1][size=$2][color=$3]', '[/color][/size][/font]');
  116. node2bbcode($doc, 'font', array('size'=>'/(\d+)/', 'color'=>'/(.+)/'), '[size=$1][color=$2]', '[/color][/size]');
  117. node2bbcode($doc, 'font', array('face'=>'/([\w ]+)/', 'size'=>'/(.+)/'), '[font=$1][size=$2]', '[/size][/font]');
  118. node2bbcode($doc, 'font', array('face'=>'/([\w ]+)/', 'color'=>'/(.+)/'), '[font=$1][color=$3]', '[/color][/font]');
  119. node2bbcode($doc, 'font', array('face'=>'/([\w ]+)/'), '[font=$1]', '[/font]');
  120. node2bbcode($doc, 'font', array('size'=>'/(\d+)/'), '[size=$1]', '[/size]');
  121. node2bbcode($doc, 'font', array('color'=>'/(.+)/'), '[color=$1]', '[/color]');
  122. */
  123. // Untested
  124. //node2bbcode($doc, 'span', array('style'=>'/.*font-size:\s*(.+?)[,;].*font-family:\s*(.+?)[,;].*color:\s*(.+?)[,;].*/'), '[size=$1][font=$2][color=$3]', '[/color][/font][/size]');
  125. //node2bbcode($doc, 'span', array('style'=>'/.*font-size:\s*(\d+)[,;].*/'), '[size=$1]', '[/size]');
  126. //node2bbcode($doc, 'span', array('style'=>'/.*font-size:\s*(.+?)[,;].*/'), '[size=$1]', '[/size]');
  127. node2bbcode($doc, 'span', array('style'=>'/.*color:\s*(.+?)[,;].*/'), '[color="$1"]', '[/color]');
  128. //node2bbcode($doc, 'span', array('style'=>'/.*font-family:\s*(.+?)[,;].*/'), '[font=$1]', '[/font]');
  129. //node2bbcode($doc, 'div', array('style'=>'/.*font-family:\s*(.+?)[,;].*font-size:\s*(\d+?)pt.*/'), '[font=$1][size=$2]', '[/size][/font]');
  130. //node2bbcode($doc, 'div', array('style'=>'/.*font-family:\s*(.+?)[,;].*font-size:\s*(\d+?)px.*/'), '[font=$1][size=$2]', '[/size][/font]');
  131. //node2bbcode($doc, 'div', array('style'=>'/.*font-family:\s*(.+?)[,;].*/'), '[font=$1]', '[/font]');
  132. // Importing the classes - interesting for importing of posts from third party networks that were exported from friendica
  133. // Test
  134. //node2bbcode($doc, 'span', array('class'=>'/([\w ]+)/'), '[class=$1]', '[/class]');
  135. node2bbcode($doc, 'span', array('class'=>'type-link'), '[class=type-link]', '[/class]');
  136. node2bbcode($doc, 'span', array('class'=>'type-video'), '[class=type-video]', '[/class]');
  137. node2bbcode($doc, 'strong', array(), '[b]', '[/b]');
  138. node2bbcode($doc, 'em', array(), '[i]', '[/i]');
  139. node2bbcode($doc, 'b', array(), '[b]', '[/b]');
  140. node2bbcode($doc, 'i', array(), '[i]', '[/i]');
  141. node2bbcode($doc, 'u', array(), '[u]', '[/u]');
  142. node2bbcode($doc, 'big', array(), "[size=large]", "[/size]");
  143. node2bbcode($doc, 'small', array(), "[size=small]", "[/size]");
  144. node2bbcode($doc, 'blockquote', array(), '[quote]', '[/quote]');
  145. node2bbcode($doc, 'br', array(), "\n", '');
  146. node2bbcode($doc, 'p', array('class'=>'MsoNormal'), "\n", "");
  147. node2bbcode($doc, 'div', array('class'=>'MsoNormal'), "\r", "");
  148. node2bbcode($doc, 'span', array(), "", "");
  149. node2bbcode($doc, 'span', array(), "", "");
  150. node2bbcode($doc, 'pre', array(), "", "");
  151. node2bbcode($doc, 'div', array(), "\r", "\r");
  152. node2bbcode($doc, 'p', array(), "\n", "\n");
  153. node2bbcode($doc, 'ul', array(), "[list]", "[/list]");
  154. node2bbcode($doc, 'ol', array(), "[list=1]", "[/list]");
  155. node2bbcode($doc, 'li', array(), "[*]", "");
  156. node2bbcode($doc, 'hr', array(), "[hr]", "");
  157. node2bbcode($doc, 'table', array(), "", "");
  158. node2bbcode($doc, 'tr', array(), "\n", "");
  159. node2bbcode($doc, 'td', array(), "\t", "");
  160. //node2bbcode($doc, 'table', array(), "[table]", "[/table]");
  161. //node2bbcode($doc, 'th', array(), "[th]", "[/th]");
  162. //node2bbcode($doc, 'tr', array(), "[tr]", "[/tr]");
  163. //node2bbcode($doc, 'td', array(), "[td]", "[/td]");
  164. //node2bbcode($doc, 'h1', array(), "\n\n[size=xx-large][b]", "[/b][/size]\n");
  165. //node2bbcode($doc, 'h2', array(), "\n\n[size=x-large][b]", "[/b][/size]\n");
  166. //node2bbcode($doc, 'h3', array(), "\n\n[size=large][b]", "[/b][/size]\n");
  167. //node2bbcode($doc, 'h4', array(), "\n\n[size=medium][b]", "[/b][/size]\n");
  168. //node2bbcode($doc, 'h5', array(), "\n\n[size=small][b]", "[/b][/size]\n");
  169. //node2bbcode($doc, 'h6', array(), "\n\n[size=x-small][b]", "[/b][/size]\n");
  170. node2bbcode($doc, 'h1', array(), "\n\n[h1]", "[/h1]\n");
  171. node2bbcode($doc, 'h2', array(), "\n\n[h2]", "[/h2]\n");
  172. node2bbcode($doc, 'h3', array(), "\n\n[h3]", "[/h3]\n");
  173. node2bbcode($doc, 'h4', array(), "\n\n[h4]", "[/h4]\n");
  174. node2bbcode($doc, 'h5', array(), "\n\n[h5]", "[/h5]\n");
  175. node2bbcode($doc, 'h6', array(), "\n\n[h6]", "[/h6]\n");
  176. node2bbcode($doc, 'a', array('href'=>'/mailto:(.+)/'), '[mail=$1]', '[/mail]');
  177. node2bbcode($doc, 'a', array('href'=>'/(.+)/'), '[url=$1]', '[/url]');
  178. node2bbcode($doc, 'img', array('src'=>'/(.+)/', 'width'=>'/(\d+)/', 'height'=>'/(\d+)/'), '[img=$2x$3]$1', '[/img]');
  179. node2bbcode($doc, 'img', array('src'=>'/(.+)/'), '[img]$1', '[/img]');
  180. node2bbcode($doc, 'video', array('src'=>'/(.+)/'), '[video]$1', '[/video]');
  181. node2bbcode($doc, 'audio', array('src'=>'/(.+)/'), '[audio]$1', '[/audio]');
  182. node2bbcode($doc, 'iframe', array('src'=>'/(.+)/'), '[iframe]$1', '[/iframe]');
  183. node2bbcode($doc, 'code', array(), '[code]', '[/code]');
  184. node2bbcode($doc, 'key', array(), '[code]', '[/code]');
  185. $message = $doc->saveHTML();
  186. // I'm removing something really disturbing
  187. // Don't know exactly what it is
  188. $message = str_replace(chr(194).chr(160), ' ', $message);
  189. $message = str_replace("&nbsp;", " ", $message);
  190. // removing multiple DIVs
  191. $message = preg_replace('=\r *\r=i', "\n", $message);
  192. $message = str_replace("\r", "\n", $message);
  193. call_hooks('html2bbcode', $message);
  194. $message = strip_tags($message);
  195. $message = html_entity_decode($message, ENT_QUOTES, 'UTF-8');
  196. $message = str_replace(array("<"), array("&lt;"), $message);
  197. // remove quotes if they don't make sense
  198. $message = preg_replace('=\[/quote\][\s]*\[quote\]=i', "\n", $message);
  199. $message = preg_replace('=\[quote\]\s*=i', "[quote]", $message);
  200. $message = preg_replace('=\s*\[/quote\]=i', "[/quote]", $message);
  201. do {
  202. $oldmessage = $message;
  203. $message = str_replace("\n \n", "\n\n", $message);
  204. } while ($oldmessage != $message);
  205. do {
  206. $oldmessage = $message;
  207. $message = str_replace("\n\n\n", "\n\n", $message);
  208. } while ($oldmessage != $message);
  209. do {
  210. $oldmessage = $message;
  211. $message = str_replace(array(
  212. "[/size]\n\n",
  213. "\n[hr]",
  214. "[hr]\n",
  215. "\n[list",
  216. "[/list]\n",
  217. "\n[/",
  218. "[list]\n",
  219. "[list=1]\n",
  220. "\n[*]"),
  221. array(
  222. "[/size]\n",
  223. "[hr]",
  224. "[hr]",
  225. "[list",
  226. "[/list]",
  227. "[/",
  228. "[list]",
  229. "[list=1]",
  230. "[*]"),
  231. $message);
  232. } while ($message != $oldmessage);
  233. $message = str_replace(array('[b][b]', '[/b][/b]', '[i][i]', '[/i][/i]'),
  234. array('[b]', '[/b]', '[i]', '[/i]'), $message);
  235. // Handling Yahoo style of mails
  236. $message = str_replace('[hr][b]From:[/b]', '[quote][b]From:[/b]', $message);
  237. return(trim($message));
  238. }
  239. ?>