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

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)
    {
        global $lang_m_b_favorite, $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_favorite['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_favorite['access_denied_group_t']);
        }
        elseif(forum_all_password($f_id))
        {
             $message = $lang_m_b_favorite['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_favorite['hide_topic']);
        }
        return $message;        
    }    
    
    $topic = $DB->one_select( "id, forum_id, title, hiden", "topics", "id = '{$id}'");
    if (!$topic['id'])
        message ($lang_message['error'], $lang_m_b_favorite['topic_not_found'], 1);
    else
    {  
        $lang_location = str_replace("{link}", topic_link($topic['id']), $lang_m_b_favorite['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_favorite['location_online']);
        $lang_location = str_replace("{title}", $topic['title'], $lang_location);
        $onl_location = $lang_location;
        
        $find_error = false;
        
        if ($member_id['lb_favorite'] == "")
        {
            if (!check_access_fav($topic['forum_id'], $topic['hiden']))
            {
                $DB->prefix = DLE_USER_PREFIX;
                $DB->update("lb_favorite = '{$id}'", "users", "user_id = '{$member_id['user_id']}'");
            }
            else
            {
                message ($lang_message['error'], check_access_fav ($topic['forum_id'], $topic['hiden']), 1);
                $find_error = true;
            }
        }
        else
        {
            $favorites = explode (",", $member_id['lb_favorite']);
            if (in_array($id, $favorites))
            {
                $key_fav = array_search($id, $favorites);
                unset($favorites[$key_fav]);
            }
            else
            {
                if (!check_access_fav($topic['forum_id'], $topic['hiden']))
                    array_push ($favorites, $id);
                else
                {
                    message ($lang_message['error'], check_access_fav ($topic['forum_id'], $topic['hiden']), 1);
                    $find_error = true;
                }
            }
            $fav = implode (",", $favorites);
            $DB->prefix = DLE_USER_PREFIX;
            $DB->update("lb_favorite = '{$fav}'", "users", "user_id = '{$member_id['user_id']}'");
        }
        
        if (!$find_error)
            header( "Location: ".topic_link($id) );
    }
}    
?>