<?php

/****************************************/
// ИНФОРМАЦИЯ:
// ==== Форум: LogicBoard
// ==== Автор: Никита Курдин (ShapeShifter)
// ==== Copyright © Никита Курдин Игоревич 2011
// ==== Данный код защищен авторскими правами
// ==== Официальный сайт: http://logicboard.ru

/****************************************/

if (! defined ( 'LogicBoard' ))
{
	@include '../../../logs/save_log.php';
	exit ( "Error, wrong way to file.<br><a href=\"/\">Go to main</a>." );
}

$lang_m_b_topic_posts = language_forum ("board/modules/board/topic_posts");

$_SESSION['back_link_board'] = $_SERVER['REQUEST_URI'];

$topic = $DB->one_join_select( "t.*, p.vote_num, p.title as p_title, p.question, p.variants, p.multiple, p.answers", "LEFT", "topics t||topics_poll p", "t.poll_id=p.id", "t.id = '{$id}'" );

function here_loaction ()
{
    global $link_speddbar, $onl_location, $lang_m_b_topic_posts;
    
    $link_speddbar = speedbar_forum (0)."|".$lang_m_b_topic_posts['location_access_denied'];
    $onl_location = $lang_m_b_topic_posts['location_online_access_denied'];
}

if ($topic['id'] AND !forum_permission($topic['forum_id'], "read_forum"))
{
    here_loaction ();
    message ($lang_message['access_denied'], str_replace("{group}", $cache_group[$member_id['user_group']]['g_title'], $lang_m_b_topic_posts['access_denied_group_v']), 1);
}
elseif ($topic['id'] AND !forum_permission($topic['forum_id'], "read_theme"))
{
    here_loaction ();
    message ($lang_message['access_denied'], str_replace("{group}", $cache_group[$member_id['user_group']]['g_title'], $lang_m_b_topic_posts['access_denied_group_r']), 1);
}
elseif ($topic['id'] AND $topic['hiden'] AND !forum_options_topics($topic['forum_id'], "hideshow"))
{
    here_loaction ();
    message ($lang_message['access_denied'], $lang_m_b_topic_posts['access_denied_group_h'], 1);
}
elseif ($topic['id'] AND forum_all_password($topic['forum_id']))
{   
    if(isset($_POST['check_pass']))
    {
        $check_f_pass_id = intval($_POST['f_id']);
        $check_f_pass = $_POST['f_pass'];
        if ($LB_flood->isBlock())
        {
            message ($lang_message['flood_control'], str_replace("{time}", $LB_flood->block_time, $lang_message['flood_control_stop']));
        }
        elseif ($check_f_pass == $cache_forums[$topic['forum_id']]['password'] AND $cache_forums[$topic['forum_id']]['password'] != "")
        {
            if($member_id['user_group'] != 5)
                $who = $member_id['name'];
            else
                $who = $_IP;
            
            $check_f_pass = md5($who.$cache_forums[$topic['forum_id']]['password']);
            update_cookie( "LB_password_forum_".$topic['forum_id'], $check_f_pass, 365 );
            header( "Location: {$_SERVER['REQUEST_URI']}" );
            exit();
        }
        elseif ($check_f_pass != $cache_forums[$topic['forum_id']]['password'] AND $cache_forums[$topic['forum_id']]['password'] != "")
            message ($lang_message['access_denied'], $lang_m_b_topic_posts['forum_wrong_pass']);
    }
    
    $lang_location = str_replace("{title}", $topic['title'], $lang_m_b_topic_posts['location_pass']);
    $link_speddbar = speedbar_forum ($topic['forum_id'])."|".$lang_location;

    $lang_location = str_replace("{link}", topic_link($topic['id']), $lang_m_b_topic_posts['location_online_pass']);
    $lang_location = str_replace("{title}", $topic['title'], $lang_location);
    $onl_location = $lang_location;
    
    message ($lang_message['access_denied'], $lang_m_b_topic_posts['forum_pass']);
            
    $tpl->load_template ( 'board/forum_password.tpl' );
    $tpl->tags('{forum_title}', $cache_forums[forum_all_password($topic['forum_id'])]['title']);
    $tpl->tags('{forum_id}', forum_all_password($topic['forum_id']));
    $tpl->compile('content');
    $tpl->clear();
}
elseif($topic['id'])
{  
    cookie_forums_read_update ($topic['forum_id'], $topic['date_last']);    
    member_topic_read_update ($topic['id'], $topic['date_last']);
    
    if ($member_id['lb_subscribe'])
    {
        $subscribe_member = explode (",", $member_id['lb_subscribe']);
        if (in_array($topic['id'], $subscribe_member))
        {
            $topic_subs = $DB->one_select( "send_status", "topics_subscribe", "subs_member = '{$member_id['user_id']}'" );
            if ($topic_subs['send_status'])
            {
                $DB->update("send_status = '0'", "topics_subscribe", "subs_member = '{$member_id['user_id']}' AND topic = '{$topic['id']}'");
            }
        }
    }
    
    $meta_info_text = $topic['forum_id'];
    $meta_info_other = $topic['title'];
    $meta_info_forum = true;
    $rss_link = "?topicid=".$id;
        
    if (isset($_POST['polltopic']) AND $cache_forums[$topic['forum_id']]['allow_poll'] AND $topic['poll_id'])
    {
        if ($LB_flood->isBlock())
            message ($lang_message['flood_control'], str_replace("{time}", $LB_flood->block_time, $lang_message['flood_control_stop']));
        else
        {
            $errors = array();   
            $variants = explode ("\n", $topic['variants']);
            $tp_checked = array();
        
            if($logged)
                $poll_log = $DB->one_select( "id", "topics_poll_logs", "(member_id = '{$member_id['user_id']}' OR ip = '{$_IP}') AND poll_id = '{$topic['poll_id']}'", "LIMIT 1" );
            else
                $poll_log = $DB->one_select( "id", "topics_poll_logs", "ip = '{$_IP}' AND poll_id = '{$topic['poll_id']}'", "LIMIT 1" );
        
            if ($poll_log['id'])
                $errors[] = $lang_m_b_topic_posts['poll_used'];
        
            if ($topic['multiple'])
            {            
                $tp = $_POST['tp'];
                if (!is_array($tp))
                    $errors[] = $lang_m_b_topic_posts['poll_no_answer'];
                else
                {
                    foreach ($tp as $tp_check)
                    {
                        $tp_check = intval($tp_check);
                        $tp_checked[$tp_check] = $tp_check;
                
                        if (!$variants[$tp_check])
                            $errors[] = $lang_m_b_topic_posts['poll_answer_not_found'];
                    }
                
                    $answer = implode("|", $tp_checked);
                }    
            } 
            else
            {
                if (!isset($_POST['tp_1']))
                    $errors[] = $lang_m_b_topic_posts['poll_no_answer'];
                
                $tp_1 = intval($_POST['tp_1']);
                if (!$variants[$tp_1])
                    $errors[] = $lang_m_b_topic_posts['poll_answer_not_found'];
                
                $answer = $tp_1;
                $tp_checked[$tp_1] = $tp_1;
            }
        
            if( ! $errors[0] )
            { 
                $answers = array();
            
                if ($topic['answers'])
                {
                    $answers_old = explode ("|", $topic['answers']);
                    foreach ($answers_old as $vote)
                    {
                        $vote = explode (":", $vote);
                        list($sp, $num) = $vote;
                    
                        if (isset($tp_checked[$sp]))
                        {
                            unset($tp_checked[$sp]);
                            $num += 1;
                        }
                        
                        $result[$sp] = $num;
                    }
                
                    foreach ($result as $key => $old)
                    {
                        $answers[] = $key.":".$old;
                    }
                } 
                        
                if (count($tp_checked))
                {
                    foreach ($tp_checked as $new)
                    {
                        $answers[] = $new.":1";
                    }
                }

                if (count($answers))
                    $answers = implode ("|", $answers);
                else
                    $answers = "";
            
                if (!$logged)
                {
                    $member_id['user_id'] = 0;
                    $member_id['name'] = "";
                }
                                        
                $DB->update("vote_num = vote_num+1, answers = '{$answers}'", "topics_poll", "id = '{$topic['poll_id']}'");
                $DB->insert("poll_id = '{$topic['poll_id']}', ip = '{$_IP}', member_id = '{$member_id['user_id']}', log_date = '{$time}', answer = '{$answer}', member_name = '{$member_id['name']}'", "topics_poll_logs");
                
                header( "Location: ".topic_link($topic['id']) );
                exit();
            }
            else
                message ($lang_message['error'], $errors);
        }
    }
    
    if (isset($_POST['addpost']))
    {
        if ($LB_flood->isBlock())
            message ($lang_message['flood_control'], str_replace("{time}", $LB_flood->block_time, $lang_message['flood_control_stop']));
        elseif ($topic['status'] == "closed" AND !forum_options_topics("0", "reply_close") AND !$cache_group[$member_id['user_group']]['g_reply_close'])
            message ($lang_message['access_denied'], $lang_m_b_topic_posts['access_denied_close']); 
        elseif (!member_publ_access(1))  
            message ($lang_message['access_denied'], $lang_m_b_topic_posts['access_denied_answer']);    
        elseif($cache_config['basket_on']['conf_value'] AND $cache_config['basket_fid']['conf_value'] == $topic['forum_id'])
            message ($lang_message['access_denied'], $lang_m_b_topic_posts['forum_is_basket']);
        elseif (forum_permission($topic['forum_id'], "answer_theme"))
        {
            $errors = array();
                                    
            $_POST['text'] = htmlspecialchars($_POST['text']);
            filters_input ('post');
                        
            if (utf8_strlen($_POST['text']) < intval($cache_config['posts_text_min']['conf_value']))
                $errors[] = str_replace("{min}", intval($cache_config['posts_text_min']['conf_value']), $lang_m_b_topic_posts['post_text_min']);
        
            if (utf8_strlen($_POST['text']) > intval($cache_config['posts_text_max']['conf_value']))
                $errors[] = str_replace("{max}", intval($cache_config['posts_text_max']['conf_value']), $lang_m_b_topic_posts['post_text_max']);
                
            $_POST['text'] = parse_word(html_entity_decode($_POST['text']), $cache_forums[$topic['forum_id']]['allow_bbcode']);
            $text = $DB->addslashes($_POST['text']);
                
            if (!$logged)
            {
                $member_id['user_id'] = 0;
                $member_id['name'] = $DB->addslashes(parse_word($_POST['guest_name'], false, false));
                if (!$member_id['name'] OR utf8_strlen($member_id['name']) > 40)
                    $errors[] = $lang_m_b_topic_posts['no_name'];
                    
                if ($cache_config['security_captcha_posts']['conf_value'])
                {
                    if(!isset($_SESSION['captcha_keystring']) OR $_SESSION['captcha_keystring'] != $_POST['keystring'])
                        $errors[] = $lang_message['captcha'];
                }
        
                if (captcha_dop_check("guest"))
                {
                    $_SESSION['captcha_keystring_a'] = trim($_POST['keystring_dop']);
                    if (!captcha_dop_check_answer())
                        $errors[] = $lang_message['keystring'];
                }         
            }

   	        if( ! $errors[0] )
            {     
                unset($_SESSION['LB_reply_text_'.$topic['id']]);
                
                unset($_SESSION['captcha_keystring']);
                unset($_SESSION['captcha_keystring_a']);
                unset($_SESSION['captcha_keystring_q_num']);
                unset($_SESSION['captcha_keystring_q']);
            
                $cache_forums[$topic['forum_id']]['last_title'] = $topic['title'];     
                $cache_forums[$topic['forum_id']]['last_post_member'] = $member_id['name'];
                $cache_forums[$topic['forum_id']]['last_post_member_id'] = $member_id['user_id'];
                $cache_forums[$topic['forum_id']]['last_post_date'] = $time;
                $cache_forums[$topic['forum_id']]['last_topic_id'] = $topic['id'];
                
                $topic['title'] = $DB->addslashes($topic['title']);
            
                if ($cache_config['posts_paste']['conf_value'] AND $topic['last_post_member'] == $member_id['user_id'] AND ($time - (intval($cache_config['posts_pastemin']['conf_value']) * 60)) <= $topic['date_last'])
                {
                    $post_last = $DB->one_select( "text", "posts", "pid = '{$topic['last_post_id']}'" );
                    
                    $text = $DB->addslashes($post_last['text'])."<br /><br />".$text;
                    
                    $DB->update("text = '{$text}', post_date = '{$time}'", "posts", "pid = '{$topic['last_post_id']}'");
                    $DB->update("date_last = '{$time}'", "topics", "id = '{$topic['id']}'");
                    $DB->update("last_title = '{$topic['title']}', last_post_member = '{$member_id['name']}', last_post_member_id = '{$member_id['user_id']}', last_post_date = '{$time}', last_topic_id = '{$topic['id']}', last_post_id = '{$post_id}'", "forums", "id = '{$topic['forum_id']}'");
                }
                else
                {
                    $DB->insert("topic_id = '{$topic['id']}', new_topic = '0', text = '{$text}', post_date = '{$time}', post_member_id = '{$member_id['user_id']}', post_member_name = '{$member_id['name']}', ip = '{$_IP}'", "posts");
                    $post_id = $DB->insert_id();
                    $DB->update("last_post_member = '{$member_id['user_id']}', member_name_last = '{$member_id['name']}', last_post_id = '{$post_id}', date_last = '{$time}', post_num = post_num+1", "topics", "id = '{$topic['id']}'");
                    $DB->update("last_title = '{$topic['title']}', last_post_member = '{$member_id['name']}', last_post_member_id = '{$member_id['user_id']}', last_post_date = '{$time}', last_topic_id = '{$topic['id']}', posts = posts+1, last_post_id = '{$post_id}'", "forums", "id = '{$topic['forum_id']}'");

                    $cache_forums[$topic['forum_id']]['posts'] += 1;
                
                    if ($cache_forums[$topic['forum_id']]['postcount'])
                    {
                        $DB->prefix = DLE_USER_PREFIX;
                        $DB->update("posts_num = posts_num+1", "users", "user_id = '{$member_id['user_id']}'");
                    }
                }
                
                $cache_forums[$topic['forum_id']]['last_post_id'] = $post_id; 
                
                topic_do_subscribe ($topic['id'], $topic['title']);
                
                cookie_forums_read_update ($topic['forum_id'], $time);
                member_topic_read_update ($topic['id'], $time);
        
                $cache->update("forums", $cache_forums);
                header( "Location: ".topic_link($topic['id'], true) );
                exit();
            }
            else
                message ($lang_message['error'], $errors, 1); 
        } 
        else
            message ($lang_message['access_denied'], $lang_m_b_topic_posts['access_denied_forum_answer']);     
    }
    
    $link_speddbar = speedbar_forum ($topic['forum_id'])."|".str_replace("{title}", $topic['title'], $lang_m_b_topic_posts['location']);
    
    $lang_location = str_replace("{link}", topic_link($topic['id']), $lang_m_b_topic_posts['location_online']);
    $lang_location = str_replace("{title}", $topic['title'], $lang_location);
    $onl_location = $lang_location;
    
    $where = "";
    
    if (isset ( $_REQUEST['page'] ))
	   $page = intval ( $_GET['page'] );
    else
	   $page = 0;

    $link_on_post = navigation_link("board", "topic", $id, $dopnav).$page;

    if ($page < 0)
	   $page = 0;

    if ($page)
    {
	   $page = $page - 1;
	   $page = $page * $cache_config['topic_post_page']['conf_value'];
    }

    $i = $page;
    
    $dopnav = "";
    $go_show = "";
    
    if(!forum_options_topics($topic['forum_id'], "hideshow"))
        $where = "AND hide = '0'";
    else
    {
        if (isset($_GET['go']) AND $_GET['go'] == "hide")
        {
            $where = "AND hide = '1'";
            $dopnav = "hide=topics";
            $go_show = "hide";
        }   
    }
        
    $nav = $DB->one_select( "COUNT(*) as count", "posts", "topic_id = '{$id}' AND fixed = '0' {$where}");
    $nav_all = $nav['count'];
    $DB->free($nav);
    
    $find_words = $DB->addslashes(htmlspecialchars(strip_data(urldecode($_REQUEST['s']))));;
    if ($find_words)
    {
        if ($dopnav)
            $dopnav .= "&s=".urlencode($find_words);
        else
            $dopnav = "s=".urlencode($find_words);
    }
    
    $link_nav = navigation_link("board", "topic", $id, $dopnav);
    
    if ($nav_all > $cache_config['topic_post_page']['conf_value'])
    {
        include LB_CLASS.'/navigation_board.php';
        $navigation = new navigation;
        $navigation->creat($page, $nav_all, $cache_config['topic_post_page']['conf_value'], $link_nav, "5");

        $tpl->load_template ( 'navigation.tpl' );
        $tpl->tags('{pages}', $navigation->result);
        $tpl->compile('navigationt');
        
        unset($navigation);
    }
    else
        $tpl->result['navigationt'] = "";
        
    if (isset($_GET['go']) AND $_GET['go'] == "last")
    {
        $number_pages = ceil( $nav_all / $cache_config['topic_post_page']['conf_value'] );
        header( "Location: ".$link_nav.$number_pages."#post".$topic['last_post_id'] );
        exit();
    }
    
    $_SESSION['Get_Next_Post_Buttom'] = 0;

    include LB_CLASS.'/posts_out.php';
    $LB_posts = new LB_posts;
        
    if ($topic['postfixed'])
    {
        $DB->prefix = array ( 1=> DLE_USER_PREFIX );
        $LB_posts->query = $DB->join_select( "p.*, mo.mo_id, mo.mo_date, u.name, user_id, banned, user_group, foto, signature, posts_num", "LEFT", "posts p||users u||members_online mo", "p.post_member_id=u.user_id||u.user_id=mo.mo_member_id", "topic_id = '{$id}' AND fixed = '1' {$where}", "ORDER by fixed DESC, post_date ASC" );
        $j = 0;
        $LB_posts->Data_out("board/topic_posts.tpl", "posts_fixed", $topic, true, false, true);
    }
    
    $DB->prefix = array ( 1=> DLE_USER_PREFIX );
    $LB_posts->query = $DB->join_select( "p.*, mo.mo_id, mo.mo_date, u.name, user_id, banned, user_group, foto, signature, posts_num", "LEFT", "posts p||users u||members_online mo", "p.post_member_id=u.user_id||u.user_id=mo.mo_member_id", "topic_id = '{$id}' AND fixed = '0' {$where}", "ORDER by fixed DESC, post_date ASC LIMIT ".$page.", ".$cache_config['topic_post_page']['conf_value'] );
    $LB_posts->Data_out("board/topic_posts.tpl", "posts", $topic);
    
    unset($LB_posts);
        
    if ($topic['status'] == "closed" AND !forum_options_topics("0", "reply_close") AND !$cache_group[$member_id['user_group']]['g_reply_close'])
    {
   	    $tpl->load_template( 'message.tpl' );
        $tpl->tags( '{title}', $lang_m_b_topic_posts['topic_title_close'] );
        $tpl->tags( '{message}', $lang_m_b_topic_posts['topic_info_close'] );
        $tpl->compile( 'form' );
        $tpl->clear();
    }
    elseif(!forum_permission($topic['forum_id'], "answer_theme"))
    {
        $tpl->load_template( 'message.tpl' );
        $tpl->tags( '{title}', $lang_message['access_denied'] );        
        $tpl->tags( '{message}', $lang_m_b_topic_posts['topic_info_answer'] );
        $tpl->compile( 'form' );
        $tpl->clear(); 
    }
    elseif (!member_publ_access(1))
    {
   	    $tpl->load_template( 'message.tpl' );
        $tpl->tags( '{title}', $lang_message['access_denied'] );                
        $tpl->tags( '{message}', str_replace("{info}", member_publ_info(), $lang_m_b_topic_posts['topic_info_publ']) );
        $tpl->compile( 'form' );
        $tpl->clear();
    }
    elseif($cache_config['basket_on']['conf_value'] AND $cache_config['basket_fid']['conf_value'] == $topic['forum_id'])
    {
        $tpl->load_template( 'message.tpl' );
        $tpl->tags( '{title}', $lang_message['access_denied'] );        
        $tpl->tags( '{message}', $lang_m_b_topic_posts['topic_info_basket'] );
        $tpl->compile( 'form' );
        $tpl->clear();
    }
    elseif (!$cache_group[$member_id['user_group']]['g_reply_topic'] AND $topic['member_id_open'] != $member_id['user_id'] AND $logged)
    {
   	    $tpl->load_template( 'message.tpl' );
        $tpl->tags( '{title}', $lang_message['access_denied'] );
        $tpl->tags( '{message}', str_replace("{group}", $cache_group[$member_id['user_group']]['g_title'], $lang_m_b_topic_posts['topic_info_answer_other']) );
        $tpl->compile( 'form' );
        $tpl->clear();
    }
    else
    {
        if ($topic['status'] == "closed" AND (forum_options_topics("0", "reply_close") OR $cache_group[$member_id['user_group']]['g_reply_close']))
        {
   	        $tpl->load_template( 'message.tpl' );
            $tpl->tags( '{title}', $lang_message['warning'] );
            $tpl->tags( '{message}', $lang_m_b_topic_posts['topic_info_close_answer'] );
            $tpl->compile( 'form' );
            $tpl->clear();
        }
        
        $tpl->load_template ( 'board/reply_fast.tpl' );
        
        if ($cache_forums[$topic['forum_id']]['allow_bbcode'])
        {
            require LB_MAIN . '/components/scripts/bbcode/bbcode.php';
            $tpl->tags('{bbcode}', $bbcode_script.$bbcode); 
        }
        else
            $tpl->tags('{bbcode}', "");
            
        if ($cache_config['security_captcha_posts']['conf_value'] AND !$logged)
        {
            $tpl->tags_blocks("captcha");
            $tpl->tags( '{captcha}', "<img id=\"recaptcha_img\" src=\"".$redirect_url."components/class/kcaptcha/kcaptcha.php\"><br /><a href=\"#\" id=\"recaptcha\">".$lang_message['change_captcha']."</a>" );
        }
        else
            $tpl->tags_blocks("captcha", false);
        
        if (captcha_dop_check("guest") AND !$logged)
        {
            $tpl->tags_blocks("captcha_dop");
            $tpl->tags( '{captcha_dop}', captcha_dop());
        }
        else
            $tpl->tags_blocks("captcha_dop", false);
            
        $tpl->tags('{reply_link}', reply_link($topic['id']));
        $tpl->tags('{tid}', $topic['id']);
        $tpl->compile('form');
        $tpl->clear();
    }        
        
    if($cache_forums[$topic['forum_id']]['allow_poll'] AND $topic['poll_id'])
    {
        $poll_check = false;
        if ($cache_config['topic_polllog']['conf_value'])
        {
            if ($logged)
                $poll_log_where = "(ip = '{$_IP}' OR member_id = '{$member_id['user_id']}')";
            else
                $poll_log_where = "ip = '{$_IP}'";
                
            $poll_log = $DB->one_select( "*", "topics_poll_logs", "poll_id = '{$topic['poll_id']}' AND {$poll_log_where}" );
            
            if ($poll_log['id'])
                $poll_check = true;
                
            $DB->free($poll_log);
        }
        
        $tpl->load_template ( 'board/topic_poll.tpl' );
        $tpl->tags('{title}', $topic['p_title']); 
        $tpl->tags('{question}', $topic['question']); 
        $tpl->tags('{tid}', $topic['id']);
        
        if ($poll_check)
        {
            $tpl->tags('{variants}', topic_poll_logs($topic['variants'], $topic['answers'], $topic['vote_num']));  
            $tpl->tags_blocks("result", false);
            $tpl->tags_blocks("vote", false);
        }
        elseif (isset($_GET['poll']) AND $_GET['poll'] == "yes")
        {
            $tpl->tags('{variants}', topic_poll_logs($topic['variants'], $topic['answers'], $topic['vote_num']));
            $tpl->tags_blocks("result");
            $tpl->tags_blocks("vote", false);
            $tpl->tags('{vote_link}', $link_nav);
            $tpl->tags('{poll_link}', $link_nav);
        }
        else
        {
            $tpl->tags_blocks("vote");
            $tpl->tags_blocks("result", false);
            $tpl->tags('{poll_link}', $link_nav."&poll=yes");
            $tpl->tags('{variants}', topic_poll_variants($topic['variants'], $topic['multiple']));
            $tpl->tags('{poll_link}', $_SERVER['REQUEST_URI']."&poll=yes");
        }
        $tpl->compile('poll');  
        
        $tpl->result['poll'] = "<div id=\"topic_vote_jq\">".$tpl->result['poll']."</div>"; 
    }
    else
        $tpl->result['poll'] = "";
    
    $tpl->load_template ( 'board/topic_posts_global.tpl' );    
    $tpl->tags('{topic_title}', $topic['title']);
    
    if (isset($tpl->result['posts_fixed']))
        $tpl->tags('{posts_fixed}', $tpl->result['posts_fixed']);
    else
        $tpl->tags('{posts_fixed}', "");
    
    $tpl->tags('{posts}', $tpl->result['posts']);
    $tpl->tags('{poll}', $tpl->result['poll']);
    $tpl->tags('{fast_forum}', ForumsList($topic['forum_id'], 0, "", "", true));
    
    $tpl->tags_blocks("moder", forum_options_topics_mas($topic['forum_id'], $topic['id'], "check"));
    if(forum_options_topics_mas($topic['forum_id'], $topic['id'], "check"))
    {
        $tpl->tags('{moder_comm}', forum_options_topics_mas($topic['forum_id'], $topic['id'], "posts"));
        $tpl->tags('{moder_topic}', forum_options_topics_mas($topic['forum_id'], $topic['id'], "topic"));
        $tpl->block( "'\\[author_topic\\](.*?)\\[/author_topic\\]'si", "" );
    }
    else
    { 
        if($logged AND $topic['member_id_open'] == $member_id['user_id'] AND forum_options_topics_author("check"))
        {
            $tpl->tags_blocks("author_topic");
            $tpl->tags('{moder_topic}', forum_options_topics_author());
        }
        else
            $tpl->tags_blocks("author_topic", false);
    }
     
    $tpl->tags('{id}', $topic['id']);
    $tpl->tags_blocks("posts_out");
    
    $tpl->tags('{form}', $tpl->result['form']);
    $tpl->tags('{pages}', $tpl->result['navigationt']);
    
    $favorite_add = true;
    $tpl->tags('[link_favorite]', "<a href=\"".topic_favorite($topic['id'])."\" onclick=\"AddDelFavorite('".$topic['id']."');return false;\">" );
    $tpl->tags('[/link_favorite]', "</a>" );
    
    if ($member_id['lb_favorite'] != "")
    {
        $favorites = explode (",", $member_id['lb_favorite']);
        if (in_array($topic['id'], $favorites))
        {
            $favorite_add = false;
            $tpl->tags('{favorite_title}', $lang_m_b_topic_posts['do_fav_del']);
        }
    }
    
    if ($favorite_add) $tpl->tags('{favorite_title}', $lang_m_b_topic_posts['do_fav_add']);
    
    $subscribe_add = true;
    $tpl->tags('[link_subscribe]', "<a href=\"".topic_subscribe($topic['id'])."\" onclick=\"AddDelSubscribe('".$topic['id']."');return false;\">" );
    $tpl->tags('[/link_subscribe]', "</a>" );
    
    if ($member_id['lb_subscribe'] != "")
    {
        $subscribe = explode (",", $member_id['lb_subscribe']);
        if (in_array($topic['id'], $subscribe))
        {
            $subscribe_add = false;
            $tpl->tags('{subscribe_title}', $lang_m_b_topic_posts['do_subs_del']);
        }
    }
    
    if ($subscribe_add) $tpl->tags('{subscribe_title}', $lang_m_b_topic_posts['do_subs_add']);
        
    if ($cache_config['topic_sharelink']['conf_value'] AND count($cache_sharelink) >= 1)
    {
        $tpl->tags_blocks("share_links");
        $tpl->tags('{share_links}', share_links($topic['id'], $topic['title']));
    }
    else
        $tpl->tags_blocks("share_links", false);
        
    $tpl->compile('content');
    $tpl->clear();
    
    $script_update = "";
    $script_new_posts = "";
    
    $time_out_new_post = intval($cache_config['posts_check_new_auto']['conf_value']) * 1000;
    $number_pages = ceil( $nav_all / $cache_config['topic_post_page']['conf_value'] );
    $script_new_posts_active = false;
    
    if ($number_pages == intval($_GET['page']))
        $script_new_posts_active = true;
    
    if (intval($cache_config['posts_get_next_page']['conf_value']))
    {
        $script_update = '
    <span id="script_update">
    <script type="text/javascript">
        $(document).ready(function(){
                    
            if ($("span#newpost-out").length == "0")
            {
                show_message("3", LB_lang[\'error\'], LB_lang[\'no_object\'] + "span#newpost-out");
            }
            
            var top_s = 0;
            var loaded_content = false;
            var loaded_content_hieght = $(window).scrollTop() + $(window).height();
            var loaded_content_page = "'.intval($_GET['page']).'";
            var loaded_content_post = "'.$i.'";
            var loaded_content_end = "0";
            var go_show = "'.$go_show.'";
                    
            function check(){
                
                if ($("span#newpost-out").length == "0")
                {
                    show_message("3", LB_lang[\'error\'], LB_lang[\'no_object\'] + "span#newpost-out");
                    return false;
                }
                
                top_s = $("#newpost-out").offset().top;
                loaded_content_hieght = $(window).scrollTop() + $(window).height() + 120;
                
                if ( !loaded_content && loaded_content_hieght > top_s ) {
                    show_loading_message();
                    
                    $.get(LB_root + "components/scripts/ajax/get_next_page_posts.php", {tid:"'.$topic['id'].'", page:loaded_content_page, last_post_i:loaded_content_post, go:go_show, content_end:loaded_content_end}, function(data){
                        $("span#newpost-out").before(data);
                        remove_loading_message();
                    });
                    
                    Error_AJAX_jQ ("span#newpost-out");
                    loaded_content = true;
                }
            }
            $(window).scroll(check);
            check();  
        });    
    </script>
    </span>';
    } 

    if (intval($cache_config['posts_check_new_auto']['conf_value']) AND !intval($cache_config['posts_get_next_page']['conf_value']) AND $topic['status'] != "closed" AND $script_new_posts_active)
    {
        $script_new_posts = '
            <script type="text/javascript">
            var date_now = "'.$time.'";

            function check_new_posts()
            {
                show_loading_message(LB_lang[\'search_new_answers\']);
                        
                $.get(LB_root + "components/scripts/ajax/check_new_posts.php", {tid:"'.$topic['id'].'", date_now:date_now}, function(data){
                    $("span#newpost-out").before(data);
                    remove_loading_message();
                });
                   
                Error_AJAX_jQ ("span#newpost-out");     
            }
                
            setInterval(check_new_posts, '.$time_out_new_post.');
            
            </script>';
    }
    
    $script_word = "
    <script type=\"text/javascript\">
    $(window).load(function(){
        $(\"[id^=post-id-]\").highlight('".stripslashes($find_words)."');
    });
    </script>";
    
    if ($find_words) $tpl->result['content'] .= $script_word.$script_update.$script_new_posts;
    else $tpl->result['content'] .= $script_update.$script_new_posts;
    
    if ($_SESSION['LB_read_topic'] != $topic['id'])
    {            
        $_SESSION['LB_read_topic'] = $topic['id'];      
        $DB->update("views = views + 1", "topics", "id='{$topic['id']}'");
    }
}
else
    message ($lang_m_b_topic_posts['not_found_title'], $lang_m_b_topic_posts['not_found_info'], 1);

?>