<?php
    $_back = isset($_GET['b']) ? str_replace('@', '&', trim(strval($_GET['b']))) : '';
    $_id = isset($_GET['i']) ? intval($_GET['i']) : 0;
    $_new_forum = isset($_GET['x']) ? intval($_GET['x']) : 0;
    
    $community = mysqli_query_logged("SELECT * FROM community_threads, community_forums, community_sections, community WHERE community_threads.thread_id = " . sq($_id) . " AND community_threads.forum_id = community_forums.forum_id AND community_forums.section_id = community_sections.section_id AND community_sections.community_id = community.community_id");
    if (!$community_row = mysqli_fetch_assoc($community))    
    {
        make_cookie('notice', 'This thread does not exist.');
        header('Location: ./?s=communities');
        die;        
    }
    
    require_once('include/functions/community_banned.php');
    community_banned($community_row['community_id']);
    require_once('include/functions/community_permissions.php');        
    community_permissions($community_row['community_id'], $community_row['section_id'], $community_row['forum_id']);
    
    if (!$GLOBALS['auth']['community']['thread_move'])
    {
        make_cookie('notice', 'You do not have access to move messages from this forum.');
        header('Location: ./?s=community_forum&i=' . $community_forums_row['forum_id']);
        die;
    }
    
    $community_forums = mysqli_query_logged("SELECT * FROM community_forums, community_sections, community WHERE community_forums.forum_id = " . sq($_new_forum) . " AND community_forums.forum_id AND community_forums.section_id = community_sections.section_id AND community_sections.community_id = '" . $community_row['community_id'] . "' AND community_sections.community_id = community.community_id");
    if (!$community_forums_row = mysqli_fetch_assoc($community_forums))    
    {
        make_cookie('notice', 'This thread does not exist.');
        header('Location: ./?s=communities');
        die;        
    }
    
    require_once('include/functions/community_permissions.php');        
    community_permissions($community_forums_row['community_id'], $community_forums_row['section_id'], $community_forums_row['forum_id']);

    if (!$GLOBALS['auth']['community']['thread_move'])
    {
        make_cookie('notice', 'You do not have access to move messages to this forum.');
        header('Location: ./?s=community_forum&i=' . $community_forums_row['forum_id']);
        die;
    }

    mysqli_query_logged("UPDATE community_threads SET forum_id = '" . $community_forums_row['forum_id'] . "' WHERE thread_id = '" . $community_row['thread_id'] . "'");
    $array = array($community_row['forum_id'], $community_forums_row['forum_id']);
    foreach ($array AS $forum_id)
    {
        $community_threads = mysqli_query_logged("SELECT SUM(thread_messages) AS sum FROM community_threads WHERE forum_id = '" . $forum_id . "'");
        if ($community_threads_row = mysqli_fetch_assoc($community_threads))
        {
            mysqli_query_logged("UPDATE community_forums SET forum_messages = '" . $community_threads_row['sum'] . "' WHERE forum_id = '" . $forum_id . "'");
        }
        $community_threads = mysqli_query_logged("SELECT COUNT(*) AS count FROM community_threads WHERE forum_id = '" . $forum_id . "'");
        if ($community_threads_row = mysqli_fetch_assoc($community_threads))
        {
            mysqli_query_logged("UPDATE community_forums SET forum_threads = '" . $community_threads_row['count'] . "' WHERE forum_id = '" . $forum_id . "'");
        }
    }

    header('Location: ./?' . ($_back ? $_back : 's=community_forum&i=' . $community_forums_row['forum_id']));
?>