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.
 
 
 
 
 
 

176 lines
6.1 KiB

  1. <?php
  2. require_once("include/oembed.php");
  3. require_once("include/event.php");
  4. require_once("library/markdown.php");
  5. require_once("include/html2bbcode.php");
  6. require_once("include/bbcode.php");
  7. require_once("library/html-to-markdown/HTML_To_Markdown.php");
  8. // we don't want to support a bbcode specific markdown interpreter
  9. // and the markdown library we have is pretty good, but provides HTML output.
  10. // So we'll use that to convert to HTML, then convert the HTML back to bbcode,
  11. // and then clean up a few Diaspora specific constructs.
  12. function diaspora2bb($s) {
  13. $s = html_entity_decode($s,ENT_COMPAT,'UTF-8');
  14. // Remove CR to avoid problems with following code
  15. $s = str_replace("\r","",$s);
  16. $s = str_replace("\n"," \n",$s);
  17. // The parser cannot handle paragraphs correctly
  18. $s = str_replace(array("</p>", "<p>", '<p dir="ltr">'),array("<br>", "<br>", "<br>"),$s);
  19. // Escaping the hash tags
  20. $s = preg_replace('/\#([^\s\#])/','&#35;$1',$s);
  21. $s = Markdown($s);
  22. $s = preg_replace('/\@\{(.+?)\; (.+?)\@(.+?)\}/','@[url=https://$3/u/$2]$1[/url]',$s);
  23. $s = str_replace('&#35;','#',$s);
  24. $search = array(" \n", "\n ");
  25. $replace = array("\n", "\n");
  26. do {
  27. $oldtext = $s;
  28. $s = str_replace($search, $replace, $s);
  29. } while ($oldtext != $s);
  30. $s = str_replace("\n\n", "<br>", $s);
  31. $s = html2bbcode($s);
  32. // protect the recycle symbol from turning into a tag, but without unescaping angles and naked ampersands
  33. $s = str_replace('&#x2672;',html_entity_decode('&#x2672;',ENT_QUOTES,'UTF-8'),$s);
  34. // Convert everything that looks like a link to a link
  35. $s = preg_replace("/([^\]\=]|^)(https?\:\/\/)([a-zA-Z0-9\:\/\-\?\&\;\.\=\_\~\#\%\$\!\+\,]+)/ism", '$1[url=$2$3]$2$3[/url]',$s);
  36. //$s = preg_replace("/([^\]\=]|^)(https?\:\/\/)(vimeo|youtu|www\.youtube|soundcloud)([a-zA-Z0-9\:\/\-\?\&\;\.\=\_\~\#\%\$\!\+\,]+)/ism", '$1[url=$2$3$4]$2$3$4[/url]',$s);
  37. $s = bb_tag_preg_replace("/\[url\=?(.*?)\]https?:\/\/www.youtube.com\/watch\?v\=(.*?)\[\/url\]/ism",'[youtube]$2[/youtube]','url',$s);
  38. $s = bb_tag_preg_replace("/\[url\=https?:\/\/www.youtube.com\/watch\?v\=(.*?)\].*?\[\/url\]/ism",'[youtube]$1[/youtube]','url',$s);
  39. $s = bb_tag_preg_replace("/\[url\=?(.*?)\]https?:\/\/vimeo.com\/([0-9]+)(.*?)\[\/url\]/ism",'[vimeo]$2[/vimeo]','url',$s);
  40. $s = bb_tag_preg_replace("/\[url\=https?:\/\/vimeo.com\/([0-9]+)\](.*?)\[\/url\]/ism",'[vimeo]$1[/vimeo]','url',$s);
  41. // remove duplicate adjacent code tags
  42. $s = preg_replace("/(\[code\])+(.*?)(\[\/code\])+/ism","[code]$2[/code]", $s);
  43. // Don't show link to full picture (until it is fixed)
  44. $s = scale_external_images($s, false);
  45. return $s;
  46. }
  47. function bb2diaspora($Text,$preserve_nl = false, $fordiaspora = true) {
  48. $a = get_app();
  49. $OriginalText = $Text;
  50. // Since Diaspora is creating a summary for links, this function removes them before posting
  51. if ($fordiaspora)
  52. $Text = bb_remove_share_information($Text);
  53. /**
  54. * Transform #tags, strip off the [url] and replace spaces with underscore
  55. */
  56. $URLSearchString = "^\[\]";
  57. $Text = preg_replace_callback("/#\[url\=([$URLSearchString]*)\](.*?)\[\/url\]/i", create_function('$match',
  58. 'return \'#\'. str_replace(\' \', \'_\', $match[2]);'
  59. ), $Text);
  60. // Converting images with size parameters to simple images. Markdown doesn't know it.
  61. $Text = preg_replace("/\[img\=([0-9]*)x([0-9]*)\](.*?)\[\/img\]/ism", '[img]$3[/img]', $Text);
  62. // Convert it to HTML - don't try oembed
  63. if ($fordiaspora) {
  64. $Text = bbcode($Text, $preserve_nl, false, 3);
  65. // Add all tags that maybe were removed
  66. if (preg_match_all("/#\[url\=([$URLSearchString]*)\](.*?)\[\/url\]/ism",$OriginalText, $tags)) {
  67. $tagline = "";
  68. foreach($tags[2] as $tag) {
  69. $tag = html_entity_decode($tag, ENT_QUOTES, 'UTF-8');
  70. if (!strpos(html_entity_decode($Text, ENT_QUOTES, 'UTF-8'), "#".$tag))
  71. $tagline .= "#".$tag." ";
  72. }
  73. $Text = $Text." ".$tagline;
  74. }
  75. } else
  76. $Text = bbcode($Text, $preserve_nl, false, 4);
  77. // mask some special HTML chars from conversation to markdown
  78. $Text = str_replace(array('&lt;','&gt;','&amp;'),array('&_lt_;','&_gt_;','&_amp_;'),$Text);
  79. // If a link is followed by a quote then there should be a newline before it
  80. // Maybe we should make this newline at every time before a quote.
  81. $Text = str_replace(array("</a><blockquote>"), array("</a><br><blockquote>"), $Text);
  82. $stamp1 = microtime(true);
  83. // Now convert HTML to Markdown
  84. $Text = new HTML_To_Markdown($Text);
  85. // unmask the special chars back to HTML
  86. $Text = str_replace(array('&_lt_;','&_gt_;','&_amp_;'),array('&lt;','&gt;','&amp;'),$Text);
  87. $a->save_timestamp($stamp1, "parser");
  88. // Libertree has a problem with escaped hashtags.
  89. $Text = str_replace(array('\#'), array('#'), $Text);
  90. // Remove any leading or trailing whitespace, as this will mess up
  91. // the Diaspora signature verification and cause the item to disappear
  92. $Text = trim($Text);
  93. call_hooks('bb2diaspora',$Text);
  94. return $Text;
  95. }
  96. function unescape_underscores_in_links($m) {
  97. $y = str_replace('\\_','_', $m[2]);
  98. return('[' . $m[1] . '](' . $y . ')');
  99. }
  100. function format_event_diaspora($ev) {
  101. $a = get_app();
  102. if(! ((is_array($ev)) && count($ev)))
  103. return '';
  104. $bd_format = t('l F d, Y \@ g:i A') ; // Friday January 18, 2011 @ 8 AM
  105. $o = 'Friendica event notification:' . "\n";
  106. $o .= '**' . (($ev['summary']) ? bb2diaspora($ev['summary']) : bb2diaspora($ev['desc'])) . '**' . "\n";
  107. $o .= t('Starts:') . ' ' . '['
  108. . (($ev['adjust']) ? day_translate(datetime_convert('UTC', 'UTC',
  109. $ev['start'] , $bd_format ))
  110. : day_translate(datetime_convert('UTC', 'UTC',
  111. $ev['start'] , $bd_format)))
  112. . '](' . $a->get_baseurl() . '/localtime/?f=&time=' . urlencode(datetime_convert('UTC','UTC',$ev['start'])) . ")\n";
  113. if(! $ev['nofinish'])
  114. $o .= t('Finishes:') . ' ' . '['
  115. . (($ev['adjust']) ? day_translate(datetime_convert('UTC', 'UTC',
  116. $ev['finish'] , $bd_format ))
  117. : day_translate(datetime_convert('UTC', 'UTC',
  118. $ev['finish'] , $bd_format )))
  119. . '](' . $a->get_baseurl() . '/localtime/?f=&time=' . urlencode(datetime_convert('UTC','UTC',$ev['finish'])) . ")\n";
  120. if(strlen($ev['location']))
  121. $o .= t('Location:') . bb2diaspora($ev['location'])
  122. . "\n";
  123. $o .= "\n";
  124. return $o;
  125. }