<?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_favorite = language_forum ("board/scripts/ajax/favorite");
    header( "Content-type: text/html; charset=windows-1251" );
    
    function check_access_fav($f_id = 0, $hide = 0)
    {
        global $lang_s_a_favorite;
        
        $message = "";
        
        if(!forum_permission($f_id, "read_forum"))
            $message = $lang_s_a_favorite['access_denied_forum'];
        elseif (!forum_permission($f_id, "read_theme"))
            $message = $lang_s_a_favorite['access_denied_read'];
        elseif(forum_all_password($f_id))
            $message = $lang_s_a_favorite['access_denied_pass'];
        elseif(!forum_options_topics($f_id, "hideshow") AND $hide)
            $message = $lang_s_a_favorite['access_denied_hide'];
        return $message;        
    }    
    
    $id = intval($_GET['tid']);
    
    $topic = $DB->one_select( "id, forum_id, hiden", "topics", "id = '{$id}'");
    if (!$topic['id'])
        echo show_jq_message ("3", $lang_s_a_favorite['error_title'], $lang_s_a_favorite['error_info']);
    else
    {
        $find_error = false;
        $error = "";
        $act = $lang_s_a_favorite['act_1'];
        
        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
            {
                $error .= check_access_fav ($topic['forum_id'], $topic['hiden']);
                $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]);
                $act = $lang_s_a_favorite['act_2'];
            }
            else
            {
                if (!check_access_fav($topic['forum_id'], $topic['hiden']))
                    array_push ($favorites, $id);
                else
                {
                    $error .= check_access_fav ($topic['forum_id'], $topic['hiden']);
                    $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)
            echo show_jq_message ("3", $lang_s_a_favorite['error_title'], $error);
        else
            echo show_jq_message ("1", $lang_s_a_favorite['done_title'], str_replace("{act}", $act, $lang_s_a_favorite['done_info']));
    }
    
    stop_script ();
}   

?>