-
Notifications
You must be signed in to change notification settings - Fork 6
/
bbcode2plain.php
241 lines (176 loc) · 6.56 KB
/
bbcode2plain.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
<?php
define("IN_SYNCOM", 1);
/*
// Für Tests
define("IN_MYBB", 1);
$basepath = dirname($_SERVER["SCRIPT_FILENAME"]);
require_once $basepath."/../global.php";
require MYBB_ROOT.'/syncom/config.php';
require_once "mybbapi.php";*/
require_once "functions.php";
function breaklines($line, $level)
{
$wraplen = 75-$level;
$newlines = array();
do {
$oldline = $line;
$subline = substr($line, 0, $wraplen);
$pos = strrpos($subline, ' ');
if ($pos == 0)
$pos = strpos($line, ' ');
if (($pos > 0) and strlen($line) > $wraplen) {
$newline = trim(substr($line, 0, $pos));
if ($level > 0)
$newline = str_repeat(">", $level).' '.$newline;
$newlines[] = $newline." ";
$line = substr($line, $pos+1);
}
} while ((strlen($line) > $wraplen) and !($oldline == $line));
if ($level > 0)
$line = str_repeat(">", $level).' '.$line;
$newlines[] = $line;
return(implode($newlines, "\n"));
}
function quotelevel($message)
{
$lines = explode("\n", $message);
$newlines = array();
$level = 0;
foreach($lines as $line) {;
$line = trim($line);
$startquote = false;
while (strpos("*".$line, '[quote]') > 0) {
$level++;
$pos = strpos($line, '[quote]');
$line = substr($line, 0, $pos).substr($line, $pos+7);
$startquote = true;
}
$currlevel = $level;
while (strpos("*".$line, '[/quote]') > 0) {
$level--;
if ($level < 0)
$level = 0;
$pos = strpos($line, '[/quote]');
$line = substr($line, 0, $pos).substr($line, $pos+8);
}
if (!$startquote or ($line != ''))
$newlines[] = breaklines($line, $currlevel);
}
return(implode($newlines, "\n"));
}
function bbcode2plain2($bbcode)
{
global $lang;
// Zuerst mal nach HTML konvertieren
$parser = new postParser;
$parser_options = array(
'allow_html' => 0,
'filter_badwords' => 1,
'allow_mycode' => 1,
'allow_imgcode' => 0,
'allow_smilies' => 0,
'shorten_urls' => 0,
);
$html = $parser->parse_message($bbcode, $parser_options);
$html = str_replace('<cite>'.$lang->quote.'</cite>', '', $html);
$html = preg_replace('=(.*?)<blockquote><cite>(.*?)</cite>(.*?)=i',
'$1<br />$2<br /><blockquote>$3', $html);
// Und nun die Konvertierung
$message = str_replace("\r", "", $html);
$doc = new DOMDocument();
$doc->preserveWhiteSpace = false;
$message = mb_convert_encoding($message, 'HTML-ENTITIES', "UTF-8");
@$doc->loadHTML($message);
$xpath = new DomXPath($doc);
$list = $xpath->query("//pre");
foreach ($list as $node) {
$node->nodeValue = str_replace("\n", "\r", $node->nodeValue);
}
$message = $doc->saveHTML();
$message = str_replace(array("\n<", ">\n", "\r", "\n", "\xC3\x82\xC2\xA0"), array("<", ">", "<br>", " ", ""), $message);
$message = preg_replace('= [\s]*=i', " ", $message);
// nach <a href="...">...</a> suchen, die ... miteinander vergleichen und bei Gleichheit durch ein einzelnes ... ersetzen.
$pattern = '/<a.*?href="(.*?)".*?>(.*?)<\/a>/is';
preg_match_all($pattern, $message, $result, PREG_SET_ORDER);
foreach ($result as $treffer) {
if ($treffer[1] == $treffer[2]) {
$search = '<a href="'.$treffer[1].'" target="_blank">'.$treffer[1].'</a>';
$message = str_replace($search, $treffer[1], $message);
}
}
@$doc->loadHTML($message);
node2bbcode($doc, 'html', array(), '', '');
node2bbcode($doc, 'body', array(), '', '');
// MyBB-Auszeichnungen
node2bbcode($doc, 'span', array('style'=>'text-decoration: underline;'), '_', '_');
node2bbcode($doc, 'span', array('style'=>'font-style: italic;'), '/', '/');
node2bbcode($doc, 'span', array('style'=>'font-weight: bold;'), '*', '*');
node2bbcode($doc, 'strong', array(), '*', '*');
node2bbcode($doc, 'b', array(), '*', '*');
node2bbcode($doc, 'i', array(), '/', '/');
node2bbcode($doc, 'u', array(), '_', '_');
node2bbcode($doc, 'blockquote', array(), '[quote]', "[/quote]\n");
node2bbcode($doc, 'br', array(), "\n", '');
node2bbcode($doc, 'span', array(), "", "");
node2bbcode($doc, 'pre', array(), "", "");
node2bbcode($doc, 'div', array(), "\r", "\r");
node2bbcode($doc, 'p', array(), "\n", "\n");
//node2bbcode($doc, 'ul', array(), "\n[list]", "[/list]\n");
//node2bbcode($doc, 'ol', array(), "\n[list=1]", "[/list]\n");
node2bbcode($doc, 'li', array(), "\n* ", "\n");
node2bbcode($doc, 'hr', array(), str_repeat("-", 70), "");
node2bbcode($doc, 'tr', array(), "\n", "");
node2bbcode($doc, 'td', array(), "\t", "");
node2bbcode($doc, 'h1', array(), "\n\n*", "*\n");
node2bbcode($doc, 'h2', array(), "\n\n*", "*\n");
node2bbcode($doc, 'h3', array(), "\n\n*", "*\n");
node2bbcode($doc, 'h4', array(), "\n\n*", "*\n");
node2bbcode($doc, 'h5', array(), "\n\n*", "*\n");
node2bbcode($doc, 'h6', array(), "\n\n*", "*\n");
node2bbcode($doc, 'a', array('href'=>'/(.+)/'), ' $1', '', true);
node2bbcode($doc, 'img', array('alt'=>'/(.+)/'), '$1', '');
node2bbcode($doc, 'img', array('title'=>'/(.+)/'), '$1', '');
node2bbcode($doc, 'img', array(), '', '');
node2bbcode($doc, 'img', array('src'=>'/(.+)/'), '[img]$1', '[/img]');
$message = $doc->saveHTML();
$message = str_replace("[img]", "", $message);
$message = str_replace("[/img]", "", $message);
// was ersetze ich da?
// Irgendein stoerrisches UTF-Zeug
$message = str_replace(chr(194).chr(160), ' ', $message);
$message = str_replace(" ", " ", $message);
// Aufeinanderfolgende DIVs
$message = preg_replace('=\r *\r=i', "\n", $message);
$message = str_replace("\r", "\n", $message);
$message = strip_tags($message);
$message = html_entity_decode($message, ENT_QUOTES, 'UTF-8');
do {
$oldmessage = $message;
$message = str_replace("\n\n\n", "\n\n", $message);
} while ($oldmessage != $message);
$message = quotelevel(trim($message));
return(trim($message));
}
/*
// Für Tests
//$dir = scandir($syncom['outgoing-spool'].'/phperror/');
$dir = scandir($syncom['outgoing-spool'].'/error/');
//$dir = array("outlqp0yS");
//$dir = array("outwMI47A");
//$dir = array("outzySkpW");
foreach ($dir as $spoolfile) {
//$file = $syncom['outgoing-spool'].'/phperror/'.$spoolfile;
$file = $syncom['outgoing-spool'].'/error/'.$spoolfile;
if (!is_dir($file) and (file_exists($file))) {
$message = unserialize(file_get_contents($file));
$body = stripslashes(str_replace(array('\r', '\n'), array("\r", "\n"), $message['body']));
$pattern = "/\[quote='(.*?)' pid='(\d+)' dateline='(\d+)'\].*?/is";
$body = preg_replace($pattern, '[quote=$1]', $body);
$body = str_ireplace(array('[collapsed]', '[/collapsed]'), array('[quote]', '[/quote]'), $body);
$plaintext = bbcode2plain2($body);
echo $file."\n***********************\n".$plaintext;
echo "\n-----------------------------------------------\n";
}
}
*/
?>