<?php
    require_once('include/functions/ip_decode.php');
    require_once('include/functions/pages.php');
    require_once('include/functions/community_reply_overall_vote.php');
    require_once('include/functions/community_thread_overall_rating.php');
    require_once('include/functions/rw_code.php');
    require_once('include/functions/coolness.php');    

    if (language() == 'french')
    {
        define('SHOW_COMMUNITY_THREAD_ERROR_NO_THREAD', 'D�sol�, ce sujet n\'existe pas.');
        define('SHOW_COMMUNITY_THREAD_ERROR_NO_FORUM', 'D�sol�, ce forum n\'existe pas.');
        define('SHOW_COMMUNITY_THREAD_ERROR_NO_SECTION', 'D�sol�, ce section n\'existe pas.');
        define('SHOW_COMMUNITY_THREAD_ERROR_NO_COMMUNITY', 'D�sol�, cette communaut� n\'existe pas.');
        define('SHOW_COMMUNITY_THREAD_ADD_UPDATE', 'Ajouter Mise � jour');
        define('SHOW_COMMUNITY_THREAD_LOADING_QUOTE', 'Citation De Chargement...');
        define('SHOW_COMMUNITY_THREAD_GOOD', 'Bon');
        define('SHOW_COMMUNITY_THREAD_BAD', 'Mauvais');
        define('SHOW_COMMUNITY_THREAD_COMMUNITY', 'Communaut�s');
        define('SHOW_COMMUNITY_THREAD_SMILEYS', 'Smileys');
        define('SHOW_COMMUNITY_THREAD_MUST_BE_LOGGED_IN_TO_VOTE', 'Vous devez etre connect� afin de voter.');
        define('SHOW_COMMUNITY_THREAD_POST_A_REPLY', 'Poster Une R�ponse');
        define('SHOW_COMMUNITY_THREAD_THIS_THREAD_IS_LOCKED', 'Ce sujet est verrouill�. Aucun poster est permis. ');
        define('SHOW_COMMUNITY_THREAD_YOU_CAN_NOT_POST_TWICE', 'Vous ne pouvez pas poster deux fois en rang. Essayer mettant � jour votre derni�re r�ponse plut�t.');
        define('SHOW_COMMUNITY_THREAD_MUST_BE_LOGGED_IN_TO_REPLY', 'Vous devez �tre connect� pour poster une r�ponse. ');
        define('SHOW_COMMUNITY_THREAD_DELETE_THIS_THREAD', 'Effacer Ce Sujet');
        define('SHOW_COMMUNITY_THREAD_UNLOCK_THIS_THREAD', 'Ouvrir Ce Sujet');
        define('SHOW_COMMUNITY_THREAD_LOCK_THIS_THREAD', 'Verrouiller Ce Sujet');
        define('SHOW_COMMUNITY_THREAD_UN_STICKY_THIS_THREAD', 'D�coller Ce Sujet');
        define('SHOW_COMMUNITY_THREAD_STICKY_THIS_THREAD', 'Coller Ce Sujet');
        define('SHOW_COMMUNITY_THREAD_MOVE_THIS_MESSAGE', 'D�placer Ce Sujet');
        define('SHOW_COMMUNITY_THREAD_MODERATOR_TOOLS', 'Outils De Mod�rateur');
        define('FUNCTIONS_COMMUNITY_DISPLAY_MESSAGE_REPLIED_ON', 'a r�pondu le');
        define('FUNCTIONS_COMMUNITY_DISPLAY_MESSAGE_FROM', 'de');
        define('FUNCTIONS_COMMUNITY_DISPLAY_MESSAGE_UPDATE', 'Mise � Jour');
        define('FUNCTIONS_COMMUNITY_DISPLAY_MESSAGE_WROTE_ON', 'a �crit sur');
    }
    else
    {
        define('SHOW_COMMUNITY_THREAD_ERROR_NO_THREAD', 'Sorry, that thread does not exist.');
        define('SHOW_COMMUNITY_THREAD_ERROR_NO_FORUM', 'Sorry, that forum does not exist.');
        define('SHOW_COMMUNITY_THREAD_ERROR_NO_SECTION', 'Sorry, that section does not exist.');
        define('SHOW_COMMUNITY_THREAD_ERROR_NO_COMMUNITY', 'Sorry, that community does not exist.');
        define('SHOW_COMMUNITY_THREAD_ADD_UPDATE', 'Add Update');
        define('SHOW_COMMUNITY_THREAD_LOADING_QUOTE', 'Loading Quote...');
        define('SHOW_COMMUNITY_THREAD_GOOD', 'notice_good');
        define('SHOW_COMMUNITY_THREAD_BAD', 'Bad');
        define('SHOW_COMMUNITY_THREAD_COMMUNITY', 'Communities');
        define('SHOW_COMMUNITY_THREAD_SMILEYS', 'Smileys');
        define('SHOW_COMMUNITY_THREAD_MUST_BE_LOGGED_IN_TO_VOTE', 'You must be logged-in in order to vote.');
        define('SHOW_COMMUNITY_THREAD_POST_A_REPLY', 'Post A Reply');
        define('SHOW_COMMUNITY_THREAD_THIS_THREAD_IS_LOCKED', 'This thread is locked. No posting is allowed.');
        define('SHOW_COMMUNITY_THREAD_YOU_CAN_NOT_POST_TWICE', 'You can not post twice in a row. Try updating your last reply instead.');
        define('SHOW_COMMUNITY_THREAD_MUST_BE_LOGGED_IN_TO_REPLY', 'You must be logged in to post a reply.');
        define('SHOW_COMMUNITY_THREAD_DELETE_THIS_THREAD', 'Delete This Thread');
        define('SHOW_COMMUNITY_THREAD_UNLOCK_THIS_THREAD', 'Unlock This Thread');
        define('SHOW_COMMUNITY_THREAD_LOCK_THIS_THREAD', 'Lock This Thread');
        define('SHOW_COMMUNITY_THREAD_UN_STICKY_THIS_THREAD', 'Un-Sticky This Thread');
        define('SHOW_COMMUNITY_THREAD_STICKY_THIS_THREAD', 'Sticky This Thread');
        define('SHOW_COMMUNITY_THREAD_MOVE_THIS_MESSAGE', 'Move This Message');
        define('SHOW_COMMUNITY_THREAD_MODERATOR_TOOLS', 'Moderator Tools');
        define('FUNCTIONS_COMMUNITY_DISPLAY_MESSAGE_REPLIED_ON', 'replied on');
        define('FUNCTIONS_COMMUNITY_DISPLAY_MESSAGE_FROM', 'from');
        define('FUNCTIONS_COMMUNITY_DISPLAY_MESSAGE_UPDATE', 'Update');
        define('FUNCTIONS_COMMUNITY_DISPLAY_MESSAGE_WROTE_ON', 'wrote on');
    }
    
    function community_thread($type = 'normal')
    {
        $_id = isset($_GET['i']) ? intval($_GET['i']) : 0;
        $_page = isset($_GET['p']) ? intval($_GET['p']) : 1;
        $_page = $_page > 0 ? $_page : 1;
        
        if ($type == 'normal')
        {
            $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");
        }
        elseif ($type == 'tiles')
        {
            $community = mysqli_query_logged("SELECT * FROM community_threads, community_forums, community_sections, community WHERE community_threads.thread_tile = " . 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");
        }
        elseif ($type == 'quilts')
        {
            $community = mysqli_query_logged("SELECT * FROM community_threads, community_forums, community_sections, community WHERE community_threads.thread_quilt = " . 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))    
        {
            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']);
            
            mysqli_query_logged("UPDATE community_threads SET thread_views = thread_views + 1 WHERE thread_id = '" . $community_row['thread_id'] . "'");
            
            $false_reason = '';
            $allow_modify = 0;
            $allow_modify_title = 0;
            $allow_reply = $GLOBALS['auth']['id'] && ((!$community_row['forum_locked'] && !$community_row['thread_locked']) || $GLOBALS['auth']['community']['thread_lock_post']);
            if ($allow_reply)
            {
                $community_messages = mysqli_query_logged("SELECT * FROM community_messages WHERE thread_id = '" . $_id . "' ORDER BY message_id DESC LIMIT 1");
                if ($community_messages_row = mysqli_fetch_assoc($community_messages))
                {
                    $ago = date('Y-m-d H:i:s', mktime(date("H"), date("i"), date("s"), date('m') - 6, date('d'), date('Y'))); 
                    if ($community_messages_row['message_posted_on'] < $ago)
                    {
                        $allow_reply = false;
                        $false_reason = 'This topic is too old to reply to.';
                    }
                    if ($GLOBALS['auth']['id'] == $community_messages_row['message_user_id'])
                    {
                        $ago = date('Y-m-d H:i:s', mktime(date("H"), date("i") - 15, date("s"), date('m'), date('d'), date('Y'))); 
                        if ($community_messages_row['message_posted_on'] >= $ago)
                        {
                            $allow_modify = $community_messages_row['message_id'];
                            if ($community_row['thread_first_message_id'] == $community_messages_row['message_id'])
                            {
                                $allow_modify_title = $community_row['thread_title'];
                            }
                        }
                        $allow_reply = false;
                    }
                }
            }
            
            $reply_count = mysqli_fetch_assoc(mysqli_query_logged("SELECT COUNT(*) AS count FROM community_messages WHERE thread_id = '" . $community_row['thread_id'] . "'"));
            $page_count = ceil($reply_count['count'] / COMMUNITY_REPLIES_PER_PAGE);
            if ($page_count == 0)
            {
                $page_count = 1;
            }
            
            $last_read_message = 0;
            $next_unread_message = 0;
            if ($GLOBALS['auth']['id'])
            {
                $community_threads_pointers = mysqli_query_logged("SELECT message_id FROM community_threads_pointers WHERE thread_id = '" . $community_row['thread_id'] . "' AND user_id = '" . $GLOBALS['auth']['id'] . "'");
                if ($community_threads_pointers_row = mysqli_fetch_assoc($community_threads_pointers))
                {
                    $last_read_message = $community_threads_pointers_row['message_id'];
                    $community_messages = mysqli_query_logged("SELECT message_id FROM community_messages WHERE thread_id = '" . $community_row['thread_id'] . "' AND message_id > '" . $last_read_message . "' ORDER BY message_id LIMIT 1");
                    if ($community_messages_row = mysqli_fetch_assoc($community_messages))
                    {
                        $next_unread_message = $community_messages_row['message_id'];
                    }
                    else
                    {
                        $next_unread_message = 'none';
                    }
                }
                else
                {
                    $next_unread_message = $community_row['thread_first_message_id'];
                }
                if (!isset($_GET['p']))
                {
                    $previous_count = mysqli_fetch_assoc(mysqli_query_logged("SELECT COUNT(*) AS num FROM community_messages WHERE thread_id = '" . $community_row['thread_id'] . "' AND message_id <= '" . $last_read_message . "'"));
                    $_page = ceil(($previous_count['num'] + 1) / COMMUNITY_REPLIES_PER_PAGE);            
                    if ($_page > $page_count) 
                    {
                        $_page = $page_count;
                    }        
                }        
            }
        
            $pages = '<span class="pages">Page: ' . pages('?s=community_thread&i=' . $community_row['thread_id'], $_page, $page_count) . '</span>';    
?>

<script type="text/javascript">
    function quote_message(id)
    {
        doAjax('index.php?j=community_quote&i=' + id, function(AJAX) { eval(AJAX.responseText); });
    }
      function display_update_textbox(id)
      {
          var p = '';
        p += '<br /><form action="?a=community_reply_update&i=' + id + '&b=<?php echo str_replace('&', '@', $_SERVER['QUERY_STRING']) ?>'" method="post" class="form">';
        p += '<textarea name="body" class="input_text" style="height: 120px; width: 90%;"></textarea>';
        p += '<br /><div style="padding: 5px 0px 0px 0px;"></div>';
        p += '<input type="submit" value="<?php echo htmlentities(SHOW_COMMUNITY_THREAD_ADD_UPDATE) ?>" class="input_submit">';
        p += '</form>';
        var obj = document.getElementById('message' + id);
        obj.innerHTML = p;
        obj.style.display = 'block';
      }
     function rep(str, needle, replace)
    {
        for (var i = 0; i < str.length; i++)
        {
              if (str.search(needle) != -1)
              {
                str = str.replace(needle, replace);
              }
          }
          return str;
      }     
</script>

<?php
            $newtitle = '';
            if ($community_row['thread_tile'])
            {
                $tiles = mysqli_query_logged("SELECT * FROM tiles WHERE tile_id = '" . $community_row['thread_tile'] . "'");
                if ($tiles_row = mysqli_fetch_assoc($tiles))
                {
                    if ($tiles_row['comment'])
                    {
                        $newtitle = substr($tiles_row['comment'], 0, 50);
                    }
                }
                else
                {
                    $newtitle = 'Removed Tile';
                }
            } 
            
            if ($type == 'normal')
            {
                include('include/parts/header.php');
            }
        
            echo box_outside_top('<a href="?s=community">' . SHOW_COMMUNITY_THREAD_COMMUNITY . '</a> - <a href="?s=community&i=' . $community_row['community_id'] . '">' . htmlspecialchars($community_row['community_name']) . '</a> - <a href="?s=community_forum&i=' . $community_row['forum_id'] . '">' . htmlspecialchars($community_row['forum_name_' . language()]) . '</a> - Reading Thread', '[ <a href="?s=community_my_forum&i=' . $community_row['community_id'] . '&type=new">New Threads</a> | <a href="?s=community_my_forum&i=' . $community_row['community_id'] . '&type=my_new">Participating New Threads</a> ]');
            echo box_inside_top();
            echo '<table width="100%" border="0" cellpadding="0" cellspacing="0"><tr>';
            echo '<td>';
            echo '<span style="font-size: 14px; font-weight: bold;">';
            if ($community_row['thread_locked'])
            {
                echo '[ <span class="notice_attention">Locked</span> ] ';
            }
            if ($community_row['thread_sticky'])
            {
                echo '[ <span class="notice_error">Sticky</span> ] ';
            }
            echo htmlentities($newtitle ? $newtitle : $community_row['thread_title']);
            echo '</span>';
            echo '</td>';
            echo '<td align="right">';
            echo '<span style="font-weight: bold;">Rating: <span id="thread_ranking">' . thread_overall_rating($community_row['thread_id']) . '</span></span>';
            if ($GLOBALS['auth']['id'] && $community_row['thread_user_id'] != $GLOBALS['auth']['id'])
            {
                echo '&nbsp;&nbsp;&nbsp;<form class="form" style="display: inline"><span style="font-weight: bold;">Change:</span> <select id="rating" name="rating" class="input_select" onChange="doAjax(\'index.php?j=thread_rating&i=' . $community_row['thread_id'] . '&v=\' + document.getElementById(\'rating\').value, function(AJAX) { eval(AJAX.responseText); }); return false;"><option value="0"></option>';
                $current_vote = mysqli_query_logged("SELECT category_id FROM community_threads_ratings WHERE thread_id = '" . $_id . "' AND user_id = '" . $GLOBALS['auth']['id'] . "'");
                if (mysqli_num_rows($current_vote))
                {
                    $current_vote = mysqli_fetch_assoc($current_vote);
                    $current_vote = $current_vote['category_id'];    
                }
                $community_threads_categories = mysqli_query_logged("SELECT * FROM community_threads_categories ORDER BY name");
                while ($community_threads_categories_row = mysqli_fetch_assoc($community_threads_categories))
                {
                    echo '<option value="' . $community_threads_categories_row['id'] . '" class="' . ($community_threads_categories_row['positive'] ? 'notice_good' : 'notice_error') . '"' . ($current_vote == $community_threads_categories_row['id'] ? ' selected' : '') . '>' . $community_threads_categories_row['name'] . '</option>';
                }
                echo '</select></form>';
            }
            echo '</td>';
            echo '</tr></table>';
            echo box_inside_bottom();
            echo box_outside_bottom();
        
            echo box_outside_top('<a href="?s=community">' . SHOW_COMMUNITY_THREAD_COMMUNITY . '</a> - <a href="?s=community&i=' . $community_row['community_id'] . '">' . htmlspecialchars($community_row['community_name']) . '</a> - <a href="?s=community_forum&i=' . $community_row['forum_id'] . '">' . htmlspecialchars($community_row['forum_name_' . language()]) . '</a> - Reading Thread<br />' . $pages, '[ <a href="?s=smileys">Smileys</a> | <a href="?s=community_help">Help</a> ]');
            $i = 0;
            $last_displayed_message = 0;
            $community_messages = mysqli_query_logged("SELECT * FROM community_messages WHERE thread_id = '" . $community_row['thread_id'] . "' ORDER BY message_id LIMIT " . (($_page - 1) * COMMUNITY_REPLIES_PER_PAGE . ", " . COMMUNITY_REPLIES_PER_PAGE));
            if (mysqli_num_rows($community_messages))
            {
                while ($community_messages_row = mysqli_fetch_assoc($community_messages))
                {
                    if ($GLOBALS['auth']['id'])
                    {
                        $last_displayed_message = $community_messages_row['message_id'];
                    }
                    if ($next_unread_message == $community_messages_row['message_id'])
                    {
                        if ($i++)
                        {
                            echo '<div style="padding: 5px 0px 0px 0px;"></div>';
                        }
                        echo '<div id="l" class="inside" style="font-size: 12px; font-weight: bold; padding-left: 8px;"><span class="notice_good">You\'ve read up to this point.</span></div>';    
                        echo '<div style="padding: 5px 0px 0px 0px;"></div>';
                    }
                    elseif ($i++)
                    {
                        echo '<div style="padding: 5px 0px 0px 0px;"></div>';
                    }
                    $ignored = mysqli_query_logged("SELECT * FROM ignored WHERE user_id = '" . $GLOBALS['auth']['id'] . "' AND link_id = '" . $community_messages_row['message_user_id'] . "'");
                    if ($ignored_row = mysqli_fetch_assoc($ignored))
                    {
                        $message_threashold = 10000;
                    }
                    else
                    {
                        $message_threashold = -5;
                    }
                    $allow_update = ($community_messages_row['message_user_id'] == $GLOBALS['auth']['id'] && !$community_row['thread_locked']) || $GLOBALS['auth']['community']['thread_lock_post'] ? true : false;
                    $rating = reply_overall_rating($community_messages_row['message_id']);
                    if ($rating == 0)
                    {
                        $rating_str = 'Neutral [0]';
                    }
                    elseif ($rating > 0)
                    {
                        $rating_str = '<span class="notice_good">Good [+' . $rating . ']</span>';
                    }
                    else
                    {
                        $rating_str = '<span class="notice_error">Bad [' . $rating . ']</span>';
                    }
                    echo '<table width="100%" cellpadding="0" cellspacing="0" border="0">';
                    echo '<tr><td class="inside" style="padding: 5px;">';
                    echo '<span id="r' . $community_messages_row['message_id'] . '" style="float: right; font-weight: bold;">' . $rating_str;
                    if ($GLOBALS['auth']['id'] && $GLOBALS['auth']['id'] != $community_messages_row['message_user_id'] && !mysqli_num_rows(mysqli_query_logged("SELECT * FROM community_messages_rating WHERE message_id = '" . $community_messages_row['message_id'] . "' AND user_id = '" . $GLOBALS['auth']['id'] . "'")))
                    {
                        echo '&nbsp;&nbsp;&nbsp;<span style="font-weight: normal;"><span class="notice_good"><a href="#" onClick="doAjax(\'index.php?j=reply_vote&i=' . $community_messages_row['message_id'] . '&v=good\', function(AJAX) { eval(AJAX.responseText); }); return false;">Good</a></span> / <span class="notice_error"><a href="#" onClick="doAjax(\'index.php?j=reply_vote&i=' . $community_messages_row['message_id'] . '&v=bad\', function(AJAX) { eval(AJAX.responseText); }); return false;">Bad</a></span></span>'; 
                    }
                    echo '</span>';
                    if ($GLOBALS['auth']['community']['thread_delete'] || $GLOBALS['auth']['community']['message_delete'])
                    {
                        if ($community_messages_row['message_id'] == $community_row['thread_first_message_id'])
                        {
                            if ($GLOBALS['auth']['community']['thread_delete'])
                            {
                                echo '<span style="float: right; margin-right: 15px;"><a href="?a=community_thread_delete&i=' . $community_row['thread_id'] . '">Delete Thread</a></span>';
                            }
                        }
                        else
                        {
                            if ($GLOBALS['auth']['community']['message_delete'])
                            {
                                echo '<span style="float: right; margin-right: 15px;"><a href="?a=community_reply_delete&i=' . $community_messages_row['message_id'] . '&b=' . str_replace('&', '@', $_SERVER['QUERY_STRING']) . '">Delete Reply</a></span>';
                            }
                        }
                    }
                    echo '<span style="float: right; margin-right: 15px;"><a href="javascript:toggle(\'i' . $community_messages_row['message_id'] . '\');">Toggle Reply</a></span>';
                    echo '<b>' . get_username($community_messages_row['message_user_id'], 1) . '</b> ' . FUNCTIONS_COMMUNITY_DISPLAY_MESSAGE_REPLIED_ON . ' ' . nice_date($community_messages_row['message_posted_on']) . ' ' . FUNCTIONS_COMMUNITY_DISPLAY_MESSAGE_FROM . ' <a href="?s=lookup_ip&ip=' . decode_ip($community_messages_row['message_ip']) . '">' . decode_ip($community_messages_row['message_ip']) . '</a>';
                    echo '</td></tr>';
                    echo '</table>';
                    echo '<div id="i' . $community_messages_row['message_id'] . '" style="display: ' . ($rating > $message_threashold ? 'block' : 'none') . ';">';
                    echo '<table width="100%" cellpadding="0" cellspacing="0" border="0">';
                    echo '<tr><td colspan="2" height="4" nowrap></td></tr>';
                    echo '<tr>';
                    echo '<td width="160" rowspan="3" style="font-size: 10px; font-weight: bold; vertical-align: top; text-align: left;">';
                    echo box_image_top('margin: 0px 5px 0px 0px;');
                    echo '<a href="?s=member&u=' . get_username($community_messages_row['message_user_id']) . '"><img src="?g=thumb&i=' . get_main_image_id($community_messages_row['message_user_id']) . '" width="' . THUMB_WIDTH . '" height="' . THUMB_HEIGHT . '" alt="' . get_username($community_messages_row['message_user_id']) . '" border="0" style="border: 0px;" /></a>';
                    echo '<div style="padding: 3px 0px 0px 0px;"></div>';
                    echo 'Coolness: ' . coolness($community_messages_row['message_user_id']) . '<br />';
                    echo box_image_bottom();
                    echo '</td>';
                    echo '<td valign="top" height="90" class="inside">';
                    echo '<div style="font-size: 11px; font-weight: bold; ">';
                    $community_messages_bodies = mysqli_query_logged("SELECT * FROM community_messages_bodies WHERE message_id = '" . $community_messages_row['message_id'] . "'");
                    if ($community_messages_bodies_row = mysqli_fetch_assoc($community_messages_bodies))
                    {
                        echo transform_body($community_messages_bodies_row['message_body']);
                    }
                    else
                    {
                        echo 'No Body!';
                    }        
                    echo '</div>';
                    $community_messages_updates = mysqli_query_logged("SELECT * FROM community_messages_updates WHERE message_id = '" . $community_messages_row['message_id'] . "' ORDER BY update_id");
                    if (mysqli_num_rows($community_messages_updates))
                    {
                        echo '<br />';
                        while ($community_messages_updates_row = mysqli_fetch_array($community_messages_updates))
                        {
                            echo '<div style="padding: 5px 0px 0px 0px;"></div>';
                            echo '<span class="notice_attention"><b>' . FUNCTIONS_COMMUNITY_DISPLAY_MESSAGE_UPDATE . '</b></span> ';
                            echo '<b>' . get_username($community_messages_updates_row['update_user_id'], 1) . '</b> ' . FUNCTIONS_COMMUNITY_DISPLAY_MESSAGE_WROTE_ON . ' ' . nice_date($community_messages_updates_row['update_posted_on']) . ' ' . FUNCTIONS_COMMUNITY_DISPLAY_MESSAGE_FROM . ' <a href="?s=lookup_ip&ip=' . decode_ip($community_messages_updates_row['update_ip']) . '">' . decode_ip($community_messages_updates_row['update_ip']) . '</a>';
                            echo '<div style="padding: 5px 0px 0px 0px;"></div>';
                            echo '<div style="font-size: 11px; font-weight: bold;">';
                            echo transform_body($community_messages_updates_row['update_body']);
                            echo '</div>';
                        }
                    }
                    echo '<div id="message' . $community_messages_row['message_id'] . '" style="display: none;"></div>';
                    echo '</td>';
                    echo '</tr>';
                    echo '<tr><td height="4" nowrap></td></tr>';
                    echo '<tr><td height="18" class="inside" style="padding: 5px;">';
                    if ($allow_reply)
                    {
                        echo '<span style="float: right; margin-left: 15px;"><a href="javascript:quote_message(' . $community_messages_row['message_id'] . ');">Quote Message</a></span>';
                    }
                    if ($allow_update)
                    {
                        echo '<span style="float: right;"><a href="javascript:display_update_textbox(' . $community_messages_row['message_id'] . ');">Add Update</a></span>';
                    }
                    if ($community_messages_row['message_mood'])
                    {
                        $members_moods = mysqli_query_logged("SELECT * FROM members_moods WHERE id = '" . $community_messages_row['message_mood'] . "'");
                        if ($members_moods_row = mysqli_fetch_assoc($members_moods))
                        {
                            echo 'I\'m feeling ' . $members_moods_row['mood'] . ' right now..';
                        }
                    }                    
                    echo '</td></tr>';
                    echo '</table>';
                    echo '</div>';
                    if ($community_row['thread_last_message_id'] == $community_messages_row['message_id'] && $next_unread_message == 'none')
                    {
                        echo '<div style="padding: 5px 0px 0px 0px;"></div>';
                        echo '<div id="l" class="inside" style="font-size: 12px; font-weight: bold; padding-left: 8px;"><span class="notice_good">You\'ve already read all the messages in this thread.</span></div>';    
                    }
                }
            }
            else
            {
                echo '<table width="100%" cellpadding="0" cellspacing="0" border="0">';
                echo '<tr><td class="inside" style="padding: 5px;">';
                echo 'There are no messages in this thread yet.';
                echo '</td></tr>';
                echo '</table>';
            }
        
            echo box_outside_bottom();
        
            if (mysqli_num_rows($community_messages) > 5)
            {
                echo make_pages('<a href="?s=community">' . SHOW_COMMUNITY_THREAD_COMMUNITY . '</a> - <a href="?s=community&i=' . $community_row['community_id'] . '">' . htmlspecialchars($community_row['community_name']) . '</a> - <a href="?s=community_forum&i=' . $community_row['forum_id'] . '">' . htmlspecialchars($community_row['forum_name_' . language()]) . '</a> - Reading Thread<br />' . $pages);
            }
            
            if ($GLOBALS['auth']['id'] && $last_displayed_message > $last_read_message)
            {
                mysqli_query_logged("REPLACE INTO community_threads_pointers SET thread_id = '" . $community_row['thread_id'] . "', user_id = '" . $GLOBALS['auth']['id'] . "', message_id = '" . $last_displayed_message . "'");
            }
        
            echo box_outside_top(SHOW_COMMUNITY_THREAD_POST_A_REPLY, '[ <a href="?s=community_my_forum&i=' . $community_row['community_id'] . '&type=new">New Threads</a> | <a href="?s=community_my_forum&i=' . $community_row['community_id'] . '&type=my_new">Participating New Threads</a> ]');
            echo box_inside_top();
            if ($allow_reply)
            {
?>

<form action="?a=community_reply_add&i=<?php echo $community_row['thread_id']; ?>&b=<?php echo str_replace('&', '@', $_SERVER['QUERY_STRING']) ?>" method="post" class="form">
<textarea name="body" id="reply_playarea" class="input_text" style="height: 120px; width: 90%"></textarea>
<div style="padding: 5px 0px 0px 0px;"></div>
<input type="submit" value="<?php echo SHOW_COMMUNITY_THREAD_POST_A_REPLY ?>" class="input_submit">
</form>

<?php
            }
            elseif ($GLOBALS['auth']['id'] && (($community_row['forum_locked'] || $community_row['thread_locked']) && !$GLOBALS['auth']['community']['thread_lock_post']))
            {
                echo SHOW_COMMUNITY_THREAD_THIS_THREAD_IS_LOCKED;    
            }
            elseif ($false_reason)
            {
                echo $false_reason;
            }
            elseif ($GLOBALS['auth']['id'])
            {
                if ($allow_modify)
                {
                    $community_messages_bodies = mysqli_query_logged("SELECT * FROM community_messages_bodies WHERE message_id = '" . $allow_modify . "'");
                    if ($community_messages_bodies_row = mysqli_fetch_assoc($community_messages_bodies))
                    {
                        echo 'You can edit your message for up to 15 minutes after you have posted it.';
                        echo '<div style="padding: 5px 0px 0px 0px;"></div>';
                        echo '<form action="?a=community_reply_modify&i=' . $allow_modify . '&b=' . str_replace('&', '@', $_SERVER['QUERY_STRING']) . '" method="post" class="form">';
                        if ($allow_modify_title)
                        {
                            echo '<input name="title" class="input_text" style="width: 90%" value="' . htmlspecialchars($allow_modify_title) . '">';
                            echo '<div style="padding: 5px 0px 0px 0px;"></div>';
                        }
                        echo '<textarea name="body" class="input_text" style="height: 120px; width: 90%">' . htmlspecialchars($community_messages_bodies_row['message_body']) . '</textarea>';
                        echo '<div style="padding: 5px 0px 0px 0px;"></div>';
                        echo '<input type="submit" value="Modify Message" class="input_submit">';
                        echo '</form>';
                    }
                    else
                    {
                        echo 'Message Not Found...';
                    }
                }
                else
                {
                    echo SHOW_COMMUNITY_THREAD_YOU_CAN_NOT_POST_TWICE;
                }
            }    
            else
            {
                echo SHOW_COMMUNITY_THREAD_MUST_BE_LOGGED_IN_TO_REPLY;    
            }
            echo box_inside_bottom();
            echo box_outside_bottom();
        
            if ($GLOBALS['auth']['community']['thread_delete'] || $GLOBALS['auth']['community']['thread_lock'] || $GLOBALS['auth']['community']['thread_sticky'] || $GLOBALS['auth']['community']['thread_move'])
            {
                $i = 0;
                echo box_outside_top(SHOW_COMMUNITY_THREAD_MODERATOR_TOOLS);
                echo box_inside_top();
                if ($GLOBALS['auth']['community']['thread_delete'])
                {
                    echo '<a href="?a=community_thread_delete&i=' . $community_row['thread_id'] . '&b=' . str_replace('&', '@', $_SERVER['QUERY_STRING']) . '" onClick="return confirm(\'Are you sure you want to delete this message?\r\n You cannot undo this.\');">' . SHOW_COMMUNITY_THREAD_DELETE_THIS_THREAD . '</a><br />';
                }
                if ($GLOBALS['auth']['community']['thread_lock'])
                {
                    if ($community_row['thread_locked'])
                    {
                        echo '<a href="?a=community_thread_lock&i=' . $community_row['thread_id'] . '&b=' . str_replace('&', '@', $_SERVER['QUERY_STRING']) . '&unlock" onClick="return confirm(\'Are you sure you want to unlock this message?\r\n Any member will be able to post again.\');">' . SHOW_COMMUNITY_THREAD_UNLOCK_THIS_THREAD . '</a><br />';
                    }
                    else
                    {
                        echo '<a href="?a=community_thread_lock&i=' . $community_row['thread_id'] . '&b=' . str_replace('&', '@', $_SERVER['QUERY_STRING']) . '&lock" onClick="return confirm(\'Are you sure you want to lock this message?\r\n No further posting will be allowed (except by authorised moderators).\');">' . SHOW_COMMUNITY_THREAD_LOCK_THIS_THREAD . '</a><br />';
                    }
                }
                if ($GLOBALS['auth']['community']['thread_sticky'])
                {
                    if ($community_row['thread_sticky'])
                    {
                        echo '<a href="?a=community_thread_sticky&i=' . $community_row['thread_id'] . '&b=' . str_replace('&', '@', $_SERVER['QUERY_STRING']) . '&unstick" onClick="return confirm(\'Are you sure you want to make this a regular message?\r\n It wont appear at the top of the forum anymore.\');">' . SHOW_COMMUNITY_THREAD_UN_STICKY_THIS_THREAD . '</a><br />';
                    }
                    else
                    {
                        echo '<a href="?a=community_thread_sticky&i=' . $community_row['thread_id'] . '&b=' . str_replace('&', '@', $_SERVER['QUERY_STRING']) . '&stick" onClick="return confirm(\'Are you sure you want to sticky this message?\r\n It will appear at the top of the forum.\');">' . SHOW_COMMUNITY_THREAD_STICKY_THIS_THREAD . '</a><br />';
                    }
                }
                if ($GLOBALS['auth']['community']['thread_move'])
                {
                    echo '<a href="?s=community_thread_move&i=' . $community_row['thread_id'] . '&b=' . str_replace('&', '@', $_SERVER['QUERY_STRING']) . '">' . SHOW_COMMUNITY_THREAD_MOVE_THIS_MESSAGE . '</a><br />';                
                }
                echo box_inside_bottom();
                echo box_outside_bottom();
            }
            
            if ($GLOBALS['auth']['id'])
            {
                echo make_pages('<a href="?s=community">' . SHOW_COMMUNITY_THREAD_COMMUNITY . '</a> - <a href="?s=community&i=' . $community_row['community_id'] . '">' . htmlspecialchars($community_row['community_name']) . '</a> - <a href="?s=community_forum&i=' . $community_row['forum_id'] . '">' . htmlspecialchars($community_row['forum_name_' . language()]) . '</a> - Reading Thread');
            }
            
            if ($type == 'normal')
            {
                include('include/parts/footer.php');
            }
        }
        else
        {
            if ($type == 'tiles' || $type == 'quilts')
            {
                comments($_id, $type);
            }
            else
            {
                header('Location: ./?s=communities');
                die;
            }
        }
    }

    function transform_body($b)
    {
        return bbcodes(images_code(nl2br(smileys(htmlspecialchars($b)))));
    }
?>