PageRenderTime 72ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 1ms

/forum/includes/functions_admin.php

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

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