2 require_once "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))
96 $urls[$treffer[1]] = $treffer[1];
101 function html2plain($html, $wraplength = 75, $compact = false)
105 $message = str_replace("\r", "", $html);
107 $doc = new DOMDocument();
108 $doc->preserveWhiteSpace = false;
110 $message = mb_convert_encoding($message, 'HTML-ENTITIES', "UTF-8");
112 @$doc->loadHTML($message);
114 $xpath = new DomXPath($doc);
115 $list = $xpath->query("//pre");
116 foreach ($list as $node) {
117 $node->nodeValue = str_replace("\n", "\r", $node->nodeValue);
120 $message = $doc->saveHTML();
121 $message = str_replace(array("\n<", ">\n", "\r", "\n", "\xC3\x82\xC2\xA0"), array("<", ">", "<br>", " ", ""), $message);
122 $message = preg_replace('= [\s]*=i', " ", $message);
124 // Collecting all links
125 $urls = collecturls($message);
127 @$doc->loadHTML($message);
129 node2bbcode($doc, 'html', array(), '', '');
130 node2bbcode($doc, 'body', array(), '', '');
132 // MyBB-Auszeichnungen
134 node2bbcode($doc, 'span', array('style'=>'text-decoration: underline;'), '_', '_');
135 node2bbcode($doc, 'span', array('style'=>'font-style: italic;'), '/', '/');
136 node2bbcode($doc, 'span', array('style'=>'font-weight: bold;'), '*', '*');
138 node2bbcode($doc, 'strong', array(), '*', '*');
139 node2bbcode($doc, 'b', array(), '*', '*');
140 node2bbcode($doc, 'i', array(), '/', '/');
141 node2bbcode($doc, 'u', array(), '_', '_');
145 node2bbcode($doc, 'blockquote', array(), "»", "«");
147 node2bbcode($doc, 'blockquote', array(), '[quote]', "[/quote]\n");
149 node2bbcode($doc, 'br', array(), "\n", '');
151 node2bbcode($doc, 'span', array(), "", "");
152 node2bbcode($doc, 'pre', array(), "", "");
153 node2bbcode($doc, 'div', array(), "\r", "\r");
154 node2bbcode($doc, 'p', array(), "\n", "\n");
156 //node2bbcode($doc, 'ul', array(), "\n[list]", "[/list]\n");
157 //node2bbcode($doc, 'ol', array(), "\n[list=1]", "[/list]\n");
158 node2bbcode($doc, 'li', array(), "\n* ", "\n");
160 node2bbcode($doc, 'hr', array(), "\n".str_repeat("-", 70)."\n", "");
162 node2bbcode($doc, 'tr', array(), "\n", "");
163 node2bbcode($doc, 'td', array(), "\t", "");
165 node2bbcode($doc, 'h1', array(), "\n\n*", "*\n");
166 node2bbcode($doc, 'h2', array(), "\n\n*", "*\n");
167 node2bbcode($doc, 'h3', array(), "\n\n*", "*\n");
168 node2bbcode($doc, 'h4', array(), "\n\n*", "*\n");
169 node2bbcode($doc, 'h5', array(), "\n\n*", "*\n");
170 node2bbcode($doc, 'h6', array(), "\n\n*", "*\n");
172 // Problem: there is no reliable way to detect if it is a link to a tag or profile
173 //node2bbcode($doc, 'a', array('href'=>'/(.+)/'), ' $1 ', '', true);
174 node2bbcode($doc, 'a', array('href'=>'/(.+)/', 'rel'=>'oembed'), ' $1 ', '', true);
175 //node2bbcode($doc, 'img', array('alt'=>'/(.+)/'), '$1', '');
176 //node2bbcode($doc, 'img', array('title'=>'/(.+)/'), '$1', '');
177 //node2bbcode($doc, 'img', array(), '', '');
179 node2bbcode($doc, 'img', array('src'=>'/(.+)/'), '[img]$1', '[/img]');
181 node2bbcode($doc, 'img', array('src'=>'/(.+)/'), '', '');
183 node2bbcode($doc, 'iframe', array('src'=>'/(.+)/'), ' $1 ', '', true);
185 $message = $doc->saveHTML();
188 $message = str_replace("[img]", "", $message);
189 $message = str_replace("[/img]", "", $message);
192 // was ersetze ich da?
193 // Irgendein stoerrisches UTF-Zeug
194 $message = str_replace(chr(194).chr(160), ' ', $message);
196 $message = str_replace(" ", " ", $message);
198 // Aufeinanderfolgende DIVs
199 $message = preg_replace('=\r *\r=i', "\n", $message);
200 $message = str_replace("\r", "\n", $message);
202 $message = strip_tags($message);
204 $message = html_entity_decode($message, ENT_QUOTES, 'UTF-8');
208 foreach ($urls as $id=>$url)
209 if (strpos($message, $url) == false)
210 $message .= "\n".$url." ";
211 //$message .= "\n[".($counter++)."] ".$url;
215 $oldmessage = $message;
216 $message = str_replace("\n\n\n", "\n\n", $message);
217 } while ($oldmessage != $message);
219 $message = quotelevel(trim($message), $wraplength);
221 return(trim($message));