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