7f942be141e70b8fc72bfab092f377193bebce64
[friendica.git/.git] / include / bb2diaspora.php
1 <?php
2
3 use Friendica\Content\Text\BBCode;
4 use Friendica\Content\Text\Markdown;
5 use Friendica\Core\Addon;
6 use Friendica\Core\L10n;
7 use Friendica\Core\System;
8 use Friendica\Model\Contact;
9 use Friendica\Network\Probe;
10 use Friendica\Util\DateTimeFormat;
11 use League\HTMLToMarkdown\HtmlConverter;
12
13 require_once 'include/event.php';
14 require_once 'include/html2bbcode.php';
15
16 function diaspora2bb($s) {
17         return Markdown::toBBCode($s);
18 }
19
20 /**
21  * @brief Callback function to replace a Friendica style mention in a mention for Diaspora
22  *
23  * @param array $match Matching values for the callback
24  * @return string Replaced mention
25  */
26 function diaspora_mentions($match) {
27
28         $contact = Contact::getDetailsByURL($match[3]);
29
30         if (!x($contact, 'addr')) {
31                 $contact = Probe::uri($match[3]);
32         }
33
34         if (!x($contact, 'addr')) {
35                 return $match[0];
36         }
37
38         $mention = '@{' . $match[2] . '; ' . $contact['addr'] . '}';
39         return $mention;
40 }
41
42 /**
43  * @brief Converts a BBCode text into Markdown
44  *
45  * This function converts a BBCode item body to be sent to Markdown-enabled
46  * systems like Diaspora and Libertree
47  *
48  * @param string $Text
49  * @param bool $fordiaspora Diaspora requires more changes than Libertree
50  * @return string
51  */
52 function bb2diaspora($Text, $fordiaspora = true) {
53         $a = get_app();
54
55         $OriginalText = $Text;
56
57         // Since Diaspora is creating a summary for links, this function removes them before posting
58         if ($fordiaspora) {
59                 $Text = BBCode::removeShareInformation($Text);
60         }
61
62         /**
63          * Transform #tags, strip off the [url] and replace spaces with underscore
64          */
65         $URLSearchString = "^\[\]";
66         $Text = preg_replace_callback("/#\[url\=([$URLSearchString]*)\](.*?)\[\/url\]/i",
67                 function ($matches) {
68                         return '#' . str_replace(' ', '_', $matches[2]);
69                 }
70         , $Text);
71
72         // Converting images with size parameters to simple images. Markdown doesn't know it.
73         $Text = preg_replace("/\[img\=([0-9]*)x([0-9]*)\](.*?)\[\/img\]/ism", '[img]$3[/img]', $Text);
74
75         // Extracting multi-line code blocks before the whitespace processing/code highlighter in BBCode::convert()
76         $codeblocks = [];
77
78         $Text = preg_replace_callback("#\[code(?:=([^\]]*))?\](.*?)\[\/code\]#is",
79                 function ($matches) use (&$codeblocks) {
80                         $return = $matches[0];
81                         if (strpos($matches[2], "\n") !== false) {
82                                 $return = '#codeblock-' . count($codeblocks) . '#';
83
84                                 $prefix = '````' . $matches[1] . PHP_EOL;
85                                 $codeblocks[] = $prefix . trim($matches[2]) . PHP_EOL . '````';
86                         }
87                         return $return;
88                 }
89         , $Text);
90
91         // Convert it to HTML - don't try oembed
92         if ($fordiaspora) {
93                 $Text = BBCode::convert($Text, false, 3);
94
95                 // Add all tags that maybe were removed
96                 if (preg_match_all("/#\[url\=([$URLSearchString]*)\](.*?)\[\/url\]/ism", $OriginalText, $tags)) {
97                         $tagline = "";
98                         foreach ($tags[2] as $tag) {
99                                 $tag = html_entity_decode($tag, ENT_QUOTES, 'UTF-8');
100                                 if (!strpos(html_entity_decode($Text, ENT_QUOTES, 'UTF-8'), '#' . $tag)) {
101                                         $tagline .= '#' . $tag . ' ';
102                                 }
103                         }
104                         $Text = $Text." ".$tagline;
105                 }
106         } else {
107                 $Text = BBCode::convert($Text, false, 4);
108         }
109
110         // mask some special HTML chars from conversation to markdown
111         $Text = str_replace(['&lt;', '&gt;', '&amp;'], ['&_lt_;', '&_gt_;', '&_amp_;'], $Text);
112
113         // If a link is followed by a quote then there should be a newline before it
114         // Maybe we should make this newline at every time before a quote.
115         $Text = str_replace(["</a><blockquote>"], ["</a><br><blockquote>"], $Text);
116
117         $stamp1 = microtime(true);
118
119         // Now convert HTML to Markdown
120         $converter = new HtmlConverter();
121         $Text = $converter->convert($Text);
122
123         // unmask the special chars back to HTML
124         $Text = str_replace(['&\_lt\_;', '&\_gt\_;', '&\_amp\_;'], ['&lt;', '&gt;', '&amp;'], $Text);
125
126         $a->save_timestamp($stamp1, "parser");
127
128         // Libertree has a problem with escaped hashtags.
129         $Text = str_replace(['\#'], ['#'], $Text);
130
131         // Remove any leading or trailing whitespace, as this will mess up
132         // the Diaspora signature verification and cause the item to disappear
133         $Text = trim($Text);
134
135         if ($fordiaspora) {
136                 $URLSearchString = "^\[\]";
137                 $Text = preg_replace_callback("/([@]\[(.*?)\])\(([$URLSearchString]*?)\)/ism", 'diaspora_mentions', $Text);
138         }
139
140         // Restore code blocks
141         $Text = preg_replace_callback('/#codeblock-([0-9]+)#/iU',
142                 function ($matches) use ($codeblocks) {
143             $return = '';
144             if (isset($codeblocks[intval($matches[1])])) {
145                 $return = $codeblocks[$matches[1]];
146             }
147                         return $return;
148                 }
149         , $Text);
150
151         Addon::callHooks('bb2diaspora',$Text);
152
153         return $Text;
154 }
155
156 function unescape_underscores_in_links($m) {
157         $y = str_replace('\\_', '_', $m[2]);
158         return('[' . $m[1] . '](' . $y . ')');
159 }
160
161 function format_event_diaspora($ev) {
162         if (! ((is_array($ev)) && count($ev))) {
163                 return '';
164         }
165
166         $bd_format = L10n::t('l F d, Y \@ g:i A') ; // Friday January 18, 2011 @ 8 AM
167
168         $o = 'Friendica event notification:' . "\n";
169
170         $o .= '**' . (($ev['summary']) ? bb2diaspora($ev['summary']) : bb2diaspora($ev['desc'])) .  '**' . "\n";
171
172         // @todo What. Is. Going. On. With. This. Useless. Ternary. Operator? - mrpetovan
173         $o .= L10n::t('Starts:') . ' ' . '[' . day_translate(
174                         $ev['adjust'] ? DateTimeFormat::utc($ev['start'], $bd_format) : DateTimeFormat::utc($ev['start'], $bd_format)
175                 )
176                 .  '](' . System::baseUrl() . '/localtime/?f=&time=' . urlencode(DateTimeFormat::utc($ev['start'])) . ")\n";
177
178         if (! $ev['nofinish']) {
179                 $o .= L10n::t('Finishes:') . ' ' . '[' . day_translate(
180                                 $ev['adjust'] ? DateTimeFormat::utc($ev['finish'], $bd_format) : DateTimeFormat::utc($ev['finish'], $bd_format)
181                         )
182                         . '](' . System::baseUrl() . '/localtime/?f=&time=' . urlencode(DateTimeFormat::utc($ev['finish'])) . ")\n";
183         }
184
185         if (strlen($ev['location'])) {
186                 $o .= L10n::t('Location:') . bb2diaspora($ev['location'])
187                         . "\n";
188         }
189
190         $o .= "\n";
191         return $o;
192 }