Friendica Communications Platform (please note that this is a clone of the repository at github, issues are handled there) https://friendi.ca
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

262 lignes
8.3KB

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