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

if (!$_GET['secret_key'] OR $_GET['secret_key'] != $secret_key)
    message ($lang_message['access_denied'], $lang_message['secret_key'], 1);
else
{
    function check_access_fav($f_id = 0, $hide = 0, $status = "open")
    {
        global $lang_m_b_subscribe, $member_id, $cache_group;
        
        $message = "";
        
        if(!forum_permission($f_id, "read_forum"))
        {
            $message = str_replace("{group}", $cache_group[$member_id['user_group']]['g_title'], $lang_m_b_subscribe['access_denied_group_f']);
        }
        elseif (!forum_permission($f_id, "read_theme"))
        {
             $message = str_replace("{group}", $cache_group[$member_id['user_group']]['g_title'], $lang_m_b_subscribe['access_denied_group_t']);
        }
        elseif(forum_all_password($f_id))
        {
             $message = $lang_m_b_subscribe['forum_pass'];
        }
        elseif(!forum_options_topics($f_id, "hideshow") AND $hide)
        {
             $message = str_replace("{group}", $cache_group[$member_id['user_group']]['g_title'], $lang_m_b_subscribe['hide_topic']);
        }
        elseif($status == "closed")
        {
             $message = $lang_m_b_subscribe['close_topic'];
        }

        return $message;        
    }    
    
    $topic = $DB->one_select( "id, forum_id, hiden, status", "topics", "id = '{$id}'");
    if (!$topic['id'])
        message ($lang_message['error'], $lang_m_b_subscribe['not_found'], 1);
    else
    {        
        $lang_location = str_replace("{link}", topic_link($topic['id']), $lang_m_b_subscribe['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_subscribe['location_online']);
        $lang_location = str_replace("{title}", $topic['title'], $lang_location);
        $onl_location = $lang_location;
        $find_error = false;
    
        $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
            {
                message ($lang_message['error'], check_access_fav ($topic['forum_id'], $topic['hiden'], $topic['status']), 1);
                $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
                {
                    message ($lang_message['error'], check_access_fav ($topic['forum_id'], $topic['hiden'], $topic['status']), 1);
                    $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]);
                }
                else
                {
                    if (!check_access_fav($topic['forum_id'], $topic['hiden'], $topic['status']))
                        array_push ($subscribe, $id);
                    else
                    {
                        message ($lang_message['error'], check_access_fav ($topic['forum_id'], $topic['hiden'], $topic['status']), 1);
                        $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)
            header( "Location: ".topic_link($id, true) );
    }
}    
?>