<?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 profilepicture from members where USERID='".mysql_real_escape_string($_SESSION[USERID])."' limit 1"; 
	$results = $conn->execute($query);
	$ppicimg = $results->fields[profilepicture];
	$ppurl=$config['membersprofilepicdir']."/".$ppicimg;
	if(($ppicimg != "") && (file_exists($ppurl)))
	{
		$ppurl=$config['membersprofilepicurl']."/".$ppicimg;
		STemplate::assign('ppurl',$ppurl);
	}
	
	
	if($_REQUEST[deleteprofilepicture]!="")
	{
		$queryd = "UPDATE members set profilepicture='' where USERID='".mysql_real_escape_string($_SESSION[USERID])."'"; 
		$resultsd = $conn->execute($queryd);
		
		if(mysql_affected_rows()>=1)
		{
			$message = "$lang[551]";
		}
	}
	
	elseif($_REQUEST[editprofilepicture]!="")
	{
		$uploadedimage = $_FILES['profilepicimage']['tmp_name'];
		
    	if($uploadedimage == "")
		{
			$error = "$lang[552]";
		}
		elseif ($error == "")
    	{
			$theimageinfo = getimagesize($uploadedimage);

			
			if ($error == "")
			{
					
      			$thepp = $_SESSION[USERID];
      		
      			if($theimageinfo[2] == 1)
				{
        			$thepp .= ".gif";
				}
      			elseif($theimageinfo[2] == 2)
				{
        			$thepp .= ".jpg";
				}
      			elseif($theimageinfo[2] == 3)
				{
        			$thepp .= ".png";
				}
				else
				{
					$error = "$lang[553]";
				}
			}
			
			
        	if($error == "")
        	{
				$myvideoimgnew=$config['membersprofilepicdir']."/".$thepp;
				if(file_exists($myvideoimgnew))
				{
					unlink($myvideoimgnew);
				}
        		$myconvertimg = $_FILES['profilepicimage']['tmp_name'];
				
				move_uploaded_file($myconvertimg, $myvideoimgnew);

				
				$origwidth = $theimageinfo[0];
				$origheight = $theimageinfo[1];
				$max_width = "270";
				if ($origwidth > $max_width)
				{
					$new_width = $max_width;
					$ratio = $origheight / $origwidth;
					$new_height = ceil($new_width *$ratio);
				}
				else
				{
					$new_width = $origwidth;
					$new_height = $origheight;
				}
								
        		generatevideothumbs($myvideoimgnew,$myvideoimgnew,$new_width,$new_height);
				
				$max_width_thumbs = "96";
				$max_height_thumbs = "72";
				$tothumbdir = $config['membersprofilepicdir']."/thumbs/".$thepp;
				if(file_exists($tothumbdir))
				{
					unlink($tothumbdir);
				}
				generatevideothumbs($myvideoimgnew,$tothumbdir,$max_width_thumbs,$max_height_thumbs);
				
				if(file_exists($config['membersprofilepicdir']."/".$thepp))
				{
					$query = "UPDATE members SET profilepicture='$thepp' WHERE USERID='".mysql_real_escape_string($_SESSION[USERID])."'";
					$conn->execute($query);
					$message = "$lang[554]";	
				}
				else
				{
					$error = "$lang[555]";
				}
        	}
    	}
	}
	
	$templateselect = "editprofilepicture.tpl";
}
else
{
	header("Location:$config[baseurl]/login.php");exit;
}

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