/forum/includes/functions_admin.php
PHP | 3332 lines | 2474 code | 533 blank | 325 comment | 409 complexity | 8a9091489069a79f504151a51e918198 MD5 | raw file
Possible License(s): LGPL-2.1, AGPL-3.0, AGPL-1.0, GPL-2.0, MPL-2.0-no-copyleft-exception
Large files files are truncated, but you can click here to view the full file
- <?php
- /**
- *
- * @package acp
- * @version $Id$
- * @copyright (c) 2005 phpBB Group
- * @license http://opensource.org/licenses/gpl-license.php GNU Public License
- *
- */
- /**
- * @ignore
- */
- if (!defined('IN_PHPBB'))
- {
- exit;
- }
- /**
- * Recalculate Nested Sets
- *
- * @param int $new_id first left_id (should start with 1)
- * @param string $pkey primary key-column (containing the id for the parent_id of the children)
- * @param string $table constant or fullname of the table
- * @param int $parent_id parent_id of the current set (default = 0)
- * @param array $where contains strings to compare closer on the where statement (additional)
- *
- * @author EXreaction
- */
- function recalc_nested_sets(&$new_id, $pkey, $table, $parent_id = 0, $where = array())
- {
- global $db;
- $sql = 'SELECT *
- FROM ' . $table . '
- WHERE parent_id = ' . (int) $parent_id .
- ((!empty($where)) ? ' AND ' . implode(' AND ', $where) : '') . '
- ORDER BY left_id ASC';
- $result = $db->sql_query($sql);
- while ($row = $db->sql_fetchrow($result))
- {
- // First we update the left_id for this module
- if ($row['left_id'] != $new_id)
- {
- $db->sql_query('UPDATE ' . $table . ' SET ' . $db->sql_build_array('UPDATE', array('left_id' => $new_id)) . " WHERE $pkey = {$row[$pkey]}");
- }
- $new_id++;
- // Then we go through any children and update their left/right id's
- recalc_nested_sets($new_id, $pkey, $table, $row[$pkey], $where);
- // Then we come back and update the right_id for this module
- if ($row['right_id'] != $new_id)
- {
- $db->sql_query('UPDATE ' . $table . ' SET ' . $db->sql_build_array('UPDATE', array('right_id' => $new_id)) . " WHERE $pkey = {$row[$pkey]}");
- }
- $new_id++;
- }
- $db->sql_freeresult($result);
- }
- /**
- * Simple version of jumpbox, just lists authed forums
- */
- 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)
- {
- global $db, $user, $auth;
- // This query is identical to the jumpbox one
- $sql = 'SELECT forum_id, forum_name, parent_id, forum_type, forum_flags, forum_options, left_id, right_id
- FROM ' . FORUMS_TABLE . '
- ORDER BY left_id ASC';
- $result = $db->sql_query($sql, 600);
- $right = 0;
- $padding_store = array('0' => '');
- $padding = '';
- $forum_list = ($return_array) ? array() : '';
- // Sometimes it could happen that forums will be displayed here not be displayed within the index page
- // This is the result of forums not displayed at index, having list permissions and a parent of a forum with no permissions.
- // If this happens, the padding could be "broken"
- while ($row = $db->sql_fetchrow($result))
- {
- if ($row['left_id'] < $right)
- {
- $padding .= ' ';
- $padding_store[$row['parent_id']] = $padding;
- }
- else if ($row['left_id'] > $right + 1)
- {
- $padding = (isset($padding_store[$row['parent_id']])) ? $padding_store[$row['parent_id']] : '';
- }
- $right = $row['right_id'];
- $disabled = false;
- if (!$ignore_acl && $auth->acl_get('f_list', $row['forum_id']))
- {
- 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'])))
- {
- $disabled = true;
- }
- else if (!$only_acl_post && !$auth->acl_gets(array('f_list', 'a_forum', 'a_forumadd', 'a_forumdel'), $row['forum_id']))
- {
- $disabled = true;
- }
- }
- else if (!$ignore_acl)
- {
- continue;
- }
- if (
- ((is_array($ignore_id) && in_array($row['forum_id'], $ignore_id)) || $row['forum_id'] == $ignore_id)
- ||
- // Non-postable forum with no subforums, don't display
- ($row['forum_type'] == FORUM_CAT && ($row['left_id'] + 1 == $row['right_id']) && $ignore_emptycat)
- ||
- ($row['forum_type'] != FORUM_POST && $ignore_nonpost)
- )
- {
- $disabled = true;
- }
- if ($return_array)
- {
- // Include some more information...
- $selected = (is_array($select_id)) ? ((in_array($row['forum_id'], $select_id)) ? true : false) : (($row['forum_id'] == $select_id) ? true : false);
- $forum_list[$row['forum_id']] = array_merge(array('padding' => $padding, 'selected' => ($selected && !$disabled), 'disabled' => $disabled), $row);
- }
- else
- {
- $selected = (is_array($select_id)) ? ((in_array($row['forum_id'], $select_id)) ? ' selected="selected"' : '') : (($row['forum_id'] == $select_id) ? ' selected="selected"' : '');
- $forum_list .= '<option value="' . $row['forum_id'] . '"' . (($disabled) ? ' disabled="disabled" class="disabled-option"' : $selected) . '>' . $padding . $row['forum_name'] . '</option>';
- }
- }
- $db->sql_freeresult($result);
- unset($padding_store);
- return $forum_list;
- }
- /**
- * Generate size select options
- */
- function size_select_options($size_compare)
- {
- global $user;
- $size_types_text = array($user->lang['BYTES'], $user->lang['KIB'], $user->lang['MIB']);
- $size_types = array('b', 'kb', 'mb');
- $s_size_options = '';
- for ($i = 0, $size = sizeof($size_types_text); $i < $size; $i++)
- {
- $selected = ($size_compare == $size_types[$i]) ? ' selected="selected"' : '';
- $s_size_options .= '<option value="' . $size_types[$i] . '"' . $selected . '>' . $size_types_text[$i] . '</option>';
- }
- return $s_size_options;
- }
- /**
- * Generate list of groups (option fields without select)
- *
- * @param int $group_id The default group id to mark as selected
- * @param array $exclude_ids The group ids to exclude from the list, false (default) if you whish to exclude no id
- * @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.
- *
- * @return string The list of options.
- */
- function group_select_options($group_id, $exclude_ids = false, $manage_founder = false)
- {
- global $db, $user, $config;
- $exclude_sql = ($exclude_ids !== false && sizeof($exclude_ids)) ? 'WHERE ' . $db->sql_in_set('group_id', array_map('intval', $exclude_ids), true) : '';
- $sql_and = (!$config['coppa_enable']) ? (($exclude_sql) ? ' AND ' : ' WHERE ') . "group_name <> 'REGISTERED_COPPA'" : '';
- $sql_founder = ($manage_founder !== false) ? (($exclude_sql || $sql_and) ? ' AND ' : ' WHERE ') . 'group_founder_manage = ' . (int) $manage_founder : '';
- $sql = 'SELECT group_id, group_name, group_type
- FROM ' . GROUPS_TABLE . "
- $exclude_sql
- $sql_and
- $sql_founder
- ORDER BY group_type DESC, group_name ASC";
- $result = $db->sql_query($sql);
- $s_group_options = '';
- while ($row = $db->sql_fetchrow($result))
- {
- $selected = ($row['group_id'] == $group_id) ? ' selected="selected"' : '';
- $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>';
- }
- $db->sql_freeresult($result);
- return $s_group_options;
- }
- /**
- * Obtain authed forums list
- */
- function get_forum_list($acl_list = 'f_list', $id_only = true, $postable_only = false, $no_cache = false)
- {
- global $db, $auth;
- static $forum_rows;
- if (!isset($forum_rows))
- {
- // This query is identical to the jumpbox one
- $expire_time = ($no_cache) ? 0 : 600;
- $sql = 'SELECT forum_id, forum_name, parent_id, forum_type, left_id, right_id
- FROM ' . FORUMS_TABLE . '
- ORDER BY left_id ASC';
- $result = $db->sql_query($sql, $expire_time);
- $forum_rows = array();
- $right = $padding = 0;
- $padding_store = array('0' => 0);
- while ($row = $db->sql_fetchrow($result))
- {
- if ($row['left_id'] < $right)
- {
- $padding++;
- $padding_store[$row['parent_id']] = $padding;
- }
- else if ($row['left_id'] > $right + 1)
- {
- // Ok, if the $padding_store for this parent is empty there is something wrong. For now we will skip over it.
- // @todo digging deep to find out "how" this can happen.
- $padding = (isset($padding_store[$row['parent_id']])) ? $padding_store[$row['parent_id']] : $padding;
- }
- $right = $row['right_id'];
- $row['padding'] = $padding;
- $forum_rows[] = $row;
- }
- $db->sql_freeresult($result);
- unset($padding_store);
- }
- $rowset = array();
- foreach ($forum_rows as $row)
- {
- if ($postable_only && $row['forum_type'] != FORUM_POST)
- {
- continue;
- }
- if ($acl_list == '' || ($acl_list != '' && $auth->acl_gets($acl_list, $row['forum_id'])))
- {
- $rowset[] = ($id_only) ? (int) $row['forum_id'] : $row;
- }
- }
- return $rowset;
- }
- /**
- * Get forum branch
- */
- function get_forum_branch($forum_id, $type = 'all', $order = 'descending', $include_forum = true)
- {
- global $db;
- switch ($type)
- {
- case 'parents':
- $condition = 'f1.left_id BETWEEN f2.left_id AND f2.right_id';
- break;
- case 'children':
- $condition = 'f2.left_id BETWEEN f1.left_id AND f1.right_id';
- break;
- default:
- $condition = 'f2.left_id BETWEEN f1.left_id AND f1.right_id OR f1.left_id BETWEEN f2.left_id AND f2.right_id';
- break;
- }
- $rows = array();
- $sql = 'SELECT f2.*
- FROM ' . FORUMS_TABLE . ' f1
- LEFT JOIN ' . FORUMS_TABLE . " f2 ON ($condition)
- WHERE f1.forum_id = $forum_id
- ORDER BY f2.left_id " . (($order == 'descending') ? 'ASC' : 'DESC');
- $result = $db->sql_query($sql);
- while ($row = $db->sql_fetchrow($result))
- {
- if (!$include_forum && $row['forum_id'] == $forum_id)
- {
- continue;
- }
- $rows[] = $row;
- }
- $db->sql_freeresult($result);
- return $rows;
- }
- /**
- * Copies permissions from one forum to others
- *
- * @param int $src_forum_id The source forum we want to copy permissions from
- * @param array $dest_forum_ids The destination forum(s) we want to copy to
- * @param bool $clear_dest_perms True if destination permissions should be deleted
- * @param bool $add_log True if log entry should be added
- *
- * @return bool False on error
- *
- * @author bantu
- */
- function copy_forum_permissions($src_forum_id, $dest_forum_ids, $clear_dest_perms = true, $add_log = true)
- {
- global $db;
- // Only one forum id specified
- if (!is_array($dest_forum_ids))
- {
- $dest_forum_ids = array($dest_forum_ids);
- }
- // Make sure forum ids are integers
- $src_forum_id = (int) $src_forum_id;
- $dest_forum_ids = array_map('intval', $dest_forum_ids);
- // No source forum or no destination forums specified
- if (empty($src_forum_id) || empty($dest_forum_ids))
- {
- return false;
- }
- // Check if source forum exists
- $sql = 'SELECT forum_name
- FROM ' . FORUMS_TABLE . '
- WHERE forum_id = ' . $src_forum_id;
- $result = $db->sql_query($sql);
- $src_forum_name = $db->sql_fetchfield('forum_name');
- $db->sql_freeresult($result);
- // Source forum doesn't exist
- if (empty($src_forum_name))
- {
- return false;
- }
- // Check if destination forums exists
- $sql = 'SELECT forum_id, forum_name
- FROM ' . FORUMS_TABLE . '
- WHERE ' . $db->sql_in_set('forum_id', $dest_forum_ids);
- $result = $db->sql_query($sql);
- $dest_forum_ids = $dest_forum_names = array();
- while ($row = $db->sql_fetchrow($result))
- {
- $dest_forum_ids[] = (int) $row['forum_id'];
- $dest_forum_names[] = $row['forum_name'];
- }
- $db->sql_freeresult($result);
- // No destination forum exists
- if (empty($dest_forum_ids))
- {
- return false;
- }
- // From the mysql documentation:
- // Prior to MySQL 4.0.14, the target table of the INSERT statement cannot appear
- // in the FROM clause of the SELECT part of the query. This limitation is lifted in 4.0.14.
- // Due to this we stay on the safe side if we do the insertion "the manual way"
- // Rowsets we're going to insert
- $users_sql_ary = $groups_sql_ary = array();
- // Query acl users table for source forum data
- $sql = 'SELECT user_id, auth_option_id, auth_role_id, auth_setting
- FROM ' . ACL_USERS_TABLE . '
- WHERE forum_id = ' . $src_forum_id;
- $result = $db->sql_query($sql);
- while ($row = $db->sql_fetchrow($result))
- {
- $row = array(
- 'user_id' => (int) $row['user_id'],
- 'auth_option_id' => (int) $row['auth_option_id'],
- 'auth_role_id' => (int) $row['auth_role_id'],
- 'auth_setting' => (int) $row['auth_setting'],
- );
- foreach ($dest_forum_ids as $dest_forum_id)
- {
- $users_sql_ary[] = $row + array('forum_id' => $dest_forum_id);
- }
- }
- $db->sql_freeresult($result);
- // Query acl groups table for source forum data
- $sql = 'SELECT group_id, auth_option_id, auth_role_id, auth_setting
- FROM ' . ACL_GROUPS_TABLE . '
- WHERE forum_id = ' . $src_forum_id;
- $result = $db->sql_query($sql);
- while ($row = $db->sql_fetchrow($result))
- {
- $row = array(
- 'group_id' => (int) $row['group_id'],
- 'auth_option_id' => (int) $row['auth_option_id'],
- 'auth_role_id' => (int) $row['auth_role_id'],
- 'auth_setting' => (int) $row['auth_setting'],
- );
- foreach ($dest_forum_ids as $dest_forum_id)
- {
- $groups_sql_ary[] = $row + array('forum_id' => $dest_forum_id);
- }
- }
- $db->sql_freeresult($result);
- $db->sql_transaction('begin');
- // Clear current permissions of destination forums
- if ($clear_dest_perms)
- {
- $sql = 'DELETE FROM ' . ACL_USERS_TABLE . '
- WHERE ' . $db->sql_in_set('forum_id', $dest_forum_ids);
- $db->sql_query($sql);
- $sql = 'DELETE FROM ' . ACL_GROUPS_TABLE . '
- WHERE ' . $db->sql_in_set('forum_id', $dest_forum_ids);
- $db->sql_query($sql);
- }
- $db->sql_multi_insert(ACL_USERS_TABLE, $users_sql_ary);
- $db->sql_multi_insert(ACL_GROUPS_TABLE, $groups_sql_ary);
- if ($add_log)
- {
- add_log('admin', 'LOG_FORUM_COPIED_PERMISSIONS', $src_forum_name, implode(', ', $dest_forum_names));
- }
- $db->sql_transaction('commit');
- return true;
- }
- /**
- * Get physical file listing
- */
- function filelist($rootdir, $dir = '', $type = 'gif|jpg|jpeg|png')
- {
- $matches = array($dir => array());
- // Remove initial / if present
- $rootdir = (substr($rootdir, 0, 1) == '/') ? substr($rootdir, 1) : $rootdir;
- // Add closing / if not present
- $rootdir = ($rootdir && substr($rootdir, -1) != '/') ? $rootdir . '/' : $rootdir;
- // Remove initial / if present
- $dir = (substr($dir, 0, 1) == '/') ? substr($dir, 1) : $dir;
- // Add closing / if not present
- $dir = ($dir && substr($dir, -1) != '/') ? $dir . '/' : $dir;
- if (!is_dir($rootdir . $dir))
- {
- return $matches;
- }
- $dh = @opendir($rootdir . $dir);
- if (!$dh)
- {
- return $matches;
- }
- while (($fname = readdir($dh)) !== false)
- {
- if (is_file("$rootdir$dir$fname"))
- {
- if (filesize("$rootdir$dir$fname") && preg_match('#\.' . $type . '$#i', $fname))
- {
- $matches[$dir][] = $fname;
- }
- }
- else if ($fname[0] != '.' && is_dir("$rootdir$dir$fname"))
- {
- $matches += filelist($rootdir, "$dir$fname", $type);
- }
- }
- closedir($dh);
- return $matches;
- }
- /**
- * Move topic(s)
- */
- function move_topics($topic_ids, $forum_id, $auto_sync = true)
- {
- global $db;
- if (empty($topic_ids))
- {
- return;
- }
- $forum_ids = array($forum_id);
- if (!is_array($topic_ids))
- {
- $topic_ids = array($topic_ids);
- }
- $sql = 'DELETE FROM ' . TOPICS_TABLE . '
- WHERE ' . $db->sql_in_set('topic_moved_id', $topic_ids) . '
- AND forum_id = ' . $forum_id;
- $db->sql_query($sql);
- if ($auto_sync)
- {
- $sql = 'SELECT DISTINCT forum_id
- FROM ' . TOPICS_TABLE . '
- WHERE ' . $db->sql_in_set('topic_id', $topic_ids);
- $result = $db->sql_query($sql);
- while ($row = $db->sql_fetchrow($result))
- {
- $forum_ids[] = $row['forum_id'];
- }
- $db->sql_freeresult($result);
- }
- $table_ary = array(TOPICS_TABLE, POSTS_TABLE, LOG_TABLE, DRAFTS_TABLE, TOPICS_TRACK_TABLE);
- foreach ($table_ary as $table)
- {
- $sql = "UPDATE $table
- SET forum_id = $forum_id
- WHERE " . $db->sql_in_set('topic_id', $topic_ids);
- $db->sql_query($sql);
- }
- unset($table_ary);
- if ($auto_sync)
- {
- sync('forum', 'forum_id', $forum_ids, true, true);
- unset($forum_ids);
- }
- }
- /**
- * Move post(s)
- */
- function move_posts($post_ids, $topic_id, $auto_sync = true)
- {
- global $db;
- if (!is_array($post_ids))
- {
- $post_ids = array($post_ids);
- }
- $forum_ids = array();
- $topic_ids = array($topic_id);
- $sql = 'SELECT DISTINCT topic_id, forum_id
- FROM ' . POSTS_TABLE . '
- WHERE ' . $db->sql_in_set('post_id', $post_ids);
- $result = $db->sql_query($sql);
- while ($row = $db->sql_fetchrow($result))
- {
- $forum_ids[] = $row['forum_id'];
- $topic_ids[] = $row['topic_id'];
- }
- $db->sql_freeresult($result);
- $sql = 'SELECT forum_id
- FROM ' . TOPICS_TABLE . '
- WHERE topic_id = ' . $topic_id;
- $result = $db->sql_query($sql);
- $forum_row = $db->sql_fetchrow($result);
- $db->sql_freeresult($result);
- if (!$forum_row)
- {
- trigger_error('NO_TOPIC');
- }
- $sql = 'UPDATE ' . POSTS_TABLE . '
- SET forum_id = ' . $forum_row['forum_id'] . ", topic_id = $topic_id
- WHERE " . $db->sql_in_set('post_id', $post_ids);
- $db->sql_query($sql);
- $sql = 'UPDATE ' . ATTACHMENTS_TABLE . "
- SET topic_id = $topic_id, in_message = 0
- WHERE " . $db->sql_in_set('post_msg_id', $post_ids);
- $db->sql_query($sql);
- if ($auto_sync)
- {
- $forum_ids[] = $forum_row['forum_id'];
- sync('topic_reported', 'topic_id', $topic_ids);
- sync('topic_attachment', 'topic_id', $topic_ids);
- sync('topic', 'topic_id', $topic_ids, true);
- sync('forum', 'forum_id', $forum_ids, true, true);
- }
- // Update posted information
- update_posted_info($topic_ids);
- }
- /**
- * Remove topic(s)
- */
- function delete_topics($where_type, $where_ids, $auto_sync = true, $post_count_sync = true, $call_delete_posts = true)
- {
- global $db, $config;
- $approved_topics = 0;
- $forum_ids = $topic_ids = array();
- if ($where_type === 'range')
- {
- $where_clause = $where_ids;
- }
- else
- {
- $where_ids = (is_array($where_ids)) ? array_unique($where_ids) : array($where_ids);
- if (!sizeof($where_ids))
- {
- return array('topics' => 0, 'posts' => 0);
- }
- $where_clause = $db->sql_in_set($where_type, $where_ids);
- }
- // Making sure that delete_posts does not call delete_topics again...
- $return = array(
- 'posts' => ($call_delete_posts) ? delete_posts($where_type, $where_ids, false, true, $post_count_sync, false) : 0,
- );
- $sql = 'SELECT topic_id, forum_id, topic_approved, topic_moved_id
- FROM ' . TOPICS_TABLE . '
- WHERE ' . $where_clause;
- $result = $db->sql_query($sql);
- while ($row = $db->sql_fetchrow($result))
- {
- $forum_ids[] = $row['forum_id'];
- $topic_ids[] = $row['topic_id'];
- if ($row['topic_approved'] && !$row['topic_moved_id'])
- {
- $approved_topics++;
- }
- }
- $db->sql_freeresult($result);
- $return['topics'] = sizeof($topic_ids);
- if (!sizeof($topic_ids))
- {
- return $return;
- }
- $db->sql_transaction('begin');
- $table_ary = array(TOPICS_TRACK_TABLE, TOPICS_POSTED_TABLE, POLL_VOTES_TABLE, POLL_OPTIONS_TABLE, TOPICS_WATCH_TABLE, TOPICS_TABLE);
- foreach ($table_ary as $table)
- {
- $sql = "DELETE FROM $table
- WHERE " . $db->sql_in_set('topic_id', $topic_ids);
- $db->sql_query($sql);
- }
- unset($table_ary);
- $moved_topic_ids = array();
- // update the other forums
- $sql = 'SELECT topic_id, forum_id
- FROM ' . TOPICS_TABLE . '
- WHERE ' . $db->sql_in_set('topic_moved_id', $topic_ids);
- $result = $db->sql_query($sql);
- while ($row = $db->sql_fetchrow($result))
- {
- $forum_ids[] = $row['forum_id'];
- $moved_topic_ids[] = $row['topic_id'];
- }
- $db->sql_freeresult($result);
- if (sizeof($moved_topic_ids))
- {
- $sql = 'DELETE FROM ' . TOPICS_TABLE . '
- WHERE ' . $db->sql_in_set('topic_id', $moved_topic_ids);
- $db->sql_query($sql);
- }
- $db->sql_transaction('commit');
- if ($auto_sync)
- {
- sync('forum', 'forum_id', array_unique($forum_ids), true, true);
- sync('topic_reported', $where_type, $where_ids);
- }
- if ($approved_topics)
- {
- set_config_count('num_topics', $approved_topics * (-1), true);
- }
- return $return;
- }
- /**
- * Remove post(s)
- */
- function delete_posts($where_type, $where_ids, $auto_sync = true, $posted_sync = true, $post_count_sync = true, $call_delete_topics = true)
- {
- global $db, $config, $phpbb_root_path, $phpEx;
- if ($where_type === 'range')
- {
- $where_clause = $where_ids;
- }
- else
- {
- if (is_array($where_ids))
- {
- $where_ids = array_unique($where_ids);
- }
- else
- {
- $where_ids = array($where_ids);
- }
- if (!sizeof($where_ids))
- {
- return false;
- }
- $where_ids = array_map('intval', $where_ids);
- /* Possible code for splitting post deletion
- if (sizeof($where_ids) >= 1001)
- {
- // Split into chunks of 1000
- $chunks = array_chunk($where_ids, 1000);
- foreach ($chunks as $_where_ids)
- {
- delete_posts($where_type, $_where_ids, $auto_sync, $posted_sync, $post_count_sync, $call_delete_topics);
- }
- return;
- }*/
- $where_clause = $db->sql_in_set($where_type, $where_ids);
- }
- $approved_posts = 0;
- $post_ids = $topic_ids = $forum_ids = $post_counts = $remove_topics = array();
- $sql = 'SELECT post_id, poster_id, post_approved, post_postcount, topic_id, forum_id
- FROM ' . POSTS_TABLE . '
- WHERE ' . $where_clause;
- $result = $db->sql_query($sql);
- while ($row = $db->sql_fetchrow($result))
- {
- $post_ids[] = (int) $row['post_id'];
- $poster_ids[] = (int) $row['poster_id'];
- $topic_ids[] = (int) $row['topic_id'];
- $forum_ids[] = (int) $row['forum_id'];
- if ($row['post_postcount'] && $post_count_sync && $row['post_approved'])
- {
- $post_counts[$row['poster_id']] = (!empty($post_counts[$row['poster_id']])) ? $post_counts[$row['poster_id']] + 1 : 1;
- }
- if ($row['post_approved'])
- {
- $approved_posts++;
- }
- }
- $db->sql_freeresult($result);
- if (!sizeof($post_ids))
- {
- return false;
- }
- $db->sql_transaction('begin');
- $table_ary = array(POSTS_TABLE, REPORTS_TABLE);
- foreach ($table_ary as $table)
- {
- $sql = "DELETE FROM $table
- WHERE " . $db->sql_in_set('post_id', $post_ids);
- $db->sql_query($sql);
- }
- unset($table_ary);
- // Adjust users post counts
- if (sizeof($post_counts) && $post_count_sync)
- {
- foreach ($post_counts as $poster_id => $substract)
- {
- $sql = 'UPDATE ' . USERS_TABLE . '
- SET user_posts = 0
- WHERE user_id = ' . $poster_id . '
- AND user_posts < ' . $substract;
- $db->sql_query($sql);
- $sql = 'UPDATE ' . USERS_TABLE . '
- SET user_posts = user_posts - ' . $substract . '
- WHERE user_id = ' . $poster_id . '
- AND user_posts >= ' . $substract;
- $db->sql_query($sql);
- }
- }
- // Remove topics now having no posts?
- if (sizeof($topic_ids))
- {
- $sql = 'SELECT topic_id
- FROM ' . POSTS_TABLE . '
- WHERE ' . $db->sql_in_set('topic_id', $topic_ids) . '
- GROUP BY topic_id';
- $result = $db->sql_query($sql);
- while ($row = $db->sql_fetchrow($result))
- {
- $remove_topics[] = $row['topic_id'];
- }
- $db->sql_freeresult($result);
- // Actually, those not within remove_topics should be removed. ;)
- $remove_topics = array_diff($topic_ids, $remove_topics);
- }
- // Remove the message from the search index
- $search_type = basename($config['search_type']);
- if (!file_exists($phpbb_root_path . 'includes/search/' . $search_type . '.' . $phpEx))
- {
- trigger_error('NO_SUCH_SEARCH_MODULE');
- }
- include_once("{$phpbb_root_path}includes/search/$search_type.$phpEx");
- $error = false;
- $search = new $search_type($error);
- if ($error)
- {
- trigger_error($error);
- }
- $search->index_remove($post_ids, $poster_ids, $forum_ids);
- delete_attachments('post', $post_ids, false);
- $db->sql_transaction('commit');
- // Resync topics_posted table
- if ($posted_sync)
- {
- update_posted_info($topic_ids);
- }
- if ($auto_sync)
- {
- sync('topic_reported', 'topic_id', $topic_ids);
- sync('topic', 'topic_id', $topic_ids, true);
- sync('forum', 'forum_id', $forum_ids, true, true);
- }
- if ($approved_posts)
- {
- set_config_count('num_posts', $approved_posts * (-1), true);
- }
- // We actually remove topics now to not be inconsistent (the delete_topics function calls this function too)
- if (sizeof($remove_topics) && $call_delete_topics)
- {
- delete_topics('topic_id', $remove_topics, $auto_sync, $post_count_sync, false);
- }
- return sizeof($post_ids);
- }
- /**
- * Delete Attachments
- *
- * @param string $mode can be: post|message|topic|attach|user
- * @param mixed $ids can be: post_ids, message_ids, topic_ids, attach_ids, user_ids
- * @param bool $resync set this to false if you are deleting posts or topics
- */
- function delete_attachments($mode, $ids, $resync = true)
- {
- global $db, $config;
- // 0 is as bad as an empty array
- if (empty($ids))
- {
- return false;
- }
- if (is_array($ids))
- {
- $ids = array_unique($ids);
- $ids = array_map('intval', $ids);
- }
- else
- {
- $ids = array((int) $ids);
- }
- $sql_where = '';
- switch ($mode)
- {
- case 'post':
- case 'message':
- $sql_id = 'post_msg_id';
- $sql_where = ' AND in_message = ' . ($mode == 'message' ? 1 : 0);
- break;
- case 'topic':
- $sql_id = 'topic_id';
- break;
- case 'user':
- $sql_id = 'poster_id';
- break;
- case 'attach':
- default:
- $sql_id = 'attach_id';
- $mode = 'attach';
- break;
- }
- $post_ids = $message_ids = $topic_ids = $physical = array();
- // Collect post and topic ids for later use if we need to touch remaining entries (if resync is enabled)
- $sql = 'SELECT post_msg_id, topic_id, in_message, physical_filename, thumbnail, filesize, is_orphan
- FROM ' . ATTACHMENTS_TABLE . '
- WHERE ' . $db->sql_in_set($sql_id, $ids);
- $sql .= $sql_where;
- $result = $db->sql_query($sql);
- while ($row = $db->sql_fetchrow($result))
- {
- // We only need to store post/message/topic ids if resync is enabled and the file is not orphaned
- if ($resync && !$row['is_orphan'])
- {
- if (!$row['in_message'])
- {
- $post_ids[] = $row['post_msg_id'];
- $topic_ids[] = $row['topic_id'];
- }
- else
- {
- $message_ids[] = $row['post_msg_id'];
- }
- }
- $physical[] = array('filename' => $row['physical_filename'], 'thumbnail' => $row['thumbnail'], 'filesize' => $row['filesize'], 'is_orphan' => $row['is_orphan']);
- }
- $db->sql_freeresult($result);
- // Delete attachments
- $sql = 'DELETE FROM ' . ATTACHMENTS_TABLE . '
- WHERE ' . $db->sql_in_set($sql_id, $ids);
- $sql .= $sql_where;
- $db->sql_query($sql);
- $num_deleted = $db->sql_affectedrows();
- if (!$num_deleted)
- {
- return 0;
- }
- // Delete attachments from filesystem
- $space_removed = $files_removed = 0;
- foreach ($physical as $file_ary)
- {
- if (phpbb_unlink($file_ary['filename'], 'file', true) && !$file_ary['is_orphan'])
- {
- // Only non-orphaned files count to the file size
- $space_removed += $file_ary['filesize'];
- $files_removed++;
- }
- if ($file_ary['thumbnail'])
- {
- phpbb_unlink($file_ary['filename'], 'thumbnail', true);
- }
- }
- if ($space_removed || $files_removed)
- {
- set_config_count('upload_dir_size', $space_removed * (-1), true);
- set_config_count('num_files', $files_removed * (-1), true);
- }
- // If we do not resync, we do not need to adjust any message, post, topic or user entries
- if (!$resync)
- {
- return $num_deleted;
- }
- // No more use for the original ids
- unset($ids);
- // Now, we need to resync posts, messages, topics. We go through every one of them
- $post_ids = array_unique($post_ids);
- $message_ids = array_unique($message_ids);
- $topic_ids = array_unique($topic_ids);
- // Update post indicators for posts now no longer having attachments
- if (sizeof($post_ids))
- {
- // Just check which posts are still having an assigned attachment not orphaned by querying the attachments table
- $sql = 'SELECT post_msg_id
- FROM ' . ATTACHMENTS_TABLE . '
- WHERE ' . $db->sql_in_set('post_msg_id', $post_ids) . '
- AND in_message = 0
- AND is_orphan = 0';
- $result = $db->sql_query($sql);
- $remaining_ids = array();
- while ($row = $db->sql_fetchrow($result))
- {
- $remaining_ids[] = $row['post_msg_id'];
- }
- $db->sql_freeresult($result);
- // Now only unset those ids remaining
- $post_ids = array_diff($post_ids, $remaining_ids);
- if (sizeof($post_ids))
- {
- $sql = 'UPDATE ' . POSTS_TABLE . '
- SET post_attachment = 0
- WHERE ' . $db->sql_in_set('post_id', $post_ids);
- $db->sql_query($sql);
- }
- }
- // Update message table if messages are affected
- if (sizeof($message_ids))
- {
- // Just check which messages are still having an assigned attachment not orphaned by querying the attachments table
- $sql = 'SELECT post_msg_id
- FROM ' . ATTACHMENTS_TABLE . '
- WHERE ' . $db->sql_in_set('post_msg_id', $message_ids) . '
- AND in_message = 1
- AND is_orphan = 0';
- $result = $db->sql_query($sql);
- $remaining_ids = array();
- while ($row = $db->sql_fetchrow($result))
- {
- $remaining_ids[] = $row['post_msg_id'];
- }
- $db->sql_freeresult($result);
- // Now only unset those ids remaining
- $message_ids = array_diff($message_ids, $remaining_ids);
- if (sizeof($message_ids))
- {
- $sql = 'UPDATE ' . PRIVMSGS_TABLE . '
- SET message_attachment = 0
- WHERE ' . $db->sql_in_set('msg_id', $message_ids);
- $db->sql_query($sql);
- }
- }
- // Now update the topics. This is a bit trickier, because there could be posts still having attachments within the topic
- if (sizeof($topic_ids))
- {
- // Just check which topics are still having an assigned attachment not orphaned by querying the attachments table (much less entries expected)
- $sql = 'SELECT topic_id
- FROM ' . ATTACHMENTS_TABLE . '
- WHERE ' . $db->sql_in_set('topic_id', $topic_ids) . '
- AND is_orphan = 0';
- $result = $db->sql_query($sql);
- $remaining_ids = array();
- while ($row = $db->sql_fetchrow($result))
- {
- $remaining_ids[] = $row['topic_id'];
- }
- $db->sql_freeresult($result);
- // Now only unset those ids remaining
- $topic_ids = array_diff($topic_ids, $remaining_ids);
- if (sizeof($topic_ids))
- {
- $sql = 'UPDATE ' . TOPICS_TABLE . '
- SET topic_attachment = 0
- WHERE ' . $db->sql_in_set('topic_id', $topic_ids);
- $db->sql_query($sql);
- }
- }
- return $num_deleted;
- }
- /**
- * Remove topic shadows
- */
- function delete_topic_shadows($max_age, $forum_id = '', $auto_sync = true)
- {
- $where = (is_array($forum_id)) ? 'AND ' . $db->sql_in_set('t.forum_id', array_map('intval', $forum_id)) : (($forum_id) ? 'AND t.forum_id = ' . (int) $forum_id : '');
- switch ($db->sql_layer)
- {
- case 'mysql4':
- case 'mysqli':
- $sql = 'DELETE t.*
- FROM ' . TOPICS_TABLE . ' t, ' . TOPICS_TABLE . ' t2
- WHERE t.topic_moved_id = t2.topic_id
- AND t.topic_time < ' . (time() - $max_age)
- . $where;
- $db->sql_query($sql);
- break;
- default:
- $sql = 'SELECT t.topic_id
- FROM ' . TOPICS_TABLE . ' t, ' . TOPICS_TABLE . ' t2
- WHERE t.topic_moved_id = t2.topic_id
- AND t.topic_time < ' . (time() - $max_age)
- . $where;
- $result = $db->sql_query($sql);
- $topic_ids = array();
- while ($row = $db->sql_fetchrow($result))
- {
- $topic_ids[] = $row['topic_id'];
- }
- $db->sql_freeresult($result);
- if (sizeof($topic_ids))
- {
- $sql = 'DELETE FROM ' . TOPICS_TABLE . '
- WHERE ' . $db->sql_in_set('topic_id', $topic_ids);
- $db->sql_query($sql);
- }
- break;
- }
- if ($auto_sync)
- {
- $where_type = ($forum_id) ? 'forum_id' : '';
- sync('forum', $where_type, $forum_id, true, true);
- }
- }
- /**
- * Update/Sync posted information for topics
- */
- function update_posted_info(&$topic_ids)
- {
- global $db, $config;
- if (empty($topic_ids) || !$config['load_db_track'])
- {
- return;
- }
- // First of all, let us remove any posted information for these topics
- $sql = 'DELETE FROM ' . TOPICS_POSTED_TABLE . '
- WHERE ' . $db->sql_in_set('topic_id', $topic_ids);
- $db->sql_query($sql);
- // Now, let us collect the user/topic combos for rebuilding the information
- $sql = 'SELECT poster_id, topic_id
- FROM ' . POSTS_TABLE . '
- WHERE ' . $db->sql_in_set('topic_id', $topic_ids) . '
- AND poster_id <> ' . ANONYMOUS . '
- GROUP BY poster_id, topic_id';
- $result = $db->sql_query($sql);
- $posted = array();
- while ($row = $db->sql_fetchrow($result))
- {
- // Add as key to make them unique (grouping by) and circumvent empty keys on array_unique
- $posted[$row['poster_id']][] = $row['topic_id'];
- }
- $db->sql_freeresult($result);
- // Now add the information...
- $sql_ary = array();
- foreach ($posted as $user_id => $topic_row)
- {
- foreach ($topic_row as $topic_id)
- {
- $sql_ary[] = array(
- 'user_id' => (int) $user_id,
- 'topic_id' => (int) $topic_id,
- 'topic_posted' => 1,
- );
- }
- }
- unset($posted);
- $db->sql_multi_insert(TOPICS_POSTED_TABLE, $sql_ary);
- }
- /**
- * Delete attached file
- */
- function phpbb_unlink($filename, $mode = 'file', $entry_removed = false)
- {
- global $db, $phpbb_root_path, $config;
- // Because of copying topics or modifications a physical filename could be assigned more than once. If so, do not remove the file itself.
- $sql = 'SELECT COUNT(attach_id) AS num_entries
- FROM ' . ATTACHMENTS_TABLE . "
- WHERE physical_filename = '" . $db->sql_escape(utf8_basename($filename)) . "'";
- $result = $db->sql_query($sql);
- $num_entries = (int) $db->sql_fetchfield('num_entries');
- $db->sql_freeresult($result);
- // Do not remove file if at least one additional entry with the same name exist.
- if (($entry_removed && $num_entries > 0) || (!$entry_removed && $num_entries > 1))
- {
- return false;
- }
- $filename = ($mode == 'thumbnail') ? 'thumb_' . utf8_basename($filename) : utf8_basename($filename);
- return @unlink($phpbb_root_path . $config['upload_path'] . '/' . $filename);
- }
- /**
- * All-encompasing sync function
- *
- * Exaples:
- * <code>
- * sync('topic', 'topic_id', 123); // resync topic #123
- * sync('topic', 'forum_id', array(2, 3)); // resync topics from forum #2 and #3
- * sync('topic'); // resync all topics
- * sync('topic', 'range', 'topic_id BETWEEN 1 AND 60'); // resync a range of topics/forums (only available for 'topic' and 'forum' modes)
- * </code>
- *
- * Modes:
- * - forum Resync complete forum
- * - topic Resync topics
- * - topic_moved Removes topic shadows that would be in the same forum as the topic they link to
- * - topic_approved Resyncs the topic_approved flag according to the status of the first post
- * - post_reported Resyncs the post_reported flag, relying on actual reports
- * - topic_reported Resyncs the topic_reported flag, relying on post_reported flags
- * - post_attachement Same as post_reported, but with attachment flags
- * - topic_attachement Same as topic_reported, but with attachment flags
- */
- function sync($mode, $where_type = '', $where_ids = '', $resync_parents = false, $sync_extra = false)
- {
- global $db;
- if (is_array($where_ids))
- {
- $where_ids = array_unique($where_ids);
- $where_ids = array_map('intval', $where_ids);
- }
- else if ($where_type != 'range')
- {
- $where_ids = ($where_ids) ? array((int) $where_ids) : array();
- }
- if ($mode == 'forum' || $mode == 'topic' || $mode == 'topic_approved' || $mode == 'topic_reported' || $mode == 'post_reported')
- {
- if (!$where_type)
- {
- $where_sql = '';
- $where_sql_and = 'WHERE';
- }
- else if ($where_type == 'range')
- {
- // Only check a range of topics/forums. For instance: 'topic_id BETWEEN 1 AND 60'
- $where_sql = 'WHERE (' . $mode[0] . ".$where_ids)";
- $where_sql_and = $where_sql . "\n\tAND";
- }
- else
- {
- // Do not sync the "global forum"
- $where_ids = array_diff($where_ids, array(0));
- if (!sizeof($where_ids))
- {
- // Empty array with IDs. This means that we don't have any work to do. Just return.
- return;
- }
- // Limit the topics/forums we are syncing, use specific topic/forum IDs.
- // $where_type contains the field for the where clause (forum_id, topic_id)
- $where_sql = 'WHERE ' . $db->sql_in_set($mode[0] . '.' . $where_type, $where_ids);
- $where_sql_and = $where_sql . "\n\tAND";
- }
- }
- else
- {
- if (!sizeof($where_ids))
- {
- return;
- }
- // $where_type contains the field for the where clause (forum_id, topic_id)
- $where_sql = 'WHERE ' . $db->sql_in_set($mode[0] . '.' . $where_type, $where_ids);
- $where_sql_and = $where_sql . "\n\tAND";
- }
- switch ($mode)
- {
- case 'topic_moved':
- $db->sql_transaction('begin');
- switch ($db->sql_layer)
- {
- case 'mysql4':
- case 'mysqli':
- $sql = 'DELETE FROM ' . TOPICS_TABLE . '
- USING ' . TOPICS_TABLE . ' t1, ' . TOPICS_TABLE . " t2
- WHERE t1.topic_moved_id = t2.topic_id
- AND t1.forum_id = t2.forum_id";
- $db->sql_query($sql);
- break;
- default:
- $sql = 'SELECT t1.topic_id
- FROM ' .TOPICS_TABLE . ' t1, ' . TOPICS_TABLE . " t2
- WHERE t1.topic_moved_id = t2.topic_id
- AND t1.forum_id = t2.forum_id";
- $result = $db->sql_query($sql);
- $topic_id_ary = array();
- while ($row = $db->sql_fetchrow($result))
- {
- $topic_id_ary[] = $row['topic_id'];
- }
- $db->sql_freeresult($result);
- if (!sizeof($topic_id_ary))
- {
- return;
- }
- $sql = 'DELETE FROM ' . TOPICS_TABLE . '
- WHERE ' . $db->sql_in_set('topic_id', $topic_id_ary);
- $db->sql_query($sql);
- break;
- }
- $db->sql_transaction('commit');
- break;
- case 'topic_approved':
- $db->sql_transaction('begin');
- switch ($db->sql_layer)
- {
- case 'mysql4':
- case 'mysqli':
- $sql = 'UPDATE ' . TOPICS_TABLE . ' t, ' . POSTS_TABLE . " p
- SET t.topic_approved = p.post_approved
- $where_sql_and t.topic_first_post_id = p.post_id";
- $db->sql_query($sql);
- break;
- default:
- $sql = 'SELECT t.topic_id, p.post_approved
- FROM ' . TOPICS_TABLE . ' t, ' . POSTS_TABLE . " p
- $where_sql_and p.post_id = t.topic_first_post_id
- AND p.post_approved <> t.topic_approved";
- $result = $db->sql_query($sql);
- $topic_ids = array();
- while ($row = $db->sql_fetchrow($result))
- {
- $topic_ids[] = $row['topic_id'];
- }
- $db->sql_freeresult($result);
- if (!sizeof($topic_ids))
- {
- return;
- }
- $sql = 'UPDATE ' . TOPICS_TABLE . '
- SET topic_approved = 1 - topic_approved
- WHERE ' . $db->sql_in_set('topic_id', $topic_ids);
- $db->sql_query($sql);
- break;
- }
- $db->sql_transaction('commit');
- break;
- case 'post_reported':
- $post_ids = $post_reported = array();
- $db->sql_transaction('begin');
- $sql = 'SELECT p.post_id, p.post_reported
- FROM ' . POSTS_TABLE . " p
- $where_sql
- GROUP BY p.post_id, p.post_reported";
- $result = $db->sql_query($sql);
- while ($row = $db->sql_fetchrow($result))
- {
- $post_ids[$row['post_id']] = $row['post_id'];
- if ($row['post_reported'])
- {
- $post_reported[$row['post_id']] = 1;
- }
- }
- $db->sql_freeresult($result);
- $sql = 'SELECT DISTINCT(post_id)
- FROM ' . REPORTS_TABLE . '
- WHERE ' . $db->sql_in_set('post_id', $post_ids) . '
- AND report_closed = 0';
- $result = $db->sql_query($sql);
- $post_ids = array();
- while ($row = $db->sql_fetchrow($result))
- {
- if (!isset($post_reported[$row['post_id']]))
- {
- $post_ids[] = $row['post_id'];
- }
- else
- {
- unset($post_reported[$row['post_id']]);
- }
- }
- $db->sql_freeresult($result);
- // $post_reported should be empty by now, if it's not it contains
- // posts that are falsely flagged as reported
- foreach ($post_reported as $post_id => $void)
- {
- $post_ids[] = $post_id;
- }
- if (sizeof($post_ids))
- {
- $sql = 'UPDATE ' . POSTS_TABLE . '
- SET post_reported = 1 - post_reported
- WHERE ' . $db->sql_in_set('post_id', $post_ids);
- $db->sql_query($sql);
- }
- $db->sql_transaction('commit');
- break;
- case 'topic_reported':
- if ($sync_extra)
- {
- sync('post_reported', $where_type, $where_ids);
- }
- $topic_ids = $topic_reported = array();
- $db->sql_transaction('begin');
- $sql = 'SELECT DISTINCT(t.topic_id)
- FROM ' . POSTS_TABLE . " t
- $where_sql_and t.post_reported = 1";
- $result = $db->sql_query($sql);
- while ($row = $db->sql_fetchrow($result))
- {
- $topic_reported[$row['topic_id']] = 1;
- }
- $db->sql_freeresult($result);
- $sql = 'SELECT t.topic_id, t.topic_reported
- FROM ' . TOPICS_TABLE . " t
- $where_sql";
- $result = $db->sql_query($sql);
- while ($row = $db->sql_fetchrow($result))
- {
- if ($row['topic_reported'] ^ isset($topic_reported[$row['topic_id']]))
- {
- $topic_ids[] = $row['topic_id'];
- }
- }
- $db->sql_freeresult($result);
- if (sizeof($topic_ids))
- {
- $sql = 'UPDATE ' . TOPICS_TABLE . '
- SET topic_reported = 1 - topic_reported
- WHERE ' . $db->sql_in_set('topic_id', $topic_ids);
- $db->sql_query($sql);
- }
- $db->sql_transaction('commit');
- break;
- case 'post_attachment':
- $post_ids = $post_attachment = array();
- $db->sql_transaction('begin');
- $sql = 'SELECT p.post_id, p.post_attachment
- FROM ' . POSTS_TABLE . " p
- $where_sql
- GROUP BY p.post_id, p.post_attachment";
- $result = $db->sql_query($sql);
- while ($row = $db->sql_fetchrow($result))
- {
- $post_ids[$row['post_id']] = $row['post_id'];
- if ($row['post_attachment'])
- {
- $post_attachment[$row['post_id']] = 1;
- }
- }
- $db->sql_freeresult($result);
- $sql = 'SELECT DISTINCT(post_msg_id)
- FROM ' . ATTACHMENTS_TABLE . '
- WHERE ' . $db->sql_in_set('post_msg_id', $post_ids) . '
- AND in_message = 0';
- $result = $db->sql_query($sql);
- $post_ids = array();
- while ($row = $db->sql_fetchrow($result))
- {
- if (!isset($post_attachment[$row['post_msg_id']]))
- {
- $post_ids[] = $row['post_msg_id'];
- }
- else
- {
- unset($post_attachment[$row['post_msg_id']]);
- }
- }
- $db->sql_freeresult($result);
- // $post_attachment should be empty by now, if it's not it contains
- // posts that are falsely flagged as having attachments
- foreach ($post_attachment as $post_id => $void)
- {
- $post_ids[] = $post_id;
- }
- if (sizeof($post_ids))
- {
- $sql = 'UPDATE ' . POSTS_TABLE . '
- SET post_attachment = 1 - post_attachment
- WHERE ' . $db->sql_in_set('post_id', $post_ids);
- $db->sql_query($sql);
- }
- $db->sql_transaction('commit');
- break;
- case 'topic_attachment':
- if ($sync_extra)
- {
- sync('post_attachment', $where_type, $where_ids);
- }
- $topic_ids = $topic_attachment = array();
- $db->sql_transaction('begin');
- $sql = 'SELECT DISTINCT(t.topic_id)
- FROM ' . POSTS_TABLE . " t
- $where_sql_and t.post_attachment = 1";
- $result = $db->sql_query($sql);
- while ($row = $db->sql_fetchrow($result))
- {
- $topic_attachment[$row['topic_id']] = 1;
- }
- $db->sql_freeresult($result);
- $sql = 'SELECT t.topic_id, t.topic_attachment
- FROM ' . TOPICS_TABLE . " t
- $where_sql";
- $result = $db->sql_query($sql);
- while ($row = $db->sql_fetchrow($result))
- {
- if ($row['topic_attachment'] ^ isset($topic_attachment[$row['topic_id']]))
- {
- $topic_ids[] = $row['topic_id'];
- }
- }
- $db->sql_freeresult($result);
- if (sizeof($topic_ids))
- {
- $sql = 'UPDATE ' . TOPICS_TABLE . '
- SET topic_attachment = 1 - topic_attachment
- WHERE ' . $db->sql_in_set('topic_id', $topic_ids);
- $db->sql_query($sql);
- }
- $db->sql_transaction('commit');
- break;
- case 'forum':
- $db->sql_transaction('begin');
- // 1: Get the list of all forums
- $sql = 'SELECT f.*
- FROM ' . FORUMS_TABLE . " f
- $where_sql";
- $result = $db->sql_query($sql);
- $forum_data = $forum_ids = $post_ids = $last_post_id = $post_info = array();
- while ($row = $db->sql_fetchrow($result))
- {
- if ($row['forum_type'] == FORUM_LINK)
- {
- continue;
- }
- $forum_id = (int) $row['forum_id'];
- $forum_ids[$forum_id] = $forum_id;
- $forum_data[$forum_id] = $row;
- if ($sync_extra)
- {
- $forum_data[$forum_id]['posts'] = 0;
- $forum_data[$forum_id]['topics'] = 0;
- $forum_data[$forum_id]['topics_real'] = 0;
- }
- $forum_data[$forum_id]['last_post_id'] = 0;
- $forum_data[$forum_id]['last_post_subject'] = '';
- $forum_data[$forum_id]['last_post_time'] = 0;
- $forum_data[$forum_id]['last_poster_id'] = 0;
- $forum_data[$forum_id]['last_poster_name'] = '';
- $forum_data[$forum_id]['last_poster_colour'] = '';
- }
- $db->sql_freeresult($result);
- if (!sizeof($forum_ids))
- {
- break;
- }
- $forum_ids = array_values($forum_ids);
- // 2: Get topic counts for each forum (optional)
- if ($sync_extra)
- {
- $sql = 'SELECT forum_id, topic_approved, COUNT(topic_id) AS forum_topics
- FROM ' . TOPICS_TABLE . '
- WHERE ' . $db->sql_in_set('forum_id', $forum_ids) . '
- GROUP BY forum_id, topic_approved';
- $result = $db->sql_query($sql);
- while ($row = $db->sql_fetchrow($result))
- {
- $forum_id = (int) $row['forum_id'];
- $forum_data[$forum_id]['topics_real'] += $row['forum_topics'];
- if ($row['topic_approved'])
- {
- $forum_data[$forum_id]['topics'] = $row['forum_topics'];
- }
- }
- $db->sql_freeresult($result);
- }
- // 3: Get post count for each forum (optional)
- if ($sync_extra)
- {
- if (sizeof($forum_ids) == 1)
- {
- $sql = 'SELECT SUM(t.topic_replies + 1) AS forum_posts
- FROM ' . TOPICS_TABLE . ' t
- WHERE ' . $db->sql_in_set('t.forum_id', $forum_ids) . '
- AND t.topic_approved = 1
- AND t.topic_status <> ' . ITEM_MOVED;
- }
- else
- {
- $sql = 'SELECT t.forum_id, SUM(t.topic_replies + 1) AS forum_posts
- FROM ' . TOPICS_TABLE . ' t
- WHERE ' . $db->sql_in_set('t.forum_id', $forum_ids) . '
- AND t.topic_approved = 1
- AND t.topic_status <> ' . ITEM_MOVED . '
- GROUP BY t.forum_id';
- }
- $result = $db->sql_query($sql);
- while ($row = $db->sql_fetchrow($result))
- {
- $forum_id = (sizeof($forum_ids) == 1) ? (int) $forum_ids[0] : (int) $row['forum_id'];
- $forum_data[$forum_id]['posts'] = (int) $row['forum_posts'];
- }
- $db->sql_freeresult($result);
- }
- // 4: Get last_post_id for each forum
- if (sizeof($forum_ids) == 1)
- {
- $sql = 'SELECT MAX(t.topic_last_post_id) as last_post_id
- FROM ' . TOPICS_TABLE . ' t
- WHERE ' . $db->sql_in_set('t.forum_id', $forum_ids) . '
- AND t.topic_approved = 1';
- }
- else
- {
- $sql = 'SELECT t.forum_id, MAX(t.topic_last_post_id) as last_post_id
- FROM ' . TOPICS_TABLE . ' t
- WHERE ' . $db->sql_in_set('t.forum_id', $forum_ids) . '
- AND t.topic_approved = 1
- GROUP BY t.forum_id';
- }
- $result = $db->sql_query($sql);
- while ($row = $db->sql_fetchrow($result))
- {
- $forum_id = (sizeof($forum_ids) == 1) ? (int) $forum_ids[0] : (int) $row['forum_id'];
- $forum_data[$forum_id]['last_post_id'] = (int) $row['last_post_id'];
- $post_ids[] = $row['last_post_id'];
- }
- $db->sql_freeresult($result);
- // 5: Retrieve last_post infos
- if (sizeof($post_ids))
- {
- $sql = 'SELECT p.post_id, p.poster_id, p.post_subject, p.post_time, p.post_username, u.username, u.user_colour
- FROM ' . POSTS_TABLE . ' p, ' . USERS_TABLE . ' u
- WHERE ' . $db->sql_in_set('p.post_id', $post_ids) . '
- AND p.poster_id = u.user_id';
- $result = $db->sql_query($sql);
- while ($row = $db->sql_fetchrow($result))
- {
- $post_info[$row['post_id']] = $row;
- }
- $db->sql_freeresult($result);
- foreach ($forum_data as $forum_id => $data)
- {
- if ($data['last_post_id'])
- {
- if (isset($post_info[$data['last_post_id']]))
- {
- $forum_data[$forum_id]['last_post_subject'] = $post_info[$data['last_post_id']]['post_subject'];
- $forum_data[$forum_id]['last_post_time'] = $post_info[$data['last_post_id']]['post_time'];
- $forum_data[$forum_id]['last_poster_id'] = $post_info[$data['last_post_id']]['poster_id'];
- $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'];
- $forum_data[$forum_id]['last_poster_colour'] = $post_info[$data['last_post_id']]['user_colour'];
- }
- else
- {
- // For some reason we did not find the post in the db
- $forum_data[$forum_id]['last_post_id'] = 0;
- $forum_data[$forum_id]['last_post_subject'] = '';
- $forum_data[$forum_id]['last_post_time'] = 0;
- $forum_data[$forum_id]['last_poster_id'] = 0;
- $forum_data[$forum_id]['last_poster_name'] = '';
- $forum_data[$forum_id]['last_poster_colour'] = '';
- }
- }
- }
- unset($post_info);
- }
- // 6: Now do that thing
- $fieldnames = array('last_post_id', 'last_post_subject', 'last_post_time', 'last_poster_id', 'last_poster_name', 'last_poster_colour');
- if ($sync_extra)
- {
- array_push($fieldnames, 'posts', 'topics', 'topics_real');
- }
- foreach ($forum_data as $forum_id => $row)
- {
- $sql_ary = array();
- foreach ($fieldnames as $fieldname)
- {
- if ($row['forum_' . $fieldname] != $row[$fieldname])
- {
- if (preg_match('#(name|colour|subject)$#', $fieldname))
- {
- $sql_ary['forum_' . $fieldname] = (string) $row[$fieldname];
- }
- else
- {
- $sql_ary['forum_' . $fieldname] = (int) $row[$fieldname];
- }
- }
- }
- if (sizeof($sql_ary))
- {
- $sql = 'UPDATE ' . FORUMS_TABLE . '
- SET ' . $db->sql_build_array('UPDATE', $sql_ary) . '
- WHERE forum_id = ' . $forum_id;
- $db->sql_query($sql);
- }
- }
- $db->sql_transaction('commit');
- break;
- case 'topic':
- $topic_data = $post_ids = $approved_unapproved_ids = $resync_forums = $delete_topics = $delete_posts = $moved_topics = array();
- $db->sql_transaction('begin');
- $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
- FROM ' . TOPICS_TABLE . " t
- $where_sql";
- $result = $db->sql_query($sql);
- while ($row = $db->sql_fetchrow($result))
- {
- if ($row['topic_moved_id'])
- {
- $moved_topics[] = $row['topic_id'];
- continue;
- }
- $topic_id = (int) $row['topic_id'];
- $topic_data[$topic_id] = $row;
- $topic_data[$topic_id]['replies_real'] = -1;
- $topic_data[$topic_id]['replies'] = 0;
- $topic_data[$topic_id]['first_post_id'] = 0;
- $topic_data[$topic_id]['last_post_id'] = 0;
- unset($topic_data[$topic_id]['topic_id']);
- // This array holds all topic_ids
- $delete_topics[$topic_id] = '';
- …
Large files files are truncated, but you can click here to view the full file