<?
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';

if(!isset($user)){
echo '<div class="err">Доступ закрыт для гостей. Пройдите Авторизация</div>';
}else{


only_reg();

$ank['id']=$user['id'];

if(isset($_GET['cleanvbgfrxddyyun']))
{

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{	
	mysql_query("INSERT INTO `frends` (`user`, `frend`, `time`, `i`) values('$ank[id]', '$ok', '$time', '1')");
    mysql_query("INSERT INTO `frends` (`user`, `frend`, `time`, `i`) values('$ok', '$ank[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`");
    
$msgok="Поздравляем! $user[nick] теперь твой друг.";
mysql_query("INSERT INTO `mail` (`id_user`, `id_kont`, `msg`, `time`) values('0', '$ok', '$msgok', '$time')");

header("Location: /user/frends/frend_new.php?".SID);
exit;
}
}
}




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="К сожалению, $user[nick] не хочет с тобой дружить!";
mysql_query("INSERT INTO `mail` (`id_user`, `id_kont`, `msg`, `time`) values('0', '$no', '$msgno', '$time')");

  header("Location: /user/frends/frend_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;}
  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['msg']='Подьзователь удален из списка друзей';
header("location:  $_SERVER[HTTP_REFERER]");
exit;
}


$set['title'] = 'Список предложений дружбы';
include_once '../../sys/inc/thead.php';
title();
aut();

if ($ank['id']==$user['id'])
{
$k_po = mysql_result(mysql_query("SELECT COUNT(*) FROM `frends` WHERE `user` = '$ank[id]' AND `i` = '1'"), 0);
$k_p = mysql_result(mysql_query("SELECT COUNT(*) FROM `frends` INNER JOIN `user` ON `frends`.`frend`=`user`.`id` WHERE `frends`.`user` = '$ank[id]' AND `frends`.`i` = '1' AND `user`.`date_last`>'".(time()-600)."'"), 0);
$k_f = mysql_result(mysql_query("SELECT COUNT(id) FROM `frends_new` WHERE `to` = '$ank[id]' LIMIT 1"), 0);
echo '<div class="foot"><a href="/user/frends/frend.php">Друзья</a> ('.$k_po.') | <a href="/user/frends/fredon.php">На сайте</a> ('.$k_p.') | <b>Приглашения дружить</b> ('.$k_f.')</div>';

}

$m = date('m', $time);
if (substr($m, 0, 1) == 0)$m = str_replace('0', '', $m);
$d = date('d', $time);
$k_f = mysql_result(mysql_query("SELECT COUNT(id) FROM `frends_new` WHERE `to` = '$user[id]' LIMIT 1"), 0);
echo '<table class="post">'."\n";
if ($k_f==0)
{
echo "   <tr>\n";
echo "  <td class='p_t'>\n";
echo "Никто пока не предложил Вам дружбу\n";
echo "  </td>\n";
echo "   </tr>\n";
echo "</table>\n";
}

$q = mysql_query("SELECT * FROM `frends_new` WHERE `to` = '$user[id]' ORDER BY time DESC");
while ($f = mysql_fetch_array($q))
{
$a = mysql_fetch_array(mysql_query("SELECT * FROM `user` WHERE `id` = '".$f['user']."' LIMIT 1"));
echo "<table>\n";
echo "   <tr>\n";
if ($set['set_show_icon']==2){
echo "  <td class='icon48'>\n";
avatar($a['id']);
echo "  </td>\n";
}
elseif ($set['set_show_icon']==1)
{
echo "  <td class='icon14'>\n";
echo "".status($a['id'])."";
echo "  </td>\n";
}

echo "  <td class='p_t'>\n";
echo ' '.group($a['id']).'<a href="/info.php?id='.$a['id'].'">'.$a['nick'] . '</a> ';
echo ''.online($a['id']).' '.medal($a['id']).'  ('.vremja($f['time']).')';
echo "  </td>\n";
echo "   </tr>\n";
echo "</table>\n";
echo "<table>\n";
echo "   <tr>\n";
echo "  <td class='p_m'>\n";
echo '[<img src="/style/icons/ok.gif" /> <a href="/user/frends/frend_new.php?cleanvbgfrxddyyun&amp;ok='.$a['id'].'">Дружить</a>] [<img src="/style/icons/delete.gif" /> <a href="frend_new.php?no='.$a['id'].'">Отказаться</a>]';
echo "  </td>\n";
echo "   </tr>\n";
echo "</table>\n";
}

}


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