<?php
require_once ('system/func.php');
$title = 'Подарки';
require_once ('system/header.php');
auth(); // Закроем от гостей


# Настройки #
$id = abs(intval($_GET['id']));
if ($id) {
    $profile = mysql_fetch_assoc(mysql_query("SELECT * FROM `users` WHERE `id` = '" . $id . "'"));
} else {
    $profile = $user;
}
# Ошибки #


if($profile['id'] =='2'){
$_SESSION['msg'] = "Администратору не нужны подарки";
header('Location: /main');
exit();
}

if($profile['id'] =='1'){
$_SESSION['msg'] = "Системному боту не нужны подарки";
header('Location: /main');
exit();
}



if ($db = mysql_fetch_assoc(mysql_query("SELECT * FROM `users` WHERE `id`='".num($_GET['id2'])."'"))){
    
$acc = mysql_query("SELECT * FROM `users` WHERE `id`='".num($_GET['id2'])."'");    

$acc = mysql_fetch_array($acc);
}else{
	header('Location: /main');
	exit;
}









switch($_GET[mod]){
default:
$set['p_str']=10;
$k_post = mysql_query("SELECT * FROM `porarky` ");
$k_page = k_page($k_post,$set['p_str']);
$page = page($k_page);
$start = $set['p_str']*$page-$set['p_str'];

$res = mysql_query("select * from `porarky` ORDER BY id DESC LIMIT $start,$set[p_str]"); 

$avto = $res;
if($avto>=1){
//While($post = $res->fetch_assoc())
while($post = mysql_fetch_assoc($res)) 


{

echo '<div class="wrapper">';

echo'<center><a class="btn-blue" href="/gift?mod=yes&id='.$post['id'].'&id2='.$acc['id'].'"><img src="'.$post['url'].'" alt="*"/></center>';
echo "<center>Стоимость: $post[prise] ".ico('icons','almaz.png')."<br></center>";
echo"<center><div class='btn_grey'>".ico('icons','ok.png')." Подарить подарок $acc[login]</a></div></center>";
echo ' </div>';

}

echo '<div class="block_c">';
str('?',$k_page,$page); // Вывод страниц

echo ' </div>';
}else{
 
echo'<div class="block_c">';
echo"<img src=\"images/icons/city.png\"> Нет подарков!";
}
echo ' </div></div>';

echo div('main_link');
echo'<a href="/profile/'.$acc[id].'" class="link"><div class="ssilki_dop p_0"> Вернуться к '.$acc[login].'</a></div></div>';

break;
case 'yes':
echo'<div class="block_c">';
$req = mysql_query("SELECT * FROM `porarky` WHERE `id` = '".num($_GET['id'])."'");
////////////////////////////
$avto=$req;
if($avto=="0"){
header ("Location: /gift");
exit;
}


$res = mysql_query("select * from `porarky` where `id`='".num($_GET['id'])."' "); 
//while ($post = $res->fetch_assoc()){
  while($post = mysql_fetch_assoc($res)) {
  

if ($post[prise]>$user[almaz]){
echo"<img src='/images/icons/almaz.png' alt='*'/> Не достаточно алмазов.</div></div>";

require_once ('system/footer.php');
exit;
}
}

if ($db = mysql_fetch_assoc(mysql_query("SELECT * FROM `porarky` WHERE `id`='".num($_GET['id'])."'"))){
	$acc = mysql_query("SELECT * FROM `porarky` WHERE `id`='".num($_GET['id'])."'");
	
	
	$acc = mysql_fetch_array($acc);
	
	
	
	
	
}else{
	header('Location: /main?');
	exit;
}

if ($db = mysql_fetch_assoc(mysql_query("SELECT * FROM `users` WHERE `id`='".num($_GET['id2'])."'"))){
	$accs = mysql_query("SELECT * FROM `users` WHERE `id`='".num($_GET['id2'])."'");
	$accs = mysql_fetch_array($accs);
}else{
	header('Location: /main?');
	exit;
}


if(empty($_POST[name]) or empty($_POST[text])){
echo "<form action=\"/gift?mod=yes&id=".$acc['id']."&id2=".$accs['id']."\" method=\"post\">";
echo"<center>Кому:</center>
<input class='input' type=\"text\" value=\"$accs[login]\" size=\"16\" name=\"name\"/><br/>";

echo "<center>Пожелание:</center>";
echo"<input class='input' type=\"text\" size=\"15\" name=\"text\" maxlength=\"100\"/><br/>";
echo '<center><input class="btn_grey" type="submit" value="Подарить" /></center></form></div></div>';

echo div('main_link');
echo'<a href="/profile/'.$accs[id].'" class="link"><div class="ssilki_dop p_0"> Вернуться к '.$accs[login].'</a></div></div>';
}else{
$name=text($_POST[name]);
$text=text($_POST[text]);

if(mb_strlen($text) < 3)


{

header ("Location: /gift?mod=yes&id=".$acc['id']."&id2=".$accs['id']."");


exit();
}

$req = mysql_query("SELECT * FROM `users` WHERE `login` = '".$name."'");
////////////////////////////
//$avto=$req;
$avto = mysql_fetch_array($req);

$inv = mysql_fetch_assoc($res);

  //while($post = mysql_fetch_assoc($res)) {









if($avto=="0"){
echo"<img src='/images/icons/city.png' alt='*'/> Нет такого персонажа!</div></div>";
require_once ('system/footer.php');
exit;
}






$log=$user['login'];
if($name==$log){
echo"<img src='/images/icons/city.png' alt='*'/> Себе дарить подарки нельзя!</div></div>";
require_once ('system/footer.php');
exit;
}







$res = mysql_query("select * from `porarky` where `id`='".num($_GET['id'])."' "); 
$inv = mysql_fetch_assoc($res);

mysql_query("UPDATE `users` SET `almaz`= `almaz`- '".$inv['prise']."' WHERE `id`='".$user['id']."'");
$times = date("H:i d.m.y");
mysql_query("INSERT INTO `podarok_ok` SET `nick` = '".$user['id']."', `usr` = '".$accs['id']."', `times` ='".$times."', `coment` ='".$text."', `podarok` ='".$inv['url']."' ");   
echo ico('icons','ok.png')." Вы успешно подарили персонажу <b>".$name."</b> подарок!</div></div>";

$n2='Персонаж '.$user[login].' подарил вам подарок!';
mysql_query("INSERT INTO `mail` SET `in` = '1', `out` = '".$accs['id']."', `text` = '".$n2."' , `time` = '".time()."'");
$kont = mysql_query("SELECT * FROM `kont` WHERE `id_user` = '1' && `id_kont` = '".$accs['id']."' LIMIT 1");
//$kont = mysql_fetch_array($kont);


if($kont['id_kont'] != $accs['id']){
mysql_query("INSERT INTO `kont` SET `id_user` = '".$accs['id']."', `id_kont` = '1', `time` = '".time()."'");
mysql_query("INSERT INTO `kont` SET `id_user` = '1', `id_kont` = '".$accs['id']."', `time` = '".time()."'");
}else{
mysql_query("update `kont` set `time` = '".time()."' WHERE `id_user` = '3' && `id_kont` = '".$accs['id']."'");
mysql_query("update `kont` set `time` = '".time()."' WHERE `id_user` = '".$accs['id']."' && `id_kont` = '1'");
}
mysql_query("INSERT INTO `mail`(`user`,`msg`,`time`,`to`,`read`) VALUES('1','$n2','".time()."','".num($accs['id'])."','0')");





echo div('main_link');
echo'<a href="/profile/'.$accs[id].'" class="link"><div class="ssilki_dop p_0"> Вернуться к '.$accs[login].'</a></div></div>';
}
}

require_once ('system/footer.php');
?>