3 require_once("include/oembed.php");
4 require_once('include/event.php');
6 require_once('library/markdown.php');
7 require_once('include/html2bbcode.php');
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.
14 function diaspora2bb($s) {
16 // for testing purposes: Collect raw markdown articles
17 // $file = tempnam("/tmp/friendica/", "markdown");
18 // file_put_contents($file, $s);
20 $s = html_entity_decode($s,ENT_COMPAT,'UTF-8');
22 // Too many new lines. So deactivated the following line
23 // $s = str_replace("\r","\n",$s);
25 $s = str_replace("\r","",$s);
27 // <br/> is invalid. Replace it with the valid expression
28 $s = str_replace("<br/>","<br />",$s);
30 $s = preg_replace('/\@\{(.+?)\; (.+?)\@(.+?)\}/','@[url=https://$3/u/$2]$1[/url]',$s);
32 // Escaping the hash tags - doesn't always seem to work
33 // $s = preg_replace('/\#([^\s\#])/','\\#$1',$s);
35 $s = preg_replace('/\#([^\s\#])/','#$1',$s);
39 $s = str_replace('#','#',$s);
41 $s = str_replace("\n",'<br />',$s);
44 // $s = str_replace('*','*',$s);
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);
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);
57 // Don't show link to full picture (until it is fixed)
58 $s = scale_external_images($s, false);
64 function stripdcode_br_cb($s) {
65 return '[code]' . str_replace('<br />', "\n\t", $s[1]) . '[/code]';
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]);
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:
82 return preg_replace("/\[\\\\\*\]( *)/", "1. ", $s[1]);
86 function bb2diaspora($Text,$preserve_nl = false) {
88 $ev = bbtoevent($Text);
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
93 $Text = str_replace("<", "<", $Text);
94 $Text = str_replace(">", ">", $Text);
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.
102 $Text = str_replace(array("\n","\r"), array('',''),$Text);
104 // Remove the "return" character, as Diaspora uses only the "newline"
105 // character, so having the "return" character can cause signature
107 $Text = str_replace("\r", "", $Text);
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;
114 // Perform URL Search
116 // [img]pathtoimage[/img]
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
122 $Text = preg_replace("/\[url\=([$URLSearchString]*)\]\[img\](.*?)\[\/img\]\[\/url\]/ism",
123 '![' . t('image/photo') . '](' . '$2' . ')' . "\n" . '[' . t('link') . '](' . '$1' . ')', $Text);
125 $Text = preg_replace("/\[bookmark\]([$URLSearchString]*)\[\/bookmark\]/ism", '[$1]($1)', $Text);
126 $Text = preg_replace("/\[bookmark\=([$URLSearchString]*)\](.*?)\[\/bookmark\]/ism", '[$2]($1)', $Text);
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);
133 $Text = preg_replace("/\[img\](.*?)\[\/img\]/", '![' . t('image/photo') . '](' . '$1' . ')', $Text);
134 $Text = preg_replace("/\[img\=(.*?)\](.*?)\[\/img\]/", '![' . t('image/photo') . '](' . '$2' . ')', $Text);
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);
140 $Text = str_replace('*', '\\*', $Text);
141 $Text = str_replace('_', '\\_', $Text);
143 $Text = str_replace('`','\\`', $Text);
145 // Check for bold text
146 $Text = preg_replace("(\[b\](.*?)\[\/b\])is",'**$1**',$Text);
148 // Check for italics text
149 $Text = preg_replace("(\[i\](.*?)\[\/i\])is",'_$1_',$Text);
151 // Check for underline text
152 // Replace with italics since Diaspora doesn't have underline
153 $Text = preg_replace("(\[u\](.*?)\[\/u\])is",'_$1_',$Text);
155 // Check for strike-through text
156 $Text = preg_replace("(\[s\](.*?)\[\/s\])is",'**[strike]**$1**[/strike]**',$Text);
158 // Check for over-line text
159 // $Text = preg_replace("(\[o\](.*?)\[\/o\])is",'<span class="overline">$1</span>',$Text);
161 // Check for colored text
162 // Remove color since Diaspora doesn't support it
163 $Text = preg_replace("(\[color=(.*?)\](.*?)\[\/color\])is","$2",$Text);
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);
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);
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);
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);
190 // $Text = str_replace("[*]", "<li>", $Text);
192 // Check for font change text
193 // $Text = preg_replace("(\[font=(.*?)\](.*?)\[\/font\])","<span style=\"font-family: $1;\">$2</span>",$Text);
196 $Text = preg_replace_callback("/\[code\](.*?)\[\/code\]/is",'stripdcode_br_cb',$Text);
198 // Check for [code] text
199 $Text = preg_replace("/(\[code\])+(.*?)(\[\/code\])+/is","\t$2\n", $Text);
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);
212 // html5 video and audio
214 $Text = preg_replace("/\[video\](.*?)\[\/video\]/", '$1', $Text);
216 $Text = preg_replace("/\[audio\](.*?)\[\/audio\]/", '$1', $Text);
218 // $Text = preg_replace("/\[iframe\](.*?)\[\/iframe\]/", '<iframe src="$1" width="425" height="350"><a href="$1">$1</a></iframe>', $Text);
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);
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);
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);
233 $Text = str_replace('[nosmile]','',$Text);
236 // $Text = oembed_bbcode2html($Text);
238 // If we found an event earlier, strip out all the event code and replace with a reformatted version.
240 if(x($ev,'desc') && x($ev,'start')) {
242 $sub = format_event_diaspora($ev);
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);
251 $Text = preg_replace("/\<(.*?)(src|href)=(.*?)\&\;(.*?)\>/ism",'<$1$2=$3&$4>',$Text);
253 $Text = preg_replace_callback('/\[(.*?)\]\((.*?)\)/ism','unescape_underscores_in_links',$Text);
255 // Remove any leading or trailing whitespace, as this will mess up
256 // the Diaspora signature verification and cause the item to disappear
259 call_hooks('bb2diaspora',$Text);
264 function unescape_underscores_in_links($m) {
265 $y = str_replace('\\_','_', $m[2]);
266 return('[' . $m[1] . '](' . $y . ')');
269 function format_event_diaspora($ev) {
273 if(! ((is_array($ev)) && count($ev)))
276 $bd_format = t('l F d, Y \@ g:i A') ; // Friday January 18, 2011 @ 8 AM
278 $o = 'Friendica event notification:' . "\n";
280 $o .= '**' . bb2diaspora($ev['desc']) . '**' . "\n";
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";
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";
297 if(strlen($ev['location']))
298 $o .= t('Location:') . bb2diaspora($ev['location'])