<?
include '../config.php';
include '../func.php';

head();
aut();
ban();

switch ($_GET['mod']) {
    default:
    
    
    break;
    
/////////////////////////////Бот наливайко//////////////////////////////////////
case 'bot_bartender':
if (isset($_GET['ok'])) echo '<b style="color:red">Готово</b><br/>';
echo '<a href="barmen.php?mod=strip">Управление стриптизом</a><br/>
      <a href="barmen.php?mod=toast">Управление тостами</a><hr/>';

          /* * * * * * * * * * * * * * Разделы * * * * * * * * * * * * */
//Удаление раздела
if (isset($_GET['del_cat'])) {
	$_GET['del_cat'] =(int)$_GET['del_cat'];
	mysql_query("DELETE FROM bot_bartender_cat WHERE id = '".$_GET['del_cat']."'");

	$sql = mysql_query("SELECT id FROM bot_bartender_product WHERE cat_id = '".$_GET['del_cat']."'");
	while(false != ($res = mysql_fetch_assoc($sql))) {
		if (is_file('../upload/bot_bartender/'.$res['id'].'.img')) unlink('../upload/bot_bartender/'.$res['id'].'.img');
	}
	mysql_query("DELETE FROM bot_bartender_product WHERE cat_id = '".$_GET['del_cat']."'");
	header('Location: barmen.phpbots?mod=bot_bartender&ok');
}

//Добавление раздела
if (isset($_GET['new_cat']) && !empty($_POST['name'])) {
	$_POST['name'] = mysql_real_escape_string(html($_POST['name'], 'UTF-8'));
	mysql_query("INSERT INTO bot_bartender_cat SET name = '".$_POST['name']."'");
	header('Location: barmen.php?mod=bot_bartender&ok');
}
echo '
<form action="barmen.php?mod=bot_bartender&amp;new_cat&amp;" method="post">
 <div>
  Имя:<br />
  <input name="name" type="text" value="" />
  <input type="submit" class="ibutton" value="Добавить раздел" />
 </div>
</form>';

//Список разделов
$sql = mysql_query("SELECT *,(SELECT COUNT(*) FROM bot_bartender_product WHERE bot_bartender_product.cat_id = bot_bartender_cat.id ) AS count FROM bot_bartender_cat");
$cat = '';
echo '<b><small>Разделы:</small></b><br/><br/>';
while(false != ($res = mysql_fetch_assoc($sql))){
	$cat .= '<option value="'.$res['id'].'">'.$res['name'].'</option>';
	echo '<b>&raquo; '.$res['name'].'</b> ('.$res['count'].') <a href="barmen.php?mod=bot_bartender&amp;del_cat='.$res['id'].'&amp;">[x]</a><br />';
	//Список  напитков
	$p_sql = mysql_query("SELECT * FROM bot_bartender_product WHERE cat_id = '".$res['id']."'");
	while(false != ($p_res = mysql_fetch_assoc($p_sql))) {
		echo '&nbsp;&nbsp;&nbsp;';
		if (is_file('../upload/bot_bartender/'.$p_res['id'].'.img')) {
			echo '<img src="../resize.php?img=upload/bot_bartender/'.$p_res['id'].'.img&width=32&height=0" alt="" /> ';
		}
		echo $p_res['name'].' | '.$p_res['alcohol'].'%| '.$p_res['price'].'$ |<a href="barmen.php?mod=bot_bartender&amp;del_product='.$p_res['id'].'&amp;">[x]</a><br />';
	}
}

           /* * * * * * * * * * * Продукты * * * * * * * * * * * * */
//Удаление продукта
if (isset($_GET['del_product'])) {
	$_GET['del_product'] =(int)$_GET['del_product'];
	mysql_query("DELETE FROM bot_bartender_product WHERE id = '".$_GET['del_product']."'");
	//Удаление картинки
	if (is_file('../upload/bot_bartender/'.$_GET['del_product'].'.img')) unlink('../upload/bot_bartender/'.$_GET['del_product'].'.img');
	header('Location: barmen.php?mod=bot_bartender&ok');
}

//Добавление продукта
if (isset($_GET['new_product']) && !empty($_POST['name']) && !empty($_POST['alcohol']) && !empty($_POST['price']) && !empty($_POST['cat_id'])) {
	$_POST['name'] = mysql_real_escape_string(html($_POST['name']));
	$_POST['alcohol'] = (int)$_POST['alcohol'];
	$_POST['price'] = (int)$_POST['price'];
	$_POST['cat_id'] = (int)$_POST['cat_id'];
	mysql_query("INSERT INTO bot_bartender_product SET name = '".$_POST['name']."', alcohol = '".$_POST['alcohol']."', price = '".$_POST['price']."', cat_id = '".$_POST['cat_id']."'");
	$lastId = mysql_insert_id();
	if (isset($_FILES['file']['tmp_name']) && preg_match('/(jpeg|jpg|png|gif)$/i', $_FILES['file']['name'])) {
		$save = '../upload/bot_bartender/'.$lastId.'.img';
		copy($_FILES['file']['tmp_name'], $save);
		chmod($save, 0666);
	}
	header('Location: barmen.php?mod=bot_bartender&ok&');
}

echo '
<hr />
<form action="barmen.php?mod=bot_bartender&amp;new_product&amp;" method="post" enctype="multipart/form-data">
 <div>
  Имя:<br />
  <input name="name" type="text" value="" /><br />
  Картинка:<br />
  <input type="file" name="file" /><br />
  Процент алкоголя<br />
  <input name="alcohol" type="text" value="" /><br />
  Цена:<br />
  <input name="price" type="text" value="" /><br />
  Раздел:<br />
  <select size="1" name="cat_id">
   <option value="0">Не выбран</option>
   '.$cat.'
  </select><br />
  <input type="submit" class="ibutton" value="Добавить продукт" />
 </div>
</form>';
break;
/*Управление стриптизом*/
case'strip':
if (isset($_GET['ok'])) echo '<b style="color:red">Готово</b><br/>';
echo '<a href="barmen.php?mod=strip&amp;reset_tmp&amp;">Обновить кеш</a><hr />';

//Обновление кеша
if(isset($_GET['reset_tmp'])){
	$glob = glob("../upload/bot_bartender/strip/*");
	if(!is_array($glob)) $glob = array();
	file_put_contents('../data/bot_bartender/strip.dat', serialize($glob));
}

//Загрузка файла
if (isset($_FILES['file']['tmp_name']) && preg_match('/(jpeg|jpg|png|gif)$/i', $_FILES['file']['name'])) {
	$save = '../upload/bot_bartender/strip/'.time().'.'.mb_strtolower(pathInfo($_FILES['file']['name'], PATHINFO_EXTENSION), 'UTF-8');
	copy($_FILES['file']['tmp_name'], $save);
	chmod($save, 0666);
	header('Location: barmen.php?mod=strip&ok&reset_tmp&last&');
}

//Удаление файла
if (isset($_GET['del_file']) && preg_match('/^([0-9]+)\.(jpeg|jpg|png|gif)$/i', $_GET['del_file'])) {
	unlink('../upload/bot_bartender/strip/'.$_GET['del_file']);
	header('Location: barmen.php?mod=strip&ok&reset_tmp&page='.(int)$_GET['page'].'&');
}

echo '
<form action="barmen.php?mod=strip&amp;" method="post" enctype="multipart/form-data">
 <div>
  <input type="file" name="file" /><br /> <input type="submit" class="ibutton" value="Добавить фото" />
 </div>
</form><hr />
<b>Список фото:</b><br />
';

$data = unSerialize(file_get_contents('../data/bot_bartender/strip.dat'));
if(($count = count($data))){
	
	
		echo '<img src="../resize.php?img='.ltrim($data[$i], './').'&width=50&height=0" alt="" /> <a href="bots?mod=strip&amp;del_file='.basename($data[$i]).'&amp;page='.$n->page.'&amp;">[x]</a><br />';
	
	
}else echo 'Пусто...<br />';
break;

//Toasts
case 'toast':
//Сохранение
if (!empty($_POST['toast'])) {
	file_put_contents('../data/bot_bartender/toast.dat', htmlspecialchars($_POST['toast']));
	echo '<font color="#FF0000">Успешно добавлено!</font><br />';
	}
//Формы
	if ($ver == "xhtml") {
	$tpl = '
	<form action="barmen.php?mod=toast&amp;" method="post">
	 <div>
	  Тосты:<br />
	  <textarea name="toast" rows="10" cols="40">%s</textarea><br/>
	  <input type="submit" class="ibutton" value="Сохранить" />
	 </div>
	</form>';
	} else {
	$tpl = '
	Тосты:<br />
	<textarea name="toast" rows="10" cols="40" title="toast" emptyok="false">%s</textarea><br/>
	<anchor title="go">Бот наливайко
	 <go href="barmen.php?mod=bot_bartender&amp;" method="post">
	  <postfield name="toast" value="$(toast)"/>
	 </go>
	</anchor>';
	}
echo sprintf($tpl, file_get_contents('../data/bot_bartender/toast.dat'));
break;

}
////////////////////////////////////////////////////////////////////////////////