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("include/markdownify/markdownify.php");
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.
16 function diaspora2bb($s) {
18 $s = html_entity_decode($s,ENT_COMPAT,'UTF-8');
21 $s = str_replace("\r","",$s);
23 // <br/> is invalid. Replace it with the valid expression
24 $s = str_replace(array("<br/>", "</p>", "<p>", '<p dir="ltr">'),array("<br />", "<br />", "<br />", "<br />"),$s);
26 $s = preg_replace('/\@\{(.+?)\; (.+?)\@(.+?)\}/','@[url=https://$3/u/$2]$1[/url]',$s);
28 // Escaping the hash tags
29 $s = preg_replace('/\#([^\s\#])/','#$1',$s);
33 $s = str_replace('#','#',$s);
37 // protect the recycle symbol from turning into a tag, but without unescaping angles and naked ampersands
38 $s = str_replace('♲',html_entity_decode('♲',ENT_QUOTES,'UTF-8'),$s);
40 // Convert everything that looks like a link to a link
41 $s = preg_replace("/([^\]\=]|^)(https?\:\/\/)([a-zA-Z0-9\:\/\-\?\&\;\.\=\_\~\#\%\$\!\+\,]+)/ism", '$1[url=$2$3]$2$3[/url]',$s);
43 //$s = preg_replace("/([^\]\=]|^)(https?\:\/\/)(vimeo|youtu|www\.youtube|soundcloud)([a-zA-Z0-9\:\/\-\?\&\;\.\=\_\~\#\%\$\!\+\,]+)/ism", '$1[url=$2$3$4]$2$3$4[/url]',$s);
44 $s = bb_tag_preg_replace("/\[url\=?(.*?)\]https?:\/\/www.youtube.com\/watch\?v\=(.*?)\[\/url\]/ism",'[youtube]$2[/youtube]','url',$s);
45 $s = bb_tag_preg_replace("/\[url\=https?:\/\/www.youtube.com\/watch\?v\=(.*?)\].*?\[\/url\]/ism",'[youtube]$1[/youtube]','url',$s);
46 $s = bb_tag_preg_replace("/\[url\=?(.*?)\]https?:\/\/vimeo.com\/([0-9]+)(.*?)\[\/url\]/ism",'[vimeo]$2[/vimeo]','url',$s);
47 $s = bb_tag_preg_replace("/\[url\=https?:\/\/vimeo.com\/([0-9]+)\](.*?)\[\/url\]/ism",'[vimeo]$1[/vimeo]','url',$s);
48 // remove duplicate adjacent code tags
49 $s = preg_replace("/(\[code\])+(.*?)(\[\/code\])+/ism","[code]$2[/code]", $s);
51 // Don't show link to full picture (until it is fixed)
52 $s = scale_external_images($s, false);
57 function bb2diaspora($Text,$preserve_nl = false, $fordiaspora = true) {
59 $OriginalText = $Text;
61 // Since Diaspora is creating a summary for links, this function removes them before posting
63 $Text = bb_remove_share_information($Text);
66 * Transform #tags, strip off the [url] and replace spaces with underscore
68 $URLSearchString = "^\[\]";
69 $Text = preg_replace_callback("/#\[url\=([$URLSearchString]*)\](.*?)\[\/url\]/i", create_function('$match',
70 'return \'#\'. str_replace(\' \', \'_\', $match[2]);'
74 // Converting images with size parameters to simple images. Markdown doesn't know it.
75 $Text = preg_replace("/\[img\=([0-9]*)x([0-9]*)\](.*?)\[\/img\]/ism", '[img]$3[/img]', $Text);
77 // Convert it to HTML - don't try oembed
79 $Text = bbcode($Text, $preserve_nl, false, 3);
81 // Add all tags that maybe were removed
82 if (preg_match_all("/#\[url\=([$URLSearchString]*)\](.*?)\[\/url\]/ism",$OriginalText, $tags)) {
84 foreach($tags[2] as $tag)
85 if (!strpos($Text, "#".$tag))
86 $tagline .= "#".$tag." ";
88 $Text = $Text."<br />".$tagline;
92 $Text = bbcode($Text, $preserve_nl, false, 4);
94 // Libertree doesn't convert a harizontal rule if there isn't a linefeed
95 $Text = str_replace(array("<hr />", "<hr>"), array("<br /><hr />", "<br><hr>"), $Text);
98 // Now convert HTML to Markdown
99 $md = new Markdownify(false, false, false);
100 $Text = $md->parseString($Text);
102 // The Markdownify converter converts underscores '_' in URLs to '\_', which
103 // messes up the URL. Manually fix these
105 $pos = bb_find_open_close($Text, '[', ']', $count);
106 while($pos !== false) {
107 $start = substr($Text, 0, $pos['start']);
108 $subject = substr($Text, $pos['start'], $pos['end'] - $pos['start'] + 1);
109 $end = substr($Text, $pos['end'] + 1);
111 $subject = str_replace('\_', '_', $subject);
112 $Text = $start . $subject . $end;
115 $pos = bb_find_open_close($Text, '[', ']', $count);
118 // If the text going into bbcode() has a plain URL in it, i.e.
119 // with no [url] tags around it, it will come out of parseString()
120 // looking like: <http://url.com>, which gets removed by strip_tags().
121 // So take off the angle brackets of any such URL
122 $Text = preg_replace("/<http(.*?)>/is", "http$1", $Text);
124 // Remove all unconverted tags
125 $Text = strip_tags($Text);
127 // Remove any leading or trailing whitespace, as this will mess up
128 // the Diaspora signature verification and cause the item to disappear
131 call_hooks('bb2diaspora',$Text);
136 function unescape_underscores_in_links($m) {
137 $y = str_replace('\\_','_', $m[2]);
138 return('[' . $m[1] . '](' . $y . ')');
141 function format_event_diaspora($ev) {
145 if(! ((is_array($ev)) && count($ev)))
148 $bd_format = t('l F d, Y \@ g:i A') ; // Friday January 18, 2011 @ 8 AM
150 $o = 'Friendica event notification:' . "\n";
152 $o .= '**' . (($ev['summary']) ? bb2diaspora($ev['summary']) : bb2diaspora($ev['desc'])) . '**' . "\n";
154 $o .= t('Starts:') . ' ' . '['
155 . (($ev['adjust']) ? day_translate(datetime_convert('UTC', 'UTC',
156 $ev['start'] , $bd_format ))
157 : day_translate(datetime_convert('UTC', 'UTC',
158 $ev['start'] , $bd_format)))
159 . '](' . $a->get_baseurl() . '/localtime/?f=&time=' . urlencode(datetime_convert('UTC','UTC',$ev['start'])) . ")\n";
161 if(! $ev['nofinish'])
162 $o .= t('Finishes:') . ' ' . '['
163 . (($ev['adjust']) ? day_translate(datetime_convert('UTC', 'UTC',
164 $ev['finish'] , $bd_format ))
165 : day_translate(datetime_convert('UTC', 'UTC',
166 $ev['finish'] , $bd_format )))
167 . '](' . $a->get_baseurl() . '/localtime/?f=&time=' . urlencode(datetime_convert('UTC','UTC',$ev['finish'])) . ")\n";
169 if(strlen($ev['location']))
170 $o .= t('Location:') . bb2diaspora($ev['location'])