3 * @copyright Copyright (C) 2010-2023, 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\Protocol\HTTP\MediaType;
27 use Friendica\Content\Widget\ContactBlock;
28 use Friendica\Core\Hook;
29 use Friendica\Core\Renderer;
30 use Friendica\Core\Search;
32 use Friendica\Model\Contact;
33 use Friendica\Util\Network;
34 use Friendica\Util\Strings;
35 use Friendica\Util\XML;
36 use League\HTMLToMarkdown\HtmlConverter;
37 use Psr\Http\Message\UriInterface;
42 * Search all instances of a specific HTML tag node in the provided DOM document and replaces them with BBCode text nodes.
44 * @see HTML::tagToBBCodeSub()
46 private static function tagToBBCode(DOMDocument $doc, string $tag, array $attributes, string $startbb, string $endbb, bool $ignoreChildren = false)
49 $done = self::tagToBBCodeSub($doc, $tag, $attributes, $startbb, $endbb, $ignoreChildren);
54 * Search the first specific HTML tag node in the provided DOM document and replaces it with BBCode text nodes.
56 * @param DOMDocument $doc
57 * @param string $tag HTML tag name
58 * @param array $attributes Array of attributes to match and optionally use the value from
59 * @param string $startbb BBCode tag opening
60 * @param string $endbb BBCode tag closing
61 * @param bool $ignoreChildren If set to false, the HTML tag children will be appended as text inside the BBCode tag
62 * Otherwise, they will be entirely ignored. Useful for simple BBCode that draw their
63 * inner value from an attribute value and disregard the tag children.
64 * @return bool Whether a replacement was done
66 private static function tagToBBCodeSub(DOMDocument $doc, string $tag, array $attributes, string $startbb, string $endbb, bool $ignoreChildren = false): bool
68 $savestart = str_replace('$', '\x01', $startbb);
71 $xpath = new DOMXPath($doc);
73 /** @var \DOMNode[] $list */
74 $list = $xpath->query("//" . $tag);
75 foreach ($list as $node) {
77 if ($node->attributes->length) {
78 foreach ($node->attributes as $attribute) {
79 $attr[$attribute->name] = $attribute->value;
85 $startbb = $savestart;
89 foreach ($attributes as $attribute => $value) {
90 $startbb = str_replace('\x01' . ++$i, '$1', $startbb);
91 if (strpos('*' . $startbb, '$1') > 0) {
92 if ($replace && (@$attr[$attribute] != '')) {
93 $startbb = preg_replace($value, $startbb, $attr[$attribute], -1, $count);
95 // If nothing could be changed
103 if (@$attr[$attribute] != $value) {
110 $StartCode = $doc->createTextNode($startbb);
111 $EndCode = $doc->createTextNode($endbb);
113 $node->parentNode->insertBefore($StartCode, $node);
115 if (!$ignoreChildren && $node->hasChildNodes()) {
116 /** @var \DOMNode $child */
117 foreach ($node->childNodes as $key => $child) {
118 /* Remove empty text nodes at the start or at the end of the children list */
119 if ($key > 0 && $key < $node->childNodes->length - 1 || $child->nodeName != '#text' || trim($child->nodeValue) !== '') {
120 $newNode = $child->cloneNode(true);
121 $node->parentNode->insertBefore($newNode, $node);
126 $node->parentNode->insertBefore($EndCode, $node);
127 $node->parentNode->removeChild($node);
135 * Converter for HTML to BBCode
137 * Made by: ike@piratenpartei.de
138 * Originally made for the syncom project: http://wiki.piratenpartei.de/Syncom
139 * https://github.com/annando/Syncom
141 * @param string $message
142 * @param string $basepath
144 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
146 public static function toBBCode(string $message, string $basepath = ''): string
149 * Check if message is empty to prevent a lot of code below from being executed
150 * for just an empty message.
152 if ($message === '') {
156 DI::profiler()->startRecording('rendering');
157 $message = str_replace("\r", "", $message);
159 $message = Strings::performWithEscapedBlocks($message, '#<pre><code.*</code></pre>#iUs', function ($message) {
160 $message = str_replace(
173 $message = preg_replace('=<(\w+):(.+?)>=', '<removeme>', $message);
174 $message = preg_replace('=</(\w+):(.+?)>=', '</removeme>', $message);
176 $doc = new DOMDocument();
177 $doc->preserveWhiteSpace = false;
179 $message = mb_convert_encoding($message, 'HTML-ENTITIES', "UTF-8");
181 if (empty($message)) {
185 @$doc->loadHTML($message, LIBXML_HTML_NODEFDTD);
187 XML::deleteNode($doc, 'style');
188 XML::deleteNode($doc, 'head');
189 XML::deleteNode($doc, 'title');
190 XML::deleteNode($doc, 'meta');
191 XML::deleteNode($doc, 'xml');
192 XML::deleteNode($doc, 'removeme');
194 $xpath = new DomXPath($doc);
195 $list = $xpath->query("//pre");
196 foreach ($list as $node) {
197 // Ensure to escape unescaped & - they will otherwise raise a warning
198 $safe_value = preg_replace('/&(?!\w+;)/', '&', $node->nodeValue);
199 $node->nodeValue = str_replace("\n", "\r", $safe_value);
202 $message = $doc->saveHTML();
203 $message = str_replace(["\n<", ">\n", "\r", "\n", "\xC3\x82\xC2\xA0"], ["<", ">", "<br />", " ", ""], $message);
204 $message = preg_replace('= [\s]*=i', " ", $message);
206 if (empty($message)) {
210 @$doc->loadHTML($message, LIBXML_HTML_NODEFDTD);
212 self::tagToBBCode($doc, 'html', [], "", "");
213 self::tagToBBCode($doc, 'body', [], "", "");
215 // Outlook-Quote - Variant 1
216 self::tagToBBCode($doc, 'p', ['class' => 'MsoNormal', 'style' => 'margin-left:35.4pt'], '[quote]', '[/quote]');
218 // Outlook-Quote - Variant 2
222 ['style' => 'border:none;border-left:solid blue 1.5pt;padding:0cm 0cm 0cm 4.0pt'],
228 self::tagToBBCode($doc, 'span', ['style' => 'text-decoration: underline;'], '[u]', '[/u]');
229 self::tagToBBCode($doc, 'span', ['style' => 'font-style: italic;'], '[i]', '[/i]');
230 self::tagToBBCode($doc, 'span', ['style' => 'font-weight: bold;'], '[b]', '[/b]');
232 /* self::node2BBCode($doc, 'font', array('face'=>'/([\w ]+)/', 'size'=>'/(\d+)/', 'color'=>'/(.+)/'), '[font=$1][size=$2][color=$3]', '[/color][/size][/font]');
233 self::node2BBCode($doc, 'font', array('size'=>'/(\d+)/', 'color'=>'/(.+)/'), '[size=$1][color=$2]', '[/color][/size]');
234 self::node2BBCode($doc, 'font', array('face'=>'/([\w ]+)/', 'size'=>'/(.+)/'), '[font=$1][size=$2]', '[/size][/font]');
235 self::node2BBCode($doc, 'font', array('face'=>'/([\w ]+)/', 'color'=>'/(.+)/'), '[font=$1][color=$3]', '[/color][/font]');
236 self::node2BBCode($doc, 'font', array('face'=>'/([\w ]+)/'), '[font=$1]', '[/font]');
237 self::node2BBCode($doc, 'font', array('size'=>'/(\d+)/'), '[size=$1]', '[/size]');
238 self::node2BBCode($doc, 'font', array('color'=>'/(.+)/'), '[color=$1]', '[/color]');
241 //self::node2BBCode($doc, 'span', array('style'=>'/.*font-size:\s*(.+?)[,;].*font-family:\s*(.+?)[,;].*color:\s*(.+?)[,;].*/'), '[size=$1][font=$2][color=$3]', '[/color][/font][/size]');
242 //self::node2BBCode($doc, 'span', array('style'=>'/.*font-size:\s*(\d+)[,;].*/'), '[size=$1]', '[/size]');
243 //self::node2BBCode($doc, 'span', array('style'=>'/.*font-size:\s*(.+?)[,;].*/'), '[size=$1]', '[/size]');
245 self::tagToBBCode($doc, 'span', ['style' => '/.*color:\s*(.+?)[,;].*/'], '[color="$1"]', '[/color]');
247 //self::node2BBCode($doc, 'span', array('style'=>'/.*font-family:\s*(.+?)[,;].*/'), '[font=$1]', '[/font]');
248 //self::node2BBCode($doc, 'div', array('style'=>'/.*font-family:\s*(.+?)[,;].*font-size:\s*(\d+?)pt.*/'), '[font=$1][size=$2]', '[/size][/font]');
249 //self::node2BBCode($doc, 'div', array('style'=>'/.*font-family:\s*(.+?)[,;].*font-size:\s*(\d+?)px.*/'), '[font=$1][size=$2]', '[/size][/font]');
250 //self::node2BBCode($doc, 'div', array('style'=>'/.*font-family:\s*(.+?)[,;].*/'), '[font=$1]', '[/font]');
251 // Importing the classes - interesting for importing of posts from third party networks that were exported from friendica
253 //self::node2BBCode($doc, 'span', array('class'=>'/([\w ]+)/'), '[class=$1]', '[/class]');
254 self::tagToBBCode($doc, 'span', ['class' => 'type-link'], '[class=type-link]', '[/class]');
255 self::tagToBBCode($doc, 'span', ['class' => 'type-video'], '[class=type-video]', '[/class]');
257 self::tagToBBCode($doc, 'strong', [], '[b]', '[/b]');
258 self::tagToBBCode($doc, 'em', [], '[i]', '[/i]');
259 self::tagToBBCode($doc, 'b', [], '[b]', '[/b]');
260 self::tagToBBCode($doc, 'i', [], '[i]', '[/i]');
261 self::tagToBBCode($doc, 'u', [], '[u]', '[/u]');
262 self::tagToBBCode($doc, 's', [], '[s]', '[/s]');
263 self::tagToBBCode($doc, 'del', [], '[s]', '[/s]');
264 self::tagToBBCode($doc, 'strike', [], '[s]', '[/s]');
266 self::tagToBBCode($doc, 'big', [], "[size=large]", "[/size]");
267 self::tagToBBCode($doc, 'small', [], "[size=small]", "[/size]");
269 self::tagToBBCode($doc, 'blockquote', [], '[quote]', '[/quote]');
271 self::tagToBBCode($doc, 'br', [], "\n", '');
273 self::tagToBBCode($doc, 'p', ['class' => 'MsoNormal'], "\n", "");
274 self::tagToBBCode($doc, 'div', ['class' => 'MsoNormal'], "\r", "");
276 self::tagToBBCode($doc, 'span', [], "", "");
278 self::tagToBBCode($doc, 'span', [], "", "");
279 self::tagToBBCode($doc, 'pre', [], "", "");
281 self::tagToBBCode($doc, 'div', [], "\r", "\r");
282 self::tagToBBCode($doc, 'p', [], "\n", "\n");
284 self::tagToBBCode($doc, 'ul', [], "[list]", "[/list]");
285 self::tagToBBCode($doc, 'ol', [], "[list=1]", "[/list]");
286 self::tagToBBCode($doc, 'li', [], "[*]", "");
288 self::tagToBBCode($doc, 'hr', [], "[hr]", "");
290 self::tagToBBCode($doc, 'table', [], "[table]", "[/table]");
291 self::tagToBBCode($doc, 'th', [], "[th]", "[/th]");
292 self::tagToBBCode($doc, 'tr', [], "[tr]", "[/tr]");
293 self::tagToBBCode($doc, 'td', [], "[td]", "[/td]");
295 self::tagToBBCode($doc, 'h1', [], "[h1]", "[/h1]");
296 self::tagToBBCode($doc, 'h2', [], "[h2]", "[/h2]");
297 self::tagToBBCode($doc, 'h3', [], "[h3]", "[/h3]");
298 self::tagToBBCode($doc, 'h4', [], "[h4]", "[/h4]");
299 self::tagToBBCode($doc, 'h5', [], "[h5]", "[/h5]");
300 self::tagToBBCode($doc, 'h6', [], "[h6]", "[/h6]");
302 self::tagToBBCode($doc, 'a', ['href' => '/mailto:(.+)/'], '[mail=$1]', '[/mail]');
303 self::tagToBBCode($doc, 'a', ['href' => '/(.+)/'], '[url=$1]', '[/url]');
305 self::tagToBBCode($doc, 'img', ['src' => '/(.+)/', 'alt' => '/(.+)/'], '[img=$1]$2', '[/img]', true);
306 self::tagToBBCode($doc, 'img', ['src' => '/(.+)/', 'width' => '/(\d+)/', 'height' => '/(\d+)/'], '[img=$2x$3]$1', '[/img]', true);
307 self::tagToBBCode($doc, 'img', ['src' => '/(.+)/'], '[img]$1', '[/img]', true);
310 self::tagToBBCode($doc, 'video', ['src' => '/(.+)/'], '[video]$1', '[/video]', true);
311 self::tagToBBCode($doc, 'audio', ['src' => '/(.+)/'], '[audio]$1', '[/audio]', true);
312 // Backward compatibility, [iframe] support has been removed in version 2020.12
313 self::tagToBBCode($doc, 'iframe', ['src' => '/(.+)/'], '[url]$1', '[/url]', true);
315 self::tagToBBCode($doc, 'key', [], '[code]', '[/code]');
316 self::tagToBBCode($doc, 'code', [], '[code]', '[/code]');
318 $message = $doc->saveHTML();
320 // I'm removing something really disturbing
321 // Don't know exactly what it is
322 $message = str_replace(chr(194) . chr(160), ' ', $message);
324 $message = str_replace(" ", " ", $message);
326 // removing multiple DIVs
327 $message = preg_replace('=\r *\r=i', "\n", $message);
328 $message = str_replace("\r", "\n", $message);
330 Hook::callAll('html2bbcode', $message);
332 $message = strip_tags($message);
334 $message = html_entity_decode($message, ENT_QUOTES, 'UTF-8');
336 // remove quotes if they don't make sense
337 $message = preg_replace('=\[/quote\][\s]*\[quote\]=i', "\n", $message);
339 $message = preg_replace('=\[quote\]\s*=i', "[quote]", $message);
340 $message = preg_replace('=\s*\[/quote\]=i', "[/quote]", $message);
343 $oldmessage = $message;
344 $message = str_replace("\n \n", "\n\n", $message);
345 } while ($oldmessage != $message);
348 $oldmessage = $message;
349 $message = str_replace("\n\n\n", "\n\n", $message);
350 } while ($oldmessage != $message);
353 $oldmessage = $message;
354 $message = str_replace(
377 } while ($message != $oldmessage);
379 $message = str_replace(
380 ['[b][b]', '[/b][/b]', '[i][i]', '[/i][/i]'],
381 ['[b]', '[/b]', '[i]', '[/i]'],
385 // Handling Yahoo style of mails
386 $message = str_replace('[hr][b]From:[/b]', '[quote][b]From:[/b]', $message);
391 $message = preg_replace_callback(
392 '#<pre><code(?: class="language-([^"]*)")?>(.*)</code></pre>#iUs',
393 function ($matches) {
395 if ($matches[1] != '') {
396 $prefix = '[code=' . $matches[1] . ']';
399 return $prefix . "\n" . html_entity_decode($matches[2]) . "\n" . '[/code]';
404 $message = trim($message);
406 if ($basepath != '') {
407 $message = self::qualifyURLs($message, $basepath);
410 DI::profiler()->stopRecording();
415 * Sub function to complete incomplete URL
417 * @param array $matches Result of preg_replace_callback
418 * @param string $basepath Basepath that is used to complete the URL
420 * @return string The expanded URL
422 private static function qualifyURLsSub(array $matches, string $basepath): string
424 $base = parse_url($basepath);
425 unset($base['query']);
426 unset($base['fragment']);
431 if (empty($url) || empty(parse_url($url))) {
435 $parts = array_merge($base, parse_url($url));
436 $url2 = Network::unparseURL($parts);
438 return str_replace($url, $url2, $link);
442 * Complete incomplete URLs in BBCode
444 * @param string $body Body with URLs
445 * @param string $basepath Base path that is used to complete the URL
447 * @return string Body with expanded URLs
449 private static function qualifyURLs(string $body, string $basepath): string
451 $URLSearchString = "^\[\]";
453 $matches = ["/\[url\=([$URLSearchString]*)\].*?\[\/url\]/ism",
454 "/\[url\]([$URLSearchString]*)\[\/url\]/ism",
455 "/\[img\=[0-9]*x[0-9]*\](.*?)\[\/img\]/ism",
456 "/\[img\](.*?)\[\/img\]/ism",
457 "/\[zmg\=[0-9]*x[0-9]*\](.*?)\[\/img\]/ism",
458 "/\[zmg\](.*?)\[\/zmg\]/ism",
459 "/\[video\](.*?)\[\/video\]/ism",
460 "/\[audio\](.*?)\[\/audio\]/ism",
463 foreach ($matches as $match) {
464 $body = preg_replace_callback(
466 function ($match) use ($basepath) {
467 return self::qualifyURLsSub($match, $basepath);
475 private static function breakLines(string $line, int $level, int $wraplength = 75): string
477 if ($wraplength == 0) {
478 $wraplength = 2000000;
481 $wraplen = $wraplength - $level;
488 $subline = substr($line, 0, $wraplen);
490 $pos = strrpos($subline, ' ');
493 $pos = strpos($line, ' ');
496 if (($pos > 0) && strlen($line) > $wraplen) {
497 $newline = trim(substr($line, 0, $pos));
499 $newline = str_repeat(">", $level) . ' ' . $newline;
502 $newlines[] = $newline . " ";
503 $line = substr($line, $pos + 1);
505 } while ((strlen($line) > $wraplen) && !($oldline == $line));
508 $line = str_repeat(">", $level) . ' ' . $line;
513 return implode("\n", $newlines);
516 private static function quoteLevel(string $message, int $wraplength = 75): string
518 $lines = explode("\n", $message);
522 foreach ($lines as $line) {
525 while (strpos("*" . $line, '[quote]') > 0) {
527 $pos = strpos($line, '[quote]');
528 $line = substr($line, 0, $pos) . substr($line, $pos + 7);
534 while (strpos("*" . $line, '[/quote]') > 0) {
540 $pos = strpos($line, '[/quote]');
541 $line = substr($line, 0, $pos) . substr($line, $pos + 8);
544 if (!$startquote || ($line != '')) {
545 $newlines[] = self::breakLines($line, $currlevel, $wraplength);
549 return implode("\n", $newlines);
552 private static function collectURLs(string $message): array
554 $pattern = '/<a.*?href="(.*?)".*?>(.*?)<\/a>/is';
555 preg_match_all($pattern, $message, $result, PREG_SET_ORDER);
558 foreach ($result as $treffer) {
561 // A list of some links that should be ignored
562 $list = ["/user/", "/tag/", "/group/", "/profile/", "/search?search=", "/search?tag=", "mailto:", "/u/", "/node/",
563 "//plus.google.com/", "//twitter.com/"];
564 foreach ($list as $listitem) {
565 if (strpos($treffer[1], $listitem) !== false) {
570 if ((strpos($treffer[1], "//twitter.com/") !== false) && (strpos($treffer[1], "/status/") !== false)) {
574 if ((strpos($treffer[1], "//plus.google.com/") !== false) && (strpos($treffer[1], "/posts") !== false)) {
578 if ((strpos($treffer[1], "//plus.google.com/") !== false) && (strpos($treffer[1], "/photos") !== false)) {
582 $ignore = $ignore || strpos($treffer[1], '#') === 0;
585 $urls[$treffer[1]] = $treffer[1];
593 * @param string $html
594 * @param int $wraplength Ensures individual lines aren't longer than this many characters. Doesn't break words.
595 * @param bool $compact True: Completely strips image tags; False: Keeps image URLs
598 public static function toPlaintext(string $html, int $wraplength = 75, bool $compact = false): string
600 DI::profiler()->startRecording('rendering');
601 $message = str_replace("\r", "", $html);
603 $doc = new DOMDocument();
604 $doc->preserveWhiteSpace = false;
606 $message = mb_convert_encoding($message, 'HTML-ENTITIES', "UTF-8");
608 if (empty($message)) {
609 DI::profiler()->stopRecording();
613 @$doc->loadHTML($message, LIBXML_HTML_NODEFDTD);
615 $message = $doc->saveHTML();
616 // Remove eventual UTF-8 BOM
617 $message = str_replace("\xC3\x82\xC2\xA0", "", $message);
619 // Collecting all links
620 $urls = self::collectURLs($message);
622 if (empty($message)) {
623 DI::profiler()->stopRecording();
627 @$doc->loadHTML($message, LIBXML_HTML_NODEFDTD);
629 self::tagToBBCode($doc, 'html', [], '', '');
630 self::tagToBBCode($doc, 'body', [], '', '');
633 self::tagToBBCode($doc, 'blockquote', [], "»", "«");
635 self::tagToBBCode($doc, 'blockquote', [], '[quote]', "[/quote]\n");
638 self::tagToBBCode($doc, 'br', [], "\n", '');
640 self::tagToBBCode($doc, 'span', [], "", "");
641 self::tagToBBCode($doc, 'pre', [], "", "");
642 self::tagToBBCode($doc, 'div', [], "\r", "\r");
643 self::tagToBBCode($doc, 'p', [], "\n", "\n");
645 self::tagToBBCode($doc, 'li', [], "\n* ", "\n");
647 self::tagToBBCode($doc, 'hr', [], "\n" . str_repeat("-", 70) . "\n", "");
649 self::tagToBBCode($doc, 'tr', [], "\n", "");
650 self::tagToBBCode($doc, 'td', [], "\t", "");
652 self::tagToBBCode($doc, 'h1', [], "\n\n*", "*\n");
653 self::tagToBBCode($doc, 'h2', [], "\n\n*", "*\n");
654 self::tagToBBCode($doc, 'h3', [], "\n\n*", "*\n");
655 self::tagToBBCode($doc, 'h4', [], "\n\n*", "*\n");
656 self::tagToBBCode($doc, 'h5', [], "\n\n*", "*\n");
657 self::tagToBBCode($doc, 'h6', [], "\n\n*", "*\n");
660 self::tagToBBCode($doc, 'img', ['src' => '/(.+)/'], ' [img]$1', '[/img] ');
662 self::tagToBBCode($doc, 'img', ['src' => '/(.+)/'], ' ', ' ');
665 // Backward compatibility, [iframe] support has been removed in version 2020.12
666 self::tagToBBCode($doc, 'iframe', ['src' => '/(.+)/'], ' $1 ', '');
668 $message = $doc->saveHTML();
671 $message = str_replace("[img]", "", $message);
672 $message = str_replace("[/img]", "", $message);
675 // was ersetze ich da?
676 // Irgendein stoerrisches UTF-Zeug
677 $message = str_replace(chr(194) . chr(160), ' ', $message);
679 $message = str_replace(" ", " ", $message);
681 // Aufeinanderfolgende DIVs
682 $message = preg_replace('=\r *\r=i', "\n", $message);
683 $message = str_replace("\r", "\n", $message);
685 $message = strip_tags($message);
687 $message = html_entity_decode($message, ENT_QUOTES, 'UTF-8');
689 if (!$compact && ($message != '')) {
690 foreach ($urls as $id => $url) {
691 if ($url != '' && strpos($message, $url) === false) {
692 $message .= "\n" . $url . ' ';
697 $message = str_replace("\n«", "«\n", $message);
698 $message = str_replace("»\n", "\n»", $message);
701 $oldmessage = $message;
702 $message = str_replace("\n\n\n", "\n\n", $message);
703 } while ($oldmessage != $message);
705 $message = self::quoteLevel(trim($message), $wraplength);
707 DI::profiler()->stopRecording();
708 return trim($message);
712 * Converts provided HTML code to Markdown. The hardwrap parameter maximizes
713 * compatibility with Diaspora in spite of the Markdown standards.
715 * @param string $html
718 public static function toMarkdown(string $html): string
720 DI::profiler()->startRecording('rendering');
721 $converter = new HtmlConverter(['hard_break' => true]);
722 $markdown = $converter->convert($html);
724 DI::profiler()->stopRecording();
729 * Convert video HTML to BBCode tags
734 public static function toBBCodeVideo(string $s): string
737 '#<object[^>]+>(.*?)https?://www.youtube.com/((?:v|cp)/[A-Za-z0-9\-_=]+)(.*?)</object>#ism',
738 '[youtube]$2[/youtube]',
743 '#<iframe[^>](.*?)https?://www.youtube.com/embed/([A-Za-z0-9\-_=]+)(.*?)</iframe>#ism',
744 '[youtube]$2[/youtube]',
749 '#<iframe[^>](.*?)https?://player.vimeo.com/video/([0-9]+)(.*?)</iframe>#ism',
758 * transform link href and img src from relative to absolute
760 * @param string $text
761 * @param string $base base url
764 public static function relToAbs(string $text, string $base): string
770 $base = rtrim($base, '/');
772 $base2 = $base . "/";
775 $pattern = "/<a([^>]*) href=\"(?!http|https|\/)([^\"]*)\"/";
776 $replace = "<a\${1} href=\"" . $base2 . "\${2}\"";
777 $text = preg_replace($pattern, $replace, $text);
779 $pattern = "/<a([^>]*) href=\"(?!http|https)([^\"]*)\"/";
780 $replace = "<a\${1} href=\"" . $base . "\${2}\"";
781 $text = preg_replace($pattern, $replace, $text);
784 $pattern = "/<img([^>]*) src=\"(?!http|https|\/)([^\"]*)\"/";
785 $replace = "<img\${1} src=\"" . $base2 . "\${2}\"";
786 $text = preg_replace($pattern, $replace, $text);
788 $pattern = "/<img([^>]*) src=\"(?!http|https)([^\"]*)\"/";
789 $replace = "<img\${1} src=\"" . $base . "\${2}\"";
790 $text = preg_replace($pattern, $replace, $text);
798 * Loader for infinite scrolling
800 * @return string html for loader
801 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
803 public static function scrollLoader(): string
805 $tpl = Renderer::getMarkupTemplate("scroll_loader.tpl");
806 return Renderer::replaceMacros($tpl, [
807 'wait' => DI::l10n()->t('Loading more entries...'),
808 'end' => DI::l10n()->t('The end')
813 * Format contacts as picture links or as text links
815 * @param array $contact Array with contacts which contains an array with
816 * int 'id' => The ID of the contact
817 * int 'uid' => The user ID of the user who owns this data
818 * string 'name' => The name of the contact
819 * string 'url' => The url to the profile page of the contact
820 * string 'addr' => The webbie of the contact (e.g.) username@friendica.com
821 * string 'network' => The network to which the contact belongs to
822 * string 'thumb' => The contact picture
823 * string 'click' => js code which is performed when clicking on the contact
824 * @param boolean $redirect If true try to use the redir url if it's possible
825 * @param string $class CSS class for the
826 * @param boolean $textmode If true display the contacts as text links
827 * if false display the contacts as picture links
828 * @return string Formatted html
829 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
830 * @throws \ImagickException
832 public static function micropro(array $contact, bool $redirect = false, string $class = '', bool $textmode = false): string
834 // Use the contact URL if no address is available
835 if (empty($contact['addr'])) {
836 $contact["addr"] = $contact["url"];
839 $url = $contact['url'];
844 $url = Contact::magicLinkByContact($contact);
845 if (strpos($url, 'contact/redir/') === 0) {
846 $sparkle = ' sparkle';
850 // If there is some js available we don't need the url
851 if (!empty($contact['click'])) {
855 return Renderer::replaceMacros(Renderer::getMarkupTemplate($textmode ? 'micropro_txt.tpl' : 'micropro_img.tpl'), [
856 '$click' => $contact['click'] ?? '',
859 '$photo' => Contact::getThumb($contact),
860 '$name' => $contact['name'],
861 'title' => $contact['name'] . ' [' . $contact['addr'] . ']',
862 '$parkle' => $sparkle,
870 * @param string $s Search query.
871 * @param string $id HTML id
872 * @param bool $aside Display the search widgit aside.
874 * @return string Formatted HTML.
877 public static function search(string $s, string $id = 'search-box', bool $aside = true): string
881 if (strpos($s, '#') === 0) {
884 $save_label = $mode === 'text' ? DI::l10n()->t('Save') : DI::l10n()->t('Follow');
888 '$q' => urlencode($s),
890 '$search_label' => DI::l10n()->t('Search'),
891 '$save_label' => $save_label,
892 '$search_hint' => DI::l10n()->t('@name, !forum, #tags, content'),
894 '$return_url' => urlencode(Search::getSearchPath($s)),
898 $values['$search_options'] = [
899 'fulltext' => DI::l10n()->t('Full Text'),
900 'tags' => DI::l10n()->t('Tags'),
901 'contacts' => DI::l10n()->t('Contacts')
904 if (DI::config()->get('system', 'poco_local_search')) {
905 $values['$searchoption']['forums'] = DI::l10n()->t('Forums');
909 return Renderer::replaceMacros(Renderer::getMarkupTemplate('searchbox.tpl'), $values);
913 * Given a HTML text and a set of filtering reasons, adds a content hiding header with the provided reasons
915 * Reasons are expected to have been translated already.
917 * @param string $html
918 * @param array $reasons
920 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
922 public static function applyContentFilter(string $html, array $reasons): string
924 if (count($reasons)) {
925 $tpl = Renderer::getMarkupTemplate('wall/content_filter.tpl');
926 $html = Renderer::replaceMacros($tpl, [
927 '$reasons' => $reasons,
928 '$rnd' => Strings::getRandomHex(8),
929 '$openclose' => DI::l10n()->t('Click to open/close'),
938 * replace html amp entity with amp char
942 public static function unamp(string $s): string
944 return str_replace('&', '&', $s);
948 * Clean an HTML text for potentially harmful code
950 * @param string $text
951 * @param array $allowedIframeDomains List of allowed iframe source domains without the scheme
954 public static function purify(string $text, array $allowedIframeDomains = []): string
956 // Allows cid: URL scheme
957 \HTMLPurifier_URISchemeRegistry::instance()->register('cid', new HTMLPurifier_URIScheme_cid());
959 $config = \HTMLPurifier_HTML5Config::createDefault();
960 $config->set('HTML.Doctype', 'HTML5');
962 // Used to remove iframe with src attribute filtered out
963 $config->set('AutoFormat.RemoveEmpty', true);
965 $config->set('HTML.SafeIframe', true);
967 array_walk($allowedIframeDomains, function (&$domain) {
968 // Allow the domain and all its eventual sub-domains
969 $domain = '(?:(?!-)[A-Za-z0-9-]{1,63}(?<!-)\.)*' . preg_quote(trim($domain, '/'), '%');
972 $config->set('URI.SafeIframeRegexp',
974 ' . implode('|', $allowedIframeDomains) . '
976 (?:/|$) # Prevents bogus domains like youtube.com.fake.tld
980 $config->set('Attr.AllowedRel', [
981 'noreferrer' => true,
985 $config->set('Attr.AllowedFrameTargets', [
989 $config->set('AutoFormat.RemoveEmpty.Predicate', [
990 'colgroup' => [], // |
993 'iframe' => ['src'], // ↳ Default HTMLPurify values
994 'i' => ['class'], // Allows forkawesome icons
997 // Uncomment to debug HTMLPurifier behavior
998 //$config->set('Core.CollectErrors', true);
999 //$config->set('Core.MaintainLineNumbers', true);
1001 $HTMLPurifier = new \HTMLPurifier($config);
1003 $text = $HTMLPurifier->purify($text);
1005 /** @var \HTMLPurifier_ErrorCollector $errorCollector */
1006 // Uncomment to debug HTML Purifier behavior
1007 //$errorCollector = $HTMLPurifier->context->get('ErrorCollector');
1008 //var_dump($errorCollector->getRaw());
1014 * XPath arbitrary string quoting
1016 * @see https://stackoverflow.com/a/45228168
1017 * @param string $value
1020 public static function xpathQuote(string $value): string
1022 if (false === strpos($value, '"')) {
1023 return '"' . $value . '"';
1026 if (false === strpos($value, "'")) {
1027 return "'" . $value . "'";
1030 // if the value contains both single and double quotes, construct an
1031 // expression that concatenates all non-double-quote substrings with
1032 // the quotes, e.g.:
1034 // concat("'foo'", '"', "bar")
1035 return 'concat(' . implode(', \'"\', ', array_map([self::class, 'xpathQuote'], explode('"', $value))) . ')';
1039 * Checks if the provided URL is present in the DOM document in an element with the rel="me" attribute
1041 * XHTML Friends Network http://gmpg.org/xfn/
1043 * @param DOMDocument $doc
1044 * @param UriInterface $meUrl
1047 public static function checkRelMeLink(DOMDocument $doc, UriInterface $meUrl): bool
1049 $xpath = new \DOMXpath($doc);
1051 // This expression checks that "me" is among the space-delimited values of the "rel" attribute.
1052 // And that the href attribute contains exactly the provided URL
1053 $expression = "//*[contains(concat(' ', normalize-space(@rel), ' '), ' me ')][@href = " . self::xpathQuote($meUrl) . "]";
1055 $result = $xpath->query($expression);
1057 return $result !== false && $result->length > 0;
1061 * @param DOMDocument $doc
1062 * @return string|null Lowercase charset
1064 public static function extractCharset(DOMDocument $doc): ?string
1066 $xpath = new DOMXPath($doc);
1068 $expression = "string(//meta[@charset]/@charset)";
1069 if ($charset = $xpath->evaluate($expression)) {
1070 return strtolower($charset);
1074 // This expression looks for a meta tag with the http-equiv attribute set to "content-type" ignoring case
1075 // whose content attribute contains a "charset" string and returns its value
1076 $expression = "string(//meta[@http-equiv][translate(@http-equiv, 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz') = 'content-type'][contains(translate(@content, 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz'), 'charset')]/@content)";
1077 $mediaType = MediaType::fromContentType($xpath->evaluate($expression));
1078 if (isset($mediaType->parameters['charset'])) {
1079 return strtolower($mediaType->parameters['charset']);
1081 } catch(\InvalidArgumentException $e) {}