]> git.mxchange.org Git - friendica.git/blob - src/Content/Text/HTML.php
9a9f24f97f48e1a5542836817d05f1059fcad279
[friendica.git] / src / Content / Text / HTML.php
1 <?php
2
3 /**
4  * @file src/Content/Text/HTML.php
5  */
6
7 namespace Friendica\Content\Text;
8
9 use DOMDocument;
10 use DOMXPath;
11 use Friendica\Core\Addon;
12 use Friendica\Util\Network;
13 use Friendica\Util\XML;
14
15 class HTML
16 {
17         public static function sanitizeCSS($input)
18         {
19                 $cleaned = "";
20
21                 $input = strtolower($input);
22
23                 for ($i = 0; $i < strlen($input); $i++) {
24                         $char = substr($input, $i, 1);
25
26                         if (($char >= "a") && ($char <= "z")) {
27                                 $cleaned .= $char;
28                         }
29
30                         if (!(strpos(" #;:0123456789-_.%", $char) === false)) {
31                                 $cleaned .= $char;
32                         }
33                 }
34
35                 return $cleaned;
36         }
37
38         private static function tagToBBCode(DOMDocument $doc, $tag, $attributes, $startbb, $endbb)
39         {
40                 do {
41                         $done = self::tagToBBCodeSub($doc, $tag, $attributes, $startbb, $endbb);
42                 } while ($done);
43         }
44
45         private static function tagToBBCodeSub(DOMDocument $doc, $tag, $attributes, $startbb, $endbb)
46         {
47                 $savestart = str_replace('$', '\x01', $startbb);
48                 $replace = false;
49
50                 $xpath = new DOMXPath($doc);
51
52                 $list = $xpath->query("//" . $tag);
53                 foreach ($list as $node) {
54                         $attr = [];
55                         if ($node->attributes->length) {
56                                 foreach ($node->attributes as $attribute) {
57                                         $attr[$attribute->name] = $attribute->value;
58                                 }
59                         }
60
61                         $replace = true;
62
63                         $startbb = $savestart;
64
65                         $i = 0;
66
67                         foreach ($attributes as $attribute => $value) {
68                                 $startbb = str_replace('\x01' . ++$i, '$1', $startbb);
69                                 if (strpos('*' . $startbb, '$1') > 0) {
70                                         if ($replace && (@$attr[$attribute] != '')) {
71                                                 $startbb = preg_replace($value, $startbb, $attr[$attribute], -1, $count);
72
73                                                 // If nothing could be changed
74                                                 if ($count == 0) {
75                                                         $replace = false;
76                                                 }
77                                         } else {
78                                                 $replace = false;
79                                         }
80                                 } else {
81                                         if (@$attr[$attribute] != $value) {
82                                                 $replace = false;
83                                         }
84                                 }
85                         }
86
87                         if ($replace) {
88                                 $StartCode = $doc->createTextNode($startbb);
89                                 $EndCode = $doc->createTextNode($endbb);
90
91                                 $node->parentNode->insertBefore($StartCode, $node);
92
93                                 if ($node->hasChildNodes()) {
94                                         foreach ($node->childNodes as $child) {
95                                                 $newNode = $child->cloneNode(true);
96                                                 $node->parentNode->insertBefore($newNode, $node);
97                                         }
98                                 }
99
100                                 $node->parentNode->insertBefore($EndCode, $node);
101                                 $node->parentNode->removeChild($node);
102                         }
103                 }
104
105                 return $replace;
106         }
107
108         /**
109          * Made by: ike@piratenpartei.de
110          * Originally made for the syncom project: http://wiki.piratenpartei.de/Syncom
111          *                                      https://github.com/annando/Syncom
112          *
113          * @brief Converter for HTML to BBCode
114          * @param string $message
115          * @param string $basepath
116          * @return string
117          */
118         public static function toBBCode($message, $basepath = '')
119         {
120                 $message = str_replace("\r", "", $message);
121
122                 // Removing code blocks before the whitespace removal processing below
123                 $codeblocks = [];
124                 $message = preg_replace_callback(
125                         '#<pre><code(?: class="language-([^"]*)")?>(.*)</code></pre>#iUs',
126                         function ($matches) use (&$codeblocks) {
127                                 $return = '[codeblock-' . count($codeblocks) . ']';
128
129                                 $prefix = '[code]';
130                                 if ($matches[1] != '') {
131                                         $prefix = '[code=' . $matches[1] . ']';
132                                 }
133
134                                 $codeblocks[] = $prefix . PHP_EOL . trim($matches[2]) . PHP_EOL . '[/code]';
135                                 return $return;
136                         },
137                         $message
138                 );
139
140                 $message = str_replace(
141                         [
142                         "<li><p>",
143                         "</p></li>",
144                         ], [
145                         "<li>",
146                         "</li>",
147                         ], $message
148                 );
149
150                 // remove namespaces
151                 $message = preg_replace('=<(\w+):(.+?)>=', '<removeme>', $message);
152                 $message = preg_replace('=</(\w+):(.+?)>=', '</removeme>', $message);
153
154                 $doc = new DOMDocument();
155                 $doc->preserveWhiteSpace = false;
156
157                 $message = mb_convert_encoding($message, 'HTML-ENTITIES', "UTF-8");
158
159                 @$doc->loadHTML($message);
160
161                 XML::deleteNode($doc, 'style');
162                 XML::deleteNode($doc, 'head');
163                 XML::deleteNode($doc, 'title');
164                 XML::deleteNode($doc, 'meta');
165                 XML::deleteNode($doc, 'xml');
166                 XML::deleteNode($doc, 'removeme');
167
168                 $xpath = new DomXPath($doc);
169                 $list = $xpath->query("//pre");
170                 foreach ($list as $node) {
171                         $node->nodeValue = str_replace("\n", "\r", $node->nodeValue);
172                 }
173
174                 $message = $doc->saveHTML();
175                 $message = str_replace(["\n<", ">\n", "\r", "\n", "\xC3\x82\xC2\xA0"], ["<", ">", "<br />", " ", ""], $message);
176                 $message = preg_replace('= [\s]*=i', " ", $message);
177                 @$doc->loadHTML($message);
178
179                 self::tagToBBCode($doc, 'html', [], "", "");
180                 self::tagToBBCode($doc, 'body', [], "", "");
181
182                 // Outlook-Quote - Variant 1
183                 self::tagToBBCode($doc, 'p', ['class' => 'MsoNormal', 'style' => 'margin-left:35.4pt'], '[quote]', '[/quote]');
184
185                 // Outlook-Quote - Variant 2
186                 self::tagToBBCode($doc, 'div', ['style' => 'border:none;border-left:solid blue 1.5pt;padding:0cm 0cm 0cm 4.0pt'],
187                         '[quote]', '[/quote]');
188
189                 // MyBB-Stuff
190                 self::tagToBBCode($doc, 'span', ['style' => 'text-decoration: underline;'], '[u]', '[/u]');
191                 self::tagToBBCode($doc, 'span', ['style' => 'font-style: italic;'], '[i]', '[/i]');
192                 self::tagToBBCode($doc, 'span', ['style' => 'font-weight: bold;'], '[b]', '[/b]');
193
194                 /* self::node2BBCode($doc, 'font', array('face'=>'/([\w ]+)/', 'size'=>'/(\d+)/', 'color'=>'/(.+)/'), '[font=$1][size=$2][color=$3]', '[/color][/size][/font]');
195                   self::node2BBCode($doc, 'font', array('size'=>'/(\d+)/', 'color'=>'/(.+)/'), '[size=$1][color=$2]', '[/color][/size]');
196                   self::node2BBCode($doc, 'font', array('face'=>'/([\w ]+)/', 'size'=>'/(.+)/'), '[font=$1][size=$2]', '[/size][/font]');
197                   self::node2BBCode($doc, 'font', array('face'=>'/([\w ]+)/', 'color'=>'/(.+)/'), '[font=$1][color=$3]', '[/color][/font]');
198                   self::node2BBCode($doc, 'font', array('face'=>'/([\w ]+)/'), '[font=$1]', '[/font]');
199                   self::node2BBCode($doc, 'font', array('size'=>'/(\d+)/'), '[size=$1]', '[/size]');
200                   self::node2BBCode($doc, 'font', array('color'=>'/(.+)/'), '[color=$1]', '[/color]');
201                  */
202                 // Untested
203                 //self::node2BBCode($doc, 'span', array('style'=>'/.*font-size:\s*(.+?)[,;].*font-family:\s*(.+?)[,;].*color:\s*(.+?)[,;].*/'), '[size=$1][font=$2][color=$3]', '[/color][/font][/size]');
204                 //self::node2BBCode($doc, 'span', array('style'=>'/.*font-size:\s*(\d+)[,;].*/'), '[size=$1]', '[/size]');
205                 //self::node2BBCode($doc, 'span', array('style'=>'/.*font-size:\s*(.+?)[,;].*/'), '[size=$1]', '[/size]');
206
207                 self::tagToBBCode($doc, 'span', ['style' => '/.*color:\s*(.+?)[,;].*/'], '[color="$1"]', '[/color]');
208
209                 //self::node2BBCode($doc, 'span', array('style'=>'/.*font-family:\s*(.+?)[,;].*/'), '[font=$1]', '[/font]');
210                 //self::node2BBCode($doc, 'div', array('style'=>'/.*font-family:\s*(.+?)[,;].*font-size:\s*(\d+?)pt.*/'), '[font=$1][size=$2]', '[/size][/font]');
211                 //self::node2BBCode($doc, 'div', array('style'=>'/.*font-family:\s*(.+?)[,;].*font-size:\s*(\d+?)px.*/'), '[font=$1][size=$2]', '[/size][/font]');
212                 //self::node2BBCode($doc, 'div', array('style'=>'/.*font-family:\s*(.+?)[,;].*/'), '[font=$1]', '[/font]');
213                 // Importing the classes - interesting for importing of posts from third party networks that were exported from friendica
214                 // Test
215                 //self::node2BBCode($doc, 'span', array('class'=>'/([\w ]+)/'), '[class=$1]', '[/class]');
216                 self::tagToBBCode($doc, 'span', ['class' => 'type-link'], '[class=type-link]', '[/class]');
217                 self::tagToBBCode($doc, 'span', ['class' => 'type-video'], '[class=type-video]', '[/class]');
218
219                 self::tagToBBCode($doc, 'strong', [], '[b]', '[/b]');
220                 self::tagToBBCode($doc, 'em', [], '[i]', '[/i]');
221                 self::tagToBBCode($doc, 'b', [], '[b]', '[/b]');
222                 self::tagToBBCode($doc, 'i', [], '[i]', '[/i]');
223                 self::tagToBBCode($doc, 'u', [], '[u]', '[/u]');
224                 self::tagToBBCode($doc, 's', [], '[s]', '[/s]');
225                 self::tagToBBCode($doc, 'del', [], '[s]', '[/s]');
226                 self::tagToBBCode($doc, 'strike', [], '[s]', '[/s]');
227
228                 self::tagToBBCode($doc, 'big', [], "[size=large]", "[/size]");
229                 self::tagToBBCode($doc, 'small', [], "[size=small]", "[/size]");
230
231                 self::tagToBBCode($doc, 'blockquote', [], '[quote]', '[/quote]');
232
233                 self::tagToBBCode($doc, 'br', [], "\n", '');
234
235                 self::tagToBBCode($doc, 'p', ['class' => 'MsoNormal'], "\n", "");
236                 self::tagToBBCode($doc, 'div', ['class' => 'MsoNormal'], "\r", "");
237
238                 self::tagToBBCode($doc, 'span', [], "", "");
239
240                 self::tagToBBCode($doc, 'span', [], "", "");
241                 self::tagToBBCode($doc, 'pre', [], "", "");
242
243                 self::tagToBBCode($doc, 'div', [], "\r", "\r");
244                 self::tagToBBCode($doc, 'p', [], "\n", "\n");
245
246                 self::tagToBBCode($doc, 'ul', [], "[list]", "[/list]");
247                 self::tagToBBCode($doc, 'ol', [], "[list=1]", "[/list]");
248                 self::tagToBBCode($doc, 'li', [], "[*]", "");
249
250                 self::tagToBBCode($doc, 'hr', [], "[hr]", "");
251
252                 self::tagToBBCode($doc, 'table', [], "", "");
253                 self::tagToBBCode($doc, 'tr', [], "\n", "");
254                 self::tagToBBCode($doc, 'td', [], "\t", "");
255                 //self::node2BBCode($doc, 'table', array(), "[table]", "[/table]");
256                 //self::node2BBCode($doc, 'th', array(), "[th]", "[/th]");
257                 //self::node2BBCode($doc, 'tr', array(), "[tr]", "[/tr]");
258                 //self::node2BBCode($doc, 'td', array(), "[td]", "[/td]");
259                 //self::node2BBCode($doc, 'h1', array(), "\n\n[size=xx-large][b]", "[/b][/size]\n");
260                 //self::node2BBCode($doc, 'h2', array(), "\n\n[size=x-large][b]", "[/b][/size]\n");
261                 //self::node2BBCode($doc, 'h3', array(), "\n\n[size=large][b]", "[/b][/size]\n");
262                 //self::node2BBCode($doc, 'h4', array(), "\n\n[size=medium][b]", "[/b][/size]\n");
263                 //self::node2BBCode($doc, 'h5', array(), "\n\n[size=small][b]", "[/b][/size]\n");
264                 //self::node2BBCode($doc, 'h6', array(), "\n\n[size=x-small][b]", "[/b][/size]\n");
265
266                 self::tagToBBCode($doc, 'h1', [], "[h1]", "[/h1]");
267                 self::tagToBBCode($doc, 'h2', [], "[h2]", "[/h2]");
268                 self::tagToBBCode($doc, 'h3', [], "[h3]", "[/h3]");
269                 self::tagToBBCode($doc, 'h4', [], "[h4]", "[/h4]");
270                 self::tagToBBCode($doc, 'h5', [], "[h5]", "[/h5]");
271                 self::tagToBBCode($doc, 'h6', [], "[h6]", "[/h6]");
272
273                 self::tagToBBCode($doc, 'a', ['href' => '/mailto:(.+)/'], '[mail=$1]', '[/mail]');
274                 self::tagToBBCode($doc, 'a', ['href' => '/(.+)/'], '[url=$1]', '[/url]');
275
276                 self::tagToBBCode($doc, 'img', ['src' => '/(.+)/', 'width' => '/(\d+)/', 'height' => '/(\d+)/'], '[img=$2x$3]$1',
277                         '[/img]');
278                 self::tagToBBCode($doc, 'img', ['src' => '/(.+)/'], '[img]$1', '[/img]');
279
280
281                 self::tagToBBCode($doc, 'video', ['src' => '/(.+)/'], '[video]$1', '[/video]');
282                 self::tagToBBCode($doc, 'audio', ['src' => '/(.+)/'], '[audio]$1', '[/audio]');
283                 self::tagToBBCode($doc, 'iframe', ['src' => '/(.+)/'], '[iframe]$1', '[/iframe]');
284
285                 self::tagToBBCode($doc, 'key', [], '[code]', '[/code]');
286                 self::tagToBBCode($doc, 'code', [], '[code]', '[/code]');
287
288                 $message = $doc->saveHTML();
289
290                 // I'm removing something really disturbing
291                 // Don't know exactly what it is
292                 $message = str_replace(chr(194) . chr(160), ' ', $message);
293
294                 $message = str_replace("&nbsp;", " ", $message);
295
296                 // removing multiple DIVs
297                 $message = preg_replace('=\r *\r=i', "\n", $message);
298                 $message = str_replace("\r", "\n", $message);
299
300                 Addon::callHooks('html2bbcode', $message);
301
302                 $message = strip_tags($message);
303
304                 $message = html_entity_decode($message, ENT_QUOTES, 'UTF-8');
305
306                 $message = str_replace(["<"], ["&lt;"], $message);
307
308                 // remove quotes if they don't make sense
309                 $message = preg_replace('=\[/quote\][\s]*\[quote\]=i', "\n", $message);
310
311                 $message = preg_replace('=\[quote\]\s*=i', "[quote]", $message);
312                 $message = preg_replace('=\s*\[/quote\]=i', "[/quote]", $message);
313
314                 do {
315                         $oldmessage = $message;
316                         $message = str_replace("\n \n", "\n\n", $message);
317                 } while ($oldmessage != $message);
318
319                 do {
320                         $oldmessage = $message;
321                         $message = str_replace("\n\n\n", "\n\n", $message);
322                 } while ($oldmessage != $message);
323
324                 do {
325                         $oldmessage = $message;
326                         $message = str_replace(
327                                 [
328                                 "[/size]\n\n",
329                                 "\n[hr]",
330                                 "[hr]\n",
331                                 "\n[list",
332                                 "[/list]\n",
333                                 "\n[/",
334                                 "[list]\n",
335                                 "[list=1]\n",
336                                 "\n[*]"],
337                                 [
338                                 "[/size]\n",
339                                 "[hr]",
340                                 "[hr]",
341                                 "[list",
342                                 "[/list]",
343                                 "[/",
344                                 "[list]",
345                                 "[list=1]",
346                                 "[*]"], $message
347                         );
348                 } while ($message != $oldmessage);
349
350                 $message = str_replace(
351                         ['[b][b]', '[/b][/b]', '[i][i]', '[/i][/i]'], ['[b]', '[/b]', '[i]', '[/i]'], $message
352                 );
353
354                 // Handling Yahoo style of mails
355                 $message = str_replace('[hr][b]From:[/b]', '[quote][b]From:[/b]', $message);
356
357                 // Restore code blocks
358                 $message = preg_replace_callback(
359                         '#\[codeblock-([0-9]+)\]#iU',
360                         function ($matches) use ($codeblocks) {
361                                 $return = '';
362                                 if (isset($codeblocks[intval($matches[1])])) {
363                                         $return = $codeblocks[$matches[1]];
364                                 }
365                                 return $return;
366                         },
367                         $message
368                 );
369
370                 $message = trim($message);
371
372                 if ($basepath != '') {
373                         $message = self::qualifyURLs($message, $basepath);
374                 }
375
376                 return $message;
377         }
378
379         /**
380          * @brief Sub function to complete incomplete URL
381          *
382          * @param array  $matches  Result of preg_replace_callback
383          * @param string $basepath Basepath that is used to complete the URL
384          *
385          * @return string The expanded URL
386          */
387         private static function qualifyURLsSub($matches, $basepath)
388         {
389                 $base = parse_url($basepath);
390                 unset($base['query']);
391                 unset($base['fragment']);
392
393                 $link = $matches[0];
394                 $url = $matches[1];
395
396                 $parts = array_merge($base, parse_url($url));
397                 $url2 = Network::unparseURL($parts);
398
399                 return str_replace($url, $url2, $link);
400         }
401
402         /**
403          * @brief Complete incomplete URLs in BBCode
404          *
405          * @param string $body     Body with URLs
406          * @param string $basepath Base path that is used to complete the URL
407          *
408          * @return string Body with expanded URLs
409          */
410         private static function qualifyURLs($body, $basepath)
411         {
412                 $URLSearchString = "^\[\]";
413
414                 $matches = ["/\[url\=([$URLSearchString]*)\].*?\[\/url\]/ism",
415                         "/\[url\]([$URLSearchString]*)\[\/url\]/ism",
416                         "/\[img\=[0-9]*x[0-9]*\](.*?)\[\/img\]/ism",
417                         "/\[img\](.*?)\[\/img\]/ism",
418                         "/\[zmg\=[0-9]*x[0-9]*\](.*?)\[\/img\]/ism",
419                         "/\[zmg\](.*?)\[\/zmg\]/ism",
420                         "/\[video\](.*?)\[\/video\]/ism",
421                         "/\[audio\](.*?)\[\/audio\]/ism",
422                 ];
423
424                 foreach ($matches as $match) {
425                         $body = preg_replace_callback(
426                                 $match, function ($match) use ($basepath) {
427                                         return self::qualifyURLsSub($match, $basepath);
428                                 },
429                                 $body
430                         );
431                 }
432                 return $body;
433         }
434
435         private static function breakLines($line, $level, $wraplength = 75)
436         {
437                 if ($wraplength == 0) {
438                         $wraplength = 2000000;
439                 }
440
441                 $wraplen = $wraplength - $level;
442
443                 $newlines = [];
444
445                 do {
446                         $oldline = $line;
447
448                         $subline = substr($line, 0, $wraplen);
449
450                         $pos = strrpos($subline, ' ');
451
452                         if ($pos == 0) {
453                                 $pos = strpos($line, ' ');
454                         }
455
456                         if (($pos > 0) && strlen($line) > $wraplen) {
457                                 $newline = trim(substr($line, 0, $pos));
458                                 if ($level > 0) {
459                                         $newline = str_repeat(">", $level) . ' ' . $newline;
460                                 }
461
462                                 $newlines[] = $newline . " ";
463                                 $line = substr($line, $pos + 1);
464                         }
465                 } while ((strlen($line) > $wraplen) && !($oldline == $line));
466
467                 if ($level > 0) {
468                         $line = str_repeat(">", $level) . ' ' . $line;
469                 }
470
471                 $newlines[] = $line;
472
473                 return implode($newlines, "\n");
474         }
475
476         private static function quoteLevel($message, $wraplength = 75)
477         {
478                 $lines = explode("\n", $message);
479
480                 $newlines = [];
481                 $level = 0;
482                 foreach ($lines as $line) {
483                         $line = trim($line);
484                         $startquote = false;
485                         while (strpos("*" . $line, '[quote]') > 0) {
486                                 $level++;
487                                 $pos = strpos($line, '[quote]');
488                                 $line = substr($line, 0, $pos) . substr($line, $pos + 7);
489                                 $startquote = true;
490                         }
491
492                         $currlevel = $level;
493
494                         while (strpos("*" . $line, '[/quote]') > 0) {
495                                 $level--;
496                                 if ($level < 0) {
497                                         $level = 0;
498                                 }
499
500                                 $pos = strpos($line, '[/quote]');
501                                 $line = substr($line, 0, $pos) . substr($line, $pos + 8);
502                         }
503
504                         if (!$startquote || ($line != '')) {
505                                 $newlines[] = self::breakLines($line, $currlevel, $wraplength);
506                         }
507                 }
508
509                 return implode($newlines, "\n");
510         }
511
512         private static function collectURLs($message)
513         {
514                 $pattern = '/<a.*?href="(.*?)".*?>(.*?)<\/a>/is';
515                 preg_match_all($pattern, $message, $result, PREG_SET_ORDER);
516
517                 $urls = [];
518                 foreach ($result as $treffer) {
519                         $ignore = false;
520
521                         // A list of some links that should be ignored
522                         $list = ["/user/", "/tag/", "/group/", "/profile/", "/search?search=", "/search?tag=", "mailto:", "/u/", "/node/",
523                                 "//plus.google.com/", "//twitter.com/"];
524                         foreach ($list as $listitem) {
525                                 if (strpos($treffer[1], $listitem) !== false) {
526                                         $ignore = true;
527                                 }
528                         }
529
530                         if ((strpos($treffer[1], "//twitter.com/") !== false) && (strpos($treffer[1], "/status/") !== false)) {
531                                 $ignore = false;
532                         }
533
534                         if ((strpos($treffer[1], "//plus.google.com/") !== false) && (strpos($treffer[1], "/posts") !== false)) {
535                                 $ignore = false;
536                         }
537
538                         if ((strpos($treffer[1], "//plus.google.com/") !== false) && (strpos($treffer[1], "/photos") !== false)) {
539                                 $ignore = false;
540                         }
541
542                         if (!$ignore) {
543                                 $urls[$treffer[1]] = $treffer[1];
544                         }
545                 }
546
547                 return $urls;
548         }
549
550         public static function toPlaintext($html, $wraplength = 75, $compact = false)
551         {
552                 $message = str_replace("\r", "", $html);
553
554                 $doc = new DOMDocument();
555                 $doc->preserveWhiteSpace = false;
556
557                 $message = mb_convert_encoding($message, 'HTML-ENTITIES', "UTF-8");
558
559                 @$doc->loadHTML($message);
560
561                 $xpath = new DOMXPath($doc);
562                 $list = $xpath->query("//pre");
563                 foreach ($list as $node) {
564                         $node->nodeValue = str_replace("\n", "\r", $node->nodeValue);
565                 }
566
567                 $message = $doc->saveHTML();
568                 $message = str_replace(["\n<", ">\n", "\r", "\n", "\xC3\x82\xC2\xA0"], ["<", ">", "<br>", " ", ""], $message);
569                 $message = preg_replace('= [\s]*=i', " ", $message);
570
571                 // Collecting all links
572                 $urls = self::collectURLs($message);
573
574                 @$doc->loadHTML($message);
575
576                 self::tagToBBCode($doc, 'html', [], '', '');
577                 self::tagToBBCode($doc, 'body', [], '', '');
578
579                 // MyBB-Auszeichnungen
580                 /*
581                   self::node2BBCode($doc, 'span', array('style'=>'text-decoration: underline;'), '_', '_');
582                   self::node2BBCode($doc, 'span', array('style'=>'font-style: italic;'), '/', '/');
583                   self::node2BBCode($doc, 'span', array('style'=>'font-weight: bold;'), '*', '*');
584
585                   self::node2BBCode($doc, 'strong', array(), '*', '*');
586                   self::node2BBCode($doc, 'b', array(), '*', '*');
587                   self::node2BBCode($doc, 'i', array(), '/', '/');
588                   self::node2BBCode($doc, 'u', array(), '_', '_');
589                  */
590
591                 if ($compact) {
592                         self::tagToBBCode($doc, 'blockquote', [], "»", "«");
593                 } else {
594                         self::tagToBBCode($doc, 'blockquote', [], '[quote]', "[/quote]\n");
595                 }
596
597                 self::tagToBBCode($doc, 'br', [], "\n", '');
598
599                 self::tagToBBCode($doc, 'span', [], "", "");
600                 self::tagToBBCode($doc, 'pre', [], "", "");
601                 self::tagToBBCode($doc, 'div', [], "\r", "\r");
602                 self::tagToBBCode($doc, 'p', [], "\n", "\n");
603
604                 //self::node2BBCode($doc, 'ul', array(), "\n[list]", "[/list]\n");
605                 //self::node2BBCode($doc, 'ol', array(), "\n[list=1]", "[/list]\n");
606                 self::tagToBBCode($doc, 'li', [], "\n* ", "\n");
607
608                 self::tagToBBCode($doc, 'hr', [], "\n" . str_repeat("-", 70) . "\n", "");
609
610                 self::tagToBBCode($doc, 'tr', [], "\n", "");
611                 self::tagToBBCode($doc, 'td', [], "\t", "");
612
613                 self::tagToBBCode($doc, 'h1', [], "\n\n*", "*\n");
614                 self::tagToBBCode($doc, 'h2', [], "\n\n*", "*\n");
615                 self::tagToBBCode($doc, 'h3', [], "\n\n*", "*\n");
616                 self::tagToBBCode($doc, 'h4', [], "\n\n*", "*\n");
617                 self::tagToBBCode($doc, 'h5', [], "\n\n*", "*\n");
618                 self::tagToBBCode($doc, 'h6', [], "\n\n*", "*\n");
619
620                 // Problem: there is no reliable way to detect if it is a link to a tag or profile
621                 //self::node2BBCode($doc, 'a', array('href'=>'/(.+)/'), ' $1 ', ' ', true);
622                 //self::node2BBCode($doc, 'a', array('href'=>'/(.+)/', 'rel'=>'oembed'), ' $1 ', '', true);
623                 //self::node2BBCode($doc, 'img', array('alt'=>'/(.+)/'), '$1', '');
624                 //self::node2BBCode($doc, 'img', array('title'=>'/(.+)/'), '$1', '');
625                 //self::node2BBCode($doc, 'img', array(), '', '');
626                 if (!$compact) {
627                         self::tagToBBCode($doc, 'img', ['src' => '/(.+)/'], ' [img]$1', '[/img] ');
628                 } else {
629                         self::tagToBBCode($doc, 'img', ['src' => '/(.+)/'], ' ', ' ');
630                 }
631
632                 self::tagToBBCode($doc, 'iframe', ['src' => '/(.+)/'], ' $1 ', '');
633
634                 $message = $doc->saveHTML();
635
636                 if (!$compact) {
637                         $message = str_replace("[img]", "", $message);
638                         $message = str_replace("[/img]", "", $message);
639                 }
640
641                 // was ersetze ich da?
642                 // Irgendein stoerrisches UTF-Zeug
643                 $message = str_replace(chr(194) . chr(160), ' ', $message);
644
645                 $message = str_replace("&nbsp;", " ", $message);
646
647                 // Aufeinanderfolgende DIVs
648                 $message = preg_replace('=\r *\r=i', "\n", $message);
649                 $message = str_replace("\r", "\n", $message);
650
651                 $message = strip_tags($message);
652
653                 $message = html_entity_decode($message, ENT_QUOTES, 'UTF-8');
654
655                 if (!$compact && ($message != '')) {
656                         foreach ($urls as $id => $url) {
657                                 if ($url != '' && strpos($message, $url) === false) {
658                                         $message .= "\n" . $url . ' ';
659                                 }
660                         }
661                 }
662
663                 $message = str_replace("\n«", "«\n", $message);
664                 $message = str_replace("»\n", "\n»", $message);
665
666                 do {
667                         $oldmessage = $message;
668                         $message = str_replace("\n\n\n", "\n\n", $message);
669                 } while ($oldmessage != $message);
670
671                 $message = self::quoteLevel(trim($message), $wraplength);
672
673                 return trim($message);
674         }
675 }