<?php

if (!empty($_GET['no']) && is_numeric($_GET['no']) && $user_id != abs(intval($_GET['no'])) && mysql_result(mysql_query("SELECT COUNT(`id`) FROM `notifications` WHERE `for_id` = '".$user_id."' AND `from_id` = '".abs(intval($_GET['no']))."' AND `mod` = 'friends' AND `type` = 'new_friend' LIMIT 1"),0) == true)
{
	if (mysql_query("DELETE FROM `notifications` WHERE `for_id` = '".$user_id."' AND `from_id` = '".abs(intval($_GET['no']))."' AND `mod` = 'friends' AND `type` = 'new_friend'") == true) mysql_query("UPDATE `users` SET notifications_friends=notifications_friends-1 WHERE `id` = '".$user_id."'");
}



else if (!empty($_GET['ok']) && is_numeric($_GET['ok']) && $user_id != abs(intval($_GET['ok'])) && mysql_result(mysql_query("SELECT COUNT(`id`) FROM `notifications` WHERE `for_id` = '".$user_id."' AND `from_id` = '".abs(intval($_GET['ok']))."' AND `mod` = 'friends' AND `type` = 'new_friend' LIMIT 1"),0) == true && mysql_result(mysql_query("SELECT COUNT(`id`) FROM `friends` WHERE `user_id` = '".abs(intval($_GET['ok']))."' AND `friend_id` = '".$user_id."' LIMIT 1"),0) == false)
{
	$arr_user_id = mysql_fetch_array(mysql_query("SELECT * FROM `users` WHERE `id` = '".abs(intval($_GET['ok']))."' LIMIT 1"));
	if ($arr_user_id['ban'] == 1) go(URL.'/?id='.$arr_user_id['id']);
	
	if (mysql_query("INSERT INTO `friends` SET
	`user_id` = '".$user_id."',
	`friend_id` = '".$arr_user_id['id']."',
	`online` = '".$arr_user_id['visit']."',
	`time` = '".time()."'
	") == true && mysql_query("INSERT INTO `friends` SET
	`user_id` = '".$arr_user_id['id']."',
	`friend_id` = '".$user_id."',
	`online` = '".time()."',
	`time` = '".time()."'
	") == true)
	{
		if (mysql_query("DELETE FROM `notifications` WHERE `for_id` = '".$user_id."' AND `from_id` = '".$arr_user_id['id']."' AND `mod` = 'friends' AND `type` = 'new_friend'") == true)
		{
			mysql_query("UPDATE `users` SET notifications_friends=notifications_friends-1, count_friends=count_friends+1 WHERE `id` = '".$user_id."'");
			mysql_query("UPDATE `users` SET count_friends=count_friends+1 WHERE `id` = '".$arr_user_id['id']."'");
		}
		
		if (mysql_result(mysql_query("SELECT COUNT(`id`) FROM `feed` WHERE `user_id` = '".$user_id."' AND `type` = 'friends' AND `time` > '".(time()-3600*6)."' LIMIT 1"),0) == true)
		{
			$arr1 = mysql_fetch_array(mysql_query("SELECT `refid` FROM `feed` WHERE `user_id` = '".$user_id."' AND `type` = 'friends' AND `time` > '".(time()-3600*6)."' LIMIT 1"));
			if (!in_array($arr_user_id['id'], explode("||", substr($arr1['refid'], 1, -1)))) mysql_query("UPDATE `feed` SET `refid` = '|".$arr_user_id['id']."|".$arr1['refid']."', `uptime` = '".time()."' WHERE `user_id` = '".$user_id."' AND `type` = 'friends' AND `time` > '".(time()-3600*6)."' LIMIT 1");
		}
		else mysql_query("INSERT INTO `feed` SET `user_id` = '".$user_id."', `sex` = '".$user['sex']."', `refid` = '|".$arr_user_id['id']."|', `type` = 'friends', `uptime` = '".time()."', `time` = '".time()."'");
		
		if (mysql_result(mysql_query("SELECT COUNT(`id`) FROM `feed` WHERE `user_id` = '".$arr_user_id['id']."' AND `type` = 'friends' AND `time` > '".(time()-3600*6)."' LIMIT 1"),0) == true)
		{
			$arr2 = mysql_fetch_array(mysql_query("SELECT `refid` FROM `feed` WHERE `user_id` = '".$arr_user_id['id']."' AND `type` = 'friends' AND `time` > '".(time()-3600*6)."' LIMIT 1"));
			if (!in_array($user_id, explode("||", substr($arr2['refid'], 1, -1)))) mysql_query("UPDATE `feed` SET `refid` = '|".$user_id."|".$arr2['refid']."', `uptime` = '".time()."' WHERE `user_id` = '".$arr_user_id['id']."' AND `type` = 'friends' AND `time` > '".(time()-3600*6)."' LIMIT 1");
		}
		else mysql_query("INSERT INTO `feed` SET `user_id` = '".$arr_user_id['id']."', `sex` = '".$arr_user_id['sex']."', `refid` = '|".$user_id."|', `type` = 'friends', `uptime` = '".time()."', `time` = '".time()."'");

		if (mysql_query("INSERT INTO `notifications` SET
		`from_id` = '".$user_id."',
		`sex` = '".$user['sex']."',
		`for_id` = '".$arr_user_id['id']."',
		`mod` = 'friends',
		`type` = 'add_friend',
		`refid` = '".$user_id."',
		`time` = '".time()."',
		`count` = 1,
		`new` = 1
		") == true)
		{
			mysql_query("UPDATE `users` SET `notifications` = 1, notifications_journal=notifications_journal+1 WHERE `id` = '".$arr_user_id['id']."'");
		}
	}
}



else if (!empty($_GET['del_z']) && is_numeric($_GET['del_z']) && $user_id != abs(intval($_GET['del_z'])) && mysql_result(mysql_query("SELECT COUNT(`id`) FROM `notifications` WHERE `from_id` = '".$user_id."' AND `for_id` = '".abs(intval($_GET['del_z']))."' AND `mod` = 'friends' AND `type` = 'new_friend' LIMIT 1"),0) == true)
{
	if (mysql_query("DELETE FROM `notifications` WHERE `from_id` = '".$user_id."' AND `for_id` = '".abs(intval($_GET['del_z']))."' AND `mod` = 'friends' AND `type` = 'new_friend'") == true) mysql_query("UPDATE `users` SET notifications_friends=notifications_friends-1 WHERE `id` = '".abs(intval($_GET['del_z']))."'");
}


?>