3 * @copyright Copyright (C) 2010-2022, the Friendica project
5 * @license GNU AGPL version 3 or any later version
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU Affero General Public License as
9 * published by the Free Software Foundation, either version 3 of the
10 * License, or (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU Affero General Public License for more details.
17 * You should have received a copy of the GNU Affero General Public License
18 * along with this program. If not, see <https://www.gnu.org/licenses/>.
22 namespace Friendica\Content\Text;
26 use Friendica\Content\Widget\ContactBlock;
27 use Friendica\Core\Hook;
28 use Friendica\Core\Renderer;
29 use Friendica\Core\Search;
31 use Friendica\Model\Contact;
32 use Friendica\Util\Network;
33 use Friendica\Util\Strings;
34 use Friendica\Util\XML;
35 use League\HTMLToMarkdown\HtmlConverter;
40 * Search all instances of a specific HTML tag node in the provided DOM document and replaces them with BBCode text nodes.
42 * @see HTML::tagToBBCodeSub()
44 private static function tagToBBCode(DOMDocument $doc, string $tag, array $attributes, string $startbb, string $endbb, bool $ignoreChildren = false)
47 $done = self::tagToBBCodeSub($doc, $tag, $attributes, $startbb, $endbb, $ignoreChildren);
52 * Search the first specific HTML tag node in the provided DOM document and replaces it with BBCode text nodes.
54 * @param DOMDocument $doc
55 * @param string $tag HTML tag name
56 * @param array $attributes Array of attributes to match and optionally use the value from
57 * @param string $startbb BBCode tag opening
58 * @param string $endbb BBCode tag closing
59 * @param bool $ignoreChildren If set to false, the HTML tag children will be appended as text inside the BBCode tag
60 * Otherwise, they will be entirely ignored. Useful for simple BBCode that draw their
61 * inner value from an attribute value and disregard the tag children.
62 * @return bool Whether a replacement was done
64 private static function tagToBBCodeSub(DOMDocument $doc, string $tag, array $attributes, string $startbb, string $endbb, bool $ignoreChildren = false)
66 $savestart = str_replace('$', '\x01', $startbb);
69 $xpath = new DOMXPath($doc);
71 /** @var \DOMNode[] $list */
72 $list = $xpath->query("//" . $tag);
73 foreach ($list as $node) {
75 if ($node->attributes->length) {
76 foreach ($node->attributes as $attribute) {
77 $attr[$attribute->name] = $attribute->value;
83 $startbb = $savestart;
87 foreach ($attributes as $attribute => $value) {
88 $startbb = str_replace('\x01' . ++$i, '$1', $startbb);
89 if (strpos('*' . $startbb, '$1') > 0) {
90 if ($replace && (@$attr[$attribute] != '')) {
91 $startbb = preg_replace($value, $startbb, $attr[$attribute], -1, $count);
93 // If nothing could be changed
101 if (@$attr[$attribute] != $value) {
108 $StartCode = $doc->createTextNode($startbb);
109 $EndCode = $doc->createTextNode($endbb);
111 $node->parentNode->insertBefore($StartCode, $node);
113 if (!$ignoreChildren && $node->hasChildNodes()) {
114 /** @var \DOMNode $child */
115 foreach ($node->childNodes as $key => $child) {
116 /* Remove empty text nodes at the start or at the end of the children list */
117 if ($key > 0 && $key < $node->childNodes->length - 1 || $child->nodeName != '#text' || trim($child->nodeValue)) {
118 $newNode = $child->cloneNode(true);
119 $node->parentNode->insertBefore($newNode, $node);
124 $node->parentNode->insertBefore($EndCode, $node);
125 $node->parentNode->removeChild($node);
133 * Converter for HTML to BBCode
135 * Made by: ike@piratenpartei.de
136 * Originally made for the syncom project: http://wiki.piratenpartei.de/Syncom
137 * https://github.com/annando/Syncom
139 * @param string $message
140 * @param string $basepath
142 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
144 public static function toBBCode($message, $basepath = '')
146 DI::profiler()->startRecording('rendering');
147 $message = str_replace("\r", "", $message);
149 $message = Strings::performWithEscapedBlocks($message, '#<pre><code.*</code></pre>#iUs', function ($message) {
150 $message = str_replace(
163 $message = preg_replace('=<(\w+):(.+?)>=', '<removeme>', $message);
164 $message = preg_replace('=</(\w+):(.+?)>=', '</removeme>', $message);
166 $doc = new DOMDocument();
167 $doc->preserveWhiteSpace = false;
169 $message = mb_convert_encoding($message, 'HTML-ENTITIES', "UTF-8");
171 if (empty($message)) {
175 @$doc->loadHTML($message, LIBXML_HTML_NODEFDTD);
177 XML::deleteNode($doc, 'style');
178 XML::deleteNode($doc, 'head');
179 XML::deleteNode($doc, 'title');
180 XML::deleteNode($doc, 'meta');
181 XML::deleteNode($doc, 'xml');
182 XML::deleteNode($doc, 'removeme');
184 $xpath = new DomXPath($doc);
185 $list = $xpath->query("//pre");
186 foreach ($list as $node) {
187 // Ensure to escape unescaped & - they will otherwise raise a warning
188 $safe_value = preg_replace('/&(?!\w+;)/', '&', $node->nodeValue);
189 $node->nodeValue = str_replace("\n", "\r", $safe_value);
192 $message = $doc->saveHTML();
193 $message = str_replace(["\n<", ">\n", "\r", "\n", "\xC3\x82\xC2\xA0"], ["<", ">", "<br />", " ", ""], $message);
194 $message = preg_replace('= [\s]*=i', " ", $message);
196 if (empty($message)) {
200 @$doc->loadHTML($message, LIBXML_HTML_NODEFDTD);
202 self::tagToBBCode($doc, 'html', [], "", "");
203 self::tagToBBCode($doc, 'body', [], "", "");
205 // Outlook-Quote - Variant 1
206 self::tagToBBCode($doc, 'p', ['class' => 'MsoNormal', 'style' => 'margin-left:35.4pt'], '[quote]', '[/quote]');
208 // Outlook-Quote - Variant 2
212 ['style' => 'border:none;border-left:solid blue 1.5pt;padding:0cm 0cm 0cm 4.0pt'],
218 self::tagToBBCode($doc, 'span', ['style' => 'text-decoration: underline;'], '[u]', '[/u]');
219 self::tagToBBCode($doc, 'span', ['style' => 'font-style: italic;'], '[i]', '[/i]');
220 self::tagToBBCode($doc, 'span', ['style' => 'font-weight: bold;'], '[b]', '[/b]');
222 /* self::node2BBCode($doc, 'font', array('face'=>'/([\w ]+)/', 'size'=>'/(\d+)/', 'color'=>'/(.+)/'), '[font=$1][size=$2][color=$3]', '[/color][/size][/font]');
223 self::node2BBCode($doc, 'font', array('size'=>'/(\d+)/', 'color'=>'/(.+)/'), '[size=$1][color=$2]', '[/color][/size]');
224 self::node2BBCode($doc, 'font', array('face'=>'/([\w ]+)/', 'size'=>'/(.+)/'), '[font=$1][size=$2]', '[/size][/font]');
225 self::node2BBCode($doc, 'font', array('face'=>'/([\w ]+)/', 'color'=>'/(.+)/'), '[font=$1][color=$3]', '[/color][/font]');
226 self::node2BBCode($doc, 'font', array('face'=>'/([\w ]+)/'), '[font=$1]', '[/font]');
227 self::node2BBCode($doc, 'font', array('size'=>'/(\d+)/'), '[size=$1]', '[/size]');
228 self::node2BBCode($doc, 'font', array('color'=>'/(.+)/'), '[color=$1]', '[/color]');
231 //self::node2BBCode($doc, 'span', array('style'=>'/.*font-size:\s*(.+?)[,;].*font-family:\s*(.+?)[,;].*color:\s*(.+?)[,;].*/'), '[size=$1][font=$2][color=$3]', '[/color][/font][/size]');
232 //self::node2BBCode($doc, 'span', array('style'=>'/.*font-size:\s*(\d+)[,;].*/'), '[size=$1]', '[/size]');
233 //self::node2BBCode($doc, 'span', array('style'=>'/.*font-size:\s*(.+?)[,;].*/'), '[size=$1]', '[/size]');
235 self::tagToBBCode($doc, 'span', ['style' => '/.*color:\s*(.+?)[,;].*/'], '[color="$1"]', '[/color]');
237 //self::node2BBCode($doc, 'span', array('style'=>'/.*font-family:\s*(.+?)[,;].*/'), '[font=$1]', '[/font]');
238 //self::node2BBCode($doc, 'div', array('style'=>'/.*font-family:\s*(.+?)[,;].*font-size:\s*(\d+?)pt.*/'), '[font=$1][size=$2]', '[/size][/font]');
239 //self::node2BBCode($doc, 'div', array('style'=>'/.*font-family:\s*(.+?)[,;].*font-size:\s*(\d+?)px.*/'), '[font=$1][size=$2]', '[/size][/font]');
240 //self::node2BBCode($doc, 'div', array('style'=>'/.*font-family:\s*(.+?)[,;].*/'), '[font=$1]', '[/font]');
241 // Importing the classes - interesting for importing of posts from third party networks that were exported from friendica
243 //self::node2BBCode($doc, 'span', array('class'=>'/([\w ]+)/'), '[class=$1]', '[/class]');
244 self::tagToBBCode($doc, 'span', ['class' => 'type-link'], '[class=type-link]', '[/class]');
245 self::tagToBBCode($doc, 'span', ['class' => 'type-video'], '[class=type-video]', '[/class]');
247 self::tagToBBCode($doc, 'strong', [], '[b]', '[/b]');
248 self::tagToBBCode($doc, 'em', [], '[i]', '[/i]');
249 self::tagToBBCode($doc, 'b', [], '[b]', '[/b]');
250 self::tagToBBCode($doc, 'i', [], '[i]', '[/i]');
251 self::tagToBBCode($doc, 'u', [], '[u]', '[/u]');
252 self::tagToBBCode($doc, 's', [], '[s]', '[/s]');
253 self::tagToBBCode($doc, 'del', [], '[s]', '[/s]');
254 self::tagToBBCode($doc, 'strike', [], '[s]', '[/s]');
256 self::tagToBBCode($doc, 'big', [], "[size=large]", "[/size]");
257 self::tagToBBCode($doc, 'small', [], "[size=small]", "[/size]");
259 self::tagToBBCode($doc, 'blockquote', [], '[quote]', '[/quote]');
261 self::tagToBBCode($doc, 'br', [], "\n", '');
263 self::tagToBBCode($doc, 'p', ['class' => 'MsoNormal'], "\n", "");
264 self::tagToBBCode($doc, 'div', ['class' => 'MsoNormal'], "\r", "");
266 self::tagToBBCode($doc, 'span', [], "", "");
268 self::tagToBBCode($doc, 'span', [], "", "");
269 self::tagToBBCode($doc, 'pre', [], "", "");
271 self::tagToBBCode($doc, 'div', [], "\r", "\r");
272 self::tagToBBCode($doc, 'p', [], "\n", "\n");
274 self::tagToBBCode($doc, 'ul', [], "[list]", "[/list]");
275 self::tagToBBCode($doc, 'ol', [], "[list=1]", "[/list]");
276 self::tagToBBCode($doc, 'li', [], "[*]", "");
278 self::tagToBBCode($doc, 'hr', [], "[hr]", "");
280 self::tagToBBCode($doc, 'table', [], "[table]", "[/table]");
281 self::tagToBBCode($doc, 'th', [], "[th]", "[/th]");
282 self::tagToBBCode($doc, 'tr', [], "[tr]", "[/tr]");
283 self::tagToBBCode($doc, 'td', [], "[td]", "[/td]");
285 self::tagToBBCode($doc, 'h1', [], "[h1]", "[/h1]");
286 self::tagToBBCode($doc, 'h2', [], "[h2]", "[/h2]");
287 self::tagToBBCode($doc, 'h3', [], "[h3]", "[/h3]");
288 self::tagToBBCode($doc, 'h4', [], "[h4]", "[/h4]");
289 self::tagToBBCode($doc, 'h5', [], "[h5]", "[/h5]");
290 self::tagToBBCode($doc, 'h6', [], "[h6]", "[/h6]");
292 self::tagToBBCode($doc, 'a', ['href' => '/mailto:(.+)/'], '[mail=$1]', '[/mail]');
293 self::tagToBBCode($doc, 'a', ['href' => '/(.+)/'], '[url=$1]', '[/url]');
295 self::tagToBBCode($doc, 'img', ['src' => '/(.+)/', 'alt' => '/(.+)/'], '[img=$1]$2', '[/img]', true);
296 self::tagToBBCode($doc, 'img', ['src' => '/(.+)/', 'width' => '/(\d+)/', 'height' => '/(\d+)/'], '[img=$2x$3]$1', '[/img]', true);
297 self::tagToBBCode($doc, 'img', ['src' => '/(.+)/'], '[img]$1', '[/img]', true);
300 self::tagToBBCode($doc, 'video', ['src' => '/(.+)/'], '[video]$1', '[/video]', true);
301 self::tagToBBCode($doc, 'audio', ['src' => '/(.+)/'], '[audio]$1', '[/audio]', true);
302 // Backward compatibility, [iframe] support has been removed in version 2020.12
303 self::tagToBBCode($doc, 'iframe', ['src' => '/(.+)/'], '[url]$1', '[/url]', true);
305 self::tagToBBCode($doc, 'key', [], '[code]', '[/code]');
306 self::tagToBBCode($doc, 'code', [], '[code]', '[/code]');
308 $message = $doc->saveHTML();
310 // I'm removing something really disturbing
311 // Don't know exactly what it is
312 $message = str_replace(chr(194) . chr(160), ' ', $message);
314 $message = str_replace(" ", " ", $message);
316 // removing multiple DIVs
317 $message = preg_replace('=\r *\r=i', "\n", $message);
318 $message = str_replace("\r", "\n", $message);
320 Hook::callAll('html2bbcode', $message);
322 $message = strip_tags($message);
324 $message = html_entity_decode($message, ENT_QUOTES, 'UTF-8');
326 // remove quotes if they don't make sense
327 $message = preg_replace('=\[/quote\][\s]*\[quote\]=i', "\n", $message);
329 $message = preg_replace('=\[quote\]\s*=i', "[quote]", $message);
330 $message = preg_replace('=\s*\[/quote\]=i', "[/quote]", $message);
333 $oldmessage = $message;
334 $message = str_replace("\n \n", "\n\n", $message);
335 } while ($oldmessage != $message);
338 $oldmessage = $message;
339 $message = str_replace("\n\n\n", "\n\n", $message);
340 } while ($oldmessage != $message);
343 $oldmessage = $message;
344 $message = str_replace(
367 } while ($message != $oldmessage);
369 $message = str_replace(
370 ['[b][b]', '[/b][/b]', '[i][i]', '[/i][/i]'],
371 ['[b]', '[/b]', '[i]', '[/i]'],
375 // Handling Yahoo style of mails
376 $message = str_replace('[hr][b]From:[/b]', '[quote][b]From:[/b]', $message);
381 $message = preg_replace_callback(
382 '#<pre><code(?: class="language-([^"]*)")?>(.*)</code></pre>#iUs',
383 function ($matches) {
385 if ($matches[1] != '') {
386 $prefix = '[code=' . $matches[1] . ']';
389 return $prefix . "\n" . html_entity_decode($matches[2]) . "\n" . '[/code]';
394 $message = trim($message);
396 if ($basepath != '') {
397 $message = self::qualifyURLs($message, $basepath);
400 DI::profiler()->stopRecording();
405 * Sub function to complete incomplete URL
407 * @param array $matches Result of preg_replace_callback
408 * @param string $basepath Basepath that is used to complete the URL
410 * @return string The expanded URL
412 private static function qualifyURLsSub($matches, $basepath)
414 $base = parse_url($basepath);
415 unset($base['query']);
416 unset($base['fragment']);
421 if (empty($url) || empty(parse_url($url))) {
425 $parts = array_merge($base, parse_url($url));
426 $url2 = Network::unparseURL($parts);
428 return str_replace($url, $url2, $link);
432 * Complete incomplete URLs in BBCode
434 * @param string $body Body with URLs
435 * @param string $basepath Base path that is used to complete the URL
437 * @return string Body with expanded URLs
439 private static function qualifyURLs($body, $basepath)
441 $URLSearchString = "^\[\]";
443 $matches = ["/\[url\=([$URLSearchString]*)\].*?\[\/url\]/ism",
444 "/\[url\]([$URLSearchString]*)\[\/url\]/ism",
445 "/\[img\=[0-9]*x[0-9]*\](.*?)\[\/img\]/ism",
446 "/\[img\](.*?)\[\/img\]/ism",
447 "/\[zmg\=[0-9]*x[0-9]*\](.*?)\[\/img\]/ism",
448 "/\[zmg\](.*?)\[\/zmg\]/ism",
449 "/\[video\](.*?)\[\/video\]/ism",
450 "/\[audio\](.*?)\[\/audio\]/ism",
453 foreach ($matches as $match) {
454 $body = preg_replace_callback(
456 function ($match) use ($basepath) {
457 return self::qualifyURLsSub($match, $basepath);
465 private static function breakLines($line, $level, $wraplength = 75)
467 if ($wraplength == 0) {
468 $wraplength = 2000000;
471 $wraplen = $wraplength - $level;
478 $subline = substr($line, 0, $wraplen);
480 $pos = strrpos($subline, ' ');
483 $pos = strpos($line, ' ');
486 if (($pos > 0) && strlen($line) > $wraplen) {
487 $newline = trim(substr($line, 0, $pos));
489 $newline = str_repeat(">", $level) . ' ' . $newline;
492 $newlines[] = $newline . " ";
493 $line = substr($line, $pos + 1);
495 } while ((strlen($line) > $wraplen) && !($oldline == $line));
498 $line = str_repeat(">", $level) . ' ' . $line;
503 return implode("\n", $newlines);
506 private static function quoteLevel($message, $wraplength = 75)
508 $lines = explode("\n", $message);
512 foreach ($lines as $line) {
515 while (strpos("*" . $line, '[quote]') > 0) {
517 $pos = strpos($line, '[quote]');
518 $line = substr($line, 0, $pos) . substr($line, $pos + 7);
524 while (strpos("*" . $line, '[/quote]') > 0) {
530 $pos = strpos($line, '[/quote]');
531 $line = substr($line, 0, $pos) . substr($line, $pos + 8);
534 if (!$startquote || ($line != '')) {
535 $newlines[] = self::breakLines($line, $currlevel, $wraplength);
539 return implode("\n", $newlines);
542 private static function collectURLs($message)
544 $pattern = '/<a.*?href="(.*?)".*?>(.*?)<\/a>/is';
545 preg_match_all($pattern, $message, $result, PREG_SET_ORDER);
548 foreach ($result as $treffer) {
551 // A list of some links that should be ignored
552 $list = ["/user/", "/tag/", "/group/", "/profile/", "/search?search=", "/search?tag=", "mailto:", "/u/", "/node/",
553 "//plus.google.com/", "//twitter.com/"];
554 foreach ($list as $listitem) {
555 if (strpos($treffer[1], $listitem) !== false) {
560 if ((strpos($treffer[1], "//twitter.com/") !== false) && (strpos($treffer[1], "/status/") !== false)) {
564 if ((strpos($treffer[1], "//plus.google.com/") !== false) && (strpos($treffer[1], "/posts") !== false)) {
568 if ((strpos($treffer[1], "//plus.google.com/") !== false) && (strpos($treffer[1], "/photos") !== false)) {
572 $ignore = $ignore || strpos($treffer[1], '#') === 0;
575 $urls[$treffer[1]] = $treffer[1];
583 * @param string $html
584 * @param int $wraplength Ensures individual lines aren't longer than this many characters. Doesn't break words.
585 * @param bool $compact True: Completely strips image tags; False: Keeps image URLs
588 public static function toPlaintext(string $html, $wraplength = 75, $compact = false)
590 DI::profiler()->startRecording('rendering');
591 $message = str_replace("\r", "", $html);
593 $doc = new DOMDocument();
594 $doc->preserveWhiteSpace = false;
596 $message = mb_convert_encoding($message, 'HTML-ENTITIES', "UTF-8");
598 if (empty($message)) {
599 DI::profiler()->stopRecording();
603 @$doc->loadHTML($message, LIBXML_HTML_NODEFDTD);
605 $message = $doc->saveHTML();
606 // Remove eventual UTF-8 BOM
607 $message = str_replace("\xC3\x82\xC2\xA0", "", $message);
609 // Collecting all links
610 $urls = self::collectURLs($message);
612 if (empty($message)) {
613 DI::profiler()->stopRecording();
617 @$doc->loadHTML($message, LIBXML_HTML_NODEFDTD);
619 self::tagToBBCode($doc, 'html', [], '', '');
620 self::tagToBBCode($doc, 'body', [], '', '');
623 self::tagToBBCode($doc, 'blockquote', [], "»", "«");
625 self::tagToBBCode($doc, 'blockquote', [], '[quote]', "[/quote]\n");
628 self::tagToBBCode($doc, 'br', [], "\n", '');
630 self::tagToBBCode($doc, 'span', [], "", "");
631 self::tagToBBCode($doc, 'pre', [], "", "");
632 self::tagToBBCode($doc, 'div', [], "\r", "\r");
633 self::tagToBBCode($doc, 'p', [], "\n", "\n");
635 self::tagToBBCode($doc, 'li', [], "\n* ", "\n");
637 self::tagToBBCode($doc, 'hr', [], "\n" . str_repeat("-", 70) . "\n", "");
639 self::tagToBBCode($doc, 'tr', [], "\n", "");
640 self::tagToBBCode($doc, 'td', [], "\t", "");
642 self::tagToBBCode($doc, 'h1', [], "\n\n*", "*\n");
643 self::tagToBBCode($doc, 'h2', [], "\n\n*", "*\n");
644 self::tagToBBCode($doc, 'h3', [], "\n\n*", "*\n");
645 self::tagToBBCode($doc, 'h4', [], "\n\n*", "*\n");
646 self::tagToBBCode($doc, 'h5', [], "\n\n*", "*\n");
647 self::tagToBBCode($doc, 'h6', [], "\n\n*", "*\n");
650 self::tagToBBCode($doc, 'img', ['src' => '/(.+)/'], ' [img]$1', '[/img] ');
652 self::tagToBBCode($doc, 'img', ['src' => '/(.+)/'], ' ', ' ');
655 // Backward compatibility, [iframe] support has been removed in version 2020.12
656 self::tagToBBCode($doc, 'iframe', ['src' => '/(.+)/'], ' $1 ', '');
658 $message = $doc->saveHTML();
661 $message = str_replace("[img]", "", $message);
662 $message = str_replace("[/img]", "", $message);
665 // was ersetze ich da?
666 // Irgendein stoerrisches UTF-Zeug
667 $message = str_replace(chr(194) . chr(160), ' ', $message);
669 $message = str_replace(" ", " ", $message);
671 // Aufeinanderfolgende DIVs
672 $message = preg_replace('=\r *\r=i', "\n", $message);
673 $message = str_replace("\r", "\n", $message);
675 $message = strip_tags($message);
677 $message = html_entity_decode($message, ENT_QUOTES, 'UTF-8');
679 if (!$compact && ($message != '')) {
680 foreach ($urls as $id => $url) {
681 if ($url != '' && strpos($message, $url) === false) {
682 $message .= "\n" . $url . ' ';
687 $message = str_replace("\n«", "«\n", $message);
688 $message = str_replace("»\n", "\n»", $message);
691 $oldmessage = $message;
692 $message = str_replace("\n\n\n", "\n\n", $message);
693 } while ($oldmessage != $message);
695 $message = self::quoteLevel(trim($message), $wraplength);
697 DI::profiler()->stopRecording();
698 return trim($message);
702 * Converts provided HTML code to Markdown. The hardwrap parameter maximizes
703 * compatibility with Diaspora in spite of the Markdown standards.
705 * @param string $html
708 public static function toMarkdown($html)
710 DI::profiler()->startRecording('rendering');
711 $converter = new HtmlConverter(['hard_break' => true]);
712 $markdown = $converter->convert($html);
714 DI::profiler()->stopRecording();
719 * Convert video HTML to BBCode tags
724 public static function toBBCodeVideo($s)
727 '#<object[^>]+>(.*?)https?://www.youtube.com/((?:v|cp)/[A-Za-z0-9\-_=]+)(.*?)</object>#ism',
728 '[youtube]$2[/youtube]',
733 '#<iframe[^>](.*?)https?://www.youtube.com/embed/([A-Za-z0-9\-_=]+)(.*?)</iframe>#ism',
734 '[youtube]$2[/youtube]',
739 '#<iframe[^>](.*?)https?://player.vimeo.com/video/([0-9]+)(.*?)</iframe>#ism',
748 * transform link href and img src from relative to absolute
750 * @param string $text
751 * @param string $base base url
754 public static function relToAbs($text, $base)
760 $base = rtrim($base, '/');
762 $base2 = $base . "/";
765 $pattern = "/<a([^>]*) href=\"(?!http|https|\/)([^\"]*)\"/";
766 $replace = "<a\${1} href=\"" . $base2 . "\${2}\"";
767 $text = preg_replace($pattern, $replace, $text);
769 $pattern = "/<a([^>]*) href=\"(?!http|https)([^\"]*)\"/";
770 $replace = "<a\${1} href=\"" . $base . "\${2}\"";
771 $text = preg_replace($pattern, $replace, $text);
774 $pattern = "/<img([^>]*) src=\"(?!http|https|\/)([^\"]*)\"/";
775 $replace = "<img\${1} src=\"" . $base2 . "\${2}\"";
776 $text = preg_replace($pattern, $replace, $text);
778 $pattern = "/<img([^>]*) src=\"(?!http|https)([^\"]*)\"/";
779 $replace = "<img\${1} src=\"" . $base . "\${2}\"";
780 $text = preg_replace($pattern, $replace, $text);
788 * Loader for infinite scrolling
790 * @return string html for loader
791 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
793 public static function scrollLoader()
795 $tpl = Renderer::getMarkupTemplate("scroll_loader.tpl");
796 return Renderer::replaceMacros($tpl, [
797 'wait' => DI::l10n()->t('Loading more entries...'),
798 'end' => DI::l10n()->t('The end')
803 * Format contacts as picture links or as text links
805 * @param array $contact Array with contacts which contains an array with
806 * int 'id' => The ID of the contact
807 * int 'uid' => The user ID of the user who owns this data
808 * string 'name' => The name of the contact
809 * string 'url' => The url to the profile page of the contact
810 * string 'addr' => The webbie of the contact (e.g.) username@friendica.com
811 * string 'network' => The network to which the contact belongs to
812 * string 'thumb' => The contact picture
813 * string 'click' => js code which is performed when clicking on the contact
814 * @param boolean $redirect If true try to use the redir url if it's possible
815 * @param string $class CSS class for the
816 * @param boolean $textmode If true display the contacts as text links
817 * if false display the contacts as picture links
818 * @return string Formatted html
819 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
820 * @throws \ImagickException
822 public static function micropro($contact, $redirect = false, $class = '', $textmode = false)
824 // Use the contact URL if no address is available
825 if (empty($contact['addr'])) {
826 $contact["addr"] = $contact["url"];
829 $url = $contact['url'];
834 $url = Contact::magicLinkByContact($contact);
835 if (strpos($url, 'redir/') === 0) {
836 $sparkle = ' sparkle';
840 // If there is some js available we don't need the url
841 if (!empty($contact['click'])) {
845 return Renderer::replaceMacros(Renderer::getMarkupTemplate($textmode ? 'micropro_txt.tpl' : 'micropro_img.tpl'), [
846 '$click' => $contact['click'] ?? '',
849 '$photo' => Contact::getThumb($contact),
850 '$name' => $contact['name'],
851 'title' => $contact['name'] . ' [' . $contact['addr'] . ']',
852 '$parkle' => $sparkle,
860 * @param string $s Search query.
861 * @param string $id HTML id
862 * @param string $url Search url.
863 * @param bool $aside Display the search widgit aside.
865 * @return string Formatted HTML.
868 public static function search($s, $id = 'search-box', $aside = true)
872 if (strpos($s, '#') === 0) {
875 $save_label = $mode === 'text' ? DI::l10n()->t('Save') : DI::l10n()->t('Follow');
879 '$q' => urlencode($s),
881 '$search_label' => DI::l10n()->t('Search'),
882 '$save_label' => $save_label,
883 '$search_hint' => DI::l10n()->t('@name, !forum, #tags, content'),
885 '$return_url' => urlencode(Search::getSearchPath($s)),
889 $values['$search_options'] = [
890 'fulltext' => DI::l10n()->t('Full Text'),
891 'tags' => DI::l10n()->t('Tags'),
892 'contacts' => DI::l10n()->t('Contacts')
895 if (DI::config()->get('system', 'poco_local_search')) {
896 $values['$searchoption']['forums'] = DI::l10n()->t('Forums');
900 return Renderer::replaceMacros(Renderer::getMarkupTemplate('searchbox.tpl'), $values);
904 * Replace naked text hyperlink with HTML formatted hyperlink
909 public static function toLink($s)
911 $s = preg_replace("/(https?\:\/\/[a-zA-Z0-9\:\/\-\?\&\;\.\=\_\~\#\'\%\$\!\+]*)/", ' <a href="$1" target="_blank" rel="noopener noreferrer">$1</a>', $s);
912 $s = preg_replace("/\<(.*?)(src|href)=(.*?)\&\;(.*?)\>/ism", '<$1$2=$3&$4>', $s);
917 * Given a HTML text and a set of filtering reasons, adds a content hiding header with the provided reasons
919 * Reasons are expected to have been translated already.
921 * @param string $html
922 * @param array $reasons
924 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
926 public static function applyContentFilter($html, array $reasons)
928 if (count($reasons)) {
929 $tpl = Renderer::getMarkupTemplate('wall/content_filter.tpl');
930 $html = Renderer::replaceMacros($tpl, [
931 '$reasons' => $reasons,
932 '$rnd' => Strings::getRandomHex(8),
933 '$openclose' => DI::l10n()->t('Click to open/close'),
942 * replace html amp entity with amp char
946 public static function unamp($s)
948 return str_replace('&', '&', $s);
952 * Clean an HTML text for potentially harmful code
954 * @param string $text
955 * @param array $allowedIframeDomains List of allowed iframe source domains without the scheme
958 public static function purify(string $text, array $allowedIframeDomains = []): string
960 // Allows cid: URL scheme
961 \HTMLPurifier_URISchemeRegistry::instance()->register('cid', new HTMLPurifier_URIScheme_cid());
963 $config = \HTMLPurifier_HTML5Config::createDefault();
964 $config->set('HTML.Doctype', 'HTML5');
966 // Used to remove iframe with src attribute filtered out
967 $config->set('AutoFormat.RemoveEmpty', true);
969 $config->set('HTML.SafeIframe', true);
971 array_walk($allowedIframeDomains, function (&$domain) {
972 // Allow the domain and all its eventual sub-domains
973 $domain = '(?:(?!-)[A-Za-z0-9-]{1,63}(?<!-)\.)*' . preg_quote(trim($domain, '/'), '%');
976 $config->set('URI.SafeIframeRegexp',
978 ' . implode('|', $allowedIframeDomains) . '
980 (?:/|$) # Prevents bogus domains like youtube.com.fake.tld
984 $config->set('Attr.AllowedRel', [
985 'noreferrer' => true,
989 $config->set('Attr.AllowedFrameTargets', [
993 $config->set('AutoFormat.RemoveEmpty.Predicate', [
994 'colgroup' => [], // |
997 'iframe' => ['src'], // ↳ Default HTMLPurify values
998 'i' => ['class'], // Allows forkawesome icons
1001 // Uncomment to debug HTMLPurifier behavior
1002 //$config->set('Core.CollectErrors', true);
1003 //$config->set('Core.MaintainLineNumbers', true);
1005 $HTMLPurifier = new \HTMLPurifier($config);
1007 $text = $HTMLPurifier->purify($text);
1009 /** @var \HTMLPurifier_ErrorCollector $errorCollector */
1010 // Uncomment to debug HTML Purifier behavior
1011 //$errorCollector = $HTMLPurifier->context->get('ErrorCollector');
1012 //var_dump($errorCollector->getRaw());