<?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_reply = language_forum ("board/modules/board/reply");

$topic = $DB->one_select( "*", "topics", "id = '{$id}'" );

function here_loaction ()
{
    global $link_speddbar, $onl_location, $lang_m_b_reply;
    
    $link_speddbar = speedbar_forum (0)."|".$lang_m_b_reply['location_hide'];
    $onl_location = $lang_m_b_reply['location_online_hide'];
}

function here_loaction_2 ()
{
    global $link_speddbar, $onl_location, $lang_m_b_reply;
        
    $location_access_denied = str_replace("{link}", topic_link($topic['id']), $lang_m_b_reply['location_access_denied']);
    $location_access_denied = str_replace("{title}", $topic['title'], $location_access_denied);
    $link_speddbar = speedbar_forum ($topic['forum_id'])."|".$location_access_denied;

    $location_access_denied = str_replace("{link}", topic_link($topic['id']), $lang_m_b_reply['location_online_access_denied']);
    $location_access_denied = str_replace("{title}", $topic['title'], $location_access_denied);
    $onl_location = $location_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_reply['access_denied_group_f']), 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_reply['access_denied_group_r']), 1);
}
elseif (!$cache_group[$member_id['user_group']]['g_reply_topic'] AND $topic['member_id_open'] != $member_id['user_id'] AND $logged)
{
    here_loaction ();
    message ($lang_message['access_denied'], str_replace("{group}", $cache_group[$member_id['user_group']]['g_title'], $lang_m_b_reply['access_denied_group_a_other']), 1);
}
elseif ($topic['id'] AND !forum_permission($topic['forum_id'], "answer_theme"))
{
    here_loaction_2 ();
    message ($lang_message['access_denied'], str_replace("{group}", $cache_group[$member_id['user_group']]['g_title'], $lang_m_b_reply['access_denied_group_a_all']), 1);
}
elseif ($topic['status'] == "closed" AND !forum_options_topics("0", "reply_close") AND !$cache_group[$member_id['user_group']]['g_reply_close'])
{
    here_loaction_2 ();
    message ($lang_m_b_reply['topic_close'], $lang_m_b_reply['topic_close_info'], 1);
}
elseif ($topic['id'] AND $topic['hiden'] AND !forum_options_topics($topic['forum_id'], "hideshow"))
{
    here_loaction ();
    message ($lang_message['access_denied'], str_replace("{group}", $cache_group[$member_id['user_group']]['g_title'], $lang_m_b_reply['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_reply['wrong_password_forum']);
    }
    
    here_loaction_2 ();
    message ($lang_message['access_denied'], $lang_m_b_reply['write_password_forum']);
            
    $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 (!member_publ_access(1))
{
    here_loaction_2 ();
    
    $message_arr = array();
    $message_arr[] = $lang_m_b_reply['access_denied_publ_access'];
    $message_arr[] = member_publ_info();

    message ($lang_message['access_denied'], $message_arr, 1);   
}
elseif($cache_config['basket_on']['conf_value'] AND $cache_config['basket_fid']['conf_value'] == $topic['forum_id'])
{
    here_loaction_2 ();
    message ($lang_message['access_denied'], $lang_m_b_reply['forum_basket'], 1);
}
elseif($topic['id'])
{
    cookie_forums_read_update ($topic['forum_id'], $topic['date_last']);
    member_topic_read_update ($topic['id'], $topic['date_last']);
    
    $preview = 0;
    $text = "";
    $guest_id = 0;
    $guest_name = "";
    
    $meta_info_text = $topic['forum_id'];
    $meta_info_other = str_replace("{title}", $topic['title'], $lang_m_b_reply['meta_info']);
    $meta_info_forum = true;

    include_once LB_CLASS. "/upload_files.php";
    $LB_upload = new LB_Upload();
    
    $small_img = "";

    if (isset($_POST['preview']))
    {
        $preview = 1;
    
        $_POST['text'] = htmlspecialchars($_POST['text']);
        filters_input ('post');
   
        $text = parse_word(html_entity_decode($_POST['text']), $cache_forums[$topic['forum_id']]['allow_bbcode']);
                  
        $_SESSION['LB_reply_text_'.$topic['id']] = parse_back_word($text);
                 
        if (strpos($text, "[attachment=") !== false)
        {                            
            $text = show_attach ($text, "0");
        }
        
$small_img = <<<SCRIPT

    <script type="text/javascript">
    $(window).load(function(){
        Resize_img();
    });
    </script>

SCRIPT;
    
        if (!$logged)
        {
            $guest_name = words_wilter(trim(htmlspecialchars($_POST['guest_name'])));
        }
    }
    elseif (isset($_POST['add_file']))
    {      
        $preview = 2;
        $upload_status = $LB_upload->Uploading($topic['forum_id'], $topic['id']);
        if ($upload_status)
            message ($lang_message['error'], $upload_status);
            
        if (!$logged)
        {
            $guest_name = words_wilter(trim(htmlspecialchars($_POST['guest_name'])));
        }
            
        $_SESSION['LB_reply_text_'.$topic['id']] = $_POST['text'];
        
        $del_file = $_POST['del_file'];
        if (is_array($del_file) AND $del_file[0] != "")
        {
            foreach ($del_file as $value)
            {
                $value = intval($value);
                if ($value)
                    $del_status = $LB_upload->Del_Record($value, $secret_key); 
            }
            if ($del_status)
                message ($lang_message['error'], $del_status);
        }
        
    }
    elseif ($LB_flood->isBlock() AND isset($_POST['addpost']))
        message ($lang_message['flood_control'], str_replace("{time}", $LB_flood->block_time, $lang_message['flood_control_stop']));
    elseif (isset($_POST['addpost']))
    {
        $errors = array();
        
        $del_file = $_POST['del_file'];
        if (is_array($del_file) AND $del_file[0] != "")
        {
            foreach ($del_file as $value)
            {
                $value = intval($value);
                if ($value)
                    $del_status = $LB_upload->Del_Record($value, $secret_key); 
            }
            if ($del_status)
                $errors[] = $del_status;
        }
                
        $_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_reply['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_reply['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(words_wilter(trim($_POST['guest_name'])));
            if (!$member_id['name'] OR utf8_strlen($member_id['name']) > 40)
                $errors[] = $lang_m_b_reply['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] )
        {              
            $attachment_mas = $LB_upload->Add_attachments($topic['id'], 0, $member_id['user_id'], $text);
            $text = $attachment_mas[0];
                                    
            if (is_array($del_file) AND $del_file[0] != "")
            {
                foreach ($del_file as $value)
                {
                    $value = intval($value);
                    if (in_array($value, $attachment_mas[1]))
                        unset ($attachment_mas[1][$value]);
                }
            }
                        
            $a_id = implode(",", $attachment_mas[1]);
            
            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}', attachments = '{$a_id}', 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']}'");
                
                if (forum_permission($topic['forum_id'], "upload_files") AND $logged AND $a_id != "")
                    $DB->update("file_pid = '{$topic['last_post_id']}'", "topics_files", "file_tid = '{$topic['id']}' AND file_pid = '0' AND file_mid = '{$member_id['user_id']}'");
            }
            else
            {
                $DB->insert("topic_id = '{$topic['id']}', new_topic = '0', text = '{$text}', attachments = '{$a_id}', 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']}'");
                }
                 
                if (forum_permission($topic['forum_id'], "upload_files") AND $logged AND $a_id != "")   
                    $DB->update("file_pid = '{$post_id}'", "topics_files", "file_tid = '{$topic['id']}' AND file_pid = '0' AND file_mid = '{$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);
    }
        
    $lang_location = str_replace("{link}", topic_link($topic['id']), $lang_m_b_reply['location']);
    $lang_location = str_replace("{title}", $topic['title'], $lang_location);
    $link_speddbar = speedbar_forum ($topic['forum_id'])."|".$lang_location;
    
    $lang_location = str_replace("{link}", topic_link($topic['id']), $lang_m_b_reply['location_online']);
    $lang_location = str_replace("{title}", $topic['title'], $lang_location);
    $onl_location = $lang_location;
    
    if(!forum_options_topics($topic['forum_id'], "hideshow")) $where = "AND hide = '0'";
    else $where = "";
    
    include LB_CLASS.'/posts_out.php';
    $LB_posts = new LB_posts;
    
    $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}' {$where}", "ORDER by post_date DESC LIMIT 10" );
    $LB_posts->Data_out("board/reply_last_posts.tpl", "posts", $topic);
    
    unset($LB_posts);

    if ($topic['status'] == "closed" AND (forum_options_topics("0", "reply_close") OR $cache_group[$member_id['user_group']]['g_reply_close']))
         message ($lang_message['warning'], $lang_m_b_reply['close_open']);
  
    $tpl->load_template ( 'board/reply_global.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 ($preview == 1) $tpl->tags_blocks("preview");
    else $tpl->tags_blocks("preview", false);
    
    $tpl->tags('{text_pr}', $text.$small_img);     
     
    if (isset($_REQUEST['pid']) AND intval($_REQUEST['pid']) AND $preview != 1)
    {   
        $post_q = $DB->one_join_select( "p.pid, p.text, p.post_date, p.post_member_name, t.forum_id, t.hiden", "LEFT", "posts p||topics t", "p.topic_id=t.id", "p.pid = '".intval($_REQUEST['pid'])."'" );
        if ($post_q['pid'])
        {            
            if (!forum_permission($post_q['forum_id'], "read_theme") OR !forum_permission($post_q['forum_id'], "read_forum") OR forum_all_password($post_q['forum_id']))
                $_SESSION['LB_reply_text_'.$topic['id']] = "";
            elseif ($post_q['hiden'] AND !forum_options_topics($post_q['forum_id'], "hideshow"))
                $_SESSION['LB_reply_text_'.$topic['id']] = "";
            else
                $_SESSION['LB_reply_text_'.$topic['id']] = "[quote=".$post_q['post_member_name']."|".date("d.m.Y, H:i", $post_q['post_date'])."]".parse_back_word($post_q['text'])."[/quote]";
        }
    }
        
    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);
    
    if (isset($_SESSION['LB_reply_text_'.$topic['id']]) AND $_SESSION['LB_reply_text_'.$topic['id']] != "")
        $tpl->tags('{quote}', $_SESSION['LB_reply_text_'.$topic['id']]);
    else
        $tpl->tags('{quote}', "");
        
    $tpl->tags('{guest_name}', $guest_name);
    $tpl->tags('{tid}', $topic['id']);
        
    if (forum_permission($topic['forum_id'], "upload_files") AND $logged)
    {
        $tpl->tags_blocks("attachment");
        $tpl->tags('{attachments}', $LB_upload->Out_link($topic['id'], 0, $member_id['user_id'], 1));
    }
    else
        $tpl->tags_blocks("attachment", false);
    
$small_img = <<<SCRIPT

    <script type="text/javascript">
    $(window).load(function(){
        Resize_img();
    });
    </script>
SCRIPT;
    
    $tpl->tags('{posts}', $tpl->result['posts'].$small_img);
    $tpl->tags('{topic_title}', $topic['title']);
    
    $tpl->compile('content');
    $tpl->clear();
    
    unset ($LB_upload);
}
else
    message ($lang_m_b_reply['not_found'], $lang_m_b_reply['not_found_info'], 1);

?>