<?php
define('D', $_SERVER['DOCUMENT_ROOT'].'/');
include_once D.'sys/inc/home.php';
include_once D.'sys/inc/start.php';
include_once D.'sys/inc/compress.php';
include_once D.'sys/inc/sess.php';
include_once D.'sys/inc/settings.php';
include_once D.'sys/inc/db_connect.php';
include_once D.'sys/inc/ipua.php';
include_once D.'sys/inc/fnc.php';
include_once D.'sys/inc/user.php';
only_reg();

$set['title'] = 'Бар в гостевой';
include_once D.'sys/inc/thead.php';
title();
aut();

if (isset($_GET['user'])){
if (mysql_result(mysql_query("select count(*) from `user` where `id` = '".intval($_GET['user'])."'"),0)==0){header('location: /guest/');exit();}else{$us = intval($_GET['user']);}
}
if ($us)$us = $us;
else
$us = null;

if ($us)$uss = "user=".$us; else $uss=null;

#config

#text div
$divt = "<div class='p_m'>";
#
#link div
$divl = "<div class='foot'>";
#
$endiv = "</div>";
#
$cena = 500; #цена напитков
$namev = "баллов"; #название валюты
$tabval = 'balls'; #столбец валюты
#end config

echo $divt;?><center><img src="img/logo.png" width="278" height="120" alt="" /></center><?=$endiv.$divt;?>Здравствуйте, <?=$user['nick']?>. Добро пожаловать в наш бар. Здесь вы можете угостить присутствующих в гостевой напитками, десертами, закусками:)<?=$endiv.$divt;?>
<img src="img/tovar.png" alt="" /> Товаров: <?=mysql_result(mysql_query("SELECT count(*) FROM `gu_bar_nap`"),0).$endiv;?>
<?
if ($us)
{
echo $divt;?><img src="img/ygost.png" alt="" /> Вы хотите угостить: <a href="/info.php?id=<?=intval($_GET['user'])?>"><?=mysql_result(mysql_query("select nick from `user` where `id` = '".intval($_GET['user'])."'"),0)?></a>
<?
echo $endiv;
}
$act = isset($_GET['act']) ? htmlspecialchars(trim($_GET['act'])):null;

switch($act)
{
default:
$k_post=mysql_result(mysql_query("SELECT COUNT(*) FROM `gu_bar_cat`"),0);
$k_page=k_page($k_post,$set['p_str']);
$page=page($k_page);
$start=$set['p_str']*$page-$set['p_str'];
$q=mysql_query("SELECT * FROM `gu_bar_cat` where `cat` = '0' ORDER BY `id` DESC LIMIT $start, $set[p_str]");

?><table class="post"><?
if (isset($user) and $user['level']>2)echo '<tr><td class="p_m"><img src="img/folder_add.png" alt="" /> <a href="?act=create&id=0">Создать папку</a></td></tr>';

if ($k_post==0){ ?><tr><td class="p_m">Нет категорий</td></tr><? }
while ($post = mysql_fetch_assoc($q))
{

### Счётчик
$k_f=0;
$q3=mysql_query("SELECT * FROM `gu_bar_cat` WHERE `cat` = '$post[id]'");
while ($post2 = mysql_fetch_array($q3)){
$k_f=$k_f+mysql_result(mysql_query("SELECT COUNT(*) FROM `gu_bar_nap` WHERE `id_cat` = '$post2[id]'"),0);
}
$k_f=$k_f+mysql_result(mysql_query("SELECT COUNT(*) FROM `gu_bar_cat` WHERE `cat` = '$post[id]'"),0);
###

echo "<tr>
<td class='p_m'>
<img src='img/folder.png' alt='' /> <a href='?act=cat&amp;id=$post[id]&amp;$uss'>".htmlentities($post['name'], ENT_QUOTES, 'UTF-8')."</a> ($k_f)
</td>
</tr>";
}
echo "</table>";
if ($k_page>1)str('?'.$uss,$k_page,$page); // Вывод страниц
break;

case 'cat' :

### Проверка существует ли папка
if (isset($_GET['id']))$id=intval($_GET['id']);
if (mysql_result(mysql_query("SELECT COUNT(*) FROM `gu_bar_cat` WHERE `id` = '$id' LIMIT 1"),0)==0){header("Location: /index.php?");exit;}
###

if(isset($user) and $user['level']>2){
$cr = mysql_fetch_assoc(mysql_query("select `create` from `gu_bar_cat` where `id` = '$cat' limit 1"));
if ($cr['create']==1)echo $divt.'<img src="img/nap_add.png" alt="" /> <a href="?act=add&amp;cat='.$id.'">Добавить напиток</a>'.$endiv;
echo $divt.'<img src="img/folder_add.png" alt="" /> <a href="?act=create&amp;id='.$id.'">Создать папку</a><br/>'.$endiv;
}

$q=mysql_query("SELECT * FROM `gu_bar_cat` WHERE `cat` = '$id' ORDER BY `id` ASC");
?><table class="post"><?
while ($post = mysql_fetch_array($q))
{
$set['p_str']=10;
$list[]=array('cat'=>1,'post'=>$post);
}

$q=mysql_query("SELECT * FROM `gu_bar_nap` WHERE `id_cat` = '$id' ORDER BY `id` DESC");
while ($post = mysql_fetch_array($q))
{
$set['p_str']=7;
$list[]=array('id_cat'=>0,'post'=>$post);
}

$k_post=sizeof($list);
$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 "<tr><td class='p_m'>Папка пуста</td></tr>";}

for ($i=$start;$i<$k_post && $i<$set['p_str']*$page;$i++){
if ($list[$i]['cat']==1) {
$post=$list[$i]['post'];
### Счётчик
$k_f=0;
$q3=mysql_query("SELECT * FROM `gu_bar_cat` WHERE `cat` = '$post[id]'");
while ($post2 = mysql_fetch_array($q3)){
$k_f=$k_f+mysql_result(mysql_query("SELECT COUNT(*) FROM `gu_bar_nap` WHERE `id_cat` = '$post2[id]'"),0);
}
$k_f=$k_f+mysql_result(mysql_query("SELECT COUNT(*) FROM `gu_bar_nap` WHERE `id_cat` = '$post[id]'"),0);
###
echo '<tr><td class="p_m"><img src="img/folder.png" alt="" /> <a href="?act=nap_sp&amp;cat='.$post['id'].'&amp;'.$uss.'">'.htmlentities($post['name'], ENT_QUOTES, 'UTF-8').'</a> ('.$k_f.')</td></tr>';

}else{ ### Файлы
$post=$list[$i]['post'];

echo '<tr><td class="p_m"><img src="img/nap.png" alt="" /> <a href="?act=nap&amp;id='.$post['id'].'&amp;'.$uss.'">'.htmlentities($post['msg'], ENT_QUOTES, 'UTF-8').'</a></td></tr>';
}
}
?></table><?
if ($k_page>1)str('?act=cat&amp;id='.$id.'&amp;'.$uss,$k_page,$page); // Вывод страниц

break;

case 'nap_sp' :

if (isset($_GET['cat']))$cat=intval($_GET['cat']);
if (mysql_result(mysql_query("SELECT COUNT(*) FROM `gu_bar_cat` WHERE `id` = '$cat' LIMIT 1"),0)==0){header("Location: /index.php?");exit;}

$k_post=mysql_result(mysql_query("SELECT COUNT(*) FROM `gu_bar_nap` WHERE `id_cat` = '$cat'"),0);
$k_page=k_page($k_post,$set['p_str']);
$page=page($k_page);
$start=$set['p_str']*$page-$set['p_str'];
$q=mysql_query("SELECT * FROM `gu_bar_nap` where `id_cat` = '$cat' ORDER BY `id` DESC LIMIT $start, $set[p_str]");

?><table class="post"><?
$cr = mysql_fetch_assoc(mysql_query("select `create` from `gu_bar_cat` where `id` = '$cat' limit 1"));
if(isset($user) and $user['level']>2 and $cr['create']==1){
echo '<tr><td class="p_m"><img src="img/nap_add.png" alt="" /> <a href="?act=add&amp;cat='.$cat.'">Добавить напиток</a></td></tr>';
}

if ($k_post==0){ ?><tr><td class="p_m">Нет напитков</td></tr><? }
while ($post = mysql_fetch_assoc($q))
{

echo "<tr><td class='p_m'><img src='img/nap.png' alt='' /> <a href='?act=nap&amp;id=$post[id]&amp;$uss'>".htmlentities($post['msg'], ENT_QUOTES, 'UTF-8')."</a></td></tr>";
}
echo "</table>";
//echo $divl."<a href='?act=cat&amp;id=$cat&amp;$uss'>Назад</a>".$endiv;
if ($k_page>1)str('?act=nap_sp&amp;cat='.$cat.'&amp;'.$uss,$k_page,$page); // Вывод страниц

break;

case 'create' :
if (isset($_GET['id']))$id=intval($_GET['id']);
if ($id!=0 and mysql_result(mysql_query("SELECT COUNT(*) FROM `gu_bar_cat` WHERE `id` = '$id' LIMIT 1"),0)==0){header("Location: /index.php?");exit;}

if (isset($_POST['creates']))
{
$name = mysql_real_escape_string($_POST['name']);
if (isset($_POST['create']))$create = $_POST['create']==0?0:1;
else
$create=0;
if (mb_strlen($_POST['name'])>64)$err='Название слишком длинное!';
if (mb_strlen($_POST['name'])<3)$err='Название слишком короткое!';
if (!isset($err))
{
mysql_query("insert into `gu_bar_cat` (`name`, `cat`, `create`) values('$name','$id','$create')");
$id_cat = mysql_insert_id();
echo "<div class='msg'>Папка успешно создана!</div>";
if ($id==0)
header('refresh: 1; url=?act=cat&id='.$id_cat);
else
header('refresh: 1; url=?act=dei_sp&cat='.$id_cat);
}
err();
}

?>
<form action="?act=create&amp;id=<?=$id;?>" method="post">
Название:<br/>
<input type="text" name="name" value="" />
<br/>Добавление напитков:<br/>
<select name="create">
<option value="0">Нет</option>
<option value="1">Да</option>
</select>
<br/>
<input type="submit" name="creates" value="Создать" />
</form><?

echo $divl."<a href='?act=cat&amp;id=$id&amp;$uss'>Назад</a>".$endiv;
break;

case 'add' :
if (isset($_GET['cat']))$cat=intval($_GET['cat']);
if ($cat!=0 and mysql_result(mysql_query("SELECT COUNT(*) FROM `gu_bar_cat` WHERE `id` = '$cat' LIMIT 1"),0)==0){header("Location: /index.php?");exit;}

if (isset($_POST['add']))
{
$msg = mysql_real_escape_string($_POST['msg']);
$f = end(explode('.', $_FILES['file']['name']));
if (@$_FILES['file']['name']!=NULL && $f!='gif' && $f!='jpg' && $f!='png' && $f!='jpeg'){$err = 'Ошибка расширения картинки!';}

if (!isset($err))
{
if (@$_FILES['file']['name']!=NULL)
{
move_uploaded_file($_FILES['file']['tmp_name'], 'file/nap_'.md5($_FILES['file']['name']).'.'.$f);
$file = 'nap_'.md5($_FILES['file']['name']).'.'.$f;
}
mysql_query("insert into `gu_bar_nap` (`id_cat`,`msg`,`file`) values('$cat','$msg','$file')");
echo "<div class='msg'>Напиток успешно добавлен!</div>";
}
err();
}

?><form action="?act=add&amp;cat=<?=$cat;?>" method="post" enctype="multipart/form-data">
Описание напитка:<br/>
<input type="text" name="msg" value="" /><br/>
Файл:<br/>
<input type="file" name="file" accept="image/*,image/gif,image/png,image/jpeg" /><br/>
<input type="submit" name="add" value="Добавить" />
</form><?
echo $divl."<a href='?act=cat&amp;id=$cat&amp;$uss'>Назад</a>".$endiv;
break;

case 'nap' :
if (isset($_GET['id']))$id=intval($_GET['id']);
if (mysql_result(mysql_query("SELECT COUNT(*) FROM `gu_bar_nap` WHERE `id` = '$id' LIMIT 1"),0)==0){header("Location: /index.php?");exit;}

$nap = mysql_fetch_assoc(mysql_query("select * from `gu_bar_nap` where `id` = '$id' limit 1"));

echo $divt."
$nap[msg]<br/>
<img src='file/$nap[file]' alt='' />
<br/><br/>
".($uss?"[ <a href='?act=go&amp;id=$nap[id]&amp;$uss'> Угостить </a> ]":null)." стоимость: <span style='color:red'>$cena</span> $namev
".$endiv;
echo $divl."<a href='?act=cat&amp;id=$nap[id_cat]&amp;$uss'>Назад</a>".$endiv;
break;

case 'go' :
if (isset($_GET['id']))$id=intval($_GET['id']);
if (!isset($us) and $us==0 and mysql_result(mysql_query("select count(*) from `user` where `id` = '$us'"),0)==0 and $us==$user['id'] and mysql_result(mysql_query("SELECT COUNT(*) FROM `gu_bar_nap` WHERE `id` = '$id' LIMIT 1"),0)==0){header("Location: /index.php?");exit;}
if ($user['balls']>=$cena)
{
$nap = mysql_fetch_assoc(mysql_query("select * from `gu_bar_nap` where `id` = '$id' limit 1"));
mysql_query("insert into `gu_bar` (`id_ank`, `id_user`, `id_nap`, `time`) values('$us','$user[id]','$id','$_SERVER[REQUEST_TIME]')");
mysql_query("update `user` set `balls` = '".($user['balls']-$cena)."' where `id` = '$user[id]' limit 1");
$msgn = "[img]http://$_SERVER[HTTP_HOST]/gu_bar/file/$nap[file][/img]
$user[nick] заказал $nap[msg] для
".mysql_result(mysql_query("select nick from `user` where `id` = '$us'"),0);
mysql_query("INSERT INTO `mail` (`id_user`, `id_kont`, `msg`, `time`) values('0', '".$us."', '".$msgn."  [url=/guest/]Перейти[/url]', '".$time."')");
mysql_query("INSERT INTO `guest` (`id_user`, `msg`, `time`) values('0', '".$msgn."', '".$time."')");
echo "<div class='msg'>Успешно!</div>";
}else{
echo "<div class='err'>У Вас недостаточно $namev!</div>";
}
echo $divl."<a href='/guest'>В гостевую</a>".$endiv;
break;
}
if (isset($user) and $user['level']>=3)echo $divl."<a href='admin.php'>Админка бара</a>".$endiv;
include_once D.'sys/inc/tfoot.php';
?>