PageRenderTime 66ms CodeModel.GetById 28ms RepoModel.GetById 0ms app.codeStats 1ms

/includes/functions_admin.php

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

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