PageRenderTime 34ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 1ms

/includes/functions_admin.php

http://seo-phpbb.googlecode.com/
PHP | 3061 lines | 2266 code | 469 blank | 326 comment | 394 complexity | d3051af4b4898d8bceeef9536e64c944 MD5 | raw file
Possible License(s): AGPL-1.0
  1. <?php
  2. /**
  3. *
  4. * @package acp
  5. * @version $Id: functions_admin.php 8508 2008-04-20 04:58:29Z davidmj $
  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
  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'])
  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)
  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|topic|attach|user
  656. * @param mixed $ids can be: post_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. $sql_id = ($mode == 'user') ? 'poster_id' : (($mode == 'post') ? 'post_msg_id' : (($mode == 'topic') ? 'topic_id' : 'attach_id'));
  676. $post_ids = $topic_ids = $physical = array();
  677. // Collect post and topics ids for later use
  678. if ($mode == 'attach' || $mode == 'user' || ($mode == 'topic' && $resync))
  679. {
  680. $sql = 'SELECT post_msg_id as post_id, topic_id, physical_filename, thumbnail, filesize
  681. FROM ' . ATTACHMENTS_TABLE . '
  682. WHERE ' . $db->sql_in_set($sql_id, $ids);
  683. $result = $db->sql_query($sql);
  684. while ($row = $db->sql_fetchrow($result))
  685. {
  686. $post_ids[] = $row['post_id'];
  687. $topic_ids[] = $row['topic_id'];
  688. $physical[] = array('filename' => $row['physical_filename'], 'thumbnail' => $row['thumbnail'], 'filesize' => $row['filesize']);
  689. }
  690. $db->sql_freeresult($result);
  691. }
  692. if ($mode == 'post')
  693. {
  694. $sql = 'SELECT topic_id, physical_filename, thumbnail, filesize
  695. FROM ' . ATTACHMENTS_TABLE . '
  696. WHERE ' . $db->sql_in_set('post_msg_id', $ids) . '
  697. AND in_message = 0';
  698. $result = $db->sql_query($sql);
  699. while ($row = $db->sql_fetchrow($result))
  700. {
  701. $topic_ids[] = $row['topic_id'];
  702. $physical[] = array('filename' => $row['physical_filename'], 'thumbnail' => $row['thumbnail'], 'filesize' => $row['filesize']);
  703. }
  704. $db->sql_freeresult($result);
  705. }
  706. // Delete attachments
  707. $sql = 'DELETE FROM ' . ATTACHMENTS_TABLE . '
  708. WHERE ' . $db->sql_in_set($sql_id, $ids);
  709. $db->sql_query($sql);
  710. $num_deleted = $db->sql_affectedrows();
  711. if (!$num_deleted)
  712. {
  713. return 0;
  714. }
  715. // Delete attachments from filesystem
  716. $space_removed = $files_removed = 0;
  717. foreach ($physical as $file_ary)
  718. {
  719. if (phpbb_unlink($file_ary['filename'], 'file', true))
  720. {
  721. $space_removed += $file_ary['filesize'];
  722. $files_removed++;
  723. }
  724. if ($file_ary['thumbnail'])
  725. {
  726. phpbb_unlink($file_ary['filename'], 'thumbnail', true);
  727. }
  728. }
  729. set_config('upload_dir_size', $config['upload_dir_size'] - $space_removed, true);
  730. set_config('num_files', $config['num_files'] - $files_removed, true);
  731. if ($mode == 'topic' && !$resync)
  732. {
  733. return $num_deleted;
  734. }
  735. if ($mode == 'post')
  736. {
  737. $post_ids = $ids;
  738. }
  739. unset($ids);
  740. $post_ids = array_unique($post_ids);
  741. $topic_ids = array_unique($topic_ids);
  742. // Update post indicators
  743. if (sizeof($post_ids))
  744. {
  745. if ($mode == 'post' || $mode == 'topic')
  746. {
  747. $sql = 'UPDATE ' . POSTS_TABLE . '
  748. SET post_attachment = 0
  749. WHERE ' . $db->sql_in_set('post_id', $post_ids);
  750. $db->sql_query($sql);
  751. }
  752. if ($mode == 'user' || $mode == 'attach')
  753. {
  754. $remaining = array();
  755. $sql = 'SELECT post_msg_id
  756. FROM ' . ATTACHMENTS_TABLE . '
  757. WHERE ' . $db->sql_in_set('post_msg_id', $post_ids) . '
  758. AND in_message = 0';
  759. $result = $db->sql_query($sql);
  760. while ($row = $db->sql_fetchrow($result))
  761. {
  762. $remaining[] = $row['post_msg_id'];
  763. }
  764. $db->sql_freeresult($result);
  765. $unset_ids = array_diff($post_ids, $remaining);
  766. if (sizeof($unset_ids))
  767. {
  768. $sql = 'UPDATE ' . POSTS_TABLE . '
  769. SET post_attachment = 0
  770. WHERE ' . $db->sql_in_set('post_id', $unset_ids);
  771. $db->sql_query($sql);
  772. }
  773. $remaining = array();
  774. $sql = 'SELECT post_msg_id
  775. FROM ' . ATTACHMENTS_TABLE . '
  776. WHERE ' . $db->sql_in_set('post_msg_id', $post_ids) . '
  777. AND in_message = 1';
  778. $result = $db->sql_query($sql);
  779. while ($row = $db->sql_fetchrow($result))
  780. {
  781. $remaining[] = $row['post_msg_id'];
  782. }
  783. $db->sql_freeresult($result);
  784. $unset_ids = array_diff($post_ids, $remaining);
  785. if (sizeof($unset_ids))
  786. {
  787. $sql = 'UPDATE ' . PRIVMSGS_TABLE . '
  788. SET message_attachment = 0
  789. WHERE ' . $db->sql_in_set('msg_id', $unset_ids);
  790. $db->sql_query($sql);
  791. }
  792. }
  793. }
  794. if (sizeof($topic_ids))
  795. {
  796. // Update topic indicator
  797. if ($mode == 'topic')
  798. {
  799. $sql = 'UPDATE ' . TOPICS_TABLE . '
  800. SET topic_attachment = 0
  801. WHERE ' . $db->sql_in_set('topic_id', $topic_ids);
  802. $db->sql_query($sql);
  803. }
  804. if ($mode == 'post' || $mode == 'user' || $mode == 'attach')
  805. {
  806. $remaining = array();
  807. $sql = 'SELECT topic_id
  808. FROM ' . ATTACHMENTS_TABLE . '
  809. WHERE ' . $db->sql_in_set('topic_id', $topic_ids);
  810. $result = $db->sql_query($sql);
  811. while ($row = $db->sql_fetchrow($result))
  812. {
  813. $remaining[] = $row['topic_id'];
  814. }
  815. $db->sql_freeresult($result);
  816. $unset_ids = array_diff($topic_ids, $remaining);
  817. if (sizeof($unset_ids))
  818. {
  819. $sql = 'UPDATE ' . TOPICS_TABLE . '
  820. SET topic_attachment = 0
  821. WHERE ' . $db->sql_in_set('topic_id', $unset_ids);
  822. $db->sql_query($sql);
  823. }
  824. }
  825. }
  826. return $num_deleted;
  827. }
  828. /**
  829. * Remove topic shadows
  830. */
  831. function delete_topic_shadows($max_age, $forum_id = '', $auto_sync = true)
  832. {
  833. $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 : '');
  834. switch ($db->sql_layer)
  835. {
  836. case 'mysql4':
  837. case 'mysqli':
  838. $sql = 'DELETE t.*
  839. FROM ' . TOPICS_TABLE . ' t, ' . TOPICS_TABLE . ' t2
  840. WHERE t.topic_moved_id = t2.topic_id
  841. AND t.topic_time < ' . (time() - $max_age)
  842. . $where;
  843. $db->sql_query($sql);
  844. break;
  845. default:
  846. $sql = 'SELECT t.topic_id
  847. FROM ' . TOPICS_TABLE . ' t, ' . TOPICS_TABLE . ' t2
  848. WHERE t.topic_moved_id = t2.topic_id
  849. AND t.topic_time < ' . (time() - $max_age)
  850. . $where;
  851. $result = $db->sql_query($sql);
  852. $topic_ids = array();
  853. while ($row = $db->sql_fetchrow($result))
  854. {
  855. $topic_ids[] = $row['topic_id'];
  856. }
  857. $db->sql_freeresult($result);
  858. if (sizeof($topic_ids))
  859. {
  860. $sql = 'DELETE FROM ' . TOPICS_TABLE . '
  861. WHERE ' . $db->sql_in_set('topic_id', $topic_ids);
  862. $db->sql_query($sql);
  863. }
  864. break;
  865. }
  866. if ($auto_sync)
  867. {
  868. $where_type = ($forum_id) ? 'forum_id' : '';
  869. sync('forum', $where_type, $forum_id, true, true);
  870. }
  871. }
  872. /**
  873. * Update/Sync posted information for topics
  874. */
  875. function update_posted_info(&$topic_ids)
  876. {
  877. global $db, $config;
  878. if (empty($topic_ids) || !$config['load_db_track'])
  879. {
  880. return;
  881. }
  882. // First of all, let us remove any posted information for these topics
  883. $sql = 'DELETE FROM ' . TOPICS_POSTED_TABLE . '
  884. WHERE ' . $db->sql_in_set('topic_id', $topic_ids);
  885. $db->sql_query($sql);
  886. // Now, let us collect the user/topic combos for rebuilding the information
  887. $sql = 'SELECT poster_id, topic_id
  888. FROM ' . POSTS_TABLE . '
  889. WHERE ' . $db->sql_in_set('topic_id', $topic_ids) . '
  890. AND poster_id <> ' . ANONYMOUS . '
  891. GROUP BY poster_id, topic_id';
  892. $result = $db->sql_query($sql);
  893. $posted = array();
  894. while ($row = $db->sql_fetchrow($result))
  895. {
  896. // Add as key to make them unique (grouping by) and circumvent empty keys on array_unique
  897. $posted[$row['poster_id']][] = $row['topic_id'];
  898. }
  899. $db->sql_freeresult($result);
  900. // Now add the information...
  901. $sql_ary = array();
  902. foreach ($posted as $user_id => $topic_row)
  903. {
  904. foreach ($topic_row as $topic_id)
  905. {
  906. $sql_ary[] = array(
  907. 'user_id' => (int) $user_id,
  908. 'topic_id' => (int) $topic_id,
  909. 'topic_posted' => 1,
  910. );
  911. }
  912. }
  913. unset($posted);
  914. $db->sql_multi_insert(TOPICS_POSTED_TABLE, $sql_ary);
  915. }
  916. /**
  917. * Delete attached file
  918. */
  919. function phpbb_unlink($filename, $mode = 'file', $entry_removed = false)
  920. {
  921. global $db, $phpbb_root_path, $config;
  922. // Because of copying topics or modifications a physical filename could be assigned more than once. If so, do not remove the file itself.
  923. $sql = 'SELECT COUNT(attach_id) AS num_entries
  924. FROM ' . ATTACHMENTS_TABLE . "
  925. WHERE physical_filename = '" . $db->sql_escape(basename($filename)) . "'";
  926. $result = $db->sql_query($sql);
  927. $num_entries = (int) $db->sql_fetchfield('num_entries');
  928. $db->sql_freeresult($result);
  929. // Do not remove file if at least one additional entry with the same name exist.
  930. if (($entry_removed && $num_entries > 0) || (!$entry_removed && $num_entries > 1))
  931. {
  932. return false;
  933. }
  934. $filename = ($mode == 'thumbnail') ? 'thumb_' . basename($filename) : basename($filename);
  935. return @unlink($phpbb_root_path . $config['upload_path'] . '/' . $filename);
  936. }
  937. /**
  938. * All-encompasing sync function
  939. *
  940. * Exaples:
  941. * <code>
  942. * sync('topic', 'topic_id', 123); // resync topic #123
  943. * sync('topic', 'forum_id', array(2, 3)); // resync topics from forum #2 and #3
  944. * sync('topic'); // resync all topics
  945. * sync('topic', 'range', 'topic_id BETWEEN 1 AND 60'); // resync a range of topics/forums (only available for 'topic' and 'forum' modes)
  946. * </code>
  947. *
  948. * Modes:
  949. * - forum Resync complete forum
  950. * - topic Resync topics
  951. * - topic_moved Removes topic shadows that would be in the same forum as the topic they link to
  952. * - topic_approved Resyncs the topic_approved flag according to the status of the first post
  953. * - post_reported Resyncs the post_reported flag, relying on actual reports
  954. * - topic_reported Resyncs the topic_reported flag, relying on post_reported flags
  955. * - post_attachement Same as post_reported, but with attachment flags
  956. * - topic_attachement Same as topic_reported, but with attachment flags
  957. */
  958. function sync($mode, $where_type = '', $where_ids = '', $resync_parents = false, $sync_extra = false)
  959. {
  960. global $db;
  961. if (is_array($where_ids))
  962. {
  963. $where_ids = array_unique($where_ids);
  964. $where_ids = array_map('intval', $where_ids);
  965. }
  966. else if ($where_type != 'range')
  967. {
  968. $where_ids = ($where_ids) ? array((int) $where_ids) : array();
  969. }
  970. if ($mode == 'forum' || $mode == 'topic' || $mode == 'topic_approved' || $mode == 'topic_reported' || $mode == 'post_reported')
  971. {
  972. if (!$where_type)
  973. {
  974. $where_sql = '';
  975. $where_sql_and = 'WHERE';
  976. }
  977. else if ($where_type == 'range')
  978. {
  979. // Only check a range of topics/forums. For instance: 'topic_id BETWEEN 1 AND 60'
  980. $where_sql = 'WHERE (' . $mode[0] . ".$where_ids)";
  981. $where_sql_and = $where_sql . "\n\tAND";
  982. }
  983. else
  984. {
  985. // Do not sync the "global forum"
  986. $where_ids = array_diff($where_ids, array(0));
  987. if (!sizeof($where_ids))
  988. {
  989. // Empty array with IDs. This means that we don't have any work to do. Just return.
  990. return;
  991. }
  992. // Limit the topics/forums we are syncing, use specific topic/forum IDs.
  993. // $where_type contains the field for the where clause (forum_id, topic_id)
  994. $where_sql = 'WHERE ' . $db->sql_in_set($mode[0] . '.' . $where_type, $where_ids);
  995. $where_sql_and = $where_sql . "\n\tAND";
  996. }
  997. }
  998. else
  999. {
  1000. if (!sizeof($where_ids))
  1001. {
  1002. return;
  1003. }
  1004. // $where_type contains the field for the where clause (forum_id, topic_id)
  1005. $where_sql = 'WHERE ' . $db->sql_in_set($mode[0] . '.' . $where_type, $where_ids);
  1006. $where_sql_and = $where_sql . "\n\tAND";
  1007. }
  1008. switch ($mode)
  1009. {
  1010. case 'topic_moved':
  1011. switch ($db->sql_layer)
  1012. {
  1013. case 'mysql4':
  1014. case 'mysqli':
  1015. $sql = 'DELETE FROM ' . TOPICS_TABLE . '
  1016. USING ' . TOPICS_TABLE . ' t1, ' . TOPICS_TABLE . " t2
  1017. WHERE t1.topic_moved_id = t2.topic_id
  1018. AND t1.forum_id = t2.forum_id";
  1019. $db->sql_query($sql);
  1020. break;
  1021. default:
  1022. $sql = 'SELECT t1.topic_id
  1023. FROM ' .TOPICS_TABLE . ' t1, ' . TOPICS_TABLE . " t2
  1024. WHERE t1.topic_moved_id = t2.topic_id
  1025. AND t1.forum_id = t2.forum_id";
  1026. $result = $db->sql_query($sql);
  1027. $topic_id_ary = array();
  1028. while ($row = $db->sql_fetchrow($result))
  1029. {
  1030. $topic_id_ary[] = $row['topic_id'];
  1031. }
  1032. $db->sql_freeresult($result);
  1033. if (!sizeof($topic_id_ary))
  1034. {
  1035. return;
  1036. }
  1037. $sql = 'DELETE FROM ' . TOPICS_TABLE . '
  1038. WHERE ' . $db->sql_in_set('topic_id', $topic_id_ary);
  1039. $db->sql_query($sql);
  1040. break;
  1041. }
  1042. break;
  1043. case 'topic_approved':
  1044. switch ($db->sql_layer)
  1045. {
  1046. case 'mysql4':
  1047. case 'mysqli':
  1048. $sql = 'UPDATE ' . TOPICS_TABLE . ' t, ' . POSTS_TABLE . " p
  1049. SET t.topic_approved = p.post_approved
  1050. $where_sql_and t.topic_first_post_id = p.post_id";
  1051. $db->sql_query($sql);
  1052. break;
  1053. default:
  1054. $sql = 'SELECT t.topic_id, p.post_approved
  1055. FROM ' . TOPICS_TABLE . ' t, ' . POSTS_TABLE . " p
  1056. $where_sql_and p.post_id = t.topic_first_post_id
  1057. AND p.post_approved <> t.topic_approved";
  1058. $result = $db->sql_query($sql);
  1059. $topic_ids = array();
  1060. while ($row = $db->sql_fetchrow($result))
  1061. {
  1062. $topic_ids[] = $row['topic_id'];
  1063. }
  1064. $db->sql_freeresult($result);
  1065. if (!sizeof($topic_ids))
  1066. {
  1067. return;
  1068. }
  1069. $sql = 'UPDATE ' . TOPICS_TABLE . '
  1070. SET topic_approved = 1 - topic_approved
  1071. WHERE ' . $db->sql_in_set('topic_id', $topic_ids);
  1072. $db->sql_query($sql);
  1073. break;
  1074. }
  1075. break;
  1076. case 'post_reported':
  1077. $post_ids = $post_reported = array();
  1078. $sql = 'SELECT p.post_id, p.post_reported
  1079. FROM ' . POSTS_TABLE . " p
  1080. $where_sql
  1081. GROUP BY p.post_id, p.post_reported";
  1082. $result = $db->sql_query($sql);
  1083. while ($row = $db->sql_fetchrow($result))
  1084. {
  1085. $post_ids[$row['post_id']] = $row['post_id'];
  1086. if ($row['post_reported'])
  1087. {
  1088. $post_reported[$row['post_id']] = 1;
  1089. }
  1090. }
  1091. $db->sql_freeresult($result);
  1092. $sql = 'SELECT DISTINCT(post_id)
  1093. FROM ' . REPORTS_TABLE . '
  1094. WHERE ' . $db->sql_in_set('post_id', $post_ids) . '
  1095. AND report_closed = 0';
  1096. $result = $db->sql_query($sql);
  1097. $post_ids = array();
  1098. while ($row = $db->sql_fetchrow($result))
  1099. {
  1100. if (!isset($post_reported[$row['post_id']]))
  1101. {
  1102. $post_ids[] = $row['post_id'];
  1103. }
  1104. else
  1105. {
  1106. unset($post_reported[$row['post_id']]);
  1107. }
  1108. }
  1109. $db->sql_freeresult($result);
  1110. // $post_reported should be empty by now, if it's not it contains
  1111. // posts that are falsely flagged as reported
  1112. foreach ($post_reported as $post_id => $void)
  1113. {
  1114. $post_ids[] = $post_id;
  1115. }
  1116. if (sizeof($post_ids))
  1117. {
  1118. $sql = 'UPDATE ' . POSTS_TABLE . '
  1119. SET post_reported = 1 - post_reported
  1120. WHERE ' . $db->sql_in_set('post_id', $post_ids);
  1121. $db->sql_query($sql);
  1122. }
  1123. break;
  1124. case 'topic_reported':
  1125. if ($sync_extra)
  1126. {
  1127. sync('post_reported', $where_type, $where_ids);
  1128. }
  1129. $topic_ids = $topic_reported = array();
  1130. $sql = 'SELECT DISTINCT(t.topic_id)
  1131. FROM ' . POSTS_TABLE . " t
  1132. $where_sql_and t.post_reported = 1";
  1133. $result = $db->sql_query($sql);
  1134. while ($row = $db->sql_fetchrow($result))
  1135. {
  1136. $topic_reported[$row['topic_id']] = 1;
  1137. }
  1138. $db->sql_freeresult($result);
  1139. $sql = 'SELECT t.topic_id, t.topic_reported
  1140. FROM ' . TOPICS_TABLE . " t
  1141. $where_sql";
  1142. $result = $db->sql_query($sql);
  1143. while ($row = $db->sql_fetchrow($result))
  1144. {
  1145. if ($row['topic_reported'] ^ isset($topic_reported[$row['topic_id']]))
  1146. {
  1147. $topic_ids[] = $row['topic_id'];
  1148. }
  1149. }
  1150. $db->sql_freeresult($result);
  1151. if (sizeof($topic_ids))
  1152. {
  1153. $sql = 'UPDATE ' . TOPICS_TABLE . '
  1154. SET topic_reported = 1 - topic_reported
  1155. WHERE ' . $db->sql_in_set('topic_id', $topic_ids);
  1156. $db->sql_query($sql);
  1157. }
  1158. break;
  1159. case 'post_attachment':
  1160. $post_ids = $post_attachment = array();
  1161. $sql = 'SELECT p.post_id, p.post_attachment
  1162. FROM ' . POSTS_TABLE . " p
  1163. $where_sql
  1164. GROUP BY p.post_id, p.post_attachment";
  1165. $result = $db->sql_query($sql);
  1166. while ($row = $db->sql_fetchrow($result))
  1167. {
  1168. $post_ids[$row['post_id']] = $row['post_id'];
  1169. if ($row['post_attachment'])
  1170. {
  1171. $post_attachment[$row['post_id']] = 1;
  1172. }
  1173. }
  1174. $db->sql_freeresult($result);
  1175. $sql = 'SELECT DISTINCT(post_msg_id)
  1176. FROM ' . ATTACHMENTS_TABLE . '
  1177. WHERE ' . $db->sql_in_set('post_msg_id', $post_ids) . '
  1178. AND in_message = 0';
  1179. $result = $db->sql_query($sql);
  1180. $post_ids = array();
  1181. while ($row = $db->sql_fetchrow($result))
  1182. {
  1183. if (!isset($post_attachment[$row['post_msg_id']]))
  1184. {
  1185. $post_ids[] = $row['post_msg_id'];
  1186. }
  1187. else
  1188. {
  1189. unset($post_attachment[$row['post_msg_id']]);
  1190. }
  1191. }
  1192. $db->sql_freeresult($result);
  1193. // $post_attachment should be empty by now, if it's not it contains
  1194. // posts that are falsely flagged as having attachments
  1195. foreach ($post_attachment as $post_id => $void)
  1196. {
  1197. $post_ids[] = $post_id;
  1198. }
  1199. if (sizeof($post_ids))
  1200. {
  1201. $sql = 'UPDATE ' . POSTS_TABLE . '
  1202. SET post_attachment = 1 - post_attachment
  1203. WHERE ' . $db->sql_in_set('post_id', $post_ids);
  1204. $db->sql_query($sql);
  1205. }
  1206. break;
  1207. case 'topic_attachment':
  1208. if ($sync_extra)
  1209. {
  1210. sync('post_attachment', $where_type, $where_ids);
  1211. }
  1212. $topic_ids = $topic_attachment = array();
  1213. $sql = 'SELECT DISTINCT(t.topic_id)
  1214. FROM ' . POSTS_TABLE . " t
  1215. $where_sql_and t.post_attachment = 1";
  1216. $result = $db->sql_query($sql);
  1217. while ($row = $db->sql_fetchrow($result))
  1218. {
  1219. $topic_attachment[$row['topic_id']] = 1;
  1220. }
  1221. $db->sql_freeresult($result);
  1222. $sql = 'SELECT t.topic_id, t.topic_attachment
  1223. FROM ' . TOPICS_TABLE . " t
  1224. $where_sql";
  1225. $result = $db->sql_query($sql);
  1226. while ($row = $db->sql_fetchrow($result))
  1227. {
  1228. if ($row['topic_attachment'] ^ isset($topic_attachment[$row['topic_id']]))
  1229. {
  1230. $topic_ids[] = $row['topic_id'];
  1231. }
  1232. }
  1233. $db->sql_freeresult($result);
  1234. if (sizeof($topic_ids))
  1235. {
  1236. $sql = 'UPDATE ' . TOPICS_TABLE . '
  1237. SET topic_attachment = 1 - topic_attachment
  1238. WHERE ' . $db->sql_in_set('topic_id', $topic_ids);
  1239. $db->sql_query($sql);
  1240. }
  1241. break;
  1242. case 'forum':
  1243. // 1: Get the list of all forums
  1244. $sql = 'SELECT f.*
  1245. FROM ' . FORUMS_TABLE . " f
  1246. $where_sql";
  1247. $result = $db->sql_query($sql);
  1248. $forum_data = $forum_ids = $post_ids = $last_post_id = $post_info = array();
  1249. while ($row = $db->sql_fetchrow($result))
  1250. {
  1251. if ($row['forum_type'] == FORUM_LINK)
  1252. {
  1253. continue;
  1254. }
  1255. $forum_id = (int) $row['forum_id'];
  1256. $forum_ids[$forum_id] = $forum_id;
  1257. $forum_data[$forum_id] = $row;
  1258. if ($sync_extra)
  1259. {
  1260. $forum_data[$forum_id]['posts'] = 0;
  1261. $forum_data[$forum_id]['topics'] = 0;
  1262. $forum_data[$forum_id]['topics_real'] = 0;
  1263. }
  1264. $forum_data[$forum_id]['last_post_id'] = 0;
  1265. $forum_data[$forum_id]['last_post_subject'] = '';
  1266. $forum_data[$forum_id]['last_post_time'] = 0;
  1267. $forum_data[$forum_id]['last_poster_id'] = 0;
  1268. $forum_data[$forum_id]['last_poster_name'] = '';
  1269. $forum_data[$forum_id]['last_poster_colour'] = '';
  1270. }
  1271. $db->sql_freeresult($result);
  1272. if (!sizeof($forum_ids))
  1273. {
  1274. break;
  1275. }
  1276. $forum_ids = array_values($forum_ids);
  1277. // 2: Get topic counts for each forum (optional)
  1278. if ($sync_extra)
  1279. {
  1280. $sql = 'SELECT forum_id, topic_approved, COUNT(topic_id) AS forum_topics
  1281. FROM ' . TOPICS_TABLE . '
  1282. WHERE ' . $db->sql_in_set('forum_id', $forum_ids) . '
  1283. GROUP BY forum_id, topic_approved';
  1284. $result = $db->sql_query($sql);
  1285. while ($row = $db->sql_fetchrow($result))
  1286. {
  1287. $forum_id = (int) $row['forum_id'];
  1288. $forum_data[$forum_id]['topics_real'] += $row['forum_topics'];
  1289. if ($row['topic_approved'])
  1290. {
  1291. $forum_data[$forum_id]['topics'] = $row['forum_topics'];
  1292. }
  1293. }
  1294. $db->sql_freeresult($result);
  1295. }
  1296. // 3: Get post count for each forum (optional)
  1297. if ($sync_extra)
  1298. {
  1299. if (sizeof($forum_ids) == 1)
  1300. {
  1301. $sql = 'SELECT SUM(t.topic_replies + 1) AS forum_posts
  1302. FROM ' . TOPICS_TABLE . ' t
  1303. WHERE ' . $db->sql_in_set('t.forum_id', $forum_ids) . '
  1304. AND t.topic_approved = 1
  1305. AND t.topic_status <> ' . ITEM_MOVED;
  1306. }
  1307. else
  1308. {
  1309. $sql = 'SELECT t.forum_id, SUM(t.topic_replies + 1) AS forum_posts
  1310. FROM ' . TOPICS_TABLE . ' t
  1311. WHERE ' . $db->sql_in_set('t.forum_id', $forum_ids) . '
  1312. AND t.topic_approved = 1
  1313. AND t.topic_status <> ' . ITEM_MOVED . '
  1314. GROUP BY t.forum_id';
  1315. }
  1316. $result = $db->sql_query($sql);
  1317. while ($row = $db->sql_fetchrow($result))
  1318. {
  1319. $forum_id = (sizeof($forum_ids) == 1) ? (int) $forum_ids[0] : (int) $row['forum_id'];
  1320. $forum_data[$forum_id]['posts'] = (int) $row['forum_posts'];
  1321. }
  1322. $db->sql_freeresult($result);
  1323. }
  1324. // 4: Get last_post_id for each forum
  1325. if (sizeof($forum_ids) == 1)
  1326. {
  1327. $sql = 'SELECT MAX(t.topic_last_post_id) as last_post_id
  1328. FROM ' . TOPICS_TABLE . ' t
  1329. WHERE ' . $db->sql_in_set('t.forum_id', $forum_ids) . '
  1330. AND t.topic_approved = 1';
  1331. }
  1332. else
  1333. {
  1334. $sql = 'SELECT t.forum_id, MAX(t.topic_last_post_id) as last_post_id
  1335. FROM ' . TOPICS_TABLE . ' t
  1336. WHERE ' . $db->sql_in_set('t.forum_id', $forum_ids) . '
  1337. AND t.topic_approved = 1
  1338. GROUP BY t.forum_id';
  1339. }
  1340. $result = $db->sql_query($sql);
  1341. while ($row = $db->sql_fetchrow($result))
  1342. {
  1343. $forum_id = (sizeof($forum_ids) == 1) ? (int) $forum_ids[0] : (int) $row['forum_id'];
  1344. $forum_data[$forum_id]['last_post_id'] = (int) $row['last_post_id'];
  1345. $post_ids[] = $row['last_post_id'];
  1346. }
  1347. $db->sql_freeresult($result);
  1348. // 5: Retrieve last_post infos
  1349. if (sizeof($post_ids))
  1350. {
  1351. $sql = 'SELECT p.post_id, p.poster_id, p.post_subject, p.post_time, p.post_username, u.username, u.user_colour
  1352. FROM ' . POSTS_TABLE . ' p, ' . USERS_TABLE . ' u
  1353. WHERE ' . $db->sql_in_set('p.post_id', $post_ids) . '
  1354. AND p.poster_id = u.user_id';
  1355. $result = $db->sql_query($sql);
  1356. while ($row = $db->sql_fetchrow($result))
  1357. {
  1358. $post_info[$row['post_id']] = $row;
  1359. }
  1360. $db->sql_freeresult($result);
  1361. foreach ($forum_data as $forum_id => $data)
  1362. {
  1363. if ($data['last_post_id'])
  1364. {
  1365. if (isset($post_info[$data['last_post_id']]))
  1366. {
  1367. $forum_data[$forum_id]['last_post_subject'] = $post_info[$data['last_post_id']]['post_subject'];
  1368. $forum_data[$forum_id]['last_post_time'] = $post_info[$data['last_post_id']]['post_time'];
  1369. $forum_data[$forum_id]['last_poster_id'] = $post_info[$data['last_post_id']]['poster_id'];
  1370. $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'];
  1371. $forum_data[$forum_id]['last_poster_colour'] = $post_info[$data['last_post_id']]['user_colour'];
  1372. }
  1373. else
  1374. {
  1375. // For some reason we did not find the post in the db
  1376. $forum_data[$forum_id]['last_post_id'] = 0;
  1377. $forum_data[$forum_id]['last_post_subject'] = '';
  1378. $forum_data[$forum_id]['last_post_time'] = 0;
  1379. $forum_data[$forum_id]['last_poster_id'] = 0;
  1380. $forum_data[$forum_id]['last_poster_name'] = '';
  1381. $forum_data[$forum_id]['last_poster_colour'] = '';
  1382. }
  1383. }
  1384. }
  1385. unset($post_info);
  1386. }
  1387. // 6: Now do that thing
  1388. $fieldnames = array('last_post_id', 'last_post_subject', 'last_post_time', 'last_poster_id', 'last_poster_name', 'last_poster_colour');
  1389. if ($sync_extra)
  1390. {
  1391. array_push($fieldnames, 'posts', 'topics', 'topics_real');
  1392. }
  1393. foreach ($forum_data as $forum_id => $row)
  1394. {
  1395. $sql_ary = array();
  1396. foreach ($fieldnames as $fieldname)
  1397. {
  1398. if ($row['forum_' . $fieldname] != $row[$fieldname])
  1399. {
  1400. if (preg_match('#(name|colour|subject)$#', $fieldname))
  1401. {
  1402. $sql_ary['forum_' . $fieldname] = (string) $row[$fieldname];
  1403. }
  1404. else
  1405. {
  1406. $sql_ary['forum_' . $fieldname] = (int) $row[$fieldname];
  1407. }
  1408. }
  1409. }
  1410. if (sizeof($sql_ary))
  1411. {
  1412. $sql = 'UPDATE ' . FORUMS_TABLE . '
  1413. SET ' . $db->sql_build_array('UPDATE', $sql_ary) . '
  1414. WHERE forum_id = ' . $forum_id;
  1415. $db->sql_query($sql);
  1416. }
  1417. }
  1418. break;
  1419. case 'topic':
  1420. $topic_data = $post_ids = $approved_unapproved_ids = $resync_forums = $delete_topics = $delete_posts = $moved_topics = array();
  1421. $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
  1422. FROM ' . TOPICS_TABLE . " t
  1423. $where_sql";
  1424. $result = $db->sql_query($sql);
  1425. while ($row = $db->sql_fetchrow($result))
  1426. {
  1427. if ($row['topic_moved_id'])
  1428. {
  1429. $moved_topics[] = $row['topic_id'];
  1430. continue;
  1431. }
  1432. $topic_id = (int) $row['topic_id'];
  1433. $topic_data[$topic_id] = $row;
  1434. $topic_data[$topic_id]['replies_real'] = -1;
  1435. $topic_data[$topic_id]['replies'] = 0;
  1436. $topic_data[$topic_id]['first_post_id'] = 0;
  1437. $topic_data[$topic_id]['last_post_id'] = 0;
  1438. unset($topic_data[$topic_id]['topic_id']);
  1439. // This array holds all topic_ids
  1440. $delete_topics[$topic_id] = '';
  1441. if ($sync_extra)
  1442. {
  1443. $topic_data[$topic_id]['reported'] = 0;
  1444. $topic_data[$topic_id]['attachment'] = 0;
  1445. }
  1446. }
  1447. $db->sql_freeresult($result);
  1448. // Use "t" as table alias because of the $where_sql clause
  1449. // NOTE: 't.post_approved' in the GROUP BY is causing a major slowdown.
  1450. $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
  1451. FROM ' . POSTS_TABLE . " t
  1452. $where_sql
  1453. GROUP BY t.topic_id, t.post_approved";
  1454. $result = $db->sql_query($sql);
  1455. while ($row = $db->sql_fetchrow($result))
  1456. {
  1457. $topic_id = (int) $row['topic_id'];
  1458. $row['first_post_id'] = (int) $row['first_post_id'];
  1459. $row['last_post_id'] = (int) $row['last_post_id'];
  1460. if (!isset($topic_data[$topic_id]))
  1461. {
  1462. // Hey, these posts come from a topic that does not exist
  1463. $delete_posts[$topic_id] = '';
  1464. }
  1465. else
  1466. {
  1467. // Unset the corresponding entry in $delete_topics
  1468. // When we'll be done, only topics with no posts will remain
  1469. unset($delete_topics[$topic_id]);
  1470. $topic_data[$topic_id]['replies_real'] += $row['total_posts'];
  1471. $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']);
  1472. if ($row['post_approved'] || !$topic_data[$topic_id]['last_post_id'])
  1473. {
  1474. $topic_data[$topic_id]['replies'] = $row['total_posts'] - 1;
  1475. $topic_data[$topic_id]['last_post_id'] = $row['last_post_id'];
  1476. }
  1477. }
  1478. }
  1479. $db->sql_freeresult($result);
  1480. foreach ($topic_data as $topic_id => $row)
  1481. {
  1482. $post_ids[] = $row['first_post_id'];
  1483. if ($row['first_post_id'] != $row['last_post_id'])
  1484. {
  1485. $post_ids[] = $row['last_post_id'];
  1486. }
  1487. }
  1488. // Now we delete empty topics and orphan posts
  1489. if (sizeof($delete_posts))
  1490. {
  1491. delete_posts('topic_id', array_keys($delete_posts), false);
  1492. unset($delete_posts);
  1493. }
  1494. if (!sizeof($topic_data))
  1495. {
  1496. // If we get there, topic ids were invalid or topics did not contain any posts
  1497. delete_topics($where_type, $where_ids, true);
  1498. return;
  1499. }
  1500. if (sizeof($delete_topics))
  1501. {
  1502. $delete_topic_ids = array();
  1503. foreach ($delete_topics as $topic_id => $void)
  1504. {
  1505. unset($topic_data[$topic_id]);
  1506. $delete_topic_ids[] = $topic_id;
  1507. }
  1508. delete_topics('topic_id', $delete_topic_ids, false);
  1509. unset($delete_topics, $delete_topic_ids);
  1510. }
  1511. $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
  1512. FROM ' . POSTS_TABLE . ' p, ' . USERS_TABLE . ' u
  1513. WHERE ' . $db->sql_in_set('p.post_id', $post_ids) . '
  1514. AND u.user_id = p.poster_id';
  1515. $result = $db->sql_query($sql);
  1516. $post_ids = array();
  1517. while ($row = $db->sql_fetchrow($result))
  1518. {
  1519. $topic_id = intval($row['topic_id']);
  1520. if ($row['post_id'] == $topic_data[$topic_id]['first_post_id'])
  1521. {
  1522. if ($topic_data[$topic_id]['topic_approved'] != $row['post_approved'])
  1523. {
  1524. $approved_unapproved_ids[] = $topic_id;
  1525. }
  1526. $topic_data[$topic_id]['time'] = $row['post_time'];
  1527. $topic_data[$topic_id]['poster'] = $row['poster_id'];
  1528. $topic_data[$topic_id]['first_poster_name'] = ($row['poster_id'] == ANONYMOUS) ? $row['post_username'] : $row['username'];
  1529. $topic_data[$topic_id]['first_poster_colour'] = $row['user_colour'];
  1530. }
  1531. if ($row['post_id'] == $topic_data[$topic_id]['last_post_id'])
  1532. {
  1533. $topic_data[$topic_id]['last_poster_id'] = $row['poster_id'];
  1534. $topic_data[$topic_id]['last_post_subject'] = $row['post_subject'];
  1535. $topic_data[$topic_id]['last_post_time'] = $row['post_time'];
  1536. $topic_data[$topic_id]['last_poster_name'] = ($row['poster_id'] == ANONYMOUS) ? $row['post_username'] : $row['username'];
  1537. $topic_data[$topic_id]['last_poster_colour'] = $row['user_colour'];
  1538. }
  1539. }
  1540. $db->sql_freeresult($result);
  1541. // Make sure shadow topics do link to existing topics
  1542. if (sizeof($moved_topics))
  1543. {
  1544. $delete_topics = array();
  1545. $sql = 'SELECT t1.topic_id, t1.topic_moved_id
  1546. FROM ' . TOPICS_TABLE . ' t1
  1547. LEFT JOIN ' . TOPICS_TABLE . ' t2 ON (t2.topic_id = t1.topic_moved_id)
  1548. WHERE ' . $db->sql_in_set('t1.topic_id', $moved_topics) . '
  1549. AND t2.topic_id IS NULL';
  1550. $result = $db->sql_query($sql);
  1551. while ($row = $db->sql_fetchrow($result))
  1552. {
  1553. $delete_topics[] = $row['topic_id'];
  1554. }
  1555. $db->sql_freeresult($result);
  1556. if (sizeof($delete_topics))
  1557. {
  1558. delete_topics('topic_id', $delete_topics, false);
  1559. }
  1560. unset($delete_topics);
  1561. // Make sure shadow topics having no last post data being updated (this only rarely happens...)
  1562. $sql = 'SELECT topic_id, topic_moved_id, topic_last_post_id, topic_first_post_id
  1563. FROM ' . TOPICS_TABLE . '
  1564. WHERE ' . $db->sql_in_set('topic_id', $moved_topics) . '
  1565. AND topic_last_post_time = 0';
  1566. $result = $db->sql_query($sql);
  1567. $shadow_topic_data = $post_ids = array();
  1568. while ($row = $db->sql_fetchrow($result))
  1569. {
  1570. $shadow_topic_data[$row['topic_moved_id']] = $row;
  1571. $post_ids[] = $row['topic_last_post_id'];
  1572. $post_ids[] = $row['topic_first_post_id'];
  1573. }
  1574. $db->sql_freeresult($result);
  1575. $sync_shadow_topics = array();
  1576. if (sizeof($post_ids))
  1577. {
  1578. $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
  1579. FROM ' . POSTS_TABLE . ' p, ' . USERS_TABLE . ' u
  1580. WHERE ' . $db->sql_in_set('p.post_id', $post_ids) . '
  1581. AND u.user_id = p.poster_id';
  1582. $result = $db->sql_query($sql);
  1583. $post_ids = array();
  1584. while ($row = $db->sql_fetchrow($result))
  1585. {
  1586. $topic_id = (int) $row['topic_id'];
  1587. // Ok, there should be a shadow topic. If there isn't, then there's something wrong with the db.
  1588. // However, there's not much we can do about it.
  1589. if (!empty($shadow_topic_data[$topic_id]))
  1590. {
  1591. if ($row['post_id'] == $shadow_topic_data[$topic_id]['topic_first_post_id'])
  1592. {
  1593. $orig_topic_id = $shadow_topic_data[$topic_id]['topic_id'];
  1594. if (!isset($sync_shadow_topics[$orig_topic_id]))
  1595. {
  1596. $sync_shadow_topics[$orig_topic_id] = array();
  1597. }
  1598. $sync_shadow_topics[$orig_topic_id]['topic_time'] = $row['post_time'];
  1599. $sync_shadow_topics[$orig_topic_id]['topic_poster'] = $row['poster_id'];
  1600. $sync_shadow_topics[$orig_topic_id]['topic_first_poster_name'] = ($row['poster_id'] == ANONYMOUS) ? $row['post_username'] : $row['username'];
  1601. $sync_shadow_topics[$orig_topic_id]['topic_first_poster_colour'] = $row['user_colour'];
  1602. }
  1603. if ($row['post_id'] == $shadow_topic_data[$topic_id]['topic_last_post_id'])
  1604. {
  1605. $orig_topic_id = $shadow_topic_data[$topic_id]['topic_id'];
  1606. if (!isset($sync_shadow_topics[$orig_topic_id]))
  1607. {
  1608. $sync_shadow_topics[$orig_topic_id] = array();
  1609. }
  1610. $sync_shadow_topics[$orig_topic_id]['topic_last_poster_id'] = $row['poster_id'];
  1611. $sync_shadow_topics[$orig_topic_id]['topic_last_post_subject'] = $row['post_subject'];
  1612. $sync_shadow_topics[$orig_topic_id]['topic_last_post_time'] = $row['post_time'];
  1613. $sync_shadow_topics[$orig_topic_id]['topic_last_poster_name'] = ($row['poster_id'] == ANONYMOUS) ? $row['post_username'] : $row['username'];
  1614. $sync_shadow_topics[$orig_topic_id]['topic_last_poster_colour'] = $row['user_colour'];
  1615. }
  1616. }
  1617. }
  1618. $db->sql_freeresult($result);
  1619. $shadow_topic_data = array();
  1620. // Update the information we collected
  1621. if (sizeof($sync_shadow_topics))
  1622. {
  1623. foreach ($sync_shadow_topics as $sync_topic_id => $sql_ary)
  1624. {
  1625. $sql = 'UPDATE ' . TOPICS_TABLE . '
  1626. SET ' . $db->sql_build_array('UPDATE', $sql_ary) . '
  1627. WHERE topic_id = ' . $sync_topic_id;
  1628. $db->sql_query($sql);
  1629. }
  1630. }
  1631. }
  1632. unset($sync_shadow_topics, $shadow_topic_data);
  1633. }
  1634. // approved becomes unapproved, and vice-versa
  1635. if (sizeof($approved_unapproved_ids))
  1636. {
  1637. $sql = 'UPDATE ' . TOPICS_TABLE . '
  1638. SET topic_approved = 1 - topic_approved
  1639. WHERE ' . $db->sql_in_set('topic_id', $approved_unapproved_ids);
  1640. $db->sql_query($sql);
  1641. }
  1642. unset($approved_unapproved_ids);
  1643. // These are fields that will be synchronised
  1644. $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');
  1645. if ($sync_extra)
  1646. {
  1647. // This routine assumes that post_reported values are correct
  1648. // if they are not, use sync('post_reported') first
  1649. $sql = 'SELECT t.topic_id, p.post_id
  1650. FROM ' . TOPICS_TABLE . ' t, ' . POSTS_TABLE . " p
  1651. $where_sql_and p.topic_id = t.topic_id
  1652. AND p.post_reported = 1
  1653. GROUP BY t.topic_id, p.post_id";
  1654. $result = $db->sql_query($sql);
  1655. $fieldnames[] = 'reported';
  1656. while ($row = $db->sql_fetchrow($result))
  1657. {
  1658. $topic_data[intval($row['topic_id'])]['reported'] = 1;
  1659. }
  1660. $db->sql_freeresult($result);
  1661. // This routine assumes that post_attachment values are correct
  1662. // if they are not, use sync('post_attachment') first
  1663. $sql = 'SELECT t.topic_id, p.post_id
  1664. FROM ' . TOPICS_TABLE . ' t, ' . POSTS_TABLE . " p
  1665. $where_sql_and p.topic_id = t.topic_id
  1666. AND p.post_attachment = 1
  1667. GROUP BY t.topic_id, p.post_id";
  1668. $result = $db->sql_query($sql);
  1669. $fieldnames[] = 'attachment';
  1670. while ($row = $db->sql_fetchrow($result))
  1671. {
  1672. $topic_data[intval($row['topic_id'])]['attachment'] = 1;
  1673. }
  1674. $db->sql_freeresult($result);
  1675. }
  1676. foreach ($topic_data as $topic_id => $row)
  1677. {
  1678. $sql_ary = array();
  1679. foreach ($fieldnames as $fieldname)
  1680. {
  1681. if (isset($row[$fieldname]) && isset($row['topic_' . $fieldname]) && $row['topic_' . $fieldname] != $row[$fieldname])
  1682. {
  1683. $sql_ary['topic_' . $fieldname] = $row[$fieldname];
  1684. }
  1685. }
  1686. if (sizeof($sql_ary))
  1687. {
  1688. $sql = 'UPDATE ' . TOPICS_TABLE . '
  1689. SET ' . $db->sql_build_array('UPDATE', $sql_ary) . '
  1690. WHERE topic_id = ' . $topic_id;
  1691. $db->sql_query($sql);
  1692. $resync_forums[$row['forum_id']] = $row['forum_id'];
  1693. }
  1694. }
  1695. unset($topic_data);
  1696. // if some topics have been resync'ed then resync parent forums
  1697. // except when we're only syncing a range, we don't want to sync forums during
  1698. // batch processing.
  1699. if ($resync_parents && sizeof($resync_forums) && $where_type != 'range')
  1700. {
  1701. sync('forum', 'forum_id', array_values($resync_forums), true, true);
  1702. }
  1703. break;
  1704. }
  1705. return;
  1706. }
  1707. /**
  1708. * Prune function
  1709. */
  1710. function prune($forum_id, $prune_mode, $prune_date, $prune_flags = 0, $auto_sync = true)
  1711. {
  1712. global $db;
  1713. if (!is_array($forum_id))
  1714. {
  1715. $forum_id = array($forum_id);
  1716. }
  1717. if (!sizeof($forum_id))
  1718. {
  1719. return;
  1720. }
  1721. $sql_and = '';
  1722. if (!($prune_flags & FORUM_FLAG_PRUNE_ANNOUNCE))
  1723. {
  1724. $sql_and .= ' AND topic_type <> ' . POST_ANNOUNCE;
  1725. }
  1726. if (!($prune_flags & FORUM_FLAG_PRUNE_STICKY))
  1727. {
  1728. $sql_and .= ' AND topic_type <> ' . POST_STICKY;
  1729. }
  1730. if ($prune_mode == 'posted')
  1731. {
  1732. $sql_and .= " AND topic_last_post_time < $prune_date";
  1733. }
  1734. if ($prune_mode == 'viewed')
  1735. {
  1736. $sql_and .= " AND topic_last_view_time < $prune_date";
  1737. }
  1738. $sql = 'SELECT topic_id
  1739. FROM ' . TOPICS_TABLE . '
  1740. WHERE ' . $db->sql_in_set('forum_id', $forum_id) . "
  1741. AND poll_start = 0
  1742. $sql_and";
  1743. $result = $db->sql_query($sql);
  1744. $topic_list = array();
  1745. while ($row = $db->sql_fetchrow($result))
  1746. {
  1747. $topic_list[] = $row['topic_id'];
  1748. }
  1749. $db->sql_freeresult($result);
  1750. if ($prune_flags & FORUM_FLAG_PRUNE_POLL)
  1751. {
  1752. $sql = 'SELECT topic_id
  1753. FROM ' . TOPICS_TABLE . '
  1754. WHERE ' . $db->sql_in_set('forum_id', $forum_id) . "
  1755. AND poll_start > 0
  1756. AND poll_last_vote < $prune_date
  1757. $sql_and";
  1758. $result = $db->sql_query($sql);
  1759. while ($row = $db->sql_fetchrow($result))
  1760. {
  1761. $topic_list[] = $row['topic_id'];
  1762. }
  1763. $db->sql_freeresult($result);
  1764. $topic_list = array_unique($topic_list);
  1765. }
  1766. return delete_topics('topic_id', $topic_list, $auto_sync, false);
  1767. }
  1768. /**
  1769. * Function auto_prune(), this function now relies on passed vars
  1770. */
  1771. function auto_prune($forum_id, $prune_mode, $prune_flags, $prune_days, $prune_freq)
  1772. {
  1773. global $db;
  1774. $sql = 'SELECT forum_name
  1775. FROM ' . FORUMS_TABLE . "
  1776. WHERE forum_id = $forum_id";
  1777. $result = $db->sql_query($sql, 3600);
  1778. $row = $db->sql_fetchrow($result);
  1779. $db->sql_freeresult($result);
  1780. if ($row)
  1781. {
  1782. $prune_date = time() - ($prune_days * 86400);
  1783. $next_prune = time() + ($prune_freq * 86400);
  1784. prune($forum_id, $prune_mode, $prune_date, $prune_flags, true);
  1785. $sql = 'UPDATE ' . FORUMS_TABLE . "
  1786. SET prune_next = $next_prune
  1787. WHERE forum_id = $forum_id";
  1788. $db->sql_query($sql);
  1789. add_log('admin', 'LOG_AUTO_PRUNE', $row['forum_name']);
  1790. }
  1791. return;
  1792. }
  1793. /**
  1794. * remove_comments will strip the sql comment lines out of an uploaded sql file
  1795. * specifically for mssql and postgres type files in the install....
  1796. */
  1797. function remove_comments(&$output)
  1798. {
  1799. $lines = explode("\n", $output);
  1800. $output = '';
  1801. // try to keep mem. use down
  1802. $linecount = sizeof($lines);
  1803. $in_comment = false;
  1804. for ($i = 0; $i < $linecount; $i++)
  1805. {
  1806. if (trim($lines[$i]) == '/*')
  1807. {
  1808. $in_comment = true;
  1809. }
  1810. if (!$in_comment)
  1811. {
  1812. $output .= $lines[$i] . "\n";
  1813. }
  1814. if (trim($lines[$i]) == '*/')
  1815. {
  1816. $in_comment = false;
  1817. }
  1818. }
  1819. unset($lines);
  1820. return $output;
  1821. }
  1822. /**
  1823. * Cache moderators, called whenever permissions are changed via admin_permissions. Changes of username
  1824. * and group names must be carried through for the moderators table
  1825. */
  1826. function cache_moderators()
  1827. {
  1828. global $db, $cache, $auth, $phpbb_root_path, $phpEx;
  1829. // Remove cached sql results
  1830. $cache->destroy('sql', MODERATOR_CACHE_TABLE);
  1831. // Clear table
  1832. switch ($db->sql_layer)
  1833. {
  1834. case 'sqlite':
  1835. case 'firebird':
  1836. $db->sql_query('DELETE FROM ' . MODERATOR_CACHE_TABLE);
  1837. break;
  1838. default:
  1839. $db->sql_query('TRUNCATE TABLE ' . MODERATOR_CACHE_TABLE);
  1840. break;
  1841. }
  1842. // We add moderators who have forum moderator permissions without an explicit ACL_NEVER setting
  1843. $hold_ary = $ug_id_ary = $sql_ary = array();
  1844. // Grab all users having moderative options...
  1845. $hold_ary = $auth->acl_user_raw_data(false, 'm_%', false);
  1846. // Add users?
  1847. if (sizeof($hold_ary))
  1848. {
  1849. // At least one moderative option warrants a display
  1850. $ug_id_ary = array_keys($hold_ary);
  1851. // Remove users who have group memberships with DENY moderator permissions
  1852. $sql = $db->sql_build_query('SELECT', array(
  1853. 'SELECT' => 'a.forum_id, ug.user_id',
  1854. 'FROM' => array(
  1855. ACL_OPTIONS_TABLE => 'o',
  1856. USER_GROUP_TABLE => 'ug',
  1857. ACL_GROUPS_TABLE => 'a'
  1858. ),
  1859. 'LEFT_JOIN' => array(
  1860. array(
  1861. 'FROM' => array(ACL_ROLES_DATA_TABLE => 'r'),
  1862. 'ON' => 'a.auth_role_id = r.role_id'
  1863. )
  1864. ),
  1865. 'WHERE' => '(o.auth_option_id = a.auth_option_id OR o.auth_option_id = r.auth_option_id)
  1866. AND ((a.auth_setting = ' . ACL_NEVER . ' AND r.auth_setting IS NULL)
  1867. OR r.auth_setting = ' . ACL_NEVER . ')
  1868. AND a.group_id = ug.group_id
  1869. AND ' . $db->sql_in_set('ug.user_id', $ug_id_ary) . "
  1870. AND ug.user_pending = 0
  1871. AND o.auth_option " . $db->sql_like_expression('m_' . $db->any_char),
  1872. ));
  1873. $result = $db->sql_query($sql);
  1874. while ($row = $db->sql_fetchrow($result))
  1875. {
  1876. if (isset($hold_ary[$row['user_id']][$row['forum_id']]))
  1877. {
  1878. unset($hold_ary[$row['user_id']][$row['forum_id']]);
  1879. }
  1880. }
  1881. $db->sql_freeresult($result);
  1882. if (sizeof($hold_ary))
  1883. {
  1884. // Get usernames...
  1885. $sql = 'SELECT user_id, username
  1886. FROM ' . USERS_TABLE . '
  1887. WHERE ' . $db->sql_in_set('user_id', array_keys($hold_ary));
  1888. $result = $db->sql_query($sql);
  1889. $usernames_ary = array();
  1890. while ($row = $db->sql_fetchrow($result))
  1891. {
  1892. $usernames_ary[$row['user_id']] = $row['username'];
  1893. }
  1894. foreach ($hold_ary as $user_id => $forum_id_ary)
  1895. {
  1896. // Do not continue if user does not exist
  1897. if (!isset($usernames_ary[$user_id]))
  1898. {
  1899. continue;
  1900. }
  1901. foreach ($forum_id_ary as $forum_id => $auth_ary)
  1902. {
  1903. $sql_ary[] = array(
  1904. 'forum_id' => (int) $forum_id,
  1905. 'user_id' => (int) $user_id,
  1906. 'username' => (string) $usernames_ary[$user_id],
  1907. 'group_id' => 0,
  1908. 'group_name' => ''
  1909. );
  1910. }
  1911. }
  1912. }
  1913. }
  1914. // Now to the groups...
  1915. $hold_ary = $auth->acl_group_raw_data(false, 'm_%', false);
  1916. if (sizeof($hold_ary))
  1917. {
  1918. $ug_id_ary = array_keys($hold_ary);
  1919. // Make sure not hidden or special groups are involved...
  1920. $sql = 'SELECT group_name, group_id, group_type
  1921. FROM ' . GROUPS_TABLE . '
  1922. WHERE ' . $db->sql_in_set('group_id', $ug_id_ary);
  1923. $result = $db->sql_query($sql);
  1924. $groupnames_ary = array();
  1925. while ($row = $db->sql_fetchrow($result))
  1926. {
  1927. if ($row['group_type'] == GROUP_HIDDEN || $row['group_type'] == GROUP_SPECIAL)
  1928. {
  1929. unset($hold_ary[$row['group_id']]);
  1930. }
  1931. $groupnames_ary[$row['group_id']] = $row['group_name'];
  1932. }
  1933. $db->sql_freeresult($result);
  1934. foreach ($hold_ary as $group_id => $forum_id_ary)
  1935. {
  1936. // If there is no group, we do not assign it...
  1937. if (!isset($groupnames_ary[$group_id]))
  1938. {
  1939. continue;
  1940. }
  1941. foreach ($forum_id_ary as $forum_id => $auth_ary)
  1942. {
  1943. $flag = false;
  1944. foreach ($auth_ary as $auth_option => $setting)
  1945. {
  1946. // Make sure at least one ACL_YES option is set...
  1947. if ($setting == ACL_YES)
  1948. {
  1949. $flag = true;
  1950. break;
  1951. }
  1952. }
  1953. if (!$flag)
  1954. {
  1955. continue;
  1956. }
  1957. $sql_ary[] = array(
  1958. 'forum_id' => (int) $forum_id,
  1959. 'user_id' => 0,
  1960. 'username' => '',
  1961. 'group_id' => (int) $group_id,
  1962. 'group_name' => (string) $groupnames_ary[$group_id]
  1963. );
  1964. }
  1965. }
  1966. }
  1967. $db->sql_multi_insert(MODERATOR_CACHE_TABLE, $sql_ary);
  1968. }
  1969. /**
  1970. * View log
  1971. */
  1972. 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')
  1973. {
  1974. global $db, $user, $auth, $phpEx, $phpbb_root_path, $phpbb_admin_path;
  1975. $topic_id_list = $reportee_id_list = $is_auth = $is_mod = array();
  1976. $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');
  1977. switch ($mode)
  1978. {
  1979. case 'admin':
  1980. $log_type = LOG_ADMIN;
  1981. $sql_forum = '';
  1982. break;
  1983. case 'mod':
  1984. $log_type = LOG_MOD;
  1985. if ($topic_id)
  1986. {
  1987. $sql_forum = 'AND l.topic_id = ' . intval($topic_id);
  1988. }
  1989. else if (is_array($forum_id))
  1990. {
  1991. $sql_forum = 'AND ' . $db->sql_in_set('l.forum_id', array_map('intval', $forum_id));
  1992. }
  1993. else
  1994. {
  1995. $sql_forum = ($forum_id) ? 'AND l.forum_id = ' . intval($forum_id) : '';
  1996. }
  1997. break;
  1998. case 'user':
  1999. $log_type = LOG_USERS;
  2000. $sql_forum = 'AND l.reportee_id = ' . (int) $user_id;
  2001. break;
  2002. case 'users':
  2003. $log_type = LOG_USERS;
  2004. $sql_forum = '';
  2005. break;
  2006. case 'critical':
  2007. $log_type = LOG_CRITICAL;
  2008. $sql_forum = '';
  2009. break;
  2010. default:
  2011. return;
  2012. }
  2013. $sql = "SELECT l.*, u.username, u.username_clean, u.user_colour
  2014. FROM " . LOG_TABLE . " l, " . USERS_TABLE . " u
  2015. WHERE l.log_type = $log_type
  2016. AND u.user_id = l.user_id
  2017. " . (($limit_days) ? "AND l.log_time >= $limit_days" : '') . "
  2018. $sql_forum
  2019. ORDER BY $sort_by";
  2020. $result = $db->sql_query_limit($sql, $limit, $offset);
  2021. $i = 0;
  2022. $log = array();
  2023. while ($row = $db->sql_fetchrow($result))
  2024. {
  2025. if ($row['topic_id'])
  2026. {
  2027. $topic_id_list[] = $row['topic_id'];
  2028. }
  2029. if ($row['reportee_id'])
  2030. {
  2031. $reportee_id_list[] = $row['reportee_id'];
  2032. }
  2033. $log[$i] = array(
  2034. 'id' => $row['log_id'],
  2035. 'reportee_id' => $row['reportee_id'],
  2036. 'reportee_username' => '',
  2037. 'reportee_username_full'=> '',
  2038. 'user_id' => $row['user_id'],
  2039. 'username' => $row['username'],
  2040. 'username_full' => get_username_string('full', $row['user_id'], $row['username'], $row['user_colour'], false, $profile_url),
  2041. 'ip' => $row['log_ip'],
  2042. 'time' => $row['log_time'],
  2043. 'forum_id' => $row['forum_id'],
  2044. 'topic_id' => $row['topic_id'],
  2045. 'viewforum' => ($row['forum_id'] && $auth->acl_get('f_read', $row['forum_id'])) ? append_sid("{$phpbb_root_path}viewforum.$phpEx", 'f=' . $row['forum_id']) : false,
  2046. 'action' => (isset($user->lang[$row['log_operation']])) ? $user->lang[$row['log_operation']] : '{' . ucfirst(str_replace('_', ' ', $row['log_operation'])) . '}',
  2047. );
  2048. if (!empty($row['log_data']))
  2049. {
  2050. $log_data_ary = unserialize($row['log_data']);
  2051. if (isset($user->lang[$row['log_operation']]))
  2052. {
  2053. // We supress the warning about inappropriate number of passed parameters here due to possible changes within LOG strings from one version to another.
  2054. $log[$i]['action'] = @vsprintf($log[$i]['action'], $log_data_ary);
  2055. // If within the admin panel we do not censor text out
  2056. if (defined('IN_ADMIN'))
  2057. {
  2058. $log[$i]['action'] = bbcode_nl2br($log[$i]['action']);
  2059. }
  2060. else
  2061. {
  2062. $log[$i]['action'] = bbcode_nl2br(censor_text($log[$i]['action']));
  2063. }
  2064. }
  2065. else
  2066. {
  2067. $log[$i]['action'] .= '<br />' . implode('', $log_data_ary);
  2068. }
  2069. /* Apply make_clickable... has to be seen if it is for good. :/
  2070. // Seems to be not for the moment, reconsider later...
  2071. $log[$i]['action'] = make_clickable($log[$i]['action']);
  2072. */
  2073. }
  2074. $i++;
  2075. }
  2076. $db->sql_freeresult($result);
  2077. if (sizeof($topic_id_list))
  2078. {
  2079. $topic_id_list = array_unique($topic_id_list);
  2080. // This query is not really needed if move_topics() updates the forum_id field,
  2081. // although it's also used to determine if the topic still exists in the database
  2082. $sql = 'SELECT topic_id, forum_id
  2083. FROM ' . TOPICS_TABLE . '
  2084. WHERE ' . $db->sql_in_set('topic_id', array_map('intval', $topic_id_list));
  2085. $result = $db->sql_query($sql);
  2086. $default_forum_id = 0;
  2087. while ($row = $db->sql_fetchrow($result))
  2088. {
  2089. if (!$row['forum_id'])
  2090. {
  2091. if ($auth->acl_getf_global('f_read'))
  2092. {
  2093. if (!$default_forum_id)
  2094. {
  2095. $sql = 'SELECT forum_id
  2096. FROM ' . FORUMS_TABLE . '
  2097. WHERE forum_type = ' . FORUM_POST;
  2098. $f_result = $db->sql_query_limit($sql, 1);
  2099. $default_forum_id = (int) $db->sql_fetchfield('forum_id', false, $f_result);
  2100. $db->sql_freeresult($f_result);
  2101. }
  2102. $is_auth[$row['topic_id']] = $default_forum_id;
  2103. }
  2104. }
  2105. else
  2106. {
  2107. if ($auth->acl_get('f_read', $row['forum_id']))
  2108. {
  2109. $is_auth[$row['topic_id']] = $row['forum_id'];
  2110. }
  2111. }
  2112. if ($auth->acl_gets('a_', 'm_', $row['forum_id']))
  2113. {
  2114. $is_mod[$row['topic_id']] = $row['forum_id'];
  2115. }
  2116. }
  2117. $db->sql_freeresult($result);
  2118. foreach ($log as $key => $row)
  2119. {
  2120. $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;
  2121. $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;
  2122. }
  2123. }
  2124. if (sizeof($reportee_id_list))
  2125. {
  2126. $reportee_id_list = array_unique($reportee_id_list);
  2127. $reportee_names_list = array();
  2128. $sql = 'SELECT user_id, username, user_colour
  2129. FROM ' . USERS_TABLE . '
  2130. WHERE ' . $db->sql_in_set('user_id', $reportee_id_list);
  2131. $result = $db->sql_query($sql);
  2132. while ($row = $db->sql_fetchrow($result))
  2133. {
  2134. $reportee_names_list[$row['user_id']] = $row;
  2135. }
  2136. $db->sql_freeresult($result);
  2137. foreach ($log as $key => $row)
  2138. {
  2139. if (!isset($reportee_names_list[$row['reportee_id']]))
  2140. {
  2141. continue;
  2142. }
  2143. $log[$key]['reportee_username'] = $reportee_names_list[$row['reportee_id']]['username'];
  2144. $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);
  2145. }
  2146. }
  2147. $sql = 'SELECT COUNT(l.log_id) AS total_entries
  2148. FROM ' . LOG_TABLE . " l
  2149. WHERE l.log_type = $log_type
  2150. AND l.log_time >= $limit_days
  2151. $sql_forum";
  2152. $result = $db->sql_query($sql);
  2153. $log_count = (int) $db->sql_fetchfield('total_entries');
  2154. $db->sql_freeresult($result);
  2155. return;
  2156. }
  2157. /**
  2158. * Update foes - remove moderators and administrators from foe lists...
  2159. */
  2160. function update_foes($group_id = false, $user_id = false)
  2161. {
  2162. global $db, $auth;
  2163. // update foes for some user
  2164. if (is_array($user_id) && sizeof($user_id))
  2165. {
  2166. $sql = 'DELETE FROM ' . ZEBRA_TABLE . '
  2167. WHERE ' . $db->sql_in_set('zebra_id', $user_id) . '
  2168. AND foe = 1';
  2169. $db->sql_query($sql);
  2170. return;
  2171. }
  2172. // update foes for some group
  2173. if (is_array($group_id) && sizeof($group_id))
  2174. {
  2175. // Grab group settings...
  2176. $sql = $db->sql_build_query('SELECT', array(
  2177. 'SELECT' => 'a.group_id',
  2178. 'FROM' => array(
  2179. ACL_OPTIONS_TABLE => 'ao',
  2180. ACL_GROUPS_TABLE => 'a'
  2181. ),
  2182. 'LEFT_JOIN' => array(
  2183. array(
  2184. 'FROM' => array(ACL_ROLES_DATA_TABLE => 'r'),
  2185. 'ON' => 'a.auth_role_id = r.role_id'
  2186. ),
  2187. ),
  2188. 'WHERE' => '(ao.auth_option_id = a.auth_option_id OR ao.auth_option_id = r.auth_option_id)
  2189. AND ' . $db->sql_in_set('a.group_id', $group_id) . "
  2190. AND ao.auth_option IN ('a_', 'm_')",
  2191. 'GROUP_BY' => 'a.group_id'
  2192. ));
  2193. $result = $db->sql_query($sql);
  2194. $groups = array();
  2195. while ($row = $db->sql_fetchrow($result))
  2196. {
  2197. $groups[] = (int) $row['group_id'];
  2198. }
  2199. $db->sql_freeresult($result);
  2200. if (!sizeof($groups))
  2201. {
  2202. return;
  2203. }
  2204. switch ($db->sql_layer)
  2205. {
  2206. case 'mysqli':
  2207. case 'mysql4':
  2208. $sql = 'DELETE ' . (($db->sql_layer === 'mysqli' || version_compare($db->mysql_version, '4.1', '>=')) ? 'z.*' : ZEBRA_TABLE) . '
  2209. FROM ' . ZEBRA_TABLE . ' z, ' . USER_GROUP_TABLE . ' ug
  2210. WHERE z.zebra_id = ug.user_id
  2211. AND z.foe = 1
  2212. AND ' . $db->sql_in_set('ug.group_id', $groups);
  2213. $db->sql_query($sql);
  2214. break;
  2215. default:
  2216. $sql = 'SELECT user_id
  2217. FROM ' . USER_GROUP_TABLE . '
  2218. WHERE ' . $db->sql_in_set('group_id', $groups);
  2219. $result = $db->sql_query($sql);
  2220. $users = array();
  2221. while ($row = $db->sql_fetchrow($result))
  2222. {
  2223. $users[] = (int) $row['user_id'];
  2224. }
  2225. $db->sql_freeresult($result);
  2226. if (sizeof($users))
  2227. {
  2228. $sql = 'DELETE FROM ' . ZEBRA_TABLE . '
  2229. WHERE ' . $db->sql_in_set('zebra_id', $users) . '
  2230. AND foe = 1';
  2231. $db->sql_query($sql);
  2232. }
  2233. break;
  2234. }
  2235. return;
  2236. }
  2237. // update foes for everyone
  2238. $perms = array();
  2239. foreach ($auth->acl_get_list(false, array('a_', 'm_'), false) as $forum_id => $forum_ary)
  2240. {
  2241. foreach ($forum_ary as $auth_option => $user_ary)
  2242. {
  2243. $perms = array_merge($perms, $user_ary);
  2244. }
  2245. }
  2246. if (sizeof($perms))
  2247. {
  2248. $sql = 'DELETE FROM ' . ZEBRA_TABLE . '
  2249. WHERE ' . $db->sql_in_set('zebra_id', array_unique($perms)) . '
  2250. AND foe = 1';
  2251. $db->sql_query($sql);
  2252. }
  2253. unset($perms);
  2254. }
  2255. /**
  2256. * Lists inactive users
  2257. */
  2258. function view_inactive_users(&$users, &$user_count, $limit = 0, $offset = 0, $limit_days = 0, $sort_by = 'user_inactive_time DESC')
  2259. {
  2260. global $db, $user;
  2261. $sql = 'SELECT COUNT(user_id) AS user_count
  2262. FROM ' . USERS_TABLE . '
  2263. WHERE user_type = ' . USER_INACTIVE .
  2264. (($limit_days) ? " AND user_inactive_time >= $limit_days" : '');
  2265. $result = $db->sql_query($sql);
  2266. $user_count = (int) $db->sql_fetchfield('user_count');
  2267. $db->sql_freeresult($result);
  2268. if ($offset >= $user_count)
  2269. {
  2270. $offset = ($offset - $limit < 0) ? 0 : $offset - $limit;
  2271. }
  2272. $sql = 'SELECT user_id, username, user_regdate, user_lastvisit, user_inactive_time, user_inactive_reason
  2273. FROM ' . USERS_TABLE . '
  2274. WHERE user_type = ' . USER_INACTIVE .
  2275. (($limit_days) ? " AND user_inactive_time >= $limit_days" : '') . "
  2276. ORDER BY $sort_by";
  2277. $result = $db->sql_query_limit($sql, $limit, $offset);
  2278. while ($row = $db->sql_fetchrow($result))
  2279. {
  2280. $row['inactive_reason'] = $user->lang['INACTIVE_REASON_UNKNOWN'];
  2281. switch ($row['user_inactive_reason'])
  2282. {
  2283. case INACTIVE_REGISTER:
  2284. $row['inactive_reason'] = $user->lang['INACTIVE_REASON_REGISTER'];
  2285. break;
  2286. case INACTIVE_PROFILE:
  2287. $row['inactive_reason'] = $user->lang['INACTIVE_REASON_PROFILE'];
  2288. break;
  2289. case INACTIVE_MANUAL:
  2290. $row['inactive_reason'] = $user->lang['INACTIVE_REASON_MANUAL'];
  2291. break;
  2292. case INACTIVE_REMIND:
  2293. $row['inactive_reason'] = $user->lang['INACTIVE_REASON_REMIND'];
  2294. break;
  2295. }
  2296. $users[] = $row;
  2297. }
  2298. return $offset;
  2299. }
  2300. /**
  2301. * Lists warned users
  2302. */
  2303. function view_warned_users(&$users, &$user_count, $limit = 0, $offset = 0, $limit_days = 0, $sort_by = 'user_warnings DESC')
  2304. {
  2305. global $db;
  2306. $sql = 'SELECT user_id, username, user_colour, user_warnings, user_last_warning
  2307. FROM ' . USERS_TABLE . '
  2308. WHERE user_warnings > 0
  2309. ' . (($limit_days) ? "AND user_last_warning >= $limit_days" : '') . "
  2310. ORDER BY $sort_by";
  2311. $result = $db->sql_query_limit($sql, $limit, $offset);
  2312. $users = $db->sql_fetchrowset($result);
  2313. $db->sql_freeresult($result);
  2314. $sql = 'SELECT count(user_id) AS user_count
  2315. FROM ' . USERS_TABLE . '
  2316. WHERE user_warnings > 0
  2317. ' . (($limit_days) ? "AND user_last_warning >= $limit_days" : '');
  2318. $result = $db->sql_query($sql);
  2319. $user_count = (int) $db->sql_fetchfield('user_count');
  2320. $db->sql_freeresult($result);
  2321. return;
  2322. }
  2323. /**
  2324. * Get database size
  2325. * Currently only mysql and mssql are supported
  2326. */
  2327. function get_database_size()
  2328. {
  2329. global $db, $user, $table_prefix;
  2330. $database_size = false;
  2331. // This code is heavily influenced by a similar routine in phpMyAdmin 2.2.0
  2332. switch ($db->sql_layer)
  2333. {
  2334. case 'mysql':
  2335. case 'mysql4':
  2336. case 'mysqli':
  2337. $sql = 'SELECT VERSION() AS mysql_version';
  2338. $result = $db->sql_query($sql);
  2339. $row = $db->sql_fetchrow($result);
  2340. $db->sql_freeresult($result);
  2341. if ($row)
  2342. {
  2343. $version = $row['mysql_version'];
  2344. if (preg_match('#(3\.23|[45]\.)#', $version))
  2345. {
  2346. $db_name = (preg_match('#^(?:3\.23\.(?:[6-9]|[1-9]{2}))|[45]\.#', $version)) ? "`{$db->dbname}`" : $db->dbname;
  2347. $sql = 'SHOW TABLE STATUS
  2348. FROM ' . $db_name;
  2349. $result = $db->sql_query($sql, 7200);
  2350. $database_size = 0;
  2351. while ($row = $db->sql_fetchrow($result))
  2352. {
  2353. if ((isset($row['Type']) && $row['Type'] != 'MRG_MyISAM') || (isset($row['Engine']) && ($row['Engine'] == 'MyISAM' || $row['Engine'] == 'InnoDB')))
  2354. {
  2355. if ($table_prefix != '')
  2356. {
  2357. if (strpos($row['Name'], $table_prefix) !== false)
  2358. {
  2359. $database_size += $row['Data_length'] + $row['Index_length'];
  2360. }
  2361. }
  2362. else
  2363. {
  2364. $database_size += $row['Data_length'] + $row['Index_length'];
  2365. }
  2366. }
  2367. }
  2368. $db->sql_freeresult($result);
  2369. }
  2370. }
  2371. break;
  2372. case 'firebird':
  2373. global $dbname;
  2374. // if it on the local machine, we can get lucky
  2375. if (file_exists($dbname))
  2376. {
  2377. $database_size = filesize($dbname);
  2378. }
  2379. break;
  2380. case 'sqlite':
  2381. global $dbhost;
  2382. if (file_exists($dbhost))
  2383. {
  2384. $database_size = filesize($dbhost);
  2385. }
  2386. break;
  2387. case 'mssql':
  2388. case 'mssql_odbc':
  2389. $sql = 'SELECT ((SUM(size) * 8.0) * 1024.0) as dbsize
  2390. FROM sysfiles';
  2391. $result = $db->sql_query($sql, 7200);
  2392. $database_size = ($row = $db->sql_fetchrow($result)) ? $row['dbsize'] : false;
  2393. $db->sql_freeresult($result);
  2394. break;
  2395. case 'postgres':
  2396. $sql = "SELECT proname
  2397. FROM pg_proc
  2398. WHERE proname = 'pg_database_size'";
  2399. $result = $db->sql_query($sql);
  2400. $row = $db->sql_fetchrow($result);
  2401. $db->sql_freeresult($result);
  2402. if ($row['proname'] == 'pg_database_size')
  2403. {
  2404. $database = $db->dbname;
  2405. if (strpos($database, '.') !== false)
  2406. {
  2407. list($database, ) = explode('.', $database);
  2408. }
  2409. $sql = "SELECT oid
  2410. FROM pg_database
  2411. WHERE datname = '$database'";
  2412. $result = $db->sql_query($sql);
  2413. $row = $db->sql_fetchrow($result);
  2414. $db->sql_freeresult($result);
  2415. $oid = $row['oid'];
  2416. $sql = 'SELECT pg_database_size(' . $oid . ') as size';
  2417. $result = $db->sql_query($sql);
  2418. $row = $db->sql_fetchrow($result);
  2419. $db->sql_freeresult($result);
  2420. $database_size = $row['size'];
  2421. }
  2422. break;
  2423. case 'oracle':
  2424. $sql = 'SELECT SUM(bytes) as dbsize
  2425. FROM user_segments';
  2426. $result = $db->sql_query($sql, 7200);
  2427. $database_size = ($row = $db->sql_fetchrow($result)) ? $row['dbsize'] : false;
  2428. $db->sql_freeresult($result);
  2429. break;
  2430. }
  2431. $database_size = ($database_size !== false) ? get_formatted_filesize($database_size) : $user->lang['NOT_AVAILABLE'];
  2432. return $database_size;
  2433. }
  2434. /**
  2435. * Retrieve contents from remotely stored file
  2436. */
  2437. function get_remote_file($host, $directory, $filename, &$errstr, &$errno, $port = 80, $timeout = 10)
  2438. {
  2439. global $user;
  2440. if ($fsock = @fsockopen($host, $port, $errno, $errstr, $timeout))
  2441. {
  2442. @fputs($fsock, "GET $directory/$filename HTTP/1.1\r\n");
  2443. @fputs($fsock, "HOST: $host\r\n");
  2444. @fputs($fsock, "Connection: close\r\n\r\n");
  2445. $file_info = '';
  2446. $get_info = false;
  2447. while (!@feof($fsock))
  2448. {
  2449. if ($get_info)
  2450. {
  2451. $file_info .= @fread($fsock, 1024);
  2452. }
  2453. else
  2454. {
  2455. $line = @fgets($fsock, 1024);
  2456. if ($line == "\r\n")
  2457. {
  2458. $get_info = true;
  2459. }
  2460. else if (stripos($line, '404 not found') !== false)
  2461. {
  2462. $errstr = $user->lang['FILE_NOT_FOUND'] . ': ' . $filename;
  2463. return false;
  2464. }
  2465. }
  2466. }
  2467. @fclose($fsock);
  2468. }
  2469. else
  2470. {
  2471. if ($errstr)
  2472. {
  2473. $errstr = utf8_convert_message($errstr);
  2474. return false;
  2475. }
  2476. else
  2477. {
  2478. $errstr = $user->lang['FSOCK_DISABLED'];
  2479. return false;
  2480. }
  2481. }
  2482. return $file_info;
  2483. }
  2484. /**
  2485. * Tidy Warnings
  2486. * Remove all warnings which have now expired from the database
  2487. * The duration of a warning can be defined by the administrator
  2488. * This only removes the warning and reduces the associated count,
  2489. * it does not remove the user note recording the contents of the warning
  2490. */
  2491. function tidy_warnings()
  2492. {
  2493. global $db, $config;
  2494. $expire_date = time() - ($config['warnings_expire_days'] * 86400);
  2495. $warning_list = $user_list = array();
  2496. $sql = 'SELECT * FROM ' . WARNINGS_TABLE . "
  2497. WHERE warning_time < $expire_date";
  2498. $result = $db->sql_query($sql);
  2499. while ($row = $db->sql_fetchrow($result))
  2500. {
  2501. $warning_list[] = $row['warning_id'];
  2502. $user_list[$row['user_id']] = isset($user_list[$row['user_id']]) ? ++$user_list[$row['user_id']] : 1;
  2503. }
  2504. $db->sql_freeresult($result);
  2505. if (sizeof($warning_list))
  2506. {
  2507. $db->sql_transaction('begin');
  2508. $sql = 'DELETE FROM ' . WARNINGS_TABLE . '
  2509. WHERE ' . $db->sql_in_set('warning_id', $warning_list);
  2510. $db->sql_query($sql);
  2511. foreach ($user_list as $user_id => $value)
  2512. {
  2513. $sql = 'UPDATE ' . USERS_TABLE . " SET user_warnings = user_warnings - $value
  2514. WHERE user_id = $user_id";
  2515. $db->sql_query($sql);
  2516. }
  2517. $db->sql_transaction('commit');
  2518. }
  2519. set_config('warnings_last_gc', time(), true);
  2520. }
  2521. /**
  2522. * Tidy database, doing some maintanance tasks
  2523. */
  2524. function tidy_database()
  2525. {
  2526. global $db;
  2527. // Here we check permission consistency
  2528. // Sometimes, it can happen permission tables having forums listed which do not exist
  2529. $sql = 'SELECT forum_id
  2530. FROM ' . FORUMS_TABLE;
  2531. $result = $db->sql_query($sql);
  2532. $forum_ids = array(0);
  2533. while ($row = $db->sql_fetchrow($result))
  2534. {
  2535. $forum_ids[] = $row['forum_id'];
  2536. }
  2537. $db->sql_freeresult($result);
  2538. // Delete those rows from the acl tables not having listed the forums above
  2539. $sql = 'DELETE FROM ' . ACL_GROUPS_TABLE . '
  2540. WHERE ' . $db->sql_in_set('forum_id', $forum_ids, true);
  2541. $db->sql_query($sql);
  2542. $sql = 'DELETE FROM ' . ACL_USERS_TABLE . '
  2543. WHERE ' . $db->sql_in_set('forum_id', $forum_ids, true);
  2544. $db->sql_query($sql);
  2545. set_config('database_last_gc', time(), true);
  2546. }
  2547. /**
  2548. * Add permission language - this will make sure custom files will be included
  2549. */
  2550. function add_permission_language()
  2551. {
  2552. global $user, $phpEx;
  2553. // First of all, our own file. We need to include it as the first file because it presets all relevant variables.
  2554. $user->add_lang('acp/permissions_phpbb');
  2555. $files_to_add = array();
  2556. // Now search in acp and mods folder for permissions_ files.
  2557. foreach (array('acp/', 'mods/') as $path)
  2558. {
  2559. $dh = @opendir($user->lang_path . $path);
  2560. if ($dh)
  2561. {
  2562. while (($file = readdir($dh)) !== false)
  2563. {
  2564. if ($file !== 'permissions_phpbb.' . $phpEx && strpos($file, 'permissions_') === 0 && substr($file, -(strlen($phpEx) + 1)) === '.' . $phpEx)
  2565. {
  2566. $files_to_add[] = $path . substr($file, 0, -(strlen($phpEx) + 1));
  2567. }
  2568. }
  2569. closedir($dh);
  2570. }
  2571. }
  2572. if (!sizeof($files_to_add))
  2573. {
  2574. return false;
  2575. }
  2576. $user->add_lang($files_to_add);
  2577. return true;
  2578. }
  2579. ?>