<?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]))
{
	
	// Get Max Width and Height Begin
		$query="SELECT value FROM config WHERE setting='max_thumb_width'";
		$executequery=$conn->execute($query);
		$max_thumb_width = $executequery->fields[value];
		STemplate::assign('max_thumb_width',$max_thumb_width);
		$query="SELECT value FROM config WHERE setting='max_thumb_height'";
		$executequery=$conn->execute($query);
		$max_thumb_height = $executequery->fields[value];
		STemplate::assign('max_thumb_height',$max_thumb_height);
	// Get Max Width and Height End
	
	
	if($_REQUEST[createchannel]!="")
	{
		$uploadedimage = $_FILES['uploadchannelimage']['tmp_name'];
		$channelbgimage = $_FILES['channelbgimage']['tmp_name'];
		
		if ($_REQUEST[channelname] == "")
		{
			$error = "$lang[524]";
		}
		elseif ($_REQUEST[channeldescription] == "")
		{
			$error = "$lang[525]";
		}
		elseif ($_REQUEST[category] == "")
		{
			$error = "$lang[526]";
		}
		elseif ($_REQUEST[category] == "0")
		{
			$error = "$lang[527]";
		}
		elseif($uploadedimage == "")
		{
			$error = "$lang[528]";
		}
		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 == "")
    	{
			$theimageinfo = getimagesize($uploadedimage);

			
			if ($error == "" && is_numeric($_REQUEST[category]))
			{
				$channelname = htmlentities(strip_tags($_REQUEST[channelname]));
				$channeldescription = htmlentities(strip_tags($_REQUEST[channeldescription]));
				$category = htmlentities(strip_tags($_REQUEST[category]));
				$seokey = seo_clean_titles_sendmail($channelname);
			
				$query = "INSERT INTO channels SET MID='".mysql_real_escape_string($_SESSION[USERID])."', name='".mysql_real_escape_string($channelname)."', details='".mysql_real_escape_string($channeldescription)."', category='".mysql_real_escape_string($category)."', time_added='".time()."', seokey='$seokey'";
				$conn->execute($query);
    			$channelid = mysql_insert_id();
			
				if ($channelid == "" || $channelid <= 0 || !is_numeric($channelid))
				{
        			$error = "$lang[530]";
				}
			
      			$thechannel = $channelid;
      		
      			if($theimageinfo[2] == 1)
				{
        			$thechannel .= ".gif";
				}
      			elseif($theimageinfo[2] == 2)
				{
        			$thechannel .= ".jpg";
				}
      			elseif($theimageinfo[2] == 3)
				{
        			$thechannel .= ".png";
				}
				else
				{
					$error = "$lang[531]";
				}
				
				if(mysql_affected_rows()==1)
				{
					$query = "INSERT INTO channels_members SET USERID='".mysql_real_escape_string($_SESSION[USERID])."', CID='".mysql_real_escape_string($channelid)."', time_joined='".time()."'";
					$conn->execute($query);
					
					$seokeycount = channel_seokey_count($seokey);
					if ($seokeycount > 1)
					{
						$newseokey = $seokey."-".$channelid;
						$query = "UPDATE channels SET seokey='".mysql_real_escape_string($newseokey)."' WHERE CID='".mysql_real_escape_string($channelid)."'";
						$conn->execute($query);
					}
				}
			}
			
			
        	if($error == "")
        	{
				$myvideoimgnew=$config[channeldir]."/".$thechannel;
        		$myconvertimg = $_FILES['uploadchannelimage']['tmp_name'];
        		generatevideothumbs($myconvertimg,$myvideoimgnew,$config[max_thumb_width],$config[max_thumb_height]);
				
				if(file_exists($config['channeldir']."/".$thechannel))
				{
					$query = "UPDATE channels SET photo='$thechannel' WHERE CID='".mysql_real_escape_string($channelid)."'";
					$conn->execute($query);
					$message = "$lang[532]";	
				}
				else
				{
					$error = "$lang[533]";
				}
				
				
				if ($channelbgimageexists)
				{
				
					if (is_numeric($channelid))
					{
						$thechannelbg = $channelid.$thechannelbg;
					}
					$myvideoimgnewbg=$config[imagedir].$config['channelbgdir'].$thechannelbg;
        			$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]";
					}
				}
        	}
    	}
	}
	
	$templateselect = "createchannel.tpl";
}
else
{
	header("Location:$config[baseurl]/login.php");exit;
}

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

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