PageRenderTime 68ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 1ms

/forum/includes/functions_admin.php

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