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