PageRenderTime 87ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 1ms

/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
  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) $row['forum_id'];
  1549. if ($row['topic_visibility'] == ITEM_APPROVED)
  1550. {
  1551. $forum_data[$forum_id]['topics_approved'] = $row['total_topics'];
  1552. }
  1553. else if ($row['topic_visibility'] == ITEM_UNAPPROVED || $row['topic_visibility'] == ITEM_REAPPROVE)
  1554. {
  1555. $forum_data[$forum_id]['topics_unapproved'] = $row['total_topics'];
  1556. }
  1557. else if ($row['topic_visibility'] == ITEM_DELETED)
  1558. {
  1559. $forum_data[$forum_id]['topics_softdeleted'] = $row['total_topics'];
  1560. }
  1561. }
  1562. $db->sql_freeresult($result);
  1563. }
  1564. // 3: Get post count for each forum (optional)
  1565. if ($sync_extra)
  1566. {
  1567. if (sizeof($forum_ids) == 1)
  1568. {
  1569. $sql = 'SELECT SUM(t.topic_posts_approved) AS forum_posts_approved, SUM(t.topic_posts_unapproved) AS forum_posts_unapproved, SUM(t.topic_posts_softdeleted) AS forum_posts_softdeleted
  1570. FROM ' . TOPICS_TABLE . ' t
  1571. WHERE ' . $db->sql_in_set('t.forum_id', $forum_ids) . '
  1572. AND t.topic_status <> ' . ITEM_MOVED;
  1573. }
  1574. else
  1575. {
  1576. $sql = 'SELECT t.forum_id, SUM(t.topic_posts_approved) AS forum_posts_approved, SUM(t.topic_posts_unapproved) AS forum_posts_unapproved, SUM(t.topic_posts_softdeleted) AS forum_posts_softdeleted
  1577. FROM ' . TOPICS_TABLE . ' t
  1578. WHERE ' . $db->sql_in_set('t.forum_id', $forum_ids) . '
  1579. AND t.topic_status <> ' . ITEM_MOVED . '
  1580. GROUP BY t.forum_id';
  1581. }
  1582. $result = $db->sql_query($sql);
  1583. while ($row = $db->sql_fetchrow($result))
  1584. {
  1585. $forum_id = (sizeof($forum_ids) == 1) ? (int) $forum_ids[0] : (int) $row['forum_id'];
  1586. $forum_data[$forum_id]['posts_approved'] = (int) $row['forum_posts_approved'];
  1587. $forum_data[$forum_id]['posts_unapproved'] = (int) $row['forum_posts_unapproved'];
  1588. $forum_data[$forum_id]['posts_softdeleted'] = (int) $row['forum_posts_softdeleted'];
  1589. }
  1590. $db->sql_freeresult($result);
  1591. }
  1592. // 4: Get last_post_id for each forum
  1593. if (sizeof($forum_ids) == 1)
  1594. {
  1595. $sql = 'SELECT MAX(t.topic_last_post_id) as last_post_id
  1596. FROM ' . TOPICS_TABLE . ' t
  1597. WHERE ' . $db->sql_in_set('t.forum_id', $forum_ids) . '
  1598. AND t.topic_visibility = ' . ITEM_APPROVED;
  1599. }
  1600. else
  1601. {
  1602. $sql = 'SELECT t.forum_id, MAX(t.topic_last_post_id) as last_post_id
  1603. FROM ' . TOPICS_TABLE . ' t
  1604. WHERE ' . $db->sql_in_set('t.forum_id', $forum_ids) . '
  1605. AND t.topic_visibility = ' . ITEM_APPROVED . '
  1606. GROUP BY t.forum_id';
  1607. }
  1608. $result = $db->sql_query($sql);
  1609. while ($row = $db->sql_fetchrow($result))
  1610. {
  1611. $forum_id = (sizeof($forum_ids) == 1) ? (int) $forum_ids[0] : (int) $row['forum_id'];
  1612. $forum_data[$forum_id]['last_post_id'] = (int) $row['last_post_id'];
  1613. $post_ids[] = $row['last_post_id'];
  1614. }
  1615. $db->sql_freeresult($result);
  1616. // 5: Retrieve last_post infos
  1617. if (sizeof($post_ids))
  1618. {
  1619. $sql = 'SELECT p.post_id, p.poster_id, p.post_subject, p.post_time, p.post_username, u.username, u.user_colour
  1620. FROM ' . POSTS_TABLE . ' p, ' . USERS_TABLE . ' u
  1621. WHERE ' . $db->sql_in_set('p.post_id', $post_ids) . '
  1622. AND p.poster_id = u.user_id';
  1623. $result = $db->sql_query($sql);
  1624. while ($row = $db->sql_fetchrow($result))
  1625. {
  1626. $post_info[$row['post_id']] = $row;
  1627. }
  1628. $db->sql_freeresult($result);
  1629. foreach ($forum_data as $forum_id => $data)
  1630. {
  1631. if ($data['last_post_id'])
  1632. {
  1633. if (isset($post_info[$data['last_post_id']]))
  1634. {
  1635. $forum_data[$forum_id]['last_post_subject'] = $post_info[$data['last_post_id']]['post_subject'];
  1636. $forum_data[$forum_id]['last_post_time'] = $post_info[$data['last_post_id']]['post_time'];
  1637. $forum_data[$forum_id]['last_poster_id'] = $post_info[$data['last_post_id']]['poster_id'];
  1638. $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'];
  1639. $forum_data[$forum_id]['last_poster_colour'] = $post_info[$data['last_post_id']]['user_colour'];
  1640. }
  1641. else
  1642. {
  1643. // For some reason we did not find the post in the db
  1644. $forum_data[$forum_id]['last_post_id'] = 0;
  1645. $forum_data[$forum_id]['last_post_subject'] = '';
  1646. $forum_data[$forum_id]['last_post_time'] = 0;
  1647. $forum_data[$forum_id]['last_poster_id'] = 0;
  1648. $forum_data[$forum_id]['last_poster_name'] = '';
  1649. $forum_data[$forum_id]['last_poster_colour'] = '';
  1650. }
  1651. }
  1652. }
  1653. unset($post_info);
  1654. }
  1655. // 6: Now do that thing
  1656. $fieldnames = array('last_post_id', 'last_post_subject', 'last_post_time', 'last_poster_id', 'last_poster_name', 'last_poster_colour');
  1657. if ($sync_extra)
  1658. {
  1659. array_push($fieldnames, 'posts_approved', 'posts_unapproved', 'posts_softdeleted', 'topics_approved', 'topics_unapproved', 'topics_softdeleted');
  1660. }
  1661. foreach ($forum_data as $forum_id => $row)
  1662. {
  1663. $sql_ary = array();
  1664. foreach ($fieldnames as $fieldname)
  1665. {
  1666. if ($row['forum_' . $fieldname] != $row[$fieldname])
  1667. {
  1668. if (preg_match('#(name|colour|subject)$#', $fieldname))
  1669. {
  1670. $sql_ary['forum_' . $fieldname] = (string) $row[$fieldname];
  1671. }
  1672. else
  1673. {
  1674. $sql_ary['forum_' . $fieldname] = (int) $row[$fieldname];
  1675. }
  1676. }
  1677. }
  1678. if (sizeof($sql_ary))
  1679. {
  1680. $sql = 'UPDATE ' . FORUMS_TABLE . '
  1681. SET ' . $db->sql_build_array('UPDATE', $sql_ary) . '
  1682. WHERE forum_id = ' . $forum_id;
  1683. $db->sql_query($sql);
  1684. }
  1685. }
  1686. $db->sql_transaction('commit');
  1687. break;
  1688. case 'topic':
  1689. $topic_data = $post_ids = $resync_forums = $delete_topics = $delete_posts = $moved_topics = array();
  1690. $db->sql_transaction('begin');
  1691. $sql = 'SELECT t.topic_id, t.forum_id, t.topic_moved_id, t.topic_visibility, ' . (($sync_extra) ? 't.topic_attachment, t.topic_reported, ' : '') . 't.topic_poster, t.topic_time, t.topic_posts_approved, t.topic_posts_unapproved, t.topic_posts_softdeleted, 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
  1692. FROM ' . TOPICS_TABLE . " t
  1693. $where_sql";
  1694. $result = $db->sql_query($sql);
  1695. while ($row = $db->sql_fetchrow($result))
  1696. {
  1697. if ($row['topic_moved_id'])
  1698. {
  1699. $moved_topics[] = $row['topic_id'];
  1700. continue;
  1701. }
  1702. $topic_id = (int) $row['topic_id'];
  1703. $topic_data[$topic_id] = $row;
  1704. $topic_data[$topic_id]['visibility'] = ITEM_UNAPPROVED;
  1705. $topic_data[$topic_id]['posts_approved'] = 0;
  1706. $topic_data[$topic_id]['posts_unapproved'] = 0;
  1707. $topic_data[$topic_id]['posts_softdeleted'] = 0;
  1708. $topic_data[$topic_id]['first_post_id'] = 0;
  1709. $topic_data[$topic_id]['last_post_id'] = 0;
  1710. unset($topic_data[$topic_id]['topic_id']);
  1711. // This array holds all topic_ids
  1712. $delete_topics[$topic_id] = '';
  1713. if ($sync_extra)
  1714. {
  1715. $topic_data[$topic_id]['reported'] = 0;
  1716. $topic_data[$topic_id]['attachment'] = 0;
  1717. }
  1718. }
  1719. $db->sql_freeresult($result);
  1720. // Use "t" as table alias because of the $where_sql clause
  1721. // NOTE: 't.post_visibility' in the GROUP BY is causing a major slowdown.
  1722. $sql = 'SELECT t.topic_id, t.post_visibility, COUNT(t.post_id) AS total_posts, MIN(t.post_id) AS first_post_id, MAX(t.post_id) AS last_post_id
  1723. FROM ' . POSTS_TABLE . " t
  1724. $where_sql
  1725. GROUP BY t.topic_id, t.post_visibility";
  1726. $result = $db->sql_query($sql);
  1727. while ($row = $db->sql_fetchrow($result))
  1728. {
  1729. $topic_id = (int) $row['topic_id'];
  1730. $row['first_post_id'] = (int) $row['first_post_id'];
  1731. $row['last_post_id'] = (int) $row['last_post_id'];
  1732. if (!isset($topic_data[$topic_id]))
  1733. {
  1734. // Hey, these posts come from a topic that does not exist
  1735. $delete_posts[$topic_id] = '';
  1736. }
  1737. else
  1738. {
  1739. // Unset the corresponding entry in $delete_topics
  1740. // When we'll be done, only topics with no posts will remain
  1741. unset($delete_topics[$topic_id]);
  1742. if ($row['post_visibility'] == ITEM_APPROVED)
  1743. {
  1744. $topic_data[$topic_id]['posts_approved'] = $row['total_posts'];
  1745. }
  1746. else if ($row['post_visibility'] == ITEM_UNAPPROVED || $row['post_visibility'] == ITEM_REAPPROVE)
  1747. {
  1748. $topic_data[$topic_id]['posts_unapproved'] = $row['total_posts'];
  1749. }
  1750. else if ($row['post_visibility'] == ITEM_DELETED)
  1751. {
  1752. $topic_data[$topic_id]['posts_softdeleted'] = $row['total_posts'];
  1753. }
  1754. if ($row['post_visibility'] == ITEM_APPROVED)
  1755. {
  1756. $topic_data[$topic_id]['visibility'] = ITEM_APPROVED;
  1757. $topic_data[$topic_id]['first_post_id'] = $row['first_post_id'];
  1758. $topic_data[$topic_id]['last_post_id'] = $row['last_post_id'];
  1759. }
  1760. else if ($topic_data[$topic_id]['visibility'] != ITEM_APPROVED)
  1761. {
  1762. // If there is no approved post, we take the min/max of the other visibilities
  1763. // for the last and first post info, because it is only visible to moderators anyway
  1764. $topic_data[$topic_id]['first_post_id'] = (!empty($topic_data[$topic_id]['first_post_id'])) ? min($topic_data[$topic_id]['first_post_id'], $row['first_post_id']) : $row['first_post_id'];
  1765. $topic_data[$topic_id]['last_post_id'] = max($topic_data[$topic_id]['last_post_id'], $row['last_post_id']);
  1766. if ($topic_data[$topic_id]['visibility'] == ITEM_UNAPPROVED || $topic_data[$topic_id]['visibility'] == ITEM_REAPPROVE)
  1767. {
  1768. // Soft delete status is stronger than unapproved.
  1769. $topic_data[$topic_id]['visibility'] = $row['post_visibility'];
  1770. }
  1771. }
  1772. }
  1773. }
  1774. $db->sql_freeresult($result);
  1775. foreach ($topic_data as $topic_id => $row)
  1776. {
  1777. $post_ids[] = $row['first_post_id'];
  1778. if ($row['first_post_id'] != $row['last_post_id'])
  1779. {
  1780. $post_ids[] = $row['last_post_id'];
  1781. }
  1782. }
  1783. // Now we delete empty topics and orphan posts
  1784. if (sizeof($delete_posts))
  1785. {
  1786. delete_posts('topic_id', array_keys($delete_posts), false);
  1787. unset($delete_posts);
  1788. }
  1789. if (!sizeof($topic_data))
  1790. {
  1791. // If we get there, topic ids were invalid or topics did not contain any posts
  1792. delete_topics($where_type, $where_ids, true);
  1793. return;
  1794. }
  1795. if (sizeof($delete_topics))
  1796. {
  1797. $delete_topic_ids = array();
  1798. foreach ($delete_topics as $topic_id => $void)
  1799. {
  1800. unset($topic_data[$topic_id]);
  1801. $delete_topic_ids[] = $topic_id;
  1802. }
  1803. delete_topics('topic_id', $delete_topic_ids, false);
  1804. unset($delete_topics, $delete_topic_ids);
  1805. }
  1806. $sql = 'SELECT p.post_id, p.topic_id, p.post_visibility, p.poster_id, p.post_subject, p.post_username, p.post_time, u.username, u.user_colour
  1807. FROM ' . POSTS_TABLE . ' p, ' . USERS_TABLE . ' u
  1808. WHERE ' . $db->sql_in_set('p.post_id', $post_ids) . '
  1809. AND u.user_id = p.poster_id';
  1810. $result = $db->sql_query($sql);
  1811. $post_ids = array();
  1812. while ($row = $db->sql_fetchrow($result))
  1813. {
  1814. $topic_id = intval($row['topic_id']);
  1815. if ($row['post_id'] == $topic_data[$topic_id]['first_post_id'])
  1816. {
  1817. $topic_data[$topic_id]['time'] = $row['post_time'];
  1818. $topic_data[$topic_id]['poster'] = $row['poster_id'];
  1819. $topic_data[$topic_id]['first_poster_name'] = ($row['poster_id'] == ANONYMOUS) ? $row['post_username'] : $row['username'];
  1820. $topic_data[$topic_id]['first_poster_colour'] = $row['user_colour'];
  1821. }
  1822. if ($row['post_id'] == $topic_data[$topic_id]['last_post_id'])
  1823. {
  1824. $topic_data[$topic_id]['last_poster_id'] = $row['poster_id'];
  1825. $topic_data[$topic_id]['last_post_subject'] = $row['post_subject'];
  1826. $topic_data[$topic_id]['last_post_time'] = $row['post_time'];
  1827. $topic_data[$topic_id]['last_poster_name'] = ($row['poster_id'] == ANONYMOUS) ? $row['post_username'] : $row['username'];
  1828. $topic_data[$topic_id]['last_poster_colour'] = $row['user_colour'];
  1829. }
  1830. }
  1831. $db->sql_freeresult($result);
  1832. // Make sure shadow topics do link to existing topics
  1833. if (sizeof($moved_topics))
  1834. {
  1835. $delete_topics = array();
  1836. $sql = 'SELECT t1.topic_id, t1.topic_moved_id
  1837. FROM ' . TOPICS_TABLE . ' t1
  1838. LEFT JOIN ' . TOPICS_TABLE . ' t2 ON (t2.topic_id = t1.topic_moved_id)
  1839. WHERE ' . $db->sql_in_set('t1.topic_id', $moved_topics) . '
  1840. AND t2.topic_id IS NULL';
  1841. $result = $db->sql_query($sql);
  1842. while ($row = $db->sql_fetchrow($result))
  1843. {
  1844. $delete_topics[] = $row['topic_id'];
  1845. }
  1846. $db->sql_freeresult($result);
  1847. if (sizeof($delete_topics))
  1848. {
  1849. delete_topics('topic_id', $delete_topics, false);
  1850. }
  1851. unset($delete_topics);
  1852. // Make sure shadow topics having no last post data being updated (this only rarely happens...)
  1853. $sql = 'SELECT topic_id, topic_moved_id, topic_last_post_id, topic_first_post_id
  1854. FROM ' . TOPICS_TABLE . '
  1855. WHERE ' . $db->sql_in_set('topic_id', $moved_topics) . '
  1856. AND topic_last_post_time = 0';
  1857. $result = $db->sql_query($sql);
  1858. $shadow_topic_data = $post_ids = array();
  1859. while ($row = $db->sql_fetchrow($result))
  1860. {
  1861. $shadow_topic_data[$row['topic_moved_id']] = $row;
  1862. $post_ids[] = $row['topic_last_post_id'];
  1863. $post_ids[] = $row['topic_first_post_id'];
  1864. }
  1865. $db->sql_freeresult($result);
  1866. $sync_shadow_topics = array();
  1867. if (sizeof($post_ids))
  1868. {
  1869. $sql = 'SELECT p.post_id, p.topic_id, p.post_visibility, p.poster_id, p.post_subject, p.post_username, p.post_time, u.username, u.user_colour
  1870. FROM ' . POSTS_TABLE . ' p, ' . USERS_TABLE . ' u
  1871. WHERE ' . $db->sql_in_set('p.post_id', $post_ids) . '
  1872. AND u.user_id = p.poster_id';
  1873. $result = $db->sql_query($sql);
  1874. $post_ids = array();
  1875. while ($row = $db->sql_fetchrow($result))
  1876. {
  1877. $topic_id = (int) $row['topic_id'];
  1878. // Ok, there should be a shadow topic. If there isn't, then there's something wrong with the db.
  1879. // However, there's not much we can do about it.
  1880. if (!empty($shadow_topic_data[$topic_id]))
  1881. {
  1882. if ($row['post_id'] == $shadow_topic_data[$topic_id]['topic_first_post_id'])
  1883. {
  1884. $orig_topic_id = $shadow_topic_data[$topic_id]['topic_id'];
  1885. if (!isset($sync_shadow_topics[$orig_topic_id]))
  1886. {
  1887. $sync_shadow_topics[$orig_topic_id] = array();
  1888. }
  1889. $sync_shadow_topics[$orig_topic_id]['topic_time'] = $row['post_time'];
  1890. $sync_shadow_topics[$orig_topic_id]['topic_poster'] = $row['poster_id'];
  1891. $sync_shadow_topics[$orig_topic_id]['topic_first_poster_name'] = ($row['poster_id'] == ANONYMOUS) ? $row['post_username'] : $row['username'];
  1892. $sync_shadow_topics[$orig_topic_id]['topic_first_poster_colour'] = $row['user_colour'];
  1893. }
  1894. if ($row['post_id'] == $shadow_topic_data[$topic_id]['topic_last_post_id'])
  1895. {
  1896. $orig_topic_id = $shadow_topic_data[$topic_id]['topic_id'];
  1897. if (!isset($sync_shadow_topics[$orig_topic_id]))
  1898. {
  1899. $sync_shadow_topics[$orig_topic_id] = array();
  1900. }
  1901. $sync_shadow_topics[$orig_topic_id]['topic_last_poster_id'] = $row['poster_id'];
  1902. $sync_shadow_topics[$orig_topic_id]['topic_last_post_subject'] = $row['post_subject'];
  1903. $sync_shadow_topics[$orig_topic_id]['topic_last_post_time'] = $row['post_time'];
  1904. $sync_shadow_topics[$orig_topic_id]['topic_last_poster_name'] = ($row['poster_id'] == ANONYMOUS) ? $row['post_username'] : $row['username'];
  1905. $sync_shadow_topics[$orig_topic_id]['topic_last_poster_colour'] = $row['user_colour'];
  1906. }
  1907. }
  1908. }
  1909. $db->sql_freeresult($result);
  1910. $shadow_topic_data = array();
  1911. // Update the information we collected
  1912. if (sizeof($sync_shadow_topics))
  1913. {
  1914. foreach ($sync_shadow_topics as $sync_topic_id => $sql_ary)
  1915. {
  1916. $sql = 'UPDATE ' . TOPICS_TABLE . '
  1917. SET ' . $db->sql_build_array('UPDATE', $sql_ary) . '
  1918. WHERE topic_id = ' . $sync_topic_id;
  1919. $db->sql_query($sql);
  1920. }
  1921. }
  1922. }
  1923. unset($sync_shadow_topics, $shadow_topic_data);
  1924. }
  1925. // These are fields that will be synchronised
  1926. $fieldnames = array('time', 'visibility', 'posts_approved', 'posts_unapproved', 'posts_softdeleted', 'poster', 'first_post_id', 'first_poster_name', 'first_poster_colour', 'last_post_id', 'last_post_subject', 'last_post_time', 'last_poster_id', 'last_poster_name', 'last_poster_colour');
  1927. if ($sync_extra)
  1928. {
  1929. // This routine assumes that post_reported values are correct
  1930. // if they are not, use sync('post_reported') first
  1931. $sql = 'SELECT t.topic_id, p.post_id
  1932. FROM ' . TOPICS_TABLE . ' t, ' . POSTS_TABLE . " p
  1933. $where_sql_and p.topic_id = t.topic_id
  1934. AND p.post_reported = 1
  1935. GROUP BY t.topic_id, p.post_id";
  1936. $result = $db->sql_query($sql);
  1937. $fieldnames[] = 'reported';
  1938. while ($row = $db->sql_fetchrow($result))
  1939. {
  1940. $topic_data[intval($row['topic_id'])]['reported'] = 1;
  1941. }
  1942. $db->sql_freeresult($result);
  1943. // This routine assumes that post_attachment values are correct
  1944. // if they are not, use sync('post_attachment') first
  1945. $sql = 'SELECT t.topic_id, p.post_id
  1946. FROM ' . TOPICS_TABLE . ' t, ' . POSTS_TABLE . " p
  1947. $where_sql_and p.topic_id = t.topic_id
  1948. AND p.post_attachment = 1
  1949. GROUP BY t.topic_id, p.post_id";
  1950. $result = $db->sql_query($sql);
  1951. $fieldnames[] = 'attachment';
  1952. while ($row = $db->sql_fetchrow($result))
  1953. {
  1954. $topic_data[intval($row['topic_id'])]['attachment'] = 1;
  1955. }
  1956. $db->sql_freeresult($result);
  1957. }
  1958. foreach ($topic_data as $topic_id => $row)
  1959. {
  1960. $sql_ary = array();
  1961. foreach ($fieldnames as $fieldname)
  1962. {
  1963. if (isset($row[$fieldname]) && isset($row['topic_' . $fieldname]) && $row['topic_' . $fieldname] != $row[$fieldname])
  1964. {
  1965. $sql_ary['topic_' . $fieldname] = $row[$fieldname];
  1966. }
  1967. }
  1968. if (sizeof($sql_ary))
  1969. {
  1970. $sql = 'UPDATE ' . TOPICS_TABLE . '
  1971. SET ' . $db->sql_build_array('UPDATE', $sql_ary) . '
  1972. WHERE topic_id = ' . $topic_id;
  1973. $db->sql_query($sql);
  1974. $resync_forums[$row['forum_id']] = $row['forum_id'];
  1975. }
  1976. }
  1977. unset($topic_data);
  1978. $db->sql_transaction('commit');
  1979. // if some topics have been resync'ed then resync parent forums
  1980. // except when we're only syncing a range, we don't want to sync forums during
  1981. // batch processing.
  1982. if ($resync_parents && sizeof($resync_forums) && $where_type != 'range')
  1983. {
  1984. sync('forum', 'forum_id', array_values($resync_forums), true, true);
  1985. }
  1986. break;
  1987. }
  1988. return;
  1989. }
  1990. /**
  1991. * Prune function
  1992. */
  1993. function prune($forum_id, $prune_mode, $prune_date, $prune_flags = 0, $auto_sync = true)
  1994. {
  1995. global $db, $phpbb_dispatcher;
  1996. if (!is_array($forum_id))
  1997. {
  1998. $forum_id = array($forum_id);
  1999. }
  2000. if (!sizeof($forum_id))
  2001. {
  2002. return;
  2003. }
  2004. $sql_and = '';
  2005. if (!($prune_flags & FORUM_FLAG_PRUNE_ANNOUNCE))
  2006. {
  2007. $sql_and .= ' AND topic_type <> ' . POST_ANNOUNCE;
  2008. $sql_and .= ' AND topic_type <> ' . POST_GLOBAL;
  2009. }
  2010. if (!($prune_flags & FORUM_FLAG_PRUNE_STICKY))
  2011. {
  2012. $sql_and .= ' AND topic_type <> ' . POST_STICKY;
  2013. }
  2014. if ($prune_mode == 'posted')
  2015. {
  2016. $sql_and .= " AND topic_last_post_time < $prune_date";
  2017. }
  2018. if ($prune_mode == 'viewed')
  2019. {
  2020. $sql_and .= " AND topic_last_view_time < $prune_date";
  2021. }
  2022. if ($prune_mode == 'shadow')
  2023. {
  2024. $sql_and .= ' AND topic_status = ' . ITEM_MOVED . " AND topic_last_post_time < $prune_date";
  2025. }
  2026. /**
  2027. * Use this event to modify the SQL that selects topics to be pruned
  2028. *
  2029. * @event core.prune_sql
  2030. * @var string forum_id The forum id
  2031. * @var string prune_mode The prune mode
  2032. * @var string prune_date The prune date
  2033. * @var int prune_flags The prune flags
  2034. * @var bool auto_sync Whether or not to perform auto sync
  2035. * @var string sql_and SQL text appended to where clause
  2036. * @since 3.1.3-RC1
  2037. */
  2038. $vars = array('forum_id', 'prune_mode', 'prune_date', 'prune_flags', 'auto_sync', 'sql_and');
  2039. extract($phpbb_dispatcher->trigger_event('core.prune_sql', compact($vars)));
  2040. $sql = 'SELECT topic_id
  2041. FROM ' . TOPICS_TABLE . '
  2042. WHERE ' . $db->sql_in_set('forum_id', $forum_id) . "
  2043. AND poll_start = 0
  2044. $sql_and";
  2045. $result = $db->sql_query($sql);
  2046. $topic_list = array();
  2047. while ($row = $db->sql_fetchrow($result))
  2048. {
  2049. $topic_list[] = $row['topic_id'];
  2050. }
  2051. $db->sql_freeresult($result);
  2052. if ($prune_flags & FORUM_FLAG_PRUNE_POLL)
  2053. {
  2054. $sql = 'SELECT topic_id
  2055. FROM ' . TOPICS_TABLE . '
  2056. WHERE ' . $db->sql_in_set('forum_id', $forum_id) . "
  2057. AND poll_start > 0
  2058. AND poll_last_vote < $prune_date
  2059. $sql_and";
  2060. $result = $db->sql_query($sql);
  2061. while ($row = $db->sql_fetchrow($result))
  2062. {
  2063. $topic_list[] = $row['topic_id'];
  2064. }
  2065. $db->sql_freeresult($result);
  2066. $topic_list = array_unique($topic_list);
  2067. }
  2068. return delete_topics('topic_id', $topic_list, $auto_sync, false);
  2069. }
  2070. /**
  2071. * Function auto_prune(), this function now relies on passed vars
  2072. */
  2073. function auto_prune($forum_id, $prune_mode, $prune_flags, $prune_days, $prune_freq)
  2074. {
  2075. global $db;
  2076. $sql = 'SELECT forum_name
  2077. FROM ' . FORUMS_TABLE . "
  2078. WHERE forum_id = $forum_id";
  2079. $result = $db->sql_query($sql, 3600);
  2080. $row = $db->sql_fetchrow($result);
  2081. $db->sql_freeresult($result);
  2082. if ($row)
  2083. {
  2084. $prune_date = time() - ($prune_days * 86400);
  2085. $next_prune = time() + ($prune_freq * 86400);
  2086. prune($forum_id, $prune_mode, $prune_date, $prune_flags, true);
  2087. $sql = 'UPDATE ' . FORUMS_TABLE . "
  2088. SET prune_next = $next_prune
  2089. WHERE forum_id = $forum_id";
  2090. $db->sql_query($sql);
  2091. add_log('admin', 'LOG_AUTO_PRUNE', $row['forum_name']);
  2092. }
  2093. return;
  2094. }
  2095. /**
  2096. * Cache moderators. Called whenever permissions are changed
  2097. * via admin_permissions. Changes of usernames and group names
  2098. * must be carried through for the moderators table.
  2099. *
  2100. * @param \phpbb\db\driver\driver_interface $db Database connection
  2101. * @param \phpbb\cache\driver\driver_interface Cache driver
  2102. * @param \phpbb\auth\auth $auth Authentication object
  2103. * @return null
  2104. */
  2105. function phpbb_cache_moderators($db, $cache, $auth)
  2106. {
  2107. // Remove cached sql results
  2108. $cache->destroy('sql', MODERATOR_CACHE_TABLE);
  2109. // Clear table
  2110. switch ($db->get_sql_layer())
  2111. {
  2112. case 'sqlite':
  2113. case 'sqlite3':
  2114. $db->sql_query('DELETE FROM ' . MODERATOR_CACHE_TABLE);
  2115. break;
  2116. default:
  2117. $db->sql_query('TRUNCATE TABLE ' . MODERATOR_CACHE_TABLE);
  2118. break;
  2119. }
  2120. // We add moderators who have forum moderator permissions without an explicit ACL_NEVER setting
  2121. $hold_ary = $ug_id_ary = $sql_ary = array();
  2122. // Grab all users having moderative options...
  2123. $hold_ary = $auth->acl_user_raw_data(false, 'm_%', false);
  2124. // Add users?
  2125. if (sizeof($hold_ary))
  2126. {
  2127. // At least one moderative option warrants a display
  2128. $ug_id_ary = array_keys($hold_ary);
  2129. // Remove users who have group memberships with DENY moderator permissions
  2130. $sql_ary_deny = array(
  2131. 'SELECT' => 'a.forum_id, ug.user_id, g.group_id',
  2132. 'FROM' => array(
  2133. ACL_OPTIONS_TABLE => 'o',
  2134. USER_GROUP_TABLE => 'ug',
  2135. GROUPS_TABLE => 'g',
  2136. ACL_GROUPS_TABLE => 'a',
  2137. ),
  2138. 'LEFT_JOIN' => array(
  2139. array(
  2140. 'FROM' => array(ACL_ROLES_DATA_TABLE => 'r'),
  2141. 'ON' => 'a.auth_role_id = r.role_id',
  2142. ),
  2143. ),
  2144. 'WHERE' => '(o.auth_option_id = a.auth_option_id OR o.auth_option_id = r.auth_option_id)
  2145. AND ((a.auth_setting = ' . ACL_NEVER . ' AND r.auth_setting IS NULL)
  2146. OR r.auth_setting = ' . ACL_NEVER . ')
  2147. AND a.group_id = ug.group_id
  2148. AND g.group_id = ug.group_id
  2149. AND NOT (ug.group_leader = 1 AND g.group_skip_auth = 1)
  2150. AND ' . $db->sql_in_set('ug.user_id', $ug_id_ary) . "
  2151. AND ug.user_pending = 0
  2152. AND o.auth_option " . $db->sql_like_expression('m_' . $db->get_any_char()),
  2153. );
  2154. $sql = $db->sql_build_query('SELECT', $sql_ary_deny);
  2155. $result = $db->sql_query($sql);
  2156. while ($row = $db->sql_fetchrow($result))
  2157. {
  2158. if (isset($hold_ary[$row['user_id']][$row['forum_id']]))
  2159. {
  2160. unset($hold_ary[$row['user_id']][$row['forum_id']]);
  2161. }
  2162. }
  2163. $db->sql_freeresult($result);
  2164. if (sizeof($hold_ary))
  2165. {
  2166. // Get usernames...
  2167. $sql = 'SELECT user_id, username
  2168. FROM ' . USERS_TABLE . '
  2169. WHERE ' . $db->sql_in_set('user_id', array_keys($hold_ary));
  2170. $result = $db->sql_query($sql);
  2171. $usernames_ary = array();
  2172. while ($row = $db->sql_fetchrow($result))
  2173. {
  2174. $usernames_ary[$row['user_id']] = $row['username'];
  2175. }
  2176. $db->sql_freeresult($result);
  2177. foreach ($hold_ary as $user_id => $forum_id_ary)
  2178. {
  2179. // Do not continue if user does not exist
  2180. if (!isset($usernames_ary[$user_id]))
  2181. {
  2182. continue;
  2183. }
  2184. foreach ($forum_id_ary as $forum_id => $auth_ary)
  2185. {
  2186. $sql_ary[] = array(
  2187. 'forum_id' => (int) $forum_id,
  2188. 'user_id' => (int) $user_id,
  2189. 'username' => (string) $usernames_ary[$user_id],
  2190. 'group_id' => 0,
  2191. 'group_name' => ''
  2192. );
  2193. }
  2194. }
  2195. }
  2196. }
  2197. // Now to the groups...
  2198. $hold_ary = $auth->acl_group_raw_data(false, 'm_%', false);
  2199. if (sizeof($hold_ary))
  2200. {
  2201. $ug_id_ary = array_keys($hold_ary);
  2202. // Make sure not hidden or special groups are involved...
  2203. $sql = 'SELECT group_name, group_id, group_type
  2204. FROM ' . GROUPS_TABLE . '
  2205. WHERE ' . $db->sql_in_set('group_id', $ug_id_ary);
  2206. $result = $db->sql_query($sql);
  2207. $groupnames_ary = array();
  2208. while ($row = $db->sql_fetchrow($result))
  2209. {
  2210. if ($row['group_type'] == GROUP_HIDDEN || $row['group_type'] == GROUP_SPECIAL)
  2211. {
  2212. unset($hold_ary[$row['group_id']]);
  2213. }
  2214. $groupnames_ary[$row['group_id']] = $row['group_name'];
  2215. }
  2216. $db->sql_freeresult($result);
  2217. foreach ($hold_ary as $group_id => $forum_id_ary)
  2218. {
  2219. // If there is no group, we do not assign it...
  2220. if (!isset($groupnames_ary[$group_id]))
  2221. {
  2222. continue;
  2223. }
  2224. foreach ($forum_id_ary as $forum_id => $auth_ary)
  2225. {
  2226. $flag = false;
  2227. foreach ($auth_ary as $auth_option => $setting)
  2228. {
  2229. // Make sure at least one ACL_YES option is set...
  2230. if ($setting == ACL_YES)
  2231. {
  2232. $flag = true;
  2233. break;
  2234. }
  2235. }
  2236. if (!$flag)
  2237. {
  2238. continue;
  2239. }
  2240. $sql_ary[] = array(
  2241. 'forum_id' => (int) $forum_id,
  2242. 'user_id' => 0,
  2243. 'username' => '',
  2244. 'group_id' => (int) $group_id,
  2245. 'group_name' => (string) $groupnames_ary[$group_id]
  2246. );
  2247. }
  2248. }
  2249. }
  2250. $db->sql_multi_insert(MODERATOR_CACHE_TABLE, $sql_ary);
  2251. }
  2252. /**
  2253. * View log
  2254. *
  2255. * @param string $mode The mode defines which log_type is used and from which log the entry is retrieved
  2256. * @param array &$log The result array with the logs
  2257. * @param mixed &$log_count If $log_count is set to false, we will skip counting all entries in the database.
  2258. * Otherwise an integer with the number of total matching entries is returned.
  2259. * @param int $limit Limit the number of entries that are returned
  2260. * @param int $offset Offset when fetching the log entries, f.e. when paginating
  2261. * @param mixed $forum_id Restrict the log entries to the given forum_id (can also be an array of forum_ids)
  2262. * @param int $topic_id Restrict the log entries to the given topic_id
  2263. * @param int $user_id Restrict the log entries to the given user_id
  2264. * @param int $log_time Only get log entries newer than the given timestamp
  2265. * @param string $sort_by SQL order option, e.g. 'l.log_time DESC'
  2266. * @param string $keywords Will only return log entries that have the keywords in log_operation or log_data
  2267. *
  2268. * @return int Returns the offset of the last valid page, if the specified offset was invalid (too high)
  2269. */
  2270. function view_log($mode, &$log, &$log_count, $limit = 0, $offset = 0, $forum_id = 0, $topic_id = 0, $user_id = 0, $limit_days = 0, $sort_by = 'l.log_time DESC', $keywords = '')
  2271. {
  2272. global $phpbb_log;
  2273. $count_logs = ($log_count !== false);
  2274. $log = $phpbb_log->get_logs($mode, $count_logs, $limit, $offset, $forum_id, $topic_id, $user_id, $limit_days, $sort_by, $keywords);
  2275. $log_count = $phpbb_log->get_log_count();
  2276. return $phpbb_log->get_valid_offset();
  2277. }
  2278. /**
  2279. * Removes moderators and administrators from foe lists.
  2280. *
  2281. * @param \phpbb\db\driver\driver_interface $db Database connection
  2282. * @param \phpbb\auth\auth $auth Authentication object
  2283. * @param array|bool $group_id If an array, remove all members of this group from foe lists, or false to ignore
  2284. * @param array|bool $user_id If an array, remove this user from foe lists, or false to ignore
  2285. * @return null
  2286. */
  2287. function phpbb_update_foes($db, $auth, $group_id = false, $user_id = false)
  2288. {
  2289. // update foes for some user
  2290. if (is_array($user_id) && sizeof($user_id))
  2291. {
  2292. $sql = 'DELETE FROM ' . ZEBRA_TABLE . '
  2293. WHERE ' . $db->sql_in_set('zebra_id', $user_id) . '
  2294. AND foe = 1';
  2295. $db->sql_query($sql);
  2296. return;
  2297. }
  2298. // update foes for some group
  2299. if (is_array($group_id) && sizeof($group_id))
  2300. {
  2301. // Grab group settings...
  2302. $sql_ary = array(
  2303. 'SELECT' => 'a.group_id',
  2304. 'FROM' => array(
  2305. ACL_OPTIONS_TABLE => 'ao',
  2306. ACL_GROUPS_TABLE => 'a',
  2307. ),
  2308. 'LEFT_JOIN' => array(
  2309. array(
  2310. 'FROM' => array(ACL_ROLES_DATA_TABLE => 'r'),
  2311. 'ON' => 'a.auth_role_id = r.role_id',
  2312. ),
  2313. ),
  2314. 'WHERE' => '(ao.auth_option_id = a.auth_option_id OR ao.auth_option_id = r.auth_option_id)
  2315. AND ' . $db->sql_in_set('a.group_id', $group_id) . "
  2316. AND ao.auth_option IN ('a_', 'm_')",
  2317. 'GROUP_BY' => 'a.group_id',
  2318. );
  2319. $sql = $db->sql_build_query('SELECT', $sql_ary);
  2320. $result = $db->sql_query($sql);
  2321. $groups = array();
  2322. while ($row = $db->sql_fetchrow($result))
  2323. {
  2324. $groups[] = (int) $row['group_id'];
  2325. }
  2326. $db->sql_freeresult($result);
  2327. if (!sizeof($groups))
  2328. {
  2329. return;
  2330. }
  2331. switch ($db->get_sql_layer())
  2332. {
  2333. case 'mysqli':
  2334. case 'mysql4':
  2335. $sql = 'DELETE ' . (($db->get_sql_layer() === 'mysqli' || version_compare($db->sql_server_info(true), '4.1', '>=')) ? 'z.*' : ZEBRA_TABLE) . '
  2336. FROM ' . ZEBRA_TABLE . ' z, ' . USER_GROUP_TABLE . ' ug
  2337. WHERE z.zebra_id = ug.user_id
  2338. AND z.foe = 1
  2339. AND ' . $db->sql_in_set('ug.group_id', $groups);
  2340. $db->sql_query($sql);
  2341. break;
  2342. default:
  2343. $sql = 'SELECT user_id
  2344. FROM ' . USER_GROUP_TABLE . '
  2345. WHERE ' . $db->sql_in_set('group_id', $groups);
  2346. $result = $db->sql_query($sql);
  2347. $users = array();
  2348. while ($row = $db->sql_fetchrow($result))
  2349. {
  2350. $users[] = (int) $row['user_id'];
  2351. }
  2352. $db->sql_freeresult($result);
  2353. if (sizeof($users))
  2354. {
  2355. $sql = 'DELETE FROM ' . ZEBRA_TABLE . '
  2356. WHERE ' . $db->sql_in_set('zebra_id', $users) . '
  2357. AND foe = 1';
  2358. $db->sql_query($sql);
  2359. }
  2360. break;
  2361. }
  2362. return;
  2363. }
  2364. // update foes for everyone
  2365. $perms = array();
  2366. foreach ($auth->acl_get_list(false, array('a_', 'm_'), false) as $forum_id => $forum_ary)
  2367. {
  2368. foreach ($forum_ary as $auth_option => $user_ary)
  2369. {
  2370. $perms = array_merge($perms, $user_ary);
  2371. }
  2372. }
  2373. if (sizeof($perms))
  2374. {
  2375. $sql = 'DELETE FROM ' . ZEBRA_TABLE . '
  2376. WHERE ' . $db->sql_in_set('zebra_id', array_unique($perms)) . '
  2377. AND foe = 1';
  2378. $db->sql_query($sql);
  2379. }
  2380. unset($perms);
  2381. }
  2382. /**
  2383. * Lists inactive users
  2384. */
  2385. function view_inactive_users(&$users, &$user_count, $limit = 0, $offset = 0, $limit_days = 0, $sort_by = 'user_inactive_time DESC')
  2386. {
  2387. global $db, $user;
  2388. $sql = 'SELECT COUNT(user_id) AS user_count
  2389. FROM ' . USERS_TABLE . '
  2390. WHERE user_type = ' . USER_INACTIVE .
  2391. (($limit_days) ? " AND user_inactive_time >= $limit_days" : '');
  2392. $result = $db->sql_query($sql);
  2393. $user_count = (int) $db->sql_fetchfield('user_count');
  2394. $db->sql_freeresult($result);
  2395. if ($user_count == 0)
  2396. {
  2397. // Save the queries, because there are no users to display
  2398. return 0;
  2399. }
  2400. if ($offset >= $user_count)
  2401. {
  2402. $offset = ($offset - $limit < 0) ? 0 : $offset - $limit;
  2403. }
  2404. $sql = 'SELECT *
  2405. FROM ' . USERS_TABLE . '
  2406. WHERE user_type = ' . USER_INACTIVE .
  2407. (($limit_days) ? " AND user_inactive_time >= $limit_days" : '') . "
  2408. ORDER BY $sort_by";
  2409. $result = $db->sql_query_limit($sql, $limit, $offset);
  2410. while ($row = $db->sql_fetchrow($result))
  2411. {
  2412. $row['inactive_reason'] = $user->lang['INACTIVE_REASON_UNKNOWN'];
  2413. switch ($row['user_inactive_reason'])
  2414. {
  2415. case INACTIVE_REGISTER:
  2416. $row['inactive_reason'] = $user->lang['INACTIVE_REASON_REGISTER'];
  2417. break;
  2418. case INACTIVE_PROFILE:
  2419. $row['inactive_reason'] = $user->lang['INACTIVE_REASON_PROFILE'];
  2420. break;
  2421. case INACTIVE_MANUAL:
  2422. $row['inactive_reason'] = $user->lang['INACTIVE_REASON_MANUAL'];
  2423. break;
  2424. case INACTIVE_REMIND:
  2425. $row['inactive_reason'] = $user->lang['INACTIVE_REASON_REMIND'];
  2426. break;
  2427. }
  2428. $users[] = $row;
  2429. }
  2430. $db->sql_freeresult($result);
  2431. return $offset;
  2432. }
  2433. /**
  2434. * Lists warned users
  2435. */
  2436. function view_warned_users(&$users, &$user_count, $limit = 0, $offset = 0, $limit_days = 0, $sort_by = 'user_warnings DESC')
  2437. {
  2438. global $db;
  2439. $sql = 'SELECT user_id, username, user_colour, user_warnings, user_last_warning
  2440. FROM ' . USERS_TABLE . '
  2441. WHERE user_warnings > 0
  2442. ' . (($limit_days) ? "AND user_last_warning >= $limit_days" : '') . "
  2443. ORDER BY $sort_by";
  2444. $result = $db->sql_query_limit($sql, $limit, $offset);
  2445. $users = $db->sql_fetchrowset($result);
  2446. $db->sql_freeresult($result);
  2447. $sql = 'SELECT count(user_id) AS user_count
  2448. FROM ' . USERS_TABLE . '
  2449. WHERE user_warnings > 0
  2450. ' . (($limit_days) ? "AND user_last_warning >= $limit_days" : '');
  2451. $result = $db->sql_query($sql);
  2452. $user_count = (int) $db->sql_fetchfield('user_count');
  2453. $db->sql_freeresult($result);
  2454. return;
  2455. }
  2456. /**
  2457. * Get database size
  2458. * Currently only mysql and mssql are supported
  2459. */
  2460. function get_database_size()
  2461. {
  2462. global $db, $user, $table_prefix;
  2463. $database_size = false;
  2464. // This code is heavily influenced by a similar routine in phpMyAdmin 2.2.0
  2465. switch ($db->get_sql_layer())
  2466. {
  2467. case 'mysql':
  2468. case 'mysql4':
  2469. case 'mysqli':
  2470. $sql = 'SELECT VERSION() AS mysql_version';
  2471. $result = $db->sql_query($sql);
  2472. $row = $db->sql_fetchrow($result);
  2473. $db->sql_freeresult($result);
  2474. if ($row)
  2475. {
  2476. $version = $row['mysql_version'];
  2477. if (preg_match('#(3\.23|[45]\.)#', $version))
  2478. {
  2479. $db_name = (preg_match('#^(?:3\.23\.(?:[6-9]|[1-9]{2}))|[45]\.#', $version)) ? "`{$db->get_db_name()}`" : $db->get_db_name();
  2480. $sql = 'SHOW TABLE STATUS
  2481. FROM ' . $db_name;
  2482. $result = $db->sql_query($sql, 7200);
  2483. $database_size = 0;
  2484. while ($row = $db->sql_fetchrow($result))
  2485. {
  2486. if ((isset($row['Type']) && $row['Type'] != 'MRG_MyISAM') || (isset($row['Engine']) && ($row['Engine'] == 'MyISAM' || $row['Engine'] == 'InnoDB')))
  2487. {
  2488. if ($table_prefix != '')
  2489. {
  2490. if (strpos($row['Name'], $table_prefix) !== false)
  2491. {
  2492. $database_size += $row['Data_length'] + $row['Index_length'];
  2493. }
  2494. }
  2495. else
  2496. {
  2497. $database_size += $row['Data_length'] + $row['Index_length'];
  2498. }
  2499. }
  2500. }
  2501. $db->sql_freeresult($result);
  2502. }
  2503. }
  2504. break;
  2505. case 'sqlite':
  2506. case 'sqlite3':
  2507. global $dbhost;
  2508. if (file_exists($dbhost))
  2509. {
  2510. $database_size = filesize($dbhost);
  2511. }
  2512. break;
  2513. case 'mssql':
  2514. case 'mssql_odbc':
  2515. case 'mssqlnative':
  2516. $sql = 'SELECT @@VERSION AS mssql_version';
  2517. $result = $db->sql_query($sql);
  2518. $row = $db->sql_fetchrow($result);
  2519. $db->sql_freeresult($result);
  2520. $sql = 'SELECT ((SUM(size) * 8.0) * 1024.0) as dbsize
  2521. FROM sysfiles';
  2522. if ($row)
  2523. {
  2524. // Azure stats are stored elsewhere
  2525. if (strpos($row['mssql_version'], 'SQL Azure') !== false)
  2526. {
  2527. $sql = 'SELECT ((SUM(reserved_page_count) * 8.0) * 1024.0) as dbsize
  2528. FROM sys.dm_db_partition_stats';
  2529. }
  2530. }
  2531. $result = $db->sql_query($sql, 7200);
  2532. $database_size = ($row = $db->sql_fetchrow($result)) ? $row['dbsize'] : false;
  2533. $db->sql_freeresult($result);
  2534. break;
  2535. case 'postgres':
  2536. $sql = "SELECT proname
  2537. FROM pg_proc
  2538. WHERE proname = 'pg_database_size'";
  2539. $result = $db->sql_query($sql);
  2540. $row = $db->sql_fetchrow($result);
  2541. $db->sql_freeresult($result);
  2542. if ($row['proname'] == 'pg_database_size')
  2543. {
  2544. $database = $db->get_db_name();
  2545. if (strpos($database, '.') !== false)
  2546. {
  2547. list($database, ) = explode('.', $database);
  2548. }
  2549. $sql = "SELECT oid
  2550. FROM pg_database
  2551. WHERE datname = '$database'";
  2552. $result = $db->sql_query($sql);
  2553. $row = $db->sql_fetchrow($result);
  2554. $db->sql_freeresult($result);
  2555. $oid = $row['oid'];
  2556. $sql = 'SELECT pg_database_size(' . $oid . ') as size';
  2557. $result = $db->sql_query($sql);
  2558. $row = $db->sql_fetchrow($result);
  2559. $db->sql_freeresult($result);
  2560. $database_size = $row['size'];
  2561. }
  2562. break;
  2563. case 'oracle':
  2564. $sql = 'SELECT SUM(bytes) as dbsize
  2565. FROM user_segments';
  2566. $result = $db->sql_query($sql, 7200);
  2567. $database_size = ($row = $db->sql_fetchrow($result)) ? $row['dbsize'] : false;
  2568. $db->sql_freeresult($result);
  2569. break;
  2570. }
  2571. $database_size = ($database_size !== false) ? get_formatted_filesize($database_size) : $user->lang['NOT_AVAILABLE'];
  2572. return $database_size;
  2573. }
  2574. /**
  2575. * Retrieve contents from remotely stored file
  2576. *
  2577. * @deprecated 3.1.2 Use file_downloader instead
  2578. */
  2579. function get_remote_file($host, $directory, $filename, &$errstr, &$errno, $port = 80, $timeout = 6)
  2580. {
  2581. global $phpbb_container;
  2582. // Get file downloader and assign $errstr and $errno
  2583. $file_downloader = $phpbb_container->get('file_downloader');
  2584. $file_data = $file_downloader->get($host, $directory, $filename, $port, $timeout);
  2585. $errstr = $file_downloader->get_error_string();
  2586. $errno = $file_downloader->get_error_number();
  2587. return $file_data;
  2588. }
  2589. /*
  2590. * Tidy Warnings
  2591. * Remove all warnings which have now expired from the database
  2592. * The duration of a warning can be defined by the administrator
  2593. * This only removes the warning and reduces the associated count,
  2594. * it does not remove the user note recording the contents of the warning
  2595. */
  2596. function tidy_warnings()
  2597. {
  2598. global $db, $config;
  2599. $expire_date = time() - ($config['warnings_expire_days'] * 86400);
  2600. $warning_list = $user_list = array();
  2601. $sql = 'SELECT * FROM ' . WARNINGS_TABLE . "
  2602. WHERE warning_time < $expire_date";
  2603. $result = $db->sql_query($sql);
  2604. while ($row = $db->sql_fetchrow($result))
  2605. {
  2606. $warning_list[] = $row['warning_id'];
  2607. $user_list[$row['user_id']] = isset($user_list[$row['user_id']]) ? ++$user_list[$row['user_id']] : 1;
  2608. }
  2609. $db->sql_freeresult($result);
  2610. if (sizeof($warning_list))
  2611. {
  2612. $db->sql_transaction('begin');
  2613. $sql = 'DELETE FROM ' . WARNINGS_TABLE . '
  2614. WHERE ' . $db->sql_in_set('warning_id', $warning_list);
  2615. $db->sql_query($sql);
  2616. foreach ($user_list as $user_id => $value)
  2617. {
  2618. $sql = 'UPDATE ' . USERS_TABLE . " SET user_warnings = user_warnings - $value
  2619. WHERE user_id = $user_id";
  2620. $db->sql_query($sql);
  2621. }
  2622. $db->sql_transaction('commit');
  2623. }
  2624. set_config('warnings_last_gc', time(), true);
  2625. }
  2626. /**
  2627. * Tidy database, doing some maintanance tasks
  2628. */
  2629. function tidy_database()
  2630. {
  2631. global $db;
  2632. // Here we check permission consistency
  2633. // Sometimes, it can happen permission tables having forums listed which do not exist
  2634. $sql = 'SELECT forum_id
  2635. FROM ' . FORUMS_TABLE;
  2636. $result = $db->sql_query($sql);
  2637. $forum_ids = array(0);
  2638. while ($row = $db->sql_fetchrow($result))
  2639. {
  2640. $forum_ids[] = $row['forum_id'];
  2641. }
  2642. $db->sql_freeresult($result);
  2643. // Delete those rows from the acl tables not having listed the forums above
  2644. $sql = 'DELETE FROM ' . ACL_GROUPS_TABLE . '
  2645. WHERE ' . $db->sql_in_set('forum_id', $forum_ids, true);
  2646. $db->sql_query($sql);
  2647. $sql = 'DELETE FROM ' . ACL_USERS_TABLE . '
  2648. WHERE ' . $db->sql_in_set('forum_id', $forum_ids, true);
  2649. $db->sql_query($sql);
  2650. set_config('database_last_gc', time(), true);
  2651. }
  2652. /**
  2653. * Add permission language - this will make sure custom files will be included
  2654. */
  2655. function add_permission_language()
  2656. {
  2657. global $user, $phpEx, $phpbb_extension_manager;
  2658. // add permission language files from extensions
  2659. $finder = $phpbb_extension_manager->get_finder();
  2660. $lang_files = $finder
  2661. ->prefix('permissions_')
  2662. ->suffix(".$phpEx")
  2663. ->core_path('language/' . $user->lang_name . '/')
  2664. ->extension_directory('/language/' . $user->lang_name)
  2665. ->find();
  2666. foreach ($lang_files as $lang_file => $ext_name)
  2667. {
  2668. if ($ext_name === '/')
  2669. {
  2670. $user->add_lang($lang_file);
  2671. }
  2672. else
  2673. {
  2674. $user->add_lang_ext($ext_name, $lang_file);
  2675. }
  2676. }
  2677. }
  2678. /**
  2679. * Enables a particular flag in a bitfield column of a given table.
  2680. *
  2681. * @param string $table_name The table to update
  2682. * @param string $column_name The column containing a bitfield to update
  2683. * @param int $flag The binary flag which is OR-ed with the current column value
  2684. * @param string $sql_more This string is attached to the sql query generated to update the table.
  2685. *
  2686. * @return null
  2687. */
  2688. function enable_bitfield_column_flag($table_name, $column_name, $flag, $sql_more = '')
  2689. {
  2690. global $db;
  2691. $sql = 'UPDATE ' . $table_name . '
  2692. SET ' . $column_name . ' = ' . $db->sql_bit_or($column_name, $flag) . '
  2693. ' . $sql_more;
  2694. $db->sql_query($sql);
  2695. }