3 * @file src/Content/Text/HTML.php
6 namespace Friendica\Content\Text;
10 use Friendica\Content\Widget\ContactBlock;
11 use Friendica\Core\Hook;
12 use Friendica\Core\L10n;
13 use Friendica\Core\Config;
14 use Friendica\Core\Renderer;
15 use Friendica\Model\Contact;
16 use Friendica\Util\Network;
17 use Friendica\Util\Proxy as ProxyUtils;
18 use Friendica\Util\Strings;
19 use Friendica\Util\XML;
20 use League\HTMLToMarkdown\HtmlConverter;
24 public static function sanitizeCSS($input)
28 $input = strtolower($input);
30 for ($i = 0; $i < strlen($input); $i++) {
31 $char = substr($input, $i, 1);
33 if (($char >= "a") && ($char <= "z")) {
37 if (!(strpos(" #;:0123456789-_.%", $char) === false)) {
45 private static function tagToBBCode(DOMDocument $doc, $tag, $attributes, $startbb, $endbb)
48 $done = self::tagToBBCodeSub($doc, $tag, $attributes, $startbb, $endbb);
52 private static function tagToBBCodeSub(DOMDocument $doc, $tag, $attributes, $startbb, $endbb)
54 $savestart = str_replace('$', '\x01', $startbb);
57 $xpath = new DOMXPath($doc);
59 $list = $xpath->query("//" . $tag);
60 foreach ($list as $node) {
62 if ($node->attributes->length) {
63 foreach ($node->attributes as $attribute) {
64 $attr[$attribute->name] = $attribute->value;
70 $startbb = $savestart;
74 foreach ($attributes as $attribute => $value) {
75 $startbb = str_replace('\x01' . ++$i, '$1', $startbb);
76 if (strpos('*' . $startbb, '$1') > 0) {
77 if ($replace && (@$attr[$attribute] != '')) {
78 $startbb = preg_replace($value, $startbb, $attr[$attribute], -1, $count);
80 // If nothing could be changed
88 if (@$attr[$attribute] != $value) {
95 $StartCode = $doc->createTextNode($startbb);
96 $EndCode = $doc->createTextNode($endbb);
98 $node->parentNode->insertBefore($StartCode, $node);
100 if ($node->hasChildNodes()) {
101 foreach ($node->childNodes as $child) {
102 $newNode = $child->cloneNode(true);
103 $node->parentNode->insertBefore($newNode, $node);
107 $node->parentNode->insertBefore($EndCode, $node);
108 $node->parentNode->removeChild($node);
116 * Made by: ike@piratenpartei.de
117 * Originally made for the syncom project: http://wiki.piratenpartei.de/Syncom
118 * https://github.com/annando/Syncom
120 * @brief Converter for HTML to BBCode
121 * @param string $message
122 * @param string $basepath
124 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
126 public static function toBBCode($message, $basepath = '')
128 $message = str_replace("\r", "", $message);
130 // Removing code blocks before the whitespace removal processing below
132 $message = preg_replace_callback(
133 '#<pre><code(?: class="language-([^"]*)")?>(.*)</code></pre>#iUs',
134 function ($matches) use (&$codeblocks) {
135 $return = '[codeblock-' . count($codeblocks) . ']';
138 if ($matches[1] != '') {
139 $prefix = '[code=' . $matches[1] . ']';
142 $codeblocks[] = $prefix . PHP_EOL . trim($matches[2]) . PHP_EOL . '[/code]';
148 $message = str_replace(
161 $message = preg_replace('=<(\w+):(.+?)>=', '<removeme>', $message);
162 $message = preg_replace('=</(\w+):(.+?)>=', '</removeme>', $message);
164 $doc = new DOMDocument();
165 $doc->preserveWhiteSpace = false;
167 $message = mb_convert_encoding($message, 'HTML-ENTITIES', "UTF-8");
169 @$doc->loadHTML($message);
171 XML::deleteNode($doc, 'style');
172 XML::deleteNode($doc, 'head');
173 XML::deleteNode($doc, 'title');
174 XML::deleteNode($doc, 'meta');
175 XML::deleteNode($doc, 'xml');
176 XML::deleteNode($doc, 'removeme');
178 $xpath = new DomXPath($doc);
179 $list = $xpath->query("//pre");
180 foreach ($list as $node) {
181 // Ensure to escape unescaped & - they will otherwise raise a warning
182 $safe_value = preg_replace('/&(?!\w+;)/', '&', $node->nodeValue);
183 $node->nodeValue = str_replace("\n", "\r", $safe_value);
186 $message = $doc->saveHTML();
187 $message = str_replace(["\n<", ">\n", "\r", "\n", "\xC3\x82\xC2\xA0"], ["<", ">", "<br />", " ", ""], $message);
188 $message = preg_replace('= [\s]*=i', " ", $message);
189 @$doc->loadHTML($message);
191 self::tagToBBCode($doc, 'html', [], "", "");
192 self::tagToBBCode($doc, 'body', [], "", "");
194 // Outlook-Quote - Variant 1
195 self::tagToBBCode($doc, 'p', ['class' => 'MsoNormal', 'style' => 'margin-left:35.4pt'], '[quote]', '[/quote]');
197 // Outlook-Quote - Variant 2
201 ['style' => 'border:none;border-left:solid blue 1.5pt;padding:0cm 0cm 0cm 4.0pt'],
207 self::tagToBBCode($doc, 'span', ['style' => 'text-decoration: underline;'], '[u]', '[/u]');
208 self::tagToBBCode($doc, 'span', ['style' => 'font-style: italic;'], '[i]', '[/i]');
209 self::tagToBBCode($doc, 'span', ['style' => 'font-weight: bold;'], '[b]', '[/b]');
211 /* self::node2BBCode($doc, 'font', array('face'=>'/([\w ]+)/', 'size'=>'/(\d+)/', 'color'=>'/(.+)/'), '[font=$1][size=$2][color=$3]', '[/color][/size][/font]');
212 self::node2BBCode($doc, 'font', array('size'=>'/(\d+)/', 'color'=>'/(.+)/'), '[size=$1][color=$2]', '[/color][/size]');
213 self::node2BBCode($doc, 'font', array('face'=>'/([\w ]+)/', 'size'=>'/(.+)/'), '[font=$1][size=$2]', '[/size][/font]');
214 self::node2BBCode($doc, 'font', array('face'=>'/([\w ]+)/', 'color'=>'/(.+)/'), '[font=$1][color=$3]', '[/color][/font]');
215 self::node2BBCode($doc, 'font', array('face'=>'/([\w ]+)/'), '[font=$1]', '[/font]');
216 self::node2BBCode($doc, 'font', array('size'=>'/(\d+)/'), '[size=$1]', '[/size]');
217 self::node2BBCode($doc, 'font', array('color'=>'/(.+)/'), '[color=$1]', '[/color]');
220 //self::node2BBCode($doc, 'span', array('style'=>'/.*font-size:\s*(.+?)[,;].*font-family:\s*(.+?)[,;].*color:\s*(.+?)[,;].*/'), '[size=$1][font=$2][color=$3]', '[/color][/font][/size]');
221 //self::node2BBCode($doc, 'span', array('style'=>'/.*font-size:\s*(\d+)[,;].*/'), '[size=$1]', '[/size]');
222 //self::node2BBCode($doc, 'span', array('style'=>'/.*font-size:\s*(.+?)[,;].*/'), '[size=$1]', '[/size]');
224 self::tagToBBCode($doc, 'span', ['style' => '/.*color:\s*(.+?)[,;].*/'], '[color="$1"]', '[/color]');
226 //self::node2BBCode($doc, 'span', array('style'=>'/.*font-family:\s*(.+?)[,;].*/'), '[font=$1]', '[/font]');
227 //self::node2BBCode($doc, 'div', array('style'=>'/.*font-family:\s*(.+?)[,;].*font-size:\s*(\d+?)pt.*/'), '[font=$1][size=$2]', '[/size][/font]');
228 //self::node2BBCode($doc, 'div', array('style'=>'/.*font-family:\s*(.+?)[,;].*font-size:\s*(\d+?)px.*/'), '[font=$1][size=$2]', '[/size][/font]');
229 //self::node2BBCode($doc, 'div', array('style'=>'/.*font-family:\s*(.+?)[,;].*/'), '[font=$1]', '[/font]');
230 // Importing the classes - interesting for importing of posts from third party networks that were exported from friendica
232 //self::node2BBCode($doc, 'span', array('class'=>'/([\w ]+)/'), '[class=$1]', '[/class]');
233 self::tagToBBCode($doc, 'span', ['class' => 'type-link'], '[class=type-link]', '[/class]');
234 self::tagToBBCode($doc, 'span', ['class' => 'type-video'], '[class=type-video]', '[/class]');
236 self::tagToBBCode($doc, 'strong', [], '[b]', '[/b]');
237 self::tagToBBCode($doc, 'em', [], '[i]', '[/i]');
238 self::tagToBBCode($doc, 'b', [], '[b]', '[/b]');
239 self::tagToBBCode($doc, 'i', [], '[i]', '[/i]');
240 self::tagToBBCode($doc, 'u', [], '[u]', '[/u]');
241 self::tagToBBCode($doc, 's', [], '[s]', '[/s]');
242 self::tagToBBCode($doc, 'del', [], '[s]', '[/s]');
243 self::tagToBBCode($doc, 'strike', [], '[s]', '[/s]');
245 self::tagToBBCode($doc, 'big', [], "[size=large]", "[/size]");
246 self::tagToBBCode($doc, 'small', [], "[size=small]", "[/size]");
248 self::tagToBBCode($doc, 'blockquote', [], '[quote]', '[/quote]');
250 self::tagToBBCode($doc, 'br', [], "\n", '');
252 self::tagToBBCode($doc, 'p', ['class' => 'MsoNormal'], "\n", "");
253 self::tagToBBCode($doc, 'div', ['class' => 'MsoNormal'], "\r", "");
255 self::tagToBBCode($doc, 'span', [], "", "");
257 self::tagToBBCode($doc, 'span', [], "", "");
258 self::tagToBBCode($doc, 'pre', [], "", "");
260 self::tagToBBCode($doc, 'div', [], "\r", "\r");
261 self::tagToBBCode($doc, 'p', [], "\n", "\n");
263 self::tagToBBCode($doc, 'ul', [], "[list]", "[/list]");
264 self::tagToBBCode($doc, 'ol', [], "[list=1]", "[/list]");
265 self::tagToBBCode($doc, 'li', [], "[*]", "");
267 self::tagToBBCode($doc, 'hr', [], "[hr]", "");
269 self::tagToBBCode($doc, 'table', [], "", "");
270 self::tagToBBCode($doc, 'tr', [], "\n", "");
271 self::tagToBBCode($doc, 'td', [], "\t", "");
272 //self::node2BBCode($doc, 'table', array(), "[table]", "[/table]");
273 //self::node2BBCode($doc, 'th', array(), "[th]", "[/th]");
274 //self::node2BBCode($doc, 'tr', array(), "[tr]", "[/tr]");
275 //self::node2BBCode($doc, 'td', array(), "[td]", "[/td]");
276 //self::node2BBCode($doc, 'h1', array(), "\n\n[size=xx-large][b]", "[/b][/size]\n");
277 //self::node2BBCode($doc, 'h2', array(), "\n\n[size=x-large][b]", "[/b][/size]\n");
278 //self::node2BBCode($doc, 'h3', array(), "\n\n[size=large][b]", "[/b][/size]\n");
279 //self::node2BBCode($doc, 'h4', array(), "\n\n[size=medium][b]", "[/b][/size]\n");
280 //self::node2BBCode($doc, 'h5', array(), "\n\n[size=small][b]", "[/b][/size]\n");
281 //self::node2BBCode($doc, 'h6', array(), "\n\n[size=x-small][b]", "[/b][/size]\n");
283 self::tagToBBCode($doc, 'h1', [], "[h1]", "[/h1]");
284 self::tagToBBCode($doc, 'h2', [], "[h2]", "[/h2]");
285 self::tagToBBCode($doc, 'h3', [], "[h3]", "[/h3]");
286 self::tagToBBCode($doc, 'h4', [], "[h4]", "[/h4]");
287 self::tagToBBCode($doc, 'h5', [], "[h5]", "[/h5]");
288 self::tagToBBCode($doc, 'h6', [], "[h6]", "[/h6]");
290 self::tagToBBCode($doc, 'a', ['href' => '/mailto:(.+)/'], '[mail=$1]', '[/mail]');
291 self::tagToBBCode($doc, 'a', ['href' => '/(.+)/'], '[url=$1]', '[/url]');
293 self::tagToBBCode($doc, 'img', ['src' => '/(.+)/', 'alt' => '/(.+)/'], '[img=$1]$2', '[/img]');
294 self::tagToBBCode($doc, 'img', ['src' => '/(.+)/', 'width' => '/(\d+)/', 'height' => '/(\d+)/'], '[img=$2x$3]$1', '[/img]');
295 self::tagToBBCode($doc, 'img', ['src' => '/(.+)/'], '[img]$1', '[/img]');
298 self::tagToBBCode($doc, 'video', ['src' => '/(.+)/'], '[video]$1', '[/video]');
299 self::tagToBBCode($doc, 'audio', ['src' => '/(.+)/'], '[audio]$1', '[/audio]');
300 self::tagToBBCode($doc, 'iframe', ['src' => '/(.+)/'], '[iframe]$1', '[/iframe]');
302 self::tagToBBCode($doc, 'key', [], '[code]', '[/code]');
303 self::tagToBBCode($doc, 'code', [], '[code]', '[/code]');
305 $message = $doc->saveHTML();
307 // I'm removing something really disturbing
308 // Don't know exactly what it is
309 $message = str_replace(chr(194) . chr(160), ' ', $message);
311 $message = str_replace(" ", " ", $message);
313 // removing multiple DIVs
314 $message = preg_replace('=\r *\r=i', "\n", $message);
315 $message = str_replace("\r", "\n", $message);
317 Hook::callAll('html2bbcode', $message);
319 $message = strip_tags($message);
321 $message = html_entity_decode($message, ENT_QUOTES, 'UTF-8');
323 $message = str_replace(["<"], ["<"], $message);
325 // remove quotes if they don't make sense
326 $message = preg_replace('=\[/quote\][\s]*\[quote\]=i', "\n", $message);
328 $message = preg_replace('=\[quote\]\s*=i', "[quote]", $message);
329 $message = preg_replace('=\s*\[/quote\]=i', "[/quote]", $message);
332 $oldmessage = $message;
333 $message = str_replace("\n \n", "\n\n", $message);
334 } while ($oldmessage != $message);
337 $oldmessage = $message;
338 $message = str_replace("\n\n\n", "\n\n", $message);
339 } while ($oldmessage != $message);
342 $oldmessage = $message;
343 $message = str_replace(
366 } while ($message != $oldmessage);
368 $message = str_replace(
369 ['[b][b]', '[/b][/b]', '[i][i]', '[/i][/i]'],
370 ['[b]', '[/b]', '[i]', '[/i]'],
374 // Handling Yahoo style of mails
375 $message = str_replace('[hr][b]From:[/b]', '[quote][b]From:[/b]', $message);
377 // Restore code blocks
378 $message = preg_replace_callback(
379 '#\[codeblock-([0-9]+)\]#iU',
380 function ($matches) use ($codeblocks) {
382 if (isset($codeblocks[intval($matches[1])])) {
383 $return = $codeblocks[$matches[1]];
390 $message = trim($message);
392 if ($basepath != '') {
393 $message = self::qualifyURLs($message, $basepath);
400 * @brief Sub function to complete incomplete URL
402 * @param array $matches Result of preg_replace_callback
403 * @param string $basepath Basepath that is used to complete the URL
405 * @return string The expanded URL
407 private static function qualifyURLsSub($matches, $basepath)
409 $base = parse_url($basepath);
410 unset($base['query']);
411 unset($base['fragment']);
416 $parts = array_merge($base, parse_url($url));
417 $url2 = Network::unparseURL($parts);
419 return str_replace($url, $url2, $link);
423 * @brief Complete incomplete URLs in BBCode
425 * @param string $body Body with URLs
426 * @param string $basepath Base path that is used to complete the URL
428 * @return string Body with expanded URLs
430 private static function qualifyURLs($body, $basepath)
432 $URLSearchString = "^\[\]";
434 $matches = ["/\[url\=([$URLSearchString]*)\].*?\[\/url\]/ism",
435 "/\[url\]([$URLSearchString]*)\[\/url\]/ism",
436 "/\[img\=[0-9]*x[0-9]*\](.*?)\[\/img\]/ism",
437 "/\[img\](.*?)\[\/img\]/ism",
438 "/\[zmg\=[0-9]*x[0-9]*\](.*?)\[\/img\]/ism",
439 "/\[zmg\](.*?)\[\/zmg\]/ism",
440 "/\[video\](.*?)\[\/video\]/ism",
441 "/\[audio\](.*?)\[\/audio\]/ism",
444 foreach ($matches as $match) {
445 $body = preg_replace_callback(
447 function ($match) use ($basepath) {
448 return self::qualifyURLsSub($match, $basepath);
456 private static function breakLines($line, $level, $wraplength = 75)
458 if ($wraplength == 0) {
459 $wraplength = 2000000;
462 $wraplen = $wraplength - $level;
469 $subline = substr($line, 0, $wraplen);
471 $pos = strrpos($subline, ' ');
474 $pos = strpos($line, ' ');
477 if (($pos > 0) && strlen($line) > $wraplen) {
478 $newline = trim(substr($line, 0, $pos));
480 $newline = str_repeat(">", $level) . ' ' . $newline;
483 $newlines[] = $newline . " ";
484 $line = substr($line, $pos + 1);
486 } while ((strlen($line) > $wraplen) && !($oldline == $line));
489 $line = str_repeat(">", $level) . ' ' . $line;
494 return implode($newlines, "\n");
497 private static function quoteLevel($message, $wraplength = 75)
499 $lines = explode("\n", $message);
503 foreach ($lines as $line) {
506 while (strpos("*" . $line, '[quote]') > 0) {
508 $pos = strpos($line, '[quote]');
509 $line = substr($line, 0, $pos) . substr($line, $pos + 7);
515 while (strpos("*" . $line, '[/quote]') > 0) {
521 $pos = strpos($line, '[/quote]');
522 $line = substr($line, 0, $pos) . substr($line, $pos + 8);
525 if (!$startquote || ($line != '')) {
526 $newlines[] = self::breakLines($line, $currlevel, $wraplength);
530 return implode($newlines, "\n");
533 private static function collectURLs($message)
535 $pattern = '/<a.*?href="(.*?)".*?>(.*?)<\/a>/is';
536 preg_match_all($pattern, $message, $result, PREG_SET_ORDER);
539 foreach ($result as $treffer) {
542 // A list of some links that should be ignored
543 $list = ["/user/", "/tag/", "/group/", "/profile/", "/search?search=", "/search?tag=", "mailto:", "/u/", "/node/",
544 "//plus.google.com/", "//twitter.com/"];
545 foreach ($list as $listitem) {
546 if (strpos($treffer[1], $listitem) !== false) {
551 if ((strpos($treffer[1], "//twitter.com/") !== false) && (strpos($treffer[1], "/status/") !== false)) {
555 if ((strpos($treffer[1], "//plus.google.com/") !== false) && (strpos($treffer[1], "/posts") !== false)) {
559 if ((strpos($treffer[1], "//plus.google.com/") !== false) && (strpos($treffer[1], "/photos") !== false)) {
564 $urls[$treffer[1]] = $treffer[1];
572 * @param string $html
573 * @param int $wraplength Ensures individual lines aren't longer than this many characters. Doesn't break words.
574 * @param bool $compact True: Completely strips image tags; False: Keeps image URLs
577 public static function toPlaintext(string $html, $wraplength = 75, $compact = false)
579 $message = str_replace("\r", "", $html);
581 $doc = new DOMDocument();
582 $doc->preserveWhiteSpace = false;
584 $message = mb_convert_encoding($message, 'HTML-ENTITIES', "UTF-8");
586 @$doc->loadHTML($message);
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 @$doc->loadHTML($message);
597 self::tagToBBCode($doc, 'html', [], '', '');
598 self::tagToBBCode($doc, 'body', [], '', '');
601 self::tagToBBCode($doc, 'blockquote', [], "»", "«");
603 self::tagToBBCode($doc, 'blockquote', [], '[quote]', "[/quote]\n");
606 self::tagToBBCode($doc, 'br', [], "\n", '');
608 self::tagToBBCode($doc, 'span', [], "", "");
609 self::tagToBBCode($doc, 'pre', [], "", "");
610 self::tagToBBCode($doc, 'div', [], "\r", "\r");
611 self::tagToBBCode($doc, 'p', [], "\n", "\n");
613 self::tagToBBCode($doc, 'li', [], "\n* ", "\n");
615 self::tagToBBCode($doc, 'hr', [], "\n" . str_repeat("-", 70) . "\n", "");
617 self::tagToBBCode($doc, 'tr', [], "\n", "");
618 self::tagToBBCode($doc, 'td', [], "\t", "");
620 self::tagToBBCode($doc, 'h1', [], "\n\n*", "*\n");
621 self::tagToBBCode($doc, 'h2', [], "\n\n*", "*\n");
622 self::tagToBBCode($doc, 'h3', [], "\n\n*", "*\n");
623 self::tagToBBCode($doc, 'h4', [], "\n\n*", "*\n");
624 self::tagToBBCode($doc, 'h5', [], "\n\n*", "*\n");
625 self::tagToBBCode($doc, 'h6', [], "\n\n*", "*\n");
628 self::tagToBBCode($doc, 'img', ['src' => '/(.+)/'], ' [img]$1', '[/img] ');
630 self::tagToBBCode($doc, 'img', ['src' => '/(.+)/'], ' ', ' ');
633 self::tagToBBCode($doc, 'iframe', ['src' => '/(.+)/'], ' $1 ', '');
635 $message = $doc->saveHTML();
638 $message = str_replace("[img]", "", $message);
639 $message = str_replace("[/img]", "", $message);
642 // was ersetze ich da?
643 // Irgendein stoerrisches UTF-Zeug
644 $message = str_replace(chr(194) . chr(160), ' ', $message);
646 $message = str_replace(" ", " ", $message);
648 // Aufeinanderfolgende DIVs
649 $message = preg_replace('=\r *\r=i', "\n", $message);
650 $message = str_replace("\r", "\n", $message);
652 $message = strip_tags($message);
654 $message = html_entity_decode($message, ENT_QUOTES, 'UTF-8');
656 if (!$compact && ($message != '')) {
657 foreach ($urls as $id => $url) {
658 if ($url != '' && strpos($message, $url) === false) {
659 $message .= "\n" . $url . ' ';
664 $message = str_replace("\n«", "«\n", $message);
665 $message = str_replace("»\n", "\n»", $message);
668 $oldmessage = $message;
669 $message = str_replace("\n\n\n", "\n\n", $message);
670 } while ($oldmessage != $message);
672 $message = self::quoteLevel(trim($message), $wraplength);
674 return trim($message);
678 * Converts provided HTML code to Markdown. The hardwrap parameter maximizes
679 * compatibility with Diaspora in spite of the Markdown standards.
681 * @param string $html
684 public static function toMarkdown($html)
686 $converter = new HtmlConverter(['hard_break' => true]);
687 $markdown = $converter->convert($html);
693 * @brief Convert video HTML to BBCode tags
698 public static function toBBCodeVideo($s)
701 '#<object[^>]+>(.*?)https?://www.youtube.com/((?:v|cp)/[A-Za-z0-9\-_=]+)(.*?)</object>#ism',
702 '[youtube]$2[/youtube]',
707 '#<iframe[^>](.*?)https?://www.youtube.com/embed/([A-Za-z0-9\-_=]+)(.*?)</iframe>#ism',
708 '[youtube]$2[/youtube]',
713 '#<iframe[^>](.*?)https?://player.vimeo.com/video/([0-9]+)(.*?)</iframe>#ism',
722 * transform link href and img src from relative to absolute
724 * @param string $text
725 * @param string $base base url
728 public static function relToAbs($text, $base)
734 $base = rtrim($base, '/');
736 $base2 = $base . "/";
739 $pattern = "/<a([^>]*) href=\"(?!http|https|\/)([^\"]*)\"/";
740 $replace = "<a\${1} href=\"" . $base2 . "\${2}\"";
741 $text = preg_replace($pattern, $replace, $text);
743 $pattern = "/<a([^>]*) href=\"(?!http|https)([^\"]*)\"/";
744 $replace = "<a\${1} href=\"" . $base . "\${2}\"";
745 $text = preg_replace($pattern, $replace, $text);
748 $pattern = "/<img([^>]*) src=\"(?!http|https|\/)([^\"]*)\"/";
749 $replace = "<img\${1} src=\"" . $base2 . "\${2}\"";
750 $text = preg_replace($pattern, $replace, $text);
752 $pattern = "/<img([^>]*) src=\"(?!http|https)([^\"]*)\"/";
753 $replace = "<img\${1} src=\"" . $base . "\${2}\"";
754 $text = preg_replace($pattern, $replace, $text);
762 * return div element with class 'clear'
766 public static function clearDiv()
768 return '<div class="clear"></div>';
772 * Loader for infinite scrolling
774 * @return string html for loader
775 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
777 public static function scrollLoader()
779 $tpl = Renderer::getMarkupTemplate("scroll_loader.tpl");
780 return Renderer::replaceMacros($tpl, [
781 'wait' => L10n::t('Loading more entries...'),
782 'end' => L10n::t('The end')
787 * Get html for contact block.
789 * @deprecated since version 2019.03
790 * @see ContactBlock::getHTML()
792 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
793 * @throws \ImagickException
795 public static function contactBlock()
799 return ContactBlock::getHTML($a->profile);
803 * @brief 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::magicLink($contact['url']);
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' => defaults($contact, 'click', ''),
849 '$photo' => ProxyUtils::proxifyUrl($contact['thumb'], false, ProxyUtils::SIZE_THUMB),
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.
866 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
868 public static function search($s, $id = 'search-box', $url = 'search', $aside = true)
872 if (strpos($s, '#') === 0) {
875 $save_label = $mode === 'text' ? L10n::t('Save') : L10n::t('Follow');
880 '$action_url' => $url,
881 '$search_label' => L10n::t('Search'),
882 '$save_label' => $save_label,
883 '$savedsearch' => 'savedsearch',
884 '$search_hint' => L10n::t('@name, !forum, #tags, content'),
889 $values['$searchoption'] = [
890 L10n::t("Full Text"),
892 L10n::t("Contacts")];
894 if (Config::get('system', 'poco_local_search')) {
895 $values['$searchoption'][] = L10n::t("Forums");
899 return Renderer::replaceMacros(Renderer::getMarkupTemplate('searchbox.tpl'), $values);
903 * Replace naked text hyperlink with HTML formatted hyperlink
908 public static function toLink($s)
910 $s = preg_replace("/(https?\:\/\/[a-zA-Z0-9\:\/\-\?\&\;\.\=\_\~\#\'\%\$\!\+]*)/", ' <a href="$1" target="_blank">$1</a>', $s);
911 $s = preg_replace("/\<(.*?)(src|href)=(.*?)\&\;(.*?)\>/ism", '<$1$2=$3&$4>', $s);
916 * Given a HTML text and a set of filtering reasons, adds a content hiding header with the provided reasons
918 * Reasons are expected to have been translated already.
920 * @param string $html
921 * @param array $reasons
923 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
925 public static function applyContentFilter($html, array $reasons)
927 if (count($reasons)) {
928 $tpl = Renderer::getMarkupTemplate('wall/content_filter.tpl');
929 $html = Renderer::replaceMacros($tpl, [
930 '$reasons' => $reasons,
931 '$rnd' => Strings::getRandomHex(8),
932 '$openclose' => L10n::t('Click to open/close'),
941 * replace html amp entity with amp char
945 public static function unamp($s)
947 return str_replace('&', '&', $s);