<?php
    $_back = isset($_GET['b']) ? str_replace('@', '&', trim(strval($_GET['b']))) : '';
    $_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_messages = mysqli_query_logged("SELECT * FROM community_messages, community_threads, community_forums, community_sections, community WHERE community_messages.message_id = " . sq($_id) . " AND community_messages.thread_id = community_threads.thread_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_messages_row = mysqli_fetch_assoc($community_messages))    
    {
        header('Location: ./?s=communities');
        die;        
    }
    
    require_once('include/functions/community_banned.php');
    community_banned($community_messages_row['community_id']);
    require_once('include/functions/community_permissions.php');
    community_permissions($community_messages_row['community_id']);
    
    $allow_reply = $GLOBALS['auth']['id'] && ($community_messages_row['forum_locked'] == 0 || $GLOBALS['auth']['community']['locked_locked_post']) && ($community_messages_row['thread_locked'] == 0 || $GLOBALS['auth']['community']['thread_locked_post']);
    $allow_modify = 0;
    $allow_modify_title = 0;
    if ($allow_reply)
    {
        $last_message = mysqli_query_logged("SELECT * FROM community_messages WHERE thread_id = '" . $community_messages_row['thread_id'] . "' ORDER BY message_id DESC LIMIT 1");
        if ($last_message_row = mysqli_fetch_assoc($last_message))
        {
            if ($GLOBALS['auth']['id'] == $last_message_row['message_user_id'])
            {
                $ago = date('Y-m-d H:i:s', mktime(date("H"), date("i") - 25, date("s"), date('m'), date('d'), date('Y')));
                if ($last_message_row['message_posted_on'] >= $ago)
                {
                    $allow_modify = $last_message_row['message_id'];
                    if ($community_messages_row['thread_first_message_id'] == $last_message_row['message_id'])
                    {
                        $allow_modify_title = $community_threads_row['thread_title'];
                    }
                }
            }
        }
    }
    
    if ($allow_modify && $allow_modify_title)
    {
        require_once('include/functions/community_thread_valid.php');
        $error = community_thread_valid($_title, $_body);
        if ($error)    
        {
            show_error_page($error);
            die;        
        }
        mysqli_query_logged("UPDATE messages_thread SET thread_title = " . sq($_title) . " WHERE thread_id = '" . $community_messages_row['thread_id'] . "'");
        mysqli_query_logged("UPDATE community_messages_bodies SET message_body = " . sq($_body) . " WHERE message_id = '" . $community_messages_row['message_id'] . "'");
    }
    elseif ($allow_modify)
    {
        require_once('include/functions/community_reply_valid.php');
        $error = community_reply_valid($_body);
        if ($error)    
        {
            show_error_page($error);
            die;
        }
        mysqli_query_logged("UPDATE community_messages_bodies SET message_body = " . sq($_body) . " WHERE message_id = '" . $community_messages_row['message_id'] . "'");
    }
    
    header('Location: ./?' . ($_back ? $_back : 's=community_thread&i=' . $community_messages_row['thread_id']));
    die;
?>