<?
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']="Подарок для $ank[nick]";
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='У вас недостаточно LM';
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 "<img src='/style/back.gif' alt='' class='icon'/>\n";
echo " <a href='/info.php?id=$ank[id]'>На страничку</a><br />\n";
include_once '../sys/inc/tfoot.php';
exit();   
}
}
err();
echo "<form method='post' action='?user=$ank[id]&amp;dir=".intval($_GET['dir'])."&amp;p=$p[id]'>\n";
echo "Пожелание к подарку:<br />\n<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='p_t'>\n";
echo "Подарков в категории нет<br />";
echo "  </td>\n";
echo "   </tr>\n";
echo "</table>\n";
}
echo"<div class='menu_razd'>\n";
echo "<img src='/style/back.gif' alt='' class='icon'/>\n";
echo"<a href='?user=$ank[id]'>К категориям</a><br />\n";
echo"</div>\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";

if($num==1){
echo "  <div class='rer'>\n";
$num=0;
}else{
echo "  <div class='aut'>\n";
$num=1;}

echo"<b>$post[name]</b><br />";
echo"<img src='$post[url]' /><br />";
echo "Стоимость: $post[prise] LM<br />\n";
echo"<a href='?user=$ank[id]&amp;dir=$dir[id]&amp;p=$post[id]'>Купить подарок</a><br />\n";
echo"</div>\n";

}

if ($k_page>1)str("?user=$ank[id]&amp;dir=$dir[id]&amp;",$k_page,$page); // Вывод страниц
echo"<div class='menu_razd'>\n";
echo "<img src='/style/back.gif' alt='' class='icon'/>\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";


if($k_post==0)
{
echo "   <tr>\n";
echo "  <td class='p_t'>\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='linechat'>\n";
echo "<img src='/style/icons/pod.gif' 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';
?>