]> git.mxchange.org Git - friendica.git/blob - include/bb2diaspora.php
Merge pull request #1033 from annando/master
[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("include/markdownify/markdownify.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         // Simply remove cr.
21         $s = str_replace("\r","",$s);
22
23         // <br/> is invalid. Replace it with the valid expression
24         $s = str_replace(array("<br/>", "</p>", "<p>"),array("<br />", "<br />", "<br />"),$s);
25
26         $s = preg_replace('/\@\{(.+?)\; (.+?)\@(.+?)\}/','@[url=https://$3/u/$2]$1[/url]',$s);
27
28         // Escaping the hash tags
29         $s = preg_replace('/\#([^\s\#])/','&#35;$1',$s);
30
31         $s = Markdown($s);
32
33         $s = str_replace('&#35;','#',$s);
34
35         $s = html2bbcode($s);
36
37         // protect the recycle symbol from turning into a tag, but without unescaping angles and naked ampersands
38         $s = str_replace('&#x2672;',html_entity_decode('&#x2672;',ENT_QUOTES,'UTF-8'),$s);
39
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);
42
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);
50
51         // Don't show link to full picture (until it is fixed)
52         $s = scale_external_images($s, false);
53
54         return $s;
55 }
56
57 function bb2diaspora($Text,$preserve_nl = false, $fordiaspora = true) {
58
59         // Since Diaspora is creating a summary for links, this function removes them before posting
60         if ($fordiaspora)
61                 $Text = bb_remove_share_information($Text);
62
63         /**
64          * Transform #tags, strip off the [url] and replace spaces with underscore
65          */
66         $URLSearchString = "^\[\]";
67         $Text = preg_replace_callback("/#\[url\=([$URLSearchString]*)\](.*?)\[\/url\]/i", create_function('$match',
68                 'return \'#\'. str_replace(\' \', \'_\', $match[2]);'
69         ), $Text);
70
71
72         // Converting images with size parameters to simple images. Markdown doesn't know it.
73         $Text = preg_replace("/\[img\=([0-9]*)x([0-9]*)\](.*?)\[\/img\]/ism", '[img]$3[/img]', $Text);
74
75         // Convert it to HTML - don't try oembed
76         if ($fordiaspora)
77                 $Text = bbcode($Text, $preserve_nl, false, 3);
78         else {
79                 $Text = bbcode($Text, $preserve_nl, false, 4);
80                 // Libertree doesn't convert a harizontal rule if there isn't a linefeed
81                 $Text = str_replace("<hr />", "<br /><hr />", $Text);
82         }
83
84         // Now convert HTML to Markdown
85         $md = new Markdownify(false, false, false);
86         $Text = $md->parseString($Text);
87
88         // The Markdownify converter converts underscores '_' in URLs to '\_', which
89         // messes up the URL. Manually fix these
90         $count = 1;
91         $pos = bb_find_open_close($Text, '[', ']', $count);
92         while($pos !== false) {
93                 $start = substr($Text, 0, $pos['start']);
94                 $subject = substr($Text, $pos['start'], $pos['end'] - $pos['start'] + 1);
95                 $end = substr($Text, $pos['end'] + 1);
96
97                 $subject = str_replace('\_', '_', $subject);
98                 $Text = $start . $subject . $end;
99
100                 $count++;
101                 $pos = bb_find_open_close($Text, '[', ']', $count);
102         }
103
104         // If the text going into bbcode() has a plain URL in it, i.e.
105         // with no [url] tags around it, it will come out of parseString()
106         // looking like: <http://url.com>, which gets removed by strip_tags().
107         // So take off the angle brackets of any such URL
108         $Text = preg_replace("/<http(.*?)>/is", "http$1", $Text);
109
110         // Remove all unconverted tags
111         $Text = strip_tags($Text);
112
113         // Remove any leading or trailing whitespace, as this will mess up
114         // the Diaspora signature verification and cause the item to disappear
115         $Text = trim($Text);
116
117         call_hooks('bb2diaspora',$Text);
118
119         return $Text;
120 }
121
122 function unescape_underscores_in_links($m) {
123         $y = str_replace('\\_','_', $m[2]);
124         return('[' . $m[1] . '](' . $y . ')');
125 }
126
127 function format_event_diaspora($ev) {
128
129         $a = get_app();
130
131         if(! ((is_array($ev)) && count($ev)))
132                 return '';
133
134         $bd_format = t('l F d, Y \@ g:i A') ; // Friday January 18, 2011 @ 8 AM
135
136         $o = 'Friendica event notification:' . "\n";
137
138         $o .= '**' . (($ev['summary']) ? bb2diaspora($ev['summary']) : bb2diaspora($ev['desc'])) .  '**' . "\n";
139
140         $o .= t('Starts:') . ' ' . '['
141                 . (($ev['adjust']) ? day_translate(datetime_convert('UTC', 'UTC', 
142                         $ev['start'] , $bd_format ))
143                         :  day_translate(datetime_convert('UTC', 'UTC', 
144                         $ev['start'] , $bd_format)))
145                 .  '](' . $a->get_baseurl() . '/localtime/?f=&time=' . urlencode(datetime_convert('UTC','UTC',$ev['start'])) . ")\n";
146
147         if(! $ev['nofinish'])
148                 $o .= t('Finishes:') . ' ' . '[' 
149                         . (($ev['adjust']) ? day_translate(datetime_convert('UTC', 'UTC', 
150                                 $ev['finish'] , $bd_format ))
151                                 :  day_translate(datetime_convert('UTC', 'UTC', 
152                                 $ev['finish'] , $bd_format )))
153                         . '](' . $a->get_baseurl() . '/localtime/?f=&time=' . urlencode(datetime_convert('UTC','UTC',$ev['finish'])) . ")\n";
154
155         if(strlen($ev['location']))
156                 $o .= t('Location:') . bb2diaspora($ev['location']) 
157                         . "\n";
158
159         $o .= "\n";
160         return $o;
161 }