Chris@76
|
1 <?php
|
Chris@76
|
2
|
Chris@76
|
3 /**
|
Chris@76
|
4 * Simple Machines Forum (SMF)
|
Chris@76
|
5 *
|
Chris@76
|
6 * @package SMF
|
Chris@76
|
7 * @author Simple Machines http://www.simplemachines.org
|
Chris@76
|
8 * @copyright 2011 Simple Machines
|
Chris@76
|
9 * @license http://www.simplemachines.org/about/smf/license.php BSD
|
Chris@76
|
10 *
|
Chris@76
|
11 * @version 2.0
|
Chris@76
|
12 */
|
Chris@76
|
13
|
Chris@76
|
14 if (!defined('SMF'))
|
Chris@76
|
15 die('Hacking attempt...');
|
Chris@76
|
16
|
Chris@76
|
17 /* The contents of this file handle the deletion of topics, posts, and related
|
Chris@76
|
18 paraphernalia. It has the following functions:
|
Chris@76
|
19
|
Chris@76
|
20 void RemoveTopic2()
|
Chris@76
|
21 // !!!
|
Chris@76
|
22
|
Chris@76
|
23 void DeleteMessage()
|
Chris@76
|
24 // !!!
|
Chris@76
|
25
|
Chris@76
|
26 void RemoveOldTopics2()
|
Chris@76
|
27 // !!!
|
Chris@76
|
28
|
Chris@76
|
29 void removeTopics(array topics, bool decreasePostCount = true, bool ignoreRecycling = false)
|
Chris@76
|
30 // !!!
|
Chris@76
|
31
|
Chris@76
|
32 bool removeMessage(int id_msg, bool decreasePostCount = true)
|
Chris@76
|
33 // !!!
|
Chris@76
|
34 */
|
Chris@76
|
35
|
Chris@76
|
36 // Completely remove an entire topic.
|
Chris@76
|
37 function RemoveTopic2()
|
Chris@76
|
38 {
|
Chris@76
|
39 global $user_info, $topic, $board, $sourcedir, $smcFunc, $context, $modSettings;
|
Chris@76
|
40
|
Chris@76
|
41 // Make sure they aren't being lead around by someone. (:@)
|
Chris@76
|
42 checkSession('get');
|
Chris@76
|
43
|
Chris@76
|
44 // This file needs to be included for sendNotifications().
|
Chris@76
|
45 require_once($sourcedir . '/Subs-Post.php');
|
Chris@76
|
46
|
Chris@76
|
47 // Trying to fool us around, are we?
|
Chris@76
|
48 if (empty($topic))
|
Chris@76
|
49 redirectexit();
|
Chris@76
|
50
|
Chris@76
|
51 $request = $smcFunc['db_query']('', '
|
Chris@76
|
52 SELECT t.id_member_started, ms.subject, t.approved
|
Chris@76
|
53 FROM {db_prefix}topics AS t
|
Chris@76
|
54 INNER JOIN {db_prefix}messages AS ms ON (ms.id_msg = t.id_first_msg)
|
Chris@76
|
55 WHERE t.id_topic = {int:current_topic}
|
Chris@76
|
56 LIMIT 1',
|
Chris@76
|
57 array(
|
Chris@76
|
58 'current_topic' => $topic,
|
Chris@76
|
59 )
|
Chris@76
|
60 );
|
Chris@76
|
61 list ($starter, $subject, $approved) = $smcFunc['db_fetch_row']($request);
|
Chris@76
|
62 $smcFunc['db_free_result']($request);
|
Chris@76
|
63
|
Chris@76
|
64 if ($starter == $user_info['id'] && !allowedTo('remove_any'))
|
Chris@76
|
65 isAllowedTo('remove_own');
|
Chris@76
|
66 else
|
Chris@76
|
67 isAllowedTo('remove_any');
|
Chris@76
|
68
|
Chris@76
|
69 // Can they see the topic?
|
Chris@76
|
70 if ($modSettings['postmod_active'] && !$approved && $starter != $user_info['id'])
|
Chris@76
|
71 isAllowedTo('approve_posts');
|
Chris@76
|
72
|
Chris@76
|
73 // Notify people that this topic has been removed.
|
Chris@76
|
74 sendNotifications($topic, 'remove');
|
Chris@76
|
75
|
Chris@76
|
76 removeTopics($topic);
|
Chris@76
|
77
|
Chris@76
|
78 // Note, only log topic ID in native form if it's not gone forever.
|
Chris@76
|
79 if (allowedTo('remove_any') || (allowedTo('remove_own') && $starter == $user_info['id']))
|
Chris@76
|
80 logAction('remove', array((empty($modSettings['recycle_enable']) || $modSettings['recycle_board'] != $board ? 'topic' : 'old_topic_id') => $topic, 'subject' => $subject, 'member' => $starter, 'board' => $board));
|
Chris@76
|
81
|
Chris@76
|
82 redirectexit('board=' . $board . '.0');
|
Chris@76
|
83 }
|
Chris@76
|
84
|
Chris@76
|
85 // Remove just a single post.
|
Chris@76
|
86 function DeleteMessage()
|
Chris@76
|
87 {
|
Chris@76
|
88 global $user_info, $topic, $board, $modSettings, $smcFunc;
|
Chris@76
|
89
|
Chris@76
|
90 checkSession('get');
|
Chris@76
|
91
|
Chris@76
|
92 $_REQUEST['msg'] = (int) $_REQUEST['msg'];
|
Chris@76
|
93
|
Chris@76
|
94 // Is $topic set?
|
Chris@76
|
95 if (empty($topic) && isset($_REQUEST['topic']))
|
Chris@76
|
96 $topic = (int) $_REQUEST['topic'];
|
Chris@76
|
97
|
Chris@76
|
98 $request = $smcFunc['db_query']('', '
|
Chris@76
|
99 SELECT t.id_member_started, m.id_member, m.subject, m.poster_time, m.approved
|
Chris@76
|
100 FROM {db_prefix}topics AS t
|
Chris@76
|
101 INNER JOIN {db_prefix}messages AS m ON (m.id_msg = {int:id_msg} AND m.id_topic = {int:current_topic})
|
Chris@76
|
102 WHERE t.id_topic = {int:current_topic}
|
Chris@76
|
103 LIMIT 1',
|
Chris@76
|
104 array(
|
Chris@76
|
105 'current_topic' => $topic,
|
Chris@76
|
106 'id_msg' => $_REQUEST['msg'],
|
Chris@76
|
107 )
|
Chris@76
|
108 );
|
Chris@76
|
109 list ($starter, $poster, $subject, $post_time, $approved) = $smcFunc['db_fetch_row']($request);
|
Chris@76
|
110 $smcFunc['db_free_result']($request);
|
Chris@76
|
111
|
Chris@76
|
112 // Verify they can see this!
|
Chris@76
|
113 if ($modSettings['postmod_active'] && !$approved && !empty($poster) && $poster != $user_info['id'])
|
Chris@76
|
114 isAllowedTo('approve_posts');
|
Chris@76
|
115
|
Chris@76
|
116 if ($poster == $user_info['id'])
|
Chris@76
|
117 {
|
Chris@76
|
118 if (!allowedTo('delete_own'))
|
Chris@76
|
119 {
|
Chris@76
|
120 if ($starter == $user_info['id'] && !allowedTo('delete_any'))
|
Chris@76
|
121 isAllowedTo('delete_replies');
|
Chris@76
|
122 elseif (!allowedTo('delete_any'))
|
Chris@76
|
123 isAllowedTo('delete_own');
|
Chris@76
|
124 }
|
Chris@76
|
125 elseif (!allowedTo('delete_any') && ($starter != $user_info['id'] || !allowedTo('delete_replies')) && !empty($modSettings['edit_disable_time']) && $post_time + $modSettings['edit_disable_time'] * 60 < time())
|
Chris@76
|
126 fatal_lang_error('modify_post_time_passed', false);
|
Chris@76
|
127 }
|
Chris@76
|
128 elseif ($starter == $user_info['id'] && !allowedTo('delete_any'))
|
Chris@76
|
129 isAllowedTo('delete_replies');
|
Chris@76
|
130 else
|
Chris@76
|
131 isAllowedTo('delete_any');
|
Chris@76
|
132
|
Chris@76
|
133 // If the full topic was removed go back to the board.
|
Chris@76
|
134 $full_topic = removeMessage($_REQUEST['msg']);
|
Chris@76
|
135
|
Chris@76
|
136 if (allowedTo('delete_any') && (!allowedTo('delete_own') || $poster != $user_info['id']))
|
Chris@76
|
137 logAction('delete', array('topic' => $topic, 'subject' => $subject, 'member' => $poster, 'board' => $board));
|
Chris@76
|
138
|
Chris@76
|
139 // We want to redirect back to recent action.
|
Chris@76
|
140 if (isset($_REQUEST['recent']))
|
Chris@76
|
141 redirectexit('action=recent');
|
Chris@76
|
142 elseif (isset($_REQUEST['profile'], $_REQUEST['start'], $_REQUEST['u']))
|
Chris@76
|
143 redirectexit('action=profile;u=' . $_REQUEST['u'] . ';area=showposts;start=' . $_REQUEST['start']);
|
Chris@76
|
144 elseif ($full_topic)
|
Chris@76
|
145 redirectexit('board=' . $board . '.0');
|
Chris@76
|
146 else
|
Chris@76
|
147 redirectexit('topic=' . $topic . '.' . $_REQUEST['start']);
|
Chris@76
|
148 }
|
Chris@76
|
149
|
Chris@76
|
150 // So long as you are sure... all old posts will be gone.
|
Chris@76
|
151 function RemoveOldTopics2()
|
Chris@76
|
152 {
|
Chris@76
|
153 global $modSettings, $smcFunc;
|
Chris@76
|
154
|
Chris@76
|
155 isAllowedTo('admin_forum');
|
Chris@76
|
156 checkSession('post', 'admin');
|
Chris@76
|
157
|
Chris@76
|
158 // No boards at all? Forget it then :/.
|
Chris@76
|
159 if (empty($_POST['boards']))
|
Chris@76
|
160 redirectexit('action=admin;area=maintain;sa=topics');
|
Chris@76
|
161
|
Chris@76
|
162 // This should exist, but we can make sure.
|
Chris@76
|
163 $_POST['delete_type'] = isset($_POST['delete_type']) ? $_POST['delete_type'] : 'nothing';
|
Chris@76
|
164
|
Chris@76
|
165 // Custom conditions.
|
Chris@76
|
166 $condition = '';
|
Chris@76
|
167 $condition_params = array(
|
Chris@76
|
168 'boards' => array_keys($_POST['boards']),
|
Chris@76
|
169 'poster_time' => time() - 3600 * 24 * $_POST['maxdays'],
|
Chris@76
|
170 );
|
Chris@76
|
171
|
Chris@76
|
172 // Just moved notice topics?
|
Chris@76
|
173 if ($_POST['delete_type'] == 'moved')
|
Chris@76
|
174 {
|
Chris@76
|
175 $condition .= '
|
Chris@76
|
176 AND m.icon = {string:icon}
|
Chris@76
|
177 AND t.locked = {int:locked}';
|
Chris@76
|
178 $condition_params['icon'] = 'moved';
|
Chris@76
|
179 $condition_params['locked'] = 1;
|
Chris@76
|
180 }
|
Chris@76
|
181 // Otherwise, maybe locked topics only?
|
Chris@76
|
182 elseif ($_POST['delete_type'] == 'locked')
|
Chris@76
|
183 {
|
Chris@76
|
184 $condition .= '
|
Chris@76
|
185 AND t.locked = {int:locked}';
|
Chris@76
|
186 $condition_params['locked'] = 1;
|
Chris@76
|
187 }
|
Chris@76
|
188
|
Chris@76
|
189 // Exclude stickies?
|
Chris@76
|
190 if (isset($_POST['delete_old_not_sticky']))
|
Chris@76
|
191 {
|
Chris@76
|
192 $condition .= '
|
Chris@76
|
193 AND t.is_sticky = {int:is_sticky}';
|
Chris@76
|
194 $condition_params['is_sticky'] = 0;
|
Chris@76
|
195 }
|
Chris@76
|
196
|
Chris@76
|
197 // All we're gonna do here is grab the id_topic's and send them to removeTopics().
|
Chris@76
|
198 $request = $smcFunc['db_query']('', '
|
Chris@76
|
199 SELECT t.id_topic
|
Chris@76
|
200 FROM {db_prefix}topics AS t
|
Chris@76
|
201 INNER JOIN {db_prefix}messages AS m ON (m.id_msg = t.id_last_msg)
|
Chris@76
|
202 WHERE
|
Chris@76
|
203 m.poster_time < {int:poster_time}' . $condition . '
|
Chris@76
|
204 AND t.id_board IN ({array_int:boards})',
|
Chris@76
|
205 $condition_params
|
Chris@76
|
206 );
|
Chris@76
|
207 $topics = array();
|
Chris@76
|
208 while ($row = $smcFunc['db_fetch_assoc']($request))
|
Chris@76
|
209 $topics[] = $row['id_topic'];
|
Chris@76
|
210 $smcFunc['db_free_result']($request);
|
Chris@76
|
211
|
Chris@76
|
212 removeTopics($topics, false, true);
|
Chris@76
|
213
|
Chris@76
|
214 // Log an action into the moderation log.
|
Chris@76
|
215 logAction('pruned', array('days' => $_POST['maxdays']));
|
Chris@76
|
216
|
Chris@76
|
217 redirectexit('action=admin;area=maintain;sa=topics;done=purgeold');
|
Chris@76
|
218 }
|
Chris@76
|
219
|
Chris@76
|
220 // Removes the passed id_topic's. (permissions are NOT checked here!)
|
Chris@76
|
221 function removeTopics($topics, $decreasePostCount = true, $ignoreRecycling = false)
|
Chris@76
|
222 {
|
Chris@76
|
223 global $sourcedir, $modSettings, $smcFunc;
|
Chris@76
|
224
|
Chris@76
|
225 // Nothing to do?
|
Chris@76
|
226 if (empty($topics))
|
Chris@76
|
227 return;
|
Chris@76
|
228 // Only a single topic.
|
Chris@76
|
229 elseif (is_numeric($topics))
|
Chris@76
|
230 $topics = array($topics);
|
Chris@76
|
231
|
Chris@76
|
232 // Decrease the post counts.
|
Chris@76
|
233 if ($decreasePostCount)
|
Chris@76
|
234 {
|
Chris@76
|
235 $requestMembers = $smcFunc['db_query']('', '
|
Chris@76
|
236 SELECT m.id_member, COUNT(*) AS posts
|
Chris@76
|
237 FROM {db_prefix}messages AS m
|
Chris@76
|
238 INNER JOIN {db_prefix}boards AS b ON (b.id_board = m.id_board)
|
Chris@76
|
239 WHERE m.id_topic IN ({array_int:topics})
|
Chris@76
|
240 AND m.icon != {string:recycled}
|
Chris@76
|
241 AND b.count_posts = {int:do_count_posts}
|
Chris@76
|
242 AND m.approved = {int:is_approved}
|
Chris@76
|
243 GROUP BY m.id_member',
|
Chris@76
|
244 array(
|
Chris@76
|
245 'do_count_posts' => 0,
|
Chris@76
|
246 'recycled' => 'recycled',
|
Chris@76
|
247 'topics' => $topics,
|
Chris@76
|
248 'is_approved' => 1,
|
Chris@76
|
249 )
|
Chris@76
|
250 );
|
Chris@76
|
251 if ($smcFunc['db_num_rows']($requestMembers) > 0)
|
Chris@76
|
252 {
|
Chris@76
|
253 while ($rowMembers = $smcFunc['db_fetch_assoc']($requestMembers))
|
Chris@76
|
254 updateMemberData($rowMembers['id_member'], array('posts' => 'posts - ' . $rowMembers['posts']));
|
Chris@76
|
255 }
|
Chris@76
|
256 $smcFunc['db_free_result']($requestMembers);
|
Chris@76
|
257 }
|
Chris@76
|
258
|
Chris@76
|
259 // Recycle topics that aren't in the recycle board...
|
Chris@76
|
260 if (!empty($modSettings['recycle_enable']) && $modSettings['recycle_board'] > 0 && !$ignoreRecycling)
|
Chris@76
|
261 {
|
Chris@76
|
262 $request = $smcFunc['db_query']('', '
|
Chris@76
|
263 SELECT id_topic, id_board, unapproved_posts, approved
|
Chris@76
|
264 FROM {db_prefix}topics
|
Chris@76
|
265 WHERE id_topic IN ({array_int:topics})
|
Chris@76
|
266 AND id_board != {int:recycle_board}
|
Chris@76
|
267 LIMIT ' . count($topics),
|
Chris@76
|
268 array(
|
Chris@76
|
269 'recycle_board' => $modSettings['recycle_board'],
|
Chris@76
|
270 'topics' => $topics,
|
Chris@76
|
271 )
|
Chris@76
|
272 );
|
Chris@76
|
273 if ($smcFunc['db_num_rows']($request) > 0)
|
Chris@76
|
274 {
|
Chris@76
|
275 // Get topics that will be recycled.
|
Chris@76
|
276 $recycleTopics = array();
|
Chris@76
|
277 while ($row = $smcFunc['db_fetch_assoc']($request))
|
Chris@76
|
278 {
|
Chris@76
|
279 if (function_exists('apache_reset_timeout'))
|
Chris@76
|
280 @apache_reset_timeout();
|
Chris@76
|
281
|
Chris@76
|
282 $recycleTopics[] = $row['id_topic'];
|
Chris@76
|
283
|
Chris@76
|
284 // Set the id_previous_board for this topic - and make it not sticky.
|
Chris@76
|
285 $smcFunc['db_query']('', '
|
Chris@76
|
286 UPDATE {db_prefix}topics
|
Chris@76
|
287 SET id_previous_board = {int:id_previous_board}, is_sticky = {int:not_sticky}
|
Chris@76
|
288 WHERE id_topic = {int:id_topic}',
|
Chris@76
|
289 array(
|
Chris@76
|
290 'id_previous_board' => $row['id_board'],
|
Chris@76
|
291 'id_topic' => $row['id_topic'],
|
Chris@76
|
292 'not_sticky' => 0,
|
Chris@76
|
293 )
|
Chris@76
|
294 );
|
Chris@76
|
295 }
|
Chris@76
|
296 $smcFunc['db_free_result']($request);
|
Chris@76
|
297
|
Chris@76
|
298 // Mark recycled topics as recycled.
|
Chris@76
|
299 $smcFunc['db_query']('', '
|
Chris@76
|
300 UPDATE {db_prefix}messages
|
Chris@76
|
301 SET icon = {string:recycled}
|
Chris@76
|
302 WHERE id_topic IN ({array_int:recycle_topics})',
|
Chris@76
|
303 array(
|
Chris@76
|
304 'recycle_topics' => $recycleTopics,
|
Chris@76
|
305 'recycled' => 'recycled',
|
Chris@76
|
306 )
|
Chris@76
|
307 );
|
Chris@76
|
308
|
Chris@76
|
309 // Move the topics to the recycle board.
|
Chris@76
|
310 require_once($sourcedir . '/MoveTopic.php');
|
Chris@76
|
311 moveTopics($recycleTopics, $modSettings['recycle_board']);
|
Chris@76
|
312
|
Chris@76
|
313 // Close reports that are being recycled.
|
Chris@76
|
314 require_once($sourcedir . '/ModerationCenter.php');
|
Chris@76
|
315
|
Chris@76
|
316 $smcFunc['db_query']('', '
|
Chris@76
|
317 UPDATE {db_prefix}log_reported
|
Chris@76
|
318 SET closed = {int:is_closed}
|
Chris@76
|
319 WHERE id_topic IN ({array_int:recycle_topics})',
|
Chris@76
|
320 array(
|
Chris@76
|
321 'recycle_topics' => $recycleTopics,
|
Chris@76
|
322 'is_closed' => 1,
|
Chris@76
|
323 )
|
Chris@76
|
324 );
|
Chris@76
|
325
|
Chris@76
|
326 updateSettings(array('last_mod_report_action' => time()));
|
Chris@76
|
327 recountOpenReports();
|
Chris@76
|
328
|
Chris@76
|
329 // Topics that were recycled don't need to be deleted, so subtract them.
|
Chris@76
|
330 $topics = array_diff($topics, $recycleTopics);
|
Chris@76
|
331 }
|
Chris@76
|
332 else
|
Chris@76
|
333 $smcFunc['db_free_result']($request);
|
Chris@76
|
334 }
|
Chris@76
|
335
|
Chris@76
|
336 // Still topics left to delete?
|
Chris@76
|
337 if (empty($topics))
|
Chris@76
|
338 return;
|
Chris@76
|
339
|
Chris@76
|
340 $adjustBoards = array();
|
Chris@76
|
341
|
Chris@76
|
342 // Find out how many posts we are deleting.
|
Chris@76
|
343 $request = $smcFunc['db_query']('', '
|
Chris@76
|
344 SELECT id_board, approved, COUNT(*) AS num_topics, SUM(unapproved_posts) AS unapproved_posts,
|
Chris@76
|
345 SUM(num_replies) AS num_replies
|
Chris@76
|
346 FROM {db_prefix}topics
|
Chris@76
|
347 WHERE id_topic IN ({array_int:topics})
|
Chris@76
|
348 GROUP BY id_board, approved',
|
Chris@76
|
349 array(
|
Chris@76
|
350 'topics' => $topics,
|
Chris@76
|
351 )
|
Chris@76
|
352 );
|
Chris@76
|
353 while ($row = $smcFunc['db_fetch_assoc']($request))
|
Chris@76
|
354 {
|
Chris@76
|
355 if (!isset($adjustBoards[$row['id_board']]['num_posts']))
|
Chris@76
|
356 {
|
Chris@76
|
357 $adjustBoards[$row['id_board']] = array(
|
Chris@76
|
358 'num_posts' => 0,
|
Chris@76
|
359 'num_topics' => 0,
|
Chris@76
|
360 'unapproved_posts' => 0,
|
Chris@76
|
361 'unapproved_topics' => 0,
|
Chris@76
|
362 'id_board' => $row['id_board']
|
Chris@76
|
363 );
|
Chris@76
|
364 }
|
Chris@76
|
365 // Posts = (num_replies + 1) for each approved topic.
|
Chris@76
|
366 $adjustBoards[$row['id_board']]['num_posts'] += $row['num_replies'] + ($row['approved'] ? $row['num_topics'] : 0);
|
Chris@76
|
367 $adjustBoards[$row['id_board']]['unapproved_posts'] += $row['unapproved_posts'];
|
Chris@76
|
368
|
Chris@76
|
369 // Add the topics to the right type.
|
Chris@76
|
370 if ($row['approved'])
|
Chris@76
|
371 $adjustBoards[$row['id_board']]['num_topics'] += $row['num_topics'];
|
Chris@76
|
372 else
|
Chris@76
|
373 $adjustBoards[$row['id_board']]['unapproved_topics'] += $row['num_topics'];
|
Chris@76
|
374 }
|
Chris@76
|
375 $smcFunc['db_free_result']($request);
|
Chris@76
|
376
|
Chris@76
|
377 // Decrease the posts/topics...
|
Chris@76
|
378 foreach ($adjustBoards as $stats)
|
Chris@76
|
379 {
|
Chris@76
|
380 if (function_exists('apache_reset_timeout'))
|
Chris@76
|
381 @apache_reset_timeout();
|
Chris@76
|
382
|
Chris@76
|
383 $smcFunc['db_query']('', '
|
Chris@76
|
384 UPDATE {db_prefix}boards
|
Chris@76
|
385 SET
|
Chris@76
|
386 num_posts = CASE WHEN {int:num_posts} > num_posts THEN 0 ELSE num_posts - {int:num_posts} END,
|
Chris@76
|
387 num_topics = CASE WHEN {int:num_topics} > num_topics THEN 0 ELSE num_topics - {int:num_topics} END,
|
Chris@76
|
388 unapproved_posts = CASE WHEN {int:unapproved_posts} > unapproved_posts THEN 0 ELSE unapproved_posts - {int:unapproved_posts} END,
|
Chris@76
|
389 unapproved_topics = CASE WHEN {int:unapproved_topics} > unapproved_topics THEN 0 ELSE unapproved_topics - {int:unapproved_topics} END
|
Chris@76
|
390 WHERE id_board = {int:id_board}',
|
Chris@76
|
391 array(
|
Chris@76
|
392 'id_board' => $stats['id_board'],
|
Chris@76
|
393 'num_posts' => $stats['num_posts'],
|
Chris@76
|
394 'num_topics' => $stats['num_topics'],
|
Chris@76
|
395 'unapproved_posts' => $stats['unapproved_posts'],
|
Chris@76
|
396 'unapproved_topics' => $stats['unapproved_topics'],
|
Chris@76
|
397 )
|
Chris@76
|
398 );
|
Chris@76
|
399 }
|
Chris@76
|
400
|
Chris@76
|
401 // Remove Polls.
|
Chris@76
|
402 $request = $smcFunc['db_query']('', '
|
Chris@76
|
403 SELECT id_poll
|
Chris@76
|
404 FROM {db_prefix}topics
|
Chris@76
|
405 WHERE id_topic IN ({array_int:topics})
|
Chris@76
|
406 AND id_poll > {int:no_poll}
|
Chris@76
|
407 LIMIT ' . count($topics),
|
Chris@76
|
408 array(
|
Chris@76
|
409 'no_poll' => 0,
|
Chris@76
|
410 'topics' => $topics,
|
Chris@76
|
411 )
|
Chris@76
|
412 );
|
Chris@76
|
413 $polls = array();
|
Chris@76
|
414 while ($row = $smcFunc['db_fetch_assoc']($request))
|
Chris@76
|
415 $polls[] = $row['id_poll'];
|
Chris@76
|
416 $smcFunc['db_free_result']($request);
|
Chris@76
|
417
|
Chris@76
|
418 if (!empty($polls))
|
Chris@76
|
419 {
|
Chris@76
|
420 $smcFunc['db_query']('', '
|
Chris@76
|
421 DELETE FROM {db_prefix}polls
|
Chris@76
|
422 WHERE id_poll IN ({array_int:polls})',
|
Chris@76
|
423 array(
|
Chris@76
|
424 'polls' => $polls,
|
Chris@76
|
425 )
|
Chris@76
|
426 );
|
Chris@76
|
427 $smcFunc['db_query']('', '
|
Chris@76
|
428 DELETE FROM {db_prefix}poll_choices
|
Chris@76
|
429 WHERE id_poll IN ({array_int:polls})',
|
Chris@76
|
430 array(
|
Chris@76
|
431 'polls' => $polls,
|
Chris@76
|
432 )
|
Chris@76
|
433 );
|
Chris@76
|
434 $smcFunc['db_query']('', '
|
Chris@76
|
435 DELETE FROM {db_prefix}log_polls
|
Chris@76
|
436 WHERE id_poll IN ({array_int:polls})',
|
Chris@76
|
437 array(
|
Chris@76
|
438 'polls' => $polls,
|
Chris@76
|
439 )
|
Chris@76
|
440 );
|
Chris@76
|
441 }
|
Chris@76
|
442
|
Chris@76
|
443 // Get rid of the attachment, if it exists.
|
Chris@76
|
444 require_once($sourcedir . '/ManageAttachments.php');
|
Chris@76
|
445 $attachmentQuery = array(
|
Chris@76
|
446 'attachment_type' => 0,
|
Chris@76
|
447 'id_topic' => $topics,
|
Chris@76
|
448 );
|
Chris@76
|
449 removeAttachments($attachmentQuery, 'messages');
|
Chris@76
|
450
|
Chris@76
|
451 // Delete possible search index entries.
|
Chris@76
|
452 if (!empty($modSettings['search_custom_index_config']))
|
Chris@76
|
453 {
|
Chris@76
|
454 $customIndexSettings = unserialize($modSettings['search_custom_index_config']);
|
Chris@76
|
455
|
Chris@76
|
456 $words = array();
|
Chris@76
|
457 $messages = array();
|
Chris@76
|
458 $request = $smcFunc['db_query']('', '
|
Chris@76
|
459 SELECT id_msg, body
|
Chris@76
|
460 FROM {db_prefix}messages
|
Chris@76
|
461 WHERE id_topic IN ({array_int:topics})',
|
Chris@76
|
462 array(
|
Chris@76
|
463 'topics' => $topics,
|
Chris@76
|
464 )
|
Chris@76
|
465 );
|
Chris@76
|
466 while ($row = $smcFunc['db_fetch_assoc']($request))
|
Chris@76
|
467 {
|
Chris@76
|
468 if (function_exists('apache_reset_timeout'))
|
Chris@76
|
469 @apache_reset_timeout();
|
Chris@76
|
470
|
Chris@76
|
471 $words = array_merge($words, text2words($row['body'], $customIndexSettings['bytes_per_word'], true));
|
Chris@76
|
472 $messages[] = $row['id_msg'];
|
Chris@76
|
473 }
|
Chris@76
|
474 $smcFunc['db_free_result']($request);
|
Chris@76
|
475 $words = array_unique($words);
|
Chris@76
|
476
|
Chris@76
|
477 if (!empty($words) && !empty($messages))
|
Chris@76
|
478 $smcFunc['db_query']('', '
|
Chris@76
|
479 DELETE FROM {db_prefix}log_search_words
|
Chris@76
|
480 WHERE id_word IN ({array_int:word_list})
|
Chris@76
|
481 AND id_msg IN ({array_int:message_list})',
|
Chris@76
|
482 array(
|
Chris@76
|
483 'word_list' => $words,
|
Chris@76
|
484 'message_list' => $messages,
|
Chris@76
|
485 )
|
Chris@76
|
486 );
|
Chris@76
|
487 }
|
Chris@76
|
488
|
Chris@76
|
489 // Delete anything related to the topic.
|
Chris@76
|
490 $smcFunc['db_query']('', '
|
Chris@76
|
491 DELETE FROM {db_prefix}messages
|
Chris@76
|
492 WHERE id_topic IN ({array_int:topics})',
|
Chris@76
|
493 array(
|
Chris@76
|
494 'topics' => $topics,
|
Chris@76
|
495 )
|
Chris@76
|
496 );
|
Chris@76
|
497 $smcFunc['db_query']('', '
|
Chris@76
|
498 DELETE FROM {db_prefix}calendar
|
Chris@76
|
499 WHERE id_topic IN ({array_int:topics})',
|
Chris@76
|
500 array(
|
Chris@76
|
501 'topics' => $topics,
|
Chris@76
|
502 )
|
Chris@76
|
503 );
|
Chris@76
|
504 $smcFunc['db_query']('', '
|
Chris@76
|
505 DELETE FROM {db_prefix}log_topics
|
Chris@76
|
506 WHERE id_topic IN ({array_int:topics})',
|
Chris@76
|
507 array(
|
Chris@76
|
508 'topics' => $topics,
|
Chris@76
|
509 )
|
Chris@76
|
510 );
|
Chris@76
|
511 $smcFunc['db_query']('', '
|
Chris@76
|
512 DELETE FROM {db_prefix}log_notify
|
Chris@76
|
513 WHERE id_topic IN ({array_int:topics})',
|
Chris@76
|
514 array(
|
Chris@76
|
515 'topics' => $topics,
|
Chris@76
|
516 )
|
Chris@76
|
517 );
|
Chris@76
|
518 $smcFunc['db_query']('', '
|
Chris@76
|
519 DELETE FROM {db_prefix}topics
|
Chris@76
|
520 WHERE id_topic IN ({array_int:topics})',
|
Chris@76
|
521 array(
|
Chris@76
|
522 'topics' => $topics,
|
Chris@76
|
523 )
|
Chris@76
|
524 );
|
Chris@76
|
525 $smcFunc['db_query']('', '
|
Chris@76
|
526 DELETE FROM {db_prefix}log_search_subjects
|
Chris@76
|
527 WHERE id_topic IN ({array_int:topics})',
|
Chris@76
|
528 array(
|
Chris@76
|
529 'topics' => $topics,
|
Chris@76
|
530 )
|
Chris@76
|
531 );
|
Chris@76
|
532
|
Chris@76
|
533 // Update the totals...
|
Chris@76
|
534 updateStats('message');
|
Chris@76
|
535 updateStats('topic');
|
Chris@76
|
536 updateSettings(array(
|
Chris@76
|
537 'calendar_updated' => time(),
|
Chris@76
|
538 ));
|
Chris@76
|
539
|
Chris@76
|
540 require_once($sourcedir . '/Subs-Post.php');
|
Chris@76
|
541 $updates = array();
|
Chris@76
|
542 foreach ($adjustBoards as $stats)
|
Chris@76
|
543 $updates[] = $stats['id_board'];
|
Chris@76
|
544 updateLastMessages($updates);
|
Chris@76
|
545 }
|
Chris@76
|
546
|
Chris@76
|
547 // Remove a specific message (including permission checks).
|
Chris@76
|
548 function removeMessage($message, $decreasePostCount = true)
|
Chris@76
|
549 {
|
Chris@76
|
550 global $board, $sourcedir, $modSettings, $user_info, $smcFunc, $context;
|
Chris@76
|
551
|
Chris@76
|
552 if (empty($message) || !is_numeric($message))
|
Chris@76
|
553 return false;
|
Chris@76
|
554
|
Chris@76
|
555 $request = $smcFunc['db_query']('', '
|
Chris@76
|
556 SELECT
|
Chris@76
|
557 m.id_member, m.icon, m.poster_time, m.subject,' . (empty($modSettings['search_custom_index_config']) ? '' : ' m.body,') . '
|
Chris@76
|
558 m.approved, t.id_topic, t.id_first_msg, t.id_last_msg, t.num_replies, t.id_board,
|
Chris@76
|
559 t.id_member_started AS id_member_poster,
|
Chris@76
|
560 b.count_posts
|
Chris@76
|
561 FROM {db_prefix}messages AS m
|
Chris@76
|
562 INNER JOIN {db_prefix}topics AS t ON (t.id_topic = m.id_topic)
|
Chris@76
|
563 INNER JOIN {db_prefix}boards AS b ON (b.id_board = t.id_board)
|
Chris@76
|
564 WHERE m.id_msg = {int:id_msg}
|
Chris@76
|
565 LIMIT 1',
|
Chris@76
|
566 array(
|
Chris@76
|
567 'id_msg' => $message,
|
Chris@76
|
568 )
|
Chris@76
|
569 );
|
Chris@76
|
570 if ($smcFunc['db_num_rows']($request) == 0)
|
Chris@76
|
571 return false;
|
Chris@76
|
572 $row = $smcFunc['db_fetch_assoc']($request);
|
Chris@76
|
573 $smcFunc['db_free_result']($request);
|
Chris@76
|
574
|
Chris@76
|
575 if (empty($board) || $row['id_board'] != $board)
|
Chris@76
|
576 {
|
Chris@76
|
577 $delete_any = boardsAllowedTo('delete_any');
|
Chris@76
|
578
|
Chris@76
|
579 if (!in_array(0, $delete_any) && !in_array($row['id_board'], $delete_any))
|
Chris@76
|
580 {
|
Chris@76
|
581 $delete_own = boardsAllowedTo('delete_own');
|
Chris@76
|
582 $delete_own = in_array(0, $delete_own) || in_array($row['id_board'], $delete_own);
|
Chris@76
|
583 $delete_replies = boardsAllowedTo('delete_replies');
|
Chris@76
|
584 $delete_replies = in_array(0, $delete_replies) || in_array($row['id_board'], $delete_replies);
|
Chris@76
|
585
|
Chris@76
|
586 if ($row['id_member'] == $user_info['id'])
|
Chris@76
|
587 {
|
Chris@76
|
588 if (!$delete_own)
|
Chris@76
|
589 {
|
Chris@76
|
590 if ($row['id_member_poster'] == $user_info['id'])
|
Chris@76
|
591 {
|
Chris@76
|
592 if (!$delete_replies)
|
Chris@76
|
593 fatal_lang_error('cannot_delete_replies', 'permission');
|
Chris@76
|
594 }
|
Chris@76
|
595 else
|
Chris@76
|
596 fatal_lang_error('cannot_delete_own', 'permission');
|
Chris@76
|
597 }
|
Chris@76
|
598 elseif (($row['id_member_poster'] != $user_info['id'] || !$delete_replies) && !empty($modSettings['edit_disable_time']) && $row['poster_time'] + $modSettings['edit_disable_time'] * 60 < time())
|
Chris@76
|
599 fatal_lang_error('modify_post_time_passed', false);
|
Chris@76
|
600 }
|
Chris@76
|
601 elseif ($row['id_member_poster'] == $user_info['id'])
|
Chris@76
|
602 {
|
Chris@76
|
603 if (!$delete_replies)
|
Chris@76
|
604 fatal_lang_error('cannot_delete_replies', 'permission');
|
Chris@76
|
605 }
|
Chris@76
|
606 else
|
Chris@76
|
607 fatal_lang_error('cannot_delete_any', 'permission');
|
Chris@76
|
608 }
|
Chris@76
|
609
|
Chris@76
|
610 // Can't delete an unapproved message, if you can't see it!
|
Chris@76
|
611 if ($modSettings['postmod_active'] && !$row['approved'] && $row['id_member'] != $user_info['id'] && !(in_array(0, $delete_any) || in_array($row['id_board'], $delete_any)))
|
Chris@76
|
612 {
|
Chris@76
|
613 $approve_posts = boardsAllowedTo('approve_posts');
|
Chris@76
|
614 if (!in_array(0, $approve_posts) && !in_array($row['id_board'], $approve_posts))
|
Chris@76
|
615 return false;
|
Chris@76
|
616 }
|
Chris@76
|
617 }
|
Chris@76
|
618 else
|
Chris@76
|
619 {
|
Chris@76
|
620 // Check permissions to delete this message.
|
Chris@76
|
621 if ($row['id_member'] == $user_info['id'])
|
Chris@76
|
622 {
|
Chris@76
|
623 if (!allowedTo('delete_own'))
|
Chris@76
|
624 {
|
Chris@76
|
625 if ($row['id_member_poster'] == $user_info['id'] && !allowedTo('delete_any'))
|
Chris@76
|
626 isAllowedTo('delete_replies');
|
Chris@76
|
627 elseif (!allowedTo('delete_any'))
|
Chris@76
|
628 isAllowedTo('delete_own');
|
Chris@76
|
629 }
|
Chris@76
|
630 elseif (!allowedTo('delete_any') && ($row['id_member_poster'] != $user_info['id'] || !allowedTo('delete_replies')) && !empty($modSettings['edit_disable_time']) && $row['poster_time'] + $modSettings['edit_disable_time'] * 60 < time())
|
Chris@76
|
631 fatal_lang_error('modify_post_time_passed', false);
|
Chris@76
|
632 }
|
Chris@76
|
633 elseif ($row['id_member_poster'] == $user_info['id'] && !allowedTo('delete_any'))
|
Chris@76
|
634 isAllowedTo('delete_replies');
|
Chris@76
|
635 else
|
Chris@76
|
636 isAllowedTo('delete_any');
|
Chris@76
|
637
|
Chris@76
|
638 if ($modSettings['postmod_active'] && !$row['approved'] && $row['id_member'] != $user_info['id'] && !allowedTo('delete_own'))
|
Chris@76
|
639 isAllowedTo('approve_posts');
|
Chris@76
|
640 }
|
Chris@76
|
641
|
Chris@76
|
642 // Close any moderation reports for this message.
|
Chris@76
|
643 $smcFunc['db_query']('', '
|
Chris@76
|
644 UPDATE {db_prefix}log_reported
|
Chris@76
|
645 SET closed = {int:is_closed}
|
Chris@76
|
646 WHERE id_msg = {int:id_msg}',
|
Chris@76
|
647 array(
|
Chris@76
|
648 'is_closed' => 1,
|
Chris@76
|
649 'id_msg' => $message,
|
Chris@76
|
650 )
|
Chris@76
|
651 );
|
Chris@76
|
652 if ($smcFunc['db_affected_rows']() != 0)
|
Chris@76
|
653 {
|
Chris@76
|
654 require_once($sourcedir . '/ModerationCenter.php');
|
Chris@76
|
655 updateSettings(array('last_mod_report_action' => time()));
|
Chris@76
|
656 recountOpenReports();
|
Chris@76
|
657 }
|
Chris@76
|
658
|
Chris@76
|
659 // Delete the *whole* topic, but only if the topic consists of one message.
|
Chris@76
|
660 if ($row['id_first_msg'] == $message)
|
Chris@76
|
661 {
|
Chris@76
|
662 if (empty($board) || $row['id_board'] != $board)
|
Chris@76
|
663 {
|
Chris@76
|
664 $remove_any = boardsAllowedTo('remove_any');
|
Chris@76
|
665 $remove_any = in_array(0, $remove_any) || in_array($row['id_board'], $remove_any);
|
Chris@76
|
666 if (!$remove_any)
|
Chris@76
|
667 {
|
Chris@76
|
668 $remove_own = boardsAllowedTo('remove_own');
|
Chris@76
|
669 $remove_own = in_array(0, $remove_own) || in_array($row['id_board'], $remove_own);
|
Chris@76
|
670 }
|
Chris@76
|
671
|
Chris@76
|
672 if ($row['id_member'] != $user_info['id'] && !$remove_any)
|
Chris@76
|
673 fatal_lang_error('cannot_remove_any', 'permission');
|
Chris@76
|
674 elseif (!$remove_any && !$remove_own)
|
Chris@76
|
675 fatal_lang_error('cannot_remove_own', 'permission');
|
Chris@76
|
676 }
|
Chris@76
|
677 else
|
Chris@76
|
678 {
|
Chris@76
|
679 // Check permissions to delete a whole topic.
|
Chris@76
|
680 if ($row['id_member'] != $user_info['id'])
|
Chris@76
|
681 isAllowedTo('remove_any');
|
Chris@76
|
682 elseif (!allowedTo('remove_any'))
|
Chris@76
|
683 isAllowedTo('remove_own');
|
Chris@76
|
684 }
|
Chris@76
|
685
|
Chris@76
|
686 // ...if there is only one post.
|
Chris@76
|
687 if (!empty($row['num_replies']))
|
Chris@76
|
688 fatal_lang_error('delFirstPost', false);
|
Chris@76
|
689
|
Chris@76
|
690 removeTopics($row['id_topic']);
|
Chris@76
|
691 return true;
|
Chris@76
|
692 }
|
Chris@76
|
693
|
Chris@76
|
694 // Deleting a recycled message can not lower anyone's post count.
|
Chris@76
|
695 if ($row['icon'] == 'recycled')
|
Chris@76
|
696 $decreasePostCount = false;
|
Chris@76
|
697
|
Chris@76
|
698 // This is the last post, update the last post on the board.
|
Chris@76
|
699 if ($row['id_last_msg'] == $message)
|
Chris@76
|
700 {
|
Chris@76
|
701 // Find the last message, set it, and decrease the post count.
|
Chris@76
|
702 $request = $smcFunc['db_query']('', '
|
Chris@76
|
703 SELECT id_msg, id_member
|
Chris@76
|
704 FROM {db_prefix}messages
|
Chris@76
|
705 WHERE id_topic = {int:id_topic}
|
Chris@76
|
706 AND id_msg != {int:id_msg}
|
Chris@76
|
707 ORDER BY ' . ($modSettings['postmod_active'] ? 'approved DESC, ' : '') . 'id_msg DESC
|
Chris@76
|
708 LIMIT 1',
|
Chris@76
|
709 array(
|
Chris@76
|
710 'id_topic' => $row['id_topic'],
|
Chris@76
|
711 'id_msg' => $message,
|
Chris@76
|
712 )
|
Chris@76
|
713 );
|
Chris@76
|
714 $row2 = $smcFunc['db_fetch_assoc']($request);
|
Chris@76
|
715 $smcFunc['db_free_result']($request);
|
Chris@76
|
716
|
Chris@76
|
717 $smcFunc['db_query']('', '
|
Chris@76
|
718 UPDATE {db_prefix}topics
|
Chris@76
|
719 SET
|
Chris@76
|
720 id_last_msg = {int:id_last_msg},
|
Chris@76
|
721 id_member_updated = {int:id_member_updated}' . (!$modSettings['postmod_active'] || $row['approved'] ? ',
|
Chris@76
|
722 num_replies = CASE WHEN num_replies = {int:no_replies} THEN 0 ELSE num_replies - 1 END' : ',
|
Chris@76
|
723 unapproved_posts = CASE WHEN unapproved_posts = {int:no_unapproved} THEN 0 ELSE unapproved_posts - 1 END') . '
|
Chris@76
|
724 WHERE id_topic = {int:id_topic}',
|
Chris@76
|
725 array(
|
Chris@76
|
726 'id_last_msg' => $row2['id_msg'],
|
Chris@76
|
727 'id_member_updated' => $row2['id_member'],
|
Chris@76
|
728 'no_replies' => 0,
|
Chris@76
|
729 'no_unapproved' => 0,
|
Chris@76
|
730 'id_topic' => $row['id_topic'],
|
Chris@76
|
731 )
|
Chris@76
|
732 );
|
Chris@76
|
733 }
|
Chris@76
|
734 // Only decrease post counts.
|
Chris@76
|
735 else
|
Chris@76
|
736 $smcFunc['db_query']('', '
|
Chris@76
|
737 UPDATE {db_prefix}topics
|
Chris@76
|
738 SET ' . ($row['approved'] ? '
|
Chris@76
|
739 num_replies = CASE WHEN num_replies = {int:no_replies} THEN 0 ELSE num_replies - 1 END' : '
|
Chris@76
|
740 unapproved_posts = CASE WHEN unapproved_posts = {int:no_unapproved} THEN 0 ELSE unapproved_posts - 1 END') . '
|
Chris@76
|
741 WHERE id_topic = {int:id_topic}',
|
Chris@76
|
742 array(
|
Chris@76
|
743 'no_replies' => 0,
|
Chris@76
|
744 'no_unapproved' => 0,
|
Chris@76
|
745 'id_topic' => $row['id_topic'],
|
Chris@76
|
746 )
|
Chris@76
|
747 );
|
Chris@76
|
748
|
Chris@76
|
749 // Default recycle to false.
|
Chris@76
|
750 $recycle = false;
|
Chris@76
|
751
|
Chris@76
|
752 // If recycle topics has been set, make a copy of this message in the recycle board.
|
Chris@76
|
753 // Make sure we're not recycling messages that are already on the recycle board.
|
Chris@76
|
754 if (!empty($modSettings['recycle_enable']) && $row['id_board'] != $modSettings['recycle_board'] && $row['icon'] != 'recycled')
|
Chris@76
|
755 {
|
Chris@76
|
756 // Check if the recycle board exists and if so get the read status.
|
Chris@76
|
757 $request = $smcFunc['db_query']('', '
|
Chris@76
|
758 SELECT (IFNULL(lb.id_msg, 0) >= b.id_msg_updated) AS is_seen, id_last_msg
|
Chris@76
|
759 FROM {db_prefix}boards AS b
|
Chris@76
|
760 LEFT JOIN {db_prefix}log_boards AS lb ON (lb.id_board = b.id_board AND lb.id_member = {int:current_member})
|
Chris@76
|
761 WHERE b.id_board = {int:recycle_board}',
|
Chris@76
|
762 array(
|
Chris@76
|
763 'current_member' => $user_info['id'],
|
Chris@76
|
764 'recycle_board' => $modSettings['recycle_board'],
|
Chris@76
|
765 )
|
Chris@76
|
766 );
|
Chris@76
|
767 if ($smcFunc['db_num_rows']($request) == 0)
|
Chris@76
|
768 fatal_lang_error('recycle_no_valid_board');
|
Chris@76
|
769 list ($isRead, $last_board_msg) = $smcFunc['db_fetch_row']($request);
|
Chris@76
|
770 $smcFunc['db_free_result']($request);
|
Chris@76
|
771
|
Chris@76
|
772 // Is there an existing topic in the recycle board to group this post with?
|
Chris@76
|
773 $request = $smcFunc['db_query']('', '
|
Chris@76
|
774 SELECT id_topic, id_first_msg, id_last_msg
|
Chris@76
|
775 FROM {db_prefix}topics
|
Chris@76
|
776 WHERE id_previous_topic = {int:id_previous_topic}
|
Chris@76
|
777 AND id_board = {int:recycle_board}',
|
Chris@76
|
778 array(
|
Chris@76
|
779 'id_previous_topic' => $row['id_topic'],
|
Chris@76
|
780 'recycle_board' => $modSettings['recycle_board'],
|
Chris@76
|
781 )
|
Chris@76
|
782 );
|
Chris@76
|
783 list ($id_recycle_topic, $first_topic_msg, $last_topic_msg) = $smcFunc['db_fetch_row']($request);
|
Chris@76
|
784 $smcFunc['db_free_result']($request);
|
Chris@76
|
785
|
Chris@76
|
786 // Insert a new topic in the recycle board if $id_recycle_topic is empty.
|
Chris@76
|
787 if (empty($id_recycle_topic))
|
Chris@76
|
788 $smcFunc['db_insert']('',
|
Chris@76
|
789 '{db_prefix}topics',
|
Chris@76
|
790 array(
|
Chris@76
|
791 'id_board' => 'int', 'id_member_started' => 'int', 'id_member_updated' => 'int', 'id_first_msg' => 'int',
|
Chris@76
|
792 'id_last_msg' => 'int', 'unapproved_posts' => 'int', 'approved' => 'int', 'id_previous_topic' => 'int',
|
Chris@76
|
793 ),
|
Chris@76
|
794 array(
|
Chris@76
|
795 $modSettings['recycle_board'], $row['id_member'], $row['id_member'], $message,
|
Chris@76
|
796 $message, 0, 1, $row['id_topic'],
|
Chris@76
|
797 ),
|
Chris@76
|
798 array('id_topic')
|
Chris@76
|
799 );
|
Chris@76
|
800
|
Chris@76
|
801 // Capture the ID of the new topic...
|
Chris@76
|
802 $topicID = empty($id_recycle_topic) ? $smcFunc['db_insert_id']('{db_prefix}topics', 'id_topic') : $id_recycle_topic;
|
Chris@76
|
803
|
Chris@76
|
804 // If the topic creation went successful, move the message.
|
Chris@76
|
805 if ($topicID > 0)
|
Chris@76
|
806 {
|
Chris@76
|
807 $smcFunc['db_query']('', '
|
Chris@76
|
808 UPDATE {db_prefix}messages
|
Chris@76
|
809 SET
|
Chris@76
|
810 id_topic = {int:id_topic},
|
Chris@76
|
811 id_board = {int:recycle_board},
|
Chris@76
|
812 icon = {string:recycled},
|
Chris@76
|
813 approved = {int:is_approved}
|
Chris@76
|
814 WHERE id_msg = {int:id_msg}',
|
Chris@76
|
815 array(
|
Chris@76
|
816 'id_topic' => $topicID,
|
Chris@76
|
817 'recycle_board' => $modSettings['recycle_board'],
|
Chris@76
|
818 'id_msg' => $message,
|
Chris@76
|
819 'recycled' => 'recycled',
|
Chris@76
|
820 'is_approved' => 1,
|
Chris@76
|
821 )
|
Chris@76
|
822 );
|
Chris@76
|
823
|
Chris@76
|
824 // Take any reported posts with us...
|
Chris@76
|
825 $smcFunc['db_query']('', '
|
Chris@76
|
826 UPDATE {db_prefix}log_reported
|
Chris@76
|
827 SET
|
Chris@76
|
828 id_topic = {int:id_topic},
|
Chris@76
|
829 id_board = {int:recycle_board}
|
Chris@76
|
830 WHERE id_msg = {int:id_msg}',
|
Chris@76
|
831 array(
|
Chris@76
|
832 'id_topic' => $topicID,
|
Chris@76
|
833 'recycle_board' => $modSettings['recycle_board'],
|
Chris@76
|
834 'id_msg' => $message,
|
Chris@76
|
835 )
|
Chris@76
|
836 );
|
Chris@76
|
837
|
Chris@76
|
838 // Mark recycled topic as read.
|
Chris@76
|
839 if (!$user_info['is_guest'])
|
Chris@76
|
840 $smcFunc['db_insert']('replace',
|
Chris@76
|
841 '{db_prefix}log_topics',
|
Chris@76
|
842 array('id_topic' => 'int', 'id_member' => 'int', 'id_msg' => 'int'),
|
Chris@76
|
843 array($topicID, $user_info['id'], $modSettings['maxMsgID']),
|
Chris@76
|
844 array('id_topic', 'id_member')
|
Chris@76
|
845 );
|
Chris@76
|
846
|
Chris@76
|
847 // Mark recycle board as seen, if it was marked as seen before.
|
Chris@76
|
848 if (!empty($isRead) && !$user_info['is_guest'])
|
Chris@76
|
849 $smcFunc['db_insert']('replace',
|
Chris@76
|
850 '{db_prefix}log_boards',
|
Chris@76
|
851 array('id_board' => 'int', 'id_member' => 'int', 'id_msg' => 'int'),
|
Chris@76
|
852 array($modSettings['recycle_board'], $user_info['id'], $modSettings['maxMsgID']),
|
Chris@76
|
853 array('id_board', 'id_member')
|
Chris@76
|
854 );
|
Chris@76
|
855
|
Chris@76
|
856 // Add one topic and post to the recycle bin board.
|
Chris@76
|
857 $smcFunc['db_query']('', '
|
Chris@76
|
858 UPDATE {db_prefix}boards
|
Chris@76
|
859 SET
|
Chris@76
|
860 num_topics = num_topics + {int:num_topics_inc},
|
Chris@76
|
861 num_posts = num_posts + 1' .
|
Chris@76
|
862 ($message > $last_board_msg ? ', id_last_msg = {int:id_merged_msg}' : '') . '
|
Chris@76
|
863 WHERE id_board = {int:recycle_board}',
|
Chris@76
|
864 array(
|
Chris@76
|
865 'num_topics_inc' => empty($id_recycle_topic) ? 1 : 0,
|
Chris@76
|
866 'recycle_board' => $modSettings['recycle_board'],
|
Chris@76
|
867 'id_merged_msg' => $message,
|
Chris@76
|
868 )
|
Chris@76
|
869 );
|
Chris@76
|
870
|
Chris@76
|
871 // Lets increase the num_replies, and the first/last message ID as appropriate.
|
Chris@76
|
872 if (!empty($id_recycle_topic))
|
Chris@76
|
873 $smcFunc['db_query']('', '
|
Chris@76
|
874 UPDATE {db_prefix}topics
|
Chris@76
|
875 SET num_replies = num_replies + 1' .
|
Chris@76
|
876 ($message > $last_topic_msg ? ', id_last_msg = {int:id_merged_msg}' : '') .
|
Chris@76
|
877 ($message < $first_topic_msg ? ', id_first_msg = {int:id_merged_msg}' : '') . '
|
Chris@76
|
878 WHERE id_topic = {int:id_recycle_topic}',
|
Chris@76
|
879 array(
|
Chris@76
|
880 'id_recycle_topic' => $id_recycle_topic,
|
Chris@76
|
881 'id_merged_msg' => $message,
|
Chris@76
|
882 )
|
Chris@76
|
883 );
|
Chris@76
|
884
|
Chris@76
|
885 // Make sure this message isn't getting deleted later on.
|
Chris@76
|
886 $recycle = true;
|
Chris@76
|
887
|
Chris@76
|
888 // Make sure we update the search subject index.
|
Chris@76
|
889 updateStats('subject', $topicID, $row['subject']);
|
Chris@76
|
890 }
|
Chris@76
|
891
|
Chris@76
|
892 // If it wasn't approved don't keep it in the queue.
|
Chris@76
|
893 if (!$row['approved'])
|
Chris@76
|
894 $smcFunc['db_query']('', '
|
Chris@76
|
895 DELETE FROM {db_prefix}approval_queue
|
Chris@76
|
896 WHERE id_msg = {int:id_msg}
|
Chris@76
|
897 AND id_attach = {int:id_attach}',
|
Chris@76
|
898 array(
|
Chris@76
|
899 'id_msg' => $message,
|
Chris@76
|
900 'id_attach' => 0,
|
Chris@76
|
901 )
|
Chris@76
|
902 );
|
Chris@76
|
903 }
|
Chris@76
|
904
|
Chris@76
|
905 $smcFunc['db_query']('', '
|
Chris@76
|
906 UPDATE {db_prefix}boards
|
Chris@76
|
907 SET ' . ($row['approved'] ? '
|
Chris@76
|
908 num_posts = CASE WHEN num_posts = {int:no_posts} THEN 0 ELSE num_posts - 1 END' : '
|
Chris@76
|
909 unapproved_posts = CASE WHEN unapproved_posts = {int:no_unapproved} THEN 0 ELSE unapproved_posts - 1 END') . '
|
Chris@76
|
910 WHERE id_board = {int:id_board}',
|
Chris@76
|
911 array(
|
Chris@76
|
912 'no_posts' => 0,
|
Chris@76
|
913 'no_unapproved' => 0,
|
Chris@76
|
914 'id_board' => $row['id_board'],
|
Chris@76
|
915 )
|
Chris@76
|
916 );
|
Chris@76
|
917
|
Chris@76
|
918 // If the poster was registered and the board this message was on incremented
|
Chris@76
|
919 // the member's posts when it was posted, decrease his or her post count.
|
Chris@76
|
920 if (!empty($row['id_member']) && $decreasePostCount && empty($row['count_posts']) && $row['approved'])
|
Chris@76
|
921 updateMemberData($row['id_member'], array('posts' => '-'));
|
Chris@76
|
922
|
Chris@76
|
923 // Only remove posts if they're not recycled.
|
Chris@76
|
924 if (!$recycle)
|
Chris@76
|
925 {
|
Chris@76
|
926 // Remove the message!
|
Chris@76
|
927 $smcFunc['db_query']('', '
|
Chris@76
|
928 DELETE FROM {db_prefix}messages
|
Chris@76
|
929 WHERE id_msg = {int:id_msg}',
|
Chris@76
|
930 array(
|
Chris@76
|
931 'id_msg' => $message,
|
Chris@76
|
932 )
|
Chris@76
|
933 );
|
Chris@76
|
934
|
Chris@76
|
935 if (!empty($modSettings['search_custom_index_config']))
|
Chris@76
|
936 {
|
Chris@76
|
937 $customIndexSettings = unserialize($modSettings['search_custom_index_config']);
|
Chris@76
|
938 $words = text2words($row['body'], $customIndexSettings['bytes_per_word'], true);
|
Chris@76
|
939 if (!empty($words))
|
Chris@76
|
940 $smcFunc['db_query']('', '
|
Chris@76
|
941 DELETE FROM {db_prefix}log_search_words
|
Chris@76
|
942 WHERE id_word IN ({array_int:word_list})
|
Chris@76
|
943 AND id_msg = {int:id_msg}',
|
Chris@76
|
944 array(
|
Chris@76
|
945 'word_list' => $words,
|
Chris@76
|
946 'id_msg' => $message,
|
Chris@76
|
947 )
|
Chris@76
|
948 );
|
Chris@76
|
949 }
|
Chris@76
|
950
|
Chris@76
|
951 // Delete attachment(s) if they exist.
|
Chris@76
|
952 require_once($sourcedir . '/ManageAttachments.php');
|
Chris@76
|
953 $attachmentQuery = array(
|
Chris@76
|
954 'attachment_type' => 0,
|
Chris@76
|
955 'id_msg' => $message,
|
Chris@76
|
956 );
|
Chris@76
|
957 removeAttachments($attachmentQuery);
|
Chris@76
|
958 }
|
Chris@76
|
959
|
Chris@76
|
960 // Update the pesky statistics.
|
Chris@76
|
961 updateStats('message');
|
Chris@76
|
962 updateStats('topic');
|
Chris@76
|
963 updateSettings(array(
|
Chris@76
|
964 'calendar_updated' => time(),
|
Chris@76
|
965 ));
|
Chris@76
|
966
|
Chris@76
|
967 // And now to update the last message of each board we messed with.
|
Chris@76
|
968 require_once($sourcedir . '/Subs-Post.php');
|
Chris@76
|
969 if ($recycle)
|
Chris@76
|
970 updateLastMessages(array($row['id_board'], $modSettings['recycle_board']));
|
Chris@76
|
971 else
|
Chris@76
|
972 updateLastMessages($row['id_board']);
|
Chris@76
|
973
|
Chris@76
|
974 return false;
|
Chris@76
|
975 }
|
Chris@76
|
976
|
Chris@76
|
977 function RestoreTopic()
|
Chris@76
|
978 {
|
Chris@76
|
979 global $context, $smcFunc, $modSettings, $sourcedir;
|
Chris@76
|
980
|
Chris@76
|
981 // Check session.
|
Chris@76
|
982 checkSession('get');
|
Chris@76
|
983
|
Chris@76
|
984 // Is recycled board enabled?
|
Chris@76
|
985 if (empty($modSettings['recycle_enable']))
|
Chris@76
|
986 fatal_lang_error('restored_disabled', 'critical');
|
Chris@76
|
987
|
Chris@76
|
988 // Can we be in here?
|
Chris@76
|
989 isAllowedTo('move_any', $modSettings['recycle_board']);
|
Chris@76
|
990
|
Chris@76
|
991 // We need this file.
|
Chris@76
|
992 require_once($sourcedir . '/MoveTopic.php');
|
Chris@76
|
993
|
Chris@76
|
994 $unfound_messages = array();
|
Chris@76
|
995 $topics_to_restore = array();
|
Chris@76
|
996
|
Chris@76
|
997 // Restoring messages?
|
Chris@76
|
998 if (!empty($_REQUEST['msgs']))
|
Chris@76
|
999 {
|
Chris@76
|
1000 $msgs = explode(',', $_REQUEST['msgs']);
|
Chris@76
|
1001 foreach ($msgs as $k => $msg)
|
Chris@76
|
1002 $msgs[$k] = (int) $msg;
|
Chris@76
|
1003
|
Chris@76
|
1004 // Get the id_previous_board and id_previous_topic.
|
Chris@76
|
1005 $request = $smcFunc['db_query']('', '
|
Chris@76
|
1006 SELECT m.id_topic, m.id_msg, m.id_board, m.subject, m.id_member, t.id_previous_board, t.id_previous_topic,
|
Chris@76
|
1007 t.id_first_msg, b.count_posts, IFNULL(pt.id_board, 0) AS possible_prev_board
|
Chris@76
|
1008 FROM {db_prefix}messages AS m
|
Chris@76
|
1009 INNER JOIN {db_prefix}topics AS t ON (t.id_topic = m.id_topic)
|
Chris@76
|
1010 INNER JOIN {db_prefix}boards AS b ON (b.id_board = m.id_board)
|
Chris@76
|
1011 LEFT JOIN {db_prefix}topics AS pt ON (pt.id_topic = t.id_previous_topic)
|
Chris@76
|
1012 WHERE m.id_msg IN ({array_int:messages})',
|
Chris@76
|
1013 array(
|
Chris@76
|
1014 'messages' => $msgs,
|
Chris@76
|
1015 )
|
Chris@76
|
1016 );
|
Chris@76
|
1017
|
Chris@76
|
1018 $actioned_messages = array();
|
Chris@76
|
1019 $previous_topics = array();
|
Chris@76
|
1020 while ($row = $smcFunc['db_fetch_assoc']($request))
|
Chris@76
|
1021 {
|
Chris@76
|
1022 // Restoring the first post means topic.
|
Chris@76
|
1023 if ($row['id_msg'] == $row['id_first_msg'] && $row['id_previous_topic'] == $row['id_topic'])
|
Chris@76
|
1024 {
|
Chris@76
|
1025 $topics_to_restore[] = $row['id_topic'];
|
Chris@76
|
1026 continue;
|
Chris@76
|
1027 }
|
Chris@76
|
1028 // Don't know where it's going?
|
Chris@76
|
1029 if (empty($row['id_previous_topic']))
|
Chris@76
|
1030 {
|
Chris@76
|
1031 $unfound_messages[$row['id_msg']] = $row['subject'];
|
Chris@76
|
1032 continue;
|
Chris@76
|
1033 }
|
Chris@76
|
1034
|
Chris@76
|
1035 $previous_topics[] = $row['id_previous_topic'];
|
Chris@76
|
1036 if (empty($actioned_messages[$row['id_previous_topic']]))
|
Chris@76
|
1037 $actioned_messages[$row['id_previous_topic']] = array(
|
Chris@76
|
1038 'msgs' => array(),
|
Chris@76
|
1039 'count_posts' => $row['count_posts'],
|
Chris@76
|
1040 'subject' => $row['subject'],
|
Chris@76
|
1041 'previous_board' => $row['id_previous_board'],
|
Chris@76
|
1042 'possible_prev_board' => $row['possible_prev_board'],
|
Chris@76
|
1043 'current_topic' => $row['id_topic'],
|
Chris@76
|
1044 'current_board' => $row['id_board'],
|
Chris@76
|
1045 'members' => array(),
|
Chris@76
|
1046 );
|
Chris@76
|
1047
|
Chris@76
|
1048 $actioned_messages[$row['id_previous_topic']]['msgs'][$row['id_msg']] = $row['subject'];
|
Chris@76
|
1049 if ($row['id_member'])
|
Chris@76
|
1050 $actioned_messages[$row['id_previous_topic']]['members'][] = $row['id_member'];
|
Chris@76
|
1051 }
|
Chris@76
|
1052 $smcFunc['db_free_result']($request);
|
Chris@76
|
1053
|
Chris@76
|
1054 // Check for topics we are going to fully restore.
|
Chris@76
|
1055 foreach ($actioned_messages as $topic => $data)
|
Chris@76
|
1056 if (in_array($topic, $topics_to_restore))
|
Chris@76
|
1057 unset($actioned_messages[$topic]);
|
Chris@76
|
1058
|
Chris@76
|
1059 // Load any previous topics to check they exist.
|
Chris@76
|
1060 if (!empty($previous_topics))
|
Chris@76
|
1061 {
|
Chris@76
|
1062 $request = $smcFunc['db_query']('', '
|
Chris@76
|
1063 SELECT t.id_topic, t.id_board, m.subject
|
Chris@76
|
1064 FROM {db_prefix}topics AS t
|
Chris@76
|
1065 INNER JOIN {db_prefix}messages AS m ON (m.id_msg = t.id_first_msg)
|
Chris@76
|
1066 WHERE t.id_topic IN ({array_int:previous_topics})',
|
Chris@76
|
1067 array(
|
Chris@76
|
1068 'previous_topics' => $previous_topics,
|
Chris@76
|
1069 )
|
Chris@76
|
1070 );
|
Chris@76
|
1071 $previous_topics = array();
|
Chris@76
|
1072 while ($row = $smcFunc['db_fetch_assoc']($request))
|
Chris@76
|
1073 $previous_topics[$row['id_topic']] = array(
|
Chris@76
|
1074 'board' => $row['id_board'],
|
Chris@76
|
1075 'subject' => $row['subject'],
|
Chris@76
|
1076 );
|
Chris@76
|
1077 $smcFunc['db_free_result']($request);
|
Chris@76
|
1078 }
|
Chris@76
|
1079
|
Chris@76
|
1080 // Restore each topic.
|
Chris@76
|
1081 $messages = array();
|
Chris@76
|
1082 foreach ($actioned_messages as $topic => $data)
|
Chris@76
|
1083 {
|
Chris@76
|
1084 // If we have topics we are going to restore the whole lot ignore them.
|
Chris@76
|
1085 if (in_array($topic, $topics_to_restore))
|
Chris@76
|
1086 {
|
Chris@76
|
1087 unset($actioned_messages[$topic]);
|
Chris@76
|
1088 continue;
|
Chris@76
|
1089 }
|
Chris@76
|
1090
|
Chris@76
|
1091 // Move the posts back then!
|
Chris@76
|
1092 if (isset($previous_topics[$topic]))
|
Chris@76
|
1093 {
|
Chris@76
|
1094 mergePosts(array_keys($data['msgs']), $data['current_topic'], $topic);
|
Chris@76
|
1095 // Log em.
|
Chris@76
|
1096 logAction('restore_posts', array('topic' => $topic, 'subject' => $previous_topics[$topic]['subject'], 'board' => empty($data['previous_board']) ? $data['possible_prev_board'] : $data['previous_board']));
|
Chris@76
|
1097 $messages = array_merge(array_keys($data['msgs']), $messages);
|
Chris@76
|
1098 }
|
Chris@76
|
1099 else
|
Chris@76
|
1100 {
|
Chris@76
|
1101 foreach ($data['msgs'] as $msg)
|
Chris@76
|
1102 $unfound_messages[$msg['id']] = $msg['subject'];
|
Chris@76
|
1103 }
|
Chris@76
|
1104 }
|
Chris@76
|
1105
|
Chris@76
|
1106 // Put the icons back.
|
Chris@76
|
1107 if (!empty($messages))
|
Chris@76
|
1108 $smcFunc['db_query']('', '
|
Chris@76
|
1109 UPDATE {db_prefix}messages
|
Chris@76
|
1110 SET icon = {string:icon}
|
Chris@76
|
1111 WHERE id_msg IN ({array_int:messages})',
|
Chris@76
|
1112 array(
|
Chris@76
|
1113 'icon' => 'xx',
|
Chris@76
|
1114 'messages' => $messages,
|
Chris@76
|
1115 )
|
Chris@76
|
1116 );
|
Chris@76
|
1117 }
|
Chris@76
|
1118
|
Chris@76
|
1119 // Now any topics?
|
Chris@76
|
1120 if (!empty($_REQUEST['topics']))
|
Chris@76
|
1121 {
|
Chris@76
|
1122 $topics = explode(',', $_REQUEST['topics']);
|
Chris@76
|
1123 foreach ($topics as $key => $id)
|
Chris@76
|
1124 $topics_to_restore[] = (int) $id;
|
Chris@76
|
1125 }
|
Chris@76
|
1126
|
Chris@76
|
1127 if (!empty($topics_to_restore))
|
Chris@76
|
1128 {
|
Chris@76
|
1129 // Lets get the data for these topics.
|
Chris@76
|
1130 $request = $smcFunc['db_query']('', '
|
Chris@76
|
1131 SELECT t.id_topic, t.id_previous_board, t.id_board, t.id_first_msg, m.subject
|
Chris@76
|
1132 FROM {db_prefix}topics AS t
|
Chris@76
|
1133 INNER JOIN {db_prefix}messages AS m ON (m.id_msg = t.id_first_msg)
|
Chris@76
|
1134 WHERE t.id_topic IN ({array_int:topics})',
|
Chris@76
|
1135 array(
|
Chris@76
|
1136 'topics' => $topics_to_restore,
|
Chris@76
|
1137 )
|
Chris@76
|
1138 );
|
Chris@76
|
1139 while ($row = $smcFunc['db_fetch_assoc']($request))
|
Chris@76
|
1140 {
|
Chris@76
|
1141 // We can only restore if the previous board is set.
|
Chris@76
|
1142 if (empty($row['id_previous_board']))
|
Chris@76
|
1143 {
|
Chris@76
|
1144 $unfound_messages[$row['id_first_msg']] = $row['subject'];
|
Chris@76
|
1145 continue;
|
Chris@76
|
1146 }
|
Chris@76
|
1147
|
Chris@76
|
1148 // Ok we got here so me move them from here to there.
|
Chris@76
|
1149 moveTopics($row['id_topic'], $row['id_previous_board']);
|
Chris@76
|
1150
|
Chris@76
|
1151 // Lets remove the recycled icon.
|
Chris@76
|
1152 $smcFunc['db_query']('', '
|
Chris@76
|
1153 UPDATE {db_prefix}messages
|
Chris@76
|
1154 SET icon = {string:icon}
|
Chris@76
|
1155 WHERE id_topic = {int:id_topic}',
|
Chris@76
|
1156 array(
|
Chris@76
|
1157 'icon' => 'xx',
|
Chris@76
|
1158 'id_topic' => $row['id_topic'],
|
Chris@76
|
1159 )
|
Chris@76
|
1160 );
|
Chris@76
|
1161
|
Chris@76
|
1162 // Lets see if the board that we are returning to has post count enabled.
|
Chris@76
|
1163 $request2 = $smcFunc['db_query']('', '
|
Chris@76
|
1164 SELECT count_posts
|
Chris@76
|
1165 FROM {db_prefix}boards
|
Chris@76
|
1166 WHERE id_board = {int:board}',
|
Chris@76
|
1167 array(
|
Chris@76
|
1168 'board' => $row['id_previous_board'],
|
Chris@76
|
1169 )
|
Chris@76
|
1170 );
|
Chris@76
|
1171 list ($count_posts) = $smcFunc['db_fetch_row']($request2);
|
Chris@76
|
1172 $smcFunc['db_free_result']($request2);
|
Chris@76
|
1173
|
Chris@76
|
1174 if (empty($count_posts))
|
Chris@76
|
1175 {
|
Chris@76
|
1176 // Lets get the members that need their post count restored.
|
Chris@76
|
1177 $request2 = $smcFunc['db_query']('', '
|
Chris@76
|
1178 SELECT id_member, COUNT(id_msg) AS post_count
|
Chris@76
|
1179 FROM {db_prefix}messages
|
Chris@76
|
1180 WHERE id_topic = {int:topic}
|
Chris@76
|
1181 AND approved = {int:is_approved}
|
Chris@76
|
1182 GROUP BY id_member',
|
Chris@76
|
1183 array(
|
Chris@76
|
1184 'topic' => $row['id_topic'],
|
Chris@76
|
1185 'is_approved' => 1,
|
Chris@76
|
1186 )
|
Chris@76
|
1187 );
|
Chris@76
|
1188
|
Chris@76
|
1189 while ($member = $smcFunc['db_fetch_assoc']($request2))
|
Chris@76
|
1190 updateMemberData($member['id_member'], array('posts' => 'posts + ' . $member['post_count']));
|
Chris@76
|
1191 $smcFunc['db_free_result']($request2);
|
Chris@76
|
1192 }
|
Chris@76
|
1193
|
Chris@76
|
1194 // Log it.
|
Chris@76
|
1195 logAction('restore_topic', array('topic' => $row['id_topic'], 'board' => $row['id_board'], 'board_to' => $row['id_previous_board']));
|
Chris@76
|
1196 }
|
Chris@76
|
1197 $smcFunc['db_free_result']($request);
|
Chris@76
|
1198 }
|
Chris@76
|
1199
|
Chris@76
|
1200 // Didn't find some things?
|
Chris@76
|
1201 if (!empty($unfound_messages))
|
Chris@76
|
1202 fatal_lang_error('restore_not_found', false, array(implode('<br />', $unfound_messages)));
|
Chris@76
|
1203
|
Chris@76
|
1204 // Just send them to the index if they get here.
|
Chris@76
|
1205 redirectexit();
|
Chris@76
|
1206 }
|
Chris@76
|
1207
|
Chris@76
|
1208 // Take a load of messages from one place and stick them in a topic.
|
Chris@76
|
1209 function mergePosts($msgs = array(), $from_topic, $target_topic)
|
Chris@76
|
1210 {
|
Chris@76
|
1211 global $context, $smcFunc, $modSettings, $sourcedir;
|
Chris@76
|
1212
|
Chris@76
|
1213 //!!! This really needs to be rewritten to take a load of messages from ANY topic, it's also inefficient.
|
Chris@76
|
1214
|
Chris@76
|
1215 // Is it an array?
|
Chris@76
|
1216 if (!is_array($msgs))
|
Chris@76
|
1217 $msgs = array($msgs);
|
Chris@76
|
1218
|
Chris@76
|
1219 // Lets make sure they are int.
|
Chris@76
|
1220 foreach ($msgs as $key => $msg)
|
Chris@76
|
1221 $msgs[$key] = (int) $msg;
|
Chris@76
|
1222
|
Chris@76
|
1223 // Get the source information.
|
Chris@76
|
1224 $request = $smcFunc['db_query']('', '
|
Chris@76
|
1225 SELECT t.id_board, t.id_first_msg, t.num_replies, t.unapproved_posts
|
Chris@76
|
1226 FROM {db_prefix}topics AS t
|
Chris@76
|
1227 INNER JOIN {db_prefix}boards AS b ON (b.id_board = t.id_board)
|
Chris@76
|
1228 WHERE t.id_topic = {int:from_topic}',
|
Chris@76
|
1229 array(
|
Chris@76
|
1230 'from_topic' => $from_topic,
|
Chris@76
|
1231 )
|
Chris@76
|
1232 );
|
Chris@76
|
1233 list ($from_board, $from_first_msg, $from_replies, $from_unapproved_posts) = $smcFunc['db_fetch_row']($request);
|
Chris@76
|
1234 $smcFunc['db_free_result']($request);
|
Chris@76
|
1235
|
Chris@76
|
1236 // Get some target topic and board stats.
|
Chris@76
|
1237 $request = $smcFunc['db_query']('', '
|
Chris@76
|
1238 SELECT t.id_board, t.id_first_msg, t.num_replies, t.unapproved_posts, b.count_posts
|
Chris@76
|
1239 FROM {db_prefix}topics AS t
|
Chris@76
|
1240 INNER JOIN {db_prefix}boards AS b ON (b.id_board = t.id_board)
|
Chris@76
|
1241 WHERE t.id_topic = {int:target_topic}',
|
Chris@76
|
1242 array(
|
Chris@76
|
1243 'target_topic' => $target_topic,
|
Chris@76
|
1244 )
|
Chris@76
|
1245 );
|
Chris@76
|
1246 list ($target_board, $target_first_msg, $target_replies, $target_unapproved_posts, $count_posts) = $smcFunc['db_fetch_row']($request);
|
Chris@76
|
1247 $smcFunc['db_free_result']($request);
|
Chris@76
|
1248
|
Chris@76
|
1249 // Lets see if the board that we are returning to has post count enabled.
|
Chris@76
|
1250 if (empty($count_posts))
|
Chris@76
|
1251 {
|
Chris@76
|
1252 // Lets get the members that need their post count restored.
|
Chris@76
|
1253 $request = $smcFunc['db_query']('', '
|
Chris@76
|
1254 SELECT id_member
|
Chris@76
|
1255 FROM {db_prefix}messages
|
Chris@76
|
1256 WHERE id_msg IN ({array_int:messages})
|
Chris@76
|
1257 AND approved = {int:is_approved}',
|
Chris@76
|
1258 array(
|
Chris@76
|
1259 'messages' => $msgs,
|
Chris@76
|
1260 'is_approved' => 1,
|
Chris@76
|
1261 )
|
Chris@76
|
1262 );
|
Chris@76
|
1263
|
Chris@76
|
1264 while ($row = $smcFunc['db_fetch_assoc']($request))
|
Chris@76
|
1265 updateMemberData($row['id_member'], array('posts' => '+'));
|
Chris@76
|
1266 }
|
Chris@76
|
1267
|
Chris@76
|
1268 // Time to move the messages.
|
Chris@76
|
1269 $smcFunc['db_query']('', '
|
Chris@76
|
1270 UPDATE {db_prefix}messages
|
Chris@76
|
1271 SET
|
Chris@76
|
1272 id_topic = {int:target_topic},
|
Chris@76
|
1273 id_board = {int:target_board},
|
Chris@76
|
1274 icon = {string:icon}
|
Chris@76
|
1275 WHERE id_msg IN({array_int:msgs})',
|
Chris@76
|
1276 array(
|
Chris@76
|
1277 'target_topic' => $target_topic,
|
Chris@76
|
1278 'target_board' => $target_board,
|
Chris@76
|
1279 'icon' => $target_board == $modSettings['recycle_board'] ? 'recycled' : 'xx',
|
Chris@76
|
1280 'msgs' => $msgs,
|
Chris@76
|
1281 )
|
Chris@76
|
1282 );
|
Chris@76
|
1283
|
Chris@76
|
1284 // Fix the id_first_msg and id_last_msg for the target topic.
|
Chris@76
|
1285 $target_topic_data = array(
|
Chris@76
|
1286 'num_replies' => 0,
|
Chris@76
|
1287 'unapproved_posts' => 0,
|
Chris@76
|
1288 'id_first_msg' => 9999999999,
|
Chris@76
|
1289 );
|
Chris@76
|
1290 $request = $smcFunc['db_query']('', '
|
Chris@76
|
1291 SELECT MIN(id_msg) AS id_first_msg, MAX(id_msg) AS id_last_msg, COUNT(*) AS message_count, approved
|
Chris@76
|
1292 FROM {db_prefix}messages
|
Chris@76
|
1293 WHERE id_topic = {int:target_topic}
|
Chris@76
|
1294 GROUP BY id_topic, approved
|
Chris@76
|
1295 ORDER BY approved ASC
|
Chris@76
|
1296 LIMIT 2',
|
Chris@76
|
1297 array(
|
Chris@76
|
1298 'target_topic' => $target_topic,
|
Chris@76
|
1299 )
|
Chris@76
|
1300 );
|
Chris@76
|
1301 while ($row = $smcFunc['db_fetch_assoc']($request))
|
Chris@76
|
1302 {
|
Chris@76
|
1303 if ($row['id_first_msg'] < $target_topic_data['id_first_msg'])
|
Chris@76
|
1304 $target_topic_data['id_first_msg'] = $row['id_first_msg'];
|
Chris@76
|
1305 $target_topic_data['id_last_msg'] = $row['id_last_msg'];
|
Chris@76
|
1306 if (!$row['approved'])
|
Chris@76
|
1307 $target_topic_data['unapproved_posts'] = $row['message_count'];
|
Chris@76
|
1308 else
|
Chris@76
|
1309 $target_topic_data['num_replies'] = max(0, $row['message_count'] - 1);
|
Chris@76
|
1310 }
|
Chris@76
|
1311 $smcFunc['db_free_result']($request);
|
Chris@76
|
1312
|
Chris@76
|
1313 // We have a new post count for the board.
|
Chris@76
|
1314 $smcFunc['db_query']('', '
|
Chris@76
|
1315 UPDATE {db_prefix}boards
|
Chris@76
|
1316 SET
|
Chris@76
|
1317 num_posts = num_posts + {int:diff_replies},
|
Chris@76
|
1318 unapproved_posts = unapproved_posts + {int:diff_unapproved_posts}
|
Chris@76
|
1319 WHERE id_board = {int:target_board}',
|
Chris@76
|
1320 array(
|
Chris@76
|
1321 'diff_replies' => $target_topic_data['num_replies'] - $target_replies, // Lets keep in mind that the first message in a topic counts towards num_replies in a board.
|
Chris@76
|
1322 'diff_unapproved_posts' => $target_topic_data['unapproved_posts'] - $target_unapproved_posts,
|
Chris@76
|
1323 'target_board' => $target_board,
|
Chris@76
|
1324 )
|
Chris@76
|
1325 );
|
Chris@76
|
1326
|
Chris@76
|
1327 // In some cases we merged the only post in a topic so the topic data is left behind in the topic table.
|
Chris@76
|
1328 $request = $smcFunc['db_query']('', '
|
Chris@76
|
1329 SELECT id_topic
|
Chris@76
|
1330 FROM {db_prefix}messages
|
Chris@76
|
1331 WHERE id_topic = {int:from_topic}',
|
Chris@76
|
1332 array(
|
Chris@76
|
1333 'from_topic' => $from_topic,
|
Chris@76
|
1334 )
|
Chris@76
|
1335 );
|
Chris@76
|
1336
|
Chris@76
|
1337 // Remove the topic if it doesn't have any messages.
|
Chris@76
|
1338 $topic_exists = true;
|
Chris@76
|
1339 if ($smcFunc['db_num_rows']($request) == 0)
|
Chris@76
|
1340 {
|
Chris@76
|
1341 removeTopics($from_topic, false, true);
|
Chris@76
|
1342 $topic_exists = false;
|
Chris@76
|
1343 }
|
Chris@76
|
1344 $smcFunc['db_free_result']($request);
|
Chris@76
|
1345
|
Chris@76
|
1346 // Recycled topic.
|
Chris@76
|
1347 if ($topic_exists == true)
|
Chris@76
|
1348 {
|
Chris@76
|
1349 // Fix the id_first_msg and id_last_msg for the source topic.
|
Chris@76
|
1350 $source_topic_data = array(
|
Chris@76
|
1351 'num_replies' => 0,
|
Chris@76
|
1352 'unapproved_posts' => 0,
|
Chris@76
|
1353 'id_first_msg' => 9999999999,
|
Chris@76
|
1354 );
|
Chris@76
|
1355 $request = $smcFunc['db_query']('', '
|
Chris@76
|
1356 SELECT MIN(id_msg) AS id_first_msg, MAX(id_msg) AS id_last_msg, COUNT(*) AS message_count, approved, subject
|
Chris@76
|
1357 FROM {db_prefix}messages
|
Chris@76
|
1358 WHERE id_topic = {int:from_topic}
|
Chris@76
|
1359 GROUP BY id_topic, approved
|
Chris@76
|
1360 ORDER BY approved ASC
|
Chris@76
|
1361 LIMIT 2',
|
Chris@76
|
1362 array(
|
Chris@76
|
1363 'from_topic' => $from_topic,
|
Chris@76
|
1364 )
|
Chris@76
|
1365 );
|
Chris@76
|
1366 while ($row = $smcFunc['db_fetch_assoc']($request))
|
Chris@76
|
1367 {
|
Chris@76
|
1368 if ($row['id_first_msg'] < $source_topic_data['id_first_msg'])
|
Chris@76
|
1369 $source_topic_data['id_first_msg'] = $row['id_first_msg'];
|
Chris@76
|
1370 $source_topic_data['id_last_msg'] = $row['id_last_msg'];
|
Chris@76
|
1371 if (!$row['approved'])
|
Chris@76
|
1372 $source_topic_data['unapproved_posts'] = $row['message_count'];
|
Chris@76
|
1373 else
|
Chris@76
|
1374 $source_topic_data['num_replies'] = max(0, $row['message_count'] - 1);
|
Chris@76
|
1375 }
|
Chris@76
|
1376 $smcFunc['db_free_result']($request);
|
Chris@76
|
1377
|
Chris@76
|
1378 // Update the topic details for the source topic.
|
Chris@76
|
1379 $smcFunc['db_query']('', '
|
Chris@76
|
1380 UPDATE {db_prefix}topics
|
Chris@76
|
1381 SET
|
Chris@76
|
1382 id_first_msg = {int:id_first_msg},
|
Chris@76
|
1383 id_last_msg = {int:id_last_msg},
|
Chris@76
|
1384 num_replies = {int:num_replies},
|
Chris@76
|
1385 unapproved_posts = {int:unapproved_posts}
|
Chris@76
|
1386 WHERE id_topic = {int:from_topic}',
|
Chris@76
|
1387 array(
|
Chris@76
|
1388 'id_first_msg' => $source_topic_data['id_first_msg'],
|
Chris@76
|
1389 'id_last_msg' => $source_topic_data['id_last_msg'],
|
Chris@76
|
1390 'num_replies' => $source_topic_data['num_replies'],
|
Chris@76
|
1391 'unapproved_posts' => $source_topic_data['unapproved_posts'],
|
Chris@76
|
1392 'from_topic' => $from_topic,
|
Chris@76
|
1393 )
|
Chris@76
|
1394 );
|
Chris@76
|
1395
|
Chris@76
|
1396 // We have a new post count for the source board.
|
Chris@76
|
1397 $smcFunc['db_query']('', '
|
Chris@76
|
1398 UPDATE {db_prefix}boards
|
Chris@76
|
1399 SET
|
Chris@76
|
1400 num_posts = num_posts + {int:diff_replies},
|
Chris@76
|
1401 unapproved_posts = unapproved_posts + {int:diff_unapproved_posts}
|
Chris@76
|
1402 WHERE id_board = {int:from_board}',
|
Chris@76
|
1403 array(
|
Chris@76
|
1404 'diff_replies' => $source_topic_data['num_replies'] - $from_replies, // Lets keep in mind that the first message in a topic counts towards num_replies in a board.
|
Chris@76
|
1405 'diff_unapproved_posts' => $source_topic_data['unapproved_posts'] - $from_unapproved_posts,
|
Chris@76
|
1406 'from_board' => $from_board,
|
Chris@76
|
1407 )
|
Chris@76
|
1408 );
|
Chris@76
|
1409 }
|
Chris@76
|
1410
|
Chris@76
|
1411 // Finally get around to updating the destination topic, now all indexes etc on the source are fixed.
|
Chris@76
|
1412 $smcFunc['db_query']('', '
|
Chris@76
|
1413 UPDATE {db_prefix}topics
|
Chris@76
|
1414 SET
|
Chris@76
|
1415 id_first_msg = {int:id_first_msg},
|
Chris@76
|
1416 id_last_msg = {int:id_last_msg},
|
Chris@76
|
1417 num_replies = {int:num_replies},
|
Chris@76
|
1418 unapproved_posts = {int:unapproved_posts}
|
Chris@76
|
1419 WHERE id_topic = {int:target_topic}',
|
Chris@76
|
1420 array(
|
Chris@76
|
1421 'id_first_msg' => $target_topic_data['id_first_msg'],
|
Chris@76
|
1422 'id_last_msg' => $target_topic_data['id_last_msg'],
|
Chris@76
|
1423 'num_replies' => $target_topic_data['num_replies'],
|
Chris@76
|
1424 'unapproved_posts' => $target_topic_data['unapproved_posts'],
|
Chris@76
|
1425 'target_topic' => $target_topic,
|
Chris@76
|
1426 )
|
Chris@76
|
1427 );
|
Chris@76
|
1428
|
Chris@76
|
1429 // Need it to update some stats.
|
Chris@76
|
1430 require_once($sourcedir . '/Subs-Post.php');
|
Chris@76
|
1431
|
Chris@76
|
1432 // Update stats.
|
Chris@76
|
1433 updateStats('topic');
|
Chris@76
|
1434 updateStats('message');
|
Chris@76
|
1435
|
Chris@76
|
1436 // Subject cache?
|
Chris@76
|
1437 $cache_updates = array();
|
Chris@76
|
1438 if ($target_first_msg != $target_topic_data['id_first_msg'])
|
Chris@76
|
1439 $cache_updates[] = $target_topic_data['id_first_msg'];
|
Chris@76
|
1440 if (!empty($source_topic_data['id_first_msg']) && $from_first_msg != $source_topic_data['id_first_msg'])
|
Chris@76
|
1441 $cache_updates[] = $source_topic_data['id_first_msg'];
|
Chris@76
|
1442
|
Chris@76
|
1443 if (!empty($cache_updates))
|
Chris@76
|
1444 {
|
Chris@76
|
1445 $request = $smcFunc['db_query']('', '
|
Chris@76
|
1446 SELECT id_topic, subject
|
Chris@76
|
1447 FROM {db_prefix}messages
|
Chris@76
|
1448 WHERE id_msg IN ({array_int:first_messages})',
|
Chris@76
|
1449 array(
|
Chris@76
|
1450 'first_messages' => $cache_updates,
|
Chris@76
|
1451 )
|
Chris@76
|
1452 );
|
Chris@76
|
1453 while ($row = $smcFunc['db_fetch_assoc']($request))
|
Chris@76
|
1454 updateStats('subject', $row['id_topic'], $row['subject']);
|
Chris@76
|
1455 $smcFunc['db_free_result']($request);
|
Chris@76
|
1456 }
|
Chris@76
|
1457
|
Chris@76
|
1458 updateLastMessages(array($from_board, $target_board));
|
Chris@76
|
1459 }
|
Chris@76
|
1460
|
Chris@76
|
1461 ?> |