<?
    
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 mysql_query("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 mysql_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 mysql_query("SELECT message_user_id FROM community_messages WHERE thread_id = '" $_id "' ORDER BY message_id DESC LIMIT 1");
    if (
$community_messages_row mysql_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 mysql_query("SELECT id FROM members_moods WHERE user_id = '" $GLOBALS['auth']['id'] . "' ORDER BY id DESC LIMIT 1");
    if (
$last_mood mysql_fetch_array($last_mood))
    {
        
$mood $last_mood['id'];
    }
    else
    {
        
$mood 0;
    }
    
    
mysql_query("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 mysql_insert_id();
    
mysql_query("INSERT INTO community_messages_bodies SET message_id = '" $id "', message_body = " sq($_body));
    
mysql_query("REPLACE INTO community_threads_pointers SET thread_id = '" $community_row['thread_id'] . "', user_id = '" $GLOBALS['auth']['id'] . "', message_id = '" $id "'");
    
    
$message 0;
    
$community_messages mysql_query("SELECT COUNT(*) AS count FROM community_messages WHERE thread_id = '" $community_row['thread_id'] . "'");
    if (
$community_messages_row mysql_fetch_assoc($community_messages))
    {
        
$messages $community_messages_row['count'];
    }
    
mysql_query("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 mysql_query("SELECT SUM(thread_messages) AS sum FROM community_threads WHERE forum_id = '" $community_row['forum_id'] . "'");
    if (
$community_threads_row mysql_fetch_assoc($community_threads))
    {
        
mysql_query("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']));
?>