PageRenderTime 65ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

/forum/includes/functions_admin.php

https://github.com/GreyTeardrop/socionicasys-forum
PHP | 3419 lines | 2519 code | 538 blank | 362 comment | 421 complexity | 985b8e5d2cbe4fc972e89fdb9421c884 MD5 | raw file
Possible License(s): AGPL-1.0, LGPL-3.0, MPL-2.0-no-copyleft-exception

Large files files are truncated, but you can click here to view the full file

  1. <?php
  2. /**
  3. *
  4. * @package acp
  5. * @version $Id$
  6. * @copyright (c) 2005 phpBB Group
  7. * @license http://opensource.org/licenses/gpl-license.php GNU Public License
  8. *
  9. */
  10. /**
  11. * @ignore
  12. */
  13. if (!defined('IN_PHPBB'))
  14. {
  15. exit;
  16. }
  17. /**
  18. * Recalculate Nested Sets
  19. *
  20. * @param int $new_id first left_id (should start with 1)
  21. * @param string $pkey primary key-column (containing the id for the parent_id of the children)
  22. * @param string $table constant or fullname of the table
  23. * @param int $parent_id parent_id of the current set (default = 0)
  24. * @param array $where contains strings to compare closer on the where statement (additional)
  25. *
  26. * @author EXreaction
  27. */
  28. function recalc_nested_sets(&$new_id, $pkey, $table, $parent_id = 0, $where = array())
  29. {
  30. global $db;
  31. $sql = 'SELECT *
  32. FROM ' . $table . '
  33. WHERE parent_id = ' . (int) $parent_id .
  34. ((!empty($where)) ? ' AND ' . implode(' AND ', $where) : '') . '
  35. ORDER BY left_id ASC';
  36. $result = $db->sql_query($sql);
  37. while ($row = $db->sql_fetchrow($result))
  38. {
  39. // First we update the left_id for this module
  40. if ($row['left_id'] != $new_id)
  41. {
  42. $db->sql_query('UPDATE ' . $table . ' SET ' . $db->sql_build_array('UPDATE', array('left_id' => $new_id)) . " WHERE $pkey = {$row[$pkey]}");
  43. }
  44. $new_id++;
  45. // Then we go through any children and update their left/right id's
  46. recalc_nested_sets($new_id, $pkey, $table, $row[$pkey], $where);
  47. // Then we come back and update the right_id for this module
  48. if ($row['right_id'] != $new_id)
  49. {
  50. $db->sql_query('UPDATE ' . $table . ' SET ' . $db->sql_build_array('UPDATE', array('right_id' => $new_id)) . " WHERE $pkey = {$row[$pkey]}");
  51. }
  52. $new_id++;
  53. }
  54. $db->sql_freeresult($result);
  55. }
  56. /**
  57. * Simple version of jumpbox, just lists authed forums
  58. */
  59. function make_forum_select($select_id = false, $ignore_id = false, $ignore_acl = false, $ignore_nonpost = false, $ignore_emptycat = true, $only_acl_post = false, $return_array = false)
  60. {
  61. global $db, $user, $auth;
  62. // This query is identical to the jumpbox one
  63. $sql = 'SELECT forum_id, forum_name, parent_id, forum_type, forum_flags, forum_options, left_id, right_id
  64. FROM ' . FORUMS_TABLE . '
  65. ORDER BY left_id ASC';
  66. $result = $db->sql_query($sql, 600);
  67. $right = 0;
  68. $padding_store = array('0' => '');
  69. $padding = '';
  70. $forum_list = ($return_array) ? array() : '';
  71. // Sometimes it could happen that forums will be displayed here not be displayed within the index page
  72. // This is the result of forums not displayed at index, having list permissions and a parent of a forum with no permissions.
  73. // If this happens, the padding could be "broken"
  74. while ($row = $db->sql_fetchrow($result))
  75. {
  76. if ($row['left_id'] < $right)
  77. {
  78. $padding .= '&nbsp; &nbsp;';
  79. $padding_store[$row['parent_id']] = $padding;
  80. }
  81. else if ($row['left_id'] > $right + 1)
  82. {
  83. $padding = (isset($padding_store[$row['parent_id']])) ? $padding_store[$row['parent_id']] : '';
  84. }
  85. $right = $row['right_id'];
  86. $disabled = false;
  87. if (!$ignore_acl && $auth->acl_gets(array('f_list', 'a_forum', 'a_forumadd', 'a_forumdel'), $row['forum_id']))
  88. {
  89. if ($only_acl_post && !$auth->acl_get('f_post', $row['forum_id']) || (!$auth->acl_get('m_approve', $row['forum_id']) && !$auth->acl_get('f_noapprove', $row['forum_id'])))
  90. {
  91. $disabled = true;
  92. }
  93. }
  94. else if (!$ignore_acl)
  95. {
  96. continue;
  97. }
  98. if (
  99. ((is_array($ignore_id) && in_array($row['forum_id'], $ignore_id)) || $row['forum_id'] == $ignore_id)
  100. ||
  101. // Non-postable forum with no subforums, don't display
  102. ($row['forum_type'] == FORUM_CAT && ($row['left_id'] + 1 == $row['right_id']) && $ignore_emptycat)
  103. ||
  104. ($row['forum_type'] != FORUM_POST && $ignore_nonpost)
  105. )
  106. {
  107. $disabled = true;
  108. }
  109. if ($return_array)
  110. {
  111. // Include some more information...
  112. $selected = (is_array($select_id)) ? ((in_array($row['forum_id'], $select_id)) ? true : false) : (($row['forum_id'] == $select_id) ? true : false);
  113. $forum_list[$row['forum_id']] = array_merge(array('padding' => $padding, 'selected' => ($selected && !$disabled), 'disabled' => $disabled), $row);
  114. }
  115. else
  116. {
  117. $selected = (is_array($select_id)) ? ((in_array($row['forum_id'], $select_id)) ? ' selected="selected"' : '') : (($row['forum_id'] == $select_id) ? ' selected="selected"' : '');
  118. $forum_list .= '<option value="' . $row['forum_id'] . '"' . (($disabled) ? ' disabled="disabled" class="disabled-option"' : $selected) . '>' . $padding . $row['forum_name'] . '</option>';
  119. }
  120. }
  121. $db->sql_freeresult($result);
  122. unset($padding_store);
  123. return $forum_list;
  124. }
  125. /**
  126. * Generate size select options
  127. */
  128. function size_select_options($size_compare)
  129. {
  130. global $user;
  131. $size_types_text = array($user->lang['BYTES'], $user->lang['KIB'], $user->lang['MIB']);
  132. $size_types = array('b', 'kb', 'mb');
  133. $s_size_options = '';
  134. for ($i = 0, $size = sizeof($size_types_text); $i < $size; $i++)
  135. {
  136. $selected = ($size_compare == $size_types[$i]) ? ' selected="selected"' : '';
  137. $s_size_options .= '<option value="' . $size_types[$i] . '"' . $selected . '>' . $size_types_text[$i] . '</option>';
  138. }
  139. return $s_size_options;
  140. }
  141. /**
  142. * Generate list of groups (option fields without select)
  143. *
  144. * @param int $group_id The default group id to mark as selected
  145. * @param array $exclude_ids The group ids to exclude from the list, false (default) if you whish to exclude no id
  146. * @param int $manage_founder If set to false (default) all groups are returned, if 0 only those groups returned not being managed by founders only, if 1 only those groups returned managed by founders only.
  147. *
  148. * @return string The list of options.
  149. */
  150. function group_select_options($group_id, $exclude_ids = false, $manage_founder = false)
  151. {
  152. global $db, $user, $config;
  153. $exclude_sql = ($exclude_ids !== false && sizeof($exclude_ids)) ? 'WHERE ' . $db->sql_in_set('group_id', array_map('intval', $exclude_ids), true) : '';
  154. $sql_and = (!$config['coppa_enable']) ? (($exclude_sql) ? ' AND ' : ' WHERE ') . "group_name <> 'REGISTERED_COPPA'" : '';
  155. $sql_founder = ($manage_founder !== false) ? (($exclude_sql || $sql_and) ? ' AND ' : ' WHERE ') . 'group_founder_manage = ' . (int) $manage_founder : '';
  156. $sql = 'SELECT group_id, group_name, group_type
  157. FROM ' . GROUPS_TABLE . "
  158. $exclude_sql
  159. $sql_and
  160. $sql_founder
  161. ORDER BY group_type DESC, group_name ASC";
  162. $result = $db->sql_query($sql);
  163. $s_group_options = '';
  164. while ($row = $db->sql_fetchrow($result))
  165. {
  166. $selected = ($row['group_id'] == $group_id) ? ' selected="selected"' : '';
  167. $s_group_options .= '<option' . (($row['group_type'] == GROUP_SPECIAL) ? ' class="sep"' : '') . ' value="' . $row['group_id'] . '"' . $selected . '>' . (($row['group_type'] == GROUP_SPECIAL) ? $user->lang['G_' . $row['group_name']] : $row['group_name']) . '</option>';
  168. }
  169. $db->sql_freeresult($result);
  170. return $s_group_options;
  171. }
  172. /**
  173. * Obtain authed forums list
  174. */
  175. function get_forum_list($acl_list = 'f_list', $id_only = true, $postable_only = false, $no_cache = false)
  176. {
  177. global $db, $auth;
  178. static $forum_rows;
  179. if (!isset($forum_rows))
  180. {
  181. // This query is identical to the jumpbox one
  182. $expire_time = ($no_cache) ? 0 : 600;
  183. $sql = 'SELECT forum_id, forum_name, parent_id, forum_type, left_id, right_id
  184. FROM ' . FORUMS_TABLE . '
  185. ORDER BY left_id ASC';
  186. $result = $db->sql_query($sql, $expire_time);
  187. $forum_rows = array();
  188. $right = $padding = 0;
  189. $padding_store = array('0' => 0);
  190. while ($row = $db->sql_fetchrow($result))
  191. {
  192. if ($row['left_id'] < $right)
  193. {
  194. $padding++;
  195. $padding_store[$row['parent_id']] = $padding;
  196. }
  197. else if ($row['left_id'] > $right + 1)
  198. {
  199. // Ok, if the $padding_store for this parent is empty there is something wrong. For now we will skip over it.
  200. // @todo digging deep to find out "how" this can happen.
  201. $padding = (isset($padding_store[$row['parent_id']])) ? $padding_store[$row['parent_id']] : $padding;
  202. }
  203. $right = $row['right_id'];
  204. $row['padding'] = $padding;
  205. $forum_rows[] = $row;
  206. }
  207. $db->sql_freeresult($result);
  208. unset($padding_store);
  209. }
  210. $rowset = array();
  211. foreach ($forum_rows as $row)
  212. {
  213. if ($postable_only && $row['forum_type'] != FORUM_POST)
  214. {
  215. continue;
  216. }
  217. if ($acl_list == '' || ($acl_list != '' && $auth->acl_gets($acl_list, $row['forum_id'])))
  218. {
  219. $rowset[] = ($id_only) ? (int) $row['forum_id'] : $row;
  220. }
  221. }
  222. return $rowset;
  223. }
  224. /**
  225. * Get forum branch
  226. */
  227. function get_forum_branch($forum_id, $type = 'all', $order = 'descending', $include_forum = true)
  228. {
  229. global $db;
  230. switch ($type)
  231. {
  232. case 'parents':
  233. $condition = 'f1.left_id BETWEEN f2.left_id AND f2.right_id';
  234. break;
  235. case 'children':
  236. $condition = 'f2.left_id BETWEEN f1.left_id AND f1.right_id';
  237. break;
  238. default:
  239. $condition = 'f2.left_id BETWEEN f1.left_id AND f1.right_id OR f1.left_id BETWEEN f2.left_id AND f2.right_id';
  240. break;
  241. }
  242. $rows = array();
  243. $sql = 'SELECT f2.*
  244. FROM ' . FORUMS_TABLE . ' f1
  245. LEFT JOIN ' . FORUMS_TABLE . " f2 ON ($condition)
  246. WHERE f1.forum_id = $forum_id
  247. ORDER BY f2.left_id " . (($order == 'descending') ? 'ASC' : 'DESC');
  248. $result = $db->sql_query($sql);
  249. while ($row = $db->sql_fetchrow($result))
  250. {
  251. if (!$include_forum && $row['forum_id'] == $forum_id)
  252. {
  253. continue;
  254. }
  255. $rows[] = $row;
  256. }
  257. $db->sql_freeresult($result);
  258. return $rows;
  259. }
  260. /**
  261. * Copies permissions from one forum to others
  262. *
  263. * @param int $src_forum_id The source forum we want to copy permissions from
  264. * @param array $dest_forum_ids The destination forum(s) we want to copy to
  265. * @param bool $clear_dest_perms True if destination permissions should be deleted
  266. * @param bool $add_log True if log entry should be added
  267. *
  268. * @return bool False on error
  269. *
  270. * @author bantu
  271. */
  272. function copy_forum_permissions($src_forum_id, $dest_forum_ids, $clear_dest_perms = true, $add_log = true)
  273. {
  274. global $db;
  275. // Only one forum id specified
  276. if (!is_array($dest_forum_ids))
  277. {
  278. $dest_forum_ids = array($dest_forum_ids);
  279. }
  280. // Make sure forum ids are integers
  281. $src_forum_id = (int) $src_forum_id;
  282. $dest_forum_ids = array_map('intval', $dest_forum_ids);
  283. // No source forum or no destination forums specified
  284. if (empty($src_forum_id) || empty($dest_forum_ids))
  285. {
  286. return false;
  287. }
  288. // Check if source forum exists
  289. $sql = 'SELECT forum_name
  290. FROM ' . FORUMS_TABLE . '
  291. WHERE forum_id = ' . $src_forum_id;
  292. $result = $db->sql_query($sql);
  293. $src_forum_name = $db->sql_fetchfield('forum_name');
  294. $db->sql_freeresult($result);
  295. // Source forum doesn't exist
  296. if (empty($src_forum_name))
  297. {
  298. return false;
  299. }
  300. // Check if destination forums exists
  301. $sql = 'SELECT forum_id, forum_name
  302. FROM ' . FORUMS_TABLE . '
  303. WHERE ' . $db->sql_in_set('forum_id', $dest_forum_ids);
  304. $result = $db->sql_query($sql);
  305. $dest_forum_ids = $dest_forum_names = array();
  306. while ($row = $db->sql_fetchrow($result))
  307. {
  308. $dest_forum_ids[] = (int) $row['forum_id'];
  309. $dest_forum_names[] = $row['forum_name'];
  310. }
  311. $db->sql_freeresult($result);
  312. // No destination forum exists
  313. if (empty($dest_forum_ids))
  314. {
  315. return false;
  316. }
  317. // From the mysql documentation:
  318. // Prior to MySQL 4.0.14, the target table of the INSERT statement cannot appear
  319. // in the FROM clause of the SELECT part of the query. This limitation is lifted in 4.0.14.
  320. // Due to this we stay on the safe side if we do the insertion "the manual way"
  321. // Rowsets we're going to insert
  322. $users_sql_ary = $groups_sql_ary = array();
  323. // Query acl users table for source forum data
  324. $sql = 'SELECT user_id, auth_option_id, auth_role_id, auth_setting
  325. FROM ' . ACL_USERS_TABLE . '
  326. WHERE forum_id = ' . $src_forum_id;
  327. $result = $db->sql_query($sql);
  328. while ($row = $db->sql_fetchrow($result))
  329. {
  330. $row = array(
  331. 'user_id' => (int) $row['user_id'],
  332. 'auth_option_id' => (int) $row['auth_option_id'],
  333. 'auth_role_id' => (int) $row['auth_role_id'],
  334. 'auth_setting' => (int) $row['auth_setting'],
  335. );
  336. foreach ($dest_forum_ids as $dest_forum_id)
  337. {
  338. $users_sql_ary[] = $row + array('forum_id' => $dest_forum_id);
  339. }
  340. }
  341. $db->sql_freeresult($result);
  342. // Query acl groups table for source forum data
  343. $sql = 'SELECT group_id, auth_option_id, auth_role_id, auth_setting
  344. FROM ' . ACL_GROUPS_TABLE . '
  345. WHERE forum_id = ' . $src_forum_id;
  346. $result = $db->sql_query($sql);
  347. while ($row = $db->sql_fetchrow($result))
  348. {
  349. $row = array(
  350. 'group_id' => (int) $row['group_id'],
  351. 'auth_option_id' => (int) $row['auth_option_id'],
  352. 'auth_role_id' => (int) $row['auth_role_id'],
  353. 'auth_setting' => (int) $row['auth_setting'],
  354. );
  355. foreach ($dest_forum_ids as $dest_forum_id)
  356. {
  357. $groups_sql_ary[] = $row + array('forum_id' => $dest_forum_id);
  358. }
  359. }
  360. $db->sql_freeresult($result);
  361. $db->sql_transaction('begin');
  362. // Clear current permissions of destination forums
  363. if ($clear_dest_perms)
  364. {
  365. $sql = 'DELETE FROM ' . ACL_USERS_TABLE . '
  366. WHERE ' . $db->sql_in_set('forum_id', $dest_forum_ids);
  367. $db->sql_query($sql);
  368. $sql = 'DELETE FROM ' . ACL_GROUPS_TABLE . '
  369. WHERE ' . $db->sql_in_set('forum_id', $dest_forum_ids);
  370. $db->sql_query($sql);
  371. }
  372. $db->sql_multi_insert(ACL_USERS_TABLE, $users_sql_ary);
  373. $db->sql_multi_insert(ACL_GROUPS_TABLE, $groups_sql_ary);
  374. if ($add_log)
  375. {
  376. add_log('admin', 'LOG_FORUM_COPIED_PERMISSIONS', $src_forum_name, implode(', ', $dest_forum_names));
  377. }
  378. $db->sql_transaction('commit');
  379. return true;
  380. }
  381. /**
  382. * Get physical file listing
  383. */
  384. function filelist($rootdir, $dir = '', $type = 'gif|jpg|jpeg|png')
  385. {
  386. $matches = array($dir => array());
  387. // Remove initial / if present
  388. $rootdir = (substr($rootdir, 0, 1) == '/') ? substr($rootdir, 1) : $rootdir;
  389. // Add closing / if not present
  390. $rootdir = ($rootdir && substr($rootdir, -1) != '/') ? $rootdir . '/' : $rootdir;
  391. // Remove initial / if present
  392. $dir = (substr($dir, 0, 1) == '/') ? substr($dir, 1) : $dir;
  393. // Add closing / if not present
  394. $dir = ($dir && substr($dir, -1) != '/') ? $dir . '/' : $dir;
  395. if (!is_dir($rootdir . $dir))
  396. {
  397. return $matches;
  398. }
  399. $dh = @opendir($rootdir . $dir);
  400. if (!$dh)
  401. {
  402. return $matches;
  403. }
  404. while (($fname = readdir($dh)) !== false)
  405. {
  406. if (is_file("$rootdir$dir$fname"))
  407. {
  408. if (filesize("$rootdir$dir$fname") && preg_match('#\.' . $type . '$#i', $fname))
  409. {
  410. $matches[$dir][] = $fname;
  411. }
  412. }
  413. else if ($fname[0] != '.' && is_dir("$rootdir$dir$fname"))
  414. {
  415. $matches += filelist($rootdir, "$dir$fname", $type);
  416. }
  417. }
  418. closedir($dh);
  419. return $matches;
  420. }
  421. /**
  422. * Move topic(s)
  423. */
  424. function move_topics($topic_ids, $forum_id, $auto_sync = true)
  425. {
  426. global $db;
  427. if (empty($topic_ids))
  428. {
  429. return;
  430. }
  431. $forum_ids = array($forum_id);
  432. if (!is_array($topic_ids))
  433. {
  434. $topic_ids = array($topic_ids);
  435. }
  436. $sql = 'DELETE FROM ' . TOPICS_TABLE . '
  437. WHERE ' . $db->sql_in_set('topic_moved_id', $topic_ids) . '
  438. AND forum_id = ' . $forum_id;
  439. $db->sql_query($sql);
  440. if ($auto_sync)
  441. {
  442. $sql = 'SELECT DISTINCT forum_id
  443. FROM ' . TOPICS_TABLE . '
  444. WHERE ' . $db->sql_in_set('topic_id', $topic_ids);
  445. $result = $db->sql_query($sql);
  446. while ($row = $db->sql_fetchrow($result))
  447. {
  448. $forum_ids[] = $row['forum_id'];
  449. }
  450. $db->sql_freeresult($result);
  451. }
  452. $table_ary = array(TOPICS_TABLE, POSTS_TABLE, LOG_TABLE, DRAFTS_TABLE, TOPICS_TRACK_TABLE);
  453. foreach ($table_ary as $table)
  454. {
  455. $sql = "UPDATE $table
  456. SET forum_id = $forum_id
  457. WHERE " . $db->sql_in_set('topic_id', $topic_ids);
  458. $db->sql_query($sql);
  459. }
  460. unset($table_ary);
  461. if ($auto_sync)
  462. {
  463. sync('forum', 'forum_id', $forum_ids, true, true);
  464. unset($forum_ids);
  465. }
  466. }
  467. /**
  468. * Move post(s)
  469. */
  470. function move_posts($post_ids, $topic_id, $auto_sync = true)
  471. {
  472. global $db;
  473. if (!is_array($post_ids))
  474. {
  475. $post_ids = array($post_ids);
  476. }
  477. $forum_ids = array();
  478. $topic_ids = array($topic_id);
  479. $sql = 'SELECT DISTINCT topic_id, forum_id
  480. FROM ' . POSTS_TABLE . '
  481. WHERE ' . $db->sql_in_set('post_id', $post_ids);
  482. $result = $db->sql_query($sql);
  483. while ($row = $db->sql_fetchrow($result))
  484. {
  485. $forum_ids[] = (int) $row['forum_id'];
  486. $topic_ids[] = (int) $row['topic_id'];
  487. }
  488. $db->sql_freeresult($result);
  489. $sql = 'SELECT forum_id
  490. FROM ' . TOPICS_TABLE . '
  491. WHERE topic_id = ' . $topic_id;
  492. $result = $db->sql_query($sql);
  493. $forum_row = $db->sql_fetchrow($result);
  494. $db->sql_freeresult($result);
  495. if (!$forum_row)
  496. {
  497. trigger_error('NO_TOPIC');
  498. }
  499. $sql = 'UPDATE ' . POSTS_TABLE . '
  500. SET forum_id = ' . (int) $forum_row['forum_id'] . ", topic_id = $topic_id
  501. WHERE " . $db->sql_in_set('post_id', $post_ids);
  502. $db->sql_query($sql);
  503. $sql = 'UPDATE ' . ATTACHMENTS_TABLE . "
  504. SET topic_id = $topic_id, in_message = 0
  505. WHERE " . $db->sql_in_set('post_msg_id', $post_ids);
  506. $db->sql_query($sql);
  507. if ($auto_sync)
  508. {
  509. $forum_ids[] = (int) $forum_row['forum_id'];
  510. sync('topic_reported', 'topic_id', $topic_ids);
  511. sync('topic_attachment', 'topic_id', $topic_ids);
  512. sync('topic', 'topic_id', $topic_ids, true);
  513. sync('forum', 'forum_id', $forum_ids, true, true);
  514. }
  515. // Update posted information
  516. update_posted_info($topic_ids);
  517. }
  518. /**
  519. * Remove topic(s)
  520. */
  521. function delete_topics($where_type, $where_ids, $auto_sync = true, $post_count_sync = true, $call_delete_posts = true)
  522. {
  523. global $db, $config;
  524. $approved_topics = 0;
  525. $forum_ids = $topic_ids = array();
  526. if ($where_type === 'range')
  527. {
  528. $where_clause = $where_ids;
  529. }
  530. else
  531. {
  532. $where_ids = (is_array($where_ids)) ? array_unique($where_ids) : array($where_ids);
  533. if (!sizeof($where_ids))
  534. {
  535. return array('topics' => 0, 'posts' => 0);
  536. }
  537. $where_clause = $db->sql_in_set($where_type, $where_ids);
  538. }
  539. // Making sure that delete_posts does not call delete_topics again...
  540. $return = array(
  541. 'posts' => ($call_delete_posts) ? delete_posts($where_type, $where_ids, false, true, $post_count_sync, false) : 0,
  542. );
  543. $sql = 'SELECT topic_id, forum_id, topic_approved, topic_moved_id
  544. FROM ' . TOPICS_TABLE . '
  545. WHERE ' . $where_clause;
  546. $result = $db->sql_query($sql);
  547. while ($row = $db->sql_fetchrow($result))
  548. {
  549. $forum_ids[] = $row['forum_id'];
  550. $topic_ids[] = $row['topic_id'];
  551. if ($row['topic_approved'] && !$row['topic_moved_id'])
  552. {
  553. $approved_topics++;
  554. }
  555. }
  556. $db->sql_freeresult($result);
  557. $return['topics'] = sizeof($topic_ids);
  558. if (!sizeof($topic_ids))
  559. {
  560. return $return;
  561. }
  562. $db->sql_transaction('begin');
  563. $table_ary = array(BOOKMARKS_TABLE, TOPICS_TRACK_TABLE, TOPICS_POSTED_TABLE, POLL_VOTES_TABLE, POLL_OPTIONS_TABLE, TOPICS_WATCH_TABLE, TOPICS_TABLE);
  564. foreach ($table_ary as $table)
  565. {
  566. $sql = "DELETE FROM $table
  567. WHERE " . $db->sql_in_set('topic_id', $topic_ids);
  568. $db->sql_query($sql);
  569. }
  570. unset($table_ary);
  571. $moved_topic_ids = array();
  572. // update the other forums
  573. $sql = 'SELECT topic_id, forum_id
  574. FROM ' . TOPICS_TABLE . '
  575. WHERE ' . $db->sql_in_set('topic_moved_id', $topic_ids);
  576. $result = $db->sql_query($sql);
  577. while ($row = $db->sql_fetchrow($result))
  578. {
  579. $forum_ids[] = $row['forum_id'];
  580. $moved_topic_ids[] = $row['topic_id'];
  581. }
  582. $db->sql_freeresult($result);
  583. if (sizeof($moved_topic_ids))
  584. {
  585. $sql = 'DELETE FROM ' . TOPICS_TABLE . '
  586. WHERE ' . $db->sql_in_set('topic_id', $moved_topic_ids);
  587. $db->sql_query($sql);
  588. }
  589. $db->sql_transaction('commit');
  590. if ($auto_sync)
  591. {
  592. sync('forum', 'forum_id', array_unique($forum_ids), true, true);
  593. sync('topic_reported', $where_type, $where_ids);
  594. }
  595. if ($approved_topics)
  596. {
  597. set_config_count('num_topics', $approved_topics * (-1), true);
  598. }
  599. return $return;
  600. }
  601. /**
  602. * Remove post(s)
  603. */
  604. function delete_posts($where_type, $where_ids, $auto_sync = true, $posted_sync = true, $post_count_sync = true, $call_delete_topics = true)
  605. {
  606. global $db, $config, $phpbb_root_path, $phpEx;
  607. if ($where_type === 'range')
  608. {
  609. $where_clause = $where_ids;
  610. }
  611. else
  612. {
  613. if (is_array($where_ids))
  614. {
  615. $where_ids = array_unique($where_ids);
  616. }
  617. else
  618. {
  619. $where_ids = array($where_ids);
  620. }
  621. if (!sizeof($where_ids))
  622. {
  623. return false;
  624. }
  625. $where_ids = array_map('intval', $where_ids);
  626. /* Possible code for splitting post deletion
  627. if (sizeof($where_ids) >= 1001)
  628. {
  629. // Split into chunks of 1000
  630. $chunks = array_chunk($where_ids, 1000);
  631. foreach ($chunks as $_where_ids)
  632. {
  633. delete_posts($where_type, $_where_ids, $auto_sync, $posted_sync, $post_count_sync, $call_delete_topics);
  634. }
  635. return;
  636. }*/
  637. $where_clause = $db->sql_in_set($where_type, $where_ids);
  638. }
  639. $approved_posts = 0;
  640. $post_ids = $topic_ids = $forum_ids = $post_counts = $remove_topics = array();
  641. $sql = 'SELECT post_id, poster_id, post_approved, post_postcount, topic_id, forum_id
  642. FROM ' . POSTS_TABLE . '
  643. WHERE ' . $where_clause;
  644. //-- mod: Prime Trash Bin ---------------------------------------------------//
  645. $sql = substr_replace($sql, 'post_deleted_time, ', strpos($sql, 'post_id'), 0);
  646. //-- end: Prime Trash Bin ---------------------------------------------------//
  647. $result = $db->sql_query($sql);
  648. while ($row = $db->sql_fetchrow($result))
  649. {
  650. $post_ids[] = (int) $row['post_id'];
  651. $poster_ids[] = (int) $row['poster_id'];
  652. $topic_ids[] = (int) $row['topic_id'];
  653. $forum_ids[] = (int) $row['forum_id'];
  654. if ($row['post_postcount'] && $post_count_sync && $row['post_approved'])
  655. {
  656. //-- mod: Prime Trash Bin ---------------------------------------------------//
  657. // Do not reduce post counts for posts that have already been marked as deleted.
  658. if (!empty($row['post_deleted_time']))
  659. {
  660. continue;
  661. }
  662. //-- end: Prime Trash Bin ---------------------------------------------------//
  663. $post_counts[$row['poster_id']] = (!empty($post_counts[$row['poster_id']])) ? $post_counts[$row['poster_id']] + 1 : 1;
  664. }
  665. if ($row['post_approved'])
  666. {
  667. $approved_posts++;
  668. }
  669. }
  670. $db->sql_freeresult($result);
  671. if (!sizeof($post_ids))
  672. {
  673. return false;
  674. }
  675. $db->sql_transaction('begin');
  676. $table_ary = array(POSTS_TABLE, REPORTS_TABLE);
  677. //-- mod: Prime Post Revisions ----------------------------------------------//
  678. $table_ary[] = POST_REVISIONS_TABLE; // To delete the post's edit history
  679. //-- end: Prime Post Revisions ----------------------------------------------//
  680. foreach ($table_ary as $table)
  681. {
  682. $sql = "DELETE FROM $table
  683. WHERE " . $db->sql_in_set('post_id', $post_ids);
  684. $db->sql_query($sql);
  685. }
  686. unset($table_ary);
  687. // Adjust users post counts
  688. if (sizeof($post_counts) && $post_count_sync)
  689. {
  690. foreach ($post_counts as $poster_id => $substract)
  691. {
  692. $sql = 'UPDATE ' . USERS_TABLE . '
  693. SET user_posts = 0
  694. WHERE user_id = ' . $poster_id . '
  695. AND user_posts < ' . $substract;
  696. $db->sql_query($sql);
  697. $sql = 'UPDATE ' . USERS_TABLE . '
  698. SET user_posts = user_posts - ' . $substract . '
  699. WHERE user_id = ' . $poster_id . '
  700. AND user_posts >= ' . $substract;
  701. $db->sql_query($sql);
  702. }
  703. }
  704. // Remove topics now having no posts?
  705. if (sizeof($topic_ids))
  706. {
  707. $sql = 'SELECT topic_id
  708. FROM ' . POSTS_TABLE . '
  709. WHERE ' . $db->sql_in_set('topic_id', $topic_ids) . '
  710. GROUP BY topic_id';
  711. $result = $db->sql_query($sql);
  712. while ($row = $db->sql_fetchrow($result))
  713. {
  714. $remove_topics[] = $row['topic_id'];
  715. }
  716. $db->sql_freeresult($result);
  717. // Actually, those not within remove_topics should be removed. ;)
  718. $remove_topics = array_diff($topic_ids, $remove_topics);
  719. }
  720. // Remove the message from the search index
  721. $search_type = basename($config['search_type']);
  722. if (!file_exists($phpbb_root_path . 'includes/search/' . $search_type . '.' . $phpEx))
  723. {
  724. trigger_error('NO_SUCH_SEARCH_MODULE');
  725. }
  726. include_once("{$phpbb_root_path}includes/search/$search_type.$phpEx");
  727. $error = false;
  728. $search = new $search_type($error);
  729. if ($error)
  730. {
  731. trigger_error($error);
  732. }
  733. $search->index_remove($post_ids, $poster_ids, $forum_ids);
  734. delete_attachments('post', $post_ids, false);
  735. $db->sql_transaction('commit');
  736. // Resync topics_posted table
  737. if ($posted_sync)
  738. {
  739. update_posted_info($topic_ids);
  740. }
  741. if ($auto_sync)
  742. {
  743. sync('topic_reported', 'topic_id', $topic_ids);
  744. sync('topic', 'topic_id', $topic_ids, true);
  745. sync('forum', 'forum_id', $forum_ids, true, true);
  746. }
  747. if ($approved_posts)
  748. {
  749. set_config_count('num_posts', $approved_posts * (-1), true);
  750. }
  751. // We actually remove topics now to not be inconsistent (the delete_topics function calls this function too)
  752. if (sizeof($remove_topics) && $call_delete_topics)
  753. {
  754. delete_topics('topic_id', $remove_topics, $auto_sync, $post_count_sync, false);
  755. }
  756. return sizeof($post_ids);
  757. }
  758. /**
  759. * Delete Attachments
  760. *
  761. * @param string $mode can be: post|message|topic|attach|user
  762. * @param mixed $ids can be: post_ids, message_ids, topic_ids, attach_ids, user_ids
  763. * @param bool $resync set this to false if you are deleting posts or topics
  764. */
  765. function delete_attachments($mode, $ids, $resync = true)
  766. {
  767. global $db, $config;
  768. // 0 is as bad as an empty array
  769. if (empty($ids))
  770. {
  771. return false;
  772. }
  773. if (is_array($ids))
  774. {
  775. $ids = array_unique($ids);
  776. $ids = array_map('intval', $ids);
  777. }
  778. else
  779. {
  780. $ids = array((int) $ids);
  781. }
  782. $sql_where = '';
  783. switch ($mode)
  784. {
  785. case 'post':
  786. case 'message':
  787. $sql_id = 'post_msg_id';
  788. $sql_where = ' AND in_message = ' . ($mode == 'message' ? 1 : 0);
  789. break;
  790. case 'topic':
  791. $sql_id = 'topic_id';
  792. break;
  793. case 'user':
  794. $sql_id = 'poster_id';
  795. break;
  796. case 'attach':
  797. default:
  798. $sql_id = 'attach_id';
  799. $mode = 'attach';
  800. break;
  801. }
  802. $post_ids = $message_ids = $topic_ids = $physical = array();
  803. // Collect post and topic ids for later use if we need to touch remaining entries (if resync is enabled)
  804. $sql = 'SELECT post_msg_id, topic_id, in_message, physical_filename, thumbnail, filesize, is_orphan
  805. FROM ' . ATTACHMENTS_TABLE . '
  806. WHERE ' . $db->sql_in_set($sql_id, $ids);
  807. $sql .= $sql_where;
  808. $result = $db->sql_query($sql);
  809. while ($row = $db->sql_fetchrow($result))
  810. {
  811. // We only need to store post/message/topic ids if resync is enabled and the file is not orphaned
  812. if ($resync && !$row['is_orphan'])
  813. {
  814. if (!$row['in_message'])
  815. {
  816. $post_ids[] = $row['post_msg_id'];
  817. $topic_ids[] = $row['topic_id'];
  818. }
  819. else
  820. {
  821. $message_ids[] = $row['post_msg_id'];
  822. }
  823. }
  824. $physical[] = array('filename' => $row['physical_filename'], 'thumbnail' => $row['thumbnail'], 'filesize' => $row['filesize'], 'is_orphan' => $row['is_orphan']);
  825. }
  826. $db->sql_freeresult($result);
  827. // Delete attachments
  828. $sql = 'DELETE FROM ' . ATTACHMENTS_TABLE . '
  829. WHERE ' . $db->sql_in_set($sql_id, $ids);
  830. $sql .= $sql_where;
  831. $db->sql_query($sql);
  832. $num_deleted = $db->sql_affectedrows();
  833. if (!$num_deleted)
  834. {
  835. return 0;
  836. }
  837. // Delete attachments from filesystem
  838. $space_removed = $files_removed = 0;
  839. foreach ($physical as $file_ary)
  840. {
  841. if (phpbb_unlink($file_ary['filename'], 'file', true) && !$file_ary['is_orphan'])
  842. {
  843. // Only non-orphaned files count to the file size
  844. $space_removed += $file_ary['filesize'];
  845. $files_removed++;
  846. }
  847. if ($file_ary['thumbnail'])
  848. {
  849. phpbb_unlink($file_ary['filename'], 'thumbnail', true);
  850. }
  851. }
  852. if ($space_removed || $files_removed)
  853. {
  854. set_config_count('upload_dir_size', $space_removed * (-1), true);
  855. set_config_count('num_files', $files_removed * (-1), true);
  856. }
  857. // If we do not resync, we do not need to adjust any message, post, topic or user entries
  858. if (!$resync)
  859. {
  860. return $num_deleted;
  861. }
  862. // No more use for the original ids
  863. unset($ids);
  864. // Now, we need to resync posts, messages, topics. We go through every one of them
  865. $post_ids = array_unique($post_ids);
  866. $message_ids = array_unique($message_ids);
  867. $topic_ids = array_unique($topic_ids);
  868. // Update post indicators for posts now no longer having attachments
  869. if (sizeof($post_ids))
  870. {
  871. // Just check which posts are still having an assigned attachment not orphaned by querying the attachments table
  872. $sql = 'SELECT post_msg_id
  873. FROM ' . ATTACHMENTS_TABLE . '
  874. WHERE ' . $db->sql_in_set('post_msg_id', $post_ids) . '
  875. AND in_message = 0
  876. AND is_orphan = 0';
  877. $result = $db->sql_query($sql);
  878. $remaining_ids = array();
  879. while ($row = $db->sql_fetchrow($result))
  880. {
  881. $remaining_ids[] = $row['post_msg_id'];
  882. }
  883. $db->sql_freeresult($result);
  884. // Now only unset those ids remaining
  885. $post_ids = array_diff($post_ids, $remaining_ids);
  886. if (sizeof($post_ids))
  887. {
  888. $sql = 'UPDATE ' . POSTS_TABLE . '
  889. SET post_attachment = 0
  890. WHERE ' . $db->sql_in_set('post_id', $post_ids);
  891. $db->sql_query($sql);
  892. }
  893. }
  894. // Update message table if messages are affected
  895. if (sizeof($message_ids))
  896. {
  897. // Just check which messages are still having an assigned attachment not orphaned by querying the attachments table
  898. $sql = 'SELECT post_msg_id
  899. FROM ' . ATTACHMENTS_TABLE . '
  900. WHERE ' . $db->sql_in_set('post_msg_id', $message_ids) . '
  901. AND in_message = 1
  902. AND is_orphan = 0';
  903. $result = $db->sql_query($sql);
  904. $remaining_ids = array();
  905. while ($row = $db->sql_fetchrow($result))
  906. {
  907. $remaining_ids[] = $row['post_msg_id'];
  908. }
  909. $db->sql_freeresult($result);
  910. // Now only unset those ids remaining
  911. $message_ids = array_diff($message_ids, $remaining_ids);
  912. if (sizeof($message_ids))
  913. {
  914. $sql = 'UPDATE ' . PRIVMSGS_TABLE . '
  915. SET message_attachment = 0
  916. WHERE ' . $db->sql_in_set('msg_id', $message_ids);
  917. $db->sql_query($sql);
  918. }
  919. }
  920. // Now update the topics. This is a bit trickier, because there could be posts still having attachments within the topic
  921. if (sizeof($topic_ids))
  922. {
  923. // Just check which topics are still having an assigned attachment not orphaned by querying the attachments table (much less entries expected)
  924. $sql = 'SELECT topic_id
  925. FROM ' . ATTACHMENTS_TABLE . '
  926. WHERE ' . $db->sql_in_set('topic_id', $topic_ids) . '
  927. AND is_orphan = 0';
  928. $result = $db->sql_query($sql);
  929. $remaining_ids = array();
  930. while ($row = $db->sql_fetchrow($result))
  931. {
  932. $remaining_ids[] = $row['topic_id'];
  933. }
  934. $db->sql_freeresult($result);
  935. // Now only unset those ids remaining
  936. $topic_ids = array_diff($topic_ids, $remaining_ids);
  937. if (sizeof($topic_ids))
  938. {
  939. $sql = 'UPDATE ' . TOPICS_TABLE . '
  940. SET topic_attachment = 0
  941. WHERE ' . $db->sql_in_set('topic_id', $topic_ids);
  942. $db->sql_query($sql);
  943. }
  944. }
  945. return $num_deleted;
  946. }
  947. /**
  948. * Deletes shadow topics pointing to a specified forum.
  949. *
  950. * @param int $forum_id The forum id
  951. * @param string $sql_more Additional WHERE statement, e.g. t.topic_time < (time() - 1234)
  952. * @param bool $auto_sync Will call sync() if this is true
  953. *
  954. * @return array Array with affected forums
  955. *
  956. * @author bantu
  957. */
  958. function delete_topic_shadows($forum_id, $sql_more = '', $auto_sync = true)
  959. {
  960. global $db;
  961. if (!$forum_id)
  962. {
  963. // Nothing to do.
  964. return;
  965. }
  966. // Set of affected forums we have to resync
  967. $sync_forum_ids = array();
  968. // Amount of topics we select and delete at once.
  969. $batch_size = 500;
  970. do
  971. {
  972. $sql = 'SELECT t2.forum_id, t2.topic_id
  973. FROM ' . TOPICS_TABLE . ' t2, ' . TOPICS_TABLE . ' t
  974. WHERE t2.topic_moved_id = t.topic_id
  975. AND t.forum_id = ' . (int) $forum_id . '
  976. ' . (($sql_more) ? 'AND ' . $sql_more : '');
  977. $result = $db->sql_query_limit($sql, $batch_size);
  978. $topic_ids = array();
  979. while ($row = $db->sql_fetchrow($result))
  980. {
  981. $topic_ids[] = (int) $row['topic_id'];
  982. $sync_forum_ids[(int) $row['forum_id']] = (int) $row['forum_id'];
  983. }
  984. $db->sql_freeresult($result);
  985. if (!empty($topic_ids))
  986. {
  987. $sql = 'DELETE FROM ' . TOPICS_TABLE . '
  988. WHERE ' . $db->sql_in_set('topic_id', $topic_ids);
  989. $db->sql_query($sql);
  990. }
  991. }
  992. while (sizeof($topic_ids) == $batch_size);
  993. if ($auto_sync)
  994. {
  995. sync('forum', 'forum_id', $sync_forum_ids, true, true);
  996. }
  997. return $sync_forum_ids;
  998. }
  999. /**
  1000. * Update/Sync posted information for topics
  1001. */
  1002. function update_posted_info(&$topic_ids)
  1003. {
  1004. global $db, $config;
  1005. if (empty($topic_ids) || !$config['load_db_track'])
  1006. {
  1007. return;
  1008. }
  1009. // First of all, let us remove any posted information for these topics
  1010. $sql = 'DELETE FROM ' . TOPICS_POSTED_TABLE . '
  1011. WHERE ' . $db->sql_in_set('topic_id', $topic_ids);
  1012. $db->sql_query($sql);
  1013. // Now, let us collect the user/topic combos for rebuilding the information
  1014. $sql = 'SELECT poster_id, topic_id
  1015. FROM ' . POSTS_TABLE . '
  1016. WHERE ' . $db->sql_in_set('topic_id', $topic_ids) . '
  1017. AND poster_id <> ' . ANONYMOUS . '
  1018. GROUP BY poster_id, topic_id';
  1019. $result = $db->sql_query($sql);
  1020. $posted = array();
  1021. while ($row = $db->sql_fetchrow($result))
  1022. {
  1023. // Add as key to make them unique (grouping by) and circumvent empty keys on array_unique
  1024. $posted[$row['poster_id']][] = $row['topic_id'];
  1025. }
  1026. $db->sql_freeresult($result);
  1027. // Now add the information...
  1028. $sql_ary = array();
  1029. foreach ($posted as $user_id => $topic_row)
  1030. {
  1031. foreach ($topic_row as $topic_id)
  1032. {
  1033. $sql_ary[] = array(
  1034. 'user_id' => (int) $user_id,
  1035. 'topic_id' => (int) $topic_id,
  1036. 'topic_posted' => 1,
  1037. );
  1038. }
  1039. }
  1040. unset($posted);
  1041. $db->sql_multi_insert(TOPICS_POSTED_TABLE, $sql_ary);
  1042. }
  1043. /**
  1044. * Delete attached file
  1045. */
  1046. function phpbb_unlink($filename, $mode = 'file', $entry_removed = false)
  1047. {
  1048. global $db, $phpbb_root_path, $config;
  1049. // Because of copying topics or modifications a physical filename could be assigned more than once. If so, do not remove the file itself.
  1050. $sql = 'SELECT COUNT(attach_id) AS num_entries
  1051. FROM ' . ATTACHMENTS_TABLE . "
  1052. WHERE physical_filename = '" . $db->sql_escape(utf8_basename($filename)) . "'";
  1053. $result = $db->sql_query($sql);
  1054. $num_entries = (int) $db->sql_fetchfield('num_entries');
  1055. $db->sql_freeresult($result);
  1056. // Do not remove file if at least one additional entry with the same name exist.
  1057. if (($entry_removed && $num_entries > 0) || (!$entry_removed && $num_entries > 1))
  1058. {
  1059. return false;
  1060. }
  1061. $filename = ($mode == 'thumbnail') ? 'thumb_' . utf8_basename($filename) : utf8_basename($filename);
  1062. return @unlink($phpbb_root_path . $config['upload_path'] . '/' . $filename);
  1063. }
  1064. /**
  1065. * All-encompasing sync function
  1066. *
  1067. * Exaples:
  1068. * <code>
  1069. * sync('topic', 'topic_id', 123); // resync topic #123
  1070. * sync('topic', 'forum_id', array(2, 3)); // resync topics from forum #2 and #3
  1071. * sync('topic'); // resync all topics
  1072. * sync('topic', 'range', 'topic_id BETWEEN 1 AND 60'); // resync a range of topics/forums (only available for 'topic' and 'forum' modes)
  1073. * </code>
  1074. *
  1075. * Modes:
  1076. * - forum Resync complete forum
  1077. * - topic Resync topics
  1078. * - topic_moved Removes topic shadows that would be in the same forum as the topic they link to
  1079. * - topic_approved Resyncs the topic_approved flag according to the status of the first post
  1080. * - post_reported Resyncs the post_reported flag, relying on actual reports
  1081. * - topic_reported Resyncs the topic_reported flag, relying on post_reported flags
  1082. * - post_attachement Same as post_reported, but with attachment flags
  1083. * - topic_attachement Same as topic_reported, but with attachment flags
  1084. */
  1085. function sync($mode, $where_type = '', $where_ids = '', $resync_parents = false, $sync_extra = false)
  1086. {
  1087. global $db;
  1088. if (is_array($where_ids))
  1089. {
  1090. $where_ids = array_unique($where_ids);
  1091. $where_ids = array_map('intval', $where_ids);
  1092. }
  1093. else if ($where_type != 'range')
  1094. {
  1095. $where_ids = ($where_ids) ? array((int) $where_ids) : array();
  1096. }
  1097. if ($mode == 'forum' || $mode == 'topic' || $mode == 'topic_approved' || $mode == 'topic_reported' || $mode == 'post_reported')
  1098. {
  1099. if (!$where_type)
  1100. {
  1101. $where_sql = '';
  1102. $where_sql_and = 'WHERE';
  1103. }
  1104. else if ($where_type == 'range')
  1105. {
  1106. // Only check a range of topics/forums. For instance: 'topic_id BETWEEN 1 AND 60'
  1107. $where_sql = 'WHERE (' . $mode[0] . ".$where_ids)";
  1108. $where_sql_and = $where_sql . "\n\tAND";
  1109. }
  1110. else
  1111. {
  1112. // Do not sync the "global forum"
  1113. $where_ids = array_diff($where_ids, array(0));
  1114. if (!sizeof($where_ids))
  1115. {
  1116. // Empty array with IDs. This means that we don't have any work to do. Just return.
  1117. return;
  1118. }
  1119. // Limit the topics/forums we are syncing, use specific topic/forum IDs.
  1120. // $where_type contains the field for the where clause (forum_id, topic_id)
  1121. $where_sql = 'WHERE ' . $db->sql_in_set($mode[0] . '.' . $where_type, $where_ids);
  1122. $where_sql_and = $where_sql . "\n\tAND";
  1123. }
  1124. }
  1125. else
  1126. {
  1127. if (!sizeof($where_ids))
  1128. {
  1129. return;
  1130. }
  1131. // $where_type contains the field for the where clause (forum_id, topic_id)
  1132. $where_sql = 'WHERE ' . $db->sql_in_set($mode[0] . '.' . $where_type, $where_ids);
  1133. $where_sql_and = $where_sql . "\n\tAND";
  1134. }
  1135. switch ($mode)
  1136. {
  1137. case 'topic_moved':
  1138. $db->sql_transaction('begin');
  1139. switch ($db->sql_layer)
  1140. {
  1141. case 'mysql4':
  1142. case 'mysqli':
  1143. $sql = 'DELETE FROM ' . TOPICS_TABLE . '
  1144. USING ' . TOPICS_TABLE . ' t1, ' . TOPICS_TABLE . " t2
  1145. WHERE t1.topic_moved_id = t2.topic_id
  1146. AND t1.forum_id = t2.forum_id";
  1147. $db->sql_query($sql);
  1148. break;
  1149. default:
  1150. $sql = 'SELECT t1.topic_id
  1151. FROM ' .TOPICS_TABLE . ' t1, ' . TOPICS_TABLE . " t2
  1152. WHERE t1.topic_moved_id = t2.topic_id
  1153. AND t1.forum_id = t2.forum_id";
  1154. $result = $db->sql_query($sql);
  1155. $topic_id_ary = array();
  1156. while ($row = $db->sql_fetchrow($result))
  1157. {
  1158. $topic_id_ary[] = $row['topic_id'];
  1159. }
  1160. $db->sql_freeresult($result);
  1161. if (!sizeof($topic_id_ary))
  1162. {
  1163. return;
  1164. }
  1165. $sql = 'DELETE FROM ' . TOPICS_TABLE . '
  1166. WHERE ' . $db->sql_in_set('topic_id', $topic_id_ary);
  1167. $db->sql_query($sql);
  1168. break;
  1169. }
  1170. $db->sql_transaction('commit');
  1171. break;
  1172. case 'topic_approved':
  1173. $db->sql_transaction('begin');
  1174. switch ($db->sql_layer)
  1175. {
  1176. case 'mysql4':
  1177. case 'mysqli':
  1178. $sql = 'UPDATE ' . TOPICS_TABLE . ' t, ' . POSTS_TABLE . " p
  1179. SET t.topic_approved = p.post_approved
  1180. $where_sql_and t.topic_first_post_id = p.post_id";
  1181. $db->sql_query($sql);
  1182. break;
  1183. default:
  1184. $sql = 'SELECT t.topic_id, p.post_approved
  1185. FROM ' . TOPICS_TABLE . ' t, ' . POSTS_TABLE . " p
  1186. $where_sql_and p.post_id = t.topic_first_post_id
  1187. AND p.post_approved <> t.topic_approved";
  1188. $result = $db->sql_query($sql);
  1189. $topic_ids = array();
  1190. while ($row = $db->sql_fetchrow($result))
  1191. {
  1192. $topic_ids[] = $row['topic_id'];
  1193. }
  1194. $db->sql_freeresult($result);
  1195. if (!sizeof($topic_ids))
  1196. {
  1197. return;
  1198. }
  1199. $sql = 'UPDATE ' . TOPICS_TABLE . '
  1200. SET topic_approved = 1 - topic_approved
  1201. WHERE ' . $db->sql_in_set('topic_id', $topic_ids);
  1202. $db->sql_query($sql);
  1203. break;
  1204. }
  1205. $db->sql_transaction('commit');
  1206. break;
  1207. case 'post_reported':
  1208. $post_ids = $post_reported = array();
  1209. $db->sql_transaction('begin');
  1210. $sql = 'SELECT p.post_id, p.post_reported
  1211. FROM ' . POSTS_TABLE . " p
  1212. $where_sql
  1213. GROUP BY p.post_id, p.post_reported";
  1214. $result = $db->sql_query($sql);
  1215. while ($row = $db->sql_fetchrow($result))
  1216. {
  1217. $post_ids[$row['post_id']] = $row['post_id'];
  1218. if ($row['post_reported'])
  1219. {
  1220. $post_reported[$row['post_id']] = 1;
  1221. }
  1222. }
  1223. $db->sql_freeresult($result);
  1224. $sql = 'SELECT DISTINCT(post_id)
  1225. FROM ' . REPORTS_TABLE . '
  1226. WHERE ' . $db->sql_in_set('post_id', $post_ids) . '
  1227. AND report_closed = 0';
  1228. $result = $db->sql_query($sql);
  1229. $post_ids = array();
  1230. while ($row = $db->sql_fetchrow($result))
  1231. {
  1232. if (!isset($post_reported[$row['post_id']]))
  1233. {
  1234. $post_ids[] = $row['post_id'];
  1235. }
  1236. else
  1237. {
  1238. unset($post_reported[$row['post_id']]);
  1239. }
  1240. }
  1241. $db->sql_freeresult($result);
  1242. // $post_reported should be empty by now, if it's not it contains
  1243. // posts that are falsely flagged as reported
  1244. foreach ($post_reported as $post_id => $void)
  1245. {
  1246. $post_ids[] = $post_id;
  1247. }
  1248. if (sizeof($post_ids))
  1249. {
  1250. $sql = 'UPDATE ' . POSTS_TABLE . '
  1251. SET post_reported = 1 - post_reported
  1252. WHERE ' . $db->sql_in_set('post_id', $post_ids);
  1253. $db->sql_query($sql);
  1254. }
  1255. $db->sql_transaction('commit');
  1256. break;
  1257. case 'topic_reported':
  1258. if ($sync_extra)
  1259. {
  1260. sync('post_reported', $where_type, $where_ids);
  1261. }
  1262. $topic_ids = $topic_reported = array();
  1263. $db->sql_transaction('begin');
  1264. $sql = 'SELECT DISTINCT(t.topic_id)
  1265. FROM ' . POSTS_TABLE . " t
  1266. $where_sql_and t.post_reported = 1";
  1267. $result = $db->sql_query($sql);
  1268. while ($row = $db->sql_fetchrow($result))
  1269. {
  1270. $topic_reported[$row['topic_id']] = 1;
  1271. }
  1272. $db->sql_freeresult($result);
  1273. $sql = 'SELECT t.topic_id, t.topic_reported
  1274. FROM ' . TOPICS_TABLE . " t
  1275. $where_sql";
  1276. $result = $db->sql_query($sql);
  1277. while ($row = $db->sql_fetchrow($result))
  1278. {
  1279. if ($row['topic_reported'] ^ isset($topic_reported[$row['topic_id']]))
  1280. {
  1281. $topic_ids[] = $row['topic_id'];
  1282. }
  1283. }
  1284. $db->sql_freeresult($result);
  1285. if (sizeof($topic_ids))
  1286. {
  1287. $sql = 'UPDATE ' . TOPICS_TABLE . '
  1288. SET topic_reported = 1 - topic_reported
  1289. WHERE ' . $db->sql_in_set('topic_id', $topic_ids);
  1290. $db->sql_query($sql);
  1291. }
  1292. $db->sql_transaction('commit');
  1293. break;
  1294. case 'post_attachment':
  1295. $post_ids = $post_attachment = array();
  1296. $db->sql_transaction('begin');
  1297. $sql = 'SELECT p.post_id, p.post_attachment
  1298. FROM ' . POSTS_TABLE . " p
  1299. $where_sql
  1300. GROUP BY p.post_id, p.post_attachment";
  1301. $result = $db->sql_query($sql);
  1302. while ($row = $db->sql_fetchrow($result))
  1303. {
  1304. $post_ids[$row['post_id']] = $row['post_id'];
  1305. if ($row['post_attachment'])
  1306. {
  1307. $post_attachment[$row['post_id']] = 1;
  1308. }
  1309. }
  1310. $db->sql_freeresult($result);
  1311. $sql = 'SELECT DISTINCT(post_msg_id)
  1312. FROM ' . ATTACHMENTS_TABLE . '
  1313. WHERE ' . $db->sql_in_set('post_msg_id', $post_ids) . '
  1314. AND in_message = 0';
  1315. $result = $db->sql_query($sql);
  1316. $post_ids = array();
  1317. while ($row = $db->sql_fetchrow($result))
  1318. {
  1319. if (!isset($post_attachment[$row['post_msg_id']]))
  1320. {
  1321. $post_ids[] = $row['post_msg_id'];
  1322. }
  1323. else
  1324. {
  1325. unset($post_attachment[$row['post_msg_id']]);
  1326. }
  1327. }
  1328. $db->sql_freeresult($result);
  1329. // $post_attachment should be empty by now, if it's not it contains
  1330. // posts that are falsely flagged as having attachments
  1331. foreach ($post_attachment as $post_id => $void)
  1332. {
  1333. $post_ids[] = $post_id;
  1334. }
  1335. if (sizeof($post_ids))
  1336. {
  1337. $sql = 'UPDATE ' . POSTS_TABLE . '
  1338. SET post_attachment = 1 - post_attachment
  1339. WHERE ' . $db->sql_in_set('post_id', $post_ids);
  1340. $db->sql_query($sql);
  1341. }
  1342. $db->sql_transaction('commit');
  1343. break;
  1344. case 'topic_attachment':
  1345. if ($sync_extra)
  1346. {
  1347. sync('post_attachment', $where_type, $where_ids);
  1348. }
  1349. $topic_ids = $topic_attachment = array();
  1350. $db->sql_transaction('begin');
  1351. $sql = 'SELECT DISTINCT(t.topic_id)
  1352. FROM ' . POSTS_TABLE . " t
  1353. $where_sql_and t.post_attachment = 1";
  1354. $result = $db->sql_query($sql);
  1355. while ($row = $db->sql_fetchrow($result))
  1356. {
  1357. $topic_attachment[$row['topic_id']] = 1;
  1358. }
  1359. $db->sql_freeresult($result);
  1360. $sql = 'SELECT t.topic_id, t.topic_attachment
  1361. FROM ' . TOPICS_TABLE . " t
  1362. $where_sql";
  1363. $result = $db->sql_query($sql);
  1364. while ($row = $db->sql_fetchrow($result))
  1365. {
  1366. if ($row['topic_attachment'] ^ isset($topic_attachment[$row['topic_id']]))
  1367. {
  1368. $topic_ids[] = $row['topic_id'];
  1369. }
  1370. }
  1371. $db->sql_freeresult($result);
  1372. if (sizeof($topic_ids))
  1373. {
  1374. $sql = 'UPDATE ' . TOPICS_TABLE . '
  1375. SET topic_attachment = 1 - topic_attachment
  1376. WHERE ' . $db->sql_in_set('topic_id', $topic_ids);
  1377. $db->sql_query($sql);
  1378. }
  1379. $db->sql_transaction('commit');
  1380. break;
  1381. case 'forum':
  1382. $db->sql_transaction('begin');
  1383. // 1: Get the list of all forums
  1384. $sql = 'SELECT f.*
  1385. FROM ' . FORUMS_TABLE . " f
  1386. $where_sql";
  1387. $result = $db->sql_query($sql);
  1388. $forum_data = $forum_ids = $post_ids = $last_post_id = $post_info = array();
  1389. while ($row = $db->sql_fetchrow($result))
  1390. {
  1391. if ($row['forum_type'] == FORUM_LINK)
  1392. {
  1393. continue;
  1394. }
  1395. $forum_id = (int) $row['forum_id'];
  1396. $forum_ids[$forum_id] = $forum_id;
  1397. $forum_data[$forum_id] = $row;
  1398. if ($sync_extra)
  1399. {
  1400. $forum_data[$forum_id]['posts'] = 0;
  1401. $forum_data[$forum_id]['topics'] = 0;
  1402. $forum_data[$forum_id]['topics_real'] = 0;
  1403. }
  1404. $forum_data[$forum_id]['last_post_id'] = 0;
  1405. $forum_data[$forum_id]['last_post_subject'] = '';
  1406. $forum_data[$forum_id]['last_post_time'] = 0;
  1407. $forum_data[$forum_id]['last_poster_id'] = 0;
  1408. $forum_data[$forum_id]['last_poster_name'] = '';
  1409. $forum_data[$forum_id]['last_poster_colour'] = '';
  1410. }
  1411. $db->sql_freeresult($result);
  1412. if (!sizeof($forum_ids))
  1413. {
  1414. break;
  1415. }
  1416. $forum_ids = array_values($forum_ids);
  1417. // 2: Get topic counts for each forum (optional)
  1418. if ($sync_extra)
  1419. {
  1420. $sql = 'SELECT forum_id, topic_approved, COUNT(topic_id) AS forum_topics
  1421. FROM ' . TOPICS_TABLE . '
  1422. WHERE ' . $db->sql_in_set('forum_id', $forum_ids) . '
  1423. GROUP BY forum_id, topic_approved';
  1424. $result = $db->sql_query($sql);
  1425. while ($row = $db->sql_fetchrow($result))
  1426. {
  1427. $forum_id = (int) $row['forum_id'];
  1428. $forum_data[$forum_id]['topics_real'] += $row['forum_topics'];
  1429. if ($row['topic_approved'])
  1430. {
  1431. $forum_data[$forum_id]['topics'] = $row['forum_topics'];
  1432. }
  1433. }
  1434. $db->sql_freeresult($result);
  1435. }
  1436. // 3: Get post count for each forum (optional)
  1437. if ($sync_extra)
  1438. {
  1439. if (sizeof($forum_ids) == 1)
  1440. {
  1441. $sql = 'SELECT SUM(t.topic_replies + 1) AS forum_posts
  1442. FROM ' . TOPICS_TABLE . ' t
  1443. WHERE ' . $db->sql_in_set('t.forum_id', $forum_ids) . '
  1444. AND t.topic_approved = 1
  1445. AND t.topic_status <> ' . ITEM_MOVED;
  1446. }
  1447. else
  1448. {
  1449. $sql = 'SELECT t.forum_id, SUM(t.topic_replies + 1) AS forum_posts
  1450. FROM ' . TOPICS_TABLE . ' t
  1451. WHERE ' . $db->sql_in_set('t.forum_id', $forum_ids) . '
  1452. AND t.topic_approved = 1
  1453. AND t.topic_status <> ' . ITEM_MOVED . '
  1454. GROUP BY t.forum_id';
  1455. }
  1456. $result = $db->sql_query($sql);
  1457. while ($row = $db->sql_fetchrow($result))
  1458. {
  1459. $forum_id = (sizeof($forum_ids) == 1) ? (int) $forum_ids[0] : (int) $row['forum_id'];
  1460. $forum_data[$forum_id]['posts'] = (int) $row['forum_posts'];
  1461. }
  1462. $db->sql_freeresult($result);
  1463. }
  1464. // 4: Get last_post_id for each forum
  1465. if (sizeof($forum_ids) == 1)
  1466. {
  1467. $sql = 'SELECT MAX(t.topic_last_post_id) as last_post_id
  1468. FROM ' . TOPICS_TABLE . ' t
  1469. WHERE ' . $db->sql_in_set('t.forum_id', $forum_ids) . '
  1470. AND t.topic_approved = 1';
  1471. }
  1472. else
  1473. {
  1474. $sql = 'SELECT t.forum_id, MAX(t.topic_last_post_id) as last_post_id
  1475. FROM ' . TOPICS_TABLE . ' t
  1476. WHERE ' . $db->sql_in_set('t.forum_id', $forum_ids) . '
  1477. AND t.topic_approved = 1
  1478. GROUP BY t.forum_id';
  1479. }
  1480. //-- mod: Prime Trash Bin (Topics) ------------------------------------------//
  1481. // Here we update the last post information for the forums.
  1482. global $phpbb_root_path, $phpEx;
  1483. include ($phpbb_root_path . 'includes/prime_trash_bin_a.' . $phpEx);
  1484. if (stifle_topics_enabled())
  1485. {
  1486. // Get the last topic that isn't deleted, except for when doing the Trash forum.
  1487. $sql = str_replace('AND t.topic_approved', 'AND (t.topic_deleted_time = 0' . (get_trash_forum() ? ' OR t.forum_id = ' . get_trash_forum() : '') . ') AND t.topic_approved', $sql);
  1488. }
  1489. //-- end: Prime Trash Bin (Topics) ------------------------------------------//
  1490. $result = $db->sql_query($sql);
  1491. while ($row = $db->sql_fetchrow($result))
  1492. {
  1493. $forum_id = (sizeof($forum_ids) == 1) ? (int) $forum_ids[0] : (int) $row['forum_id'];
  1494. $forum_data[$forum_id]['last_post_id'] = (int) $row['last_post_id'];
  1495. $post_ids[] = $row['last_post_id'];
  1496. }
  1497. $db->sql_freeresult($result);
  1498. // 5: Retrieve last_post infos
  1499. if (sizeof($post_ids))
  1500. {
  1501. $sql = 'SELECT p.post_id, p.poster_id, p.post_subject, p.post_time, p.post_username, u.username, u.user_colour
  1502. FROM ' . POSTS_TABLE . ' p, ' . USERS_TABLE . ' u
  1503. WHERE ' . $db->sql_in_set('p.post_id', $post_ids) . '
  1504. AND p.poster_id = u.user_id';
  1505. $result = $db->sql_query($sql);
  1506. while ($row = $db->sql_fetchrow($result))
  1507. {
  1508. $post_info[$row['post_id']] = $row;
  1509. }
  1510. $db->sql_freeresult($result);
  1511. foreach ($forum_data as $forum_id => $data)
  1512. {
  1513. if ($data['last_post_id'])
  1514. {
  1515. if (isset($post_info[$data['last_post_id']]))
  1516. {
  1517. $forum_data[$forum_id]['last_post_subject'] = $post_info[$data['last_post_id']]['post_subject'];
  1518. $forum_data[$forum_id]['last_post_time'] = $post_info[$data['last_post_id']]['post_time'];
  1519. $forum_data[$forum_id]['last_poster_id'] = $post_info[$data['last_post_id']]['poster_id'];
  1520. $forum_data[$forum_id]['last_poster_name'] = ($post_info[$data['last_post_id']]['poster_id'] != ANONYMOUS) ? $post_info[$data['last_post_id']]['username'] : $post_info[$data['last_post_id']]['post_username'];
  1521. $forum_data[$forum_id]['last_poster_colour'] = $post_info[$data['last_post_id']]['user_colour'];
  1522. }
  1523. else
  1524. {
  1525. // For some reason we did not find the post in the db
  1526. $forum_data[$forum_id]['last_post_id'] = 0;
  1527. $forum_data[$forum_id]['last_post_subject'] = '';
  1528. $forum_data[$forum_id]['last_post_time'] = 0;
  1529. $forum_data[$forum_id]['last_poster_id'] = 0;
  1530. $forum_data[$forum_id]['last_poster_name'] = '';
  1531. $forum_data[$forum_id]['last_poster_colour'] = '';
  1532. }
  1533. }
  1534. }
  1535. unset($post_info);
  1536. }
  1537. // 6: Now do that thing
  1538. $fieldnames = array('last_post_id', 'last_post_subject', 'last_post_time', 'last_poster_id', 'last_poster_name', 'last_poster_colour');
  1539. if ($sync_extra)
  1540. {
  1541. array_push($fieldnames, 'posts', 'topics', 'topics_real');
  1542. }
  1543. foreach ($forum_data as $forum_id => $row)
  1544. {
  1545. $sql_ary = array();
  1546. foreach ($fieldnames as $fieldname)
  1547. {
  1548. if ($row['forum_' . $fieldname] != $row[$fieldname])
  1549. {
  1550. if (preg_match('#(name|colour|subject)$#', $fieldname))
  1551. {
  1552. $sql_ary['forum_' . $fieldname] = (string) $row[$fieldname];
  1553. }
  1554. else
  1555. {
  1556. $sql_ary['forum_' . $fieldn

Large files files are truncated, but you can click here to view the full file