PageRenderTime 61ms CodeModel.GetById 24ms RepoModel.GetById 1ms app.codeStats 0ms

/wwwroot/phpbb/includes/functions_admin.php

https://github.com/spring/spring-website
PHP | 3191 lines | 2280 code | 494 blank | 417 comment | 385 complexity | f4809c7094994c4a879579303a3749e3 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1, Apache-2.0, LGPL-3.0, BSD-3-Clause

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

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

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