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

/station/forum/includes/functions_admin.php

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