<?php
// +------------------------------------------------------------------------+
// | PHP Melody version 1.6.7 ( www.phpsugar.com )
// +------------------------------------------------------------------------+
// | PHP Melody IS NOT FREE SOFTWARE
// | If you have downloaded this software from a website other
// | than www.phpsugar.com or if you have received
// | this software from someone who is not a representative of
// | phpSugar, you are involved in an illegal activity.
// | ---
// | In such case, please contact: support@phpsugar.com.
// +------------------------------------------------------------------------+
// | Developed by: phpSugar (www.phpsugar.com) / support@phpsugar.com
// | Copyright: (c) 2004-2011 PhpSugar.com. All rights reserved.
// +------------------------------------------------------------------------+

session_start();
require('config.php');
require_once('include/functions.php');
require_once('include/user_functions.php');
require_once('include/islogged.php');

// if user is not logged in redirect to homepage
if( !is_user_logged_in() )
{
	header("Location: "._URL. "/index."._FEXT);
	exit();
}

$video_id = secure_sql(trim($_POST['video_id']));
$user_id  = (int) $_POST['user_id'];
$user_id  = secure_sql($user_id);
$res = array();

if(!empty($video_id) && !empty($user_id))
{
	// CHECK IF SUCH A VIDEO IS PRESENT IN THE DATABASE
	$query = @mysql_query("SELECT COUNT(*) as total FROM pm_videos WHERE uniq_id = '".$video_id."'");
	$res   = @mysql_fetch_array($query);
	
	@mysql_free_result($query);
	
	if($res['total'] > 0) 
	{
		unset($res);
		$res = array();
		
		// CHECK IF THIS VIDEO ISN'T ALREADY MARKED AS FAVORITE
		$query = @mysql_query("SELECT COUNT(*) as total FROM pm_favorites WHERE user_id = '".$user_id."' AND uniq_id = '".$video_id."'");
		$res   = @mysql_fetch_array($query);
		@mysql_free_result($query);
		
		if($res['total'] == 0)
		{
			unset($res);
			$res = array();
			//	CHECK IF ADDING THIS VIDEO TO THE USER'S FAVS LIST WILL EXCEED THE _FAV_LIMIT LIMIT
			$query_l = @mysql_query("SELECT COUNT(*) as total FROM pm_favorites WHERE user_id = '".$user_id."'");
			$res     = @mysql_fetch_array($query_l);

			@mysql_free_result($query_l);
			if($res['total'] < _FAV_LIMIT)
			{
				//	EVERYTHING IS OK, ADD THIS VIDEO TO THE USER'S FAVS LIST
				$query = @mysql_query("INSERT INTO pm_favorites SET uniq_id = '".$video_id."', user_id = '".$user_id."'");
			}
		}
	}
}
exit();
?>