3 require_once 'include/html2bbcode.php';
5 function breaklines($line, $level, $wraplength = 75)
7 if ($wraplength == 0) {
11 $wraplen = $wraplength - $level;
18 $subline = substr($line, 0, $wraplen);
20 $pos = strrpos($subline, ' ');
23 $pos = strpos($line, ' ');
26 if (($pos > 0) && strlen($line) > $wraplen) {
27 $newline = trim(substr($line, 0, $pos));
29 $newline = str_repeat(">", $level) . ' ' . $newline;
32 $newlines[] = $newline . " ";
33 $line = substr($line, $pos + 1);
35 } while ((strlen($line) > $wraplen) && !($oldline == $line));
38 $line = str_repeat(">", $level) . ' ' . $line;
43 return implode($newlines, "\n");
46 function quotelevel($message, $wraplength = 75)
48 $lines = explode("\n", $message);
52 foreach ($lines as $line) {
55 while (strpos("*" . $line, '[quote]') > 0) {
57 $pos = strpos($line, '[quote]');
58 $line = substr($line, 0, $pos) . substr($line, $pos + 7);
64 while (strpos("*" . $line, '[/quote]') > 0) {
70 $pos = strpos($line, '[/quote]');
71 $line = substr($line, 0, $pos) . substr($line, $pos + 8);
74 if (!$startquote || ($line != '')) {
75 $newlines[] = breaklines($line, $currlevel, $wraplength);
79 return implode($newlines, "\n");
82 function collecturls($message)
84 $pattern = '/<a.*?href="(.*?)".*?>(.*?)<\/a>/is';
85 preg_match_all($pattern, $message, $result, PREG_SET_ORDER);
88 foreach ($result as $treffer) {
91 // A list of some links that should be ignored
92 $list = ["/user/", "/tag/", "/group/", "/profile/", "/search?search=", "/search?tag=", "mailto:", "/u/", "/node/",
93 "//facebook.com/profile.php?id=", "//plus.google.com/", "//twitter.com/"];
94 foreach ($list as $listitem) {
95 if (strpos($treffer[1], $listitem) !== false) {
100 if ((strpos($treffer[1], "//twitter.com/") !== false) && (strpos($treffer[1], "/status/") !== false)) {
104 if ((strpos($treffer[1], "//plus.google.com/") !== false) && (strpos($treffer[1], "/posts") !== false)) {
108 if ((strpos($treffer[1], "//plus.google.com/") !== false) && (strpos($treffer[1], "/photos") !== false)) {
113 $urls[$treffer[1]] = $treffer[1];
120 function html2plain($html, $wraplength = 75, $compact = false)
124 $message = str_replace("\r", "", $html);
126 $doc = new DOMDocument();
127 $doc->preserveWhiteSpace = false;
129 $message = mb_convert_encoding($message, 'HTML-ENTITIES', "UTF-8");
131 @$doc->loadHTML($message);
133 $xpath = new DomXPath($doc);
134 $list = $xpath->query("//pre");
135 foreach ($list as $node) {
136 $node->nodeValue = str_replace("\n", "\r", $node->nodeValue);
139 $message = $doc->saveHTML();
140 $message = str_replace(["\n<", ">\n", "\r", "\n", "\xC3\x82\xC2\xA0"], ["<", ">", "<br>", " ", ""], $message);
141 $message = preg_replace('= [\s]*=i', " ", $message);
143 // Collecting all links
144 $urls = collecturls($message);
146 @$doc->loadHTML($message);
148 node2bbcode($doc, 'html', [], '', '');
149 node2bbcode($doc, 'body', [], '', '');
151 // MyBB-Auszeichnungen
153 node2bbcode($doc, 'span', array('style'=>'text-decoration: underline;'), '_', '_');
154 node2bbcode($doc, 'span', array('style'=>'font-style: italic;'), '/', '/');
155 node2bbcode($doc, 'span', array('style'=>'font-weight: bold;'), '*', '*');
157 node2bbcode($doc, 'strong', array(), '*', '*');
158 node2bbcode($doc, 'b', array(), '*', '*');
159 node2bbcode($doc, 'i', array(), '/', '/');
160 node2bbcode($doc, 'u', array(), '_', '_');
164 node2bbcode($doc, 'blockquote', [], "»", "«");
166 node2bbcode($doc, 'blockquote', [], '[quote]', "[/quote]\n");
169 node2bbcode($doc, 'br', [], "\n", '');
171 node2bbcode($doc, 'span', [], "", "");
172 node2bbcode($doc, 'pre', [], "", "");
173 node2bbcode($doc, 'div', [], "\r", "\r");
174 node2bbcode($doc, 'p', [], "\n", "\n");
176 //node2bbcode($doc, 'ul', array(), "\n[list]", "[/list]\n");
177 //node2bbcode($doc, 'ol', array(), "\n[list=1]", "[/list]\n");
178 node2bbcode($doc, 'li', [], "\n* ", "\n");
180 node2bbcode($doc, 'hr', [], "\n" . str_repeat("-", 70) . "\n", "");
182 node2bbcode($doc, 'tr', [], "\n", "");
183 node2bbcode($doc, 'td', [], "\t", "");
185 node2bbcode($doc, 'h1', [], "\n\n*", "*\n");
186 node2bbcode($doc, 'h2', [], "\n\n*", "*\n");
187 node2bbcode($doc, 'h3', [], "\n\n*", "*\n");
188 node2bbcode($doc, 'h4', [], "\n\n*", "*\n");
189 node2bbcode($doc, 'h5', [], "\n\n*", "*\n");
190 node2bbcode($doc, 'h6', [], "\n\n*", "*\n");
192 // Problem: there is no reliable way to detect if it is a link to a tag or profile
193 //node2bbcode($doc, 'a', array('href'=>'/(.+)/'), ' $1 ', ' ', true);
194 //node2bbcode($doc, 'a', array('href'=>'/(.+)/', 'rel'=>'oembed'), ' $1 ', '', true);
195 //node2bbcode($doc, 'img', array('alt'=>'/(.+)/'), '$1', '');
196 //node2bbcode($doc, 'img', array('title'=>'/(.+)/'), '$1', '');
197 //node2bbcode($doc, 'img', array(), '', '');
199 node2bbcode($doc, 'img', ['src' => '/(.+)/'], ' [img]$1', '[/img] ');
201 node2bbcode($doc, 'img', ['src' => '/(.+)/'], ' ', ' ');
204 node2bbcode($doc, 'iframe', ['src' => '/(.+)/'], ' $1 ', '');
206 $message = $doc->saveHTML();
209 $message = str_replace("[img]", "", $message);
210 $message = str_replace("[/img]", "", $message);
213 // was ersetze ich da?
214 // Irgendein stoerrisches UTF-Zeug
215 $message = str_replace(chr(194) . chr(160), ' ', $message);
217 $message = str_replace(" ", " ", $message);
219 // Aufeinanderfolgende DIVs
220 $message = preg_replace('=\r *\r=i', "\n", $message);
221 $message = str_replace("\r", "\n", $message);
223 $message = strip_tags($message);
225 $message = html_entity_decode($message, ENT_QUOTES, 'UTF-8');
227 if (!$compact && ($message != '')) {
228 foreach ($urls as $id => $url) {
229 if ($url != '' && strpos($message, $url) === false) {
230 $message .= "\n" . $url . ' ';
235 $message = str_replace("\n«", "«\n", $message);
236 $message = str_replace("»\n", "\n»", $message);
239 $oldmessage = $message;
240 $message = str_replace("\n\n\n", "\n\n", $message);
241 } while ($oldmessage != $message);
243 $message = quotelevel(trim($message), $wraplength);
245 return trim($message);