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

    $_back = isset($_GET['b']) ? str_replace('@', '&', trim(strval($_GET['b']))) : '';
    $_id = isset($_GET['i']) ? trim($_GET['i']) : '';
    $_body  = isset($_POST['body']) ? trim($_POST['body']) : '';
    
    $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.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 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 ($community_row['thread_locked'] && $community_row['forum_locked'] && !$GLOBALS['auth']['community']['thread_lock_post'])
    {
        show_error_page('Sorry, this thread is locked. No Posting allowed.');
        die;            
    }
    
    $community_messages = mysqli_query_logged("SELECT message_user_id FROM community_messages WHERE thread_id = '" . $_id . "' ORDER BY message_id DESC LIMIT 1");
    if ($community_messages_row = mysqli_fetch_array($community_messages))
    {
        if ($GLOBALS['auth']['id'] == $community_messages_row['message_user_id'])
        {
            show_error_page('Sorry, you can\'t reply twice in a row.');
            die;            
        }                
    }
    
    require_once('include/functions/community_reply_valid.php');
    $error = community_reply_valid($_body);
    if ($error)    
    {
        show_error_page($error);
        die;        
    }    
    
    $last_mood = mysqli_query_logged("SELECT id FROM members_moods WHERE user_id = '" . $GLOBALS['auth']['id'] . "' ORDER BY id DESC LIMIT 1");
    if ($last_mood = mysqli_fetch_array($last_mood))
    {
        $mood = $last_mood['id'];
    }
    else
    {
        $mood = 0;
    }
    
    mysqli_query_logged("INSERT INTO community_messages SET thread_id = '" . $community_row['thread_id'] . "', message_user_id = '" . $GLOBALS['auth']['id'] . "', message_posted_on = NOW(), message_ip = '" . encode_ip($_SERVER['REMOTE_ADDR']) . "', message_mood = '" . $mood . "'");
    $id = mysqli_insert_id($GLOBALS['mysqli']);
    mysqli_query_logged("INSERT INTO community_messages_bodies SET message_id = '" . $id . "', message_body = " . sq($_body));
    mysqli_query_logged("REPLACE INTO community_threads_pointers SET thread_id = '" . $community_row['thread_id'] . "', user_id = '" . $GLOBALS['auth']['id'] . "', message_id = '" . $id . "'");
    
    $message = 0;
    $community_messages = mysqli_query_logged("SELECT COUNT(*) AS count FROM community_messages WHERE thread_id = '" . $community_row['thread_id'] . "'");
    if ($community_messages_row = mysqli_fetch_assoc($community_messages))
    {
        $messages = $community_messages_row['count'];
    }
    mysqli_query_logged("UPDATE community_threads SET thread_last_user_id = '" . $GLOBALS['auth']['id']. "', thread_last_posted_on = NOW(), thread_messages = '" . $messages . "', thread_last_message_id = '" . $id . "' WHERE thread_id = '" . $community_row['thread_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'] . "'");
    }
    
    header('Location: ./?' . ($_back ? $_back : 's=community_forum&i=' . $community_row['forum_id']));
?>