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

/phpBB3/includes/functions_admin.php

http://pbb-png1.googlecode.com/
PHP | 2298 lines | 1676 code | 340 blank | 282 comment | 292 complexity | 50eca867627626714195c613859e1b29 MD5 | raw file

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

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