PageRenderTime 54ms CodeModel.GetById 11ms RepoModel.GetById 0ms app.codeStats 1ms

/includes/functions_admin.php

http://seo-phpbb.googlecode.com/
PHP | 3061 lines | 2266 code | 469 blank | 326 comment | 394 complexity | d3051af4b4898d8bceeef9536e64c944 MD5 | raw file
Possible License(s): AGPL-1.0

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 8508 2008-04-20 04:58:29Z davidmj $
  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
  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'])
  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)
  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);
  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|topic|attach|user
  656. * @param mixed $ids can be: post_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. $sql_id = ($mode == 'user') ? 'poster_id' : (($mode == 'post') ? 'post_msg_id' : (($mode == 'topic') ? 'topic_id' : 'attach_id'));
  676. $post_ids = $topic_ids = $physical = array();
  677. // Collect post and topics ids for later use
  678. if ($mode == 'attach' || $mode == 'user' || ($mode == 'topic' && $resync))
  679. {
  680. $sql = 'SELECT post_msg_id as post_id, topic_id, physical_filename, thumbnail, filesize
  681. FROM ' . ATTACHMENTS_TABLE . '
  682. WHERE ' . $db->sql_in_set($sql_id, $ids);
  683. $result = $db->sql_query($sql);
  684. while ($row = $db->sql_fetchrow($result))
  685. {
  686. $post_ids[] = $row['post_id'];
  687. $topic_ids[] = $row['topic_id'];
  688. $physical[] = array('filename' => $row['physical_filename'], 'thumbnail' => $row['thumbnail'], 'filesize' => $row['filesize']);
  689. }
  690. $db->sql_freeresult($result);
  691. }
  692. if ($mode == 'post')
  693. {
  694. $sql = 'SELECT topic_id, physical_filename, thumbnail, filesize
  695. FROM ' . ATTACHMENTS_TABLE . '
  696. WHERE ' . $db->sql_in_set('post_msg_id', $ids) . '
  697. AND in_message = 0';
  698. $result = $db->sql_query($sql);
  699. while ($row = $db->sql_fetchrow($result))
  700. {
  701. $topic_ids[] = $row['topic_id'];
  702. $physical[] = array('filename' => $row['physical_filename'], 'thumbnail' => $row['thumbnail'], 'filesize' => $row['filesize']);
  703. }
  704. $db->sql_freeresult($result);
  705. }
  706. // Delete attachments
  707. $sql = 'DELETE FROM ' . ATTACHMENTS_TABLE . '
  708. WHERE ' . $db->sql_in_set($sql_id, $ids);
  709. $db->sql_query($sql);
  710. $num_deleted = $db->sql_affectedrows();
  711. if (!$num_deleted)
  712. {
  713. return 0;
  714. }
  715. // Delete attachments from filesystem
  716. $space_removed = $files_removed = 0;
  717. foreach ($physical as $file_ary)
  718. {
  719. if (phpbb_unlink($file_ary['filename'], 'file', true))
  720. {
  721. $space_removed += $file_ary['filesize'];
  722. $files_removed++;
  723. }
  724. if ($file_ary['thumbnail'])
  725. {
  726. phpbb_unlink($file_ary['filename'], 'thumbnail', true);
  727. }
  728. }
  729. set_config('upload_dir_size', $config['upload_dir_size'] - $space_removed, true);
  730. set_config('num_files', $config['num_files'] - $files_removed, true);
  731. if ($mode == 'topic' && !$resync)
  732. {
  733. return $num_deleted;
  734. }
  735. if ($mode == 'post')
  736. {
  737. $post_ids = $ids;
  738. }
  739. unset($ids);
  740. $post_ids = array_unique($post_ids);
  741. $topic_ids = array_unique($topic_ids);
  742. // Update post indicators
  743. if (sizeof($post_ids))
  744. {
  745. if ($mode == 'post' || $mode == 'topic')
  746. {
  747. $sql = 'UPDATE ' . POSTS_TABLE . '
  748. SET post_attachment = 0
  749. WHERE ' . $db->sql_in_set('post_id', $post_ids);
  750. $db->sql_query($sql);
  751. }
  752. if ($mode == 'user' || $mode == 'attach')
  753. {
  754. $remaining = array();
  755. $sql = 'SELECT post_msg_id
  756. FROM ' . ATTACHMENTS_TABLE . '
  757. WHERE ' . $db->sql_in_set('post_msg_id', $post_ids) . '
  758. AND in_message = 0';
  759. $result = $db->sql_query($sql);
  760. while ($row = $db->sql_fetchrow($result))
  761. {
  762. $remaining[] = $row['post_msg_id'];
  763. }
  764. $db->sql_freeresult($result);
  765. $unset_ids = array_diff($post_ids, $remaining);
  766. if (sizeof($unset_ids))
  767. {
  768. $sql = 'UPDATE ' . POSTS_TABLE . '
  769. SET post_attachment = 0
  770. WHERE ' . $db->sql_in_set('post_id', $unset_ids);
  771. $db->sql_query($sql);
  772. }
  773. $remaining = array();
  774. $sql = 'SELECT post_msg_id
  775. FROM ' . ATTACHMENTS_TABLE . '
  776. WHERE ' . $db->sql_in_set('post_msg_id', $post_ids) . '
  777. AND in_message = 1';
  778. $result = $db->sql_query($sql);
  779. while ($row = $db->sql_fetchrow($result))
  780. {
  781. $remaining[] = $row['post_msg_id'];
  782. }
  783. $db->sql_freeresult($result);
  784. $unset_ids = array_diff($post_ids, $remaining);
  785. if (sizeof($unset_ids))
  786. {
  787. $sql = 'UPDATE ' . PRIVMSGS_TABLE . '
  788. SET message_attachment = 0
  789. WHERE ' . $db->sql_in_set('msg_id', $unset_ids);
  790. $db->sql_query($sql);
  791. }
  792. }
  793. }
  794. if (sizeof($topic_ids))
  795. {
  796. // Update topic indicator
  797. if ($mode == 'topic')
  798. {
  799. $sql = 'UPDATE ' . TOPICS_TABLE . '
  800. SET topic_attachment = 0
  801. WHERE ' . $db->sql_in_set('topic_id', $topic_ids);
  802. $db->sql_query($sql);
  803. }
  804. if ($mode == 'post' || $mode == 'user' || $mode == 'attach')
  805. {
  806. $remaining = array();
  807. $sql = 'SELECT topic_id
  808. FROM ' . ATTACHMENTS_TABLE . '
  809. WHERE ' . $db->sql_in_set('topic_id', $topic_ids);
  810. $result = $db->sql_query($sql);
  811. while ($row = $db->sql_fetchrow($result))
  812. {
  813. $remaining[] = $row['topic_id'];
  814. }
  815. $db->sql_freeresult($result);
  816. $unset_ids = array_diff($topic_ids, $remaining);
  817. if (sizeof($unset_ids))
  818. {
  819. $sql = 'UPDATE ' . TOPICS_TABLE . '
  820. SET topic_attachment = 0
  821. WHERE ' . $db->sql_in_set('topic_id', $unset_ids);
  822. $db->sql_query($sql);
  823. }
  824. }
  825. }
  826. return $num_deleted;
  827. }
  828. /**
  829. * Remove topic shadows
  830. */
  831. function delete_topic_shadows($max_age, $forum_id = '', $auto_sync = true)
  832. {
  833. $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 : '');
  834. switch ($db->sql_layer)
  835. {
  836. case 'mysql4':
  837. case 'mysqli':
  838. $sql = 'DELETE t.*
  839. FROM ' . TOPICS_TABLE . ' t, ' . TOPICS_TABLE . ' t2
  840. WHERE t.topic_moved_id = t2.topic_id
  841. AND t.topic_time < ' . (time() - $max_age)
  842. . $where;
  843. $db->sql_query($sql);
  844. break;
  845. default:
  846. $sql = 'SELECT t.topic_id
  847. FROM ' . TOPICS_TABLE . ' t, ' . TOPICS_TABLE . ' t2
  848. WHERE t.topic_moved_id = t2.topic_id
  849. AND t.topic_time < ' . (time() - $max_age)
  850. . $where;
  851. $result = $db->sql_query($sql);
  852. $topic_ids = array();
  853. while ($row = $db->sql_fetchrow($result))
  854. {
  855. $topic_ids[] = $row['topic_id'];
  856. }
  857. $db->sql_freeresult($result);
  858. if (sizeof($topic_ids))
  859. {
  860. $sql = 'DELETE FROM ' . TOPICS_TABLE . '
  861. WHERE ' . $db->sql_in_set('topic_id', $topic_ids);
  862. $db->sql_query($sql);
  863. }
  864. break;
  865. }
  866. if ($auto_sync)
  867. {
  868. $where_type = ($forum_id) ? 'forum_id' : '';
  869. sync('forum', $where_type, $forum_id, true, true);
  870. }
  871. }
  872. /**
  873. * Update/Sync posted information for topics
  874. */
  875. function update_posted_info(&$topic_ids)
  876. {
  877. global $db, $config;
  878. if (empty($topic_ids) || !$config['load_db_track'])
  879. {
  880. return;
  881. }
  882. // First of all, let us remove any posted information for these topics
  883. $sql = 'DELETE FROM ' . TOPICS_POSTED_TABLE . '
  884. WHERE ' . $db->sql_in_set('topic_id', $topic_ids);
  885. $db->sql_query($sql);
  886. // Now, let us collect the user/topic combos for rebuilding the information
  887. $sql = 'SELECT poster_id, topic_id
  888. FROM ' . POSTS_TABLE . '
  889. WHERE ' . $db->sql_in_set('topic_id', $topic_ids) . '
  890. AND poster_id <> ' . ANONYMOUS . '
  891. GROUP BY poster_id, topic_id';
  892. $result = $db->sql_query($sql);
  893. $posted = array();
  894. while ($row = $db->sql_fetchrow($result))
  895. {
  896. // Add as key to make them unique (grouping by) and circumvent empty keys on array_unique
  897. $posted[$row['poster_id']][] = $row['topic_id'];
  898. }
  899. $db->sql_freeresult($result);
  900. // Now add the information...
  901. $sql_ary = array();
  902. foreach ($posted as $user_id => $topic_row)
  903. {
  904. foreach ($topic_row as $topic_id)
  905. {
  906. $sql_ary[] = array(
  907. 'user_id' => (int) $user_id,
  908. 'topic_id' => (int) $topic_id,
  909. 'topic_posted' => 1,
  910. );
  911. }
  912. }
  913. unset($posted);
  914. $db->sql_multi_insert(TOPICS_POSTED_TABLE, $sql_ary);
  915. }
  916. /**
  917. * Delete attached file
  918. */
  919. function phpbb_unlink($filename, $mode = 'file', $entry_removed = false)
  920. {
  921. global $db, $phpbb_root_path, $config;
  922. // Because of copying topics or modifications a physical filename could be assigned more than once. If so, do not remove the file itself.
  923. $sql = 'SELECT COUNT(attach_id) AS num_entries
  924. FROM ' . ATTACHMENTS_TABLE . "
  925. WHERE physical_filename = '" . $db->sql_escape(basename($filename)) . "'";
  926. $result = $db->sql_query($sql);
  927. $num_entries = (int) $db->sql_fetchfield('num_entries');
  928. $db->sql_freeresult($result);
  929. // Do not remove file if at least one additional entry with the same name exist.
  930. if (($entry_removed && $num_entries > 0) || (!$entry_removed && $num_entries > 1))
  931. {
  932. return false;
  933. }
  934. $filename = ($mode == 'thumbnail') ? 'thumb_' . basename($filename) : basename($filename);
  935. return @unlink($phpbb_root_path . $config['upload_path'] . '/' . $filename);
  936. }
  937. /**
  938. * All-encompasing sync function
  939. *
  940. * Exaples:
  941. * <code>
  942. * sync('topic', 'topic_id', 123); // resync topic #123
  943. * sync('topic', 'forum_id', array(2, 3)); // resync topics from forum #2 and #3
  944. * sync('topic'); // resync all topics
  945. * sync('topic', 'range', 'topic_id BETWEEN 1 AND 60'); // resync a range of topics/forums (only available for 'topic' and 'forum' modes)
  946. * </code>
  947. *
  948. * Modes:
  949. * - forum Resync complete forum
  950. * - topic Resync topics
  951. * - topic_moved Removes topic shadows that would be in the same forum as the topic they link to
  952. * - topic_approved Resyncs the topic_approved flag according to the status of the first post
  953. * - post_reported Resyncs the post_reported flag, relying on actual reports
  954. * - topic_reported Resyncs the topic_reported flag, relying on post_reported flags
  955. * - post_attachement Same as post_reported, but with attachment flags
  956. * - topic_attachement Same as topic_reported, but with attachment flags
  957. */
  958. function sync($mode, $where_type = '', $where_ids = '', $resync_parents = false, $sync_extra = false)
  959. {
  960. global $db;
  961. if (is_array($where_ids))
  962. {
  963. $where_ids = array_unique($where_ids);
  964. $where_ids = array_map('intval', $where_ids);
  965. }
  966. else if ($where_type != 'range')
  967. {
  968. $where_ids = ($where_ids) ? array((int) $where_ids) : array();
  969. }
  970. if ($mode == 'forum' || $mode == 'topic' || $mode == 'topic_approved' || $mode == 'topic_reported' || $mode == 'post_reported')
  971. {
  972. if (!$where_type)
  973. {
  974. $where_sql = '';
  975. $where_sql_and = 'WHERE';
  976. }
  977. else if ($where_type == 'range')
  978. {
  979. // Only check a range of topics/forums. For instance: 'topic_id BETWEEN 1 AND 60'
  980. $where_sql = 'WHERE (' . $mode[0] . ".$where_ids)";
  981. $where_sql_and = $where_sql . "\n\tAND";
  982. }
  983. else
  984. {
  985. // Do not sync the "global forum"
  986. $where_ids = array_diff($where_ids, array(0));
  987. if (!sizeof($where_ids))
  988. {
  989. // Empty array with IDs. This means that we don't have any work to do. Just return.
  990. return;
  991. }
  992. // Limit the topics/forums we are syncing, use specific topic/forum IDs.
  993. // $where_type contains the field for the where clause (forum_id, topic_id)
  994. $where_sql = 'WHERE ' . $db->sql_in_set($mode[0] . '.' . $where_type, $where_ids);
  995. $where_sql_and = $where_sql . "\n\tAND";
  996. }
  997. }
  998. else
  999. {
  1000. if (!sizeof($where_ids))
  1001. {
  1002. return;
  1003. }
  1004. // $where_type contains the field for the where clause (forum_id, topic_id)
  1005. $where_sql = 'WHERE ' . $db->sql_in_set($mode[0] . '.' . $where_type, $where_ids);
  1006. $where_sql_and = $where_sql . "\n\tAND";
  1007. }
  1008. switch ($mode)
  1009. {
  1010. case 'topic_moved':
  1011. switch ($db->sql_layer)
  1012. {
  1013. case 'mysql4':
  1014. case 'mysqli':
  1015. $sql = 'DELETE FROM ' . TOPICS_TABLE . '
  1016. USING ' . TOPICS_TABLE . ' t1, ' . TOPICS_TABLE . " t2
  1017. WHERE t1.topic_moved_id = t2.topic_id
  1018. AND t1.forum_id = t2.forum_id";
  1019. $db->sql_query($sql);
  1020. break;
  1021. default:
  1022. $sql = 'SELECT t1.topic_id
  1023. FROM ' .TOPICS_TABLE . ' t1, ' . TOPICS_TABLE . " t2
  1024. WHERE t1.topic_moved_id = t2.topic_id
  1025. AND t1.forum_id = t2.forum_id";
  1026. $result = $db->sql_query($sql);
  1027. $topic_id_ary = array();
  1028. while ($row = $db->sql_fetchrow($result))
  1029. {
  1030. $topic_id_ary[] = $row['topic_id'];
  1031. }
  1032. $db->sql_freeresult($result);
  1033. if (!sizeof($topic_id_ary))
  1034. {
  1035. return;
  1036. }
  1037. $sql = 'DELETE FROM ' . TOPICS_TABLE . '
  1038. WHERE ' . $db->sql_in_set('topic_id', $topic_id_ary);
  1039. $db->sql_query($sql);
  1040. break;
  1041. }
  1042. break;
  1043. case 'topic_approved':
  1044. switch ($db->sql_layer)
  1045. {
  1046. case 'mysql4':
  1047. case 'mysqli':
  1048. $sql = 'UPDATE ' . TOPICS_TABLE . ' t, ' . POSTS_TABLE . " p
  1049. SET t.topic_approved = p.post_approved
  1050. $where_sql_and t.topic_first_post_id = p.post_id";
  1051. $db->sql_query($sql);
  1052. break;
  1053. default:
  1054. $sql = 'SELECT t.topic_id, p.post_approved
  1055. FROM ' . TOPICS_TABLE . ' t, ' . POSTS_TABLE . " p
  1056. $where_sql_and p.post_id = t.topic_first_post_id
  1057. AND p.post_approved <> t.topic_approved";
  1058. $result = $db->sql_query($sql);
  1059. $topic_ids = array();
  1060. while ($row = $db->sql_fetchrow($result))
  1061. {
  1062. $topic_ids[] = $row['topic_id'];
  1063. }
  1064. $db->sql_freeresult($result);
  1065. if (!sizeof($topic_ids))
  1066. {
  1067. return;
  1068. }
  1069. $sql = 'UPDATE ' . TOPICS_TABLE . '
  1070. SET topic_approved = 1 - topic_approved
  1071. WHERE ' . $db->sql_in_set('topic_id', $topic_ids);
  1072. $db->sql_query($sql);
  1073. break;
  1074. }
  1075. break;
  1076. case 'post_reported':
  1077. $post_ids = $post_reported = array();
  1078. $sql = 'SELECT p.post_id, p.post_reported
  1079. FROM ' . POSTS_TABLE . " p
  1080. $where_sql
  1081. GROUP BY p.post_id, p.post_reported";
  1082. $result = $db->sql_query($sql);
  1083. while ($row = $db->sql_fetchrow($result))
  1084. {
  1085. $post_ids[$row['post_id']] = $row['post_id'];
  1086. if ($row['post_reported'])
  1087. {
  1088. $post_reported[$row['post_id']] = 1;
  1089. }
  1090. }
  1091. $db->sql_freeresult($result);
  1092. $sql = 'SELECT DISTINCT(post_id)
  1093. FROM ' . REPORTS_TABLE . '
  1094. WHERE ' . $db->sql_in_set('post_id', $post_ids) . '
  1095. AND report_closed = 0';
  1096. $result = $db->sql_query($sql);
  1097. $post_ids = array();
  1098. while ($row = $db->sql_fetchrow($result))
  1099. {
  1100. if (!isset($post_reported[$row['post_id']]))
  1101. {
  1102. $post_ids[] = $row['post_id'];
  1103. }
  1104. else
  1105. {
  1106. unset($post_reported[$row['post_id']]);
  1107. }
  1108. }
  1109. $db->sql_freeresult($result);
  1110. // $post_reported should be empty by now, if it's not it contains
  1111. // posts that are falsely flagged as reported
  1112. foreach ($post_reported as $post_id => $void)
  1113. {
  1114. $post_ids[] = $post_id;
  1115. }
  1116. if (sizeof($post_ids))
  1117. {
  1118. $sql = 'UPDATE ' . POSTS_TABLE . '
  1119. SET post_reported = 1 - post_reported
  1120. WHERE ' . $db->sql_in_set('post_id', $post_ids);
  1121. $db->sql_query($sql);
  1122. }
  1123. break;
  1124. case 'topic_reported':
  1125. if ($sync_extra)
  1126. {
  1127. sync('post_reported', $where_type, $where_ids);
  1128. }
  1129. $topic_ids = $topic_reported = array();
  1130. $sql = 'SELECT DISTINCT(t.topic_id)
  1131. FROM ' . POSTS_TABLE . " t
  1132. $where_sql_and t.post_reported = 1";
  1133. $result = $db->sql_query($sql);
  1134. while ($row = $db->sql_fetchrow($result))
  1135. {
  1136. $topic_reported[$row['topic_id']] = 1;
  1137. }
  1138. $db->sql_freeresult($result);
  1139. $sql = 'SELECT t.topic_id, t.topic_reported
  1140. FROM ' . TOPICS_TABLE . " t
  1141. $where_sql";
  1142. $result = $db->sql_query($sql);
  1143. while ($row = $db->sql_fetchrow($result))
  1144. {
  1145. if ($row['topic_reported'] ^ isset($topic_reported[$row['topic_id']]))
  1146. {
  1147. $topic_ids[] = $row['topic_id'];
  1148. }
  1149. }
  1150. $db->sql_freeresult($result);
  1151. if (sizeof($topic_ids))
  1152. {
  1153. $sql = 'UPDATE ' . TOPICS_TABLE . '
  1154. SET topic_reported = 1 - topic_reported
  1155. WHERE ' . $db->sql_in_set('topic_id', $topic_ids);
  1156. $db->sql_query($sql);
  1157. }
  1158. break;
  1159. case 'post_attachment':
  1160. $post_ids = $post_attachment = array();
  1161. $sql = 'SELECT p.post_id, p.post_attachment
  1162. FROM ' . POSTS_TABLE . " p
  1163. $where_sql
  1164. GROUP BY p.post_id, p.post_attachment";
  1165. $result = $db->sql_query($sql);
  1166. while ($row = $db->sql_fetchrow($result))
  1167. {
  1168. $post_ids[$row['post_id']] = $row['post_id'];
  1169. if ($row['post_attachment'])
  1170. {
  1171. $post_attachment[$row['post_id']] = 1;
  1172. }
  1173. }
  1174. $db->sql_freeresult($result);
  1175. $sql = 'SELECT DISTINCT(post_msg_id)
  1176. FROM ' . ATTACHMENTS_TABLE . '
  1177. WHERE ' . $db->sql_in_set('post_msg_id', $post_ids) . '
  1178. AND in_message = 0';
  1179. $result = $db->sql_query($sql);
  1180. $post_ids = array();
  1181. while ($row = $db->sql_fetchrow($result))
  1182. {
  1183. if (!isset($post_attachment[$row['post_msg_id']]))
  1184. {
  1185. $post_ids[] = $row['post_msg_id'];
  1186. }
  1187. else
  1188. {
  1189. unset($post_attachment[$row['post_msg_id']]);
  1190. }
  1191. }
  1192. $db->sql_freeresult($result);
  1193. // $post_attachment should be empty by now, if it's not it contains
  1194. // posts that are falsely flagged as having attachments
  1195. foreach ($post_attachment as $post_id => $void)
  1196. {
  1197. $post_ids[] = $post_id;
  1198. }
  1199. if (sizeof($post_ids))
  1200. {
  1201. $sql = 'UPDATE ' . POSTS_TABLE . '
  1202. SET post_attachment = 1 - post_attachment
  1203. WHERE ' . $db->sql_in_set('post_id', $post_ids);
  1204. $db->sql_query($sql);
  1205. }
  1206. break;
  1207. case 'topic_attachment':
  1208. if ($sync_extra)
  1209. {
  1210. sync('post_attachment', $where_type, $where_ids);
  1211. }
  1212. $topic_ids = $topic_attachment = array();
  1213. $sql = 'SELECT DISTINCT(t.topic_id)
  1214. FROM ' . POSTS_TABLE . " t
  1215. $where_sql_and t.post_attachment = 1";
  1216. $result = $db->sql_query($sql);
  1217. while ($row = $db->sql_fetchrow($result))
  1218. {
  1219. $topic_attachment[$row['topic_id']] = 1;
  1220. }
  1221. $db->sql_freeresult($result);
  1222. $sql = 'SELECT t.topic_id, t.topic_attachment
  1223. FROM ' . TOPICS_TABLE . " t
  1224. $where_sql";
  1225. $result = $db->sql_query($sql);
  1226. while ($row = $db->sql_fetchrow($result))
  1227. {
  1228. if ($row['topic_attachment'] ^ isset($topic_attachment[$row['topic_id']]))
  1229. {
  1230. $topic_ids[] = $row['topic_id'];
  1231. }
  1232. }
  1233. $db->sql_freeresult($result);
  1234. if (sizeof($topic_ids))
  1235. {
  1236. $sql = 'UPDATE ' . TOPICS_TABLE . '
  1237. SET topic_attachment = 1 - topic_attachment
  1238. WHERE ' . $db->sql_in_set('topic_id', $topic_ids);
  1239. $db->sql_query($sql);
  1240. }
  1241. break;
  1242. case 'forum':
  1243. // 1: Get the list of all forums
  1244. $sql = 'SELECT f.*
  1245. FROM ' . FORUMS_TABLE . " f
  1246. $where_sql";
  1247. $result = $db->sql_query($sql);
  1248. $forum_data = $forum_ids = $post_ids = $last_post_id = $post_info = array();
  1249. while ($row = $db->sql_fetchrow($result))
  1250. {
  1251. if ($row['forum_type'] == FORUM_LINK)
  1252. {
  1253. continue;
  1254. }
  1255. $forum_id = (int) $row['forum_id'];
  1256. $forum_ids[$forum_id] = $forum_id;
  1257. $forum_data[$forum_id] = $row;
  1258. if ($sync_extra)
  1259. {
  1260. $forum_data[$forum_id]['posts'] = 0;
  1261. $forum_data[$forum_id]['topics'] = 0;
  1262. $forum_data[$forum_id]['topics_real'] = 0;
  1263. }
  1264. $forum_data[$forum_id]['last_post_id'] = 0;
  1265. $forum_data[$forum_id]['last_post_subject'] = '';
  1266. $forum_data[$forum_id]['last_post_time'] = 0;
  1267. $forum_data[$forum_id]['last_poster_id'] = 0;
  1268. $forum_data[$forum_id]['last_poster_name'] = '';
  1269. $forum_data[$forum_id]['last_poster_colour'] = '';
  1270. }
  1271. $db->sql_freeresult($result);
  1272. if (!sizeof($forum_ids))
  1273. {
  1274. break;
  1275. }
  1276. $forum_ids = array_values($forum_ids);
  1277. // 2: Get topic counts for each forum (optional)
  1278. if ($sync_extra)
  1279. {
  1280. $sql = 'SELECT forum_id, topic_approved, COUNT(topic_id) AS forum_topics
  1281. FROM ' . TOPICS_TABLE . '
  1282. WHERE ' . $db->sql_in_set('forum_id', $forum_ids) . '
  1283. GROUP BY forum_id, topic_approved';
  1284. $result = $db->sql_query($sql);
  1285. while ($row = $db->sql_fetchrow($result))
  1286. {
  1287. $forum_id = (int) $row['forum_id'];
  1288. $forum_data[$forum_id]['topics_real'] += $row['forum_topics'];
  1289. if ($row['topic_approved'])
  1290. {
  1291. $forum_data[$forum_id]['topics'] = $row['forum_topics'];
  1292. }
  1293. }
  1294. $db->sql_freeresult($result);
  1295. }
  1296. // 3: Get post count for each forum (optional)
  1297. if ($sync_extra)
  1298. {
  1299. if (sizeof($forum_ids) == 1)
  1300. {
  1301. $sql = 'SELECT SUM(t.topic_replies + 1) AS forum_posts
  1302. FROM ' . TOPICS_TABLE . ' t
  1303. WHERE ' . $db->sql_in_set('t.forum_id', $forum_ids) . '
  1304. AND t.topic_approved = 1
  1305. AND t.topic_status <> ' . ITEM_MOVED;
  1306. }
  1307. else
  1308. {
  1309. $sql = 'SELECT t.forum_id, SUM(t.topic_replies + 1) AS forum_posts
  1310. FROM ' . TOPICS_TABLE . ' t
  1311. WHERE ' . $db->sql_in_set('t.forum_id', $forum_ids) . '
  1312. AND t.topic_approved = 1
  1313. AND t.topic_status <> ' . ITEM_MOVED . '
  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]['posts'] = (int) $row['forum_posts'];
  1321. }
  1322. $db->sql_freeresult($result);
  1323. }
  1324. // 4: Get last_post_id for each forum
  1325. if (sizeof($forum_ids) == 1)
  1326. {
  1327. $sql = 'SELECT MAX(t.topic_last_post_id) as last_post_id
  1328. FROM ' . TOPICS_TABLE . ' t
  1329. WHERE ' . $db->sql_in_set('t.forum_id', $forum_ids) . '
  1330. AND t.topic_approved = 1';
  1331. }
  1332. else
  1333. {
  1334. $sql = 'SELECT t.forum_id, MAX(t.topic_last_post_id) as last_post_id
  1335. FROM ' . TOPICS_TABLE . ' t
  1336. WHERE ' . $db->sql_in_set('t.forum_id', $forum_ids) . '
  1337. AND t.topic_approved = 1
  1338. GROUP BY t.forum_id';
  1339. }
  1340. $result = $db->sql_query($sql);
  1341. while ($row = $db->sql_fetchrow($result))
  1342. {
  1343. $forum_id = (sizeof($forum_ids) == 1) ? (int) $forum_ids[0] : (int) $row['forum_id'];
  1344. $forum_data[$forum_id]['last_post_id'] = (int) $row['last_post_id'];
  1345. $post_ids[] = $row['last_post_id'];
  1346. }
  1347. $db->sql_freeresult($result);
  1348. // 5: Retrieve last_post infos
  1349. if (sizeof($post_ids))
  1350. {
  1351. $sql = 'SELECT p.post_id, p.poster_id, p.post_subject, p.post_time, p.post_username, u.username, u.user_colour
  1352. FROM ' . POSTS_TABLE . ' p, ' . USERS_TABLE . ' u
  1353. WHERE ' . $db->sql_in_set('p.post_id', $post_ids) . '
  1354. AND p.poster_id = u.user_id';
  1355. $result = $db->sql_query($sql);
  1356. while ($row = $db->sql_fetchrow($result))
  1357. {
  1358. $post_info[$row['post_id']] = $row;
  1359. }
  1360. $db->sql_freeresult($result);
  1361. foreach ($forum_data as $forum_id => $data)
  1362. {
  1363. if ($data['last_post_id'])
  1364. {
  1365. if (isset($post_info[$data['last_post_id']]))
  1366. {
  1367. $forum_data[$forum_id]['last_post_subject'] = $post_info[$data['last_post_id']]['post_subject'];
  1368. $forum_data[$forum_id]['last_post_time'] = $post_info[$data['last_post_id']]['post_time'];
  1369. $forum_data[$forum_id]['last_poster_id'] = $post_info[$data['last_post_id']]['poster_id'];
  1370. $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'];
  1371. $forum_data[$forum_id]['last_poster_colour'] = $post_info[$data['last_post_id']]['user_colour'];
  1372. }
  1373. else
  1374. {
  1375. // For some reason we did not find the post in the db
  1376. $forum_data[$forum_id]['last_post_id'] = 0;
  1377. $forum_data[$forum_id]['last_post_subject'] = '';
  1378. $forum_data[$forum_id]['last_post_time'] = 0;
  1379. $forum_data[$forum_id]['last_poster_id'] = 0;
  1380. $forum_data[$forum_id]['last_poster_name'] = '';
  1381. $forum_data[$forum_id]['last_poster_colour'] = '';
  1382. }
  1383. }
  1384. }
  1385. unset($post_info);
  1386. }
  1387. // 6: Now do that thing
  1388. $fieldnames = array('last_post_id', 'last_post_subject', 'last_post_time', 'last_poster_id', 'last_poster_name', 'last_poster_colour');
  1389. if ($sync_extra)
  1390. {
  1391. array_push($fieldnames, 'posts', 'topics', 'topics_real');
  1392. }
  1393. foreach ($forum_data as $forum_id => $row)
  1394. {
  1395. $sql_ary = array();
  1396. foreach ($fieldnames as $fieldname)
  1397. {
  1398. if ($row['forum_' . $fieldname] != $row[$fieldname])
  1399. {
  1400. if (preg_match('#(name|colour|subject)$#', $fieldname))
  1401. {
  1402. $sql_ary['forum_' . $fieldname] = (string) $row[$fieldname];
  1403. }
  1404. else
  1405. {
  1406. $sql_ary['forum_' . $fieldname] = (int) $row[$fieldname];
  1407. }
  1408. }
  1409. }
  1410. if (sizeof($sql_ary))
  1411. {
  1412. $sql = 'UPDATE ' . FORUMS_TABLE . '
  1413. SET ' . $db->sql_build_array('UPDATE', $sql_ary) . '
  1414. WHERE forum_id = ' . $forum_id;
  1415. $db->sql_query($sql);
  1416. }
  1417. }
  1418. break;
  1419. case 'topic':
  1420. $topic_data = $post_ids = $approved_unapproved_ids = $resync_forums = $delete_topics = $delete_posts = $moved_topics = array();
  1421. $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
  1422. FROM ' . TOPICS_TABLE . " t
  1423. $where_sql";
  1424. $result = $db->sql_query($sql);
  1425. while ($row = $db->sql_fetchrow($result))
  1426. {
  1427. if ($row['topic_moved_id'])
  1428. {
  1429. $moved_topics[] = $row['topic_id'];
  1430. continue;
  1431. }
  1432. $topic_id = (int) $row['topic_id'];
  1433. $topic_data[$topic_id] = $row;
  1434. $topic_data[$topic_id]['replies_real'] = -1;
  1435. $topic_data[$topic_id]['replies'] = 0;
  1436. $topic_data[$topic_id]['first_post_id'] = 0;
  1437. $topic_data[$topic_id]['last_post_id'] = 0;
  1438. unset($topic_data[$topic_id]['topic_id']);
  1439. // This array holds all topic_ids
  1440. $delete_topics[$topic_id] = '';
  1441. if ($sync_extra)
  1442. {
  1443. $topic_data[$topic_id]['reported'] = 0;
  1444. $topic_data[$topic_id]['attachment'] = 0;
  1445. }
  1446. }
  1447. $db->sql_freeresult($result);
  1448. // Use "t" as table alias because of the $where_sql clause
  1449. // NOTE: 't.post_approved' in the GROUP BY is causing a major slowdown.
  1450. $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
  1451. FROM ' . POSTS_TABLE . " t
  1452. $where_sql
  1453. GROUP BY t.topic_id, t.post_approved";
  1454. $result = $db->sql_query($sql);
  1455. while ($row = $db->sql_fetchrow($result))
  1456. {
  1457. $topic_id = (int) $row['topic_id'];
  1458. $row['first_post_id'] = (int) $row['first_post_id'];
  1459. $row['last_post_id'] = (int) $row['last_post_id'];
  1460. if (!isset($topic_data[$topic_id]))
  1461. {
  1462. // Hey, these posts come from a topic that does not exist
  1463. $delete_posts[$topic_id] = '';
  1464. }
  1465. else
  1466. {
  1467. // Unset the corresponding entry in $delete_topics
  1468. // When we'll be done, only topics with no posts will remain
  1469. unset($delete_topics[$topic_id]);
  1470. $topic_data[$topic_id]['replies_real'] += $row['total_posts'];
  1471. $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']);
  1472. if ($row['post_approved'] || !$topic_data[$topic_id]['last_post_id'])
  1473. {
  1474. $topic_data[$topic_id]['replies'] = $row['total_posts'] - 1;
  1475. $topic_data[$topic_id]['last_post_id'] = $row['last_post_id'];
  1476. }
  1477. }
  1478. }
  1479. $db->sql_freeresult($result);
  1480. foreach ($topic_data as $topic_id => $row)
  1481. {
  1482. $post_ids[] = $row['first_post_id'];
  1483. if ($row['first_post_id'] != $row['last_post_id'])
  1484. {
  1485. $post_ids[] = $row['last_post_id'];
  1486. }
  1487. }
  1488. // Now we delete empty topics and orphan posts
  1489. if (sizeof($delete_posts))
  1490. {
  1491. delete_posts('topic_id', array_keys($delete_posts), false);
  1492. unset($delete_posts);
  1493. }
  1494. if (!sizeof($topic_data))
  1495. {
  1496. // If we get there, topic ids were invalid or topics did not contain any posts
  1497. delete_topics($where_type, $where_ids, true);
  1498. return;
  1499. }
  1500. if (sizeof($delete_topics))
  1501. {
  1502. $delete_topic_ids = array();
  1503. foreach ($delete_topics as $topic_id => $void)
  1504. {
  1505. unset($topic_data[$topic_id]);
  1506. $delete_topic_ids[] = $topic_id;
  1507. }
  1508. delete_topics('topic_id', $delete_topic_ids, false);
  1509. unset($delete_topics, $delete_topic_ids);
  1510. }
  1511. $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
  1512. FROM ' . POSTS_TABLE . ' p, ' . USERS_TABLE . ' u
  1513. WHERE ' . $db->sql_in_set('p.post_id', $post_ids) . '
  1514. AND u.user_id = p.poster_id';
  1515. $result = $db->sql_query($sql);
  1516. $post_ids = array();
  1517. while ($row = $db->sql_fetchrow($result))
  1518. {
  1519. $topic_id = intval($row['topic_id']);
  1520. if ($row['post_id'] == $topic_data[$topic_id]['first_post_id'])
  1521. {
  1522. if ($topic_data[$topic_id]['topic_approved'] != $row['post_approved'])
  1523. {
  1524. $approved_unapproved_ids[] = $topic_id;
  1525. }
  1526. $topic_data[$topic_id]['time'] = $row['post_time'];
  1527. $topic_data[$topic_id]['poster'] = $row['poster_id'];
  1528. $topic_data[$topic_id]['first_poster_name'] = ($row['poster_id'] == ANONYMOUS) ? $row['post_username'] : $row['username'];
  1529. $topic_data[$topic_id]['first_poster_colour'] = $row['user_colour'];
  1530. }
  1531. if ($row['post_id'] == $topic_data[$topic_id]['last_post_id'])
  1532. {
  1533. $topic_data[$topic_id]['last_poster_id'] = $row['poster_id'];
  1534. $topic_data[$topic_id]['last_post_subject'] = $row['post_subject'];
  1535. $topic_data[$topic_id]['last_post_time'] = $row['post_time'];
  1536. $topic_data[$topic_id]['last_poster_name'] = ($row['poster_id'] == ANONYMOUS) ? $row['post_username'] : $row['username'];
  1537. $topic_data[$topic_id]['last_poster_colour'] = $row['user_colour'];
  1538. }
  1539. }
  1540. $db->sql_freeresult($result);
  1541. // Make sure shadow topics do link to existing topics
  1542. if (sizeof($moved_topics))
  1543. {
  1544. $delete_topics = array();
  1545. $sql = 'SELECT t1.topic_id, t1.topic_moved_id
  1546. FROM ' . TOPICS_TABLE . ' t1
  1547. LEFT JOIN ' . TOPICS_TABLE . ' t2 ON (t2.topic_id = t1.topic_moved_id)
  1548. WHERE ' . $db->sql_in_set('t1.topic_id', $moved_topics) . '
  1549. AND t2.topic_id IS NULL';
  1550. $result = $db->sql_query($sql);
  1551. while ($row = $db->sql_fetchrow($result))
  1552. {
  1553. $delete_topics[] = $row['topic_id'];
  1554. }
  1555. $db->sql_freeresult($result);
  1556. if (sizeof($delete_topics))
  1557. {
  1558. delete_topics('topic_id', $delete_topics, false);
  1559. }
  1560. unset($delete_topics);
  1561. // Make sure shadow topics having no last post data being updated (this only rarely happens...)
  1562. $sql = 'SELECT topic_id, topic_moved_id, topic_last_post_id, topic_first_post_id
  1563. FROM ' . …

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