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