<?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('rate_functions.php');
require_once('include/user_functions.php');
require_once('include/islogged.php');
require_once('include/article_functions.php');

$unique_id = $_GET['vid'];
$video_is_restricted = false;

if ( ! empty($unique_id) && strlen($unique_id) < 10) 
{
	$video = request_video($unique_id);

	$category_name = make_cats($video['category']);
	$now = time();

	if($video == 0) { 
		header("Location: index."._FEXT);
		exit;
	}
	
	if (is_user_logged_in()) 
	{
		$query = @mysql_query("SELECT COUNT(*) as total FROM pm_favorites WHERE user_id = '".$userdata['id']."' AND uniq_id = '".$video['uniq_id']."'");
		if ( ! $query)
		{
			$isfavorite = 0;
		}
		else
		{
			$res = mysql_fetch_array($query);
			@mysql_free_result($query);
			
			if ($res['total'] > 0) 
			{
				$isfavorite = 1;
			}
		}
		$total_f = count_entries('pm_favorites', 'user_id', $userdata['id']);
	}
	
	if ( ! $logged_in && $video['restricted'] == '1') // @since 1.6.5
	{
		$video_is_restricted = true;
	}
	
	if ( ! $video_is_restricted && update_view_count($video['id'])) // @since 1.6.5
	{
		add_to_chart($video['uniq_id']);
	}

	
	if ($config['mod_article'] == 1)
	{
		//	find related articles
		$related_articles = array();
		
		$safe_tag = safe_tag($video['artist']);
		$safe_tag = secure_sql($safe_tag);
				
		$sql = "SELECT * 
				FROM art_tags 
				WHERE safe_tag LIKE '". $safe_tag ."'";
		
		$result = @mysql_query($sql);
		if (@mysql_num_rows($result) > 0)
		{
			$ids = array();
			while ($row = mysql_fetch_assoc($result))
			{
				$ids[] = (int) $row['article_id'];
			}
			mysql_free_result($result);
		
			shuffle($ids);
			$related_articles = art_load_articles(0, 5, '', 0, $ids);
		}

		if ( ! array_key_exists('type', $related_articles))
		{
			$smarty->assign('related_articles', $related_articles); 
		}
		else
		{
			$smarty->assign('related_articles', array()); 
		}
	}
} 
else 
{
	header("Location: index.". _FEXT);
	exit();
}
$tags_arr = array();
$tags_arr = get_video_tags($video['uniq_id'], 1);
$tags	  = '';
if (count($tags_arr) > 0) 
foreach ($tags_arr as $k => $tag)
{
	$tags .= $tag['href'].", ";
}
$tags = substr($tags, 0, -2);
if(isset($_COOKIE[COOKIE_AUTHOR]) && $_COOKIE[COOKIE_AUTHOR] != '')
	$smarty->assign('guestname', str_replace( array('"', '>', '<'), "", $_COOKIE[COOKIE_AUTHOR]) );
else
	$smarty->assign('guestname', '');
$must_sign_in = sprintf($lang['must_sign_in'], _URL."/login."._FEXT, _URL."/register."._FEXT);
$smarty->assign('must_sign_in', $must_sign_in);

$twitter_status  = '';
$twitter_status  = sprintf($lang['detail_title'], $video['video_title'], $video['artist']);
$twitter_status .= ' '. makevideolink($video['uniq_id'], $video['artist'], $video['video_title']);
$twitter_status = urlencode($twitter_status);

// define meta tags & common variables
$meta_title = sprintf($lang['detail_title'], $video['video_title'], $video['artist']);
$meta_description = strip_tags($video['description']);
$meta_description = html_entity_decode($meta_description);
$meta_description = str_replace( array("?", '"'), "", $meta_description);
$meta_description = substr($meta_description, 0, 70);
if(strlen($meta_description) > 0) $meta_description .= '...';
$meta_keywords = '';
if(is_array($tags_arr))
foreach($tags_arr as $id => $v)
{
	$meta_keywords .= $v['tag'] . ', ';
}
$meta_keywords = substr($meta_keywords, 0, -2);
// end

$comments_no = count_entries('pm_comments', 'uniq_id', $video['uniq_id']);
$new_videos = list_videos('added', _NEWVIDS);
if ( ! $video_is_restricted)
{
	$show_comments = show_comments($video['uniq_id'], $userdata['power']);
}
else
{
	$show_comments = '';
}

$show_more_1 = show_more('related', $video['category'], 10);
$show_more_2 = '';
$show_more_3 = '';
// $show_more_2 = show_more('same_artist', $video['artist'], '5');
// $show_more_3 = show_more('best_in_category', $video['category']);
$facebook_image_src = show_thumb($video['uniq_id']);

$smarty->assign('new_videos', $new_videos);
$video_link = urldecode(makevideolink($video['uniq_id'], $video['artist'], $video['video_title']));
$smarty->assign('video', $video);
$smarty->assign('submitter', $video['submitted']);
$smarty->assign('featured', $video['featured']);
$smarty->assign('description', $video['description']);
$smarty->assign('video_link', $video_link);
$smarty->assign('twitter_status', $twitter_status);
$smarty->assign('facebook_image_src', $facebook_image_src);
if ($config['show_fb_like'] == '1')
{
	$smarty->assign('facebook_like_href', urlencode($video_link));
}

$smarty->assign('show_more_related', $show_more_1);
$smarty->assign('show_more_artist', $show_more_2);
$smarty->assign('show_more_best', $show_more_3);
$smarty->assign('isfavorite', $isfavorite);
$smarty->assign('countfavorites', $total_f);

$smarty->assign('show_comments', $show_comments);
$smarty->assign('added', $video['added']);
$smarty->assign('artist', $video['artist']);
$smarty->assign('song', $video['video_title']);
$smarty->assign('name', addslashes($video['artist'] . " - " . $video['video_title']));
$smarty->assign('views', $video['site_views_formatted']);
$smarty->assign('uniq_id', $video['uniq_id']);
$smarty->assign('category', $video['category']);
$smarty->assign('category_name', $category_name);
$smarty->assign('comments_no', $comments_no);
$smarty->assign('video_is_restricted', $video_is_restricted);

$smarty->register_function('list_categories', 'list_categories');
$smarty->assign('tags', $tags);
$smarty->assign('guests_can_comment', ($video_is_restricted) ? 0 : $config['guests_can_comment']);
$smarty->assign('user_id', $userdata['id']);
// --- DEFAULT SYSTEM FILES - DO NOT REMOVE --- //

$smarty->assign('meta_title', $meta_title);
$smarty->assign('meta_keywords', $meta_keywords);
$smarty->assign('meta_description', $meta_description);
$smarty->assign('template_dir', $template_f);
$smarty->display('detail.tpl');
?>