2 require_once("include/html2bbcode.php");
4 function breaklines($line, $level, $wraplength = 75)
12 $wraplen = $wraplength-$level;
19 $subline = substr($line, 0, $wraplen);
21 $pos = strrpos($subline, ' ');
24 $pos = strpos($line, ' ');
26 if (($pos > 0) and strlen($line) > $wraplen) {
27 $newline = trim(substr($line, 0, $pos));
29 $newline = str_repeat(">", $level).' '.$newline;
31 $newlines[] = $newline." ";
32 $line = substr($line, $pos+1);
35 } while ((strlen($line) > $wraplen) and !($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) {
69 $pos = strpos($line, '[/quote]');
70 $line = substr($line, 0, $pos).substr($line, $pos+8);
73 if (!$startquote or ($line != ''))
74 $newlines[] = breaklines($line, $currlevel, $wraplength);
76 return(implode($newlines, "\n"));
79 function collecturls($message) {
80 $pattern = '/<a.*?href="(.*?)".*?>(.*?)<\/a>/is';
81 preg_match_all($pattern, $message, $result, PREG_SET_ORDER);
84 foreach ($result as $treffer) {
88 // A list of some links that should be ignored
89 $list = array("/user/", "/tag/", "/group/", "/profile/", "/search?search=", "/search?tag=", "mailto:", "/u/", "/node/",
90 "//facebook.com/profile.php?id=", "//plus.google.com/", "//twitter.com/");
91 foreach ($list as $listitem)
92 if (strpos($treffer[1], $listitem) !== false)
95 if ((strpos($treffer[1], "//twitter.com/") !== false) and (strpos($treffer[1], "/status/") !== false))
98 if ((strpos($treffer[1], "//plus.google.com/") !== false) and (strpos($treffer[1], "/posts") !== false))
101 if ((strpos($treffer[1], "//plus.google.com/") !== false) and (strpos($treffer[1], "/photos") !== false))
105 $urls[$treffer[1]] = $treffer[1];
110 function html2plain($html, $wraplength = 75, $compact = false)
114 $message = str_replace("\r", "", $html);
116 $doc = new DOMDocument();
117 $doc->preserveWhiteSpace = false;
119 $message = mb_convert_encoding($message, 'HTML-ENTITIES', "UTF-8");
121 @$doc->loadHTML($message);
123 $xpath = new DomXPath($doc);
124 $list = $xpath->query("//pre");
125 foreach ($list as $node) {
126 $node->nodeValue = str_replace("\n", "\r", $node->nodeValue);
129 $message = $doc->saveHTML();
130 $message = str_replace(array("\n<", ">\n", "\r", "\n", "\xC3\x82\xC2\xA0"), array("<", ">", "<br>", " ", ""), $message);
131 $message = preg_replace('= [\s]*=i', " ", $message);
133 // Collecting all links
134 $urls = collecturls($message);
136 @$doc->loadHTML($message);
138 node2bbcode($doc, 'html', array(), '', '');
139 node2bbcode($doc, 'body', array(), '', '');
141 // MyBB-Auszeichnungen
143 node2bbcode($doc, 'span', array('style'=>'text-decoration: underline;'), '_', '_');
144 node2bbcode($doc, 'span', array('style'=>'font-style: italic;'), '/', '/');
145 node2bbcode($doc, 'span', array('style'=>'font-weight: bold;'), '*', '*');
147 node2bbcode($doc, 'strong', array(), '*', '*');
148 node2bbcode($doc, 'b', array(), '*', '*');
149 node2bbcode($doc, 'i', array(), '/', '/');
150 node2bbcode($doc, 'u', array(), '_', '_');
154 node2bbcode($doc, 'blockquote', array(), "»", "«");
156 node2bbcode($doc, 'blockquote', array(), '[quote]', "[/quote]\n");
158 node2bbcode($doc, 'br', array(), "\n", '');
160 node2bbcode($doc, 'span', array(), "", "");
161 node2bbcode($doc, 'pre', array(), "", "");
162 node2bbcode($doc, 'div', array(), "\r", "\r");
163 node2bbcode($doc, 'p', array(), "\n", "\n");
165 //node2bbcode($doc, 'ul', array(), "\n[list]", "[/list]\n");
166 //node2bbcode($doc, 'ol', array(), "\n[list=1]", "[/list]\n");
167 node2bbcode($doc, 'li', array(), "\n* ", "\n");
169 node2bbcode($doc, 'hr', array(), "\n".str_repeat("-", 70)."\n", "");
171 node2bbcode($doc, 'tr', array(), "\n", "");
172 node2bbcode($doc, 'td', array(), "\t", "");
174 node2bbcode($doc, 'h1', array(), "\n\n*", "*\n");
175 node2bbcode($doc, 'h2', array(), "\n\n*", "*\n");
176 node2bbcode($doc, 'h3', array(), "\n\n*", "*\n");
177 node2bbcode($doc, 'h4', array(), "\n\n*", "*\n");
178 node2bbcode($doc, 'h5', array(), "\n\n*", "*\n");
179 node2bbcode($doc, 'h6', array(), "\n\n*", "*\n");
181 // Problem: there is no reliable way to detect if it is a link to a tag or profile
182 //node2bbcode($doc, 'a', array('href'=>'/(.+)/'), ' $1 ', ' ', true);
183 //node2bbcode($doc, 'a', array('href'=>'/(.+)/', 'rel'=>'oembed'), ' $1 ', '', true);
184 //node2bbcode($doc, 'img', array('alt'=>'/(.+)/'), '$1', '');
185 //node2bbcode($doc, 'img', array('title'=>'/(.+)/'), '$1', '');
186 //node2bbcode($doc, 'img', array(), '', '');
188 node2bbcode($doc, 'img', array('src'=>'/(.+)/'), ' [img]$1', '[/img] ');
190 node2bbcode($doc, 'img', array('src'=>'/(.+)/'), ' ', ' ');
192 node2bbcode($doc, 'iframe', array('src'=>'/(.+)/'), ' $1 ', '', true);
194 $message = $doc->saveHTML();
197 $message = str_replace("[img]", "", $message);
198 $message = str_replace("[/img]", "", $message);
201 // was ersetze ich da?
202 // Irgendein stoerrisches UTF-Zeug
203 $message = str_replace(chr(194).chr(160), ' ', $message);
205 $message = str_replace(" ", " ", $message);
207 // Aufeinanderfolgende DIVs
208 $message = preg_replace('=\r *\r=i', "\n", $message);
209 $message = str_replace("\r", "\n", $message);
211 $message = strip_tags($message);
213 $message = html_entity_decode($message, ENT_QUOTES, 'UTF-8');
215 if (!$compact AND ($message != "")) {
217 foreach ($urls as $id=>$url)
219 if (strpos($message, $url) === false)
220 $message .= "\n".$url." ";
221 //$message .= "\n[".($counter++)."] ".$url;
224 $message = str_replace("\n«", "«\n", $message);
225 $message = str_replace("»\n", "\n»", $message);
228 $oldmessage = $message;
229 $message = str_replace("\n\n\n", "\n\n", $message);
230 } while ($oldmessage != $message);
232 $message = quotelevel(trim($message), $wraplength);
234 return(trim($message));