This repository was archived by the owner on Jun 5, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathPostHistory.php
More file actions
309 lines (263 loc) · 10.1 KB
/
PostHistory.php
File metadata and controls
309 lines (263 loc) · 10.1 KB
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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
<?php
/**
* Main source file for PostHistory
*
* @package PostHistory
* @version 1.0.1
*/
if (!defined('SMF'))
die('Hacking attempt...');
/**
*
*/
function PH_actions(&$actionArray)
{
global $modSettings;
if (empty($modSettings['posthistoryEnabled']))
return;
$actionArray['posthistory'] = array('PostHistory.php', 'PostHistory');
}
/**
*
*/
function PH_core_features(&$core_features)
{
$core_features['posthistory'] = array(
'settings' => array(
'posthistoryEnabled' => 1,
),
);
}
/**
*
*/
function PH_load_permissions(&$permissionGroups, &$permissionList, &$leftPermissionGroups, &$hiddenPermissions, &$relabelPermissions)
{
global $context;
$permissionList['board'] += array(
'posthistory_view' => array(true, 'post', 'moderate', 'moderate'),
'posthistory_restore' => array(true, 'post', 'moderate', 'moderate')
);
}
/**
* Displays edit from topic
*/
function PostHistory()
{
global $context, $scripturl, $smcFunc, $topic, $user_info, $txt;
if (empty($topic) || empty($_REQUEST['msg']))
fatal_lang_error('not_a_topic');
// Content is mostly duplicates so no indexing
$context['robot_no_index'] = true;
// Make sure message is integer
$_REQUEST['msg'] = (int) $_REQUEST['msg'];
// Try to get topic from cache, this checks that msg is actually in topic that user said it to be
if (($real_topic = cache_get_data('msg_topic-' . $_REQUEST['msg'], 120)) === null)
{
$request = $smcFunc['db_query']('', '
SELECT id_topic
FROM {db_prefix}messages
WHERE id_msg = {int:id_msg}
LIMIT 1',
array(
'id_msg' => $_REQUEST['msg'],
)
);
// So did it find anything?
if ($smcFunc['db_num_rows']($request))
{
list ($real_topic) = $smcFunc['db_fetch_row']($request);
$smcFunc['db_free_result']($request);
// Save save save.
cache_put_data('msg_topic-' . $_REQUEST['msg'], $real_topic, 120);
}
else
fatal_lang_error('not_a_topic');
}
if ($topic != $real_topic)
fatal_lang_error('not_a_topic');
// Get all the important topic info.
$request = $smcFunc['db_query']('', '
SELECT
t.id_topic, ms.subject, mc.subject AS msg_subject, mc.id_member, mc.body,
mc.modified_name, mc.modified_time, mc.poster_time,
IFNULL(mem.real_name, mc.poster_name) AS poster_name
FROM {db_prefix}topics AS t
INNER JOIN {db_prefix}messages AS ms ON (ms.id_msg = t.id_first_msg)
INNER JOIN {db_prefix}messages AS mc ON (mc.id_msg = {int:id_msg})
LEFT JOIN {db_prefix}members AS mem ON (mem.id_member = mc.id_member)
WHERE t.id_topic = {int:current_topic}
LIMIT 1',
array(
'current_topic' => $topic,
'id_msg' => $_REQUEST['msg'],
)
);
if ($smcFunc['db_num_rows']($request) == 0)
fatal_lang_error('not_a_topic', false);
$context['ph_topic'] = $smcFunc['db_fetch_assoc']($request);
$smcFunc['db_free_result']($request);
$context['is_popup'] = isset($_REQUEST['popup']);
if (empty($context['ph_topic']['id_member']) || $context['ph_topic']['id_member'] != $user_info['id'] || !allowedTo('posthistory_view_own'))
isAllowedTo('posthistory_view_any');
// Edit wasnt selected
if (!isset($_REQUEST['edit']))
{
$request = $smcFunc['db_query']('', '
SELECT id_edit, modified_name, modified_time
FROM {db_prefix}messages_history
WHERE id_msg = {int:id_msg}',
array(
'id_msg' => $_REQUEST['msg'],
)
);
$context['post_history'] = array();
$prev_edit = 0;
if ($context['ph_topic']['id_member'] == $user_info['id'] && !allowedTo('posthistory_restore_any'))
$can_restore = allowedTo('posthistory_restore_own');
else
$can_restore = allowedTo('posthistory_restore_any');
while ($row = $smcFunc['db_fetch_assoc']($request))
{
$context['post_history'][$row['id_edit']] = array(
'id' => $row['id_edit'],
'id_prev' => $prev_edit,
'href' => $scripturl . '?action=posthistory;topic=' . $topic . '.0;msg=' . $_REQUEST['msg'] . ';edit=' . $row['id_edit'] . ($context['is_popup'] ? ';popup' : ''),
'restore_href' => $can_restore ? $scripturl . '?action=post;msg=' . $_REQUEST['msg'] . ';topic=' . $topic . '.0;restore_edit=' . $row['id_edit'] . ';' . $context['session_var'] . '=' . $context['session_id'] : '',
'diff_current' => $scripturl . '?action=posthistory;topic=' . $topic . '.0;msg=' . $_REQUEST['msg'] . ';edit=' . $row['id_edit'] . ';compare_to=current' . ($context['is_popup'] ? ';popup' : ''),
'diff_prev' => !empty($prev_edit) ? $scripturl . '?action=posthistory;topic=' . $topic . '.0;msg=' . $_REQUEST['msg'] . ';edit=' . $row['id_edit'] . ';compare_to=' . $prev_edit . ($context['is_popup'] ? ';popup' : '') : '',
'name' => $row['modified_name'],
'time' => timeformat($row['modified_time']),
'is_original' => $row['modified_time'] == $context['ph_topic']['poster_time'] && empty($prev_edit),
'is_current' => false,
);
$prev_edit = $row['id_edit'];
}
$smcFunc['db_free_result']($request);
$context['post_history']['current'] = array(
'id' => 'current',
'id_prev' => $prev_edit,
'href' => $scripturl . '?action=posthistory;topic=' . $topic . '.0;msg=' . $_REQUEST['msg'] . ';edit=current' . ($context['is_popup'] ? ';popup' : ''),
'diff_prev' => !empty($prev_edit) ? $scripturl . '?action=posthistory;topic=' . $topic . '.0;msg=' . $_REQUEST['msg'] . ';edit=current;compare_to=' . $prev_edit . ($context['is_popup'] ? ';popup' : '') : '',
'name' => !empty($context['ph_topic']['modified_name']) ? $context['ph_topic']['modified_name'] : $context['ph_topic']['poster_name'],
'time' => timeformat(!empty($context['ph_topic']['modified_time']) ? $context['ph_topic']['modified_time'] : $context['ph_topic']['poster_time']),
'is_original' => empty($context['ph_topic']['modified_time']),
'is_current' => true,
);
$context['sub_template'] = 'list_edits';
}
else
{
// Sanitise numbers
if (is_numeric($_REQUEST['edit']))
$_REQUEST['edit'] = (int) $_REQUEST['edit'];
$context['current_edit'] = loadEdit($context['ph_topic'], $_REQUEST['edit'], $_REQUEST['msg'], !isset($_REQUEST['compare_to']));
if (isset($_REQUEST['compare_to']))
$context['compare_edit'] = loadEdit($context['ph_topic'], (int) $_REQUEST['compare_to'], $_REQUEST['msg'], false);
if (!$context['current_edit'] || (isset($context['compare_edit']) && !$context['compare_edit']))
fatal_lang_error('not_a_topic');
if (!isset($context['compare_edit']))
$context['sub_template'] = 'view_edit';
else
{
$context['edit_changes'] = __diff(
preg_split('@(\[|\]|=| |[\s, ]|<br />)@', $context['compare_edit']['body'], null, PREG_SPLIT_DELIM_CAPTURE),
preg_split('@(\[|\]|=| |[\s, ]|<br />)@', $context['current_edit']['body'], null, PREG_SPLIT_DELIM_CAPTURE)
);
$context['sub_template'] = 'compare_edit';
}
}
// Template
if ($context['is_popup'])
{
$context['template_layers'] = array();
loadLanguage('Help');
}
loadTemplate('PostHistory');
$context['page_title'] = sprintf($txt['title_view_post_history'], $context['ph_topic']['msg_subject']);
}
/**
* Loads edit
*
* @param array $topic Topic data
* @param int $id_edit Edit ID
* @param int $id_msg Message ID
* @param bool $parse Whetever to parse BBC
*/
function loadEdit($topic, $id_edit, $id_msg = 0, $parse = true)
{
global $smcFunc, $context, $scripturl, $user_info;
if (is_int($id_edit))
{
$request = $smcFunc['db_query']('', '
SELECT id_edit, id_msg, modified_name, modified_time, body
FROM {db_prefix}messages_history
WHERE id_edit = {int:edit}'. (!empty($id_msg) ? '
AND id_msg = {int:msg}' : ''),
array(
'msg' => $id_msg,
'edit' => $id_edit,
)
);
$row = $smcFunc['db_fetch_assoc']($request);
if (!$row)
return false;
$smcFunc['db_free_result']($request);
if ($topic['id_member'] == $user_info['id'] && !allowedTo('posthistory_restore_any'))
$can_restore = allowedTo('posthistory_restore_own');
else
$can_restore = allowedTo('posthistory_restore_any');
return array(
'id' => $row['id_edit'],
'href' => $scripturl . '?action=posthistory;topic=' . $topic['id_topic'] . '.0;msg=' . $row['id_msg'] . ';edit=' . $row['id_edit'] . ($context['is_popup'] ? ';popup' : ''),
'restore_href' => $can_restore ? $scripturl . '?action=post;msg=' . $id_msg . ';topic=' . $topic['id_topic'] . '.0;restore_edit=' . $row['id_edit'] . ';' . $context['session_var'] . '=' . $context['session_id'] : '',
'name' => $row['modified_name'],
'time' => timeformat($row['modified_time']),
'body' => $parse ? parse_bbc($row['body']) : $row['body'],
);
}
elseif ($id_edit == 'current')
return array(
'id' => 'current',
'href' => $scripturl . '?action=posthistory;topic=' . $topic['id_topic'] . '.0;msg=' . $id_msg . ';edit=current' . ($context['is_popup'] ? ';popup' : ''),
'name' => !empty($topic['modified_name']) ? $topic['modified_name'] : $topic['poster_name'],
'time' => timeformat(!empty($topic['modified_time']) ? $topic['modified_time'] : $topic['poster_time']),
'body' => $parse ? parse_bbc($topic['body']) : $topic['body'],
);
return false;
}
/**
* Returns difference beetween two versions
*
* @param array $old array of strings to compare (before)
* @param array $new array of strings to compare (after)
*/
function __diff($old, $new)
{
$maxlen = 0;
foreach($old as $oindex => $ovalue)
{
$nkeys = array_keys($new, $ovalue);
foreach($nkeys as $nindex)
{
$matrix[$oindex][$nindex] = isset($matrix[$oindex - 1][$nindex - 1]) ?
$matrix[$oindex - 1][$nindex - 1] + 1 : 1;
if ($matrix[$oindex][$nindex] > $maxlen)
{
$maxlen = $matrix[$oindex][$nindex];
$omax = $oindex + 1 - $maxlen;
$nmax = $nindex + 1 - $maxlen;
}
}
}
if ($maxlen == 0)
return array(
array('d' => $old, 'i'=> $new)
);
return array_merge(
__diff(array_slice($old, 0, $omax), array_slice($new, 0, $nmax)),
array_slice($new, $nmax, $maxlen),
__diff(array_slice($old, $omax + $maxlen), array_slice($new, $nmax + $maxlen))
);
}
?>