PageRenderTime 36ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 1ms

/forum/includes/functions_admin.php

https://github.com/GreyTeardrop/socionicasys-forum
PHP | 3419 lines | 2519 code | 538 blank | 362 comment | 421 complexity | 985b8e5d2cbe4fc972e89fdb9421c884 MD5 | raw file
Possible License(s): AGPL-1.0, LGPL-3.0, MPL-2.0-no-copyleft-exception
  1. <?php
  2. /**
  3. *
  4. * @package acp
  5. * @version $Id$
  6. * @copyright (c) 2005 phpBB Group
  7. * @license http://opensource.org/licenses/gpl-license.php GNU Public License
  8. *
  9. */
  10. /**
  11. * @ignore
  12. */
  13. if (!defined('IN_PHPBB'))
  14. {
  15. exit;
  16. }
  17. /**
  18. * Recalculate Nested Sets
  19. *
  20. * @param int $new_id first left_id (should start with 1)
  21. * @param string $pkey primary key-column (containing the id for the parent_id of the children)
  22. * @param string $table constant or fullname of the table
  23. * @param int $parent_id parent_id of the current set (default = 0)
  24. * @param array $where contains strings to compare closer on the where statement (additional)
  25. *
  26. * @author EXreaction
  27. */
  28. function recalc_nested_sets(&$new_id, $pkey, $table, $parent_id = 0, $where = array())
  29. {
  30. global $db;
  31. $sql = 'SELECT *
  32. FROM ' . $table . '
  33. WHERE parent_id = ' . (int) $parent_id .
  34. ((!empty($where)) ? ' AND ' . implode(' AND ', $where) : '') . '
  35. ORDER BY left_id ASC';
  36. $result = $db->sql_query($sql);
  37. while ($row = $db->sql_fetchrow($result))
  38. {
  39. // First we update the left_id for this module
  40. if ($row['left_id'] != $new_id)
  41. {
  42. $db->sql_query('UPDATE ' . $table . ' SET ' . $db->sql_build_array('UPDATE', array('left_id' => $new_id)) . " WHERE $pkey = {$row[$pkey]}");
  43. }
  44. $new_id++;
  45. // Then we go through any children and update their left/right id's
  46. recalc_nested_sets($new_id, $pkey, $table, $row[$pkey], $where);
  47. // Then we come back and update the right_id for this module
  48. if ($row['right_id'] != $new_id)
  49. {
  50. $db->sql_query('UPDATE ' . $table . ' SET ' . $db->sql_build_array('UPDATE', array('right_id' => $new_id)) . " WHERE $pkey = {$row[$pkey]}");
  51. }
  52. $new_id++;
  53. }
  54. $db->sql_freeresult($result);
  55. }
  56. /**
  57. * Simple version of jumpbox, just lists authed forums
  58. */
  59. function make_forum_select($select_id = false, $ignore_id = false, $ignore_acl = false, $ignore_nonpost = false, $ignore_emptycat = true, $only_acl_post = false, $return_array = false)
  60. {
  61. global $db, $user, $auth;
  62. // This query is identical to the jumpbox one
  63. $sql = 'SELECT forum_id, forum_name, parent_id, forum_type, forum_flags, forum_options, left_id, right_id
  64. FROM ' . FORUMS_TABLE . '
  65. ORDER BY left_id ASC';
  66. $result = $db->sql_query($sql, 600);
  67. $right = 0;
  68. $padding_store = array('0' => '');
  69. $padding = '';
  70. $forum_list = ($return_array) ? array() : '';
  71. // Sometimes it could happen that forums will be displayed here not be displayed within the index page
  72. // This is the result of forums not displayed at index, having list permissions and a parent of a forum with no permissions.
  73. // If this happens, the padding could be "broken"
  74. while ($row = $db->sql_fetchrow($result))
  75. {
  76. if ($row['left_id'] < $right)
  77. {
  78. $padding .= '&nbsp; &nbsp;';
  79. $padding_store[$row['parent_id']] = $padding;
  80. }
  81. else if ($row['left_id'] > $right + 1)
  82. {
  83. $padding = (isset($padding_store[$row['parent_id']])) ? $padding_store[$row['parent_id']] : '';
  84. }
  85. $right = $row['right_id'];
  86. $disabled = false;
  87. if (!$ignore_acl && $auth->acl_gets(array('f_list', 'a_forum', 'a_forumadd', 'a_forumdel'), $row['forum_id']))
  88. {
  89. if ($only_acl_post && !$auth->acl_get('f_post', $row['forum_id']) || (!$auth->acl_get('m_approve', $row['forum_id']) && !$auth->acl_get('f_noapprove', $row['forum_id'])))
  90. {
  91. $disabled = true;
  92. }
  93. }
  94. else if (!$ignore_acl)
  95. {
  96. continue;
  97. }
  98. if (
  99. ((is_array($ignore_id) && in_array($row['forum_id'], $ignore_id)) || $row['forum_id'] == $ignore_id)
  100. ||
  101. // Non-postable forum with no subforums, don't display
  102. ($row['forum_type'] == FORUM_CAT && ($row['left_id'] + 1 == $row['right_id']) && $ignore_emptycat)
  103. ||
  104. ($row['forum_type'] != FORUM_POST && $ignore_nonpost)
  105. )
  106. {
  107. $disabled = true;
  108. }
  109. if ($return_array)
  110. {
  111. // Include some more information...
  112. $selected = (is_array($select_id)) ? ((in_array($row['forum_id'], $select_id)) ? true : false) : (($row['forum_id'] == $select_id) ? true : false);
  113. $forum_list[$row['forum_id']] = array_merge(array('padding' => $padding, 'selected' => ($selected && !$disabled), 'disabled' => $disabled), $row);
  114. }
  115. else
  116. {
  117. $selected = (is_array($select_id)) ? ((in_array($row['forum_id'], $select_id)) ? ' selected="selected"' : '') : (($row['forum_id'] == $select_id) ? ' selected="selected"' : '');
  118. $forum_list .= '<option value="' . $row['forum_id'] . '"' . (($disabled) ? ' disabled="disabled" class="disabled-option"' : $selected) . '>' . $padding . $row['forum_name'] . '</option>';
  119. }
  120. }
  121. $db->sql_freeresult($result);
  122. unset($padding_store);
  123. return $forum_list;
  124. }
  125. /**
  126. * Generate size select options
  127. */
  128. function size_select_options($size_compare)
  129. {
  130. global $user;
  131. $size_types_text = array($user->lang['BYTES'], $user->lang['KIB'], $user->lang['MIB']);
  132. $size_types = array('b', 'kb', 'mb');
  133. $s_size_options = '';
  134. for ($i = 0, $size = sizeof($size_types_text); $i < $size; $i++)
  135. {
  136. $selected = ($size_compare == $size_types[$i]) ? ' selected="selected"' : '';
  137. $s_size_options .= '<option value="' . $size_types[$i] . '"' . $selected . '>' . $size_types_text[$i] . '</option>';
  138. }
  139. return $s_size_options;
  140. }
  141. /**
  142. * Generate list of groups (option fields without select)
  143. *
  144. * @param int $group_id The default group id to mark as selected
  145. * @param array $exclude_ids The group ids to exclude from the list, false (default) if you whish to exclude no id
  146. * @param int $manage_founder If set to false (default) all groups are returned, if 0 only those groups returned not being managed by founders only, if 1 only those groups returned managed by founders only.
  147. *
  148. * @return string The list of options.
  149. */
  150. function group_select_options($group_id, $exclude_ids = false, $manage_founder = false)
  151. {
  152. global $db, $user, $config;
  153. $exclude_sql = ($exclude_ids !== false && sizeof($exclude_ids)) ? 'WHERE ' . $db->sql_in_set('group_id', array_map('intval', $exclude_ids), true) : '';
  154. $sql_and = (!$config['coppa_enable']) ? (($exclude_sql) ? ' AND ' : ' WHERE ') . "group_name <> 'REGISTERED_COPPA'" : '';
  155. $sql_founder = ($manage_founder !== false) ? (($exclude_sql || $sql_and) ? ' AND ' : ' WHERE ') . 'group_founder_manage = ' . (int) $manage_founder : '';
  156. $sql = 'SELECT group_id, group_name, group_type
  157. FROM ' . GROUPS_TABLE . "
  158. $exclude_sql
  159. $sql_and
  160. $sql_founder
  161. ORDER BY group_type DESC, group_name ASC";
  162. $result = $db->sql_query($sql);
  163. $s_group_options = '';
  164. while ($row = $db->sql_fetchrow($result))
  165. {
  166. $selected = ($row['group_id'] == $group_id) ? ' selected="selected"' : '';
  167. $s_group_options .= '<option' . (($row['group_type'] == GROUP_SPECIAL) ? ' class="sep"' : '') . ' value="' . $row['group_id'] . '"' . $selected . '>' . (($row['group_type'] == GROUP_SPECIAL) ? $user->lang['G_' . $row['group_name']] : $row['group_name']) . '</option>';
  168. }
  169. $db->sql_freeresult($result);
  170. return $s_group_options;
  171. }
  172. /**
  173. * Obtain authed forums list
  174. */
  175. function get_forum_list($acl_list = 'f_list', $id_only = true, $postable_only = false, $no_cache = false)
  176. {
  177. global $db, $auth;
  178. static $forum_rows;
  179. if (!isset($forum_rows))
  180. {
  181. // This query is identical to the jumpbox one
  182. $expire_time = ($no_cache) ? 0 : 600;
  183. $sql = 'SELECT forum_id, forum_name, parent_id, forum_type, left_id, right_id
  184. FROM ' . FORUMS_TABLE . '
  185. ORDER BY left_id ASC';
  186. $result = $db->sql_query($sql, $expire_time);
  187. $forum_rows = array();
  188. $right = $padding = 0;
  189. $padding_store = array('0' => 0);
  190. while ($row = $db->sql_fetchrow($result))
  191. {
  192. if ($row['left_id'] < $right)
  193. {
  194. $padding++;
  195. $padding_store[$row['parent_id']] = $padding;
  196. }
  197. else if ($row['left_id'] > $right + 1)
  198. {
  199. // Ok, if the $padding_store for this parent is empty there is something wrong. For now we will skip over it.
  200. // @todo digging deep to find out "how" this can happen.
  201. $padding = (isset($padding_store[$row['parent_id']])) ? $padding_store[$row['parent_id']] : $padding;
  202. }
  203. $right = $row['right_id'];
  204. $row['padding'] = $padding;
  205. $forum_rows[] = $row;
  206. }
  207. $db->sql_freeresult($result);
  208. unset($padding_store);
  209. }
  210. $rowset = array();
  211. foreach ($forum_rows as $row)
  212. {
  213. if ($postable_only && $row['forum_type'] != FORUM_POST)
  214. {
  215. continue;
  216. }
  217. if ($acl_list == '' || ($acl_list != '' && $auth->acl_gets($acl_list, $row['forum_id'])))
  218. {
  219. $rowset[] = ($id_only) ? (int) $row['forum_id'] : $row;
  220. }
  221. }
  222. return $rowset;
  223. }
  224. /**
  225. * Get forum branch
  226. */
  227. function get_forum_branch($forum_id, $type = 'all', $order = 'descending', $include_forum = true)
  228. {
  229. global $db;
  230. switch ($type)
  231. {
  232. case 'parents':
  233. $condition = 'f1.left_id BETWEEN f2.left_id AND f2.right_id';
  234. break;
  235. case 'children':
  236. $condition = 'f2.left_id BETWEEN f1.left_id AND f1.right_id';
  237. break;
  238. default:
  239. $condition = 'f2.left_id BETWEEN f1.left_id AND f1.right_id OR f1.left_id BETWEEN f2.left_id AND f2.right_id';
  240. break;
  241. }
  242. $rows = array();
  243. $sql = 'SELECT f2.*
  244. FROM ' . FORUMS_TABLE . ' f1
  245. LEFT JOIN ' . FORUMS_TABLE . " f2 ON ($condition)
  246. WHERE f1.forum_id = $forum_id
  247. ORDER BY f2.left_id " . (($order == 'descending') ? 'ASC' : 'DESC');
  248. $result = $db->sql_query($sql);
  249. while ($row = $db->sql_fetchrow($result))
  250. {
  251. if (!$include_forum && $row['forum_id'] == $forum_id)
  252. {
  253. continue;
  254. }
  255. $rows[] = $row;
  256. }
  257. $db->sql_freeresult($result);
  258. return $rows;
  259. }
  260. /**
  261. * Copies permissions from one forum to others
  262. *
  263. * @param int $src_forum_id The source forum we want to copy permissions from
  264. * @param array $dest_forum_ids The destination forum(s) we want to copy to
  265. * @param bool $clear_dest_perms True if destination permissions should be deleted
  266. * @param bool $add_log True if log entry should be added
  267. *
  268. * @return bool False on error
  269. *
  270. * @author bantu
  271. */
  272. function copy_forum_permissions($src_forum_id, $dest_forum_ids, $clear_dest_perms = true, $add_log = true)
  273. {
  274. global $db;
  275. // Only one forum id specified
  276. if (!is_array($dest_forum_ids))
  277. {
  278. $dest_forum_ids = array($dest_forum_ids);
  279. }
  280. // Make sure forum ids are integers
  281. $src_forum_id = (int) $src_forum_id;
  282. $dest_forum_ids = array_map('intval', $dest_forum_ids);
  283. // No source forum or no destination forums specified
  284. if (empty($src_forum_id) || empty($dest_forum_ids))
  285. {
  286. return false;
  287. }
  288. // Check if source forum exists
  289. $sql = 'SELECT forum_name
  290. FROM ' . FORUMS_TABLE . '
  291. WHERE forum_id = ' . $src_forum_id;
  292. $result = $db->sql_query($sql);
  293. $src_forum_name = $db->sql_fetchfield('forum_name');
  294. $db->sql_freeresult($result);
  295. // Source forum doesn't exist
  296. if (empty($src_forum_name))
  297. {
  298. return false;
  299. }
  300. // Check if destination forums exists
  301. $sql = 'SELECT forum_id, forum_name
  302. FROM ' . FORUMS_TABLE . '
  303. WHERE ' . $db->sql_in_set('forum_id', $dest_forum_ids);
  304. $result = $db->sql_query($sql);
  305. $dest_forum_ids = $dest_forum_names = array();
  306. while ($row = $db->sql_fetchrow($result))
  307. {
  308. $dest_forum_ids[] = (int) $row['forum_id'];
  309. $dest_forum_names[] = $row['forum_name'];
  310. }
  311. $db->sql_freeresult($result);
  312. // No destination forum exists
  313. if (empty($dest_forum_ids))
  314. {
  315. return false;
  316. }
  317. // From the mysql documentation:
  318. // Prior to MySQL 4.0.14, the target table of the INSERT statement cannot appear
  319. // in the FROM clause of the SELECT part of the query. This limitation is lifted in 4.0.14.
  320. // Due to this we stay on the safe side if we do the insertion "the manual way"
  321. // Rowsets we're going to insert
  322. $users_sql_ary = $groups_sql_ary = array();
  323. // Query acl users table for source forum data
  324. $sql = 'SELECT user_id, auth_option_id, auth_role_id, auth_setting
  325. FROM ' . ACL_USERS_TABLE . '
  326. WHERE forum_id = ' . $src_forum_id;
  327. $result = $db->sql_query($sql);
  328. while ($row = $db->sql_fetchrow($result))
  329. {
  330. $row = array(
  331. 'user_id' => (int) $row['user_id'],
  332. 'auth_option_id' => (int) $row['auth_option_id'],
  333. 'auth_role_id' => (int) $row['auth_role_id'],
  334. 'auth_setting' => (int) $row['auth_setting'],
  335. );
  336. foreach ($dest_forum_ids as $dest_forum_id)
  337. {
  338. $users_sql_ary[] = $row + array('forum_id' => $dest_forum_id);
  339. }
  340. }
  341. $db->sql_freeresult($result);
  342. // Query acl groups table for source forum data
  343. $sql = 'SELECT group_id, auth_option_id, auth_role_id, auth_setting
  344. FROM ' . ACL_GROUPS_TABLE . '
  345. WHERE forum_id = ' . $src_forum_id;
  346. $result = $db->sql_query($sql);
  347. while ($row = $db->sql_fetchrow($result))
  348. {
  349. $row = array(
  350. 'group_id' => (int) $row['group_id'],
  351. 'auth_option_id' => (int) $row['auth_option_id'],
  352. 'auth_role_id' => (int) $row['auth_role_id'],
  353. 'auth_setting' => (int) $row['auth_setting'],
  354. );
  355. foreach ($dest_forum_ids as $dest_forum_id)
  356. {
  357. $groups_sql_ary[] = $row + array('forum_id' => $dest_forum_id);
  358. }
  359. }
  360. $db->sql_freeresult($result);
  361. $db->sql_transaction('begin');
  362. // Clear current permissions of destination forums
  363. if ($clear_dest_perms)
  364. {
  365. $sql = 'DELETE FROM ' . ACL_USERS_TABLE . '
  366. WHERE ' . $db->sql_in_set('forum_id', $dest_forum_ids);
  367. $db->sql_query($sql);
  368. $sql = 'DELETE FROM ' . ACL_GROUPS_TABLE . '
  369. WHERE ' . $db->sql_in_set('forum_id', $dest_forum_ids);
  370. $db->sql_query($sql);
  371. }
  372. $db->sql_multi_insert(ACL_USERS_TABLE, $users_sql_ary);
  373. $db->sql_multi_insert(ACL_GROUPS_TABLE, $groups_sql_ary);
  374. if ($add_log)
  375. {
  376. add_log('admin', 'LOG_FORUM_COPIED_PERMISSIONS', $src_forum_name, implode(', ', $dest_forum_names));
  377. }
  378. $db->sql_transaction('commit');
  379. return true;
  380. }
  381. /**
  382. * Get physical file listing
  383. */
  384. function filelist($rootdir, $dir = '', $type = 'gif|jpg|jpeg|png')
  385. {
  386. $matches = array($dir => array());
  387. // Remove initial / if present
  388. $rootdir = (substr($rootdir, 0, 1) == '/') ? substr($rootdir, 1) : $rootdir;
  389. // Add closing / if not present
  390. $rootdir = ($rootdir && substr($rootdir, -1) != '/') ? $rootdir . '/' : $rootdir;
  391. // Remove initial / if present
  392. $dir = (substr($dir, 0, 1) == '/') ? substr($dir, 1) : $dir;
  393. // Add closing / if not present
  394. $dir = ($dir && substr($dir, -1) != '/') ? $dir . '/' : $dir;
  395. if (!is_dir($rootdir . $dir))
  396. {
  397. return $matches;
  398. }
  399. $dh = @opendir($rootdir . $dir);
  400. if (!$dh)
  401. {
  402. return $matches;
  403. }
  404. while (($fname = readdir($dh)) !== false)
  405. {
  406. if (is_file("$rootdir$dir$fname"))
  407. {
  408. if (filesize("$rootdir$dir$fname") && preg_match('#\.' . $type . '$#i', $fname))
  409. {
  410. $matches[$dir][] = $fname;
  411. }
  412. }
  413. else if ($fname[0] != '.' && is_dir("$rootdir$dir$fname"))
  414. {
  415. $matches += filelist($rootdir, "$dir$fname", $type);
  416. }
  417. }
  418. closedir($dh);
  419. return $matches;
  420. }
  421. /**
  422. * Move topic(s)
  423. */
  424. function move_topics($topic_ids, $forum_id, $auto_sync = true)
  425. {
  426. global $db;
  427. if (empty($topic_ids))
  428. {
  429. return;
  430. }
  431. $forum_ids = array($forum_id);
  432. if (!is_array($topic_ids))
  433. {
  434. $topic_ids = array($topic_ids);
  435. }
  436. $sql = 'DELETE FROM ' . TOPICS_TABLE . '
  437. WHERE ' . $db->sql_in_set('topic_moved_id', $topic_ids) . '
  438. AND forum_id = ' . $forum_id;
  439. $db->sql_query($sql);
  440. if ($auto_sync)
  441. {
  442. $sql = 'SELECT DISTINCT forum_id
  443. FROM ' . TOPICS_TABLE . '
  444. WHERE ' . $db->sql_in_set('topic_id', $topic_ids);
  445. $result = $db->sql_query($sql);
  446. while ($row = $db->sql_fetchrow($result))
  447. {
  448. $forum_ids[] = $row['forum_id'];
  449. }
  450. $db->sql_freeresult($result);
  451. }
  452. $table_ary = array(TOPICS_TABLE, POSTS_TABLE, LOG_TABLE, DRAFTS_TABLE, TOPICS_TRACK_TABLE);
  453. foreach ($table_ary as $table)
  454. {
  455. $sql = "UPDATE $table
  456. SET forum_id = $forum_id
  457. WHERE " . $db->sql_in_set('topic_id', $topic_ids);
  458. $db->sql_query($sql);
  459. }
  460. unset($table_ary);
  461. if ($auto_sync)
  462. {
  463. sync('forum', 'forum_id', $forum_ids, true, true);
  464. unset($forum_ids);
  465. }
  466. }
  467. /**
  468. * Move post(s)
  469. */
  470. function move_posts($post_ids, $topic_id, $auto_sync = true)
  471. {
  472. global $db;
  473. if (!is_array($post_ids))
  474. {
  475. $post_ids = array($post_ids);
  476. }
  477. $forum_ids = array();
  478. $topic_ids = array($topic_id);
  479. $sql = 'SELECT DISTINCT topic_id, forum_id
  480. FROM ' . POSTS_TABLE . '
  481. WHERE ' . $db->sql_in_set('post_id', $post_ids);
  482. $result = $db->sql_query($sql);
  483. while ($row = $db->sql_fetchrow($result))
  484. {
  485. $forum_ids[] = (int) $row['forum_id'];
  486. $topic_ids[] = (int) $row['topic_id'];
  487. }
  488. $db->sql_freeresult($result);
  489. $sql = 'SELECT forum_id
  490. FROM ' . TOPICS_TABLE . '
  491. WHERE topic_id = ' . $topic_id;
  492. $result = $db->sql_query($sql);
  493. $forum_row = $db->sql_fetchrow($result);
  494. $db->sql_freeresult($result);
  495. if (!$forum_row)
  496. {
  497. trigger_error('NO_TOPIC');
  498. }
  499. $sql = 'UPDATE ' . POSTS_TABLE . '
  500. SET forum_id = ' . (int) $forum_row['forum_id'] . ", topic_id = $topic_id
  501. WHERE " . $db->sql_in_set('post_id', $post_ids);
  502. $db->sql_query($sql);
  503. $sql = 'UPDATE ' . ATTACHMENTS_TABLE . "
  504. SET topic_id = $topic_id, in_message = 0
  505. WHERE " . $db->sql_in_set('post_msg_id', $post_ids);
  506. $db->sql_query($sql);
  507. if ($auto_sync)
  508. {
  509. $forum_ids[] = (int) $forum_row['forum_id'];
  510. sync('topic_reported', 'topic_id', $topic_ids);
  511. sync('topic_attachment', 'topic_id', $topic_ids);
  512. sync('topic', 'topic_id', $topic_ids, true);
  513. sync('forum', 'forum_id', $forum_ids, true, true);
  514. }
  515. // Update posted information
  516. update_posted_info($topic_ids);
  517. }
  518. /**
  519. * Remove topic(s)
  520. */
  521. function delete_topics($where_type, $where_ids, $auto_sync = true, $post_count_sync = true, $call_delete_posts = true)
  522. {
  523. global $db, $config;
  524. $approved_topics = 0;
  525. $forum_ids = $topic_ids = array();
  526. if ($where_type === 'range')
  527. {
  528. $where_clause = $where_ids;
  529. }
  530. else
  531. {
  532. $where_ids = (is_array($where_ids)) ? array_unique($where_ids) : array($where_ids);
  533. if (!sizeof($where_ids))
  534. {
  535. return array('topics' => 0, 'posts' => 0);
  536. }
  537. $where_clause = $db->sql_in_set($where_type, $where_ids);
  538. }
  539. // Making sure that delete_posts does not call delete_topics again...
  540. $return = array(
  541. 'posts' => ($call_delete_posts) ? delete_posts($where_type, $where_ids, false, true, $post_count_sync, false) : 0,
  542. );
  543. $sql = 'SELECT topic_id, forum_id, topic_approved, topic_moved_id
  544. FROM ' . TOPICS_TABLE . '
  545. WHERE ' . $where_clause;
  546. $result = $db->sql_query($sql);
  547. while ($row = $db->sql_fetchrow($result))
  548. {
  549. $forum_ids[] = $row['forum_id'];
  550. $topic_ids[] = $row['topic_id'];
  551. if ($row['topic_approved'] && !$row['topic_moved_id'])
  552. {
  553. $approved_topics++;
  554. }
  555. }
  556. $db->sql_freeresult($result);
  557. $return['topics'] = sizeof($topic_ids);
  558. if (!sizeof($topic_ids))
  559. {
  560. return $return;
  561. }
  562. $db->sql_transaction('begin');
  563. $table_ary = array(BOOKMARKS_TABLE, TOPICS_TRACK_TABLE, TOPICS_POSTED_TABLE, POLL_VOTES_TABLE, POLL_OPTIONS_TABLE, TOPICS_WATCH_TABLE, TOPICS_TABLE);
  564. foreach ($table_ary as $table)
  565. {
  566. $sql = "DELETE FROM $table
  567. WHERE " . $db->sql_in_set('topic_id', $topic_ids);
  568. $db->sql_query($sql);
  569. }
  570. unset($table_ary);
  571. $moved_topic_ids = array();
  572. // update the other forums
  573. $sql = 'SELECT topic_id, forum_id
  574. FROM ' . TOPICS_TABLE . '
  575. WHERE ' . $db->sql_in_set('topic_moved_id', $topic_ids);
  576. $result = $db->sql_query($sql);
  577. while ($row = $db->sql_fetchrow($result))
  578. {
  579. $forum_ids[] = $row['forum_id'];
  580. $moved_topic_ids[] = $row['topic_id'];
  581. }
  582. $db->sql_freeresult($result);
  583. if (sizeof($moved_topic_ids))
  584. {
  585. $sql = 'DELETE FROM ' . TOPICS_TABLE . '
  586. WHERE ' . $db->sql_in_set('topic_id', $moved_topic_ids);
  587. $db->sql_query($sql);
  588. }
  589. $db->sql_transaction('commit');
  590. if ($auto_sync)
  591. {
  592. sync('forum', 'forum_id', array_unique($forum_ids), true, true);
  593. sync('topic_reported', $where_type, $where_ids);
  594. }
  595. if ($approved_topics)
  596. {
  597. set_config_count('num_topics', $approved_topics * (-1), true);
  598. }
  599. return $return;
  600. }
  601. /**
  602. * Remove post(s)
  603. */
  604. function delete_posts($where_type, $where_ids, $auto_sync = true, $posted_sync = true, $post_count_sync = true, $call_delete_topics = true)
  605. {
  606. global $db, $config, $phpbb_root_path, $phpEx;
  607. if ($where_type === 'range')
  608. {
  609. $where_clause = $where_ids;
  610. }
  611. else
  612. {
  613. if (is_array($where_ids))
  614. {
  615. $where_ids = array_unique($where_ids);
  616. }
  617. else
  618. {
  619. $where_ids = array($where_ids);
  620. }
  621. if (!sizeof($where_ids))
  622. {
  623. return false;
  624. }
  625. $where_ids = array_map('intval', $where_ids);
  626. /* Possible code for splitting post deletion
  627. if (sizeof($where_ids) >= 1001)
  628. {
  629. // Split into chunks of 1000
  630. $chunks = array_chunk($where_ids, 1000);
  631. foreach ($chunks as $_where_ids)
  632. {
  633. delete_posts($where_type, $_where_ids, $auto_sync, $posted_sync, $post_count_sync, $call_delete_topics);
  634. }
  635. return;
  636. }*/
  637. $where_clause = $db->sql_in_set($where_type, $where_ids);
  638. }
  639. $approved_posts = 0;
  640. $post_ids = $topic_ids = $forum_ids = $post_counts = $remove_topics = array();
  641. $sql = 'SELECT post_id, poster_id, post_approved, post_postcount, topic_id, forum_id
  642. FROM ' . POSTS_TABLE . '
  643. WHERE ' . $where_clause;
  644. //-- mod: Prime Trash Bin ---------------------------------------------------//
  645. $sql = substr_replace($sql, 'post_deleted_time, ', strpos($sql, 'post_id'), 0);
  646. //-- end: Prime Trash Bin ---------------------------------------------------//
  647. $result = $db->sql_query($sql);
  648. while ($row = $db->sql_fetchrow($result))
  649. {
  650. $post_ids[] = (int) $row['post_id'];
  651. $poster_ids[] = (int) $row['poster_id'];
  652. $topic_ids[] = (int) $row['topic_id'];
  653. $forum_ids[] = (int) $row['forum_id'];
  654. if ($row['post_postcount'] && $post_count_sync && $row['post_approved'])
  655. {
  656. //-- mod: Prime Trash Bin ---------------------------------------------------//
  657. // Do not reduce post counts for posts that have already been marked as deleted.
  658. if (!empty($row['post_deleted_time']))
  659. {
  660. continue;
  661. }
  662. //-- end: Prime Trash Bin ---------------------------------------------------//
  663. $post_counts[$row['poster_id']] = (!empty($post_counts[$row['poster_id']])) ? $post_counts[$row['poster_id']] + 1 : 1;
  664. }
  665. if ($row['post_approved'])
  666. {
  667. $approved_posts++;
  668. }
  669. }
  670. $db->sql_freeresult($result);
  671. if (!sizeof($post_ids))
  672. {
  673. return false;
  674. }
  675. $db->sql_transaction('begin');
  676. $table_ary = array(POSTS_TABLE, REPORTS_TABLE);
  677. //-- mod: Prime Post Revisions ----------------------------------------------//
  678. $table_ary[] = POST_REVISIONS_TABLE; // To delete the post's edit history
  679. //-- end: Prime Post Revisions ----------------------------------------------//
  680. foreach ($table_ary as $table)
  681. {
  682. $sql = "DELETE FROM $table
  683. WHERE " . $db->sql_in_set('post_id', $post_ids);
  684. $db->sql_query($sql);
  685. }
  686. unset($table_ary);
  687. // Adjust users post counts
  688. if (sizeof($post_counts) && $post_count_sync)
  689. {
  690. foreach ($post_counts as $poster_id => $substract)
  691. {
  692. $sql = 'UPDATE ' . USERS_TABLE . '
  693. SET user_posts = 0
  694. WHERE user_id = ' . $poster_id . '
  695. AND user_posts < ' . $substract;
  696. $db->sql_query($sql);
  697. $sql = 'UPDATE ' . USERS_TABLE . '
  698. SET user_posts = user_posts - ' . $substract . '
  699. WHERE user_id = ' . $poster_id . '
  700. AND user_posts >= ' . $substract;
  701. $db->sql_query($sql);
  702. }
  703. }
  704. // Remove topics now having no posts?
  705. if (sizeof($topic_ids))
  706. {
  707. $sql = 'SELECT topic_id
  708. FROM ' . POSTS_TABLE . '
  709. WHERE ' . $db->sql_in_set('topic_id', $topic_ids) . '
  710. GROUP BY topic_id';
  711. $result = $db->sql_query($sql);
  712. while ($row = $db->sql_fetchrow($result))
  713. {
  714. $remove_topics[] = $row['topic_id'];
  715. }
  716. $db->sql_freeresult($result);
  717. // Actually, those not within remove_topics should be removed. ;)
  718. $remove_topics = array_diff($topic_ids, $remove_topics);
  719. }
  720. // Remove the message from the search index
  721. $search_type = basename($config['search_type']);
  722. if (!file_exists($phpbb_root_path . 'includes/search/' . $search_type . '.' . $phpEx))
  723. {
  724. trigger_error('NO_SUCH_SEARCH_MODULE');
  725. }
  726. include_once("{$phpbb_root_path}includes/search/$search_type.$phpEx");
  727. $error = false;
  728. $search = new $search_type($error);
  729. if ($error)
  730. {
  731. trigger_error($error);
  732. }
  733. $search->index_remove($post_ids, $poster_ids, $forum_ids);
  734. delete_attachments('post', $post_ids, false);
  735. $db->sql_transaction('commit');
  736. // Resync topics_posted table
  737. if ($posted_sync)
  738. {
  739. update_posted_info($topic_ids);
  740. }
  741. if ($auto_sync)
  742. {
  743. sync('topic_reported', 'topic_id', $topic_ids);
  744. sync('topic', 'topic_id', $topic_ids, true);
  745. sync('forum', 'forum_id', $forum_ids, true, true);
  746. }
  747. if ($approved_posts)
  748. {
  749. set_config_count('num_posts', $approved_posts * (-1), true);
  750. }
  751. // We actually remove topics now to not be inconsistent (the delete_topics function calls this function too)
  752. if (sizeof($remove_topics) && $call_delete_topics)
  753. {
  754. delete_topics('topic_id', $remove_topics, $auto_sync, $post_count_sync, false);
  755. }
  756. return sizeof($post_ids);
  757. }
  758. /**
  759. * Delete Attachments
  760. *
  761. * @param string $mode can be: post|message|topic|attach|user
  762. * @param mixed $ids can be: post_ids, message_ids, topic_ids, attach_ids, user_ids
  763. * @param bool $resync set this to false if you are deleting posts or topics
  764. */
  765. function delete_attachments($mode, $ids, $resync = true)
  766. {
  767. global $db, $config;
  768. // 0 is as bad as an empty array
  769. if (empty($ids))
  770. {
  771. return false;
  772. }
  773. if (is_array($ids))
  774. {
  775. $ids = array_unique($ids);
  776. $ids = array_map('intval', $ids);
  777. }
  778. else
  779. {
  780. $ids = array((int) $ids);
  781. }
  782. $sql_where = '';
  783. switch ($mode)
  784. {
  785. case 'post':
  786. case 'message':
  787. $sql_id = 'post_msg_id';
  788. $sql_where = ' AND in_message = ' . ($mode == 'message' ? 1 : 0);
  789. break;
  790. case 'topic':
  791. $sql_id = 'topic_id';
  792. break;
  793. case 'user':
  794. $sql_id = 'poster_id';
  795. break;
  796. case 'attach':
  797. default:
  798. $sql_id = 'attach_id';
  799. $mode = 'attach';
  800. break;
  801. }
  802. $post_ids = $message_ids = $topic_ids = $physical = array();
  803. // Collect post and topic ids for later use if we need to touch remaining entries (if resync is enabled)
  804. $sql = 'SELECT post_msg_id, topic_id, in_message, physical_filename, thumbnail, filesize, is_orphan
  805. FROM ' . ATTACHMENTS_TABLE . '
  806. WHERE ' . $db->sql_in_set($sql_id, $ids);
  807. $sql .= $sql_where;
  808. $result = $db->sql_query($sql);
  809. while ($row = $db->sql_fetchrow($result))
  810. {
  811. // We only need to store post/message/topic ids if resync is enabled and the file is not orphaned
  812. if ($resync && !$row['is_orphan'])
  813. {
  814. if (!$row['in_message'])
  815. {
  816. $post_ids[] = $row['post_msg_id'];
  817. $topic_ids[] = $row['topic_id'];
  818. }
  819. else
  820. {
  821. $message_ids[] = $row['post_msg_id'];
  822. }
  823. }
  824. $physical[] = array('filename' => $row['physical_filename'], 'thumbnail' => $row['thumbnail'], 'filesize' => $row['filesize'], 'is_orphan' => $row['is_orphan']);
  825. }
  826. $db->sql_freeresult($result);
  827. // Delete attachments
  828. $sql = 'DELETE FROM ' . ATTACHMENTS_TABLE . '
  829. WHERE ' . $db->sql_in_set($sql_id, $ids);
  830. $sql .= $sql_where;
  831. $db->sql_query($sql);
  832. $num_deleted = $db->sql_affectedrows();
  833. if (!$num_deleted)
  834. {
  835. return 0;
  836. }
  837. // Delete attachments from filesystem
  838. $space_removed = $files_removed = 0;
  839. foreach ($physical as $file_ary)
  840. {
  841. if (phpbb_unlink($file_ary['filename'], 'file', true) && !$file_ary['is_orphan'])
  842. {
  843. // Only non-orphaned files count to the file size
  844. $space_removed += $file_ary['filesize'];
  845. $files_removed++;
  846. }
  847. if ($file_ary['thumbnail'])
  848. {
  849. phpbb_unlink($file_ary['filename'], 'thumbnail', true);
  850. }
  851. }
  852. if ($space_removed || $files_removed)
  853. {
  854. set_config_count('upload_dir_size', $space_removed * (-1), true);
  855. set_config_count('num_files', $files_removed * (-1), true);
  856. }
  857. // If we do not resync, we do not need to adjust any message, post, topic or user entries
  858. if (!$resync)
  859. {
  860. return $num_deleted;
  861. }
  862. // No more use for the original ids
  863. unset($ids);
  864. // Now, we need to resync posts, messages, topics. We go through every one of them
  865. $post_ids = array_unique($post_ids);
  866. $message_ids = array_unique($message_ids);
  867. $topic_ids = array_unique($topic_ids);
  868. // Update post indicators for posts now no longer having attachments
  869. if (sizeof($post_ids))
  870. {
  871. // Just check which posts are still having an assigned attachment not orphaned by querying the attachments table
  872. $sql = 'SELECT post_msg_id
  873. FROM ' . ATTACHMENTS_TABLE . '
  874. WHERE ' . $db->sql_in_set('post_msg_id', $post_ids) . '
  875. AND in_message = 0
  876. AND is_orphan = 0';
  877. $result = $db->sql_query($sql);
  878. $remaining_ids = array();
  879. while ($row = $db->sql_fetchrow($result))
  880. {
  881. $remaining_ids[] = $row['post_msg_id'];
  882. }
  883. $db->sql_freeresult($result);
  884. // Now only unset those ids remaining
  885. $post_ids = array_diff($post_ids, $remaining_ids);
  886. if (sizeof($post_ids))
  887. {
  888. $sql = 'UPDATE ' . POSTS_TABLE . '
  889. SET post_attachment = 0
  890. WHERE ' . $db->sql_in_set('post_id', $post_ids);
  891. $db->sql_query($sql);
  892. }
  893. }
  894. // Update message table if messages are affected
  895. if (sizeof($message_ids))
  896. {
  897. // Just check which messages are still having an assigned attachment not orphaned by querying the attachments table
  898. $sql = 'SELECT post_msg_id
  899. FROM ' . ATTACHMENTS_TABLE . '
  900. WHERE ' . $db->sql_in_set('post_msg_id', $message_ids) . '
  901. AND in_message = 1
  902. AND is_orphan = 0';
  903. $result = $db->sql_query($sql);
  904. $remaining_ids = array();
  905. while ($row = $db->sql_fetchrow($result))
  906. {
  907. $remaining_ids[] = $row['post_msg_id'];
  908. }
  909. $db->sql_freeresult($result);
  910. // Now only unset those ids remaining
  911. $message_ids = array_diff($message_ids, $remaining_ids);
  912. if (sizeof($message_ids))
  913. {
  914. $sql = 'UPDATE ' . PRIVMSGS_TABLE . '
  915. SET message_attachment = 0
  916. WHERE ' . $db->sql_in_set('msg_id', $message_ids);
  917. $db->sql_query($sql);
  918. }
  919. }
  920. // Now update the topics. This is a bit trickier, because there could be posts still having attachments within the topic
  921. if (sizeof($topic_ids))
  922. {
  923. // Just check which topics are still having an assigned attachment not orphaned by querying the attachments table (much less entries expected)
  924. $sql = 'SELECT topic_id
  925. FROM ' . ATTACHMENTS_TABLE . '
  926. WHERE ' . $db->sql_in_set('topic_id', $topic_ids) . '
  927. AND is_orphan = 0';
  928. $result = $db->sql_query($sql);
  929. $remaining_ids = array();
  930. while ($row = $db->sql_fetchrow($result))
  931. {
  932. $remaining_ids[] = $row['topic_id'];
  933. }
  934. $db->sql_freeresult($result);
  935. // Now only unset those ids remaining
  936. $topic_ids = array_diff($topic_ids, $remaining_ids);
  937. if (sizeof($topic_ids))
  938. {
  939. $sql = 'UPDATE ' . TOPICS_TABLE . '
  940. SET topic_attachment = 0
  941. WHERE ' . $db->sql_in_set('topic_id', $topic_ids);
  942. $db->sql_query($sql);
  943. }
  944. }
  945. return $num_deleted;
  946. }
  947. /**
  948. * Deletes shadow topics pointing to a specified forum.
  949. *
  950. * @param int $forum_id The forum id
  951. * @param string $sql_more Additional WHERE statement, e.g. t.topic_time < (time() - 1234)
  952. * @param bool $auto_sync Will call sync() if this is true
  953. *
  954. * @return array Array with affected forums
  955. *
  956. * @author bantu
  957. */
  958. function delete_topic_shadows($forum_id, $sql_more = '', $auto_sync = true)
  959. {
  960. global $db;
  961. if (!$forum_id)
  962. {
  963. // Nothing to do.
  964. return;
  965. }
  966. // Set of affected forums we have to resync
  967. $sync_forum_ids = array();
  968. // Amount of topics we select and delete at once.
  969. $batch_size = 500;
  970. do
  971. {
  972. $sql = 'SELECT t2.forum_id, t2.topic_id
  973. FROM ' . TOPICS_TABLE . ' t2, ' . TOPICS_TABLE . ' t
  974. WHERE t2.topic_moved_id = t.topic_id
  975. AND t.forum_id = ' . (int) $forum_id . '
  976. ' . (($sql_more) ? 'AND ' . $sql_more : '');
  977. $result = $db->sql_query_limit($sql, $batch_size);
  978. $topic_ids = array();
  979. while ($row = $db->sql_fetchrow($result))
  980. {
  981. $topic_ids[] = (int) $row['topic_id'];
  982. $sync_forum_ids[(int) $row['forum_id']] = (int) $row['forum_id'];
  983. }
  984. $db->sql_freeresult($result);
  985. if (!empty($topic_ids))
  986. {
  987. $sql = 'DELETE FROM ' . TOPICS_TABLE . '
  988. WHERE ' . $db->sql_in_set('topic_id', $topic_ids);
  989. $db->sql_query($sql);
  990. }
  991. }
  992. while (sizeof($topic_ids) == $batch_size);
  993. if ($auto_sync)
  994. {
  995. sync('forum', 'forum_id', $sync_forum_ids, true, true);
  996. }
  997. return $sync_forum_ids;
  998. }
  999. /**
  1000. * Update/Sync posted information for topics
  1001. */
  1002. function update_posted_info(&$topic_ids)
  1003. {
  1004. global $db, $config;
  1005. if (empty($topic_ids) || !$config['load_db_track'])
  1006. {
  1007. return;
  1008. }
  1009. // First of all, let us remove any posted information for these topics
  1010. $sql = 'DELETE FROM ' . TOPICS_POSTED_TABLE . '
  1011. WHERE ' . $db->sql_in_set('topic_id', $topic_ids);
  1012. $db->sql_query($sql);
  1013. // Now, let us collect the user/topic combos for rebuilding the information
  1014. $sql = 'SELECT poster_id, topic_id
  1015. FROM ' . POSTS_TABLE . '
  1016. WHERE ' . $db->sql_in_set('topic_id', $topic_ids) . '
  1017. AND poster_id <> ' . ANONYMOUS . '
  1018. GROUP BY poster_id, topic_id';
  1019. $result = $db->sql_query($sql);
  1020. $posted = array();
  1021. while ($row = $db->sql_fetchrow($result))
  1022. {
  1023. // Add as key to make them unique (grouping by) and circumvent empty keys on array_unique
  1024. $posted[$row['poster_id']][] = $row['topic_id'];
  1025. }
  1026. $db->sql_freeresult($result);
  1027. // Now add the information...
  1028. $sql_ary = array();
  1029. foreach ($posted as $user_id => $topic_row)
  1030. {
  1031. foreach ($topic_row as $topic_id)
  1032. {
  1033. $sql_ary[] = array(
  1034. 'user_id' => (int) $user_id,
  1035. 'topic_id' => (int) $topic_id,
  1036. 'topic_posted' => 1,
  1037. );
  1038. }
  1039. }
  1040. unset($posted);
  1041. $db->sql_multi_insert(TOPICS_POSTED_TABLE, $sql_ary);
  1042. }
  1043. /**
  1044. * Delete attached file
  1045. */
  1046. function phpbb_unlink($filename, $mode = 'file', $entry_removed = false)
  1047. {
  1048. global $db, $phpbb_root_path, $config;
  1049. // Because of copying topics or modifications a physical filename could be assigned more than once. If so, do not remove the file itself.
  1050. $sql = 'SELECT COUNT(attach_id) AS num_entries
  1051. FROM ' . ATTACHMENTS_TABLE . "
  1052. WHERE physical_filename = '" . $db->sql_escape(utf8_basename($filename)) . "'";
  1053. $result = $db->sql_query($sql);
  1054. $num_entries = (int) $db->sql_fetchfield('num_entries');
  1055. $db->sql_freeresult($result);
  1056. // Do not remove file if at least one additional entry with the same name exist.
  1057. if (($entry_removed && $num_entries > 0) || (!$entry_removed && $num_entries > 1))
  1058. {
  1059. return false;
  1060. }
  1061. $filename = ($mode == 'thumbnail') ? 'thumb_' . utf8_basename($filename) : utf8_basename($filename);
  1062. return @unlink($phpbb_root_path . $config['upload_path'] . '/' . $filename);
  1063. }
  1064. /**
  1065. * All-encompasing sync function
  1066. *
  1067. * Exaples:
  1068. * <code>
  1069. * sync('topic', 'topic_id', 123); // resync topic #123
  1070. * sync('topic', 'forum_id', array(2, 3)); // resync topics from forum #2 and #3
  1071. * sync('topic'); // resync all topics
  1072. * sync('topic', 'range', 'topic_id BETWEEN 1 AND 60'); // resync a range of topics/forums (only available for 'topic' and 'forum' modes)
  1073. * </code>
  1074. *
  1075. * Modes:
  1076. * - forum Resync complete forum
  1077. * - topic Resync topics
  1078. * - topic_moved Removes topic shadows that would be in the same forum as the topic they link to
  1079. * - topic_approved Resyncs the topic_approved flag according to the status of the first post
  1080. * - post_reported Resyncs the post_reported flag, relying on actual reports
  1081. * - topic_reported Resyncs the topic_reported flag, relying on post_reported flags
  1082. * - post_attachement Same as post_reported, but with attachment flags
  1083. * - topic_attachement Same as topic_reported, but with attachment flags
  1084. */
  1085. function sync($mode, $where_type = '', $where_ids = '', $resync_parents = false, $sync_extra = false)
  1086. {
  1087. global $db;
  1088. if (is_array($where_ids))
  1089. {
  1090. $where_ids = array_unique($where_ids);
  1091. $where_ids = array_map('intval', $where_ids);
  1092. }
  1093. else if ($where_type != 'range')
  1094. {
  1095. $where_ids = ($where_ids) ? array((int) $where_ids) : array();
  1096. }
  1097. if ($mode == 'forum' || $mode == 'topic' || $mode == 'topic_approved' || $mode == 'topic_reported' || $mode == 'post_reported')
  1098. {
  1099. if (!$where_type)
  1100. {
  1101. $where_sql = '';
  1102. $where_sql_and = 'WHERE';
  1103. }
  1104. else if ($where_type == 'range')
  1105. {
  1106. // Only check a range of topics/forums. For instance: 'topic_id BETWEEN 1 AND 60'
  1107. $where_sql = 'WHERE (' . $mode[0] . ".$where_ids)";
  1108. $where_sql_and = $where_sql . "\n\tAND";
  1109. }
  1110. else
  1111. {
  1112. // Do not sync the "global forum"
  1113. $where_ids = array_diff($where_ids, array(0));
  1114. if (!sizeof($where_ids))
  1115. {
  1116. // Empty array with IDs. This means that we don't have any work to do. Just return.
  1117. return;
  1118. }
  1119. // Limit the topics/forums we are syncing, use specific topic/forum IDs.
  1120. // $where_type contains the field for the where clause (forum_id, topic_id)
  1121. $where_sql = 'WHERE ' . $db->sql_in_set($mode[0] . '.' . $where_type, $where_ids);
  1122. $where_sql_and = $where_sql . "\n\tAND";
  1123. }
  1124. }
  1125. else
  1126. {
  1127. if (!sizeof($where_ids))
  1128. {
  1129. return;
  1130. }
  1131. // $where_type contains the field for the where clause (forum_id, topic_id)
  1132. $where_sql = 'WHERE ' . $db->sql_in_set($mode[0] . '.' . $where_type, $where_ids);
  1133. $where_sql_and = $where_sql . "\n\tAND";
  1134. }
  1135. switch ($mode)
  1136. {
  1137. case 'topic_moved':
  1138. $db->sql_transaction('begin');
  1139. switch ($db->sql_layer)
  1140. {
  1141. case 'mysql4':
  1142. case 'mysqli':
  1143. $sql = 'DELETE FROM ' . TOPICS_TABLE . '
  1144. USING ' . TOPICS_TABLE . ' t1, ' . TOPICS_TABLE . " t2
  1145. WHERE t1.topic_moved_id = t2.topic_id
  1146. AND t1.forum_id = t2.forum_id";
  1147. $db->sql_query($sql);
  1148. break;
  1149. default:
  1150. $sql = 'SELECT t1.topic_id
  1151. FROM ' .TOPICS_TABLE . ' t1, ' . TOPICS_TABLE . " t2
  1152. WHERE t1.topic_moved_id = t2.topic_id
  1153. AND t1.forum_id = t2.forum_id";
  1154. $result = $db->sql_query($sql);
  1155. $topic_id_ary = array();
  1156. while ($row = $db->sql_fetchrow($result))
  1157. {
  1158. $topic_id_ary[] = $row['topic_id'];
  1159. }
  1160. $db->sql_freeresult($result);
  1161. if (!sizeof($topic_id_ary))
  1162. {
  1163. return;
  1164. }
  1165. $sql = 'DELETE FROM ' . TOPICS_TABLE . '
  1166. WHERE ' . $db->sql_in_set('topic_id', $topic_id_ary);
  1167. $db->sql_query($sql);
  1168. break;
  1169. }
  1170. $db->sql_transaction('commit');
  1171. break;
  1172. case 'topic_approved':
  1173. $db->sql_transaction('begin');
  1174. switch ($db->sql_layer)
  1175. {
  1176. case 'mysql4':
  1177. case 'mysqli':
  1178. $sql = 'UPDATE ' . TOPICS_TABLE . ' t, ' . POSTS_TABLE . " p
  1179. SET t.topic_approved = p.post_approved
  1180. $where_sql_and t.topic_first_post_id = p.post_id";
  1181. $db->sql_query($sql);
  1182. break;
  1183. default:
  1184. $sql = 'SELECT t.topic_id, p.post_approved
  1185. FROM ' . TOPICS_TABLE . ' t, ' . POSTS_TABLE . " p
  1186. $where_sql_and p.post_id = t.topic_first_post_id
  1187. AND p.post_approved <> t.topic_approved";
  1188. $result = $db->sql_query($sql);
  1189. $topic_ids = array();
  1190. while ($row = $db->sql_fetchrow($result))
  1191. {
  1192. $topic_ids[] = $row['topic_id'];
  1193. }
  1194. $db->sql_freeresult($result);
  1195. if (!sizeof($topic_ids))
  1196. {
  1197. return;
  1198. }
  1199. $sql = 'UPDATE ' . TOPICS_TABLE . '
  1200. SET topic_approved = 1 - topic_approved
  1201. WHERE ' . $db->sql_in_set('topic_id', $topic_ids);
  1202. $db->sql_query($sql);
  1203. break;
  1204. }
  1205. $db->sql_transaction('commit');
  1206. break;
  1207. case 'post_reported':
  1208. $post_ids = $post_reported = array();
  1209. $db->sql_transaction('begin');
  1210. $sql = 'SELECT p.post_id, p.post_reported
  1211. FROM ' . POSTS_TABLE . " p
  1212. $where_sql
  1213. GROUP BY p.post_id, p.post_reported";
  1214. $result = $db->sql_query($sql);
  1215. while ($row = $db->sql_fetchrow($result))
  1216. {
  1217. $post_ids[$row['post_id']] = $row['post_id'];
  1218. if ($row['post_reported'])
  1219. {
  1220. $post_reported[$row['post_id']] = 1;
  1221. }
  1222. }
  1223. $db->sql_freeresult($result);
  1224. $sql = 'SELECT DISTINCT(post_id)
  1225. FROM ' . REPORTS_TABLE . '
  1226. WHERE ' . $db->sql_in_set('post_id', $post_ids) . '
  1227. AND report_closed = 0';
  1228. $result = $db->sql_query($sql);
  1229. $post_ids = array();
  1230. while ($row = $db->sql_fetchrow($result))
  1231. {
  1232. if (!isset($post_reported[$row['post_id']]))
  1233. {
  1234. $post_ids[] = $row['post_id'];
  1235. }
  1236. else
  1237. {
  1238. unset($post_reported[$row['post_id']]);
  1239. }
  1240. }
  1241. $db->sql_freeresult($result);
  1242. // $post_reported should be empty by now, if it's not it contains
  1243. // posts that are falsely flagged as reported
  1244. foreach ($post_reported as $post_id => $void)
  1245. {
  1246. $post_ids[] = $post_id;
  1247. }
  1248. if (sizeof($post_ids))
  1249. {
  1250. $sql = 'UPDATE ' . POSTS_TABLE . '
  1251. SET post_reported = 1 - post_reported
  1252. WHERE ' . $db->sql_in_set('post_id', $post_ids);
  1253. $db->sql_query($sql);
  1254. }
  1255. $db->sql_transaction('commit');
  1256. break;
  1257. case 'topic_reported':
  1258. if ($sync_extra)
  1259. {
  1260. sync('post_reported', $where_type, $where_ids);
  1261. }
  1262. $topic_ids = $topic_reported = array();
  1263. $db->sql_transaction('begin');
  1264. $sql = 'SELECT DISTINCT(t.topic_id)
  1265. FROM ' . POSTS_TABLE . " t
  1266. $where_sql_and t.post_reported = 1";
  1267. $result = $db->sql_query($sql);
  1268. while ($row = $db->sql_fetchrow($result))
  1269. {
  1270. $topic_reported[$row['topic_id']] = 1;
  1271. }
  1272. $db->sql_freeresult($result);
  1273. $sql = 'SELECT t.topic_id, t.topic_reported
  1274. FROM ' . TOPICS_TABLE . " t
  1275. $where_sql";
  1276. $result = $db->sql_query($sql);
  1277. while ($row = $db->sql_fetchrow($result))
  1278. {
  1279. if ($row['topic_reported'] ^ isset($topic_reported[$row['topic_id']]))
  1280. {
  1281. $topic_ids[] = $row['topic_id'];
  1282. }
  1283. }
  1284. $db->sql_freeresult($result);
  1285. if (sizeof($topic_ids))
  1286. {
  1287. $sql = 'UPDATE ' . TOPICS_TABLE . '
  1288. SET topic_reported = 1 - topic_reported
  1289. WHERE ' . $db->sql_in_set('topic_id', $topic_ids);
  1290. $db->sql_query($sql);
  1291. }
  1292. $db->sql_transaction('commit');
  1293. break;
  1294. case 'post_attachment':
  1295. $post_ids = $post_attachment = array();
  1296. $db->sql_transaction('begin');
  1297. $sql = 'SELECT p.post_id, p.post_attachment
  1298. FROM ' . POSTS_TABLE . " p
  1299. $where_sql
  1300. GROUP BY p.post_id, p.post_attachment";
  1301. $result = $db->sql_query($sql);
  1302. while ($row = $db->sql_fetchrow($result))
  1303. {
  1304. $post_ids[$row['post_id']] = $row['post_id'];
  1305. if ($row['post_attachment'])
  1306. {
  1307. $post_attachment[$row['post_id']] = 1;
  1308. }
  1309. }
  1310. $db->sql_freeresult($result);
  1311. $sql = 'SELECT DISTINCT(post_msg_id)
  1312. FROM ' . ATTACHMENTS_TABLE . '
  1313. WHERE ' . $db->sql_in_set('post_msg_id', $post_ids) . '
  1314. AND in_message = 0';
  1315. $result = $db->sql_query($sql);
  1316. $post_ids = array();
  1317. while ($row = $db->sql_fetchrow($result))
  1318. {
  1319. if (!isset($post_attachment[$row['post_msg_id']]))
  1320. {
  1321. $post_ids[] = $row['post_msg_id'];
  1322. }
  1323. else
  1324. {
  1325. unset($post_attachment[$row['post_msg_id']]);
  1326. }
  1327. }
  1328. $db->sql_freeresult($result);
  1329. // $post_attachment should be empty by now, if it's not it contains
  1330. // posts that are falsely flagged as having attachments
  1331. foreach ($post_attachment as $post_id => $void)
  1332. {
  1333. $post_ids[] = $post_id;
  1334. }
  1335. if (sizeof($post_ids))
  1336. {
  1337. $sql = 'UPDATE ' . POSTS_TABLE . '
  1338. SET post_attachment = 1 - post_attachment
  1339. WHERE ' . $db->sql_in_set('post_id', $post_ids);
  1340. $db->sql_query($sql);
  1341. }
  1342. $db->sql_transaction('commit');
  1343. break;
  1344. case 'topic_attachment':
  1345. if ($sync_extra)
  1346. {
  1347. sync('post_attachment', $where_type, $where_ids);
  1348. }
  1349. $topic_ids = $topic_attachment = array();
  1350. $db->sql_transaction('begin');
  1351. $sql = 'SELECT DISTINCT(t.topic_id)
  1352. FROM ' . POSTS_TABLE . " t
  1353. $where_sql_and t.post_attachment = 1";
  1354. $result = $db->sql_query($sql);
  1355. while ($row = $db->sql_fetchrow($result))
  1356. {
  1357. $topic_attachment[$row['topic_id']] = 1;
  1358. }
  1359. $db->sql_freeresult($result);
  1360. $sql = 'SELECT t.topic_id, t.topic_attachment
  1361. FROM ' . TOPICS_TABLE . " t
  1362. $where_sql";
  1363. $result = $db->sql_query($sql);
  1364. while ($row = $db->sql_fetchrow($result))
  1365. {
  1366. if ($row['topic_attachment'] ^ isset($topic_attachment[$row['topic_id']]))
  1367. {
  1368. $topic_ids[] = $row['topic_id'];
  1369. }
  1370. }
  1371. $db->sql_freeresult($result);
  1372. if (sizeof($topic_ids))
  1373. {
  1374. $sql = 'UPDATE ' . TOPICS_TABLE . '
  1375. SET topic_attachment = 1 - topic_attachment
  1376. WHERE ' . $db->sql_in_set('topic_id', $topic_ids);
  1377. $db->sql_query($sql);
  1378. }
  1379. $db->sql_transaction('commit');
  1380. break;
  1381. case 'forum':
  1382. $db->sql_transaction('begin');
  1383. // 1: Get the list of all forums
  1384. $sql = 'SELECT f.*
  1385. FROM ' . FORUMS_TABLE . " f
  1386. $where_sql";
  1387. $result = $db->sql_query($sql);
  1388. $forum_data = $forum_ids = $post_ids = $last_post_id = $post_info = array();
  1389. while ($row = $db->sql_fetchrow($result))
  1390. {
  1391. if ($row['forum_type'] == FORUM_LINK)
  1392. {
  1393. continue;
  1394. }
  1395. $forum_id = (int) $row['forum_id'];
  1396. $forum_ids[$forum_id] = $forum_id;
  1397. $forum_data[$forum_id] = $row;
  1398. if ($sync_extra)
  1399. {
  1400. $forum_data[$forum_id]['posts'] = 0;
  1401. $forum_data[$forum_id]['topics'] = 0;
  1402. $forum_data[$forum_id]['topics_real'] = 0;
  1403. }
  1404. $forum_data[$forum_id]['last_post_id'] = 0;
  1405. $forum_data[$forum_id]['last_post_subject'] = '';
  1406. $forum_data[$forum_id]['last_post_time'] = 0;
  1407. $forum_data[$forum_id]['last_poster_id'] = 0;
  1408. $forum_data[$forum_id]['last_poster_name'] = '';
  1409. $forum_data[$forum_id]['last_poster_colour'] = '';
  1410. }
  1411. $db->sql_freeresult($result);
  1412. if (!sizeof($forum_ids))
  1413. {
  1414. break;
  1415. }
  1416. $forum_ids = array_values($forum_ids);
  1417. // 2: Get topic counts for each forum (optional)
  1418. if ($sync_extra)
  1419. {
  1420. $sql = 'SELECT forum_id, topic_approved, COUNT(topic_id) AS forum_topics
  1421. FROM ' . TOPICS_TABLE . '
  1422. WHERE ' . $db->sql_in_set('forum_id', $forum_ids) . '
  1423. GROUP BY forum_id, topic_approved';
  1424. $result = $db->sql_query($sql);
  1425. while ($row = $db->sql_fetchrow($result))
  1426. {
  1427. $forum_id = (int) $row['forum_id'];
  1428. $forum_data[$forum_id]['topics_real'] += $row['forum_topics'];
  1429. if ($row['topic_approved'])
  1430. {
  1431. $forum_data[$forum_id]['topics'] = $row['forum_topics'];
  1432. }
  1433. }
  1434. $db->sql_freeresult($result);
  1435. }
  1436. // 3: Get post count for each forum (optional)
  1437. if ($sync_extra)
  1438. {
  1439. if (sizeof($forum_ids) == 1)
  1440. {
  1441. $sql = 'SELECT SUM(t.topic_replies + 1) AS forum_posts
  1442. FROM ' . TOPICS_TABLE . ' t
  1443. WHERE ' . $db->sql_in_set('t.forum_id', $forum_ids) . '
  1444. AND t.topic_approved = 1
  1445. AND t.topic_status <> ' . ITEM_MOVED;
  1446. }
  1447. else
  1448. {
  1449. $sql = 'SELECT t.forum_id, SUM(t.topic_replies + 1) AS forum_posts
  1450. FROM ' . TOPICS_TABLE . ' t
  1451. WHERE ' . $db->sql_in_set('t.forum_id', $forum_ids) . '
  1452. AND t.topic_approved = 1
  1453. AND t.topic_status <> ' . ITEM_MOVED . '
  1454. GROUP BY t.forum_id';
  1455. }
  1456. $result = $db->sql_query($sql);
  1457. while ($row = $db->sql_fetchrow($result))
  1458. {
  1459. $forum_id = (sizeof($forum_ids) == 1) ? (int) $forum_ids[0] : (int) $row['forum_id'];
  1460. $forum_data[$forum_id]['posts'] = (int) $row['forum_posts'];
  1461. }
  1462. $db->sql_freeresult($result);
  1463. }
  1464. // 4: Get last_post_id for each forum
  1465. if (sizeof($forum_ids) == 1)
  1466. {
  1467. $sql = 'SELECT MAX(t.topic_last_post_id) as last_post_id
  1468. FROM ' . TOPICS_TABLE . ' t
  1469. WHERE ' . $db->sql_in_set('t.forum_id', $forum_ids) . '
  1470. AND t.topic_approved = 1';
  1471. }
  1472. else
  1473. {
  1474. $sql = 'SELECT t.forum_id, MAX(t.topic_last_post_id) as last_post_id
  1475. FROM ' . TOPICS_TABLE . ' t
  1476. WHERE ' . $db->sql_in_set('t.forum_id', $forum_ids) . '
  1477. AND t.topic_approved = 1
  1478. GROUP BY t.forum_id';
  1479. }
  1480. //-- mod: Prime Trash Bin (Topics) ------------------------------------------//
  1481. // Here we update the last post information for the forums.
  1482. global $phpbb_root_path, $phpEx;
  1483. include ($phpbb_root_path . 'includes/prime_trash_bin_a.' . $phpEx);
  1484. if (stifle_topics_enabled())
  1485. {
  1486. // Get the last topic that isn't deleted, except for when doing the Trash forum.
  1487. $sql = str_replace('AND t.topic_approved', 'AND (t.topic_deleted_time = 0' . (get_trash_forum() ? ' OR t.forum_id = ' . get_trash_forum() : '') . ') AND t.topic_approved', $sql);
  1488. }
  1489. //-- end: Prime Trash Bin (Topics) ------------------------------------------//
  1490. $result = $db->sql_query($sql);
  1491. while ($row = $db->sql_fetchrow($result))
  1492. {
  1493. $forum_id = (sizeof($forum_ids) == 1) ? (int) $forum_ids[0] : (int) $row['forum_id'];
  1494. $forum_data[$forum_id]['last_post_id'] = (int) $row['last_post_id'];
  1495. $post_ids[] = $row['last_post_id'];
  1496. }
  1497. $db->sql_freeresult($result);
  1498. // 5: Retrieve last_post infos
  1499. if (sizeof($post_ids))
  1500. {
  1501. $sql = 'SELECT p.post_id, p.poster_id, p.post_subject, p.post_time, p.post_username, u.username, u.user_colour
  1502. FROM ' . POSTS_TABLE . ' p, ' . USERS_TABLE . ' u
  1503. WHERE ' . $db->sql_in_set('p.post_id', $post_ids) . '
  1504. AND p.poster_id = u.user_id';
  1505. $result = $db->sql_query($sql);
  1506. while ($row = $db->sql_fetchrow($result))
  1507. {
  1508. $post_info[$row['post_id']] = $row;
  1509. }
  1510. $db->sql_freeresult($result);
  1511. foreach ($forum_data as $forum_id => $data)
  1512. {
  1513. if ($data['last_post_id'])
  1514. {
  1515. if (isset($post_info[$data['last_post_id']]))
  1516. {
  1517. $forum_data[$forum_id]['last_post_subject'] = $post_info[$data['last_post_id']]['post_subject'];
  1518. $forum_data[$forum_id]['last_post_time'] = $post_info[$data['last_post_id']]['post_time'];
  1519. $forum_data[$forum_id]['last_poster_id'] = $post_info[$data['last_post_id']]['poster_id'];
  1520. $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'];
  1521. $forum_data[$forum_id]['last_poster_colour'] = $post_info[$data['last_post_id']]['user_colour'];
  1522. }
  1523. else
  1524. {
  1525. // For some reason we did not find the post in the db
  1526. $forum_data[$forum_id]['last_post_id'] = 0;
  1527. $forum_data[$forum_id]['last_post_subject'] = '';
  1528. $forum_data[$forum_id]['last_post_time'] = 0;
  1529. $forum_data[$forum_id]['last_poster_id'] = 0;
  1530. $forum_data[$forum_id]['last_poster_name'] = '';
  1531. $forum_data[$forum_id]['last_poster_colour'] = '';
  1532. }
  1533. }
  1534. }
  1535. unset($post_info);
  1536. }
  1537. // 6: Now do that thing
  1538. $fieldnames = array('last_post_id', 'last_post_subject', 'last_post_time', 'last_poster_id', 'last_poster_name', 'last_poster_colour');
  1539. if ($sync_extra)
  1540. {
  1541. array_push($fieldnames, 'posts', 'topics', 'topics_real');
  1542. }
  1543. foreach ($forum_data as $forum_id => $row)
  1544. {
  1545. $sql_ary = array();
  1546. foreach ($fieldnames as $fieldname)
  1547. {
  1548. if ($row['forum_' . $fieldname] != $row[$fieldname])
  1549. {
  1550. if (preg_match('#(name|colour|subject)$#', $fieldname))
  1551. {
  1552. $sql_ary['forum_' . $fieldname] = (string) $row[$fieldname];
  1553. }
  1554. else
  1555. {
  1556. $sql_ary['forum_' . $fieldname] = (int) $row[$fieldname];
  1557. }
  1558. }
  1559. }
  1560. if (sizeof($sql_ary))
  1561. {
  1562. $sql = 'UPDATE ' . FORUMS_TABLE . '
  1563. SET ' . $db->sql_build_array('UPDATE', $sql_ary) . '
  1564. WHERE forum_id = ' . $forum_id;
  1565. $db->sql_query($sql);
  1566. }
  1567. }
  1568. $db->sql_transaction('commit');
  1569. break;
  1570. case 'topic':
  1571. $topic_data = $post_ids = $approved_unapproved_ids = $resync_forums = $delete_topics = $delete_posts = $moved_topics = array();
  1572. $db->sql_transaction('begin');
  1573. $sql = 'SELECT t.topic_id, t.forum_id, t.topic_moved_id, t.topic_approved, ' . (($sync_extra) ? 't.topic_attachment, t.topic_reported, ' : '') . 't.topic_poster, t.topic_time, t.topic_replies, t.topic_replies_real, t.topic_first_post_id, t.topic_first_poster_name, t.topic_first_poster_colour, t.topic_last_post_id, t.topic_last_post_subject, t.topic_last_poster_id, t.topic_last_poster_name, t.topic_last_poster_colour, t.topic_last_post_time
  1574. FROM ' . TOPICS_TABLE . " t
  1575. $where_sql";
  1576. $result = $db->sql_query($sql);
  1577. while ($row = $db->sql_fetchrow($result))
  1578. {
  1579. if ($row['topic_moved_id'])
  1580. {
  1581. $moved_topics[] = $row['topic_id'];
  1582. continue;
  1583. }
  1584. $topic_id = (int) $row['topic_id'];
  1585. $topic_data[$topic_id] = $row;
  1586. $topic_data[$topic_id]['replies_real'] = -1;
  1587. $topic_data[$topic_id]['replies'] = 0;
  1588. $topic_data[$topic_id]['first_post_id'] = 0;
  1589. $topic_data[$topic_id]['last_post_id'] = 0;
  1590. unset($topic_data[$topic_id]['topic_id']);
  1591. // This array holds all topic_ids
  1592. $delete_topics[$topic_id] = '';
  1593. if ($sync_extra)
  1594. {
  1595. $topic_data[$topic_id]['reported'] = 0;
  1596. $topic_data[$topic_id]['attachment'] = 0;
  1597. }
  1598. }
  1599. $db->sql_freeresult($result);
  1600. //-- mod: Prime Trash Bin (Posts) -------------------------------------------//
  1601. // Here we update the last post information for the topics.
  1602. global $phpbb_root_path, $phpEx;
  1603. include ($phpbb_root_path . 'includes/prime_trash_bin_a.' . $phpEx);
  1604. if (stifle_posts_enabled())
  1605. {
  1606. // Get the last topic that isn't deleted, except for when doing the Trash forum.
  1607. $where_sql2 = str_replace('WHERE ', 'WHERE (t.post_deleted_time = 0' . (get_trash_forum() ? ' OR t.forum_id = ' . get_trash_forum() : '') . ') AND ', $where_sql);
  1608. $sql = 'SELECT t.topic_id, MIN(t.post_id) AS first_post_id, MAX(t.post_id) AS last_post_id
  1609. FROM ' . POSTS_TABLE . " t
  1610. $where_sql2
  1611. GROUP BY t.topic_id";
  1612. $result = $db->sql_query($sql);
  1613. while ($row2 = $db->sql_fetchrow($result))
  1614. {
  1615. $topic_id = (int) $row2['topic_id'];
  1616. $first_post_ids[$topic_id] = $row2['first_post_id'];
  1617. $last_post_ids[$topic_id] = $row2['last_post_id'];
  1618. }
  1619. $db->sql_freeresult($result);
  1620. }
  1621. //-- end: Prime Trash Bin (Posts) -------------------------------------------//
  1622. // Use "t" as table alias because of the $where_sql clause
  1623. // NOTE: 't.post_approved' in the GROUP BY is causing a major slowdown.
  1624. $sql = 'SELECT t.topic_id, t.post_approved, COUNT(t.post_id) AS total_posts, MIN(t.post_id) AS first_post_id, MAX(t.post_id) AS last_post_id
  1625. FROM ' . POSTS_TABLE . " t
  1626. $where_sql
  1627. GROUP BY t.topic_id, t.post_approved";
  1628. $result = $db->sql_query($sql);
  1629. while ($row = $db->sql_fetchrow($result))
  1630. {
  1631. $topic_id = (int) $row['topic_id'];
  1632. $row['first_post_id'] = (int) $row['first_post_id'];
  1633. $row['last_post_id'] = (int) $row['last_post_id'];
  1634. //-- mod: Prime Trash Bin (Posts) -------------------------------------------//
  1635. // Here we update the last post information for the topics.
  1636. if (stifle_posts_enabled())
  1637. {
  1638. if (!empty($first_post_ids[$topic_id]))
  1639. {
  1640. $row['first_post_id'] = (int) $first_post_ids[$topic_id];
  1641. }
  1642. if (!empty($last_post_ids[$topic_id]))
  1643. {
  1644. $row['last_post_id'] = (int) $last_post_ids[$topic_id];
  1645. }
  1646. }
  1647. //-- end: Prime Trash Bin (Posts) -------------------------------------------//
  1648. if (!isset($topic_data[$topic_id]))
  1649. {
  1650. // Hey, these posts come from a topic that does not exist
  1651. $delete_posts[$topic_id] = '';
  1652. }
  1653. else
  1654. {
  1655. // Unset the corresponding entry in $delete_topics
  1656. // When we'll be done, only topics with no posts will remain
  1657. unset($delete_topics[$topic_id]);
  1658. $topic_data[$topic_id]['replies_real'] += $row['total_posts'];
  1659. $topic_data[$topic_id]['first_post_id'] = (!$topic_data[$topic_id]['first_post_id']) ? $row['first_post_id'] : min($topic_data[$topic_id]['first_post_id'], $row['first_post_id']);
  1660. if ($row['post_approved'] || !$topic_data[$topic_id]['last_post_id'])
  1661. {
  1662. $topic_data[$topic_id]['replies'] = $row['total_posts'] - 1;
  1663. $topic_data[$topic_id]['last_post_id'] = $row['last_post_id'];
  1664. }
  1665. }
  1666. }
  1667. $db->sql_freeresult($result);
  1668. foreach ($topic_data as $topic_id => $row)
  1669. {
  1670. $post_ids[] = $row['first_post_id'];
  1671. if ($row['first_post_id'] != $row['last_post_id'])
  1672. {
  1673. $post_ids[] = $row['last_post_id'];
  1674. }
  1675. }
  1676. // Now we delete empty topics and orphan posts
  1677. if (sizeof($delete_posts))
  1678. {
  1679. delete_posts('topic_id', array_keys($delete_posts), false);
  1680. unset($delete_posts);
  1681. }
  1682. if (!sizeof($topic_data))
  1683. {
  1684. // If we get there, topic ids were invalid or topics did not contain any posts
  1685. delete_topics($where_type, $where_ids, true);
  1686. return;
  1687. }
  1688. if (sizeof($delete_topics))
  1689. {
  1690. $delete_topic_ids = array();
  1691. foreach ($delete_topics as $topic_id => $void)
  1692. {
  1693. unset($topic_data[$topic_id]);
  1694. $delete_topic_ids[] = $topic_id;
  1695. }
  1696. delete_topics('topic_id', $delete_topic_ids, false);
  1697. unset($delete_topics, $delete_topic_ids);
  1698. }
  1699. $sql = 'SELECT p.post_id, p.topic_id, p.post_approved, p.poster_id, p.post_subject, p.post_username, p.post_time, u.username, u.user_colour
  1700. FROM ' . POSTS_TABLE . ' p, ' . USERS_TABLE . ' u
  1701. WHERE ' . $db->sql_in_set('p.post_id', $post_ids) . '
  1702. AND u.user_id = p.poster_id';
  1703. $result = $db->sql_query($sql);
  1704. $post_ids = array();
  1705. while ($row = $db->sql_fetchrow($result))
  1706. {
  1707. $topic_id = intval($row['topic_id']);
  1708. if ($row['post_id'] == $topic_data[$topic_id]['first_post_id'])
  1709. {
  1710. if ($topic_data[$topic_id]['topic_approved'] != $row['post_approved'])
  1711. {
  1712. $approved_unapproved_ids[] = $topic_id;
  1713. }
  1714. $topic_data[$topic_id]['time'] = $row['post_time'];
  1715. $topic_data[$topic_id]['poster'] = $row['poster_id'];
  1716. $topic_data[$topic_id]['first_poster_name'] = ($row['poster_id'] == ANONYMOUS) ? $row['post_username'] : $row['username'];
  1717. $topic_data[$topic_id]['first_poster_colour'] = $row['user_colour'];
  1718. }
  1719. if ($row['post_id'] == $topic_data[$topic_id]['last_post_id'])
  1720. {
  1721. $topic_data[$topic_id]['last_poster_id'] = $row['poster_id'];
  1722. $topic_data[$topic_id]['last_post_subject'] = $row['post_subject'];
  1723. $topic_data[$topic_id]['last_post_time'] = $row['post_time'];
  1724. $topic_data[$topic_id]['last_poster_name'] = ($row['poster_id'] == ANONYMOUS) ? $row['post_username'] : $row['username'];
  1725. $topic_data[$topic_id]['last_poster_colour'] = $row['user_colour'];
  1726. }
  1727. }
  1728. $db->sql_freeresult($result);
  1729. // Make sure shadow topics do link to existing topics
  1730. if (sizeof($moved_topics))
  1731. {
  1732. $delete_topics = array();
  1733. $sql = 'SELECT t1.topic_id, t1.topic_moved_id
  1734. FROM ' . TOPICS_TABLE . ' t1
  1735. LEFT JOIN ' . TOPICS_TABLE . ' t2 ON (t2.topic_id = t1.topic_moved_id)
  1736. WHERE ' . $db->sql_in_set('t1.topic_id', $moved_topics) . '
  1737. AND t2.topic_id IS NULL';
  1738. $result = $db->sql_query($sql);
  1739. while ($row = $db->sql_fetchrow($result))
  1740. {
  1741. $delete_topics[] = $row['topic_id'];
  1742. }
  1743. $db->sql_freeresult($result);
  1744. if (sizeof($delete_topics))
  1745. {
  1746. delete_topics('topic_id', $delete_topics, false);
  1747. }
  1748. unset($delete_topics);
  1749. // Make sure shadow topics having no last post data being updated (this only rarely happens...)
  1750. $sql = 'SELECT topic_id, topic_moved_id, topic_last_post_id, topic_first_post_id
  1751. FROM ' . TOPICS_TABLE . '
  1752. WHERE ' . $db->sql_in_set('topic_id', $moved_topics) . '
  1753. AND topic_last_post_time = 0';
  1754. $result = $db->sql_query($sql);
  1755. $shadow_topic_data = $post_ids = array();
  1756. while ($row = $db->sql_fetchrow($result))
  1757. {
  1758. $shadow_topic_data[$row['topic_moved_id']] = $row;
  1759. $post_ids[] = $row['topic_last_post_id'];
  1760. $post_ids[] = $row['topic_first_post_id'];
  1761. }
  1762. $db->sql_freeresult($result);
  1763. $sync_shadow_topics = array();
  1764. if (sizeof($post_ids))
  1765. {
  1766. $sql = 'SELECT p.post_id, p.topic_id, p.post_approved, p.poster_id, p.post_subject, p.post_username, p.post_time, u.username, u.user_colour
  1767. FROM ' . POSTS_TABLE . ' p, ' . USERS_TABLE . ' u
  1768. WHERE ' . $db->sql_in_set('p.post_id', $post_ids) . '
  1769. AND u.user_id = p.poster_id';
  1770. $result = $db->sql_query($sql);
  1771. $post_ids = array();
  1772. while ($row = $db->sql_fetchrow($result))
  1773. {
  1774. $topic_id = (int) $row['topic_id'];
  1775. // Ok, there should be a shadow topic. If there isn't, then there's something wrong with the db.
  1776. // However, there's not much we can do about it.
  1777. if (!empty($shadow_topic_data[$topic_id]))
  1778. {
  1779. if ($row['post_id'] == $shadow_topic_data[$topic_id]['topic_first_post_id'])
  1780. {
  1781. $orig_topic_id = $shadow_topic_data[$topic_id]['topic_id'];
  1782. if (!isset($sync_shadow_topics[$orig_topic_id]))
  1783. {
  1784. $sync_shadow_topics[$orig_topic_id] = array();
  1785. }
  1786. $sync_shadow_topics[$orig_topic_id]['topic_time'] = $row['post_time'];
  1787. $sync_shadow_topics[$orig_topic_id]['topic_poster'] = $row['poster_id'];
  1788. $sync_shadow_topics[$orig_topic_id]['topic_first_poster_name'] = ($row['poster_id'] == ANONYMOUS) ? $row['post_username'] : $row['username'];
  1789. $sync_shadow_topics[$orig_topic_id]['topic_first_poster_colour'] = $row['user_colour'];
  1790. }
  1791. if ($row['post_id'] == $shadow_topic_data[$topic_id]['topic_last_post_id'])
  1792. {
  1793. $orig_topic_id = $shadow_topic_data[$topic_id]['topic_id'];
  1794. if (!isset($sync_shadow_topics[$orig_topic_id]))
  1795. {
  1796. $sync_shadow_topics[$orig_topic_id] = array();
  1797. }
  1798. $sync_shadow_topics[$orig_topic_id]['topic_last_poster_id'] = $row['poster_id'];
  1799. $sync_shadow_topics[$orig_topic_id]['topic_last_post_subject'] = $row['post_subject'];
  1800. $sync_shadow_topics[$orig_topic_id]['topic_last_post_time'] = $row['post_time'];
  1801. $sync_shadow_topics[$orig_topic_id]['topic_last_poster_name'] = ($row['poster_id'] == ANONYMOUS) ? $row['post_username'] : $row['username'];
  1802. $sync_shadow_topics[$orig_topic_id]['topic_last_poster_colour'] = $row['user_colour'];
  1803. }
  1804. }
  1805. }
  1806. $db->sql_freeresult($result);
  1807. $shadow_topic_data = array();
  1808. // Update the information we collected
  1809. if (sizeof($sync_shadow_topics))
  1810. {
  1811. foreach ($sync_shadow_topics as $sync_topic_id => $sql_ary)
  1812. {
  1813. $sql = 'UPDATE ' . TOPICS_TABLE . '
  1814. SET ' . $db->sql_build_array('UPDATE', $sql_ary) . '
  1815. WHERE topic_id = ' . $sync_topic_id;
  1816. $db->sql_query($sql);
  1817. }
  1818. }
  1819. }
  1820. unset($sync_shadow_topics, $shadow_topic_data);
  1821. }
  1822. // approved becomes unapproved, and vice-versa
  1823. if (sizeof($approved_unapproved_ids))
  1824. {
  1825. $sql = 'UPDATE ' . TOPICS_TABLE . '
  1826. SET topic_approved = 1 - topic_approved
  1827. WHERE ' . $db->sql_in_set('topic_id', $approved_unapproved_ids);
  1828. $db->sql_query($sql);
  1829. }
  1830. unset($approved_unapproved_ids);
  1831. // These are fields that will be synchronised
  1832. $fieldnames = array('time', 'replies', 'replies_real', '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');
  1833. if ($sync_extra)
  1834. {
  1835. // This routine assumes that post_reported values are correct
  1836. // if they are not, use sync('post_reported') first
  1837. $sql = 'SELECT t.topic_id, p.post_id
  1838. FROM ' . TOPICS_TABLE . ' t, ' . POSTS_TABLE . " p
  1839. $where_sql_and p.topic_id = t.topic_id
  1840. AND p.post_reported = 1
  1841. GROUP BY t.topic_id, p.post_id";
  1842. $result = $db->sql_query($sql);
  1843. $fieldnames[] = 'reported';
  1844. while ($row = $db->sql_fetchrow($result))
  1845. {
  1846. $topic_data[intval($row['topic_id'])]['reported'] = 1;
  1847. }
  1848. $db->sql_freeresult($result);
  1849. // This routine assumes that post_attachment values are correct
  1850. // if they are not, use sync('post_attachment') first
  1851. $sql = 'SELECT t.topic_id, p.post_id
  1852. FROM ' . TOPICS_TABLE . ' t, ' . POSTS_TABLE . " p
  1853. $where_sql_and p.topic_id = t.topic_id
  1854. AND p.post_attachment = 1
  1855. GROUP BY t.topic_id, p.post_id";
  1856. $result = $db->sql_query($sql);
  1857. $fieldnames[] = 'attachment';
  1858. while ($row = $db->sql_fetchrow($result))
  1859. {
  1860. $topic_data[intval($row['topic_id'])]['attachment'] = 1;
  1861. }
  1862. $db->sql_freeresult($result);
  1863. }
  1864. foreach ($topic_data as $topic_id => $row)
  1865. {
  1866. $sql_ary = array();
  1867. foreach ($fieldnames as $fieldname)
  1868. {
  1869. if (isset($row[$fieldname]) && isset($row['topic_' . $fieldname]) && $row['topic_' . $fieldname] != $row[$fieldname])
  1870. {
  1871. $sql_ary['topic_' . $fieldname] = $row[$fieldname];
  1872. }
  1873. }
  1874. if (sizeof($sql_ary))
  1875. {
  1876. $sql = 'UPDATE ' . TOPICS_TABLE . '
  1877. SET ' . $db->sql_build_array('UPDATE', $sql_ary) . '
  1878. WHERE topic_id = ' . $topic_id;
  1879. $db->sql_query($sql);
  1880. $resync_forums[$row['forum_id']] = $row['forum_id'];
  1881. }
  1882. }
  1883. unset($topic_data);
  1884. $db->sql_transaction('commit');
  1885. // if some topics have been resync'ed then resync parent forums
  1886. // except when we're only syncing a range, we don't want to sync forums during
  1887. // batch processing.
  1888. if ($resync_parents && sizeof($resync_forums) && $where_type != 'range')
  1889. {
  1890. sync('forum', 'forum_id', array_values($resync_forums), true, true);
  1891. }
  1892. break;
  1893. }
  1894. return;
  1895. }
  1896. /**
  1897. * Prune function
  1898. */
  1899. function prune($forum_id, $prune_mode, $prune_date, $prune_flags = 0, $auto_sync = true)
  1900. {
  1901. global $db;
  1902. if (!is_array($forum_id))
  1903. {
  1904. $forum_id = array($forum_id);
  1905. }
  1906. if (!sizeof($forum_id))
  1907. {
  1908. return;
  1909. }
  1910. $sql_and = '';
  1911. if (!($prune_flags & FORUM_FLAG_PRUNE_ANNOUNCE))
  1912. {
  1913. $sql_and .= ' AND topic_type <> ' . POST_ANNOUNCE;
  1914. }
  1915. if (!($prune_flags & FORUM_FLAG_PRUNE_STICKY))
  1916. {
  1917. $sql_and .= ' AND topic_type <> ' . POST_STICKY;
  1918. }
  1919. if ($prune_mode == 'posted')
  1920. {
  1921. $sql_and .= " AND topic_last_post_time < $prune_date";
  1922. }
  1923. if ($prune_mode == 'viewed')
  1924. {
  1925. $sql_and .= " AND topic_last_view_time < $prune_date";
  1926. }
  1927. $sql = 'SELECT topic_id
  1928. FROM ' . TOPICS_TABLE . '
  1929. WHERE ' . $db->sql_in_set('forum_id', $forum_id) . "
  1930. AND poll_start = 0
  1931. $sql_and";
  1932. $result = $db->sql_query($sql);
  1933. $topic_list = array();
  1934. while ($row = $db->sql_fetchrow($result))
  1935. {
  1936. $topic_list[] = $row['topic_id'];
  1937. }
  1938. $db->sql_freeresult($result);
  1939. if ($prune_flags & FORUM_FLAG_PRUNE_POLL)
  1940. {
  1941. $sql = 'SELECT topic_id
  1942. FROM ' . TOPICS_TABLE . '
  1943. WHERE ' . $db->sql_in_set('forum_id', $forum_id) . "
  1944. AND poll_start > 0
  1945. AND poll_last_vote < $prune_date
  1946. $sql_and";
  1947. $result = $db->sql_query($sql);
  1948. while ($row = $db->sql_fetchrow($result))
  1949. {
  1950. $topic_list[] = $row['topic_id'];
  1951. }
  1952. $db->sql_freeresult($result);
  1953. $topic_list = array_unique($topic_list);
  1954. }
  1955. return delete_topics('topic_id', $topic_list, $auto_sync, false);
  1956. }
  1957. /**
  1958. * Function auto_prune(), this function now relies on passed vars
  1959. */
  1960. function auto_prune($forum_id, $prune_mode, $prune_flags, $prune_days, $prune_freq)
  1961. {
  1962. global $db;
  1963. $sql = 'SELECT forum_name
  1964. FROM ' . FORUMS_TABLE . "
  1965. WHERE forum_id = $forum_id";
  1966. $result = $db->sql_query($sql, 3600);
  1967. $row = $db->sql_fetchrow($result);
  1968. $db->sql_freeresult($result);
  1969. if ($row)
  1970. {
  1971. $prune_date = time() - ($prune_days * 86400);
  1972. $next_prune = time() + ($prune_freq * 86400);
  1973. prune($forum_id, $prune_mode, $prune_date, $prune_flags, true);
  1974. $sql = 'UPDATE ' . FORUMS_TABLE . "
  1975. SET prune_next = $next_prune
  1976. WHERE forum_id = $forum_id";
  1977. $db->sql_query($sql);
  1978. add_log('admin', 'LOG_AUTO_PRUNE', $row['forum_name']);
  1979. }
  1980. return;
  1981. }
  1982. /**
  1983. * remove_comments will strip the sql comment lines out of an uploaded sql file
  1984. * specifically for mssql and postgres type files in the install....
  1985. */
  1986. function remove_comments(&$output)
  1987. {
  1988. $lines = explode("\n", $output);
  1989. $output = '';
  1990. // try to keep mem. use down
  1991. $linecount = sizeof($lines);
  1992. $in_comment = false;
  1993. for ($i = 0; $i < $linecount; $i++)
  1994. {
  1995. if (trim($lines[$i]) == '/*')
  1996. {
  1997. $in_comment = true;
  1998. }
  1999. if (!$in_comment)
  2000. {
  2001. $output .= $lines[$i] . "\n";
  2002. }
  2003. if (trim($lines[$i]) == '*/')
  2004. {
  2005. $in_comment = false;
  2006. }
  2007. }
  2008. unset($lines);
  2009. return $output;
  2010. }
  2011. /**
  2012. * Cache moderators, called whenever permissions are changed via admin_permissions. Changes of username
  2013. * and group names must be carried through for the moderators table
  2014. */
  2015. function cache_moderators()
  2016. {
  2017. global $db, $cache, $auth, $phpbb_root_path, $phpEx;
  2018. // Remove cached sql results
  2019. $cache->destroy('sql', MODERATOR_CACHE_TABLE);
  2020. // Clear table
  2021. switch ($db->sql_layer)
  2022. {
  2023. case 'sqlite':
  2024. case 'firebird':
  2025. $db->sql_query('DELETE FROM ' . MODERATOR_CACHE_TABLE);
  2026. break;
  2027. default:
  2028. $db->sql_query('TRUNCATE TABLE ' . MODERATOR_CACHE_TABLE);
  2029. break;
  2030. }
  2031. // We add moderators who have forum moderator permissions without an explicit ACL_NEVER setting
  2032. $hold_ary = $ug_id_ary = $sql_ary = array();
  2033. // Grab all users having moderative options...
  2034. $hold_ary = $auth->acl_user_raw_data(false, 'm_%', false);
  2035. // Add users?
  2036. if (sizeof($hold_ary))
  2037. {
  2038. // At least one moderative option warrants a display
  2039. $ug_id_ary = array_keys($hold_ary);
  2040. // Remove users who have group memberships with DENY moderator permissions
  2041. $sql = $db->sql_build_query('SELECT', array(
  2042. 'SELECT' => 'a.forum_id, ug.user_id, g.group_id',
  2043. 'FROM' => array(
  2044. ACL_OPTIONS_TABLE => 'o',
  2045. USER_GROUP_TABLE => 'ug',
  2046. GROUPS_TABLE => 'g',
  2047. ACL_GROUPS_TABLE => 'a',
  2048. ),
  2049. 'LEFT_JOIN' => array(
  2050. array(
  2051. 'FROM' => array(ACL_ROLES_DATA_TABLE => 'r'),
  2052. 'ON' => 'a.auth_role_id = r.role_id'
  2053. )
  2054. ),
  2055. 'WHERE' => '(o.auth_option_id = a.auth_option_id OR o.auth_option_id = r.auth_option_id)
  2056. AND ((a.auth_setting = ' . ACL_NEVER . ' AND r.auth_setting IS NULL)
  2057. OR r.auth_setting = ' . ACL_NEVER . ')
  2058. AND a.group_id = ug.group_id
  2059. AND g.group_id = ug.group_id
  2060. AND NOT (ug.group_leader = 1 AND g.group_skip_auth = 1)
  2061. AND ' . $db->sql_in_set('ug.user_id', $ug_id_ary) . "
  2062. AND ug.user_pending = 0
  2063. AND o.auth_option " . $db->sql_like_expression('m_' . $db->any_char),
  2064. ));
  2065. $result = $db->sql_query($sql);
  2066. while ($row = $db->sql_fetchrow($result))
  2067. {
  2068. if (isset($hold_ary[$row['user_id']][$row['forum_id']]))
  2069. {
  2070. unset($hold_ary[$row['user_id']][$row['forum_id']]);
  2071. }
  2072. }
  2073. $db->sql_freeresult($result);
  2074. if (sizeof($hold_ary))
  2075. {
  2076. // Get usernames...
  2077. $sql = 'SELECT user_id, username
  2078. FROM ' . USERS_TABLE . '
  2079. WHERE ' . $db->sql_in_set('user_id', array_keys($hold_ary));
  2080. $result = $db->sql_query($sql);
  2081. $usernames_ary = array();
  2082. while ($row = $db->sql_fetchrow($result))
  2083. {
  2084. $usernames_ary[$row['user_id']] = $row['username'];
  2085. }
  2086. foreach ($hold_ary as $user_id => $forum_id_ary)
  2087. {
  2088. // Do not continue if user does not exist
  2089. if (!isset($usernames_ary[$user_id]))
  2090. {
  2091. continue;
  2092. }
  2093. foreach ($forum_id_ary as $forum_id => $auth_ary)
  2094. {
  2095. $sql_ary[] = array(
  2096. 'forum_id' => (int) $forum_id,
  2097. 'user_id' => (int) $user_id,
  2098. 'username' => (string) $usernames_ary[$user_id],
  2099. 'group_id' => 0,
  2100. 'group_name' => ''
  2101. );
  2102. }
  2103. }
  2104. }
  2105. }
  2106. // Now to the groups...
  2107. $hold_ary = $auth->acl_group_raw_data(false, 'm_%', false);
  2108. if (sizeof($hold_ary))
  2109. {
  2110. $ug_id_ary = array_keys($hold_ary);
  2111. // Make sure not hidden or special groups are involved...
  2112. $sql = 'SELECT group_name, group_id, group_type
  2113. FROM ' . GROUPS_TABLE . '
  2114. WHERE ' . $db->sql_in_set('group_id', $ug_id_ary);
  2115. $result = $db->sql_query($sql);
  2116. $groupnames_ary = array();
  2117. while ($row = $db->sql_fetchrow($result))
  2118. {
  2119. if ($row['group_type'] == GROUP_HIDDEN || $row['group_type'] == GROUP_SPECIAL)
  2120. {
  2121. unset($hold_ary[$row['group_id']]);
  2122. }
  2123. $groupnames_ary[$row['group_id']] = $row['group_name'];
  2124. }
  2125. $db->sql_freeresult($result);
  2126. foreach ($hold_ary as $group_id => $forum_id_ary)
  2127. {
  2128. // If there is no group, we do not assign it...
  2129. if (!isset($groupnames_ary[$group_id]))
  2130. {
  2131. continue;
  2132. }
  2133. foreach ($forum_id_ary as $forum_id => $auth_ary)
  2134. {
  2135. $flag = false;
  2136. foreach ($auth_ary as $auth_option => $setting)
  2137. {
  2138. // Make sure at least one ACL_YES option is set...
  2139. if ($setting == ACL_YES)
  2140. {
  2141. $flag = true;
  2142. break;
  2143. }
  2144. }
  2145. if (!$flag)
  2146. {
  2147. continue;
  2148. }
  2149. $sql_ary[] = array(
  2150. 'forum_id' => (int) $forum_id,
  2151. 'user_id' => 0,
  2152. 'username' => '',
  2153. 'group_id' => (int) $group_id,
  2154. 'group_name' => (string) $groupnames_ary[$group_id]
  2155. );
  2156. }
  2157. }
  2158. }
  2159. $db->sql_multi_insert(MODERATOR_CACHE_TABLE, $sql_ary);
  2160. }
  2161. /**
  2162. * View log
  2163. * If $log_count is set to false, we will skip counting all entries in the database.
  2164. */
  2165. 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 = '')
  2166. {
  2167. global $db, $user, $auth, $phpEx, $phpbb_root_path, $phpbb_admin_path;
  2168. $topic_id_list = $reportee_id_list = $is_auth = $is_mod = array();
  2169. $profile_url = (defined('IN_ADMIN')) ? append_sid("{$phpbb_admin_path}index.$phpEx", 'i=users&amp;mode=overview') : append_sid("{$phpbb_root_path}memberlist.$phpEx", 'mode=viewprofile');
  2170. switch ($mode)
  2171. {
  2172. case 'admin':
  2173. $log_type = LOG_ADMIN;
  2174. $sql_forum = '';
  2175. break;
  2176. case 'mod':
  2177. $log_type = LOG_MOD;
  2178. $sql_forum = '';
  2179. if ($topic_id)
  2180. {
  2181. $sql_forum = 'AND l.topic_id = ' . (int) $topic_id;
  2182. }
  2183. else if (is_array($forum_id))
  2184. {
  2185. $sql_forum = 'AND ' . $db->sql_in_set('l.forum_id', array_map('intval', $forum_id));
  2186. }
  2187. else if ($forum_id)
  2188. {
  2189. $sql_forum = 'AND l.forum_id = ' . (int) $forum_id;
  2190. }
  2191. break;
  2192. case 'user':
  2193. $log_type = LOG_USERS;
  2194. $sql_forum = 'AND l.reportee_id = ' . (int) $user_id;
  2195. break;
  2196. case 'users':
  2197. $log_type = LOG_USERS;
  2198. $sql_forum = '';
  2199. break;
  2200. case 'critical':
  2201. $log_type = LOG_CRITICAL;
  2202. $sql_forum = '';
  2203. break;
  2204. default:
  2205. return;
  2206. }
  2207. // Use no preg_quote for $keywords because this would lead to sole backslashes being added
  2208. // We also use an OR connection here for spaces and the | string. Currently, regex is not supported for searching (but may come later).
  2209. $keywords = preg_split('#[\s|]+#u', utf8_strtolower($keywords), 0, PREG_SPLIT_NO_EMPTY);
  2210. $sql_keywords = '';
  2211. if (!empty($keywords))
  2212. {
  2213. $keywords_pattern = array();
  2214. // Build pattern and keywords...
  2215. for ($i = 0, $num_keywords = sizeof($keywords); $i < $num_keywords; $i++)
  2216. {
  2217. $keywords_pattern[] = preg_quote($keywords[$i], '#');
  2218. $keywords[$i] = $db->sql_like_expression($db->any_char . $keywords[$i] . $db->any_char);
  2219. }
  2220. $keywords_pattern = '#' . implode('|', $keywords_pattern) . '#ui';
  2221. $operations = array();
  2222. foreach ($user->lang as $key => $value)
  2223. {
  2224. if (substr($key, 0, 4) == 'LOG_' && preg_match($keywords_pattern, $value))
  2225. {
  2226. $operations[] = $key;
  2227. }
  2228. }
  2229. $sql_keywords = 'AND (';
  2230. if (!empty($operations))
  2231. {
  2232. $sql_keywords .= $db->sql_in_set('l.log_operation', $operations) . ' OR ';
  2233. }
  2234. $sql_keywords .= 'LOWER(l.log_data) ' . implode(' OR LOWER(l.log_data) ', $keywords) . ')';
  2235. }
  2236. $sql = "SELECT l.*, u.username, u.username_clean, u.user_colour
  2237. FROM " . LOG_TABLE . " l, " . USERS_TABLE . " u
  2238. WHERE l.log_type = $log_type
  2239. AND u.user_id = l.user_id
  2240. " . (($limit_days) ? "AND l.log_time >= $limit_days" : '') . "
  2241. $sql_keywords
  2242. $sql_forum
  2243. ORDER BY $sort_by";
  2244. $result = $db->sql_query_limit($sql, $limit, $offset);
  2245. $i = 0;
  2246. $log = array();
  2247. while ($row = $db->sql_fetchrow($result))
  2248. {
  2249. if ($row['topic_id'])
  2250. {
  2251. $topic_id_list[] = $row['topic_id'];
  2252. }
  2253. if ($row['reportee_id'])
  2254. {
  2255. $reportee_id_list[] = $row['reportee_id'];
  2256. }
  2257. //-- mod: Prime Trash Bin ---------------------------------------------------//
  2258. // Here we set the log entry to display the optional "Reason" parameter if it exists.
  2259. $log_data_ary = unserialize($row['log_data']);
  2260. if (count($log_data_ary) == 2 && !empty($log_data_ary[1]) && in_array($row['log_operation'], array('LOG_TOPIC_STIFLED', 'LOG_TOPIC_TRASHED', 'LOG_TOPIC_UNSTIFLED', 'LOG_POST_STIFLED', 'LOG_POST_TRASHED', 'LOG_POST_UNSTIFLED')) && isset($user->lang[$row['log_operation'] . '_2']))
  2261. {
  2262. $row['log_operation'] = $row['log_operation'] . '_2';
  2263. }
  2264. //-- end: Prime Trash Bin ---------------------------------------------------//
  2265. $log[$i] = array(
  2266. 'id' => $row['log_id'],
  2267. 'reportee_id' => $row['reportee_id'],
  2268. 'reportee_username' => '',
  2269. 'reportee_username_full'=> '',
  2270. 'user_id' => $row['user_id'],
  2271. 'username' => $row['username'],
  2272. 'username_full' => get_username_string('full', $row['user_id'], $row['username'], $row['user_colour'], false, $profile_url),
  2273. 'ip' => $row['log_ip'],
  2274. 'time' => $row['log_time'],
  2275. 'forum_id' => $row['forum_id'],
  2276. 'topic_id' => $row['topic_id'],
  2277. 'viewforum' => ($row['forum_id'] && $auth->acl_get('f_read', $row['forum_id'])) ? append_sid("{$phpbb_root_path}viewforum.$phpEx", 'f=' . $row['forum_id']) : false,
  2278. 'action' => (isset($user->lang[$row['log_operation']])) ? $user->lang[$row['log_operation']] : '{' . ucfirst(str_replace('_', ' ', $row['log_operation'])) . '}',
  2279. );
  2280. if (!empty($row['log_data']))
  2281. {
  2282. $log_data_ary = @unserialize($row['log_data']);
  2283. $log_data_ary = ($log_data_ary === false) ? array() : $log_data_ary;
  2284. if (isset($user->lang[$row['log_operation']]))
  2285. {
  2286. // Check if there are more occurrences of % than arguments, if there are we fill out the arguments array
  2287. // It doesn't matter if we add more arguments than placeholders
  2288. if ((substr_count($log[$i]['action'], '%') - sizeof($log_data_ary)) > 0)
  2289. {
  2290. $log_data_ary = array_merge($log_data_ary, array_fill(0, substr_count($log[$i]['action'], '%') - sizeof($log_data_ary), ''));
  2291. }
  2292. $log[$i]['action'] = vsprintf($log[$i]['action'], $log_data_ary);
  2293. // If within the admin panel we do not censor text out
  2294. if (defined('IN_ADMIN'))
  2295. {
  2296. $log[$i]['action'] = bbcode_nl2br($log[$i]['action']);
  2297. }
  2298. else
  2299. {
  2300. $log[$i]['action'] = bbcode_nl2br(censor_text($log[$i]['action']));
  2301. }
  2302. }
  2303. else if (!empty($log_data_ary))
  2304. {
  2305. $log[$i]['action'] .= '<br />' . implode('', $log_data_ary);
  2306. }
  2307. /* Apply make_clickable... has to be seen if it is for good. :/
  2308. // Seems to be not for the moment, reconsider later...
  2309. $log[$i]['action'] = make_clickable($log[$i]['action']);
  2310. */
  2311. }
  2312. $i++;
  2313. }
  2314. $db->sql_freeresult($result);
  2315. if (sizeof($topic_id_list))
  2316. {
  2317. $topic_id_list = array_unique($topic_id_list);
  2318. // This query is not really needed if move_topics() updates the forum_id field,
  2319. // although it's also used to determine if the topic still exists in the database
  2320. $sql = 'SELECT topic_id, forum_id
  2321. FROM ' . TOPICS_TABLE . '
  2322. WHERE ' . $db->sql_in_set('topic_id', array_map('intval', $topic_id_list));
  2323. $result = $db->sql_query($sql);
  2324. $default_forum_id = 0;
  2325. while ($row = $db->sql_fetchrow($result))
  2326. {
  2327. if (!$row['forum_id'])
  2328. {
  2329. if ($auth->acl_getf_global('f_read'))
  2330. {
  2331. if (!$default_forum_id)
  2332. {
  2333. $sql = 'SELECT forum_id
  2334. FROM ' . FORUMS_TABLE . '
  2335. WHERE forum_type = ' . FORUM_POST;
  2336. $f_result = $db->sql_query_limit($sql, 1);
  2337. $default_forum_id = (int) $db->sql_fetchfield('forum_id', false, $f_result);
  2338. $db->sql_freeresult($f_result);
  2339. }
  2340. $is_auth[$row['topic_id']] = $default_forum_id;
  2341. }
  2342. }
  2343. else
  2344. {
  2345. if ($auth->acl_get('f_read', $row['forum_id']))
  2346. {
  2347. $is_auth[$row['topic_id']] = $row['forum_id'];
  2348. }
  2349. }
  2350. if ($auth->acl_gets('a_', 'm_', $row['forum_id']))
  2351. {
  2352. $is_mod[$row['topic_id']] = $row['forum_id'];
  2353. }
  2354. }
  2355. $db->sql_freeresult($result);
  2356. foreach ($log as $key => $row)
  2357. {
  2358. $log[$key]['viewtopic'] = (isset($is_auth[$row['topic_id']])) ? append_sid("{$phpbb_root_path}viewtopic.$phpEx", 'f=' . $is_auth[$row['topic_id']] . '&amp;t=' . $row['topic_id']) : false;
  2359. $log[$key]['viewlogs'] = (isset($is_mod[$row['topic_id']])) ? append_sid("{$phpbb_root_path}mcp.$phpEx", 'i=logs&amp;mode=topic_logs&amp;t=' . $row['topic_id'], true, $user->session_id) : false;
  2360. }
  2361. }
  2362. if (sizeof($reportee_id_list))
  2363. {
  2364. $reportee_id_list = array_unique($reportee_id_list);
  2365. $reportee_names_list = array();
  2366. $sql = 'SELECT user_id, username, user_colour
  2367. FROM ' . USERS_TABLE . '
  2368. WHERE ' . $db->sql_in_set('user_id', $reportee_id_list);
  2369. $result = $db->sql_query($sql);
  2370. while ($row = $db->sql_fetchrow($result))
  2371. {
  2372. $reportee_names_list[$row['user_id']] = $row;
  2373. }
  2374. $db->sql_freeresult($result);
  2375. foreach ($log as $key => $row)
  2376. {
  2377. if (!isset($reportee_names_list[$row['reportee_id']]))
  2378. {
  2379. continue;
  2380. }
  2381. $log[$key]['reportee_username'] = $reportee_names_list[$row['reportee_id']]['username'];
  2382. $log[$key]['reportee_username_full'] = get_username_string('full', $row['reportee_id'], $reportee_names_list[$row['reportee_id']]['username'], $reportee_names_list[$row['reportee_id']]['user_colour'], false, $profile_url);
  2383. }
  2384. }
  2385. if ($log_count !== false)
  2386. {
  2387. $sql = 'SELECT COUNT(l.log_id) AS total_entries
  2388. FROM ' . LOG_TABLE . ' l, ' . USERS_TABLE . " u
  2389. WHERE l.log_type = $log_type
  2390. AND l.user_id = u.user_id
  2391. AND l.log_time >= $limit_days
  2392. $sql_keywords
  2393. $sql_forum";
  2394. $result = $db->sql_query($sql);
  2395. $log_count = (int) $db->sql_fetchfield('total_entries');
  2396. $db->sql_freeresult($result);
  2397. }
  2398. return;
  2399. }
  2400. /**
  2401. * Update foes - remove moderators and administrators from foe lists...
  2402. */
  2403. function update_foes($group_id = false, $user_id = false)
  2404. {
  2405. global $db, $auth;
  2406. // update foes for some user
  2407. if (is_array($user_id) && sizeof($user_id))
  2408. {
  2409. $sql = 'DELETE FROM ' . ZEBRA_TABLE . '
  2410. WHERE ' . $db->sql_in_set('zebra_id', $user_id) . '
  2411. AND foe = 1';
  2412. $db->sql_query($sql);
  2413. return;
  2414. }
  2415. // update foes for some group
  2416. if (is_array($group_id) && sizeof($group_id))
  2417. {
  2418. // Grab group settings...
  2419. $sql = $db->sql_build_query('SELECT', array(
  2420. 'SELECT' => 'a.group_id',
  2421. 'FROM' => array(
  2422. ACL_OPTIONS_TABLE => 'ao',
  2423. ACL_GROUPS_TABLE => 'a'
  2424. ),
  2425. 'LEFT_JOIN' => array(
  2426. array(
  2427. 'FROM' => array(ACL_ROLES_DATA_TABLE => 'r'),
  2428. 'ON' => 'a.auth_role_id = r.role_id'
  2429. ),
  2430. ),
  2431. 'WHERE' => '(ao.auth_option_id = a.auth_option_id OR ao.auth_option_id = r.auth_option_id)
  2432. AND ' . $db->sql_in_set('a.group_id', $group_id) . "
  2433. AND ao.auth_option IN ('a_', 'm_')",
  2434. 'GROUP_BY' => 'a.group_id'
  2435. ));
  2436. $result = $db->sql_query($sql);
  2437. $groups = array();
  2438. while ($row = $db->sql_fetchrow($result))
  2439. {
  2440. $groups[] = (int) $row['group_id'];
  2441. }
  2442. $db->sql_freeresult($result);
  2443. if (!sizeof($groups))
  2444. {
  2445. return;
  2446. }
  2447. switch ($db->sql_layer)
  2448. {
  2449. case 'mysqli':
  2450. case 'mysql4':
  2451. $sql = 'DELETE ' . (($db->sql_layer === 'mysqli' || version_compare($db->sql_server_info(true), '4.1', '>=')) ? 'z.*' : ZEBRA_TABLE) . '
  2452. FROM ' . ZEBRA_TABLE . ' z, ' . USER_GROUP_TABLE . ' ug
  2453. WHERE z.zebra_id = ug.user_id
  2454. AND z.foe = 1
  2455. AND ' . $db->sql_in_set('ug.group_id', $groups);
  2456. $db->sql_query($sql);
  2457. break;
  2458. default:
  2459. $sql = 'SELECT user_id
  2460. FROM ' . USER_GROUP_TABLE . '
  2461. WHERE ' . $db->sql_in_set('group_id', $groups);
  2462. $result = $db->sql_query($sql);
  2463. $users = array();
  2464. while ($row = $db->sql_fetchrow($result))
  2465. {
  2466. $users[] = (int) $row['user_id'];
  2467. }
  2468. $db->sql_freeresult($result);
  2469. if (sizeof($users))
  2470. {
  2471. $sql = 'DELETE FROM ' . ZEBRA_TABLE . '
  2472. WHERE ' . $db->sql_in_set('zebra_id', $users) . '
  2473. AND foe = 1';
  2474. $db->sql_query($sql);
  2475. }
  2476. break;
  2477. }
  2478. return;
  2479. }
  2480. // update foes for everyone
  2481. $perms = array();
  2482. foreach ($auth->acl_get_list(false, array('a_', 'm_'), false) as $forum_id => $forum_ary)
  2483. {
  2484. foreach ($forum_ary as $auth_option => $user_ary)
  2485. {
  2486. $perms = array_merge($perms, $user_ary);
  2487. }
  2488. }
  2489. if (sizeof($perms))
  2490. {
  2491. $sql = 'DELETE FROM ' . ZEBRA_TABLE . '
  2492. WHERE ' . $db->sql_in_set('zebra_id', array_unique($perms)) . '
  2493. AND foe = 1';
  2494. $db->sql_query($sql);
  2495. }
  2496. unset($perms);
  2497. }
  2498. /**
  2499. * Lists inactive users
  2500. */
  2501. function view_inactive_users(&$users, &$user_count, $limit = 0, $offset = 0, $limit_days = 0, $sort_by = 'user_inactive_time DESC')
  2502. {
  2503. global $db, $user;
  2504. $sql = 'SELECT COUNT(user_id) AS user_count
  2505. FROM ' . USERS_TABLE . '
  2506. WHERE user_type = ' . USER_INACTIVE .
  2507. (($limit_days) ? " AND user_inactive_time >= $limit_days" : '');
  2508. $result = $db->sql_query($sql);
  2509. $user_count = (int) $db->sql_fetchfield('user_count');
  2510. $db->sql_freeresult($result);
  2511. if ($offset >= $user_count)
  2512. {
  2513. $offset = ($offset - $limit < 0) ? 0 : $offset - $limit;
  2514. }
  2515. $sql = 'SELECT *
  2516. FROM ' . USERS_TABLE . '
  2517. WHERE user_type = ' . USER_INACTIVE .
  2518. (($limit_days) ? " AND user_inactive_time >= $limit_days" : '') . "
  2519. ORDER BY $sort_by";
  2520. $result = $db->sql_query_limit($sql, $limit, $offset);
  2521. while ($row = $db->sql_fetchrow($result))
  2522. {
  2523. $row['inactive_reason'] = $user->lang['INACTIVE_REASON_UNKNOWN'];
  2524. switch ($row['user_inactive_reason'])
  2525. {
  2526. case INACTIVE_REGISTER:
  2527. $row['inactive_reason'] = $user->lang['INACTIVE_REASON_REGISTER'];
  2528. break;
  2529. case INACTIVE_PROFILE:
  2530. $row['inactive_reason'] = $user->lang['INACTIVE_REASON_PROFILE'];
  2531. break;
  2532. case INACTIVE_MANUAL:
  2533. $row['inactive_reason'] = $user->lang['INACTIVE_REASON_MANUAL'];
  2534. break;
  2535. case INACTIVE_REMIND:
  2536. $row['inactive_reason'] = $user->lang['INACTIVE_REASON_REMIND'];
  2537. break;
  2538. }
  2539. $users[] = $row;
  2540. }
  2541. return $offset;
  2542. }
  2543. /**
  2544. * Lists warned users
  2545. */
  2546. function view_warned_users(&$users, &$user_count, $limit = 0, $offset = 0, $limit_days = 0, $sort_by = 'user_warnings DESC')
  2547. {
  2548. global $db;
  2549. $sql = 'SELECT user_id, username, user_colour, user_warnings, user_last_warning
  2550. FROM ' . USERS_TABLE . '
  2551. WHERE user_warnings > 0
  2552. ' . (($limit_days) ? "AND user_last_warning >= $limit_days" : '') . "
  2553. ORDER BY $sort_by";
  2554. $result = $db->sql_query_limit($sql, $limit, $offset);
  2555. $users = $db->sql_fetchrowset($result);
  2556. $db->sql_freeresult($result);
  2557. $sql = 'SELECT count(user_id) AS user_count
  2558. FROM ' . USERS_TABLE . '
  2559. WHERE user_warnings > 0
  2560. ' . (($limit_days) ? "AND user_last_warning >= $limit_days" : '');
  2561. $result = $db->sql_query($sql);
  2562. $user_count = (int) $db->sql_fetchfield('user_count');
  2563. $db->sql_freeresult($result);
  2564. return;
  2565. }
  2566. /**
  2567. * Get database size
  2568. * Currently only mysql and mssql are supported
  2569. */
  2570. function get_database_size()
  2571. {
  2572. global $db, $user, $table_prefix;
  2573. $database_size = false;
  2574. // This code is heavily influenced by a similar routine in phpMyAdmin 2.2.0
  2575. switch ($db->sql_layer)
  2576. {
  2577. case 'mysql':
  2578. case 'mysql4':
  2579. case 'mysqli':
  2580. $sql = 'SELECT VERSION() AS mysql_version';
  2581. $result = $db->sql_query($sql);
  2582. $row = $db->sql_fetchrow($result);
  2583. $db->sql_freeresult($result);
  2584. if ($row)
  2585. {
  2586. $version = $row['mysql_version'];
  2587. if (preg_match('#(3\.23|[45]\.)#', $version))
  2588. {
  2589. $db_name = (preg_match('#^(?:3\.23\.(?:[6-9]|[1-9]{2}))|[45]\.#', $version)) ? "`{$db->dbname}`" : $db->dbname;
  2590. $sql = 'SHOW TABLE STATUS
  2591. FROM ' . $db_name;
  2592. $result = $db->sql_query($sql, 7200);
  2593. $database_size = 0;
  2594. while ($row = $db->sql_fetchrow($result))
  2595. {
  2596. if ((isset($row['Type']) && $row['Type'] != 'MRG_MyISAM') || (isset($row['Engine']) && ($row['Engine'] == 'MyISAM' || $row['Engine'] == 'InnoDB')))
  2597. {
  2598. if ($table_prefix != '')
  2599. {
  2600. if (strpos($row['Name'], $table_prefix) !== false)
  2601. {
  2602. $database_size += $row['Data_length'] + $row['Index_length'];
  2603. }
  2604. }
  2605. else
  2606. {
  2607. $database_size += $row['Data_length'] + $row['Index_length'];
  2608. }
  2609. }
  2610. }
  2611. $db->sql_freeresult($result);
  2612. }
  2613. }
  2614. break;
  2615. case 'firebird':
  2616. global $dbname;
  2617. // if it on the local machine, we can get lucky
  2618. if (file_exists($dbname))
  2619. {
  2620. $database_size = filesize($dbname);
  2621. }
  2622. break;
  2623. case 'sqlite':
  2624. global $dbhost;
  2625. if (file_exists($dbhost))
  2626. {
  2627. $database_size = filesize($dbhost);
  2628. }
  2629. break;
  2630. case 'mssql':
  2631. case 'mssql_odbc':
  2632. case 'mssqlnative':
  2633. $sql = 'SELECT ((SUM(size) * 8.0) * 1024.0) as dbsize
  2634. FROM sysfiles';
  2635. $result = $db->sql_query($sql, 7200);
  2636. $database_size = ($row = $db->sql_fetchrow($result)) ? $row['dbsize'] : false;
  2637. $db->sql_freeresult($result);
  2638. break;
  2639. case 'postgres':
  2640. $sql = "SELECT proname
  2641. FROM pg_proc
  2642. WHERE proname = 'pg_database_size'";
  2643. $result = $db->sql_query($sql);
  2644. $row = $db->sql_fetchrow($result);
  2645. $db->sql_freeresult($result);
  2646. if ($row['proname'] == 'pg_database_size')
  2647. {
  2648. $database = $db->dbname;
  2649. if (strpos($database, '.') !== false)
  2650. {
  2651. list($database, ) = explode('.', $database);
  2652. }
  2653. $sql = "SELECT oid
  2654. FROM pg_database
  2655. WHERE datname = '$database'";
  2656. $result = $db->sql_query($sql);
  2657. $row = $db->sql_fetchrow($result);
  2658. $db->sql_freeresult($result);
  2659. $oid = $row['oid'];
  2660. $sql = 'SELECT pg_database_size(' . $oid . ') as size';
  2661. $result = $db->sql_query($sql);
  2662. $row = $db->sql_fetchrow($result);
  2663. $db->sql_freeresult($result);
  2664. $database_size = $row['size'];
  2665. }
  2666. break;
  2667. case 'oracle':
  2668. $sql = 'SELECT SUM(bytes) as dbsize
  2669. FROM user_segments';
  2670. $result = $db->sql_query($sql, 7200);
  2671. $database_size = ($row = $db->sql_fetchrow($result)) ? $row['dbsize'] : false;
  2672. $db->sql_freeresult($result);
  2673. break;
  2674. }
  2675. $database_size = ($database_size !== false) ? get_formatted_filesize($database_size) : $user->lang['NOT_AVAILABLE'];
  2676. return $database_size;
  2677. }
  2678. /**
  2679. * Retrieve contents from remotely stored file
  2680. */
  2681. function get_remote_file($host, $directory, $filename, &$errstr, &$errno, $port = 80, $timeout = 10)
  2682. {
  2683. global $user;
  2684. if ($fsock = @fsockopen($host, $port, $errno, $errstr, $timeout))
  2685. {
  2686. @fputs($fsock, "GET $directory/$filename HTTP/1.1\r\n");
  2687. @fputs($fsock, "HOST: $host\r\n");
  2688. @fputs($fsock, "Connection: close\r\n\r\n");
  2689. $file_info = '';
  2690. $get_info = false;
  2691. while (!@feof($fsock))
  2692. {
  2693. if ($get_info)
  2694. {
  2695. $file_info .= @fread($fsock, 1024);
  2696. }
  2697. else
  2698. {
  2699. $line = @fgets($fsock, 1024);
  2700. if ($line == "\r\n")
  2701. {
  2702. $get_info = true;
  2703. }
  2704. else if (stripos($line, '404 not found') !== false)
  2705. {
  2706. $errstr = $user->lang['FILE_NOT_FOUND'] . ': ' . $filename;
  2707. return false;
  2708. }
  2709. }
  2710. }
  2711. @fclose($fsock);
  2712. }
  2713. else
  2714. {
  2715. if ($errstr)
  2716. {
  2717. $errstr = utf8_convert_message($errstr);
  2718. return false;
  2719. }
  2720. else
  2721. {
  2722. $errstr = $user->lang['FSOCK_DISABLED'];
  2723. return false;
  2724. }
  2725. }
  2726. return $file_info;
  2727. }
  2728. /**
  2729. * Tidy Warnings
  2730. * Remove all warnings which have now expired from the database
  2731. * The duration of a warning can be defined by the administrator
  2732. * This only removes the warning and reduces the associated count,
  2733. * it does not remove the user note recording the contents of the warning
  2734. */
  2735. function tidy_warnings()
  2736. {
  2737. global $db, $config;
  2738. $expire_date = time() - ($config['warnings_expire_days'] * 86400);
  2739. $warning_list = $user_list = array();
  2740. $sql = 'SELECT * FROM ' . WARNINGS_TABLE . "
  2741. WHERE warning_time < $expire_date";
  2742. $result = $db->sql_query($sql);
  2743. while ($row = $db->sql_fetchrow($result))
  2744. {
  2745. $warning_list[] = $row['warning_id'];
  2746. $user_list[$row['user_id']] = isset($user_list[$row['user_id']]) ? ++$user_list[$row['user_id']] : 1;
  2747. }
  2748. $db->sql_freeresult($result);
  2749. if (sizeof($warning_list))
  2750. {
  2751. $db->sql_transaction('begin');
  2752. $sql = 'DELETE FROM ' . WARNINGS_TABLE . '
  2753. WHERE ' . $db->sql_in_set('warning_id', $warning_list);
  2754. $db->sql_query($sql);
  2755. foreach ($user_list as $user_id => $value)
  2756. {
  2757. $sql = 'UPDATE ' . USERS_TABLE . " SET user_warnings = user_warnings - $value
  2758. WHERE user_id = $user_id";
  2759. $db->sql_query($sql);
  2760. }
  2761. $db->sql_transaction('commit');
  2762. }
  2763. set_config('warnings_last_gc', time(), true);
  2764. }
  2765. /**
  2766. * Tidy database, doing some maintanance tasks
  2767. */
  2768. function tidy_database()
  2769. {
  2770. global $db;
  2771. // Here we check permission consistency
  2772. // Sometimes, it can happen permission tables having forums listed which do not exist
  2773. $sql = 'SELECT forum_id
  2774. FROM ' . FORUMS_TABLE;
  2775. $result = $db->sql_query($sql);
  2776. $forum_ids = array(0);
  2777. while ($row = $db->sql_fetchrow($result))
  2778. {
  2779. $forum_ids[] = $row['forum_id'];
  2780. }
  2781. $db->sql_freeresult($result);
  2782. // Delete those rows from the acl tables not having listed the forums above
  2783. $sql = 'DELETE FROM ' . ACL_GROUPS_TABLE . '
  2784. WHERE ' . $db->sql_in_set('forum_id', $forum_ids, true);
  2785. $db->sql_query($sql);
  2786. $sql = 'DELETE FROM ' . ACL_USERS_TABLE . '
  2787. WHERE ' . $db->sql_in_set('forum_id', $forum_ids, true);
  2788. $db->sql_query($sql);
  2789. set_config('database_last_gc', time(), true);
  2790. }
  2791. /**
  2792. * Add permission language - this will make sure custom files will be included
  2793. */
  2794. function add_permission_language()
  2795. {
  2796. global $user, $phpEx;
  2797. // First of all, our own file. We need to include it as the first file because it presets all relevant variables.
  2798. $user->add_lang('acp/permissions_phpbb');
  2799. $files_to_add = array();
  2800. // Now search in acp and mods folder for permissions_ files.
  2801. foreach (array('acp/', 'mods/') as $path)
  2802. {
  2803. $dh = @opendir($user->lang_path . $user->lang_name . '/' . $path);
  2804. if ($dh)
  2805. {
  2806. while (($file = readdir($dh)) !== false)
  2807. {
  2808. if ($file !== 'permissions_phpbb.' . $phpEx && strpos($file, 'permissions_') === 0 && substr($file, -(strlen($phpEx) + 1)) === '.' . $phpEx)
  2809. {
  2810. $files_to_add[] = $path . substr($file, 0, -(strlen($phpEx) + 1));
  2811. }
  2812. }
  2813. closedir($dh);
  2814. }
  2815. }
  2816. if (!sizeof($files_to_add))
  2817. {
  2818. return false;
  2819. }
  2820. $user->add_lang($files_to_add);
  2821. return true;
  2822. }
  2823. /**
  2824. * Obtains the latest version information
  2825. *
  2826. * @param bool $force_update Ignores cached data. Defaults to false.
  2827. * @param bool $warn_fail Trigger a warning if obtaining the latest version information fails. Defaults to false.
  2828. * @param int $ttl Cache version information for $ttl seconds. Defaults to 86400 (24 hours).
  2829. *
  2830. * @return string | false Version info on success, false on failure.
  2831. */
  2832. function obtain_latest_version_info($force_update = false, $warn_fail = false, $ttl = 86400)
  2833. {
  2834. global $cache;
  2835. $info = $cache->get('versioncheck');
  2836. if ($info === false || $force_update)
  2837. {
  2838. $errstr = '';
  2839. $errno = 0;
  2840. // www.phpBB-SEO.com SEO TOOLKIT BEGIN
  2841. global $config;
  2842. $url = 'www.phpbb-seo.com';
  2843. $dir = (strpos($config['default_lang'], 'fr') !== false ? '/fr' : '/en') . '/updatecheck';
  2844. $info = get_remote_file($url, $dir, ((defined('PHPBB_SEO_QA')) ? 'test_30x.txt' : 'premod_30x.txt'), $errstr, $errno);
  2845. //$info = get_remote_file('version.phpbb.com', '/phpbb',
  2846. // ((defined('PHPBB_QA')) ? '30x_qa.txt' : '30x.txt'), $errstr, $errno);
  2847. // www.phpBB-SEO.com SEO TOOLKIT END
  2848. if ($info === false)
  2849. {
  2850. $cache->destroy('versioncheck');
  2851. if ($warn_fail)
  2852. {
  2853. trigger_error($errstr, E_USER_WARNING);
  2854. }
  2855. return false;
  2856. }
  2857. $cache->put('versioncheck', $info, $ttl);
  2858. }
  2859. return $info;
  2860. }
  2861. /**
  2862. * Enables a particular flag in a bitfield column of a given table.
  2863. *
  2864. * @param string $table_name The table to update
  2865. * @param string $column_name The column containing a bitfield to update
  2866. * @param int $flag The binary flag which is OR-ed with the current column value
  2867. * @param string $sql_more This string is attached to the sql query generated to update the table.
  2868. *
  2869. * @return void
  2870. */
  2871. function enable_bitfield_column_flag($table_name, $column_name, $flag, $sql_more = '')
  2872. {
  2873. global $db;
  2874. $sql = 'UPDATE ' . $table_name . '
  2875. SET ' . $column_name . ' = ' . $db->sql_bit_or($column_name, $flag) . '
  2876. ' . $sql_more;
  2877. $db->sql_query($sql);
  2878. }
  2879. ?>