]> git.mxchange.org Git - friendica.git/blob - src/Content/Text/HTML.php
Merge pull request #12591 from MrPetovan/task/2023-licence
[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\Content\Widget\ContactBlock;
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', [], "[list]", "[/list]");
284                         self::tagToBBCode($doc, 'ol', [], "[list=1]", "[/list]");
285                         self::tagToBBCode($doc, '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                         do {
352                                 $oldmessage = $message;
353                                 $message = str_replace(
354                                         [
355                                                 "[/size]\n\n",
356                                                 "\n[hr]",
357                                                 "[hr]\n",
358                                                 "\n[list",
359                                                 "[/list]\n",
360                                                 "\n[/",
361                                                 "[list]\n",
362                                                 "[list=1]\n",
363                                                 "\n[*]"],
364                                         [
365                                                 "[/size]\n",
366                                                 "[hr]",
367                                                 "[hr]",
368                                                 "[list",
369                                                 "[/list]",
370                                                 "[/",
371                                                 "[list]",
372                                                 "[list=1]",
373                                                 "[*]"],
374                                         $message
375                                 );
376                         } while ($message != $oldmessage);
377
378                         $message = str_replace(
379                                 ['[b][b]', '[/b][/b]', '[i][i]', '[/i][/i]'],
380                                 ['[b]', '[/b]', '[i]', '[/i]'],
381                                 $message
382                         );
383
384                         // Handling Yahoo style of mails
385                         $message = str_replace('[hr][b]From:[/b]', '[quote][b]From:[/b]', $message);
386
387                         return $message;
388                 });
389
390                 $message = preg_replace_callback(
391                         '#<pre><code(?: class="language-([^"]*)")?>(.*)</code></pre>#iUs',
392                         function ($matches) {
393                                 $prefix = '[code]';
394                                 if ($matches[1] != '') {
395                                         $prefix = '[code=' . $matches[1] . ']';
396                                 }
397
398                                 return $prefix . "\n" . html_entity_decode($matches[2]) . "\n" . '[/code]';
399                         },
400                         $message
401                 );
402
403                 $message = trim($message);
404
405                 if ($basepath != '') {
406                         $message = self::qualifyURLs($message, $basepath);
407                 }
408
409                 DI::profiler()->stopRecording();
410                 return $message;
411         }
412
413         /**
414          * Sub function to complete incomplete URL
415          *
416          * @param array  $matches  Result of preg_replace_callback
417          * @param string $basepath Basepath that is used to complete the URL
418          *
419          * @return string The expanded URL
420          */
421         private static function qualifyURLsSub(array $matches, string $basepath): string
422         {
423                 $base = parse_url($basepath);
424                 unset($base['query']);
425                 unset($base['fragment']);
426
427                 $link = $matches[0];
428                 $url = $matches[1];
429
430                 if (empty($url) || empty(parse_url($url))) {
431                         return $matches[0];
432                 }
433
434                 $parts = array_merge($base, parse_url($url));
435                 $url2 = Network::unparseURL($parts);
436
437                 return str_replace($url, $url2, $link);
438         }
439
440         /**
441          * Complete incomplete URLs in BBCode
442          *
443          * @param string $body     Body with URLs
444          * @param string $basepath Base path that is used to complete the URL
445          *
446          * @return string Body with expanded URLs
447          */
448         private static function qualifyURLs(string $body, string $basepath): string
449         {
450                 $URLSearchString = "^\[\]";
451
452                 $matches = ["/\[url\=([$URLSearchString]*)\].*?\[\/url\]/ism",
453                         "/\[url\]([$URLSearchString]*)\[\/url\]/ism",
454                         "/\[img\=[0-9]*x[0-9]*\](.*?)\[\/img\]/ism",
455                         "/\[img\](.*?)\[\/img\]/ism",
456                         "/\[zmg\=[0-9]*x[0-9]*\](.*?)\[\/img\]/ism",
457                         "/\[zmg\](.*?)\[\/zmg\]/ism",
458                         "/\[video\](.*?)\[\/video\]/ism",
459                         "/\[audio\](.*?)\[\/audio\]/ism",
460                 ];
461
462                 foreach ($matches as $match) {
463                         $body = preg_replace_callback(
464                                 $match,
465                                 function ($match) use ($basepath) {
466                                         return self::qualifyURLsSub($match, $basepath);
467                                 },
468                                 $body
469                         );
470                 }
471                 return $body;
472         }
473
474         private static function breakLines(string $line, int $level, int $wraplength = 75): string
475         {
476                 if ($wraplength == 0) {
477                         $wraplength = 2000000;
478                 }
479
480                 $wraplen = $wraplength - $level;
481
482                 $newlines = [];
483
484                 do {
485                         $oldline = $line;
486
487                         $subline = substr($line, 0, $wraplen);
488
489                         $pos = strrpos($subline, ' ');
490
491                         if ($pos == 0) {
492                                 $pos = strpos($line, ' ');
493                         }
494
495                         if (($pos > 0) && strlen($line) > $wraplen) {
496                                 $newline = trim(substr($line, 0, $pos));
497                                 if ($level > 0) {
498                                         $newline = str_repeat(">", $level) . ' ' . $newline;
499                                 }
500
501                                 $newlines[] = $newline . " ";
502                                 $line = substr($line, $pos + 1);
503                         }
504                 } while ((strlen($line) > $wraplen) && !($oldline == $line));
505
506                 if ($level > 0) {
507                         $line = str_repeat(">", $level) . ' ' . $line;
508                 }
509
510                 $newlines[] = $line;
511
512                 return implode("\n", $newlines);
513         }
514
515         private static function quoteLevel(string $message, int $wraplength = 75): string
516         {
517                 $lines = explode("\n", $message);
518
519                 $newlines = [];
520                 $level = 0;
521                 foreach ($lines as $line) {
522                         $line = trim($line);
523                         $startquote = false;
524                         while (strpos("*" . $line, '[quote]') > 0) {
525                                 $level++;
526                                 $pos = strpos($line, '[quote]');
527                                 $line = substr($line, 0, $pos) . substr($line, $pos + 7);
528                                 $startquote = true;
529                         }
530
531                         $currlevel = $level;
532
533                         while (strpos("*" . $line, '[/quote]') > 0) {
534                                 $level--;
535                                 if ($level < 0) {
536                                         $level = 0;
537                                 }
538
539                                 $pos = strpos($line, '[/quote]');
540                                 $line = substr($line, 0, $pos) . substr($line, $pos + 8);
541                         }
542
543                         if (!$startquote || ($line != '')) {
544                                 $newlines[] = self::breakLines($line, $currlevel, $wraplength);
545                         }
546                 }
547
548                 return implode("\n", $newlines);
549         }
550
551         private static function collectURLs(string $message): array
552         {
553                 $pattern = '/<a.*?href="(.*?)".*?>(.*?)<\/a>/is';
554                 preg_match_all($pattern, $message, $result, PREG_SET_ORDER);
555
556                 $urls = [];
557                 foreach ($result as $treffer) {
558                         $ignore = false;
559
560                         // A list of some links that should be ignored
561                         $list = ["/user/", "/tag/", "/group/", "/profile/", "/search?search=", "/search?tag=", "mailto:", "/u/", "/node/",
562                                 "//plus.google.com/", "//twitter.com/"];
563                         foreach ($list as $listitem) {
564                                 if (strpos($treffer[1], $listitem) !== false) {
565                                         $ignore = true;
566                                 }
567                         }
568
569                         if ((strpos($treffer[1], "//twitter.com/") !== false) && (strpos($treffer[1], "/status/") !== false)) {
570                                 $ignore = false;
571                         }
572
573                         if ((strpos($treffer[1], "//plus.google.com/") !== false) && (strpos($treffer[1], "/posts") !== false)) {
574                                 $ignore = false;
575                         }
576
577                         if ((strpos($treffer[1], "//plus.google.com/") !== false) && (strpos($treffer[1], "/photos") !== false)) {
578                                 $ignore = false;
579                         }
580
581                         $ignore = $ignore || strpos($treffer[1], '#') === 0;
582
583                         if (!$ignore) {
584                                 $urls[$treffer[1]] = $treffer[1];
585                         }
586                 }
587
588                 return $urls;
589         }
590
591         /**
592          * @param string $html
593          * @param int    $wraplength Ensures individual lines aren't longer than this many characters. Doesn't break words.
594          * @param bool   $compact    True: Completely strips image tags; False: Keeps image URLs
595          * @return string
596          */
597         public static function toPlaintext(string $html, int $wraplength = 75, bool $compact = false): string
598         {
599                 DI::profiler()->startRecording('rendering');
600                 $message = str_replace("\r", "", $html);
601
602                 $doc = new DOMDocument();
603                 $doc->preserveWhiteSpace = false;
604
605                 $message = mb_convert_encoding($message, 'HTML-ENTITIES', "UTF-8");
606
607                 if (empty($message)) {
608                         DI::profiler()->stopRecording();
609                         return '';
610                 }
611
612                 @$doc->loadHTML($message, LIBXML_HTML_NODEFDTD);
613
614                 $message = $doc->saveHTML();
615                 // Remove eventual UTF-8 BOM
616                 $message = str_replace("\xC3\x82\xC2\xA0", "", $message);
617
618                 // Collecting all links
619                 $urls = self::collectURLs($message);
620
621                 if (empty($message)) {
622                         DI::profiler()->stopRecording();
623                         return '';
624                 }
625
626                 @$doc->loadHTML($message, LIBXML_HTML_NODEFDTD);
627
628                 self::tagToBBCode($doc, 'html', [], '', '');
629                 self::tagToBBCode($doc, 'body', [], '', '');
630
631                 if ($compact) {
632                         self::tagToBBCode($doc, 'blockquote', [], "»", "«");
633                 } else {
634                         self::tagToBBCode($doc, 'blockquote', [], '[quote]', "[/quote]\n");
635                 }
636
637                 self::tagToBBCode($doc, 'br', [], "\n", '');
638
639                 self::tagToBBCode($doc, 'span', [], "", "");
640                 self::tagToBBCode($doc, 'pre', [], "", "");
641                 self::tagToBBCode($doc, 'div', [], "\r", "\r");
642                 self::tagToBBCode($doc, 'p', [], "\n", "\n");
643
644                 self::tagToBBCode($doc, 'li', [], "\n* ", "\n");
645
646                 self::tagToBBCode($doc, 'hr', [], "\n" . str_repeat("-", 70) . "\n", "");
647
648                 self::tagToBBCode($doc, 'tr', [], "\n", "");
649                 self::tagToBBCode($doc, 'td', [], "\t", "");
650
651                 self::tagToBBCode($doc, 'h1', [], "\n\n*", "*\n");
652                 self::tagToBBCode($doc, 'h2', [], "\n\n*", "*\n");
653                 self::tagToBBCode($doc, 'h3', [], "\n\n*", "*\n");
654                 self::tagToBBCode($doc, 'h4', [], "\n\n*", "*\n");
655                 self::tagToBBCode($doc, 'h5', [], "\n\n*", "*\n");
656                 self::tagToBBCode($doc, 'h6', [], "\n\n*", "*\n");
657
658                 if (!$compact) {
659                         self::tagToBBCode($doc, 'img', ['src' => '/(.+)/'], ' [img]$1', '[/img] ');
660                 } else {
661                         self::tagToBBCode($doc, 'img', ['src' => '/(.+)/'], ' ', ' ');
662                 }
663
664                 // Backward compatibility, [iframe] support has been removed in version 2020.12
665                 self::tagToBBCode($doc, 'iframe', ['src' => '/(.+)/'], ' $1 ', '');
666
667                 $message = $doc->saveHTML();
668
669                 if (!$compact) {
670                         $message = str_replace("[img]", "", $message);
671                         $message = str_replace("[/img]", "", $message);
672                 }
673
674                 // was ersetze ich da?
675                 // Irgendein stoerrisches UTF-Zeug
676                 $message = str_replace(chr(194) . chr(160), ' ', $message);
677
678                 $message = str_replace("&nbsp;", " ", $message);
679
680                 // Aufeinanderfolgende DIVs
681                 $message = preg_replace('=\r *\r=i', "\n", $message);
682                 $message = str_replace("\r", "\n", $message);
683
684                 $message = strip_tags($message);
685
686                 $message = html_entity_decode($message, ENT_QUOTES, 'UTF-8');
687
688                 if (!$compact && ($message != '')) {
689                         foreach ($urls as $id => $url) {
690                                 if ($url != '' && strpos($message, $url) === false) {
691                                         $message .= "\n" . $url . ' ';
692                                 }
693                         }
694                 }
695
696                 $message = str_replace("\n«", "«\n", $message);
697                 $message = str_replace("»\n", "\n»", $message);
698
699                 do {
700                         $oldmessage = $message;
701                         $message = str_replace("\n\n\n", "\n\n", $message);
702                 } while ($oldmessage != $message);
703
704                 $message = self::quoteLevel(trim($message), $wraplength);
705
706                 DI::profiler()->stopRecording();
707                 return trim($message);
708         }
709
710         /**
711          * Converts provided HTML code to Markdown. The hardwrap parameter maximizes
712          * compatibility with Diaspora in spite of the Markdown standards.
713          *
714          * @param string $html
715          * @return string
716          */
717         public static function toMarkdown(string $html): string
718         {
719                 DI::profiler()->startRecording('rendering');
720                 $converter = new HtmlConverter(['hard_break' => true]);
721                 $markdown = $converter->convert($html);
722
723                 DI::profiler()->stopRecording();
724                 return $markdown;
725         }
726
727         /**
728          * Convert video HTML to BBCode tags
729          *
730          * @param string $s
731          * @return string
732          */
733         public static function toBBCodeVideo(string $s): string
734         {
735                 $s = preg_replace(
736                         '#<object[^>]+>(.*?)https?://www.youtube.com/((?:v|cp)/[A-Za-z0-9\-_=]+)(.*?)</object>#ism',
737                         '[youtube]$2[/youtube]',
738                         $s
739                 );
740
741                 $s = preg_replace(
742                         '#<iframe[^>](.*?)https?://www.youtube.com/embed/([A-Za-z0-9\-_=]+)(.*?)</iframe>#ism',
743                         '[youtube]$2[/youtube]',
744                         $s
745                 );
746
747                 $s = preg_replace(
748                         '#<iframe[^>](.*?)https?://player.vimeo.com/video/([0-9]+)(.*?)</iframe>#ism',
749                         '[vimeo]$2[/vimeo]',
750                         $s
751                 );
752
753                 return $s;
754         }
755
756         /**
757          * transform link href and img src from relative to absolute
758          *
759          * @param string $text
760          * @param string $base base url
761          * @return string
762          */
763         public static function relToAbs(string $text, string $base): string
764         {
765                 if (empty($base)) {
766                         return $text;
767                 }
768
769                 $base = rtrim($base, '/');
770
771                 $base2 = $base . "/";
772
773                 // Replace links
774                 $pattern = "/<a([^>]*) href=\"(?!http|https|\/)([^\"]*)\"/";
775                 $replace = "<a\${1} href=\"" . $base2 . "\${2}\"";
776                 $text = preg_replace($pattern, $replace, $text);
777
778                 $pattern = "/<a([^>]*) href=\"(?!http|https)([^\"]*)\"/";
779                 $replace = "<a\${1} href=\"" . $base . "\${2}\"";
780                 $text = preg_replace($pattern, $replace, $text);
781
782                 // Replace images
783                 $pattern = "/<img([^>]*) src=\"(?!http|https|\/)([^\"]*)\"/";
784                 $replace = "<img\${1} src=\"" . $base2 . "\${2}\"";
785                 $text = preg_replace($pattern, $replace, $text);
786
787                 $pattern = "/<img([^>]*) src=\"(?!http|https)([^\"]*)\"/";
788                 $replace = "<img\${1} src=\"" . $base . "\${2}\"";
789                 $text = preg_replace($pattern, $replace, $text);
790
791
792                 // Done
793                 return $text;
794         }
795
796         /**
797          * Loader for infinite scrolling
798          *
799          * @return string html for loader
800          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
801          */
802         public static function scrollLoader(): string
803         {
804                 $tpl = Renderer::getMarkupTemplate("scroll_loader.tpl");
805                 return Renderer::replaceMacros($tpl, [
806                         'wait' => DI::l10n()->t('Loading more entries...'),
807                         'end' => DI::l10n()->t('The end')
808                 ]);
809         }
810
811         /**
812          * Format contacts as picture links or as text links
813          *
814          * @param array   $contact  Array with contacts which contains an array with
815          *                          int 'id' => The ID of the contact
816          *                          int 'uid' => The user ID of the user who owns this data
817          *                          string 'name' => The name of the contact
818          *                          string 'url' => The url to the profile page of the contact
819          *                          string 'addr' => The webbie of the contact (e.g.) username@friendica.com
820          *                          string 'network' => The network to which the contact belongs to
821          *                          string 'thumb' => The contact picture
822          *                          string 'click' => js code which is performed when clicking on the contact
823          * @param boolean $redirect If true try to use the redir url if it's possible
824          * @param string  $class    CSS class for the
825          * @param boolean $textmode If true display the contacts as text links
826          *                          if false display the contacts as picture links
827          * @return string Formatted html
828          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
829          * @throws \ImagickException
830          */
831         public static function micropro(array $contact, bool $redirect = false, string $class = '', bool $textmode = false): string
832         {
833                 // Use the contact URL if no address is available
834                 if (empty($contact['addr'])) {
835                         $contact["addr"] = $contact["url"];
836                 }
837
838                 $url = $contact['url'];
839                 $sparkle = '';
840                 $redir = false;
841
842                 if ($redirect) {
843                         $url = Contact::magicLinkByContact($contact);
844                         if (strpos($url, 'contact/redir/') === 0) {
845                                 $sparkle = ' sparkle';
846                         }
847                 }
848
849                 // If there is some js available we don't need the url
850                 if (!empty($contact['click'])) {
851                         $url = '';
852                 }
853
854                 return Renderer::replaceMacros(Renderer::getMarkupTemplate($textmode ? 'micropro_txt.tpl' : 'micropro_img.tpl'), [
855                         '$click' => $contact['click'] ?? '',
856                         '$class' => $class,
857                         '$url' => $url,
858                         '$photo' => Contact::getThumb($contact),
859                         '$name' => $contact['name'],
860                         'title' => $contact['name'] . ' [' . $contact['addr'] . ']',
861                         '$parkle' => $sparkle,
862                         '$redir' => $redir
863                 ]);
864         }
865
866         /**
867          * Search box.
868          *
869          * @param string $s     Search query.
870          * @param string $id    HTML id
871          * @param bool   $aside Display the search widgit aside.
872          *
873          * @return string Formatted HTML.
874          * @throws \Exception
875          */
876         public static function search(string $s, string $id = 'search-box', bool $aside = true): string
877         {
878                 $mode = 'text';
879
880                 if (strpos($s, '#') === 0) {
881                         $mode = 'tag';
882                 }
883                 $save_label = $mode === 'text' ? DI::l10n()->t('Save') : DI::l10n()->t('Follow');
884
885                 $values = [
886                         '$s'            => $s,
887                         '$q'            => urlencode($s),
888                         '$id'           => $id,
889                         '$search_label' => DI::l10n()->t('Search'),
890                         '$save_label'   => $save_label,
891                         '$search_hint'  => DI::l10n()->t('@name, !forum, #tags, content'),
892                         '$mode'         => $mode,
893                         '$return_url'   => urlencode(Search::getSearchPath($s)),
894                 ];
895
896                 if (!$aside) {
897                         $values['$search_options'] = [
898                                 'fulltext' => DI::l10n()->t('Full Text'),
899                                 'tags'     => DI::l10n()->t('Tags'),
900                                 'contacts' => DI::l10n()->t('Contacts')
901                         ];
902
903                         if (DI::config()->get('system', 'poco_local_search')) {
904                                 $values['$searchoption']['forums'] = DI::l10n()->t('Forums');
905                         }
906                 }
907
908                 return Renderer::replaceMacros(Renderer::getMarkupTemplate('searchbox.tpl'), $values);
909         }
910
911         /**
912          * Given a HTML text and a set of filtering reasons, adds a content hiding header with the provided reasons
913          *
914          * Reasons are expected to have been translated already.
915          *
916          * @param string $html
917          * @param array  $reasons
918          * @return string
919          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
920          */
921         public static function applyContentFilter(string $html, array $reasons): string
922         {
923                 if (count($reasons)) {
924                         $tpl = Renderer::getMarkupTemplate('wall/content_filter.tpl');
925                         $html = Renderer::replaceMacros($tpl, [
926                                 '$reasons'   => $reasons,
927                                 '$rnd'       => Strings::getRandomHex(8),
928                                 '$openclose' => DI::l10n()->t('Click to open/close'),
929                                 '$html'      => $html
930                         ]);
931                 }
932
933                 return $html;
934         }
935
936         /**
937          * replace html amp entity with amp char
938          * @param string $s
939          * @return string
940          */
941         public static function unamp(string $s): string
942         {
943                 return str_replace('&amp;', '&', $s);
944         }
945
946         /**
947          * Clean an HTML text for potentially harmful code
948          *
949          * @param string $text
950          * @param array  $allowedIframeDomains List of allowed iframe source domains without the scheme
951          * @return string
952          */
953         public static function purify(string $text, array $allowedIframeDomains = []): string
954         {
955                 // Allows cid: URL scheme
956                 \HTMLPurifier_URISchemeRegistry::instance()->register('cid', new HTMLPurifier_URIScheme_cid());
957
958                 $config = \HTMLPurifier_HTML5Config::createDefault();
959                 $config->set('HTML.Doctype', 'HTML5');
960
961                 // Used to remove iframe with src attribute filtered out
962                 $config->set('AutoFormat.RemoveEmpty', true);
963
964                 $config->set('HTML.SafeIframe', true);
965
966                 array_walk($allowedIframeDomains, function (&$domain) {
967                         // Allow the domain and all its eventual sub-domains
968                         $domain = '(?:(?!-)[A-Za-z0-9-]{1,63}(?<!-)\.)*' . preg_quote(trim($domain, '/'), '%');
969                 });
970
971                 $config->set('URI.SafeIframeRegexp',
972                         '%^https://(?:
973                                 ' . implode('|', $allowedIframeDomains) . '
974                         )
975                         (?:/|$) # Prevents bogus domains like youtube.com.fake.tld
976                         %xi'
977                 );
978
979                 $config->set('Attr.AllowedRel', [
980                         'noreferrer' => true,
981                         'noopener' => true,
982                         'tag' => true,
983                 ]);
984                 $config->set('Attr.AllowedFrameTargets', [
985                         '_blank' => true,
986                 ]);
987
988                 $config->set('AutoFormat.RemoveEmpty.Predicate', [
989                         'colgroup' => [],        // |
990                         'th'       => [],        // |
991                         'td'       => [],        // |
992                         'iframe'   => ['src'],   // ↳ Default HTMLPurify values
993                         'i'        => ['class'], // Allows forkawesome icons
994                 ]);
995
996                 // Uncomment to debug HTMLPurifier behavior
997                 //$config->set('Core.CollectErrors', true);
998                 //$config->set('Core.MaintainLineNumbers', true);
999
1000                 $HTMLPurifier = new \HTMLPurifier($config);
1001
1002                 $text = $HTMLPurifier->purify($text);
1003
1004                 /** @var \HTMLPurifier_ErrorCollector $errorCollector */
1005                 // Uncomment to debug HTML Purifier behavior
1006                 //$errorCollector = $HTMLPurifier->context->get('ErrorCollector');
1007                 //var_dump($errorCollector->getRaw());
1008
1009                 return $text;
1010         }
1011
1012         /**
1013          * XPath arbitrary string quoting
1014          *
1015          * @see https://stackoverflow.com/a/45228168
1016          * @param string $value
1017          * @return string
1018          */
1019         public static function xpathQuote(string $value): string
1020         {
1021                 if (false === strpos($value, '"')) {
1022                         return '"' . $value . '"';
1023                 }
1024
1025                 if (false === strpos($value, "'")) {
1026                         return "'" . $value . "'";
1027                 }
1028
1029                 // if the value contains both single and double quotes, construct an
1030                 // expression that concatenates all non-double-quote substrings with
1031                 // the quotes, e.g.:
1032                 //
1033                 //    concat("'foo'", '"', "bar")
1034                 return 'concat(' . implode(', \'"\', ', array_map(['self', 'xpathQuote'], explode('"', $value))) . ')';
1035         }
1036
1037         /**
1038          * Checks if the provided URL is present in the DOM document in an element with the rel="me" attribute
1039          *
1040          * XHTML Friends Network http://gmpg.org/xfn/
1041          *
1042          * @param DOMDocument  $doc
1043          * @param UriInterface $meUrl
1044          * @return bool
1045          */
1046         public static function checkRelMeLink(DOMDocument $doc, UriInterface $meUrl): bool
1047         {
1048                 $xpath = new \DOMXpath($doc);
1049
1050                 // This expression checks that "me" is among the space-delimited values of the "rel" attribute.
1051                 // And that the href attribute contains exactly the provided URL
1052                 $expression = "//*[contains(concat(' ', normalize-space(@rel), ' '), ' me ')][@href = " . self::xpathQuote($meUrl) . "]";
1053
1054                 $result = $xpath->query($expression);
1055
1056                 return $result !== false && $result->length > 0;
1057         }
1058 }