<?php


session_start();
include("include/config.php");
include("include/functions/import.php");
$thebaseurl = $config['baseurl'];

$mainmenu = "4";
STemplate::assign('mainmenu',$mainmenu);

function title_to_channel_key($a)
{
	global $conn;
    global $config;
    $query="SELECT CID FROM channels WHERE seokey='".mysql_real_escape_string($a)."'";
    $executequery=$conn->execute($query);
    return $executequery->fields[CID];
}

$cid = title_to_channel_key($_REQUEST[key]);

if (is_numeric($cid))
{
	STemplate::assign('cid',$cid);
}
else
{
	$error = "$lang[510]";
}

if ($error == "")
{
	if (does_channel_exist($cid))
	{
		$query = "SELECT * FROM channels WHERE CID='".mysql_real_escape_string($cid)."'";
       	$executequery = $conn->execute($query);
       	$channelarray = $executequery->getarray();
		STemplate::assign('channelarray',$channelarray);
		
		if(mysql_affected_rows()>=1)
		{
		$USERID = $channelarray[0][MID];
		STemplate::assign('USERID',$USERID);
		$CID = $channelarray[0][CID];
		STemplate::assign('CID',$CID);
		$seokey = $channelarray[0][seokey];
		
		STemplate::assign('pagetitle',$channelarray[0][name]." $lang[513]");

		update_viewcount_channel($CID);
		
    	$query = "SELECT * FROM channels_comments WHERE CID='$CID' ORDER BY COMID desc";
    	$executequery = $conn->execute($query);
    	$showcomments =  $executequery->getarray();
		STemplate::assign('showcomments',$showcomments);
		
		$query3 = "SELECT * FROM channels_videos WHERE CID='$CID' order by time_added desc";
       	$executequery3 = $conn->execute($query3);
       	$channelvideos = $executequery3->getrows();
		STemplate::assign('channelvideos',$channelvideos);
		
		$showtheme = "channeldetails.tpl";
		}
		else
		{
			$error = "$lang[511]";
		}
		if ($_REQUEST['removevideo'] != "" && $_REQUEST['RVIDEOID'] != "")
		{

        	$query="SELECT USERID FROM videos WHERE VIDEOID='".mysql_real_escape_string($_REQUEST[RVIDEOID])."'";
        	$executequery = $conn->execute($query);
        	$ownerUSERID = $executequery->fields[USERID];

			if (($_SESSION[USERID] == $USERID) || ($_SESSION[USERID] == $ownerUSERID))
			{
				$queryd = "DELETE FROM channels_videos WHERE CID='".mysql_real_escape_string($CID)."' AND VIDEOID='".mysql_real_escape_string($_REQUEST[RVIDEOID])."'";
       			$executequeryd = $conn->execute($queryd);
				header("Location:$config[baseurl]/$seokey");
			}
		}
	}	
	else
	{
		$error = "$lang[512]";
	}
}


//TEMPLATES BEGIN
STemplate::assign('error',$error);
STemplate::assign('message',$message);

STemplate::display('header.tpl');
if ($error == "")
{
	STemplate::display($showtheme);
}
else
{
	STemplate::display('playvideoerror.tpl');
}
STemplate::display('footer.tpl');
//TEMPLATES END
?>