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', [], "[ul]", "\n[/ul]");
285 self::tagToBBCode($doc, 'ol', [], "[ol]", "\n[/ol]");
286 self::tagToBBCode($doc, 'li', [], "\n[li]", "[/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);
352 $message = str_replace(
353 ['[b][b]', '[/b][/b]', '[i][i]', '[/i][/i]'],
354 ['[b]', '[/b]', '[i]', '[/i]'],
358 // Handling Yahoo style of mails
359 $message = str_replace('[hr][b]From:[/b]', '[quote][b]From:[/b]', $message);
364 $message = preg_replace_callback(
365 '#<pre><code(?: class="language-([^"]*)")?>(.*)</code></pre>#iUs',
366 function ($matches) {
368 if ($matches[1] != '') {
369 $prefix = '[code=' . $matches[1] . ']';
372 return $prefix . "\n" . html_entity_decode($matches[2]) . "\n" . '[/code]';
377 $message = trim($message);
379 if ($basepath != '') {
380 $message = self::qualifyURLs($message, $basepath);
383 DI::profiler()->stopRecording();
388 * Sub function to complete incomplete URL
390 * @param array $matches Result of preg_replace_callback
391 * @param string $basepath Basepath that is used to complete the URL
393 * @return string The expanded URL
395 private static function qualifyURLsSub(array $matches, string $basepath): string
397 $base = parse_url($basepath);
398 unset($base['query']);
399 unset($base['fragment']);
404 if (empty($url) || empty(parse_url($url))) {
408 $parts = array_merge($base, parse_url($url));
409 $url2 = Network::unparseURL($parts);
411 return str_replace($url, $url2, $link);
415 * Complete incomplete URLs in BBCode
417 * @param string $body Body with URLs
418 * @param string $basepath Base path that is used to complete the URL
420 * @return string Body with expanded URLs
422 private static function qualifyURLs(string $body, string $basepath): string
424 $URLSearchString = "^\[\]";
426 $matches = ["/\[url\=([$URLSearchString]*)\].*?\[\/url\]/ism",
427 "/\[url\]([$URLSearchString]*)\[\/url\]/ism",
428 "/\[img\=[0-9]*x[0-9]*\](.*?)\[\/img\]/ism",
429 "/\[img\](.*?)\[\/img\]/ism",
430 "/\[zmg\=[0-9]*x[0-9]*\](.*?)\[\/img\]/ism",
431 "/\[zmg\](.*?)\[\/zmg\]/ism",
432 "/\[video\](.*?)\[\/video\]/ism",
433 "/\[audio\](.*?)\[\/audio\]/ism",
436 foreach ($matches as $match) {
437 $body = preg_replace_callback(
439 function ($match) use ($basepath) {
440 return self::qualifyURLsSub($match, $basepath);
448 private static function breakLines(string $line, int $level, int $wraplength = 75): string
450 if ($wraplength == 0) {
451 $wraplength = 2000000;
454 $wraplen = $wraplength - $level;
461 $subline = substr($line, 0, $wraplen);
463 $pos = strrpos($subline, ' ');
466 $pos = strpos($line, ' ');
469 if (($pos > 0) && strlen($line) > $wraplen) {
470 $newline = trim(substr($line, 0, $pos));
472 $newline = str_repeat(">", $level) . ' ' . $newline;
475 $newlines[] = $newline . " ";
476 $line = substr($line, $pos + 1);
478 } while ((strlen($line) > $wraplen) && !($oldline == $line));
481 $line = str_repeat(">", $level) . ' ' . $line;
486 return implode("\n", $newlines);
489 private static function quoteLevel(string $message, int $wraplength = 75): string
491 $lines = explode("\n", $message);
495 foreach ($lines as $line) {
498 while (strpos("*" . $line, '[quote]') > 0) {
500 $pos = strpos($line, '[quote]');
501 $line = substr($line, 0, $pos) . substr($line, $pos + 7);
507 while (strpos("*" . $line, '[/quote]') > 0) {
513 $pos = strpos($line, '[/quote]');
514 $line = substr($line, 0, $pos) . substr($line, $pos + 8);
517 if (!$startquote || ($line != '')) {
518 $newlines[] = self::breakLines($line, $currlevel, $wraplength);
522 return implode("\n", $newlines);
525 private static function collectURLs(string $message): array
527 $pattern = '/<a.*?href="(.*?)".*?>(.*?)<\/a>/is';
528 preg_match_all($pattern, $message, $result, PREG_SET_ORDER);
531 foreach ($result as $treffer) {
534 // A list of some links that should be ignored
535 $list = ["/user/", "/tag/", "/group/", "/profile/", "/search?search=", "/search?tag=", "mailto:", "/u/", "/node/",
536 "//plus.google.com/", "//twitter.com/"];
537 foreach ($list as $listitem) {
538 if (strpos($treffer[1], $listitem) !== false) {
543 if ((strpos($treffer[1], "//twitter.com/") !== false) && (strpos($treffer[1], "/status/") !== false)) {
547 if ((strpos($treffer[1], "//plus.google.com/") !== false) && (strpos($treffer[1], "/posts") !== false)) {
551 if ((strpos($treffer[1], "//plus.google.com/") !== false) && (strpos($treffer[1], "/photos") !== false)) {
555 $ignore = $ignore || strpos($treffer[1], '#') === 0;
558 $urls[$treffer[1]] = $treffer[1];
566 * @param string $html
567 * @param int $wraplength Ensures individual lines aren't longer than this many characters. Doesn't break words.
568 * @param bool $compact True: Completely strips image tags; False: Keeps image URLs
571 public static function toPlaintext(string $html, int $wraplength = 75, bool $compact = false): string
573 DI::profiler()->startRecording('rendering');
574 $message = str_replace("\r", "", $html);
576 $doc = new DOMDocument();
577 $doc->preserveWhiteSpace = false;
579 $message = mb_convert_encoding($message, 'HTML-ENTITIES', "UTF-8");
581 if (empty($message)) {
582 DI::profiler()->stopRecording();
586 @$doc->loadHTML($message, LIBXML_HTML_NODEFDTD);
588 $message = $doc->saveHTML();
589 // Remove eventual UTF-8 BOM
590 $message = str_replace("\xC3\x82\xC2\xA0", "", $message);
592 // Collecting all links
593 $urls = self::collectURLs($message);
595 if (empty($message)) {
596 DI::profiler()->stopRecording();
600 @$doc->loadHTML($message, LIBXML_HTML_NODEFDTD);
602 self::tagToBBCode($doc, 'html', [], '', '');
603 self::tagToBBCode($doc, 'body', [], '', '');
606 self::tagToBBCode($doc, 'blockquote', [], "»", "«");
608 self::tagToBBCode($doc, 'blockquote', [], '[quote]', "[/quote]\n");
611 self::tagToBBCode($doc, 'br', [], "\n", '');
613 self::tagToBBCode($doc, 'span', [], "", "");
614 self::tagToBBCode($doc, 'pre', [], "", "");
615 self::tagToBBCode($doc, 'div', [], "\r", "\r");
616 self::tagToBBCode($doc, 'p', [], "\n", "\n");
618 self::tagToBBCode($doc, 'li', [], "\n* ", "\n");
620 self::tagToBBCode($doc, 'hr', [], "\n" . str_repeat("-", 70) . "\n", "");
622 self::tagToBBCode($doc, 'tr', [], "\n", "");
623 self::tagToBBCode($doc, 'td', [], "\t", "");
625 self::tagToBBCode($doc, 'h1', [], "\n\n*", "*\n");
626 self::tagToBBCode($doc, 'h2', [], "\n\n*", "*\n");
627 self::tagToBBCode($doc, 'h3', [], "\n\n*", "*\n");
628 self::tagToBBCode($doc, 'h4', [], "\n\n*", "*\n");
629 self::tagToBBCode($doc, 'h5', [], "\n\n*", "*\n");
630 self::tagToBBCode($doc, 'h6', [], "\n\n*", "*\n");
633 self::tagToBBCode($doc, 'img', ['src' => '/(.+)/'], ' [img]$1', '[/img] ');
635 self::tagToBBCode($doc, 'img', ['src' => '/(.+)/'], ' ', ' ');
638 // Backward compatibility, [iframe] support has been removed in version 2020.12
639 self::tagToBBCode($doc, 'iframe', ['src' => '/(.+)/'], ' $1 ', '');
641 $message = $doc->saveHTML();
644 $message = str_replace("[img]", "", $message);
645 $message = str_replace("[/img]", "", $message);
648 // was ersetze ich da?
649 // Irgendein stoerrisches UTF-Zeug
650 $message = str_replace(chr(194) . chr(160), ' ', $message);
652 $message = str_replace(" ", " ", $message);
654 // Aufeinanderfolgende DIVs
655 $message = preg_replace('=\r *\r=i', "\n", $message);
656 $message = str_replace("\r", "\n", $message);
658 $message = strip_tags($message);
660 $message = html_entity_decode($message, ENT_QUOTES, 'UTF-8');
662 if (!$compact && ($message != '')) {
663 foreach ($urls as $id => $url) {
664 if ($url != '' && strpos($message, $url) === false) {
665 $message .= "\n" . $url . ' ';
670 $message = str_replace("\n«", "«\n", $message);
671 $message = str_replace("»\n", "\n»", $message);
674 $oldmessage = $message;
675 $message = str_replace("\n\n\n", "\n\n", $message);
676 } while ($oldmessage != $message);
678 $message = self::quoteLevel(trim($message), $wraplength);
680 DI::profiler()->stopRecording();
681 return trim($message);
685 * Converts provided HTML code to Markdown. The hardwrap parameter maximizes
686 * compatibility with Diaspora in spite of the Markdown standards.
688 * @param string $html
691 public static function toMarkdown(string $html): string
693 DI::profiler()->startRecording('rendering');
694 $converter = new HtmlConverter(['hard_break' => true]);
695 $markdown = $converter->convert($html);
697 DI::profiler()->stopRecording();
702 * Convert video HTML to BBCode tags
707 public static function toBBCodeVideo(string $s): string
710 '#<object[^>]+>(.*?)https?://www.youtube.com/((?:v|cp)/[A-Za-z0-9\-_=]+)(.*?)</object>#ism',
711 '[youtube]$2[/youtube]',
716 '#<iframe[^>](.*?)https?://www.youtube.com/embed/([A-Za-z0-9\-_=]+)(.*?)</iframe>#ism',
717 '[youtube]$2[/youtube]',
722 '#<iframe[^>](.*?)https?://player.vimeo.com/video/([0-9]+)(.*?)</iframe>#ism',
731 * transform link href and img src from relative to absolute
733 * @param string $text
734 * @param string $base base url
737 public static function relToAbs(string $text, string $base): string
743 $base = rtrim($base, '/');
745 $base2 = $base . "/";
748 $pattern = "/<a([^>]*) href=\"(?!http|https|\/)([^\"]*)\"/";
749 $replace = "<a\${1} href=\"" . $base2 . "\${2}\"";
750 $text = preg_replace($pattern, $replace, $text);
752 $pattern = "/<a([^>]*) href=\"(?!http|https)([^\"]*)\"/";
753 $replace = "<a\${1} href=\"" . $base . "\${2}\"";
754 $text = preg_replace($pattern, $replace, $text);
757 $pattern = "/<img([^>]*) src=\"(?!http|https|\/)([^\"]*)\"/";
758 $replace = "<img\${1} src=\"" . $base2 . "\${2}\"";
759 $text = preg_replace($pattern, $replace, $text);
761 $pattern = "/<img([^>]*) src=\"(?!http|https)([^\"]*)\"/";
762 $replace = "<img\${1} src=\"" . $base . "\${2}\"";
763 $text = preg_replace($pattern, $replace, $text);
771 * Loader for infinite scrolling
773 * @return string html for loader
774 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
776 public static function scrollLoader(): string
778 $tpl = Renderer::getMarkupTemplate("scroll_loader.tpl");
779 return Renderer::replaceMacros($tpl, [
780 'wait' => DI::l10n()->t('Loading more entries...'),
781 'end' => DI::l10n()->t('The end')
786 * Format contacts as picture links or as text links
788 * @param array $contact Array with contacts which contains an array with
789 * int 'id' => The ID of the contact
790 * int 'uid' => The user ID of the user who owns this data
791 * string 'name' => The name of the contact
792 * string 'url' => The url to the profile page of the contact
793 * string 'addr' => The webbie of the contact (e.g.) username@friendica.com
794 * string 'network' => The network to which the contact belongs to
795 * string 'thumb' => The contact picture
796 * string 'click' => js code which is performed when clicking on the contact
797 * @param boolean $redirect If true try to use the redir url if it's possible
798 * @param string $class CSS class for the
799 * @param boolean $textmode If true display the contacts as text links
800 * if false display the contacts as picture links
801 * @return string Formatted html
802 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
803 * @throws \ImagickException
805 public static function micropro(array $contact, bool $redirect = false, string $class = '', bool $textmode = false): string
807 // Use the contact URL if no address is available
808 if (empty($contact['addr'])) {
809 $contact["addr"] = $contact["url"];
812 $url = $contact['url'];
817 $url = Contact::magicLinkByContact($contact);
818 if (strpos($url, 'contact/redir/') === 0) {
819 $sparkle = ' sparkle';
823 // If there is some js available we don't need the url
824 if (!empty($contact['click'])) {
828 return Renderer::replaceMacros(Renderer::getMarkupTemplate($textmode ? 'micropro_txt.tpl' : 'micropro_img.tpl'), [
829 '$click' => $contact['click'] ?? '',
832 '$photo' => Contact::getThumb($contact),
833 '$name' => $contact['name'],
834 'title' => $contact['name'] . ' [' . $contact['addr'] . ']',
835 '$parkle' => $sparkle,
843 * @param string $s Search query.
844 * @param string $id HTML id
845 * @param bool $aside Display the search widgit aside.
847 * @return string Formatted HTML.
850 public static function search(string $s, string $id = 'search-box', bool $aside = true): string
854 if (strpos($s, '#') === 0) {
857 $save_label = $mode === 'text' ? DI::l10n()->t('Save') : DI::l10n()->t('Follow');
861 '$q' => urlencode($s),
863 '$search_label' => DI::l10n()->t('Search'),
864 '$save_label' => $save_label,
865 '$search_hint' => DI::l10n()->t('@name, !forum, #tags, content'),
867 '$return_url' => urlencode(Search::getSearchPath($s)),
871 $values['$search_options'] = [
872 'fulltext' => DI::l10n()->t('Full Text'),
873 'tags' => DI::l10n()->t('Tags'),
874 'contacts' => DI::l10n()->t('Contacts')
877 if (DI::config()->get('system', 'poco_local_search')) {
878 $values['$searchoption']['forums'] = DI::l10n()->t('Forums');
882 return Renderer::replaceMacros(Renderer::getMarkupTemplate('searchbox.tpl'), $values);
886 * Given a HTML text and a set of filtering reasons, adds a content hiding header with the provided reasons
888 * Reasons are expected to have been translated already.
890 * @param string $html
891 * @param array $reasons
893 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
895 public static function applyContentFilter(string $html, array $reasons): string
897 if (count($reasons)) {
898 $tpl = Renderer::getMarkupTemplate('wall/content_filter.tpl');
899 $html = Renderer::replaceMacros($tpl, [
900 '$reasons' => $reasons,
901 '$rnd' => Strings::getRandomHex(8),
902 '$openclose' => DI::l10n()->t('Click to open/close'),
911 * replace html amp entity with amp char
915 public static function unamp(string $s): string
917 return str_replace('&', '&', $s);
921 * Clean an HTML text for potentially harmful code
923 * @param string $text
924 * @param array $allowedIframeDomains List of allowed iframe source domains without the scheme
927 public static function purify(string $text, array $allowedIframeDomains = []): string
929 // Allows cid: URL scheme
930 \HTMLPurifier_URISchemeRegistry::instance()->register('cid', new HTMLPurifier_URIScheme_cid());
932 $config = \HTMLPurifier_HTML5Config::createDefault();
933 $config->set('HTML.Doctype', 'HTML5');
935 // Used to remove iframe with src attribute filtered out
936 $config->set('AutoFormat.RemoveEmpty', true);
938 $config->set('HTML.SafeIframe', true);
940 array_walk($allowedIframeDomains, function (&$domain) {
941 // Allow the domain and all its eventual sub-domains
942 $domain = '(?:(?!-)[A-Za-z0-9-]{1,63}(?<!-)\.)*' . preg_quote(trim($domain, '/'), '%');
945 $config->set('URI.SafeIframeRegexp',
947 ' . implode('|', $allowedIframeDomains) . '
949 (?:/|$) # Prevents bogus domains like youtube.com.fake.tld
953 $config->set('Attr.AllowedRel', [
954 'noreferrer' => true,
958 $config->set('Attr.AllowedFrameTargets', [
962 $config->set('AutoFormat.RemoveEmpty.Predicate', [
963 'colgroup' => [], // |
966 'iframe' => ['src'], // ↳ Default HTMLPurify values
967 'i' => ['class'], // Allows forkawesome icons
970 // Uncomment to debug HTMLPurifier behavior
971 //$config->set('Core.CollectErrors', true);
972 //$config->set('Core.MaintainLineNumbers', true);
974 $HTMLPurifier = new \HTMLPurifier($config);
976 $text = $HTMLPurifier->purify($text);
978 /** @var \HTMLPurifier_ErrorCollector $errorCollector */
979 // Uncomment to debug HTML Purifier behavior
980 //$errorCollector = $HTMLPurifier->context->get('ErrorCollector');
981 //var_dump($errorCollector->getRaw());
987 * XPath arbitrary string quoting
989 * @see https://stackoverflow.com/a/45228168
990 * @param string $value
993 public static function xpathQuote(string $value): string
995 if (false === strpos($value, '"')) {
996 return '"' . $value . '"';
999 if (false === strpos($value, "'")) {
1000 return "'" . $value . "'";
1003 // if the value contains both single and double quotes, construct an
1004 // expression that concatenates all non-double-quote substrings with
1005 // the quotes, e.g.:
1007 // concat("'foo'", '"', "bar")
1008 return 'concat(' . implode(', \'"\', ', array_map([self::class, 'xpathQuote'], explode('"', $value))) . ')';
1012 * Checks if the provided URL is present in the DOM document in an element with the rel="me" attribute
1014 * XHTML Friends Network http://gmpg.org/xfn/
1016 * @param DOMDocument $doc
1017 * @param UriInterface $meUrl
1020 public static function checkRelMeLink(DOMDocument $doc, UriInterface $meUrl): bool
1022 $xpath = new \DOMXpath($doc);
1024 // This expression checks that "me" is among the space-delimited values of the "rel" attribute.
1025 // And that the href attribute contains exactly the provided URL
1026 $expression = "//*[contains(concat(' ', normalize-space(@rel), ' '), ' me ')][@href = " . self::xpathQuote($meUrl) . "]";
1028 $result = $xpath->query($expression);
1030 return $result !== false && $result->length > 0;
1034 * @param DOMDocument $doc
1035 * @return string|null Lowercase charset
1037 public static function extractCharset(DOMDocument $doc): ?string
1039 $xpath = new DOMXPath($doc);
1041 $expression = "string(//meta[@charset]/@charset)";
1042 if ($charset = $xpath->evaluate($expression)) {
1043 return strtolower($charset);
1047 // This expression looks for a meta tag with the http-equiv attribute set to "content-type" ignoring case
1048 // whose content attribute contains a "charset" string and returns its value
1049 $expression = "string(//meta[@http-equiv][translate(@http-equiv, 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz') = 'content-type'][contains(translate(@content, 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz'), 'charset')]/@content)";
1050 $mediaType = MediaType::fromContentType($xpath->evaluate($expression));
1051 if (isset($mediaType->parameters['charset'])) {
1052 return strtolower($mediaType->parameters['charset']);
1054 } catch(\InvalidArgumentException $e) {}