<?
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['user']) && !is_numeric($_GET['user'])){header("Location: /index.php?".SID);exit;}
if (mysql_result(mysql_query("SELECT COUNT(*) FROM `user` WHERE `id` = '".intval($_GET['user'])."' LIMIT 1",$db), 0)==0){header("Location: /index.php?".SID);exit;}
$ank=get_user(intval($_GET['user']));

$set['title']="Подарки";
include_once '../sys/inc/thead.php';
title();





if(isset($_GET['p']) && isset($_GET['dir']) && mysql_result(mysql_query("SELECT COUNT(*) FROM `present_dir` WHERE `id` = '".intval($_GET['dir'])."'"),0) && mysql_result(mysql_query("SELECT COUNT(*) FROM `present_list` WHERE `id` = '".intval($_GET['p'])."'"),0))
{
$p=mysql_fetch_array(mysql_query("select * from `present_list` where `id`='".intval($_GET['p'])."';"));
if ($p['prise'] > $user['balls'])
{
$err='У вас недостаточно монет';
err();
echo"<div class='foot'>\n";
echo"<a href='?user=$ank[id]&amp;dir=".intval($_GET['dir'])."'>Назад</a><br />\n";
echo"</div>\n";
include_once '../sys/inc/tfoot.php';
exit();
}

if(isset($_POST['komm']))
{
$komm=mysql_real_escape_string($_POST['komm']);
if (strlen2($komm)>150)$err='Комментарий превышает предел в 150 символов';
if (!isset($err))
{
$komm=stripcslashes(htmlspecialchars($_POST['komm']));
mysql_query("UPDATE `user` SET `balls` = '".($user['balls']-$p['prise'])."' WHERE `id` ='$user[id]';");
mysql_query("INSERT INTO `present_give` SET `id_user` = '".$user['id']."', `id_give` = '$ank[id]', `time` ='".time()."', `id_p` ='$p[id]', `coment` ='$komm';");     
$msg='<b><a href="/info.php?id='.$user['id'].'">'.$user['nick'].' </a>подарил Вам </b>';
mysql_query("INSERT INTO `present_jurnal` SET `msg` = '$msg', `id_user` = '$ank[id]', `time` ='".time()."';");
$q = mysql_query("SELECT * FROM `fanat` WHERE `user` = '$user[id]' AND `i` = '1'");
while ($f = mysql_fetch_array($q))
{
$a = mysql_fetch_array(mysql_query("SELECT * FROM `user` WHERE `id` = '$f[frend]' LIMIT 1"));

$msg_lenta="подарил подарок [img]http://$_SERVER[SERVER_NAME]/style/icons/pod16.gif[/img] обитателю  [url=/info.php?id=$ank[id]/][b]$ank[nick][/b][/url]";

mysql_query("INSERT INTO `lenta` (`id_user`, `id_kont`, `msg`, `time`) values('$user[id]', '$a[id]', '$msg_lenta', '$time')"); 
}
msg('Подарок успешно подарен! Спасибо!');
echo"<div class='mytitle'>\n";
echo "".online($ank['id'])."  <a href='/info.php?id=$ank[id]'><span style=\"color:$ank[ncolor]\">$ank[nick]</span></a><br />Подарок доставлен\n";
echo"</div>\n";
echo"У Bас еще <img src='/style/img/coins.gif' alt='' class='icon'/> <b>$user[balls]</b> монет\n";
include_once '../sys/inc/tfoot.php';
exit();   
}
}
err();
echo"<div class='mytitle'>\n";
echo"".online($ank['id'])." <a href='/info.php?id=$ank[id]'><span style=\"color:$ank[ncolor]\">$ank[nick]</span></a><br />\n";
echo"Пожелание к подарку:\n";
echo"</div>\n";
echo "<form method='post' action='?user=$ank[id]&amp;dir=".intval($_GET['dir'])."&amp;p=$p[id]'>\n";
echo "<input type=\"text\" name=\"komm\" value=\"\" /><br />\n";
echo "<input value=\"Подарить\" type=\"submit\" />\n";
echo "</form>\n";

echo"<div class='foot'>\n";
echo"<a href='?user=$ank[id]&amp;dir=".intval($_GET['dir'])."'>Назад в категорию</a><br />\n";
echo"</div>\n";
}
else if(isset($_GET['dir']) && mysql_result(mysql_query("SELECT COUNT(*) FROM `present_dir` WHERE `id` = '".intval($_GET['dir'])."'"),0))
{
$dir=mysql_fetch_array(mysql_query("select * from `present_dir` where `id`='".intval($_GET['dir'])."';"));
$k_post=mysql_result(mysql_query("SELECT COUNT(*) FROM `present_list` WHERE `id_dir` = '$dir[id]'"),0);
$k_page=k_page($k_post,$set['p_str']);
$page=page($k_page);
$start=$set['p_str']*$page-$set['p_str'];
if($k_post==0)
{
echo "<table class='post'>\n";
echo "   <tr>\n";
echo "  <td class='komm'>\n";
echo "Подарков нет<br />";
echo "  </td>\n";
echo "   </tr>\n";
echo "</table>\n";
}
echo"<div class='mytitle'>\n";
echo"".online($ank['id'])." <a href='/info.php?id=$ank[id]'><span style=\"color:$ank[ncolor]\">$ank[nick]</span></a><br />\n";
echo"Выбор подарка\n";
echo"</div>\n";
echo"У Bас <img src='/style/img/coins.gif' alt='' class='icon'/> <font color='red'>$user[balls]</font> монет\n";

echo "<table class='post'>\n";
$res = mysql_query("select * from `present_list` WHERE `id_dir` = '$dir[id]' ORDER by `id` DESC LIMIT $start, $set[p_str];"); 
while ($post = mysql_fetch_array($res)){
echo "   <tr>\n";

echo '<tr><td class="icon14">';
echo"<img src='$post[url]' width='70' height='70' /><br />";
echo '</td><td class="anput1">';
echo "<b><font color='#fb85ec'>$post[name]</font></b><br />";
echo "<font color='#7b736d'>Стоимость: <font color='#fc6b75'>$post[prise]</font> монет</font><br />\n";
echo "<a href='?user=$ank[id]&amp;dir=$dir[id]&amp;p=$post[id]'><font color='#474644'><u>Купить подарок</u></font></a><br />\n";
echo '</td></tr>';
}
echo '</table>';
if ($k_page>1)str("?user=$ank[id]&amp;dir=$dir[id]&amp;",$k_page,$page); // Вывод страниц
echo"<div class='foot'>\n";
echo"<a href='?user=$ank[id]'>К категориям</a><br />\n";
echo"</div>\n";
}
else
{
$k_post=mysql_result(mysql_query("SELECT COUNT(*) FROM `present_dir`"),0);
$k_page=k_page($k_post,$set['p_str']);
$page=page($k_page);
$start=$set['p_str']*$page-$set['p_str'];
echo "<table class='post'>\n";
echo"<div class='mytitle'>\n";
echo"".online($ank['id'])." <a href='/info.php?id=$ank[id]'><span style=\"color:$ank[ncolor]\">$ank[nick]</span></a><br />\n";
echo"Выбор категории подарка\n";
echo"</div>\n";

if($k_post==0)
{
echo "   <tr>\n";
echo "  <td class='komm'>\n";
echo "Категорий нет<br />";
echo "  </td>\n";
echo "   </tr>\n";
}

$res = mysql_query("select * from `present_dir` ORDER by `id` DESC LIMIT $start, $set[p_str];"); 
while ($post = mysql_fetch_array($res)){
echo "   <tr>\n";
echo "  <td class='userline'>\n";
echo "<img src='/style/img/gft.png' alt='' class='icon'/> \n";
echo"<a href='?user=$ank[id]&amp;dir=$post[id]'>$post[name]</a> ".mysql_result(mysql_query("SELECT COUNT(*) FROM `present_list` WHERE `id_dir` = '$post[id]'"),0)."<br />";
echo "  </td>\n";
echo "   </tr>\n";
}
echo"</table>";
if ($k_page>1)str("?user=$ank[id]&amp;",$k_page,$page); // Вывод страниц
}





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