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

/forum/includes/functions_admin.php

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