<?php

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

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

if($_SERVER['HTTP_X_REQUESTED_WITH'] != "XMLHttpRequest")
    exit ("Error.");

@session_start ();

@error_reporting ( E_ERROR );
@ini_set ( 'display_errors', true );
@ini_set ( 'html_errors', false );
@ini_set ( 'error_reporting', E_ERROR );

define ( 'LogicBoard', true );
define ( 'LB_MAIN', realpath("../../../") );

define ( 'LB_CLASS', LB_MAIN . '/components/class' );
define ( 'LB_GLOBAL', LB_MAIN . '/components/global' );
define ( 'LB_CONFIG', LB_MAIN . '/components/config' );
define ( 'LB_MODULES', LB_MAIN . '/components/modules' );
define ( 'LB_UPLOADS', LB_MAIN . '/uploads/' );

require_once LB_CLASS . '/database.php';
include_once LB_CONFIG . '/board_db.php';

if (get_magic_quotes_gpc())
{
    include_once LB_CLASS. "/magic_quotes_gpc.php";
    $mq_gpc = new mq_gpc();
    $mq_gpc->del_slashes();
    unset($mq_gpc);  
}

$_IP = $_SERVER['REMOTE_ADDR'];

require_once LB_CLASS . '/cache.php';
require_once LB_GLOBAL . '/creat_cache.php';

$redirect_url = $cache_config['general_site']['conf_value'];
$onl_limit = $time - (intval($cache_config['online_time']['conf_value']) * 60);

require_once LB_GLOBAL . '/functions.php';
require_once LB_GLOBAL . '/login.php';

if ($cache_config['general_close']['conf_value'] AND $cache_group[$member_id['user_group']]['g_show_close_f'] != 1)
{
    stop_script("Offline.");
}

if (!$logged)
{
    stop_script("Not logged in.");
}

$banned_ip = LB_banned("ip", $_IP);
$banned_name = LB_banned("user_id", $member_id['user_id']);
if ($banned_ip OR $banned_name)
{
    stop_script("Banned.");
}

if (!$_GET['secret_key'] OR $_GET['secret_key'] != $secret_key)
{
    stop_script("Wrong secret key.");
}
else
{
    $lang_s_a_subscribe = language_forum ("board/scripts/ajax/subscribe");
    header( "Content-type: text/html; charset=windows-1251" );
    
    function check_access_fav($f_id = 0, $hide = 0, $status = "open")
    {
        global $lang_s_a_subscribe;
        
        $message = "";
        
        if(!forum_permission($f_id, "read_forum"))
            $message = $lang_s_a_subscribe['access_denied_forum'];
        elseif (!forum_permission($f_id, "read_theme"))
            $message = $lang_s_a_subscribe['access_denied_read'];
        elseif(forum_all_password($f_id))
            $message = $lang_s_a_subscribe['access_denied_pass'];
        elseif(!forum_options_topics($f_id, "hideshow") AND $hide)
            $message = $lang_s_a_subscribe['access_denied_hide'];
        elseif($status == "closed")
             $message = $lang_s_a_subscribe['access_denied_close'];
        return $message;        
    }    
    
    $id = intval($_GET['tid']);
    
    $topic = $DB->one_select( "id, forum_id, hiden, status", "topics", "id = '{$id}'");
    if (!$topic['id'])
        echo show_jq_message ("3", $lang_s_a_subscribe['error_title'], $lang_s_a_subscribe['error_info']);
    else
    {
        $find_error = false;
        $error = "";
        $act = $lang_s_a_subscribe['act_1'];
        
        $topic_subs = $DB->one_select( "id", "topics_subscribe", "topic = '{$id}' AND subs_member = '{$member_id['user_id']}'");
        
        if (!$topic_subs['id'])
        {
            if (!check_access_fav($topic['forum_id'], $topic['hiden'], $topic['status']))
                $DB->insert("subs_member = '{$member_id['user_id']}', topic = '{$id}', date = '{$time}'", "topics_subscribe");
            else
            {
                $error .= check_access_fav ($topic['forum_id'], $topic['hiden'], $topic['status']);
                $find_error = true;
            }
        }
        else
        {
            $DB->delete("id = '{$topic_subs['id']}'", "topics_subscribe");
        }
        
        if (!$find_error)  
        {
            if ($member_id['lb_subscribe'] == "")
            {
                if (!check_access_fav($topic['forum_id'], $topic['hiden'], $topic['status']))
                {
                    $DB->prefix = DLE_USER_PREFIX;
                    $DB->update("lb_subscribe = '{$id}'", "users", "user_id = '{$member_id['user_id']}'");
                }
                else
                {
                    $error .= check_access_fav ($topic['forum_id'], $topic['hiden'], $topic['status']);
                    $find_error = true;
                }
            }
            else
            {
                $subscribe = explode (",", $member_id['lb_subscribe']);
                if (in_array($id, $subscribe))
                {
                    $key_fav = array_search($id, $subscribe);
                    unset($subscribe[$key_fav]);
                    $act = $lang_s_a_subscribe['act_2'];
                }
                else
                {
                    if (!check_access_fav($topic['forum_id'], $topic['hiden'], $topic['status']))
                        array_push ($subscribe, $id);
                    else
                    {
                        $error .= check_access_fav ($topic['forum_id'], $topic['hiden'], $topic['status']);
                        $find_error = true;
                    }
                }
                $subs = implode (",", $subscribe);
                $DB->prefix = DLE_USER_PREFIX;
                $DB->update("lb_subscribe = '{$subs}'", "users", "user_id = '{$member_id['user_id']}'");
            }
        }
        
        if ($find_error)
            echo show_jq_message ("3", $lang_s_a_subscribe['error_title'], $error);
        else
            echo show_jq_message ("1", $lang_s_a_subscribe['done_title'], str_replace("{act}", $act, $lang_s_a_subscribe['done_info']));
    }
    
    stop_script();
}    
?>