<?php
/**************************************************************************************************
|
| iDocScript - Document Sharing Script
| http://www.idocscript.com
| webmaster@idocscript.com
|
|**************************************************************************************************
|
| By using this software you agree that you have read and acknowledged our End-User License 
| Agreement available at http://www.idocscript.com/eula.html and to be bound by it.
|
| Copyright (c) 2011 iDocScript.com. All rights reserved.
|**************************************************************************************************/

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

if ($_SESSION['USERID'] != "" && $_SESSION['USERID'] >= 0 && is_numeric($_SESSION['USERID']))
{
	$UID = intval(cleanit($_SESSION['USERID']));
	
	if($_REQUEST['deleteprofilepicture']!="")
	{
		$queryd = "UPDATE members set profilepicture='' where USERID='".mysql_real_escape_string($UID)."'"; 
		$resultsd = $conn->execute($queryd);
		$message = $lang['551'];
	}
	elseif($_REQUEST['editprofilepicture']!="")
	{
		$uploadedimage = $_FILES['profilepicimage']['tmp_name'];
		
    	if($uploadedimage == "")
		{
			$error = $lang['552'];
		}
		elseif ($error == "")
    	{
			$theimageinfo = getimagesize($uploadedimage);			
			if ($error == "")
			{
      			$thepp = $UID;
      			if($theimageinfo[2] == 1)
				{
        			$thepp .= ".gif";
				}
      			elseif($theimageinfo[2] == 2)
				{
        			$thepp .= ".jpg";
				}
      			elseif($theimageinfo[2] == 3)
				{
        			$thepp .= ".png";
				}
				else
				{
					$error = $lang['553'];
				}
			}
        	if($error == "")
        	{
				$mydocimgnew=$config['membersprofilepicdir']."/".$thepp;
				if(file_exists($mydocimgnew))
				{
					unlink($mydocimgnew);
				}
        		$myconvertimg = $_FILES['profilepicimage']['tmp_name'];
				move_uploaded_file($myconvertimg, $mydocimgnew);				
				$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;
				}
        		generatedocthumbs($mydocimgnew,$mydocimgnew,$new_width,$new_height);
				$max_width_thumbs = "96";
				$max_height_thumbs = "72";
				$tothumbdir = $config['membersprofilepicdir']."/thumbs/".$thepp;
				if(file_exists($tothumbdir))
				{
					unlink($tothumbdir);
				}
				generatedocthumbs($mydocimgnew,$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($UID)."'";
					$conn->execute($query);
					$message = $lang['554'];	
				}
				else
				{
					$error = $lang['555'];
				}
        	}
    	}
	}
	
	$query = "select profilepicture from members where USERID='".mysql_real_escape_string($UID)."' 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);
	}
	
	$templateselect = "editprofilepicture.tpl";
}
else
{
	$redirect = base64_encode($thebaseurl."/editprofilepicture.php");
	header("Location:$config[baseurl]/login.php?redirect=$redirect");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
?>