<?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]))
{
	$query = "select bgimage from members where USERID='$_SESSION[USERID]' limit 1"; 
	$results = $conn->execute($query);
	$pbgimg = $results->fields[bgimage];
	$pbgurl=$config[imagedir].$config['profilebgdir'].$pbgimg;
	if(($pbgimg != "") && (file_exists($pbgurl)))
	{
		$pbgurl=$config[imageurl].$config['profilebgdir'].$pbgimg;
		STemplate::assign('pbgurl',$pbgurl);
	}
	
	
	if($_REQUEST[deletebackground]!="")
	{
		$queryd = "UPDATE members set bgimage='' where USERID='$_SESSION[USERID]'"; 
		$resultsd = $conn->execute($queryd);
		
		if(mysql_affected_rows()>=1)
		{
			$deleteimg=$config[imagedir].$config['profilebgdir'].$pbgimg;
			if(file_exists($deleteimg))
			{
				unlink($deleteimg);
			}
			
			$message = "$lang[543]";
		}
	}
	
	elseif($_REQUEST[editbackground]!="")
	{
		$uploadedimage = $_FILES['profilebgimage']['tmp_name'];
		
    	if($uploadedimage == "")
		{
			$error = "$lang[544]";
		}
		elseif ($error == "")
    	{
			$theimageinfo = getimagesize($uploadedimage);

			
			if ($error == "")
			{
					
      			$theseries = $_SESSION[USERID];
      		
      			if($theimageinfo[2] == 1)
				{
        			$theseries .= ".gif";
				}
      			elseif($theimageinfo[2] == 2)
				{
        			$theseries .= ".jpg";
				}
      			elseif($theimageinfo[2] == 3)
				{
        			$theseries .= ".png";
				}
				else
				{
					$error = "$lang[545]";
				}
			}
			
			
        	if($error == "")
        	{
				$myvideoimgnew=$config[imagedir].$config['profilebgdir'].$theseries;
				if(file_exists($myvideoimgnew))
				{
					unlink($myvideoimgnew);
				}
        		$myconvertimg = $_FILES['profilebgimage']['tmp_name'];
				
				move_uploaded_file($myconvertimg, $myvideoimgnew);
        		//generatevideothumbs($myconvertimg,$myvideoimgnew,$theimageinfo[0],$theimageinfo[1]);
				
				if(file_exists($config[imagedir].$config['profilebgdir'].$theseries))
				{
					$query = "UPDATE members SET bgimage='$theseries' WHERE USERID='$_SESSION[USERID]'";
					$conn->execute($query);
					$message = "$lang[546]";	
				}
				else
				{
					$error = "$lang[534]";
				}
        	}
    	}
	}
	
	$templateselect = "editbackground.tpl";
}
else
{
	header("Location:$config[baseurl]/login.php");exit;
}

$pagetitle = "$lang[547]";
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
?>