<?

/*
// +--------------------------------------------------------------------------+
// | Project:    TBDevYSE - TBDev Yuna Scatari Edition                        |
// +--------------------------------------------------------------------------+
// | This file is part of TBDevYSE. TBDevYSE is based on TBDev,               |
// | originally by RedBeard of TorrentBits, extensively modified by           |
// | Gartenzwerg.                                                             |
// |                                                                          |
// | TBDevYSE is free software; you can redistribute it and/or modify         |
// | it under the terms of the GNU General Public License as published by     |
// | the Free Software Foundation; either version 2 of the License, or        |
// | (at your option) any later version.                                      |
// |                                                                          |
// | TBDevYSE is distributed in the hope that it will be useful,              |
// | but WITHOUT ANY WARRANTY; without even the implied warranty of           |
// | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the            |
// | GNU General Public License for more details.                             |
// |                                                                          |
// | You should have received a copy of the GNU General Public License        |
// | along with TBDevYSE; if not, write to the Free Software Foundation,      |
// | Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA            |
// +--------------------------------------------------------------------------+
// |                                               Do not remove above lines! |
// +--------------------------------------------------------------------------+
*/


require_once 'include/bittorrent.php';

dbconn();
loggedinorreturn();

header("Content-Type: text/html; charset=".$tracker_lang['language_charset']);


if($_SERVER['HTTP_X_REQUESTED_WITH'] == 'XMLHttpRequest') {


    function bark($msg, $error = true) {
        global $tracker_lang;
        echo $msg;
        exit;
    }



	//$id = (int)$_REQUEST['id'];
    $id = (int)$_POST['id'];
    $type = $_POST['type'];
	$page  = $_POST['page'];
   

    if (!isset($id) && !isset($type) && !isset($page))
        bark($tracker_lang['torrent_not_selected']);
		
	//Если пользователь не авторизирован	
	if(!$CURUSER)
		die();

     if($type == 'add') {

        $res = sql_query("SELECT name FROM torrents WHERE id = ".$id."") or sqlerr(__FILE__, __LINE__);
        $arr = mysql_fetch_array($res);




    if ((get_row_count("bookmarks", "WHERE userid = ".sqlesc($CURUSER['id'])." AND torrentid = ".sqlesc($id)."")) > 0)
        bark("Уже есть в закладках!");

        sql_query("INSERT INTO bookmarks (userid, torrentid) VALUES (".sqlesc($CURUSER['id']).", ".sqlesc($id).")") or sqlerr(__FILE__,__LINE__);

       
	    //Выводим(зависит от типа)
        if($page == "details")
			echo "<a href=\"javascript:bookmark('$id', 'del');\"><b>Убрать из избранного</b></a><div id=\"loading\"></div>";
		elseif($page == "browse")
			echo '<img src="./pic/bookmark_on.gif"  border="0" title="Убрать из избранного" onclick="bookmark('.$id.', \'del\' , \'browse\');" />';	
			
        die();
       

        
     }

     if($type == 'del') {

        $res = sql_query("SELECT name FROM torrents WHERE id = ".sqlesc($id)."") or sqlerr(__FILE__, __LINE__);
        $arr = mysql_fetch_array($res);




    if ((get_row_count("bookmarks", "WHERE userid = ".sqlesc($CURUSER['id'])." AND torrentid = ".$id."")) < 0)
         bark("Нету в закладках");

        sql_query("DELETE FROM bookmarks WHERE torrentid='".sqlesc($id)."' AND userid='".sqlesc($CURUSER['id'])."' ") or sqlerr(__FILE__,__LINE__);

        //header("Refresh: 3; url=browse.php");
        //bark("Удалено",false);
        
		//Выводим(зависит от типа)
        if($page == "details")
			echo "<a href=\"javascript:bookmark('$id', 'add');\"><b>Добавить в избранное</b></a><div id=\"loading\"></div>";
		elseif($page == "browse")	
			echo '<img src="./pic/bookmark_off.gif"  border="0" title="Добавить в избранное" onclick="bookmark('.$id.', \'add\' , \'browse\');"  />';		
        die();
        


}
}


?>