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