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.

226 lines
7.3 KiB

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