<?php
    require_once('include/functions/ip_encode.php');

    $_id = isset($_GET['i']) ? intval($_GET['i']) : 0;
    $_title = isset($_POST['title']) ? trim(strval($_POST['title'])) : '';
    $_body  = isset($_POST['body']) ? trim(strval($_POST['body'])) : '';

    $community = mysqli_query_logged("SELECT * FROM community_forums, community_sections, community WHERE community_forums.forum_id = " . sq($_id) . " AND community_forums.forum_deleted = '0' AND community_forums.section_id = community_sections.section_id AND community_sections.section_deleted = '0' AND community_sections.community_id = community.community_id");
    if (!$community_row = mysqli_fetch_assoc($community))
    {
        make_cookie('notice', 'Sorry, that forum 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 ($community_row['forum_locked'] != 0 && !$GLOBALS['auth']['community']['locked_post'])
    {
        show_error_page('Sorry, that forum is locked. No Posting allowed.');
        die;
    }

    if ($community_row['forum_automated'] != 0 && !$GLOBALS['auth']['community']['automate_post'])
    {
        show_error_page('Sorry, that forum is locked. No Posting allowed.');
        die;
    }
    
    require_once('include/functions/community_thread_valid.php');
    $error = community_thread_valid($_title, $_body);
    if ($error)    
    {
        show_error_page($error);
        die;        
    }    

    $mood = 0;    
    $members_moods = mysqli_query_logged("SELECT id FROM members_moods WHERE user_id = '" . $GLOBALS['auth']['id'] . "' ORDER BY id DESC LIMIT 1");
    if ($members_moods_row = mysqli_fetch_array($members_moods))
    {
        $mood = $members_moods_row['id'];
    }

    mysqli_query_logged("INSERT INTO community_messages SET thread_id = '0', message_user_id = '" . $GLOBALS['auth']['id'] . "', message_posted_on = NOW(), message_ip = '" . encode_ip($_SERVER['REMOTE_ADDR']) . "', message_mood = '" . $mood . "'");
    $message_id = mysqli_insert_id($GLOBALS['mysqli']);
    mysqli_query_logged("INSERT INTO community_messages_bodies SET message_id = '" . $message_id . "', message_body = " . sq($_body));
    mysqli_query_logged("INSERT INTO community_threads SET forum_id = '" . $community_row['forum_id'] . "', thread_user_id = '" . $GLOBALS['auth']['id'] . "', thread_posted_on = NOW(), thread_last_user_id = '" . $GLOBALS['auth']['id'] . "', thread_last_posted_on = NOW(), thread_title = " . sq($_title) . ", thread_messages = '1', thread_first_message_id = '" . $message_id . "', thread_last_message_id = '" . $message_id . "'");    
    $thread_id = mysqli_insert_id($GLOBALS['mysqli']);
    mysqli_query_logged("UPDATE community_messages SET thread_id = '" . $thread_id . "' WHERE message_id = '" . $message_id . "'");

    mysqli_query_logged("REPLACE INTO community_threads_pointers SET thread_id = '" . $thread_id . "', user_id = '" . $GLOBALS['auth']['id'] . "', message_id = '" . $message_id . "'");

    $community_threads = mysqli_query_logged("SELECT SUM(thread_messages) AS sum FROM community_threads WHERE forum_id = '" . $community_row['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 = '" . $community_row['forum_id'] . "'");
    }
    $community_threads = mysqli_query_logged("SELECT COUNT(*) AS count FROM community_threads WHERE forum_id = '" . $community_row['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 = '" . $community_row['forum_id'] . "'");
    }

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