]> git.mxchange.org Git - friendica.git/blob - include/bb2diaspora.php
Merge pull request #350 from fermionic/improve-bb2diaspora-conversions
[friendica.git] / include / bb2diaspora.php
1 <?php
2
3 require_once("include/oembed.php");
4 require_once('include/event.php');
5
6 require_once('library/markdown.php');
7 require_once('include/html2bbcode.php');
8
9 // we don't want to support a bbcode specific markdown interpreter
10 // and the markdown library we have is pretty good, but provides HTML output.
11 // So we'll use that to convert to HTML, then convert the HTML back to bbcode,
12 // and then clean up a few Diaspora specific constructs.
13
14 function diaspora2bb($s) {
15
16         // for testing purposes: Collect raw markdown articles
17         // $file = tempnam("/tmp/friendica/", "markdown");
18         // file_put_contents($file, $s);
19
20         $s = html_entity_decode($s,ENT_COMPAT,'UTF-8');
21
22         // Too many new lines. So deactivated the following line
23         // $s = str_replace("\r","\n",$s);
24         // Simply remove cr.
25         $s = str_replace("\r","",$s);
26
27         // <br/> is invalid. Replace it with the valid expression
28         $s = str_replace("<br/>","<br />",$s);
29
30         $s = preg_replace('/\@\{(.+?)\; (.+?)\@(.+?)\}/','@[url=https://$3/u/$2]$1[/url]',$s);
31
32         // Escaping the hash tags - doesn't always seem to work
33         // $s = preg_replace('/\#([^\s\#])/','\\#$1',$s);
34         // This seems to work
35         $s = preg_replace('/\#([^\s\#])/','&#35;$1',$s);
36
37         $s = Markdown($s);
38
39         $s = str_replace('&#35;','#',$s);
40
41         $s = str_replace("\n",'<br />',$s);
42
43         $s = html2bbcode($s);
44 //      $s = str_replace('&#42;','*',$s);
45
46         // Convert everything that looks like a link to a link
47         $s = preg_replace("/([^\]\=]|^)(https?\:\/\/)([a-zA-Z0-9\:\/\-\?\&\;\.\=\_\~\#\%\$\!\+\,]+)/ism", '$1[url=$2$3]$2$3[/url]',$s);
48
49         //$s = preg_replace("/([^\]\=]|^)(https?\:\/\/)(vimeo|youtu|www\.youtube|soundcloud)([a-zA-Z0-9\:\/\-\?\&\;\.\=\_\~\#\%\$\!\+\,]+)/ism", '$1[url=$2$3$4]$2$3$4[/url]',$s);
50         $s = preg_replace("/\[url\=?(.*?)\]https?:\/\/www.youtube.com\/watch\?v\=(.*?)\[\/url\]/ism",'[youtube]$2[/youtube]',$s);
51         $s = preg_replace("/\[url\=https?:\/\/www.youtube.com\/watch\?v\=(.*?)\].*?\[\/url\]/ism",'[youtube]$1[/youtube]',$s);
52         $s = preg_replace("/\[url\=?(.*?)\]https?:\/\/vimeo.com\/([0-9]+)(.*?)\[\/url\]/ism",'[vimeo]$2[/vimeo]',$s);
53         $s = preg_replace("/\[url\=https?:\/\/vimeo.com\/([0-9]+)\](.*?)\[\/url\]/ism",'[vimeo]$1[/vimeo]',$s);
54         // remove duplicate adjacent code tags
55         $s = preg_replace("/(\[code\])+(.*?)(\[\/code\])+/ism","[code]$2[/code]", $s);
56
57         // Don't show link to full picture (until it is fixed)
58         $s = scale_external_images($s, false);
59
60         return $s;
61 }
62
63
64 function stripdcode_br_cb($s) {
65         return '[code]' . str_replace('<br />', "\n\t", $s[1]) . '[/code]';
66 }
67
68
69 function diaspora_ul($s) {
70         // Replace "[\\*]" followed by any number (including zero) of
71         // spaces by "* " to match Diaspora's list format
72         return preg_replace("/\[\\\\\*\]( *)/", "* ", $s[1]);
73 }
74
75
76 function diaspora_ol($s) {
77         // A hack: Diaspora will create a properly-numbered ordered list even
78         // if you use '1.' for each element of the list, like:
79         //              1. First element
80         //              1. Second element
81         //              1. Third element
82         return preg_replace("/\[\\\\\*\]( *)/", "1. ", $s[1]);
83 }
84
85
86 function bb2diaspora($Text,$preserve_nl = false) {
87
88         $ev = bbtoevent($Text);
89
90         // Replace any html brackets with HTML Entities to prevent executing HTML or script
91         // Don't use strip_tags here because it breaks [url] search by replacing & with amp
92
93         $Text = str_replace("<", "&lt;", $Text);
94         $Text = str_replace(">", "&gt;", $Text);
95
96         // If we find any event code, turn it into an event.
97         // After we're finished processing the bbcode we'll 
98         // replace all of the event code with a reformatted version.
99
100
101         if($preserve_nl)
102                 $Text = str_replace(array("\n","\r"), array('',''),$Text);
103         else
104                 // Remove the "return" character, as Diaspora uses only the "newline"
105                 // character, so having the "return" character can cause signature
106                 // failures
107                 $Text = str_replace("\r", "", $Text);
108
109         // Set up the parameters for a URL search string
110         $URLSearchString = "^\[\]";
111         // Set up the parameters for a MAIL search string
112         $MAILSearchString = $URLSearchString;
113
114         // Perform URL Search
115
116         // [img]pathtoimage[/img]
117
118         // the following was added on 10-January-2012 due to an inability of Diaspora's
119         // new javascript markdown processor to handle links with images as the link "text"
120         // It is not optimal and may be removed if this ability is restored in the future
121
122         $Text = preg_replace("/\[url\=([$URLSearchString]*)\]\[img\](.*?)\[\/img\]\[\/url\]/ism", 
123                 '![' . t('image/photo') . '](' . '$2' . ')' . "\n" . '[' . t('link') . '](' . '$1' . ')', $Text);
124
125         $Text = preg_replace("/\[bookmark\]([$URLSearchString]*)\[\/bookmark\]/ism", '[$1]($1)', $Text);
126         $Text = preg_replace("/\[bookmark\=([$URLSearchString]*)\](.*?)\[\/bookmark\]/ism", '[$2]($1)', $Text);
127
128         $Text = preg_replace("/\[url\]([$URLSearchString]*)\[\/url\]/ism", '[$1]($1)', $Text);
129         $Text = preg_replace("/\#\[url\=([$URLSearchString]*)\](.*?)\[\/url\]/ism", '[#$2]($1)', $Text);
130         $Text = preg_replace("/\[url\=([$URLSearchString]*)\](.*?)\[\/url\]/ism", '[$2]($1)', $Text);
131
132
133         $Text = preg_replace("/\[img\](.*?)\[\/img\]/", '![' . t('image/photo') . '](' . '$1' . ')', $Text);
134         $Text = preg_replace("/\[img\=(.*?)\](.*?)\[\/img\]/", '![' . t('image/photo') . '](' . '$2' . ')', $Text);
135
136         // Perform MAIL Search
137         $Text = preg_replace("(\[mail\]([$MAILSearchString]*)\[/mail\])", '[$1](mailto:$1)', $Text);
138         $Text = preg_replace("/\[mail\=([$MAILSearchString]*)\](.*?)\[\/mail\]/", '[$2](mailto:$1)', $Text);
139          
140         $Text = str_replace('*', '\\*', $Text);
141         $Text = str_replace('_', '\\_', $Text);
142
143         $Text = str_replace('`','\\`', $Text);
144
145         // Check for bold text
146         $Text = preg_replace("(\[b\](.*?)\[\/b\])is",'**$1**',$Text);
147
148         // Check for italics text
149         $Text = preg_replace("(\[i\](.*?)\[\/i\])is",'_$1_',$Text);
150
151         // Check for underline text
152         // Replace with italics since Diaspora doesn't have underline
153         $Text = preg_replace("(\[u\](.*?)\[\/u\])is",'_$1_',$Text);
154
155         // Check for strike-through text
156         $Text = preg_replace("(\[s\](.*?)\[\/s\])is",'**[strike]**$1**[/strike]**',$Text);
157
158         // Check for over-line text
159 //      $Text = preg_replace("(\[o\](.*?)\[\/o\])is",'<span class="overline">$1</span>',$Text);
160
161         // Check for colored text
162         // Remove color since Diaspora doesn't support it
163         $Text = preg_replace("(\[color=(.*?)\](.*?)\[\/color\])is","$2",$Text);
164
165         // Check for sized text
166         // Remove it since Diaspora doesn't support sizes very well
167         $Text = preg_replace("(\[size=(.*?)\](.*?)\[\/size\])is","$2",$Text);
168
169         // Check for list text
170         $Text = preg_replace_callback("/\[list\](.*?)\[\/list\]/is", 'diaspora_ul', $Text);
171         $Text = preg_replace_callback("/\[ul\](.*?)\[\/ul\]/is", 'diaspora_ul', $Text);
172         $Text = preg_replace_callback("/\[list=1\](.*?)\[\/list\]/is", 'diaspora_ol', $Text);
173         $Text = preg_replace_callback("/\[list=i\](.*?)\[\/list\]/s",'diaspora_ol', $Text);
174         $Text = preg_replace_callback("/\[list=I\](.*?)\[\/list\]/s", 'diaspora_ol', $Text);
175         $Text = preg_replace_callback("/\[list=a\](.*?)\[\/list\]/s", 'diaspora_ol', $Text);
176         $Text = preg_replace_callback("/\[list=A\](.*?)\[\/list\]/s", 'diaspora_ol', $Text);
177         $Text = preg_replace_callback("/\[ol\](.*?)\[\/ol\]/is", 'diaspora_ol', $Text);
178 //      $Text = preg_replace("/\[li\](.*?)\[\/li\]/s", '<li>$1</li>' ,$Text);
179
180         // Just get rid of table tags since Diaspora doesn't support tables
181         $Text = preg_replace("/\[th\](.*?)\[\/th\]/s", '$1' ,$Text);
182         $Text = preg_replace("/\[td\](.*?)\[\/td\]/s", '$1' ,$Text);
183         $Text = preg_replace("/\[tr\](.*?)\[\/tr\]/s", '$1' ,$Text);
184         $Text = preg_replace("/\[table\](.*?)\[\/table\]/s", '$1' ,$Text);
185
186         $Text = preg_replace("/\[table border=(.*?)\](.*?)\[\/table\]/s", '$2' ,$Text);
187 //      $Text = preg_replace("/\[table border=0\](.*?)\[\/table\]/s", '<table border="0" >$1</table>' ,$Text);
188
189         
190 //      $Text = str_replace("[*]", "<li>", $Text);
191
192         // Check for font change text
193 //      $Text = preg_replace("(\[font=(.*?)\](.*?)\[\/font\])","<span style=\"font-family: $1;\">$2</span>",$Text);
194
195
196         $Text = preg_replace_callback("/\[code\](.*?)\[\/code\]/is",'stripdcode_br_cb',$Text);
197
198         // Check for [code] text
199         $Text = preg_replace("/(\[code\])+(.*?)(\[\/code\])+/is","\t$2\n", $Text);
200
201
202
203
204         // Declare the format for [quote] layout
205         //      $QuoteLayout = '<blockquote>$1</blockquote>';                     
206         // Check for [quote] text
207         $Text = preg_replace("/\[quote\](.*?)\[\/quote\]/is",">$1\n\n", $Text);
208         $Text = preg_replace("/\[quote=(.*?)\](.*?)\[\/quote\]/is",">$2\n\n", $Text);
209          
210         // Images
211
212         // html5 video and audio
213
214         $Text = preg_replace("/\[video\](.*?)\[\/video\]/", '$1', $Text);
215
216         $Text = preg_replace("/\[audio\](.*?)\[\/audio\]/", '$1', $Text);
217
218 //      $Text = preg_replace("/\[iframe\](.*?)\[\/iframe\]/", '<iframe src="$1" width="425" height="350"><a href="$1">$1</a></iframe>', $Text);
219          
220         // [img=widthxheight]image source[/img]
221 //      $Text = preg_replace("/\[img\=([0-9]*)x([0-9]*)\](.*?)\[\/img\]/", '<img src="$3" style="height:{$2}px; width:{$1}px;" >', $Text);
222
223         $Text = preg_replace("/\[youtube\]https?:\/\/www.youtube.com\/watch\?v\=(.*?)\[\/youtube\]/ism",'http://www.youtube.com/watch?v=$1',$Text); 
224         $Text = preg_replace("/\[youtube\]https?:\/\/www.youtube.com\/embed\/(.*?)\[\/youtube\]/ism",'http://www.youtube.com/watch?v=$1',$Text); 
225         $Text = preg_replace("/\[youtube\]https?:\/\/youtu.be\/(.*?)\[\/youtube\]/ism",'http://www.youtube.com/watch?v=$1',$Text); 
226         $Text = preg_replace("/\[youtube\]([A-Za-z0-9\-_=]+)(.*?)\[\/youtube\]/ism", 'http://www.youtube.com/watch?v=$1', $Text);
227
228         $Text = preg_replace("/\[vimeo\]https?:\/\/player.vimeo.com\/video\/([0-9]+)(.*?)\[\/vimeo\]/ism",'http://vimeo.com/$1',$Text); 
229         $Text = preg_replace("/\[vimeo\]https?:\/\/vimeo.com\/([0-9]+)(.*?)\[\/vimeo\]/ism",'http://vimeo.com/$1',$Text); 
230         $Text = preg_replace("/\[vimeo\]([0-9]+)(.*?)\[\/vimeo\]/ism", 'http://vimeo.com/$1',$Text);
231
232
233         $Text = str_replace('[nosmile]','',$Text);
234
235         // oembed tag
236         //      $Text = oembed_bbcode2html($Text);
237
238         // If we found an event earlier, strip out all the event code and replace with a reformatted version.
239
240         if(x($ev,'desc') && x($ev,'start')) {
241
242                 $sub = format_event_diaspora($ev);
243         
244                 $Text = preg_replace("/\[event\-description\](.*?)\[\/event\-description\]/is",$sub,$Text);
245                 $Text = preg_replace("/\[event\-start\](.*?)\[\/event\-start\]/is",'',$Text);
246                 $Text = preg_replace("/\[event\-finish\](.*?)\[\/event\-finish\]/is",'',$Text);
247                 $Text = preg_replace("/\[event\-location\](.*?)\[\/event\-location\]/is",'',$Text);
248                 $Text = preg_replace("/\[event\-adjust\](.*?)\[\/event\-adjust\]/is",'',$Text);
249         }
250
251         $Text = preg_replace("/\<(.*?)(src|href)=(.*?)\&amp\;(.*?)\>/ism",'<$1$2=$3&$4>',$Text);
252
253         $Text = preg_replace_callback('/\[(.*?)\]\((.*?)\)/ism','unescape_underscores_in_links',$Text);
254
255         // Remove any leading or trailing whitespace, as this will mess up
256         // the Diaspora signature verification and cause the item to disappear
257         $Text = trim($Text);
258         
259         call_hooks('bb2diaspora',$Text);
260
261         return $Text;
262 }
263
264 function unescape_underscores_in_links($m) {
265         $y = str_replace('\\_','_', $m[2]);
266         return('[' . $m[1] . '](' . $y . ')');
267 }
268
269 function format_event_diaspora($ev) {
270
271         $a = get_app();
272
273         if(! ((is_array($ev)) && count($ev)))
274                 return '';
275
276         $bd_format = t('l F d, Y \@ g:i A') ; // Friday January 18, 2011 @ 8 AM
277
278         $o = 'Friendica event notification:' . "\n";
279
280         $o .= '**' . bb2diaspora($ev['desc']) .  '**' . "\n";
281
282         $o .= t('Starts:') . ' ' . '['
283                 . (($ev['adjust']) ? day_translate(datetime_convert('UTC', 'UTC', 
284                         $ev['start'] , $bd_format ))
285                         :  day_translate(datetime_convert('UTC', 'UTC', 
286                         $ev['start'] , $bd_format)))
287                 .  '](' . $a->get_baseurl() . '/localtime/?f=&time=' . urlencode(datetime_convert('UTC','UTC',$ev['start'])) . ")\n";
288
289         if(! $ev['nofinish'])
290                 $o .= t('Finishes:') . ' ' . '[' 
291                         . (($ev['adjust']) ? day_translate(datetime_convert('UTC', 'UTC', 
292                                 $ev['finish'] , $bd_format ))
293                                 :  day_translate(datetime_convert('UTC', 'UTC', 
294                                 $ev['finish'] , $bd_format )))
295                         . '](' . $a->get_baseurl() . '/localtime/?f=&time=' . urlencode(datetime_convert('UTC','UTC',$ev['finish'])) . ")\n";
296
297         if(strlen($ev['location']))
298                 $o .= t('Location:') . bb2diaspora($ev['location']) 
299                         . "\n";
300
301         $o .= "\n";
302         return $o;
303 }