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.

236 lines
6.5KB

  1. <?php
  2. require_once("include/html2bbcode.php");
  3. function breaklines($line, $level, $wraplength = 75)
  4. {
  5. if ($wraplength == 0)
  6. $wraplength = 2000000;
  7. // return($line);
  8. $wraplen = $wraplength-$level;
  9. $newlines = array();
  10. do {
  11. $oldline = $line;
  12. $subline = substr($line, 0, $wraplen);
  13. $pos = strrpos($subline, ' ');
  14. if ($pos == 0)
  15. $pos = strpos($line, ' ');
  16. if (($pos > 0) and strlen($line) > $wraplen) {
  17. $newline = trim(substr($line, 0, $pos));
  18. if ($level > 0)
  19. $newline = str_repeat(">", $level).' '.$newline;
  20. $newlines[] = $newline." ";
  21. $line = substr($line, $pos+1);
  22. }
  23. } while ((strlen($line) > $wraplen) and !($oldline == $line));
  24. if ($level > 0)
  25. $line = str_repeat(">", $level).' '.$line;
  26. $newlines[] = $line;
  27. return(implode($newlines, "\n"));
  28. }
  29. function quotelevel($message, $wraplength = 75)
  30. {
  31. $lines = explode("\n", $message);
  32. $newlines = array();
  33. $level = 0;
  34. foreach ($lines as $line) {;
  35. $line = trim($line);
  36. $startquote = false;
  37. while (strpos("*".$line, '[quote]') > 0) {
  38. $level++;
  39. $pos = strpos($line, '[quote]');
  40. $line = substr($line, 0, $pos).substr($line, $pos+7);
  41. $startquote = true;
  42. }
  43. $currlevel = $level;
  44. while (strpos("*".$line, '[/quote]') > 0) {
  45. $level--;
  46. if ($level < 0)
  47. $level = 0;
  48. $pos = strpos($line, '[/quote]');
  49. $line = substr($line, 0, $pos).substr($line, $pos+8);
  50. }
  51. if (!$startquote or ($line != ''))
  52. $newlines[] = breaklines($line, $currlevel, $wraplength);
  53. }
  54. return(implode($newlines, "\n"));
  55. }
  56. function collecturls($message) {
  57. $pattern = '/<a.*?href="(.*?)".*?>(.*?)<\/a>/is';
  58. preg_match_all($pattern, $message, $result, PREG_SET_ORDER);
  59. $urls = array();
  60. foreach ($result as $treffer) {
  61. $ignore = false;
  62. // A list of some links that should be ignored
  63. $list = array("/user/", "/tag/", "/group/", "/profile/", "/search?search=", "/search?tag=", "mailto:", "/u/", "/node/",
  64. "//facebook.com/profile.php?id=", "//plus.google.com/", "//twitter.com/");
  65. foreach ($list as $listitem)
  66. if (strpos($treffer[1], $listitem) !== false)
  67. $ignore = true;
  68. if ((strpos($treffer[1], "//twitter.com/") !== false) and (strpos($treffer[1], "/status/") !== false))
  69. $ignore = false;
  70. if ((strpos($treffer[1], "//plus.google.com/") !== false) and (strpos($treffer[1], "/posts") !== false))
  71. $ignore = false;
  72. if ((strpos($treffer[1], "//plus.google.com/") !== false) and (strpos($treffer[1], "/photos") !== false))
  73. $ignore = false;
  74. if (!$ignore)
  75. $urls[$treffer[1]] = $treffer[1];
  76. }
  77. return($urls);
  78. }
  79. function html2plain($html, $wraplength = 75, $compact = false)
  80. {
  81. global $lang;
  82. $message = str_replace("\r", "", $html);
  83. $doc = new DOMDocument();
  84. $doc->preserveWhiteSpace = false;
  85. $message = mb_convert_encoding($message, 'HTML-ENTITIES', "UTF-8");
  86. @$doc->loadHTML($message);
  87. $xpath = new DomXPath($doc);
  88. $list = $xpath->query("//pre");
  89. foreach ($list as $node) {
  90. $node->nodeValue = str_replace("\n", "\r", $node->nodeValue);
  91. }
  92. $message = $doc->saveHTML();
  93. $message = str_replace(array("\n<", ">\n", "\r", "\n", "\xC3\x82\xC2\xA0"), array("<", ">", "<br>", " ", ""), $message);
  94. $message = preg_replace('= [\s]*=i', " ", $message);
  95. // Collecting all links
  96. $urls = collecturls($message);
  97. @$doc->loadHTML($message);
  98. node2bbcode($doc, 'html', array(), '', '');
  99. node2bbcode($doc, 'body', array(), '', '');
  100. // MyBB-Auszeichnungen
  101. /*
  102. node2bbcode($doc, 'span', array('style'=>'text-decoration: underline;'), '_', '_');
  103. node2bbcode($doc, 'span', array('style'=>'font-style: italic;'), '/', '/');
  104. node2bbcode($doc, 'span', array('style'=>'font-weight: bold;'), '*', '*');
  105. node2bbcode($doc, 'strong', array(), '*', '*');
  106. node2bbcode($doc, 'b', array(), '*', '*');
  107. node2bbcode($doc, 'i', array(), '/', '/');
  108. node2bbcode($doc, 'u', array(), '_', '_');
  109. */
  110. if ($compact)
  111. node2bbcode($doc, 'blockquote', array(), "»", "«");
  112. else
  113. node2bbcode($doc, 'blockquote', array(), '[quote]', "[/quote]\n");
  114. node2bbcode($doc, 'br', array(), "\n", '');
  115. node2bbcode($doc, 'span', array(), "", "");
  116. node2bbcode($doc, 'pre', array(), "", "");
  117. node2bbcode($doc, 'div', array(), "\r", "\r");
  118. node2bbcode($doc, 'p', array(), "\n", "\n");
  119. //node2bbcode($doc, 'ul', array(), "\n[list]", "[/list]\n");
  120. //node2bbcode($doc, 'ol', array(), "\n[list=1]", "[/list]\n");
  121. node2bbcode($doc, 'li', array(), "\n* ", "\n");
  122. node2bbcode($doc, 'hr', array(), "\n".str_repeat("-", 70)."\n", "");
  123. node2bbcode($doc, 'tr', array(), "\n", "");
  124. node2bbcode($doc, 'td', array(), "\t", "");
  125. node2bbcode($doc, 'h1', array(), "\n\n*", "*\n");
  126. node2bbcode($doc, 'h2', array(), "\n\n*", "*\n");
  127. node2bbcode($doc, 'h3', array(), "\n\n*", "*\n");
  128. node2bbcode($doc, 'h4', array(), "\n\n*", "*\n");
  129. node2bbcode($doc, 'h5', array(), "\n\n*", "*\n");
  130. node2bbcode($doc, 'h6', array(), "\n\n*", "*\n");
  131. // Problem: there is no reliable way to detect if it is a link to a tag or profile
  132. //node2bbcode($doc, 'a', array('href'=>'/(.+)/'), ' $1 ', ' ', true);
  133. //node2bbcode($doc, 'a', array('href'=>'/(.+)/', 'rel'=>'oembed'), ' $1 ', '', true);
  134. //node2bbcode($doc, 'img', array('alt'=>'/(.+)/'), '$1', '');
  135. //node2bbcode($doc, 'img', array('title'=>'/(.+)/'), '$1', '');
  136. //node2bbcode($doc, 'img', array(), '', '');
  137. if (!$compact)
  138. node2bbcode($doc, 'img', array('src'=>'/(.+)/'), ' [img]$1', '[/img] ');
  139. else
  140. node2bbcode($doc, 'img', array('src'=>'/(.+)/'), ' ', ' ');
  141. node2bbcode($doc, 'iframe', array('src'=>'/(.+)/'), ' $1 ', '', true);
  142. $message = $doc->saveHTML();
  143. if (!$compact) {
  144. $message = str_replace("[img]", "", $message);
  145. $message = str_replace("[/img]", "", $message);
  146. }
  147. // was ersetze ich da?
  148. // Irgendein stoerrisches UTF-Zeug
  149. $message = str_replace(chr(194).chr(160), ' ', $message);
  150. $message = str_replace("&nbsp;", " ", $message);
  151. // Aufeinanderfolgende DIVs
  152. $message = preg_replace('=\r *\r=i', "\n", $message);
  153. $message = str_replace("\r", "\n", $message);
  154. $message = strip_tags($message);
  155. $message = html_entity_decode($message, ENT_QUOTES, 'UTF-8');
  156. if (!$compact AND ($message != "")) {
  157. $counter = 1;
  158. foreach ($urls as $id=>$url)
  159. if ($url != "")
  160. if (strpos($message, $url) === false)
  161. $message .= "\n".$url." ";
  162. //$message .= "\n[".($counter++)."] ".$url;
  163. }
  164. $message = str_replace("\n«", "«\n", $message);
  165. $message = str_replace("»\n", "\n»", $message);
  166. do {
  167. $oldmessage = $message;
  168. $message = str_replace("\n\n\n", "\n\n", $message);
  169. } while ($oldmessage != $message);
  170. $message = quotelevel(trim($message), $wraplength);
  171. return(trim($message));
  172. }