]> git.mxchange.org Git - friendica.git/blob - src/Content/Text/Markdown.php
Merge pull request #7274 from MrPetovan/bug/7271-allow-brackets-in-urls
[friendica.git] / src / Content / Text / Markdown.php
1 <?php
2
3 /**
4  * @file src/Content/Text/Markdown.php
5  */
6
7 namespace Friendica\Content\Text;
8
9 use Friendica\BaseObject;
10 use Friendica\Core\System;
11 use Friendica\Model\Contact;
12
13 /**
14  * Friendica-specific usage of Markdown
15  *
16  * @author Hypolite Petovan <hypolite@mrpetovan.com>
17  */
18 class Markdown extends BaseObject
19 {
20         /**
21          * Converts a Markdown string into HTML. The hardwrap parameter maximizes
22          * compatibility with Diaspora in spite of the Markdown standard.
23          *
24          * @brief Converts a Markdown string into HTML
25          * @param string $text
26          * @param bool   $hardwrap
27          * @return string
28          * @throws \Exception
29          */
30         public static function convert($text, $hardwrap = true) {
31                 $stamp1 = microtime(true);
32
33                 $MarkdownParser = new MarkdownParser();
34                 $MarkdownParser->code_class_prefix  = 'language-';
35                 $MarkdownParser->hard_wrap          = $hardwrap;
36                 $MarkdownParser->hashtag_protection = true;
37                 $MarkdownParser->url_filter_func    = function ($url) {
38                         if (strpos($url, '#') === 0) {
39                                 $url = ltrim($_SERVER['REQUEST_URI'], '/') . $url;
40                         }
41                         return  $url;
42                 };
43
44                 $html = $MarkdownParser->transform($text);
45
46                 self::getApp()->getProfiler()->saveTimestamp($stamp1, "parser", System::callstack());
47
48                 return $html;
49         }
50
51         /**
52          * @brief Callback function to replace a Diaspora style mention in a mention for Friendica
53          *
54          * @param array $match Matching values for the callback
55          *                     [1] = mention type (@ or !)
56          *                     [2] = name (optional)
57          *                     [3] = address
58          * @return string Replaced mention
59          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
60          * @throws \ImagickException
61          */
62         private static function diasporaMention2BBCodeCallback($match)
63         {
64                 if ($match[3] == '') {
65                         return;
66                 }
67
68                 $data = Contact::getDetailsByAddr($match[3]);
69
70                 if (empty($data)) {
71                         return;
72                 }
73
74                 $name = $match[2];
75
76                 if ($name == '') {
77                         $name = $data['name'];
78                 }
79
80                 return $match[1] . '[url=' . $data['url'] . ']' . $name . '[/url]';
81         }
82
83         /*
84          * we don't want to support a bbcode specific markdown interpreter
85          * and the markdown library we have is pretty good, but provides HTML output.
86          * So we'll use that to convert to HTML, then convert the HTML back to bbcode,
87          * and then clean up a few Diaspora specific constructs.
88          */
89         public static function toBBCode($s)
90         {
91                 $s = html_entity_decode($s, ENT_COMPAT, 'UTF-8');
92
93                 // The parser cannot handle paragraphs correctly
94                 $s = str_replace(['</p>', '<p>', '<p dir="ltr">'], ['<br>', '<br>', '<br>'], $s);
95
96                 // Escaping hashtags that could be titles
97                 $s = preg_replace('/^\#([^\s\#])/im', '\#$1', $s);
98
99                 $s = self::convert($s);
100
101                 $regexp = "/([@!])\{(?:([^\}]+?); ?)?([^\} ]+)\}/";
102                 $s = preg_replace_callback($regexp, ['self', 'diasporaMention2BBCodeCallback'], $s);
103
104                 $s = HTML::toBBCode($s);
105
106                 // protect the recycle symbol from turning into a tag, but without unescaping angles and naked ampersands
107                 $s = str_replace('&#x2672;', html_entity_decode('&#x2672;', ENT_QUOTES, 'UTF-8'), $s);
108
109                 // Convert everything that looks like a link to a link
110                 $s = preg_replace('/([^\]=]|^)(https?\:\/\/)([a-zA-Z0-9:\/\-?&;.=_~#%$!+,@]+(?<!,))/ism', '$1[url=$2$3]$2$3[/url]', $s);
111
112                 //$s = preg_replace("/([^\]\=]|^)(https?\:\/\/)(vimeo|youtu|www\.youtube|soundcloud)([a-zA-Z0-9\:\/\-\?\&\;\.\=\_\~\#\%\$\!\+\,]+)/ism", '$1[url=$2$3$4]$2$3$4[/url]',$s);
113                 $s = BBCode::pregReplaceInTag('/\[url\=?(.*?)\]https?:\/\/www.youtube.com\/watch\?v\=(.*?)\[\/url\]/ism', '[youtube]$2[/youtube]', 'url', $s);
114                 $s = BBCode::pregReplaceInTag('/\[url\=https?:\/\/www.youtube.com\/watch\?v\=(.*?)\].*?\[\/url\]/ism'   , '[youtube]$1[/youtube]', 'url', $s);
115                 $s = BBCode::pregReplaceInTag('/\[url\=?(.*?)\]https?:\/\/vimeo.com\/([0-9]+)(.*?)\[\/url\]/ism'        , '[vimeo]$2[/vimeo]'    , 'url', $s);
116                 $s = BBCode::pregReplaceInTag('/\[url\=https?:\/\/vimeo.com\/([0-9]+)\](.*?)\[\/url\]/ism'              , '[vimeo]$1[/vimeo]'    , 'url', $s);
117
118                 // remove duplicate adjacent code tags
119                 $s = preg_replace('/(\[code\])+(.*?)(\[\/code\])+/ism', '[code]$2[/code]', $s);
120
121                 // Don't show link to full picture (until it is fixed)
122                 $s = BBCode::scaleExternalImages($s, false);
123
124                 return $s;
125         }
126 }