<?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]))
{
	
	// Show Avatars List Begin
	$avatardir = $config['avatardir'];
  	$allowed_types = array('png','jpg','jpeg','gif');
  	$dimg = opendir($avatardir);
  	
	while($imgfile = readdir($dimg))
  	{
   		if(in_array(strtolower(substr($imgfile,-3)),$allowed_types))
   		{
    		$avatararray[] = $imgfile;
    		sort($avatararray);
    		reset ($avatararray);
   		}
  	}
	STemplate::assign('avatararray',$avatararray);
	// Show Avatars List End
	
	
	// Get Current Avatar Begin
		$query="SELECT avatar FROM members WHERE USERID='".mysql_real_escape_string($_SESSION[USERID])."'";
        $executequery=$conn->execute($query);
        $avatar = $executequery->fields[avatar];
		STemplate::assign('avatar',$avatar);
		$query="SELECT customavatar FROM members WHERE USERID='".mysql_real_escape_string($_SESSION[USERID])."'";
        $executequery=$conn->execute($query);
        $customavatar = $executequery->fields[customavatar];
		STemplate::assign('customavatar',$customavatar);
	// Get Current Avatar End
	
	// Get Max Width and Height Begin
		$query="SELECT value FROM config WHERE setting='max_avatar_width'";
		$executequery=$conn->execute($query);
		$max_avatar_width = $executequery->fields[value];
		STemplate::assign('max_avatar_width',$max_avatar_width);
		$query="SELECT value FROM config WHERE setting='max_avatar_height'";
		$executequery=$conn->execute($query);
		$max_avatar_height = $executequery->fields[value];
		STemplate::assign('max_avatar_height',$max_avatar_height);
	// Get Max Width and Height End
	
	
	if($_REQUEST[changeavatar]!="")
	{
		$uploadedimage = $_FILES['uploadavatar']['tmp_name'];
    	if($uploadedimage == "")
		{
			$error = $lang[489];
		}
		else
    	{
      		$theavatar = $_SESSION[USERID];
      		$theimageinfo = getimagesize($uploadedimage);
      		if($theimageinfo[2] == 1)
			{
        		$theavatar .= ".gif";
			}
      		elseif($theimageinfo[2] == 2)
			{
        		$theavatar .= ".jpg";
			}
      		elseif($theimageinfo[2] == 3)
			{
        		$theavatar .= ".png";
			}
			else
			{
				$error = $lang[490];
			}
			
			if($theimageinfo[0] >= $max_avatar_width)
			{
				$error = $lang[491];
			}
			elseif($theimageinfo[1] >= $max_avatar_height)
			{
				$error = $lang[492];
			}

        	if($error == "")
        	{
				// Delete Previous Uploaded Avatar Begin
				if($customavatar == "1")
				{
					if($avatar != "")
					{
						unlink($config['cavatardir']."/".$avatar);
					}
				}
				// Delete Previous Uploaded Avatar End

          		move_uploaded_file($_FILES['uploadavatar']['tmp_name'], $config['cavatardir']."/".$theavatar);
				
				if(file_exists($config['cavatardir']."/".$theavatar))
				{
					$query="UPDATE members SET avatar='$theavatar', customavatar='1' WHERE USERID='$_SESSION[USERID]'";
					$conn->execute($query);
			
					if(mysql_affected_rows()>=1)
					{
						$message = $lang[493];
					}
					else
					{
						$error = $lang[494];
					}
				}
				else
				{
					$error = $lang[495];
				}
        	}
    	}
	}
	
	
	if($_REQUEST[change]!="")
	{
		$selectedavatar = $_REQUEST[selectedImg];
		
		$query="UPDATE members SET avatar='".mysql_real_escape_string($selectedavatar)."', customavatar='0' WHERE USERID='$_SESSION[USERID]'";
		$conn->execute($query);
			
		if(mysql_affected_rows()>=1)
		{
			$message = $lang[496];
		}
		else
		{
			$error = $lang[497];
		}
	}
	
	$templateselect = "changeavatar.tpl";
}
else
{
	header("Location:$config[baseurl]/login.php");exit;
}

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