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\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;
36 use Psr\Http\Message\UriInterface;
41 * Search all instances of a specific HTML tag node in the provided DOM document and replaces them with BBCode text nodes.
43 * @see HTML::tagToBBCodeSub()
45 private static function tagToBBCode(DOMDocument $doc, string $tag, array $attributes, string $startbb, string $endbb, bool $ignoreChildren = false)
48 $done = self::tagToBBCodeSub($doc, $tag, $attributes, $startbb, $endbb, $ignoreChildren);
53 * Search the first specific HTML tag node in the provided DOM document and replaces it with BBCode text nodes.
55 * @param DOMDocument $doc
56 * @param string $tag HTML tag name
57 * @param array $attributes Array of attributes to match and optionally use the value from
58 * @param string $startbb BBCode tag opening
59 * @param string $endbb BBCode tag closing
60 * @param bool $ignoreChildren If set to false, the HTML tag children will be appended as text inside the BBCode tag
61 * Otherwise, they will be entirely ignored. Useful for simple BBCode that draw their
62 * inner value from an attribute value and disregard the tag children.
63 * @return bool Whether a replacement was done
65 private static function tagToBBCodeSub(DOMDocument $doc, string $tag, array $attributes, string $startbb, string $endbb, bool $ignoreChildren = false): bool
67 $savestart = str_replace('$', '\x01', $startbb);
70 $xpath = new DOMXPath($doc);
72 /** @var \DOMNode[] $list */
73 $list = $xpath->query("//" . $tag);
74 foreach ($list as $node) {
76 if ($node->attributes->length) {
77 foreach ($node->attributes as $attribute) {
78 $attr[$attribute->name] = $attribute->value;
84 $startbb = $savestart;
88 foreach ($attributes as $attribute => $value) {
89 $startbb = str_replace('\x01' . ++$i, '$1', $startbb);
90 if (strpos('*' . $startbb, '$1') > 0) {
91 if ($replace && (@$attr[$attribute] != '')) {
92 $startbb = preg_replace($value, $startbb, $attr[$attribute], -1, $count);
94 // If nothing could be changed
102 if (@$attr[$attribute] != $value) {
109 $StartCode = $doc->createTextNode($startbb);
110 $EndCode = $doc->createTextNode($endbb);
112 $node->parentNode->insertBefore($StartCode, $node);
114 if (!$ignoreChildren && $node->hasChildNodes()) {
115 /** @var \DOMNode $child */
116 foreach ($node->childNodes as $key => $child) {
117 /* Remove empty text nodes at the start or at the end of the children list */
118 if ($key > 0 && $key < $node->childNodes->length - 1 || $child->nodeName != '#text' || trim($child->nodeValue) !== '') {
119 $newNode = $child->cloneNode(true);
120 $node->parentNode->insertBefore($newNode, $node);
125 $node->parentNode->insertBefore($EndCode, $node);
126 $node->parentNode->removeChild($node);
134 * Converter for HTML to BBCode
136 * Made by: ike@piratenpartei.de
137 * Originally made for the syncom project: http://wiki.piratenpartei.de/Syncom
138 * https://github.com/annando/Syncom
140 * @param string $message
141 * @param string $basepath
143 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
145 public static function toBBCode(string $message, string $basepath = ''): string
148 * Check if message is empty to prevent a lot of code below from being executed
149 * for just an empty message.
151 if ($message === '') {
155 DI::profiler()->startRecording('rendering');
156 $message = str_replace("\r", "", $message);
158 $message = Strings::performWithEscapedBlocks($message, '#<pre><code.*</code></pre>#iUs', function ($message) {
159 $message = str_replace(
172 $message = preg_replace('=<(\w+):(.+?)>=', '<removeme>', $message);
173 $message = preg_replace('=</(\w+):(.+?)>=', '</removeme>', $message);
175 $doc = new DOMDocument();
176 $doc->preserveWhiteSpace = false;
178 $message = mb_convert_encoding($message, 'HTML-ENTITIES', "UTF-8");
180 if (empty($message)) {
184 @$doc->loadHTML($message, LIBXML_HTML_NODEFDTD);
186 XML::deleteNode($doc, 'style');
187 XML::deleteNode($doc, 'head');
188 XML::deleteNode($doc, 'title');
189 XML::deleteNode($doc, 'meta');
190 XML::deleteNode($doc, 'xml');
191 XML::deleteNode($doc, 'removeme');
193 $xpath = new DomXPath($doc);
194 $list = $xpath->query("//pre");
195 foreach ($list as $node) {
196 // Ensure to escape unescaped & - they will otherwise raise a warning
197 $safe_value = preg_replace('/&(?!\w+;)/', '&', $node->nodeValue);
198 $node->nodeValue = str_replace("\n", "\r", $safe_value);
201 $message = $doc->saveHTML();
202 $message = str_replace(["\n<", ">\n", "\r", "\n", "\xC3\x82\xC2\xA0"], ["<", ">", "<br />", " ", ""], $message);
203 $message = preg_replace('= [\s]*=i', " ", $message);
205 if (empty($message)) {
209 @$doc->loadHTML($message, LIBXML_HTML_NODEFDTD);
211 self::tagToBBCode($doc, 'html', [], "", "");
212 self::tagToBBCode($doc, 'body', [], "", "");
214 // Outlook-Quote - Variant 1
215 self::tagToBBCode($doc, 'p', ['class' => 'MsoNormal', 'style' => 'margin-left:35.4pt'], '[quote]', '[/quote]');
217 // Outlook-Quote - Variant 2
221 ['style' => 'border:none;border-left:solid blue 1.5pt;padding:0cm 0cm 0cm 4.0pt'],
227 self::tagToBBCode($doc, 'span', ['style' => 'text-decoration: underline;'], '[u]', '[/u]');
228 self::tagToBBCode($doc, 'span', ['style' => 'font-style: italic;'], '[i]', '[/i]');
229 self::tagToBBCode($doc, 'span', ['style' => 'font-weight: bold;'], '[b]', '[/b]');
231 /* self::node2BBCode($doc, 'font', array('face'=>'/([\w ]+)/', 'size'=>'/(\d+)/', 'color'=>'/(.+)/'), '[font=$1][size=$2][color=$3]', '[/color][/size][/font]');
232 self::node2BBCode($doc, 'font', array('size'=>'/(\d+)/', 'color'=>'/(.+)/'), '[size=$1][color=$2]', '[/color][/size]');
233 self::node2BBCode($doc, 'font', array('face'=>'/([\w ]+)/', 'size'=>'/(.+)/'), '[font=$1][size=$2]', '[/size][/font]');
234 self::node2BBCode($doc, 'font', array('face'=>'/([\w ]+)/', 'color'=>'/(.+)/'), '[font=$1][color=$3]', '[/color][/font]');
235 self::node2BBCode($doc, 'font', array('face'=>'/([\w ]+)/'), '[font=$1]', '[/font]');
236 self::node2BBCode($doc, 'font', array('size'=>'/(\d+)/'), '[size=$1]', '[/size]');
237 self::node2BBCode($doc, 'font', array('color'=>'/(.+)/'), '[color=$1]', '[/color]');
240 //self::node2BBCode($doc, 'span', array('style'=>'/.*font-size:\s*(.+?)[,;].*font-family:\s*(.+?)[,;].*color:\s*(.+?)[,;].*/'), '[size=$1][font=$2][color=$3]', '[/color][/font][/size]');
241 //self::node2BBCode($doc, 'span', array('style'=>'/.*font-size:\s*(\d+)[,;].*/'), '[size=$1]', '[/size]');
242 //self::node2BBCode($doc, 'span', array('style'=>'/.*font-size:\s*(.+?)[,;].*/'), '[size=$1]', '[/size]');
244 self::tagToBBCode($doc, 'span', ['style' => '/.*color:\s*(.+?)[,;].*/'], '[color="$1"]', '[/color]');
246 //self::node2BBCode($doc, 'span', array('style'=>'/.*font-family:\s*(.+?)[,;].*/'), '[font=$1]', '[/font]');
247 //self::node2BBCode($doc, 'div', array('style'=>'/.*font-family:\s*(.+?)[,;].*font-size:\s*(\d+?)pt.*/'), '[font=$1][size=$2]', '[/size][/font]');
248 //self::node2BBCode($doc, 'div', array('style'=>'/.*font-family:\s*(.+?)[,;].*font-size:\s*(\d+?)px.*/'), '[font=$1][size=$2]', '[/size][/font]');
249 //self::node2BBCode($doc, 'div', array('style'=>'/.*font-family:\s*(.+?)[,;].*/'), '[font=$1]', '[/font]');
250 // Importing the classes - interesting for importing of posts from third party networks that were exported from friendica
252 //self::node2BBCode($doc, 'span', array('class'=>'/([\w ]+)/'), '[class=$1]', '[/class]');
253 self::tagToBBCode($doc, 'span', ['class' => 'type-link'], '[class=type-link]', '[/class]');
254 self::tagToBBCode($doc, 'span', ['class' => 'type-video'], '[class=type-video]', '[/class]');
256 self::tagToBBCode($doc, 'strong', [], '[b]', '[/b]');
257 self::tagToBBCode($doc, 'em', [], '[i]', '[/i]');
258 self::tagToBBCode($doc, 'b', [], '[b]', '[/b]');
259 self::tagToBBCode($doc, 'i', [], '[i]', '[/i]');
260 self::tagToBBCode($doc, 'u', [], '[u]', '[/u]');
261 self::tagToBBCode($doc, 's', [], '[s]', '[/s]');
262 self::tagToBBCode($doc, 'del', [], '[s]', '[/s]');
263 self::tagToBBCode($doc, 'strike', [], '[s]', '[/s]');
265 self::tagToBBCode($doc, 'big', [], "[size=large]", "[/size]");
266 self::tagToBBCode($doc, 'small', [], "[size=small]", "[/size]");
268 self::tagToBBCode($doc, 'blockquote', [], '[quote]', '[/quote]');
270 self::tagToBBCode($doc, 'br', [], "\n", '');
272 self::tagToBBCode($doc, 'p', ['class' => 'MsoNormal'], "\n", "");
273 self::tagToBBCode($doc, 'div', ['class' => 'MsoNormal'], "\r", "");
275 self::tagToBBCode($doc, 'span', [], "", "");
277 self::tagToBBCode($doc, 'span', [], "", "");
278 self::tagToBBCode($doc, 'pre', [], "", "");
280 self::tagToBBCode($doc, 'div', [], "\r", "\r");
281 self::tagToBBCode($doc, 'p', [], "\n", "\n");
283 self::tagToBBCode($doc, 'ul', [], "[ul]", "\n[/ul]");
284 self::tagToBBCode($doc, 'ol', [], "[ol]", "\n[/ol]");
285 self::tagToBBCode($doc, 'li', [], "\n[li]", "[/li]");
287 self::tagToBBCode($doc, 'hr', [], "[hr]", "");
289 self::tagToBBCode($doc, 'table', [], "[table]", "[/table]");
290 self::tagToBBCode($doc, 'th', [], "[th]", "[/th]");
291 self::tagToBBCode($doc, 'tr', [], "[tr]", "[/tr]");
292 self::tagToBBCode($doc, 'td', [], "[td]", "[/td]");
294 self::tagToBBCode($doc, 'h1', [], "[h1]", "[/h1]");
295 self::tagToBBCode($doc, 'h2', [], "[h2]", "[/h2]");
296 self::tagToBBCode($doc, 'h3', [], "[h3]", "[/h3]");
297 self::tagToBBCode($doc, 'h4', [], "[h4]", "[/h4]");
298 self::tagToBBCode($doc, 'h5', [], "[h5]", "[/h5]");
299 self::tagToBBCode($doc, 'h6', [], "[h6]", "[/h6]");
301 self::tagToBBCode($doc, 'a', ['href' => '/mailto:(.+)/'], '[mail=$1]', '[/mail]');
302 self::tagToBBCode($doc, 'a', ['href' => '/(.+)/'], '[url=$1]', '[/url]');
304 self::tagToBBCode($doc, 'img', ['src' => '/(.+)/', 'alt' => '/(.+)/'], '[img=$1]$2', '[/img]', true);
305 self::tagToBBCode($doc, 'img', ['src' => '/(.+)/', 'width' => '/(\d+)/', 'height' => '/(\d+)/'], '[img=$2x$3]$1', '[/img]', true);
306 self::tagToBBCode($doc, 'img', ['src' => '/(.+)/'], '[img]$1', '[/img]', true);
309 self::tagToBBCode($doc, 'video', ['src' => '/(.+)/'], '[video]$1', '[/video]', true);
310 self::tagToBBCode($doc, 'audio', ['src' => '/(.+)/'], '[audio]$1', '[/audio]', true);
311 // Backward compatibility, [iframe] support has been removed in version 2020.12
312 self::tagToBBCode($doc, 'iframe', ['src' => '/(.+)/'], '[url]$1', '[/url]', true);
314 self::tagToBBCode($doc, 'key', [], '[code]', '[/code]');
315 self::tagToBBCode($doc, 'code', [], '[code]', '[/code]');
317 $message = $doc->saveHTML();
319 // I'm removing something really disturbing
320 // Don't know exactly what it is
321 $message = str_replace(chr(194) . chr(160), ' ', $message);
323 $message = str_replace(" ", " ", $message);
325 // removing multiple DIVs
326 $message = preg_replace('=\r *\r=i', "\n", $message);
327 $message = str_replace("\r", "\n", $message);
329 Hook::callAll('html2bbcode', $message);
331 $message = strip_tags($message);
333 $message = html_entity_decode($message, ENT_QUOTES, 'UTF-8');
335 // remove quotes if they don't make sense
336 $message = preg_replace('=\[/quote\][\s]*\[quote\]=i', "\n", $message);
338 $message = preg_replace('=\[quote\]\s*=i', "[quote]", $message);
339 $message = preg_replace('=\s*\[/quote\]=i', "[/quote]", $message);
342 $oldmessage = $message;
343 $message = str_replace("\n \n", "\n\n", $message);
344 } while ($oldmessage != $message);
347 $oldmessage = $message;
348 $message = str_replace("\n\n\n", "\n\n", $message);
349 } while ($oldmessage != $message);
351 $message = str_replace(
352 ['[b][b]', '[/b][/b]', '[i][i]', '[/i][/i]'],
353 ['[b]', '[/b]', '[i]', '[/i]'],
357 // Handling Yahoo style of mails
358 $message = str_replace('[hr][b]From:[/b]', '[quote][b]From:[/b]', $message);
363 $message = preg_replace_callback(
364 '#<pre><code(?: class="language-([^"]*)")?>(.*)</code></pre>#iUs',
365 function ($matches) {
367 if ($matches[1] != '') {
368 $prefix = '[code=' . $matches[1] . ']';
371 return $prefix . "\n" . html_entity_decode($matches[2]) . "\n" . '[/code]';
376 $message = trim($message);
378 if ($basepath != '') {
379 $message = self::qualifyURLs($message, $basepath);
382 DI::profiler()->stopRecording();
387 * Sub function to complete incomplete URL
389 * @param array $matches Result of preg_replace_callback
390 * @param string $basepath Basepath that is used to complete the URL
392 * @return string The expanded URL
394 private static function qualifyURLsSub(array $matches, string $basepath): string
396 $base = parse_url($basepath);
397 unset($base['query']);
398 unset($base['fragment']);
403 if (empty($url) || empty(parse_url($url))) {
407 $parts = array_merge($base, parse_url($url));
408 $url2 = Network::unparseURL($parts);
410 return str_replace($url, $url2, $link);
414 * Complete incomplete URLs in BBCode
416 * @param string $body Body with URLs
417 * @param string $basepath Base path that is used to complete the URL
419 * @return string Body with expanded URLs
421 private static function qualifyURLs(string $body, string $basepath): string
423 $URLSearchString = "^\[\]";
425 $matches = ["/\[url\=([$URLSearchString]*)\].*?\[\/url\]/ism",
426 "/\[url\]([$URLSearchString]*)\[\/url\]/ism",
427 "/\[img\=[0-9]*x[0-9]*\](.*?)\[\/img\]/ism",
428 "/\[img\](.*?)\[\/img\]/ism",
429 "/\[zmg\=[0-9]*x[0-9]*\](.*?)\[\/img\]/ism",
430 "/\[zmg\](.*?)\[\/zmg\]/ism",
431 "/\[video\](.*?)\[\/video\]/ism",
432 "/\[audio\](.*?)\[\/audio\]/ism",
435 foreach ($matches as $match) {
436 $body = preg_replace_callback(
438 function ($match) use ($basepath) {
439 return self::qualifyURLsSub($match, $basepath);
447 private static function breakLines(string $line, int $level, int $wraplength = 75): string
449 if ($wraplength == 0) {
450 $wraplength = 2000000;
453 $wraplen = $wraplength - $level;
460 $subline = substr($line, 0, $wraplen);
462 $pos = strrpos($subline, ' ');
465 $pos = strpos($line, ' ');
468 if (($pos > 0) && strlen($line) > $wraplen) {
469 $newline = trim(substr($line, 0, $pos));
471 $newline = str_repeat(">", $level) . ' ' . $newline;
474 $newlines[] = $newline . " ";
475 $line = substr($line, $pos + 1);
477 } while ((strlen($line) > $wraplen) && !($oldline == $line));
480 $line = str_repeat(">", $level) . ' ' . $line;
485 return implode("\n", $newlines);
488 private static function quoteLevel(string $message, int $wraplength = 75): string
490 $lines = explode("\n", $message);
494 foreach ($lines as $line) {
497 while (strpos("*" . $line, '[quote]') > 0) {
499 $pos = strpos($line, '[quote]');
500 $line = substr($line, 0, $pos) . substr($line, $pos + 7);
506 while (strpos("*" . $line, '[/quote]') > 0) {
512 $pos = strpos($line, '[/quote]');
513 $line = substr($line, 0, $pos) . substr($line, $pos + 8);
516 if (!$startquote || ($line != '')) {
517 $newlines[] = self::breakLines($line, $currlevel, $wraplength);
521 return implode("\n", $newlines);
524 private static function collectURLs(string $message): array
526 $pattern = '/<a.*?href="(.*?)".*?>(.*?)<\/a>/is';
527 preg_match_all($pattern, $message, $result, PREG_SET_ORDER);
530 foreach ($result as $treffer) {
533 // A list of some links that should be ignored
534 $list = ["/user/", "/tag/", "/group/", "/profile/", "/search?search=", "/search?tag=", "mailto:", "/u/", "/node/",
535 "//plus.google.com/", "//twitter.com/"];
536 foreach ($list as $listitem) {
537 if (strpos($treffer[1], $listitem) !== false) {
542 if ((strpos($treffer[1], "//twitter.com/") !== false) && (strpos($treffer[1], "/status/") !== false)) {
546 if ((strpos($treffer[1], "//plus.google.com/") !== false) && (strpos($treffer[1], "/posts") !== false)) {
550 if ((strpos($treffer[1], "//plus.google.com/") !== false) && (strpos($treffer[1], "/photos") !== false)) {
554 $ignore = $ignore || strpos($treffer[1], '#') === 0;
557 $urls[$treffer[1]] = $treffer[1];
565 * @param string $html
566 * @param int $wraplength Ensures individual lines aren't longer than this many characters. Doesn't break words.
567 * @param bool $compact True: Completely strips image tags; False: Keeps image URLs
570 public static function toPlaintext(string $html, int $wraplength = 75, bool $compact = false): string
572 DI::profiler()->startRecording('rendering');
573 $message = str_replace("\r", "", $html);
575 $doc = new DOMDocument();
576 $doc->preserveWhiteSpace = false;
578 $message = mb_convert_encoding($message, 'HTML-ENTITIES', "UTF-8");
580 if (empty($message)) {
581 DI::profiler()->stopRecording();
585 @$doc->loadHTML($message, LIBXML_HTML_NODEFDTD);
587 $message = $doc->saveHTML();
588 // Remove eventual UTF-8 BOM
589 $message = str_replace("\xC3\x82\xC2\xA0", "", $message);
591 // Collecting all links
592 $urls = self::collectURLs($message);
594 if (empty($message)) {
595 DI::profiler()->stopRecording();
599 @$doc->loadHTML($message, LIBXML_HTML_NODEFDTD);
601 self::tagToBBCode($doc, 'html', [], '', '');
602 self::tagToBBCode($doc, 'body', [], '', '');
605 self::tagToBBCode($doc, 'blockquote', [], "»", "«");
607 self::tagToBBCode($doc, 'blockquote', [], '[quote]', "[/quote]\n");
610 self::tagToBBCode($doc, 'br', [], "\n", '');
612 self::tagToBBCode($doc, 'span', [], "", "");
613 self::tagToBBCode($doc, 'pre', [], "", "");
614 self::tagToBBCode($doc, 'div', [], "\r", "\r");
615 self::tagToBBCode($doc, 'p', [], "\n", "\n");
617 self::tagToBBCode($doc, 'li', [], "\n* ", "\n");
619 self::tagToBBCode($doc, 'hr', [], "\n" . str_repeat("-", 70) . "\n", "");
621 self::tagToBBCode($doc, 'tr', [], "\n", "");
622 self::tagToBBCode($doc, 'td', [], "\t", "");
624 self::tagToBBCode($doc, 'h1', [], "\n\n*", "*\n");
625 self::tagToBBCode($doc, 'h2', [], "\n\n*", "*\n");
626 self::tagToBBCode($doc, 'h3', [], "\n\n*", "*\n");
627 self::tagToBBCode($doc, 'h4', [], "\n\n*", "*\n");
628 self::tagToBBCode($doc, 'h5', [], "\n\n*", "*\n");
629 self::tagToBBCode($doc, 'h6', [], "\n\n*", "*\n");
632 self::tagToBBCode($doc, 'img', ['src' => '/(.+)/'], ' [img]$1', '[/img] ');
634 self::tagToBBCode($doc, 'img', ['src' => '/(.+)/'], ' ', ' ');
637 // Backward compatibility, [iframe] support has been removed in version 2020.12
638 self::tagToBBCode($doc, 'iframe', ['src' => '/(.+)/'], ' $1 ', '');
640 $message = $doc->saveHTML();
643 $message = str_replace("[img]", "", $message);
644 $message = str_replace("[/img]", "", $message);
647 // was ersetze ich da?
648 // Irgendein stoerrisches UTF-Zeug
649 $message = str_replace(chr(194) . chr(160), ' ', $message);
651 $message = str_replace(" ", " ", $message);
653 // Aufeinanderfolgende DIVs
654 $message = preg_replace('=\r *\r=i', "\n", $message);
655 $message = str_replace("\r", "\n", $message);
657 $message = strip_tags($message);
659 $message = html_entity_decode($message, ENT_QUOTES, 'UTF-8');
661 if (!$compact && ($message != '')) {
662 foreach ($urls as $id => $url) {
663 if ($url != '' && strpos($message, $url) === false) {
664 $message .= "\n" . $url . ' ';
669 $message = str_replace("\n«", "«\n", $message);
670 $message = str_replace("»\n", "\n»", $message);
673 $oldmessage = $message;
674 $message = str_replace("\n\n\n", "\n\n", $message);
675 } while ($oldmessage != $message);
677 $message = self::quoteLevel(trim($message), $wraplength);
679 DI::profiler()->stopRecording();
680 return trim($message);
684 * Converts provided HTML code to Markdown. The hardwrap parameter maximizes
685 * compatibility with Diaspora in spite of the Markdown standards.
687 * @param string $html
690 public static function toMarkdown(string $html): string
692 DI::profiler()->startRecording('rendering');
693 $converter = new HtmlConverter(['hard_break' => true]);
694 $markdown = $converter->convert($html);
696 DI::profiler()->stopRecording();
701 * Convert video HTML to BBCode tags
706 public static function toBBCodeVideo(string $s): string
709 '#<object[^>]+>(.*?)https?://www.youtube.com/((?:v|cp)/[A-Za-z0-9\-_=]+)(.*?)</object>#ism',
710 '[youtube]$2[/youtube]',
715 '#<iframe[^>](.*?)https?://www.youtube.com/embed/([A-Za-z0-9\-_=]+)(.*?)</iframe>#ism',
716 '[youtube]$2[/youtube]',
721 '#<iframe[^>](.*?)https?://player.vimeo.com/video/([0-9]+)(.*?)</iframe>#ism',
730 * transform link href and img src from relative to absolute
732 * @param string $text
733 * @param string $base base url
736 public static function relToAbs(string $text, string $base): string
742 $base = rtrim($base, '/');
744 $base2 = $base . "/";
747 $pattern = "/<a([^>]*) href=\"(?!http|https|\/)([^\"]*)\"/";
748 $replace = "<a\${1} href=\"" . $base2 . "\${2}\"";
749 $text = preg_replace($pattern, $replace, $text);
751 $pattern = "/<a([^>]*) href=\"(?!http|https)([^\"]*)\"/";
752 $replace = "<a\${1} href=\"" . $base . "\${2}\"";
753 $text = preg_replace($pattern, $replace, $text);
756 $pattern = "/<img([^>]*) src=\"(?!http|https|\/)([^\"]*)\"/";
757 $replace = "<img\${1} src=\"" . $base2 . "\${2}\"";
758 $text = preg_replace($pattern, $replace, $text);
760 $pattern = "/<img([^>]*) src=\"(?!http|https)([^\"]*)\"/";
761 $replace = "<img\${1} src=\"" . $base . "\${2}\"";
762 $text = preg_replace($pattern, $replace, $text);
770 * Loader for infinite scrolling
772 * @return string html for loader
773 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
775 public static function scrollLoader(): string
777 $tpl = Renderer::getMarkupTemplate("scroll_loader.tpl");
778 return Renderer::replaceMacros($tpl, [
779 'wait' => DI::l10n()->t('Loading more entries...'),
780 'end' => DI::l10n()->t('The end')
785 * Format contacts as picture links or as text links
787 * @param array $contact Array with contacts which contains an array with
788 * int 'id' => The ID of the contact
789 * int 'uid' => The user ID of the user who owns this data
790 * string 'name' => The name of the contact
791 * string 'url' => The url to the profile page of the contact
792 * string 'addr' => The webbie of the contact (e.g.) username@friendica.com
793 * string 'network' => The network to which the contact belongs to
794 * string 'thumb' => The contact picture
795 * string 'click' => js code which is performed when clicking on the contact
796 * @param boolean $redirect If true try to use the redir url if it's possible
797 * @param string $class CSS class for the
798 * @param boolean $textmode If true display the contacts as text links
799 * if false display the contacts as picture links
800 * @return string Formatted html
801 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
802 * @throws \ImagickException
804 public static function micropro(array $contact, bool $redirect = false, string $class = '', bool $textmode = false): string
806 // Use the contact URL if no address is available
807 if (empty($contact['addr'])) {
808 $contact["addr"] = $contact["url"];
811 $url = $contact['url'];
816 $url = Contact::magicLinkByContact($contact);
817 if (strpos($url, 'contact/redir/') === 0) {
818 $sparkle = ' sparkle';
822 // If there is some js available we don't need the url
823 if (!empty($contact['click'])) {
827 return Renderer::replaceMacros(Renderer::getMarkupTemplate($textmode ? 'micropro_txt.tpl' : 'micropro_img.tpl'), [
828 '$click' => $contact['click'] ?? '',
831 '$photo' => Contact::getThumb($contact),
832 '$name' => $contact['name'],
833 'title' => $contact['name'] . ' [' . $contact['addr'] . ']',
834 '$parkle' => $sparkle,
842 * @param string $s Search query.
843 * @param string $id HTML id
844 * @param bool $aside Display the search widget aside.
846 * @return string Formatted HTML.
849 public static function search(string $s, string $id = 'search-box', bool $aside = true): string
853 if (strpos($s, '#') === 0) {
856 $save_label = $mode === 'text' ? DI::l10n()->t('Save') : DI::l10n()->t('Follow');
860 '$q' => urlencode($s),
862 '$search_label' => DI::l10n()->t('Search'),
863 '$save_label' => $save_label,
864 '$search_hint' => DI::l10n()->t('@name, !forum, #tags, content'),
866 '$return_url' => urlencode(Search::getSearchPath($s)),
870 $values['$search_options'] = [
871 'fulltext' => DI::l10n()->t('Full Text'),
872 'tags' => DI::l10n()->t('Tags'),
873 'contacts' => DI::l10n()->t('Contacts')
876 if (DI::config()->get('system', 'poco_local_search')) {
877 $values['$searchoption']['forums'] = DI::l10n()->t('Forums');
881 return Renderer::replaceMacros(Renderer::getMarkupTemplate('searchbox.tpl'), $values);
885 * Given a HTML text and a set of filtering reasons, adds a content hiding header with the provided reasons
887 * Reasons are expected to have been translated already.
889 * @param string $html
890 * @param array $reasons
892 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
894 public static function applyContentFilter(string $html, array $reasons): string
896 if (count($reasons)) {
897 $tpl = Renderer::getMarkupTemplate('wall/content_filter.tpl');
898 $html = Renderer::replaceMacros($tpl, [
899 '$reasons' => $reasons,
900 '$rnd' => Strings::getRandomHex(8),
901 '$openclose' => DI::l10n()->t('Click to open/close'),
910 * replace html amp entity with amp char
914 public static function unamp(string $s): string
916 return str_replace('&', '&', $s);
920 * Clean an HTML text for potentially harmful code
922 * @param string $text
923 * @param array $allowedIframeDomains List of allowed iframe source domains without the scheme
926 public static function purify(string $text, array $allowedIframeDomains = []): string
928 // Allows cid: URL scheme
929 \HTMLPurifier_URISchemeRegistry::instance()->register('cid', new HTMLPurifier_URIScheme_cid());
931 $config = \HTMLPurifier_HTML5Config::createDefault();
932 $config->set('HTML.Doctype', 'HTML5');
934 // Used to remove iframe with src attribute filtered out
935 $config->set('AutoFormat.RemoveEmpty', true);
937 $config->set('HTML.SafeIframe', true);
939 array_walk($allowedIframeDomains, function (&$domain) {
940 // Allow the domain and all its eventual sub-domains
941 $domain = '(?:(?!-)[A-Za-z0-9-]{1,63}(?<!-)\.)*' . preg_quote(trim($domain, '/'), '%');
944 $config->set('URI.SafeIframeRegexp',
946 ' . implode('|', $allowedIframeDomains) . '
948 (?:/|$) # Prevents bogus domains like youtube.com.fake.tld
952 $config->set('Attr.AllowedRel', [
953 'noreferrer' => true,
957 $config->set('Attr.AllowedFrameTargets', [
961 $config->set('AutoFormat.RemoveEmpty.Predicate', [
962 'colgroup' => [], // |
965 'iframe' => ['src'], // ↳ Default HTMLPurify values
966 'i' => ['class'], // Allows forkawesome icons
969 // Uncomment to debug HTMLPurifier behavior
970 //$config->set('Core.CollectErrors', true);
971 //$config->set('Core.MaintainLineNumbers', true);
973 $HTMLPurifier = new \HTMLPurifier($config);
975 $text = $HTMLPurifier->purify($text);
977 /** @var \HTMLPurifier_ErrorCollector $errorCollector */
978 // Uncomment to debug HTML Purifier behavior
979 //$errorCollector = $HTMLPurifier->context->get('ErrorCollector');
980 //var_dump($errorCollector->getRaw());
986 * XPath arbitrary string quoting
988 * @see https://stackoverflow.com/a/45228168
989 * @param string $value
992 public static function xpathQuote(string $value): string
994 if (false === strpos($value, '"')) {
995 return '"' . $value . '"';
998 if (false === strpos($value, "'")) {
999 return "'" . $value . "'";
1002 // if the value contains both single and double quotes, construct an
1003 // expression that concatenates all non-double-quote substrings with
1004 // the quotes, e.g.:
1006 // concat("'foo'", '"', "bar")
1007 return 'concat(' . implode(', \'"\', ', array_map([self::class, 'xpathQuote'], explode('"', $value))) . ')';
1011 * Checks if the provided URL is present in the DOM document in an element with the rel="me" attribute
1013 * XHTML Friends Network http://gmpg.org/xfn/
1015 * @param DOMDocument $doc
1016 * @param UriInterface $meUrl
1019 public static function checkRelMeLink(DOMDocument $doc, UriInterface $meUrl): bool
1021 $xpath = new \DOMXpath($doc);
1023 // This expression checks that "me" is among the space-delimited values of the "rel" attribute.
1024 // And that the href attribute contains exactly the provided URL
1025 $expression = "//*[contains(concat(' ', normalize-space(@rel), ' '), ' me ')][@href = " . self::xpathQuote($meUrl) . "]";
1027 $result = $xpath->query($expression);
1029 return $result !== false && $result->length > 0;
1033 * @param DOMDocument $doc
1034 * @return string|null Lowercase charset
1036 public static function extractCharset(DOMDocument $doc): ?string
1038 $xpath = new DOMXPath($doc);
1040 $expression = "string(//meta[@charset]/@charset)";
1041 if ($charset = $xpath->evaluate($expression)) {
1042 return strtolower($charset);
1046 // This expression looks for a meta tag with the http-equiv attribute set to "content-type" ignoring case
1047 // whose content attribute contains a "charset" string and returns its value
1048 $expression = "string(//meta[@http-equiv][translate(@http-equiv, 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz') = 'content-type'][contains(translate(@content, 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz'), 'charset')]/@content)";
1049 $mediaType = MediaType::fromContentType($xpath->evaluate($expression));
1050 if (isset($mediaType->parameters['charset'])) {
1051 return strtolower($mediaType->parameters['charset']);
1053 } catch(\InvalidArgumentException $e) {}