<?

include_once '../../sys/inc/start.php';

include_once '../../sys/inc/compress.php';

include_once '../../sys/inc/sess.php';

include_once '../../sys/inc/home.php';

include_once '../../sys/inc/settings.php';

include_once '../../sys/inc/db_connect.php';

include_once '../../sys/inc/ipua.php';

include_once '../../sys/inc/fnc.php';

include_once '../../sys/inc/user.php';

only_reg();





if (isset($_GET['no']))

{

	$no = intval($_GET['no']);

	if (mysql_result(mysql_query("SELECT COUNT(*) FROM `user` WHERE `id` = '$no' LIMIT 1"),0)==0){header("Location: index.php?");exit;}

	mysql_query("DELETE FROM `frends` WHERE `user` = '$user[id]' AND `frend` = '$no' LIMIT 1");

	mysql_query("DELETE FROM `frends` WHERE `user` = '$no' AND `frend` = '$user[id]' LIMIT 1");

	mysql_query("DELETE FROM `frends_new` WHERE `user` = '$no' AND `to` = '$user[id]' LIMIT 1");

	mysql_query("DELETE FROM `frends_new` WHERE `user` = '$user[id]' AND `to` = '$no' LIMIT 1");

	mysql_query("OPTIMIZE TABLE `frends`");

	mysql_query("OPTIMIZE TABLE `frends_new`");

	$msgno="К сожалению, обитатель [b]$user[nick][/b] отказал вам в предложение дружбы!";

	mysql_query("INSERT INTO `mail` (`id_user`, `id_kont`, `msg`, `time`) values('0', '$no', '$msgno', '$time')");

	$_SESSION['message']="Заявка отклонена";

	header("Location: new.php?".SID);

	exit;

}



if (isset($_GET['ok']))

{

	$ok = intval($_GET['ok']);

	if (mysql_result(mysql_query("SELECT COUNT(*) FROM `user` WHERE `id` = '$ok' LIMIT 1"),0)==0){header("Location: index.php?");exit;}

    $a = mysql_result(mysql_query("SELECT COUNT(*) FROM `frends_new` WHERE `user`='$ok' AND `to`='$user[id]'"),0);

	$as = mysql_fetch_array(mysql_query("SELECT * FROM `user` WHERE `id` = '".$ok."' LIMIT 1"));

	if ($a==0)

	{

	die("Ошибка");

	}

	else{

	if (mysql_result(mysql_query("SELECT COUNT(*) FROM `frends_new` WHERE (`user` = '$user[id]' AND `to` = '$ok') OR (`user` = '$ok' AND `to` = '$user[id]')"),0)==1)

	{

	mysql_query("INSERT INTO `frends` (`user`, `frend`, `time`, `i`) values('$user[id]', '$ok', '$time', '1')");

    mysql_query("INSERT INTO `frends` (`user`, `frend`, `time`, `i`) values('$ok', '$user[id]', '$time', '1')");

    mysql_query("DELETE FROM `frends_new` WHERE `user` = '$ok' AND `to` = '$user[id]' LIMIT 1");

	mysql_query("DELETE FROM `frends_new` WHERE `user` = '$user[id]' AND `to` = '$ok' LIMIT 1");

    mysql_query("OPTIMIZE TABLE `frends`");

    mysql_query("OPTIMIZE TABLE `frends_new`");

	}

	$_SESSION['message']="Пользователь добавлен в список ваших друзей";

	$msgok="Поздравляем! пользователь [b]$user[nick][/b] стал вашим другом.";

	mysql_query("INSERT INTO `mail` (`id_user`, `id_kont`, `msg`, `time`) values('0', '$ok', '$msgok', '$time')");

	header("Location: new.php?".SID);

	exit;

	}

}



if (isset($_GET['del']))

{

  $no = intval($_GET['del']);

  if (mysql_result(mysql_query("SELECT COUNT(*) FROM `user` WHERE `id` = '$no' LIMIT 1"),0)==0){header("Location: index.php?");exit;}

  if (mysql_result(mysql_query("SELECT COUNT(*) FROM `frends` WHERE (`user` = '$user[id]' AND `frend` = '$no') OR (`user` = '$no' AND `frend` = '$user[id]') LIMIT 1"),0)>0)

  {

  mysql_query("DELETE FROM `frends` WHERE `user` = '$user[id]' AND `frend` = '$no' LIMIT 1");

  mysql_query("DELETE FROM `frends` WHERE `user` = '$no' AND `frend` = '$user[id]' LIMIT 1");

  mysql_query("DELETE FROM `frends_new` WHERE `user` = '$no' AND `to` = '$user[id]' LIMIT 1");

  mysql_query("DELETE FROM `frends_new` WHERE `user` = '$user[id]' AND `to` = '$no' LIMIT 1");

  mysql_query("OPTIMIZE TABLE `frends`");

  mysql_query("OPTIMIZE TABLE `frends_new`");



	$msgno="К сожалению, пользователь [b]$user[nick][/b] удалил вас из списка друзей. ";

	mysql_query("INSERT INTO `mail` (`id_user`, `id_kont`, `msg`, `time`) values('0', '$no', '$msgno', '$time')");

	$_SESSION['message']="Пользователь удален из списка ваших друзей";

	header("location:  $_SERVER[HTTP_REFERER]");

	}

exit;

}



if (isset($_GET['otm']))

{

  $no = intval($_GET['otm']);

  if (mysql_result(mysql_query("SELECT COUNT(*) FROM `user` WHERE `id` = '$no' LIMIT 1"),0)==0){header("Location: index.php?");exit;}

  if (mysql_result(mysql_query("SELECT COUNT(*) FROM `frends_new` WHERE (`user` = '$user[id]' AND `to` = '$no') OR (`user` = '$no' AND `to` = '$user[id]') LIMIT 1"),0)>0)

  {

  mysql_query("DELETE FROM `frends` WHERE `user` = '$user[id]' AND `frend` = '$no' LIMIT 1");

  mysql_query("DELETE FROM `frends` WHERE `user` = '$no' AND `frend` = '$user[id]' LIMIT 1");

  mysql_query("DELETE FROM `frends_new` WHERE `user` = '$no' AND `to` = '$user[id]' LIMIT 1");

  mysql_query("DELETE FROM `frends_new` WHERE `user` = '$user[id]' AND `to` = '$no' LIMIT 1");

  mysql_query("OPTIMIZE TABLE `frends`");

  mysql_query("OPTIMIZE TABLE `frends_new`");

	$_SESSION['message']="Заявка отклонена";

	$msgno="[b]$user[nick][/b], отменил свою заявку на добавление вас в друзья. ";

	mysql_query("INSERT INTO `mail` (`id_user`, `id_kont`, `msg`, `time`) values('0', '$no', '$msgno', '$time')");

	header("location:  $_SERVER[HTTP_REFERER]");

	}

exit;

}



if (isset($_GET['add']))

{

$ank['id']=intval($_GET['add']);

if (mysql_result(mysql_query("SELECT COUNT(*) FROM `user` WHERE `id` = '$ank[id]' LIMIT 1"),0)==0){header("Location: index.php?".SID);exit;}

if (mysql_result(mysql_query("SELECT COUNT(*) FROM `frends` WHERE (`user` = '$user[id]' AND `frend` = '$ank[id]') OR (`user` = '$ank[id]' AND `frend` = '$user[id]') LIMIT 1"),0)==1){header("Location: index.php?".SID);exit;}

if (mysql_result(mysql_query("SELECT COUNT(*) FROM `frends_new` WHERE (`user` = '$user[id]' AND `to` = '$ank[id]') OR (`user` = '$ank[id]' AND `to` = '$user[id]') LIMIT 1"),0)==1){header("Location: index.php?".SID);exit;}

if ($ank['id']==$user['id']){header("Location: index.php?".SID);exit;}

	$_SESSION['message']="Заявка отправлена";

mysql_query("INSERT INTO `frends_new` (`user`, `to`, `time`) values('$user[id]', '$ank[id]', '$time')");

mysql_query("OPTIMIZE TABLE `frends_new`");

header("location:  $_SERVER[HTTP_REFERER]&fok");

exit;

}

include_once '../../sys/inc/tfoot.php';

?>