<?php 

require '../system/start.php'; 
require '../system/functions.php'; 
$name_user = isset ($_GET['user']) ? trim($_GET['user']) : '';
reg(); 
$str['mesto'] = 'Подарки'; 
$str['block'] = $set['site'].' / Подарки'; 
include '../style/'.$style.'/head.php'; 
switch($act) 
{ default: 
$query = mysql_query("select * from `gifts` order by `id` limit ".$page.",5"); 
$total = mysql_result(mysql_query("select count(*) from `gifts`"),0); 
while($row = mysql_fetch_assoc($query)) 
{  $i++; 
 if($i%2) 
 {   echo '<div class="forum1">'; 
 } 
 else 
 {   echo '<div class="forum2">'; 
 } 
 echo '<img src="../images/'.$row['path'].'" alt="Подарок" /><br /> 
 Стоимость: 300 монет<br /> 
 <a href="gifts.php?act=send' . ($name_user ? '&amp;user=' . $name_user . '' : '') . '&amp;id='.$row['id'].'">Подарить!</a></div>'; } 
pages($page, $total, 'gifts.php?' . ($name_user ? 'user=' . $name_user . '&amp;' : '') . '', 5); 
break; 
case 'send': 
$gift = mysql_fetch_assoc(mysql_query("select * from `gifts` where `id` = ".int($_GET['id'])."")); 
echo '<div class="mess">'; 
if(empty($gift['path'])) 
{  echo 'Подарок не найден!</div> 
 <div class="on"> 
 <img class="ico" src="../style/'.$style.'/ico/newpost.png" alt="" /> 
 <a href="gifts.php' . ($name_user ? '?user=' . $name_user . '' : '') . '">Назад</a></div>'; 
 break; }
if($user['balls'] < 300) 
{  echo 'На вашем счету недостаточно '.(300-$user['balls']).' монет для покупки подарка!</div> 
 <div class="on"> 
 <img class="ico" src="../style/'.$style.'/ico/newpost.png" alt="" /> 
 <a href="gifts.php' . ($name_user ? '?user=' . $name_user . '' : '') . '">Назад</a></div>'; 
 break; } 
if(isset($_POST['login']) && !empty($_POST['login'])) 
{  $info = mysql_fetch_assoc(mysql_query("select * from `users` where `login` = '".protect($_POST['login'])."'")); 
 if(empty($info['id'])) 
 {  echo 'Пользователь не найден!</div> 
 <div class="on"> 
 <img class="ico" src="../style/'.$style.'/ico/newpost.png" alt="" /> 
 <a href="gifts.php">Назад</a></div>'; 
 break;  }  mysql_query("INSERT INTO `gifts_users` (id_gift,id_sender,id_getter,opis) VALUES (".$gift['id'].",".$user['id'].",".$info['id'].",'".protect($_POST['opis'])."')");mysql_query("update users set balls=balls-300 where id=".$user['id']."");

 echo 'Подарок успешно отправлен!</div> 
 <div class="on"> 
 <img class="ico" src="../style/'.$style.'/ico/newpost.png" alt="" /> 
 <a href="gifts.php' . ($name_user ? '?user=' . $name_user . '' : '') . '">Назад</a></div>'; 
 break; } 
echo '<form method="post" action="gifts.php?act=send' . ($name_user ? '&amp;user=' . $name_user . '' : '') . '&amp;id='.int($_GET['id']).'"> 
<div> 
Введите логин пользователя:<br/> 
<input type="text" name="login" maxlength="32" value="' . $name_user . '"/><br /> 
Сообщение: (необязательно)<br/> 
<textarea cols="22" rows="2" name="opis" id="opis"></textarea><br /> 
<input type="submit" name="do" value="Подарить"/> 
</div> 
</form> 
</div> 
<div class="on"> 
<img class="ico" src="../style/'.$style.'/ico/newpost.png" alt="" /> 
<a href="gifts.php' . ($name_user ? '?user=' . $name_user . '' : '') . '">Назад</a></div>'; 
break; 
case 'user': 
$uzver = mysql_fetch_assoc(mysql_query("select * from `users` where `id` = ".int($_GET['id'])."")); 
if(empty($uzver['login'])) 
{ 
 echo '<div class="mess">Пользователь не найден!</div> 
 <div class="on"> 
 <img class="ico" src="../style/'.$style.'/ico/newpost.png" alt="" /> 
 </div>'; 
 break; 
} 
$query = mysql_query("select * from `gifts_users` where `id_getter` = ".$uzver['id']." order by `id` desc limit ".$page.",".$set['str'].""); 
$total = mysql_result(mysql_query("select count(*) from `gifts_users` where `id_getter` = ".$uzver['id'].""),0); 
if($total == 0) 
{  echo '<div class="on">У пользователя нет подарков!</div>'; } 
else 
{  while($row = mysql_fetch_assoc($query)) 
 {   $i++; 
  if($i%2) 
  {    echo '<div class="forum1">';   } 
  else 
  {    echo '<div class="forum2">';   } 
  $u = mysql_fetch_assoc(mysql_query("select `login` from `users` where `id` = ".$row['id_sender']."")); 
  echo '<img src="../images/'.$row['id_gift'].'.png" alt="Подарок" /><br /> 
  Подарил: '.$u['login'].'<br /> 
  </div>';  } 
 pages($page, $total, 'gifts.php?act=user&amp;id='.$uzver['id'].'&amp;', 5); } 
break; 
}
include '../style/'.$style.'/foot.php';
?>