<?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');

function show_more_xml($what_kind = 'related', $categories = '1', $limit = 5) 
{ 
	global $lang;
	
	$categories	= secure_sql( trim($categories) );
	$query	= '';
	$result	= '';
	$sql	= '';
	
	if($what_kind != 'related' && $what_kind != 'same_artist' && $what_kind != 'best_in_category')
		$what_kind = 'related';		//	default value
	
	switch($what_kind)
	{
		case 'related':
			$sql = "SELECT * 
					FROM pm_videos 
					WHERE category = '".$categories."' 
					ORDER BY RAND() 
					LIMIT ".$limit;
		break;
		
		case 'same_artist':
			$sql = "SELECT * 
					FROM pm_videos 
					WHERE artist='".$categories."' 
					ORDER BY added DESC 
					LIMIT ".$limit;
		break;
		
		case 'best_in_category':
			if(strstr($categories, ",")) 
			{
				$temp = explode(",", $categories);
				$categories = trim($temp[0]);
			}
			$sql = "SELECT pm_videos.*, pm_ratings.*
					FROM pm_videos 
					JOIN pm_ratings ON ( pm_videos.uniq_id = pm_ratings.id ) 
					WHERE (category LIKE '%,$categories,%' 
						   OR category LIKE '%,$categories' 
						   OR category LIKE '$categories,%' 
						   OR category LIKE '$categories')
					ORDER BY total_votes DESC , total_value DESC 
					LIMIT ".$limit;
		break;
	}

	$query = @mysql_query($sql);
	
	// backup 
	if (mysql_num_rows($query) == 0 && $what_kind == 'best_in_category')
	{
		$sql = "SELECT * 
				FROM pm_videos 
				WHERE category = '".$categories."' 
				ORDER BY site_views DESC  
				LIMIT ".$limit;
		$query = @mysql_query($sql);
	}
	
	$result = '';
	if(@mysql_num_rows($query) > 0)
	{
		$n = 1;
		while($row = mysql_fetch_array($query))
		{
			$results .= "
			<video id=\"00".$n."\">
				<title>".$row['artist']." - ".$row['video_title']."</title>
				<thumb>".show_thumb($row['uniq_id'])."</thumb>
				<url>".makevideolink($row['uniq_id'], $row['artist'], $row['video_title'])."</url>
			</video>";
			$n++;
		}
		mysql_free_result($query);
	}
	else
	{
		$results .= "
			<video id=\"001\">
				<title>".$lang['top_videos_msg1']."</title>
				<thumb>".show_thumb($row['uniq_id'])."</thumb>
				<url></url>
			</video>";
	}
	
	return $results;
}

$illegal_chars = array(">", "<", "&", "'", '"');

$video_id 	= $_GET['vid'];
$video_id 	= str_replace($illegal_chars, "", $video_id);
$video 		= request_video($video_id, '', true);


	if ( ! is_array($video))
	{
		exit();
	}
		
		switch ($action)
		{
			case 'show_more_best':
			
				$output =  show_more('best_in_category', $video['category'], 10);
					
			break;
			
			case 'show_more_artist':
			
				$output = show_more('same_artist', $video['artist'], 10);
			
			break;
			
			case 'show_more_related':
			
				$output = show_more('related', $video['category'], 10);
			
			break;
		}
$output =  show_more_xml('related', $video['category'], 10);
@header("Content-Type: text/xml; charset=utf-8");
echo '<?xml version="1.0" encoding="utf-8"?>
<videolist>
<title>Related videos:</title>';
echo $output."\n</videolist>";
exit();
?>