<?php
// +------------------------------------------------------------------------+
// | PHP Melody version 1.6.7 ( www.phpsugar.com )
// +------------------------------------------------------------------------+
// | PHP Melody IS NOT FREE SOFTWARE
// | If you have downloaded this software from a website other
// | than www.phpsugar.com or if you have received
// | this software from someone who is not a representative of
// | phpSugar, you are involved in an illegal activity.
// | ---
// | In such case, please contact: support@phpsugar.com.
// +------------------------------------------------------------------------+
// | Developed by: phpSugar (www.phpsugar.com) / support@phpsugar.com
// | Copyright: (c) 2004-2011 PhpSugar.com. All rights reserved.
// +------------------------------------------------------------------------+
session_start();
require('config.php');
require_once('include/functions.php');
require_once('include/user_functions.php');
require_once('include/islogged.php');

$whitelist	  = array('.jpg', '.gif', '.png', '.jpeg');
$allowed_type = array('image/png', 'image/gif', 'image/jpg', 'image/jpeg', 'image/pjpeg');

// define meta tags & common variables
$meta_title	= sprintf($lang['upload_avatar'], $userdata['name']);
$meta_description = '';
// end

if ($logged_in)
{
	$avatar	= _URL ."/". _UPFOLDER ."/avatars/". $userdata['avatar'];

	$tdir 	= _UPFOLDER ."/avatars/"; 

	if (isset($_POST['submit']))
	{ 
		$temp		 = explode(".", $_FILES['imagefile']['name']);
		$file_ext	 = '.'. strtolower($temp[count($temp) - 1]);
		if (strlen($file_ext) > 4)
		{
			$file_ext = '';
		}

		if (in_array($_FILES['imagefile']['type'], $allowed_type) && in_array($file_ext, $whitelist))
		{
			$rand		 = rand(343, 1000);
			$new_name	 = substr(md5($_FILES['imagefile']['name']), 1, 8) . $file_ext;
			$avatar_name = 'avatar'. $rand .'-'. $userdata['id'] . $file_ext;
		
			$copy = @copy($_FILES['imagefile']['tmp_name'], "$tdir" . $new_name); 
			if ($copy === TRUE)
			{
				$resize = resize_then_crop("$tdir". $new_name, "$tdir". $avatar_name, THUMB_W_AVATAR, THUMB_H_AVATAR, "255", "255", "255", $allowed_type);
				if($resize != false)
				{
					if( ! in_array($userdata['avatar'], array('no_avatar.gif', 'male.gif', 'female.gif')))
					{ 
						@unlink($tdir. $userdata['avatar']);
					}
					
					@mysql_query("UPDATE pm_users SET avatar = '". $avatar_name ."' WHERE username = '".$userdata['username']."'");
					
					$err = $lang['ua_msg3'];   // Resize successful
				}
				else
				{
					$err = $lang['ua_msg4'];	// Error: Upload Failed
				}
				
				$ttemp = "$tdir" . $new_name;
				@unlink($ttemp); // delete temp image				
 			}
			else 
			{
				$err = $lang['ua_msg4'];	// Error: Upload Failed
			} 
		}
  		else
  		{
    		$err = $lang['ua_msg5'];	// Error: Filetype Is Wrong 
		}
  
		$top_videos = top_videos($config['top_videos_sort'], _TOPVIDS);
		$smarty->assign('top_videos', $top_videos);
		$smarty->assign('err_msg', $err);
		$smarty->assign('avatar', $avatar);
		$smarty->assign('username', $username);
		
		// --- DEFAULT SYSTEM FILES - DO NOT REMOVE --- //
		$smarty->assign('meta_title', $meta_title);
		$smarty->assign('meta_description', $meta_description);
		$smarty->assign('template_dir', $template_f);
		$smarty->display('upload_avatar.tpl');
	}
	else
	{
		$top_videos = top_videos($config['top_videos_sort'], _TOPVIDS);
		$smarty->assign('top_videos', $top_videos);	
		$smarty->assign('avatar', $avatar);
		$smarty->assign('username', $username);
	
		// --- DEFAULT SYSTEM FILES - DO NOT REMOVE --- //
		$smarty->assign('meta_title', $meta_title);
		$smarty->assign('meta_description', $meta_description);
		$smarty->assign('template_dir', $template_f);
		$smarty->display('upload_avatar.tpl');
	}
}
exit();
?>
