<?php


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

if ($_SESSION[USERID] != "" && $_SESSION[USERID] >= 0 && is_numeric($_SESSION[USERID]))
{
	if ($_REQUEST[CID] != "" && $_REQUEST[CID] >= 0 && is_numeric($_REQUEST[CID]))
	{

    	$query = "SELECT * FROM channels WHERE CID='".mysql_real_escape_string($_REQUEST[CID])."'";
    	$executequery = $conn->execute($query);
    	$channelarray = $executequery->getarray();
		STemplate::assign('channelarray',$channelarray);
		
		if($_REQUEST[editchannel]!="")
		{
			$name = htmlentities(strip_tags($_REQUEST[name]));
			$name = escape($name);
			$details = htmlentities(strip_tags($_REQUEST[details]));
			$details = escape($details);
			$category = htmlentities(strip_tags($_REQUEST[category]));
			$category = escape($category);
			$channelid = htmlentities(strip_tags($_REQUEST[CID]));
			
			$photo = $_FILES['uploadchannelimage']['tmp_name'];
			$channelbgimage = $_FILES['channelbgimage']['tmp_name'];
			
			if ($photo != "")
			{
				$photoinfo = getimagesize($photo);
			
				$thephotobg = "";
      		
      			if($photoinfo[2] == 1)
				{
        			$thephotobg .= ".gif";
				}
      			elseif($photoinfo[2] == 2)
				{
        			$thephotobg .= ".jpg";
				}
      			elseif($photoinfo[2] == 3)
				{
        			$thephotobg .= ".png";
				}
				else
				{
					$error = "$lang[602]";
				}
				$photoexists = true;
			}
			elseif($channelbgimage != "")
			{
				$channelbgimageinfo = getimagesize($channelbgimage);
			
				$thechannelbg = "";
      		
      			if($channelbgimageinfo[2] == 1)
				{
        			$thechannelbg .= ".gif";
				}
      			elseif($channelbgimageinfo[2] == 2)
				{
        			$thechannelbg .= ".jpg";
				}
      			elseif($channelbgimageinfo[2] == 3)
				{
        			$thechannelbg .= ".png";
				}
				else
				{
					$error = "$lang[529]";
				}
				$channelbgimageexists = true;
			}
			
			
			if ($error == "")
			{
			
				if ($photoexists)
				{
				
					if (is_numeric($channelid))
					{
						$thephotobg = $channelid.$thephotobg;
					}
					$myvideoimgnew=$config['channeldir']."/".$thephotobg;
					if(file_exists($myvideoimgnew))
					{
						unlink($myvideoimgnew);
					}
				
        			$myconvertimg = $_FILES['uploadchannelimage']['tmp_name'];
				
					//move_uploaded_file($myconvertimg, $myvideoimgnew);
					generatevideothumbs($myconvertimg,$myvideoimgnew,$config[max_thumb_width],$config[max_thumb_height]);
				
					if(file_exists($config['channeldir']."/".$thephotobg))
					{
						$query = "UPDATE channels SET photo='$thephotobg' WHERE CID='".mysql_real_escape_string($channelid)."'";
						$conn->execute($query);
					}
					else
					{
						$error = "$lang[603]";
					}
				}
				
			
				if ($channelbgimageexists)
				{
				
					if (is_numeric($channelid))
					{
						$thechannelbg = $channelid.$thechannelbg;
					}
					$myvideoimgnewbg=$config[imagedir].$config['channelbgdir'].$thechannelbg;
					if(file_exists($myvideoimgnewbg))
					{
						unlink($myvideoimgnewbg);
					}
				
        			$myconvertimgbg = $_FILES['channelbgimage']['tmp_name'];
				
					move_uploaded_file($myconvertimgbg, $myvideoimgnewbg);
				
					if(file_exists($config[imagedir].$config['channelbgdir'].$thechannelbg))
					{
						$query = "UPDATE channels SET bgimage='$thechannelbg' WHERE CID='".mysql_real_escape_string($channelid)."'";
						$conn->execute($query);
					}
					else
					{
						$error = "$lang[534]";
					}
				}
				
				$query="UPDATE channels SET name='".mysql_real_escape_string($name)."', details='".mysql_real_escape_string($details)."', category='".mysql_real_escape_string($category)."' WHERE CID='".mysql_real_escape_string($_REQUEST[CID])."'";
				$conn->execute($query);

				$message = "$lang[604] <a href=\"$thebaseurl/mychannelsedit.php?CID=$_REQUEST[CID]\">$lang[605]</a>";

			}
		}
		$templateselect = "mychannelsedit.tpl";
	}
	else
	{
		$error = "$lang[606]";
		$templateselect = "playvideoerror.tpl";
	}
}
else
{
	header("Location:$config[baseurl]/login.php");exit;
}

STemplate::assign('pagetitle',"$lang[607]");

//TEMPLATES BEGIN
STemplate::assign('error',$error);
STemplate::assign('message',$message);
STemplate::display('header.tpl');
STemplate::display($templateselect);
STemplate::display('footer.tpl');
//TEMPLATES END
?>