<?php

if (!empty($_GET['del_avatar']) && is_numeric($_GET['del_avatar']))
{
	$del_avatar = abs(intval($_GET['del_avatar']));
	
	if (mysql_result(mysql_query("SELECT COUNT(`id`) FROM `avatars` WHERE `id` = '".$del_avatar."' AND `user_id` = '".$user_id."' LIMIT 1"),0) == true)
	{
		$arr = mysql_fetch_array(mysql_query("SELECT * FROM `avatars` WHERE `id` = '".$del_avatar."' AND `user_id` = '".$user_id."' LIMIT 1"));
		mysql_query("DELETE FROM `avatars` WHERE `id` = '".$del_avatar."'");
		mysql_query("DELETE FROM `avatars_comm` WHERE `fid` = '".$del_avatar."'");
		if (mysql_result(mysql_query("SELECT COUNT(`id`) FROM `wall` WHERE `type` = 'update_avatar' AND `photos` = '".$arr['name']."' AND `user_id` = '".$user_id."' LIMIT 1"),0) == true)
		{
			mysql_query("DELETE FROM `wall` WHERE `type` = 'update_avatar' AND `photos` = '".$arr['name']."' AND `user_id` = '".$user_id."'");
			mysql_query("UPDATE `users` SET count_wall=count_wall-1 WHERE `id` = '".$user_id."'");
		}
			
		if ($arr['name'] == $user['avatar'])
		{
			mysql_query("UPDATE `users` SET `avatar` = '' WHERE `id` = '".$user_id."'");
		}
			if (file_exists(HOME .'/files/avatars/'. $user_id.'/'.$arr['name'])) {@unlink(HOME .'/files/avatars/'. $user_id.'/'.$arr['name']);}
			if (file_exists(HOME .'/files/avatars/'.$user_id.'/preview/'.$arr['name'])) {@unlink(HOME .'/files/avatars/'. $user_id.'/preview/'.$arr['name']);}
			if (file_exists(HOME .'/files/avatars/'. $user_id.'/mini/'.$arr['name'])) {@unlink(HOME .'/files/avatars/'. $user_id.'/mini/'.$arr['name']);}
			if (file_exists(HOME .'/files/avatars/'. $user_id.'/icons/'.$arr['name'])) {@unlink(HOME .'/files/avatars/'. $user_id.'/icons/'.$arr['name']);}

		mysql_query("UPDATE `users` SET count_avatars=count_avatars-1 WHERE `id` = '".$user_id."'");
	}
}

?>