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 /** @var \DOMNode[] $list */
60 $list = $xpath->query("//" . $tag);
61 foreach ($list as $node) {
63 if ($node->attributes->length) {
64 foreach ($node->attributes as $attribute) {
65 $attr[$attribute->name] = $attribute->value;
71 $startbb = $savestart;
75 foreach ($attributes as $attribute => $value) {
76 $startbb = str_replace('\x01' . ++$i, '$1', $startbb);
77 if (strpos('*' . $startbb, '$1') > 0) {
78 if ($replace && (@$attr[$attribute] != '')) {
79 $startbb = preg_replace($value, $startbb, $attr[$attribute], -1, $count);
81 // If nothing could be changed
89 if (@$attr[$attribute] != $value) {
96 $StartCode = $doc->createTextNode($startbb);
97 $EndCode = $doc->createTextNode($endbb);
99 $node->parentNode->insertBefore($StartCode, $node);
101 if ($node->hasChildNodes()) {
102 /** @var \DOMNode $child */
103 foreach ($node->childNodes as $key => $child) {
104 /* Remove empty text nodes at the start or at the end of the children list */
105 if ($key > 0 && $key < count($node->childNodes) -1 || trim($child->nodeValue)) {
106 $newNode = $child->cloneNode(true);
107 $node->parentNode->insertBefore($newNode, $node);
112 $node->parentNode->insertBefore($EndCode, $node);
113 $node->parentNode->removeChild($node);
121 * Made by: ike@piratenpartei.de
122 * Originally made for the syncom project: http://wiki.piratenpartei.de/Syncom
123 * https://github.com/annando/Syncom
125 * @brief Converter for HTML to BBCode
126 * @param string $message
127 * @param string $basepath
129 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
131 public static function toBBCode($message, $basepath = '')
133 $message = str_replace("\r", "", $message);
135 // Removing code blocks before the whitespace removal processing below
137 $message = preg_replace_callback(
138 '#<pre><code(?: class="language-([^"]*)")?>(.*)</code></pre>#iUs',
139 function ($matches) use (&$codeblocks) {
140 $return = '[codeblock-' . count($codeblocks) . ']';
143 if ($matches[1] != '') {
144 $prefix = '[code=' . $matches[1] . ']';
147 $codeblocks[] = $prefix . PHP_EOL . trim($matches[2]) . PHP_EOL . '[/code]';
153 $message = str_replace(
166 $message = preg_replace('=<(\w+):(.+?)>=', '<removeme>', $message);
167 $message = preg_replace('=</(\w+):(.+?)>=', '</removeme>', $message);
169 $doc = new DOMDocument();
170 $doc->preserveWhiteSpace = false;
172 $message = mb_convert_encoding($message, 'HTML-ENTITIES', "UTF-8");
174 @$doc->loadHTML($message, LIBXML_HTML_NODEFDTD);
176 XML::deleteNode($doc, 'style');
177 XML::deleteNode($doc, 'head');
178 XML::deleteNode($doc, 'title');
179 XML::deleteNode($doc, 'meta');
180 XML::deleteNode($doc, 'xml');
181 XML::deleteNode($doc, 'removeme');
183 $xpath = new DomXPath($doc);
184 $list = $xpath->query("//pre");
185 foreach ($list as $node) {
186 // Ensure to escape unescaped & - they will otherwise raise a warning
187 $safe_value = preg_replace('/&(?!\w+;)/', '&', $node->nodeValue);
188 $node->nodeValue = str_replace("\n", "\r", $safe_value);
191 $message = $doc->saveHTML();
192 $message = str_replace(["\n<", ">\n", "\r", "\n", "\xC3\x82\xC2\xA0"], ["<", ">", "<br />", " ", ""], $message);
193 $message = preg_replace('= [\s]*=i', " ", $message);
195 @$doc->loadHTML($message, LIBXML_HTML_NODEFDTD);
197 self::tagToBBCode($doc, 'html', [], "", "");
198 self::tagToBBCode($doc, 'body', [], "", "");
200 // Outlook-Quote - Variant 1
201 self::tagToBBCode($doc, 'p', ['class' => 'MsoNormal', 'style' => 'margin-left:35.4pt'], '[quote]', '[/quote]');
203 // Outlook-Quote - Variant 2
207 ['style' => 'border:none;border-left:solid blue 1.5pt;padding:0cm 0cm 0cm 4.0pt'],
213 self::tagToBBCode($doc, 'span', ['style' => 'text-decoration: underline;'], '[u]', '[/u]');
214 self::tagToBBCode($doc, 'span', ['style' => 'font-style: italic;'], '[i]', '[/i]');
215 self::tagToBBCode($doc, 'span', ['style' => 'font-weight: bold;'], '[b]', '[/b]');
217 /* self::node2BBCode($doc, 'font', array('face'=>'/([\w ]+)/', 'size'=>'/(\d+)/', 'color'=>'/(.+)/'), '[font=$1][size=$2][color=$3]', '[/color][/size][/font]');
218 self::node2BBCode($doc, 'font', array('size'=>'/(\d+)/', 'color'=>'/(.+)/'), '[size=$1][color=$2]', '[/color][/size]');
219 self::node2BBCode($doc, 'font', array('face'=>'/([\w ]+)/', 'size'=>'/(.+)/'), '[font=$1][size=$2]', '[/size][/font]');
220 self::node2BBCode($doc, 'font', array('face'=>'/([\w ]+)/', 'color'=>'/(.+)/'), '[font=$1][color=$3]', '[/color][/font]');
221 self::node2BBCode($doc, 'font', array('face'=>'/([\w ]+)/'), '[font=$1]', '[/font]');
222 self::node2BBCode($doc, 'font', array('size'=>'/(\d+)/'), '[size=$1]', '[/size]');
223 self::node2BBCode($doc, 'font', array('color'=>'/(.+)/'), '[color=$1]', '[/color]');
226 //self::node2BBCode($doc, 'span', array('style'=>'/.*font-size:\s*(.+?)[,;].*font-family:\s*(.+?)[,;].*color:\s*(.+?)[,;].*/'), '[size=$1][font=$2][color=$3]', '[/color][/font][/size]');
227 //self::node2BBCode($doc, 'span', array('style'=>'/.*font-size:\s*(\d+)[,;].*/'), '[size=$1]', '[/size]');
228 //self::node2BBCode($doc, 'span', array('style'=>'/.*font-size:\s*(.+?)[,;].*/'), '[size=$1]', '[/size]');
230 self::tagToBBCode($doc, 'span', ['style' => '/.*color:\s*(.+?)[,;].*/'], '[color="$1"]', '[/color]');
232 //self::node2BBCode($doc, 'span', array('style'=>'/.*font-family:\s*(.+?)[,;].*/'), '[font=$1]', '[/font]');
233 //self::node2BBCode($doc, 'div', array('style'=>'/.*font-family:\s*(.+?)[,;].*font-size:\s*(\d+?)pt.*/'), '[font=$1][size=$2]', '[/size][/font]');
234 //self::node2BBCode($doc, 'div', array('style'=>'/.*font-family:\s*(.+?)[,;].*font-size:\s*(\d+?)px.*/'), '[font=$1][size=$2]', '[/size][/font]');
235 //self::node2BBCode($doc, 'div', array('style'=>'/.*font-family:\s*(.+?)[,;].*/'), '[font=$1]', '[/font]');
236 // Importing the classes - interesting for importing of posts from third party networks that were exported from friendica
238 //self::node2BBCode($doc, 'span', array('class'=>'/([\w ]+)/'), '[class=$1]', '[/class]');
239 self::tagToBBCode($doc, 'span', ['class' => 'type-link'], '[class=type-link]', '[/class]');
240 self::tagToBBCode($doc, 'span', ['class' => 'type-video'], '[class=type-video]', '[/class]');
242 self::tagToBBCode($doc, 'strong', [], '[b]', '[/b]');
243 self::tagToBBCode($doc, 'em', [], '[i]', '[/i]');
244 self::tagToBBCode($doc, 'b', [], '[b]', '[/b]');
245 self::tagToBBCode($doc, 'i', [], '[i]', '[/i]');
246 self::tagToBBCode($doc, 'u', [], '[u]', '[/u]');
247 self::tagToBBCode($doc, 's', [], '[s]', '[/s]');
248 self::tagToBBCode($doc, 'del', [], '[s]', '[/s]');
249 self::tagToBBCode($doc, 'strike', [], '[s]', '[/s]');
251 self::tagToBBCode($doc, 'big', [], "[size=large]", "[/size]");
252 self::tagToBBCode($doc, 'small', [], "[size=small]", "[/size]");
254 self::tagToBBCode($doc, 'blockquote', [], '[quote]', '[/quote]');
256 self::tagToBBCode($doc, 'br', [], "\n", '');
258 self::tagToBBCode($doc, 'p', ['class' => 'MsoNormal'], "\n", "");
259 self::tagToBBCode($doc, 'div', ['class' => 'MsoNormal'], "\r", "");
261 self::tagToBBCode($doc, 'span', [], "", "");
263 self::tagToBBCode($doc, 'span', [], "", "");
264 self::tagToBBCode($doc, 'pre', [], "", "");
266 self::tagToBBCode($doc, 'div', [], "\r", "\r");
267 self::tagToBBCode($doc, 'p', [], "\n", "\n");
269 self::tagToBBCode($doc, 'ul', [], "[list]", "[/list]");
270 self::tagToBBCode($doc, 'ol', [], "[list=1]", "[/list]");
271 self::tagToBBCode($doc, 'li', [], "[*]", "");
273 self::tagToBBCode($doc, 'hr', [], "[hr]", "");
275 self::tagToBBCode($doc, 'table', [], "", "");
276 self::tagToBBCode($doc, 'tr', [], "\n", "");
277 self::tagToBBCode($doc, 'td', [], "\t", "");
278 //self::node2BBCode($doc, 'table', array(), "[table]", "[/table]");
279 //self::node2BBCode($doc, 'th', array(), "[th]", "[/th]");
280 //self::node2BBCode($doc, 'tr', array(), "[tr]", "[/tr]");
281 //self::node2BBCode($doc, 'td', array(), "[td]", "[/td]");
282 //self::node2BBCode($doc, 'h1', array(), "\n\n[size=xx-large][b]", "[/b][/size]\n");
283 //self::node2BBCode($doc, 'h2', array(), "\n\n[size=x-large][b]", "[/b][/size]\n");
284 //self::node2BBCode($doc, 'h3', array(), "\n\n[size=large][b]", "[/b][/size]\n");
285 //self::node2BBCode($doc, 'h4', array(), "\n\n[size=medium][b]", "[/b][/size]\n");
286 //self::node2BBCode($doc, 'h5', array(), "\n\n[size=small][b]", "[/b][/size]\n");
287 //self::node2BBCode($doc, 'h6', array(), "\n\n[size=x-small][b]", "[/b][/size]\n");
289 self::tagToBBCode($doc, 'h1', [], "[h1]", "[/h1]");
290 self::tagToBBCode($doc, 'h2', [], "[h2]", "[/h2]");
291 self::tagToBBCode($doc, 'h3', [], "[h3]", "[/h3]");
292 self::tagToBBCode($doc, 'h4', [], "[h4]", "[/h4]");
293 self::tagToBBCode($doc, 'h5', [], "[h5]", "[/h5]");
294 self::tagToBBCode($doc, 'h6', [], "[h6]", "[/h6]");
296 self::tagToBBCode($doc, 'a', ['href' => '/mailto:(.+)/'], '[mail=$1]', '[/mail]');
297 self::tagToBBCode($doc, 'a', ['href' => '/(.+)/'], '[url=$1]', '[/url]');
299 self::tagToBBCode($doc, 'img', ['src' => '/(.+)/', 'alt' => '/(.+)/'], '[img=$1]$2', '[/img]');
300 self::tagToBBCode($doc, 'img', ['src' => '/(.+)/', 'width' => '/(\d+)/', 'height' => '/(\d+)/'], '[img=$2x$3]$1', '[/img]');
301 self::tagToBBCode($doc, 'img', ['src' => '/(.+)/'], '[img]$1', '[/img]');
304 self::tagToBBCode($doc, 'video', ['src' => '/(.+)/'], '[video]$1', '[/video]');
305 self::tagToBBCode($doc, 'audio', ['src' => '/(.+)/'], '[audio]$1', '[/audio]');
306 self::tagToBBCode($doc, 'iframe', ['src' => '/(.+)/'], '[iframe]$1', '[/iframe]');
308 self::tagToBBCode($doc, 'key', [], '[code]', '[/code]');
309 self::tagToBBCode($doc, 'code', [], '[code]', '[/code]');
311 $message = $doc->saveHTML();
313 // I'm removing something really disturbing
314 // Don't know exactly what it is
315 $message = str_replace(chr(194) . chr(160), ' ', $message);
317 $message = str_replace(" ", " ", $message);
319 // removing multiple DIVs
320 $message = preg_replace('=\r *\r=i', "\n", $message);
321 $message = str_replace("\r", "\n", $message);
323 Hook::callAll('html2bbcode', $message);
325 $message = strip_tags($message);
327 $message = html_entity_decode($message, ENT_QUOTES, 'UTF-8');
329 $message = str_replace(["<"], ["<"], $message);
331 // remove quotes if they don't make sense
332 $message = preg_replace('=\[/quote\][\s]*\[quote\]=i', "\n", $message);
334 $message = preg_replace('=\[quote\]\s*=i', "[quote]", $message);
335 $message = preg_replace('=\s*\[/quote\]=i', "[/quote]", $message);
338 $oldmessage = $message;
339 $message = str_replace("\n \n", "\n\n", $message);
340 } while ($oldmessage != $message);
343 $oldmessage = $message;
344 $message = str_replace("\n\n\n", "\n\n", $message);
345 } while ($oldmessage != $message);
348 $oldmessage = $message;
349 $message = str_replace(
372 } while ($message != $oldmessage);
374 $message = str_replace(
375 ['[b][b]', '[/b][/b]', '[i][i]', '[/i][/i]'],
376 ['[b]', '[/b]', '[i]', '[/i]'],
380 // Handling Yahoo style of mails
381 $message = str_replace('[hr][b]From:[/b]', '[quote][b]From:[/b]', $message);
383 // Restore code blocks
384 $message = preg_replace_callback(
385 '#\[codeblock-([0-9]+)\]#iU',
386 function ($matches) use ($codeblocks) {
388 if (isset($codeblocks[intval($matches[1])])) {
389 $return = $codeblocks[$matches[1]];
396 $message = trim($message);
398 if ($basepath != '') {
399 $message = self::qualifyURLs($message, $basepath);
406 * @brief Sub function to complete incomplete URL
408 * @param array $matches Result of preg_replace_callback
409 * @param string $basepath Basepath that is used to complete the URL
411 * @return string The expanded URL
413 private static function qualifyURLsSub($matches, $basepath)
415 $base = parse_url($basepath);
416 unset($base['query']);
417 unset($base['fragment']);
422 $parts = array_merge($base, parse_url($url));
423 $url2 = Network::unparseURL($parts);
425 return str_replace($url, $url2, $link);
429 * @brief Complete incomplete URLs in BBCode
431 * @param string $body Body with URLs
432 * @param string $basepath Base path that is used to complete the URL
434 * @return string Body with expanded URLs
436 private static function qualifyURLs($body, $basepath)
438 $URLSearchString = "^\[\]";
440 $matches = ["/\[url\=([$URLSearchString]*)\].*?\[\/url\]/ism",
441 "/\[url\]([$URLSearchString]*)\[\/url\]/ism",
442 "/\[img\=[0-9]*x[0-9]*\](.*?)\[\/img\]/ism",
443 "/\[img\](.*?)\[\/img\]/ism",
444 "/\[zmg\=[0-9]*x[0-9]*\](.*?)\[\/img\]/ism",
445 "/\[zmg\](.*?)\[\/zmg\]/ism",
446 "/\[video\](.*?)\[\/video\]/ism",
447 "/\[audio\](.*?)\[\/audio\]/ism",
450 foreach ($matches as $match) {
451 $body = preg_replace_callback(
453 function ($match) use ($basepath) {
454 return self::qualifyURLsSub($match, $basepath);
462 private static function breakLines($line, $level, $wraplength = 75)
464 if ($wraplength == 0) {
465 $wraplength = 2000000;
468 $wraplen = $wraplength - $level;
475 $subline = substr($line, 0, $wraplen);
477 $pos = strrpos($subline, ' ');
480 $pos = strpos($line, ' ');
483 if (($pos > 0) && strlen($line) > $wraplen) {
484 $newline = trim(substr($line, 0, $pos));
486 $newline = str_repeat(">", $level) . ' ' . $newline;
489 $newlines[] = $newline . " ";
490 $line = substr($line, $pos + 1);
492 } while ((strlen($line) > $wraplen) && !($oldline == $line));
495 $line = str_repeat(">", $level) . ' ' . $line;
500 return implode($newlines, "\n");
503 private static function quoteLevel($message, $wraplength = 75)
505 $lines = explode("\n", $message);
509 foreach ($lines as $line) {
512 while (strpos("*" . $line, '[quote]') > 0) {
514 $pos = strpos($line, '[quote]');
515 $line = substr($line, 0, $pos) . substr($line, $pos + 7);
521 while (strpos("*" . $line, '[/quote]') > 0) {
527 $pos = strpos($line, '[/quote]');
528 $line = substr($line, 0, $pos) . substr($line, $pos + 8);
531 if (!$startquote || ($line != '')) {
532 $newlines[] = self::breakLines($line, $currlevel, $wraplength);
536 return implode($newlines, "\n");
539 private static function collectURLs($message)
541 $pattern = '/<a.*?href="(.*?)".*?>(.*?)<\/a>/is';
542 preg_match_all($pattern, $message, $result, PREG_SET_ORDER);
545 foreach ($result as $treffer) {
548 // A list of some links that should be ignored
549 $list = ["/user/", "/tag/", "/group/", "/profile/", "/search?search=", "/search?tag=", "mailto:", "/u/", "/node/",
550 "//plus.google.com/", "//twitter.com/"];
551 foreach ($list as $listitem) {
552 if (strpos($treffer[1], $listitem) !== false) {
557 if ((strpos($treffer[1], "//twitter.com/") !== false) && (strpos($treffer[1], "/status/") !== false)) {
561 if ((strpos($treffer[1], "//plus.google.com/") !== false) && (strpos($treffer[1], "/posts") !== false)) {
565 if ((strpos($treffer[1], "//plus.google.com/") !== false) && (strpos($treffer[1], "/photos") !== false)) {
569 $ignore = $ignore || strpos($treffer[1], '#') === 0;
572 $urls[$treffer[1]] = $treffer[1];
580 * @param string $html
581 * @param int $wraplength Ensures individual lines aren't longer than this many characters. Doesn't break words.
582 * @param bool $compact True: Completely strips image tags; False: Keeps image URLs
585 public static function toPlaintext(string $html, $wraplength = 75, $compact = false)
587 $message = str_replace("\r", "", $html);
589 $doc = new DOMDocument();
590 $doc->preserveWhiteSpace = false;
592 $message = mb_convert_encoding($message, 'HTML-ENTITIES', "UTF-8");
594 @$doc->loadHTML($message, LIBXML_HTML_NODEFDTD);
596 $message = $doc->saveHTML();
597 // Remove eventual UTF-8 BOM
598 $message = str_replace("\xC3\x82\xC2\xA0", "", $message);
600 // Collecting all links
601 $urls = self::collectURLs($message);
603 @$doc->loadHTML($message, LIBXML_HTML_NODEFDTD);
605 self::tagToBBCode($doc, 'html', [], '', '');
606 self::tagToBBCode($doc, 'body', [], '', '');
609 self::tagToBBCode($doc, 'blockquote', [], "»", "«");
611 self::tagToBBCode($doc, 'blockquote', [], '[quote]', "[/quote]\n");
614 self::tagToBBCode($doc, 'br', [], "\n", '');
616 self::tagToBBCode($doc, 'span', [], "", "");
617 self::tagToBBCode($doc, 'pre', [], "", "");
618 self::tagToBBCode($doc, 'div', [], "\r", "\r");
619 self::tagToBBCode($doc, 'p', [], "\n", "\n");
621 self::tagToBBCode($doc, 'li', [], "\n* ", "\n");
623 self::tagToBBCode($doc, 'hr', [], "\n" . str_repeat("-", 70) . "\n", "");
625 self::tagToBBCode($doc, 'tr', [], "\n", "");
626 self::tagToBBCode($doc, 'td', [], "\t", "");
628 self::tagToBBCode($doc, 'h1', [], "\n\n*", "*\n");
629 self::tagToBBCode($doc, 'h2', [], "\n\n*", "*\n");
630 self::tagToBBCode($doc, 'h3', [], "\n\n*", "*\n");
631 self::tagToBBCode($doc, 'h4', [], "\n\n*", "*\n");
632 self::tagToBBCode($doc, 'h5', [], "\n\n*", "*\n");
633 self::tagToBBCode($doc, 'h6', [], "\n\n*", "*\n");
636 self::tagToBBCode($doc, 'img', ['src' => '/(.+)/'], ' [img]$1', '[/img] ');
638 self::tagToBBCode($doc, 'img', ['src' => '/(.+)/'], ' ', ' ');
641 self::tagToBBCode($doc, 'iframe', ['src' => '/(.+)/'], ' $1 ', '');
643 $message = $doc->saveHTML();
646 $message = str_replace("[img]", "", $message);
647 $message = str_replace("[/img]", "", $message);
650 // was ersetze ich da?
651 // Irgendein stoerrisches UTF-Zeug
652 $message = str_replace(chr(194) . chr(160), ' ', $message);
654 $message = str_replace(" ", " ", $message);
656 // Aufeinanderfolgende DIVs
657 $message = preg_replace('=\r *\r=i', "\n", $message);
658 $message = str_replace("\r", "\n", $message);
660 $message = strip_tags($message);
662 $message = html_entity_decode($message, ENT_QUOTES, 'UTF-8');
664 if (!$compact && ($message != '')) {
665 foreach ($urls as $id => $url) {
666 if ($url != '' && strpos($message, $url) === false) {
667 $message .= "\n" . $url . ' ';
672 $message = str_replace("\n«", "«\n", $message);
673 $message = str_replace("»\n", "\n»", $message);
676 $oldmessage = $message;
677 $message = str_replace("\n\n\n", "\n\n", $message);
678 } while ($oldmessage != $message);
680 $message = self::quoteLevel(trim($message), $wraplength);
682 return trim($message);
686 * Converts provided HTML code to Markdown. The hardwrap parameter maximizes
687 * compatibility with Diaspora in spite of the Markdown standards.
689 * @param string $html
692 public static function toMarkdown($html)
694 $converter = new HtmlConverter(['hard_break' => true]);
695 $markdown = $converter->convert($html);
701 * @brief Convert video HTML to BBCode tags
706 public static function toBBCodeVideo($s)
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($text, $base)
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 * return div element with class 'clear'
774 public static function clearDiv()
776 return '<div class="clear"></div>';
780 * Loader for infinite scrolling
782 * @return string html for loader
783 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
785 public static function scrollLoader()
787 $tpl = Renderer::getMarkupTemplate("scroll_loader.tpl");
788 return Renderer::replaceMacros($tpl, [
789 'wait' => L10n::t('Loading more entries...'),
790 'end' => L10n::t('The end')
795 * Get html for contact block.
797 * @deprecated since version 2019.03
798 * @see ContactBlock::getHTML()
800 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
801 * @throws \ImagickException
803 public static function contactBlock()
807 return ContactBlock::getHTML($a->profile);
811 * @brief Format contacts as picture links or as text links
813 * @param array $contact Array with contacts which contains an array with
814 * int 'id' => The ID of the contact
815 * int 'uid' => The user ID of the user who owns this data
816 * string 'name' => The name of the contact
817 * string 'url' => The url to the profile page of the contact
818 * string 'addr' => The webbie of the contact (e.g.) username@friendica.com
819 * string 'network' => The network to which the contact belongs to
820 * string 'thumb' => The contact picture
821 * string 'click' => js code which is performed when clicking on the contact
822 * @param boolean $redirect If true try to use the redir url if it's possible
823 * @param string $class CSS class for the
824 * @param boolean $textmode If true display the contacts as text links
825 * if false display the contacts as picture links
826 * @return string Formatted html
827 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
828 * @throws \ImagickException
830 public static function micropro($contact, $redirect = false, $class = '', $textmode = false)
832 // Use the contact URL if no address is available
833 if (empty($contact['addr'])) {
834 $contact["addr"] = $contact["url"];
837 $url = $contact['url'];
842 $url = Contact::magicLink($contact['url']);
843 if (strpos($url, 'redir/') === 0) {
844 $sparkle = ' sparkle';
848 // If there is some js available we don't need the url
849 if (!empty($contact['click'])) {
853 return Renderer::replaceMacros(Renderer::getMarkupTemplate(($textmode)?'micropro_txt.tpl':'micropro_img.tpl'), [
854 '$click' => defaults($contact, 'click', ''),
857 '$photo' => ProxyUtils::proxifyUrl($contact['thumb'], false, ProxyUtils::SIZE_THUMB),
858 '$name' => $contact['name'],
859 'title' => $contact['name'] . ' [' . $contact['addr'] . ']',
860 '$parkle' => $sparkle,
868 * @param string $s Search query.
869 * @param string $id HTML id
870 * @param string $url Search url.
871 * @param bool $aside Display the search widgit aside.
873 * @return string Formatted HTML.
874 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
876 public static function search($s, $id = 'search-box', $url = 'search', $aside = true)
880 if (strpos($s, '#') === 0) {
883 $save_label = $mode === 'text' ? L10n::t('Save') : L10n::t('Follow');
888 '$action_url' => $url,
889 '$search_label' => L10n::t('Search'),
890 '$save_label' => $save_label,
891 '$savedsearch' => 'savedsearch',
892 '$search_hint' => L10n::t('@name, !forum, #tags, content'),
897 $values['$searchoption'] = [
898 L10n::t("Full Text"),
900 L10n::t("Contacts")];
902 if (Config::get('system', 'poco_local_search')) {
903 $values['$searchoption'][] = L10n::t("Forums");
907 return Renderer::replaceMacros(Renderer::getMarkupTemplate('searchbox.tpl'), $values);
911 * Replace naked text hyperlink with HTML formatted hyperlink
916 public static function toLink($s)
918 $s = preg_replace("/(https?\:\/\/[a-zA-Z0-9\:\/\-\?\&\;\.\=\_\~\#\'\%\$\!\+]*)/", ' <a href="$1" target="_blank">$1</a>', $s);
919 $s = preg_replace("/\<(.*?)(src|href)=(.*?)\&\;(.*?)\>/ism", '<$1$2=$3&$4>', $s);
924 * Given a HTML text and a set of filtering reasons, adds a content hiding header with the provided reasons
926 * Reasons are expected to have been translated already.
928 * @param string $html
929 * @param array $reasons
931 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
933 public static function applyContentFilter($html, array $reasons)
935 if (count($reasons)) {
936 $tpl = Renderer::getMarkupTemplate('wall/content_filter.tpl');
937 $html = Renderer::replaceMacros($tpl, [
938 '$reasons' => $reasons,
939 '$rnd' => Strings::getRandomHex(8),
940 '$openclose' => L10n::t('Click to open/close'),
949 * replace html amp entity with amp char
953 public static function unamp($s)
955 return str_replace('&', '&', $s);