/
functions_posting.php
235 lines (212 loc) · 6.6 KB
/
functions_posting.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
<?php
/**
* MyBB 1.8
* Copyright 2014 MyBB Group, All Rights Reserved
*
* Website: http://www.mybb.com
* License: http://www.mybb.com/about/license
*
*/
/**
* Selectively removes quote tags from a message, depending on its nested depth. This is to be used with reply with quote functions.
* For malformed quote tag structures, will try to simulate how MyBB's parser handles the issue, but is slightly inaccurate.
* Examples, with a cutoff depth of 2:
* #1. INPUT: [quote]a[quote=me]b[quote]c[/quote][/quote][/quote]
* OUTPUT: [quote]a[quote=me]b[/quote][/quote]
* #2. INPUT: [quote=a][quote=b][quote=c][quote=d][/quote][quote=e][/quote][/quote][quote=f][/quote][/quote]
* OUTPUT: [quote=a][quote=b][/quote][quote=f][/quote][/quote]
*
* @param string $text the message from which quotes are to be removed
* @param integer $rmdepth nested depth at which quotes should be removed; if none supplied, will use MyBB's default; must be at least 0
* @return string the original message passed in $text, but with quote tags selectively removed
*/
function remove_message_quotes(&$text, $rmdepth=null)
{
if(!$text)
{
return $text;
}
if(!isset($rmdepth))
{
global $mybb;
$rmdepth = $mybb->settings['maxquotedepth'];
}
$rmdepth = (int)$rmdepth;
// find all tokens
// note, at various places, we use the prefix "s" to denote "start" (ie [quote]) and "e" to denote "end" (ie [/quote])
preg_match_all("#\[quote(=(?:"|\"|')?.*?(?:"|\"|')?)?\]#si", $text, $smatches, PREG_OFFSET_CAPTURE | PREG_PATTERN_ORDER);
preg_match_all("#\[/quote\]#i", $text, $ematches, PREG_OFFSET_CAPTURE | PREG_PATTERN_ORDER);
if(empty($smatches) || empty($ematches))
{
return $text;
}
// make things easier by only keeping offsets
$soffsets = $eoffsets = array();
foreach($smatches[0] as $id => $match)
{
$soffsets[] = $match[1];
}
$first_token = 0;
if(isset($soffsets[0])) {
$first_token = $soffsets[0];
}
// whilst we loop, also remove unnecessary end tokens at the start of string
foreach($ematches[0] as $id => $match)
{
if($match[1] > $first_token)
{
$eoffsets[] = $match[1];
}
}
unset($smatches, $ematches);
// elmininate malformed quotes by parsing like the parser does (preg_replace in a while loop)
// NOTE: this is slightly inaccurate because the parser considers [quote] and [quote=...] to be different things
$good_offsets = array();
while(!empty($soffsets) && !empty($eoffsets)) // don't rely on this condition - an end offset before the start offset will cause this to loop indefinitely
{
$last_offset = 0;
foreach($soffsets as $sk => &$soffset)
{
if($soffset >= $last_offset)
{
// search for corresponding eoffset
foreach($eoffsets as $ek => &$eoffset) // use foreach instead of for to get around indexing issues with unset
{
if($eoffset > $soffset)
{
// we've found a pair
$good_offsets[$soffset] = 1;
$good_offsets[$eoffset] = -1;
$last_offset = $eoffset;
unset($soffsets[$sk], $eoffsets[$ek]);
break;
}
}
}
}
// remove any end offsets occurring before start offsets
$first_start = reset($soffsets);
foreach($eoffsets as $ek => &$eoffset)
{
if($eoffset < $first_start)
{
unset($eoffsets[$ek]);
}
else
{
break;
}
}
// we don't need to remove start offsets after the last end offset, because the loop will deplete something before that
}
if(empty($good_offsets))
{
return $text;
}
ksort($good_offsets);
// we now have a list of all the ordered tokens, ready to go through
$depth = 0;
$remove_regions = array();
$tmp_start = 0;
foreach($good_offsets as $offset => $dincr)
{
if($depth == $rmdepth && $dincr == 1)
{
$tmp_start = $offset;
}
$depth += $dincr;
if($depth == $rmdepth && $dincr == -1)
{
$remove_regions[] = array($tmp_start, $offset);
}
}
if(empty($remove_regions))
{
return $text;
}
// finally, remove the quotes from the string
$newtext = '';
$cpy_start = 0;
foreach($remove_regions as &$region)
{
$newtext .= substr($text, $cpy_start, $region[0]-$cpy_start);
$cpy_start = $region[1]+8; // 8 = strlen('[/quote]')
// clean up newlines
$next_char = $text[$region[1]+8];
if($next_char == "\r" || $next_char == "\n")
{
++$cpy_start;
if($next_char == "\r" && $text[$region[1]+9] == "\n")
{
++$cpy_start;
}
}
}
// append remaining end text
if(strlen($text) != $cpy_start)
{
$newtext .= substr($text, $cpy_start);
}
// we're done
return $newtext;
}
/**
* Performs cleanup of a quoted message, such as replacing /me commands, before presenting quoted post to the user.
*
* @param array $quoted_post quoted post info, taken from the DB (requires the 'message', 'username', 'pid' and 'dateline' entries to be set; will use 'userusername' if present. requires 'quote_is_pm' if quote message is from a private message)
* @param boolean $remove_message_quotes whether to call remove_message_quotes() on the quoted message
* @return string the cleaned up message, wrapped in a quote tag
*/
function parse_quoted_message(&$quoted_post, $remove_message_quotes=true)
{
global $parser, $lang, $plugins;
if(!isset($parser))
{
require_once MYBB_ROOT."inc/class_parser.php";
$parser = new postParser;
}
// Swap username over if we have a registered user
if(isset($quoted_post['userusername']))
{
$quoted_post['username'] = $quoted_post['userusername'];
}
else
{
if(empty($quoted_post['username']))
{
$quoted_post['username'] = htmlspecialchars_uni($lang->guest);
}
}
// Clean up the message
$quoted_post['message'] = preg_replace(array(
'#(^|\r|\n)/me ([^\r\n<]*)#i',
'#(^|\r|\n)/slap ([^\r\n<]*)#i',
'#\[attachment=([0-9]+?)\]#i'
), array(
"\\1* {$quoted_post['username']} \\2",
"\\1* {$quoted_post['username']} {$lang->slaps} \\2 {$lang->with_trout}",
"",
), $quoted_post['message']);
$quoted_post['message'] = $parser->parse_badwords($quoted_post['message']);
if($remove_message_quotes)
{
global $mybb;
$max_quote_depth = (int)$mybb->settings['maxquotedepth'];
if($max_quote_depth)
{
$quoted_post['message'] = remove_message_quotes($quoted_post['message'], $max_quote_depth-1); // we're wrapping the message in a [quote] tag, so take away one quote depth level
}
}
$quoted_post = $plugins->run_hooks("parse_quoted_message", $quoted_post);
$extra = '';
if(empty($quoted_post['quote_is_pm']))
{
$extra = " pid='{$quoted_post['pid']}' dateline='{$quoted_post['dateline']}'";
}
$quote_char = '"';
if(strpos($quoted_post['username'], '"') !== false)
{
$quote_char = "'";
}
return "[quote={$quote_char}{$quoted_post['username']}{$quote_char}{$extra}]\n{$quoted_post['message']}\n[/quote]\n\n";
}