]> git.mxchange.org Git - friendica.git/blob - src/Content/Text/HTML.php
Posts per author/server on the community pages (#13764)
[friendica.git] / src / Content / Text / HTML.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2010-2023, the Friendica project
4  *
5  * @license GNU AGPL version 3 or any later version
6  *
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.
11  *
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.
16  *
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/>.
19  *
20  */
21
22 namespace Friendica\Content\Text;
23
24 use DOMDocument;
25 use DOMXPath;
26 use Friendica\Protocol\HTTP\MediaType;
27 use Friendica\Core\Hook;
28 use Friendica\Core\Renderer;
29 use Friendica\Core\Search;
30 use Friendica\DI;
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;
37
38 class HTML
39 {
40         /**
41          * Search all instances of a specific HTML tag node in the provided DOM document and replaces them with BBCode text nodes.
42          *
43          * @see HTML::tagToBBCodeSub()
44          */
45         private static function tagToBBCode(DOMDocument $doc, string $tag, array $attributes, string $startbb, string $endbb, bool $ignoreChildren = false)
46         {
47                 do {
48                         $done = self::tagToBBCodeSub($doc, $tag, $attributes, $startbb, $endbb, $ignoreChildren);
49                 } while ($done);
50         }
51
52         /**
53          * Search the first specific HTML tag node in the provided DOM document and replaces it with BBCode text nodes.
54          *
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
64          */
65         private static function tagToBBCodeSub(DOMDocument $doc, string $tag, array $attributes, string $startbb, string $endbb, bool $ignoreChildren = false): bool
66         {
67                 $savestart = str_replace('$', '\x01', $startbb);
68                 $replace = false;
69
70                 $xpath = new DOMXPath($doc);
71
72                 /** @var \DOMNode[] $list */
73                 $list = $xpath->query("//" . $tag);
74                 foreach ($list as $node) {
75                         $attr = [];
76                         if ($node->attributes->length) {
77                                 foreach ($node->attributes as $attribute) {
78                                         $attr[$attribute->name] = $attribute->value;
79                                 }
80                         }
81
82                         $replace = true;
83
84                         $startbb = $savestart;
85
86                         $i = 0;
87
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);
93
94                                                 // If nothing could be changed
95                                                 if ($count == 0) {
96                                                         $replace = false;
97                                                 }
98                                         } else {
99                                                 $replace = false;
100                                         }
101                                 } else {
102                                         if (@$attr[$attribute] != $value) {
103                                                 $replace = false;
104                                         }
105                                 }
106                         }
107
108                         if ($replace) {
109                                 $StartCode = $doc->createTextNode($startbb);
110                                 $EndCode = $doc->createTextNode($endbb);
111
112                                 $node->parentNode->insertBefore($StartCode, $node);
113
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);
121                                                 }
122                                         }
123                                 }
124
125                                 $node->parentNode->insertBefore($EndCode, $node);
126                                 $node->parentNode->removeChild($node);
127                         }
128                 }
129
130                 return $replace;
131         }
132
133         /**
134          * Converter for HTML to BBCode
135          *
136          * Made by: ike@piratenpartei.de
137          * Originally made for the syncom project: http://wiki.piratenpartei.de/Syncom
138          *                    https://github.com/annando/Syncom
139          *
140          * @param string $message
141          * @param string $basepath
142          * @return string
143          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
144          */
145         public static function toBBCode(string $message, string $basepath = ''): string
146         {
147                 /*
148                  * Check if message is empty to prevent a lot of code below from being executed
149                  * for just an empty message.
150                  */
151                 if ($message === '') {
152                         return '';
153                 }
154
155                 DI::profiler()->startRecording('rendering');
156                 $message = str_replace("\r", "", $message);
157
158                 $message = Strings::performWithEscapedBlocks($message, '#<pre><code.*</code></pre>#iUs', function ($message) {
159                         $message = str_replace(
160                                 [
161                                         "<li><p>",
162                                         "</p></li>",
163                                 ],
164                                 [
165                                         "<li>",
166                                         "</li>",
167                                 ],
168                                 $message
169                         );
170
171                         // remove namespaces
172                         $message = preg_replace('=<(\w+):(.+?)>=', '<removeme>', $message);
173                         $message = preg_replace('=</(\w+):(.+?)>=', '</removeme>', $message);
174
175                         $doc = new DOMDocument();
176                         $doc->preserveWhiteSpace = false;
177
178                         $message = mb_convert_encoding($message, 'HTML-ENTITIES', "UTF-8");
179
180                         if (empty($message)) {
181                                 return '';
182                         }
183
184                         @$doc->loadHTML($message, LIBXML_HTML_NODEFDTD);
185
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');
192
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+;)/', '&amp;', $node->nodeValue);
198                                 $node->nodeValue = str_replace("\n", "\r", $safe_value);
199                         }
200
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);
204
205                         if (empty($message)) {
206                                 return '';
207                         }
208
209                         @$doc->loadHTML($message, LIBXML_HTML_NODEFDTD);
210
211                         self::tagToBBCode($doc, 'html', [], "", "");
212                         self::tagToBBCode($doc, 'body', [], "", "");
213
214                         // Outlook-Quote - Variant 1
215                         self::tagToBBCode($doc, 'p', ['class' => 'MsoNormal', 'style' => 'margin-left:35.4pt'], '[quote]', '[/quote]');
216
217                         // Outlook-Quote - Variant 2
218                         self::tagToBBCode(
219                                 $doc,
220                                 'div',
221                                 ['style' => 'border:none;border-left:solid blue 1.5pt;padding:0cm 0cm 0cm 4.0pt'],
222                                 '[quote]',
223                                 '[/quote]'
224                         );
225
226                         // MyBB-Stuff
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]');
230
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]');
238                          */
239                         // Untested
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]');
243
244                         self::tagToBBCode($doc, 'span', ['style' => '/.*color:\s*(.+?)[,;].*/'], '[color="$1"]', '[/color]');
245
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
251                         // Test
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]');
255
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]');
264
265                         self::tagToBBCode($doc, 'big', [], "[size=large]", "[/size]");
266                         self::tagToBBCode($doc, 'small', [], "[size=small]", "[/size]");
267
268                         self::tagToBBCode($doc, 'blockquote', [], '[quote]', '[/quote]');
269
270                         self::tagToBBCode($doc, 'br', [], "\n", '');
271
272                         self::tagToBBCode($doc, 'p', ['class' => 'MsoNormal'], "\n", "");
273                         self::tagToBBCode($doc, 'div', ['class' => 'MsoNormal'], "\r", "");
274
275                         self::tagToBBCode($doc, 'span', [], "", "");
276
277                         self::tagToBBCode($doc, 'span', [], "", "");
278                         self::tagToBBCode($doc, 'pre', [], "", "");
279
280                         self::tagToBBCode($doc, 'div', [], "\r", "\r");
281                         self::tagToBBCode($doc, 'p', [], "\n", "\n");
282
283                         self::tagToBBCode($doc, 'ul', [], "[ul]", "\n[/ul]");
284                         self::tagToBBCode($doc, 'ol', [], "[ol]", "\n[/ol]");
285                         self::tagToBBCode($doc, 'li', [], "\n[li]", "[/li]");
286
287                         self::tagToBBCode($doc, 'hr', [], "[hr]", "");
288
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]");
293
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]");
300
301                         self::tagToBBCode($doc, 'a', ['href' => '/mailto:(.+)/'], '[mail=$1]', '[/mail]');
302                         self::tagToBBCode($doc, 'a', ['href' => '/(.+)/'], '[url=$1]', '[/url]');
303
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);
307
308
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);
313
314                         self::tagToBBCode($doc, 'key', [], '[code]', '[/code]');
315                         self::tagToBBCode($doc, 'code', [], '[code]', '[/code]');
316
317                         $message = $doc->saveHTML();
318
319                         // I'm removing something really disturbing
320                         // Don't know exactly what it is
321                         $message = str_replace(chr(194) . chr(160), ' ', $message);
322
323                         $message = str_replace("&nbsp;", " ", $message);
324
325                         // removing multiple DIVs
326                         $message = preg_replace('=\r *\r=i', "\n", $message);
327                         $message = str_replace("\r", "\n", $message);
328
329                         Hook::callAll('html2bbcode', $message);
330
331                         $message = strip_tags($message);
332
333                         $message = html_entity_decode($message, ENT_QUOTES, 'UTF-8');
334
335                         // remove quotes if they don't make sense
336                         $message = preg_replace('=\[/quote\][\s]*\[quote\]=i', "\n", $message);
337
338                         $message = preg_replace('=\[quote\]\s*=i', "[quote]", $message);
339                         $message = preg_replace('=\s*\[/quote\]=i', "[/quote]", $message);
340
341                         do {
342                                 $oldmessage = $message;
343                                 $message = str_replace("\n \n", "\n\n", $message);
344                         } while ($oldmessage != $message);
345
346                         do {
347                                 $oldmessage = $message;
348                                 $message = str_replace("\n\n\n", "\n\n", $message);
349                         } while ($oldmessage != $message);
350
351                         $message = str_replace(
352                                 ['[b][b]', '[/b][/b]', '[i][i]', '[/i][/i]'],
353                                 ['[b]', '[/b]', '[i]', '[/i]'],
354                                 $message
355                         );
356
357                         // Handling Yahoo style of mails
358                         $message = str_replace('[hr][b]From:[/b]', '[quote][b]From:[/b]', $message);
359
360                         return $message;
361                 });
362
363                 $message = preg_replace_callback(
364                         '#<pre><code(?: class="language-([^"]*)")?>(.*)</code></pre>#iUs',
365                         function ($matches) {
366                                 $prefix = '[code]';
367                                 if ($matches[1] != '') {
368                                         $prefix = '[code=' . $matches[1] . ']';
369                                 }
370
371                                 return $prefix . "\n" . html_entity_decode($matches[2]) . "\n" . '[/code]';
372                         },
373                         $message
374                 );
375
376                 $message = trim($message);
377
378                 if ($basepath != '') {
379                         $message = self::qualifyURLs($message, $basepath);
380                 }
381
382                 DI::profiler()->stopRecording();
383                 return $message;
384         }
385
386         /**
387          * Sub function to complete incomplete URL
388          *
389          * @param array  $matches  Result of preg_replace_callback
390          * @param string $basepath Basepath that is used to complete the URL
391          *
392          * @return string The expanded URL
393          */
394         private static function qualifyURLsSub(array $matches, string $basepath): string
395         {
396                 $base = parse_url($basepath);
397                 unset($base['query']);
398                 unset($base['fragment']);
399
400                 $link = $matches[0];
401                 $url = $matches[1];
402
403                 if (empty($url) || empty(parse_url($url))) {
404                         return $matches[0];
405                 }
406
407                 $parts = array_merge($base, parse_url($url));
408                 $url2 = Network::unparseURL($parts);
409
410                 return str_replace($url, $url2, $link);
411         }
412
413         /**
414          * Complete incomplete URLs in BBCode
415          *
416          * @param string $body     Body with URLs
417          * @param string $basepath Base path that is used to complete the URL
418          *
419          * @return string Body with expanded URLs
420          */
421         private static function qualifyURLs(string $body, string $basepath): string
422         {
423                 $URLSearchString = "^\[\]";
424
425                 $matches = [
426                         "/\[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",
434                 ];
435
436                 foreach ($matches as $match) {
437                         $body = preg_replace_callback(
438                                 $match,
439                                 function ($match) use ($basepath) {
440                                         return self::qualifyURLsSub($match, $basepath);
441                                 },
442                                 $body
443                         );
444                 }
445                 return $body;
446         }
447
448         private static function breakLines(string $line, int $level, int $wraplength = 75): string
449         {
450                 if ($wraplength == 0) {
451                         $wraplength = 2000000;
452                 }
453
454                 $wraplen = $wraplength - $level;
455
456                 $newlines = [];
457
458                 do {
459                         $oldline = $line;
460
461                         $subline = substr($line, 0, $wraplen);
462
463                         $pos = strrpos($subline, ' ');
464
465                         if ($pos == 0) {
466                                 $pos = strpos($line, ' ');
467                         }
468
469                         if (($pos > 0) && strlen($line) > $wraplen) {
470                                 $newline = trim(substr($line, 0, $pos));
471                                 if ($level > 0) {
472                                         $newline = str_repeat(">", $level) . ' ' . $newline;
473                                 }
474
475                                 $newlines[] = $newline . " ";
476                                 $line = substr($line, $pos + 1);
477                         }
478                 } while ((strlen($line) > $wraplen) && !($oldline == $line));
479
480                 if ($level > 0) {
481                         $line = str_repeat(">", $level) . ' ' . $line;
482                 }
483
484                 $newlines[] = $line;
485
486                 return implode("\n", $newlines);
487         }
488
489         private static function quoteLevel(string $message, int $wraplength = 75): string
490         {
491                 $lines = explode("\n", $message);
492
493                 $newlines = [];
494                 $level = 0;
495                 foreach ($lines as $line) {
496                         $line = trim($line);
497                         $startquote = false;
498                         while (strpos("*" . $line, '[quote]') > 0) {
499                                 $level++;
500                                 $pos = strpos($line, '[quote]');
501                                 $line = substr($line, 0, $pos) . substr($line, $pos + 7);
502                                 $startquote = true;
503                         }
504
505                         $currlevel = $level;
506
507                         while (strpos("*" . $line, '[/quote]') > 0) {
508                                 $level--;
509                                 if ($level < 0) {
510                                         $level = 0;
511                                 }
512
513                                 $pos = strpos($line, '[/quote]');
514                                 $line = substr($line, 0, $pos) . substr($line, $pos + 8);
515                         }
516
517                         if (!$startquote || ($line != '')) {
518                                 $newlines[] = self::breakLines($line, $currlevel, $wraplength);
519                         }
520                 }
521
522                 return implode("\n", $newlines);
523         }
524
525         private static function collectURLs(string $message): array
526         {
527                 $pattern = '/<a.*?href="(.*?)".*?>(.*?)<\/a>/is';
528                 preg_match_all($pattern, $message, $result, PREG_SET_ORDER);
529
530                 $urls = [];
531                 foreach ($result as $treffer) {
532                         $ignore = false;
533
534                         // A list of some links that should be ignored
535                         $list = [
536                                 "/user/", "/tag/", "/group/", "/circle/", "/profile/", "/search?search=", "/search?tag=", "mailto:", "/u/", "/node/",
537                                 "//plus.google.com/", "//twitter.com/"
538                         ];
539                         foreach ($list as $listitem) {
540                                 if (strpos($treffer[1], $listitem) !== false) {
541                                         $ignore = true;
542                                 }
543                         }
544
545                         if ((strpos($treffer[1], "//twitter.com/") !== false) && (strpos($treffer[1], "/status/") !== false)) {
546                                 $ignore = false;
547                         }
548
549                         if ((strpos($treffer[1], "//plus.google.com/") !== false) && (strpos($treffer[1], "/posts") !== false)) {
550                                 $ignore = false;
551                         }
552
553                         if ((strpos($treffer[1], "//plus.google.com/") !== false) && (strpos($treffer[1], "/photos") !== false)) {
554                                 $ignore = false;
555                         }
556
557                         $ignore = $ignore || strpos($treffer[1], '#') === 0;
558
559                         if (!$ignore) {
560                                 $urls[$treffer[1]] = $treffer[1];
561                         }
562                 }
563
564                 return $urls;
565         }
566
567         /**
568          * @param string $html
569          * @param int    $wraplength Ensures individual lines aren't longer than this many characters. Doesn't break words.
570          * @param bool   $compact    True: Completely strips image tags; False: Keeps image URLs
571          * @return string
572          */
573         public static function toPlaintext(string $html, int $wraplength = 75, bool $compact = false): string
574         {
575                 DI::profiler()->startRecording('rendering');
576                 $message = str_replace("\r", "", $html);
577
578                 $doc = new DOMDocument();
579                 $doc->preserveWhiteSpace = false;
580
581                 $message = mb_convert_encoding($message, 'HTML-ENTITIES', "UTF-8");
582
583                 if (empty($message)) {
584                         DI::profiler()->stopRecording();
585                         return '';
586                 }
587
588                 @$doc->loadHTML($message, LIBXML_HTML_NODEFDTD);
589
590                 $message = $doc->saveHTML();
591                 // Remove eventual UTF-8 BOM
592                 $message = str_replace("\xC3\x82\xC2\xA0", "", $message);
593
594                 // Collecting all links
595                 $urls = self::collectURLs($message);
596
597                 if (empty($message)) {
598                         DI::profiler()->stopRecording();
599                         return '';
600                 }
601
602                 @$doc->loadHTML($message, LIBXML_HTML_NODEFDTD);
603
604                 self::tagToBBCode($doc, 'html', [], '', '');
605                 self::tagToBBCode($doc, 'body', [], '', '');
606
607                 if ($compact) {
608                         self::tagToBBCode($doc, 'blockquote', [], "»", "«");
609                 } else {
610                         self::tagToBBCode($doc, 'blockquote', [], '[quote]', "[/quote]\n");
611                 }
612
613                 self::tagToBBCode($doc, 'br', [], "\n", '');
614
615                 self::tagToBBCode($doc, 'span', [], "", "");
616                 self::tagToBBCode($doc, 'pre', [], "", "");
617                 self::tagToBBCode($doc, 'div', [], "\r", "\r");
618                 self::tagToBBCode($doc, 'p', [], "\n", "\n");
619
620                 self::tagToBBCode($doc, 'li', [], "\n* ", "\n");
621
622                 self::tagToBBCode($doc, 'hr', [], "\n" . str_repeat("-", 70) . "\n", "");
623
624                 self::tagToBBCode($doc, 'tr', [], "\n", "");
625                 self::tagToBBCode($doc, 'td', [], "\t", "");
626
627                 self::tagToBBCode($doc, 'h1', [], "\n\n*", "*\n");
628                 self::tagToBBCode($doc, 'h2', [], "\n\n*", "*\n");
629                 self::tagToBBCode($doc, 'h3', [], "\n\n*", "*\n");
630                 self::tagToBBCode($doc, 'h4', [], "\n\n*", "*\n");
631                 self::tagToBBCode($doc, 'h5', [], "\n\n*", "*\n");
632                 self::tagToBBCode($doc, 'h6', [], "\n\n*", "*\n");
633
634                 if (!$compact) {
635                         self::tagToBBCode($doc, 'img', ['src' => '/(.+)/'], ' [img]$1', '[/img] ');
636                 } else {
637                         self::tagToBBCode($doc, 'img', ['src' => '/(.+)/'], ' ', ' ');
638                 }
639
640                 // Backward compatibility, [iframe] support has been removed in version 2020.12
641                 self::tagToBBCode($doc, 'iframe', ['src' => '/(.+)/'], ' $1 ', '');
642
643                 $message = $doc->saveHTML();
644
645                 if (!$compact) {
646                         $message = str_replace("[img]", "", $message);
647                         $message = str_replace("[/img]", "", $message);
648                 }
649
650                 // was ersetze ich da?
651                 // Irgendein stoerrisches UTF-Zeug
652                 $message = str_replace(chr(194) . chr(160), ' ', $message);
653
654                 $message = str_replace("&nbsp;", " ", $message);
655
656                 // Aufeinanderfolgende DIVs
657                 $message = preg_replace('=\r *\r=i', "\n", $message);
658                 $message = str_replace("\r", "\n", $message);
659
660                 $message = strip_tags($message);
661
662                 $message = html_entity_decode($message, ENT_QUOTES, 'UTF-8');
663
664                 if (!$compact && ($message != '')) {
665                         foreach ($urls as $id => $url) {
666                                 if ($url != '' && strpos($message, $url) === false) {
667                                         $message .= "\n" . $url . ' ';
668                                 }
669                         }
670                 }
671
672                 $message = str_replace("\n«", "«\n", $message);
673                 $message = str_replace("»\n", "\n»", $message);
674
675                 do {
676                         $oldmessage = $message;
677                         $message = str_replace("\n\n\n", "\n\n", $message);
678                 } while ($oldmessage != $message);
679
680                 $message = self::quoteLevel(trim($message), $wraplength);
681
682                 DI::profiler()->stopRecording();
683                 return trim($message);
684         }
685
686         /**
687          * Converts provided HTML code to Markdown. The hardwrap parameter maximizes
688          * compatibility with Diaspora in spite of the Markdown standards.
689          *
690          * @param string $html
691          * @return string
692          */
693         public static function toMarkdown(string $html): string
694         {
695                 DI::profiler()->startRecording('rendering');
696                 $converter = new HtmlConverter(['hard_break' => true]);
697                 $markdown = $converter->convert($html);
698
699                 DI::profiler()->stopRecording();
700                 return $markdown;
701         }
702
703         /**
704          * Convert video HTML to BBCode tags
705          *
706          * @param string $s
707          * @return string
708          */
709         public static function toBBCodeVideo(string $s): string
710         {
711                 $s = preg_replace(
712                         '#<object[^>]+>(.*?)https?://www.youtube.com/((?:v|cp)/[A-Za-z0-9\-_=]+)(.*?)</object>#ism',
713                         '[youtube]$2[/youtube]',
714                         $s
715                 );
716
717                 $s = preg_replace(
718                         '#<iframe[^>](.*?)https?://www.youtube.com/embed/([A-Za-z0-9\-_=]+)(.*?)</iframe>#ism',
719                         '[youtube]$2[/youtube]',
720                         $s
721                 );
722
723                 $s = preg_replace(
724                         '#<iframe[^>](.*?)https?://player.vimeo.com/video/([0-9]+)(.*?)</iframe>#ism',
725                         '[vimeo]$2[/vimeo]',
726                         $s
727                 );
728
729                 return $s;
730         }
731
732         /**
733          * transform link href and img src from relative to absolute
734          *
735          * @param string $text
736          * @param string $base base url
737          * @return string
738          */
739         public static function relToAbs(string $text, string $base): string
740         {
741                 if (empty($base)) {
742                         return $text;
743                 }
744
745                 $base = rtrim($base, '/');
746
747                 $base2 = $base . "/";
748
749                 // Replace links
750                 $pattern = "/<a([^>]*) href=\"(?!http|https|\/)([^\"]*)\"/";
751                 $replace = "<a\${1} href=\"" . $base2 . "\${2}\"";
752                 $text = preg_replace($pattern, $replace, $text);
753
754                 $pattern = "/<a([^>]*) href=\"(?!http|https)([^\"]*)\"/";
755                 $replace = "<a\${1} href=\"" . $base . "\${2}\"";
756                 $text = preg_replace($pattern, $replace, $text);
757
758                 // Replace images
759                 $pattern = "/<img([^>]*) src=\"(?!http|https|\/)([^\"]*)\"/";
760                 $replace = "<img\${1} src=\"" . $base2 . "\${2}\"";
761                 $text = preg_replace($pattern, $replace, $text);
762
763                 $pattern = "/<img([^>]*) src=\"(?!http|https)([^\"]*)\"/";
764                 $replace = "<img\${1} src=\"" . $base . "\${2}\"";
765                 $text = preg_replace($pattern, $replace, $text);
766
767
768                 // Done
769                 return $text;
770         }
771
772         /**
773          * Loader for infinite scrolling
774          *
775          * @return string html for loader
776          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
777          */
778         public static function scrollLoader(): string
779         {
780                 $tpl = Renderer::getMarkupTemplate("scroll_loader.tpl");
781                 return Renderer::replaceMacros($tpl, [
782                         'wait' => DI::l10n()->t('Loading more entries...'),
783                         'end' => DI::l10n()->t('The end')
784                 ]);
785         }
786
787         /**
788          * Format contacts as picture links or as text links
789          *
790          * @param array   $contact  Array with contacts which contains an array with
791          *                          int 'id' => The ID of the contact
792          *                          int 'uid' => The user ID of the user who owns this data
793          *                          string 'name' => The name of the contact
794          *                          string 'url' => The url to the profile page of the contact
795          *                          string 'addr' => The webbie of the contact (e.g.) username@friendica.com
796          *                          string 'network' => The network to which the contact belongs to
797          *                          string 'thumb' => The contact picture
798          *                          string 'click' => js code which is performed when clicking on the contact
799          * @param boolean $redirect If true try to use the redir url if it's possible
800          * @param string  $class    CSS class for the
801          * @param boolean $textmode If true display the contacts as text links
802          *                          if false display the contacts as picture links
803          * @return string Formatted html
804          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
805          * @throws \ImagickException
806          */
807         public static function micropro(array $contact, bool $redirect = false, string $class = '', bool $textmode = false): string
808         {
809                 // Use the contact URL if no address is available
810                 if (empty($contact['addr'])) {
811                         $contact["addr"] = $contact["url"];
812                 }
813
814                 $url = $contact['url'];
815                 $sparkle = '';
816                 $redir = false;
817
818                 if ($redirect) {
819                         $url = Contact::magicLinkByContact($contact);
820                         if (strpos($url, 'contact/redir/') === 0) {
821                                 $sparkle = ' sparkle';
822                         }
823                 }
824
825                 // If there is some js available we don't need the url
826                 if (!empty($contact['click'])) {
827                         $url = '';
828                 }
829
830                 return Renderer::replaceMacros(Renderer::getMarkupTemplate($textmode ? 'micropro_txt.tpl' : 'micropro_img.tpl'), [
831                         '$click' => $contact['click'] ?? '',
832                         '$class' => $class,
833                         '$url' => $url,
834                         '$photo' => Contact::getThumb($contact),
835                         '$name' => $contact['name'],
836                         'title' => $contact['name'] . ' [' . $contact['addr'] . ']',
837                         '$parkle' => $sparkle,
838                         '$redir' => $redir
839                 ]);
840         }
841
842         /**
843          * Search box.
844          *
845          * @param string $s     Search query.
846          * @param string $id    HTML id
847          * @param bool   $aside Display the search widget aside.
848          *
849          * @return string Formatted HTML.
850          * @throws \Exception
851          */
852         public static function search(string $s, string $id = 'search-box', bool $aside = true): string
853         {
854                 $mode = 'text';
855
856                 if (strpos($s, '#') === 0) {
857                         $mode = 'tag';
858                 }
859                 $save_label = $mode === 'text' ? DI::l10n()->t('Save') : DI::l10n()->t('Follow');
860
861                 $values = [
862                         '$s'            => $s,
863                         '$q'            => urlencode($s),
864                         '$id'           => $id,
865                         '$search_label' => DI::l10n()->t('Search'),
866                         '$save_label'   => $save_label,
867                         '$search_hint'  => DI::l10n()->t('@name, !group, #tags, content'),
868                         '$mode'         => $mode,
869                         '$return_url'   => urlencode(Search::getSearchPath($s)),
870                 ];
871
872                 if (!$aside) {
873                         $values['$search_options'] = [
874                                 'fulltext' => DI::l10n()->t('Full Text'),
875                                 'tags'     => DI::l10n()->t('Tags'),
876                                 'contacts' => DI::l10n()->t('Contacts')
877                         ];
878
879                         if (DI::config()->get('system', 'poco_local_search')) {
880                                 $values['$searchoption']['groups'] = DI::l10n()->t('Groups');
881                         }
882                 }
883
884                 return Renderer::replaceMacros(Renderer::getMarkupTemplate('searchbox.tpl'), $values);
885         }
886
887         /**
888          * Given a HTML text and a set of filtering reasons, adds a content hiding header with the provided reasons
889          *
890          * Reasons are expected to have been translated already.
891          *
892          * @param string $html
893          * @param array  $reasons
894          * @return string
895          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
896          */
897         public static function applyContentFilter(string $html, array $reasons): string
898         {
899                 if (count($reasons)) {
900                         $tpl = Renderer::getMarkupTemplate('wall/content_filter.tpl');
901                         $html = Renderer::replaceMacros($tpl, [
902                                 '$reasons'   => $reasons,
903                                 '$rnd'       => Strings::getRandomHex(8),
904                                 '$openclose' => DI::l10n()->t('Click to open/close'),
905                                 '$html'      => $html
906                         ]);
907                 }
908
909                 return $html;
910         }
911
912         /**
913          * replace html amp entity with amp char
914          * @param string $s
915          * @return string
916          */
917         public static function unamp(string $s): string
918         {
919                 return str_replace('&amp;', '&', $s);
920         }
921
922         /**
923          * Clean an HTML text for potentially harmful code
924          *
925          * @param string $text
926          * @param array  $allowedIframeDomains List of allowed iframe source domains without the scheme
927          * @return string
928          */
929         public static function purify(string $text, array $allowedIframeDomains = []): string
930         {
931                 // Allows cid: URL scheme
932                 \HTMLPurifier_URISchemeRegistry::instance()->register('cid', new HTMLPurifier_URIScheme_cid());
933
934                 $config = \HTMLPurifier_HTML5Config::createDefault();
935                 $config->set('HTML.Doctype', 'HTML5');
936
937                 // Used to remove iframe with src attribute filtered out
938                 $config->set('AutoFormat.RemoveEmpty', true);
939
940                 $config->set('HTML.SafeIframe', true);
941
942                 array_walk($allowedIframeDomains, function (&$domain) {
943                         // Allow the domain and all its eventual sub-domains
944                         $domain = '(?:(?!-)[A-Za-z0-9-]{1,63}(?<!-)\.)*' . preg_quote(trim($domain, '/'), '%');
945                 });
946
947                 $config->set(
948                         'URI.SafeIframeRegexp',
949                         '%^https://(?:
950                                 ' . implode('|', $allowedIframeDomains) . '
951                         )
952                         (?:/|$) # Prevents bogus domains like youtube.com.fake.tld
953                         %xi'
954                 );
955
956                 $config->set('Attr.AllowedRel', [
957                         'noreferrer' => true,
958                         'noopener' => true,
959                         'tag' => true,
960                 ]);
961                 $config->set('Attr.AllowedFrameTargets', [
962                         '_blank' => true,
963                 ]);
964
965                 $config->set('AutoFormat.RemoveEmpty.Predicate', [
966                         'colgroup' => [],        // |
967                         'th'       => [],        // |
968                         'td'       => [],        // |
969                         'iframe'   => ['src'],   // ↳ Default HTMLPurify values
970                         'i'        => ['class'], // Allows forkawesome icons
971                 ]);
972
973                 // Uncomment to debug HTMLPurifier behavior
974                 //$config->set('Core.CollectErrors', true);
975                 //$config->set('Core.MaintainLineNumbers', true);
976
977                 $HTMLPurifier = new \HTMLPurifier($config);
978
979                 $text = $HTMLPurifier->purify($text);
980
981                 /** @var \HTMLPurifier_ErrorCollector $errorCollector */
982                 // Uncomment to debug HTML Purifier behavior
983                 //$errorCollector = $HTMLPurifier->context->get('ErrorCollector');
984                 //var_dump($errorCollector->getRaw());
985
986                 return $text;
987         }
988
989         /**
990          * XPath arbitrary string quoting
991          *
992          * @see https://stackoverflow.com/a/45228168
993          * @param string $value
994          * @return string
995          */
996         public static function xpathQuote(string $value): string
997         {
998                 if (false === strpos($value, '"')) {
999                         return '"' . $value . '"';
1000                 }
1001
1002                 if (false === strpos($value, "'")) {
1003                         return "'" . $value . "'";
1004                 }
1005
1006                 // if the value contains both single and double quotes, construct an
1007                 // expression that concatenates all non-double-quote substrings with
1008                 // the quotes, e.g.:
1009                 //
1010                 //    concat("'foo'", '"', "bar")
1011                 return 'concat(' . implode(', \'"\', ', array_map([self::class, 'xpathQuote'], explode('"', $value))) . ')';
1012         }
1013
1014         /**
1015          * Checks if the provided URL is present in the DOM document in an element with the rel="me" attribute
1016          *
1017          * XHTML Friends Network http://gmpg.org/xfn/
1018          *
1019          * @param DOMDocument  $doc
1020          * @param UriInterface $meUrl
1021          * @return bool
1022          */
1023         public static function checkRelMeLink(DOMDocument $doc, UriInterface $meUrl): bool
1024         {
1025                 $xpath = new \DOMXpath($doc);
1026
1027                 // This expression checks that "me" is among the space-delimited values of the "rel" attribute.
1028                 // And that the href attribute contains exactly the provided URL
1029                 $expression = "//*[contains(concat(' ', normalize-space(@rel), ' '), ' me ')][@href = " . self::xpathQuote($meUrl) . "]";
1030
1031                 $result = $xpath->query($expression);
1032
1033                 return $result !== false && $result->length > 0;
1034         }
1035
1036         /**
1037          * @param DOMDocument $doc
1038          * @return string|null Lowercase charset
1039          */
1040         public static function extractCharset(DOMDocument $doc): ?string
1041         {
1042                 $xpath = new DOMXPath($doc);
1043
1044                 $expression = "string(//meta[@charset]/@charset)";
1045                 if ($charset = $xpath->evaluate($expression)) {
1046                         return strtolower($charset);
1047                 }
1048
1049                 try {
1050                         // This expression looks for a meta tag with the http-equiv attribute set to "content-type" ignoring case
1051                         // whose content attribute contains a "charset" string and returns its value
1052                         $expression = "string(//meta[@http-equiv][translate(@http-equiv, 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz') = 'content-type'][contains(translate(@content, 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz'), 'charset')]/@content)";
1053                         $mediaType = MediaType::fromContentType($xpath->evaluate($expression));
1054                         if (isset($mediaType->parameters['charset'])) {
1055                                 return strtolower($mediaType->parameters['charset']);
1056                         }
1057                 } catch (\InvalidArgumentException $e) {
1058                 }
1059
1060                 return null;
1061         }
1062 }