]> git.mxchange.org Git - friendica.git/blob - include/bb2diaspora.php
e818acb83e37c712ed53d41dcf4e91c01652203d
[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         // Simply remove cr.
21         //$s = str_replace("\r","",$s);
22 /*
23         // The parser has problems with unbalanced HTML elements
24         $doc = new DOMDocument();
25         $doc->preserveWhiteSpace = false;
26         $s = mb_convert_encoding($s, 'HTML-ENTITIES', "UTF-8");
27         $doctype = '<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">';
28         $encoding = '<?xml encoding="UTF-8">';
29         @$doc->loadHTML($encoding.$doctype."<html><body>".$s."</body></html>");
30         $doc->encoding = 'UTF-8';
31         $s = $doc->saveHTML();
32         $s = str_replace(array("<html><body>", "</body></html>", $doctype, $encoding), array("", "", "", ""), $s);
33 */
34
35         // The parser has problems with unbalanced html elements
36         $s = str_replace(array("<br/>", "</p>", "<p>", '<p dir="ltr">'),array("<br />", "<br />", "<br />", "<br />"),$s);
37
38         // Escaping the hash tags
39         $s = preg_replace('/\#([^\s\#])/','&#35;$1',$s);
40
41         $s = Markdown($s);
42
43         $s = preg_replace('/\@\{(.+?)\; (.+?)\@(.+?)\}/','@[url=https://$3/u/$2]$1[/url]',$s);
44
45         $s = str_replace('&#35;','#',$s);
46
47         $s = html2bbcode($s);
48
49         // protect the recycle symbol from turning into a tag, but without unescaping angles and naked ampersands
50         $s = str_replace('&#x2672;',html_entity_decode('&#x2672;',ENT_QUOTES,'UTF-8'),$s);
51
52         // Convert everything that looks like a link to a link
53         $s = preg_replace("/([^\]\=]|^)(https?\:\/\/)([a-zA-Z0-9\:\/\-\?\&\;\.\=\_\~\#\%\$\!\+\,]+)/ism", '$1[url=$2$3]$2$3[/url]',$s);
54
55         //$s = preg_replace("/([^\]\=]|^)(https?\:\/\/)(vimeo|youtu|www\.youtube|soundcloud)([a-zA-Z0-9\:\/\-\?\&\;\.\=\_\~\#\%\$\!\+\,]+)/ism", '$1[url=$2$3$4]$2$3$4[/url]',$s);
56         $s = bb_tag_preg_replace("/\[url\=?(.*?)\]https?:\/\/www.youtube.com\/watch\?v\=(.*?)\[\/url\]/ism",'[youtube]$2[/youtube]','url',$s);
57         $s = bb_tag_preg_replace("/\[url\=https?:\/\/www.youtube.com\/watch\?v\=(.*?)\].*?\[\/url\]/ism",'[youtube]$1[/youtube]','url',$s);
58         $s = bb_tag_preg_replace("/\[url\=?(.*?)\]https?:\/\/vimeo.com\/([0-9]+)(.*?)\[\/url\]/ism",'[vimeo]$2[/vimeo]','url',$s);
59         $s = bb_tag_preg_replace("/\[url\=https?:\/\/vimeo.com\/([0-9]+)\](.*?)\[\/url\]/ism",'[vimeo]$1[/vimeo]','url',$s);
60         // remove duplicate adjacent code tags
61         $s = preg_replace("/(\[code\])+(.*?)(\[\/code\])+/ism","[code]$2[/code]", $s);
62
63         // Don't show link to full picture (until it is fixed)
64         $s = scale_external_images($s, false);
65
66         return $s;
67 }
68
69 function bb2diaspora($Text,$preserve_nl = false, $fordiaspora = true) {
70
71         $a = get_app();
72
73         $OriginalText = $Text;
74
75         // Since Diaspora is creating a summary for links, this function removes them before posting
76         if ($fordiaspora)
77                 $Text = bb_remove_share_information($Text);
78
79         /**
80          * Transform #tags, strip off the [url] and replace spaces with underscore
81          */
82         $URLSearchString = "^\[\]";
83         $Text = preg_replace_callback("/#\[url\=([$URLSearchString]*)\](.*?)\[\/url\]/i", create_function('$match',
84                 'return \'#\'. str_replace(\' \', \'_\', $match[2]);'
85         ), $Text);
86
87
88         // Converting images with size parameters to simple images. Markdown doesn't know it.
89         $Text = preg_replace("/\[img\=([0-9]*)x([0-9]*)\](.*?)\[\/img\]/ism", '[img]$3[/img]', $Text);
90
91         // Convert it to HTML - don't try oembed
92         if ($fordiaspora) {
93                 $Text = bbcode($Text, $preserve_nl, false, 3);
94
95                 // Add all tags that maybe were removed
96                 if (preg_match_all("/#\[url\=([$URLSearchString]*)\](.*?)\[\/url\]/ism",$OriginalText, $tags)) {
97                         $tagline = "";
98                         foreach($tags[2] as $tag)
99                                 if (!strpos($Text, "#".$tag))
100                                         $tagline .= "#".$tag." ";
101
102                         $Text = $Text."<br />".$tagline;
103                 }
104
105         } else
106                 $Text = bbcode($Text, $preserve_nl, false, 4);
107
108         // If a link is followed by a quote then there should be a newline before it
109         // Maybe we should make this newline at every time before a quote.
110         $Text = str_replace(array("</a><blockquote>"), array("</a><br><blockquote>"), $Text);
111
112         $stamp1 = microtime(true);
113
114         // Now convert HTML to Markdown
115         $Text = new HTML_To_Markdown($Text);
116
117         $a->save_timestamp($stamp1, "parser");
118
119         // Libertree has a problem with escaped hashtags - Diaspora doesn't seem to.
120         if (!$fordiaspora)
121                 $Text = str_replace(array('\#'), array('#'), $Text);
122
123         // Remove any leading or trailing whitespace, as this will mess up
124         // the Diaspora signature verification and cause the item to disappear
125         $Text = trim($Text);
126
127         call_hooks('bb2diaspora',$Text);
128
129         return $Text;
130 }
131
132 function unescape_underscores_in_links($m) {
133         $y = str_replace('\\_','_', $m[2]);
134         return('[' . $m[1] . '](' . $y . ')');
135 }
136
137 function format_event_diaspora($ev) {
138
139         $a = get_app();
140
141         if(! ((is_array($ev)) && count($ev)))
142                 return '';
143
144         $bd_format = t('l F d, Y \@ g:i A') ; // Friday January 18, 2011 @ 8 AM
145
146         $o = 'Friendica event notification:' . "\n";
147
148         $o .= '**' . (($ev['summary']) ? bb2diaspora($ev['summary']) : bb2diaspora($ev['desc'])) .  '**' . "\n";
149
150         $o .= t('Starts:') . ' ' . '['
151                 . (($ev['adjust']) ? day_translate(datetime_convert('UTC', 'UTC', 
152                         $ev['start'] , $bd_format ))
153                         :  day_translate(datetime_convert('UTC', 'UTC', 
154                         $ev['start'] , $bd_format)))
155                 .  '](' . $a->get_baseurl() . '/localtime/?f=&time=' . urlencode(datetime_convert('UTC','UTC',$ev['start'])) . ")\n";
156
157         if(! $ev['nofinish'])
158                 $o .= t('Finishes:') . ' ' . '[' 
159                         . (($ev['adjust']) ? day_translate(datetime_convert('UTC', 'UTC', 
160                                 $ev['finish'] , $bd_format ))
161                                 :  day_translate(datetime_convert('UTC', 'UTC', 
162                                 $ev['finish'] , $bd_format )))
163                         . '](' . $a->get_baseurl() . '/localtime/?f=&time=' . urlencode(datetime_convert('UTC','UTC',$ev['finish'])) . ")\n";
164
165         if(strlen($ev['location']))
166                 $o .= t('Location:') . bb2diaspora($ev['location']) 
167                         . "\n";
168
169         $o .= "\n";
170         return $o;
171 }