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) {
85 // A list of some links that should be ignored
86 $list = array("/user/", "/tag/", "/group/", "/profile/", "/search?search=", "/search?tag=", "mailto:", "/u/", "/node/",
87 "//facebook.com/profile.php?id=", "//plus.google.com/");
88 foreach ($list as $listitem)
89 if (strpos($treffer[1], $listitem) !== false)
92 if ((strpos($treffer[1], "//plus.google.com/") !== false) and (strpos($treffer[1], "/posts") !== false))
95 if ((strpos($treffer[1], "//plus.google.com/") !== false) and (strpos($treffer[1], "/photos") !== false))
99 $urls[$treffer[1]] = $treffer[1];
104 function html2plain($html, $wraplength = 75, $compact = false)
108 $message = str_replace("\r", "", $html);
110 $doc = new DOMDocument();
111 $doc->preserveWhiteSpace = false;
113 $message = mb_convert_encoding($message, 'HTML-ENTITIES', "UTF-8");
115 @$doc->loadHTML($message);
117 $xpath = new DomXPath($doc);
118 $list = $xpath->query("//pre");
119 foreach ($list as $node) {
120 $node->nodeValue = str_replace("\n", "\r", $node->nodeValue);
123 $message = $doc->saveHTML();
124 $message = str_replace(array("\n<", ">\n", "\r", "\n", "\xC3\x82\xC2\xA0"), array("<", ">", "<br>", " ", ""), $message);
125 $message = preg_replace('= [\s]*=i', " ", $message);
127 // Collecting all links
128 $urls = collecturls($message);
130 @$doc->loadHTML($message);
132 node2bbcode($doc, 'html', array(), '', '');
133 node2bbcode($doc, 'body', array(), '', '');
135 // MyBB-Auszeichnungen
137 node2bbcode($doc, 'span', array('style'=>'text-decoration: underline;'), '_', '_');
138 node2bbcode($doc, 'span', array('style'=>'font-style: italic;'), '/', '/');
139 node2bbcode($doc, 'span', array('style'=>'font-weight: bold;'), '*', '*');
141 node2bbcode($doc, 'strong', array(), '*', '*');
142 node2bbcode($doc, 'b', array(), '*', '*');
143 node2bbcode($doc, 'i', array(), '/', '/');
144 node2bbcode($doc, 'u', array(), '_', '_');
148 node2bbcode($doc, 'blockquote', array(), "»", "«");
150 node2bbcode($doc, 'blockquote', array(), '[quote]', "[/quote]\n");
152 node2bbcode($doc, 'br', array(), "\n", '');
154 node2bbcode($doc, 'span', array(), "", "");
155 node2bbcode($doc, 'pre', array(), "", "");
156 node2bbcode($doc, 'div', array(), "\r", "\r");
157 node2bbcode($doc, 'p', array(), "\n", "\n");
159 //node2bbcode($doc, 'ul', array(), "\n[list]", "[/list]\n");
160 //node2bbcode($doc, 'ol', array(), "\n[list=1]", "[/list]\n");
161 node2bbcode($doc, 'li', array(), "\n* ", "\n");
163 node2bbcode($doc, 'hr', array(), "\n".str_repeat("-", 70)."\n", "");
165 node2bbcode($doc, 'tr', array(), "\n", "");
166 node2bbcode($doc, 'td', array(), "\t", "");
168 node2bbcode($doc, 'h1', array(), "\n\n*", "*\n");
169 node2bbcode($doc, 'h2', array(), "\n\n*", "*\n");
170 node2bbcode($doc, 'h3', array(), "\n\n*", "*\n");
171 node2bbcode($doc, 'h4', array(), "\n\n*", "*\n");
172 node2bbcode($doc, 'h5', array(), "\n\n*", "*\n");
173 node2bbcode($doc, 'h6', array(), "\n\n*", "*\n");
175 // Problem: there is no reliable way to detect if it is a link to a tag or profile
176 //node2bbcode($doc, 'a', array('href'=>'/(.+)/'), ' $1 ', '', true);
177 node2bbcode($doc, 'a', array('href'=>'/(.+)/', 'rel'=>'oembed'), ' $1 ', '', true);
178 //node2bbcode($doc, 'img', array('alt'=>'/(.+)/'), '$1', '');
179 //node2bbcode($doc, 'img', array('title'=>'/(.+)/'), '$1', '');
180 //node2bbcode($doc, 'img', array(), '', '');
182 node2bbcode($doc, 'img', array('src'=>'/(.+)/'), '[img]$1', '[/img]');
184 node2bbcode($doc, 'img', array('src'=>'/(.+)/'), '', '');
186 node2bbcode($doc, 'iframe', array('src'=>'/(.+)/'), ' $1 ', '', true);
188 $message = $doc->saveHTML();
191 $message = str_replace("[img]", "", $message);
192 $message = str_replace("[/img]", "", $message);
195 // was ersetze ich da?
196 // Irgendein stoerrisches UTF-Zeug
197 $message = str_replace(chr(194).chr(160), ' ', $message);
199 $message = str_replace(" ", " ", $message);
201 // Aufeinanderfolgende DIVs
202 $message = preg_replace('=\r *\r=i', "\n", $message);
203 $message = str_replace("\r", "\n", $message);
205 $message = strip_tags($message);
207 $message = html_entity_decode($message, ENT_QUOTES, 'UTF-8');
211 foreach ($urls as $id=>$url)
212 if (strpos($message, $url) == false)
213 $message .= "\n".$url." ";
214 //$message .= "\n[".($counter++)."] ".$url;
218 $oldmessage = $message;
219 $message = str_replace("\n\n\n", "\n\n", $message);
220 } while ($oldmessage != $message);
222 $message = quotelevel(trim($message), $wraplength);
224 return(trim($message));