<?

require'../config.php';
$align='left';
$title='Управление ботами';
head();
levels(8);
who_add(0,'enter');

switch($mod){
default:
echo '&#187; <a href="bots?mod=names">Ники ботов</a><br />';
echo '&#187; <a href="bots?mod=set">Настройки</a><br />';
echo '&#187; <a href="bots?mod=bot_bartender">Бармен</a><br /><br />';


break;

case set:

if (empty($act)){
if ($ver!='wml')echo '<form action="bots?mod='.$mod.'&amp;act=act&amp;'.SID.'" method="post">';



echo 'Принимать ответы с компа (викт):<br/>';
echo '<select name="vict">';
if($set['vict'] == 0){
echo '<option value="0">Нет</option>';
echo '<option value="1">Да</option>';
} else {
echo '<option value="1">Да</option>';
echo '<option value="0">Нет</option>';
}
echo '</select><br/>';

echo 'Интервал Умника (сек.):<br/>';
echo '<select name="victint">';
if($set['victint'] === 10){
echo '<option value="10">10</option>';
}
elseif($set['victint'] === '30'){
echo '<option value="30">30</option>';
}
elseif($set['victint'] === '60'){
echo '<option value="60">60</option>';
}
elseif($set['victint'] === '120'){
echo '<option value="120">120</option>';
}
echo '<option value="10">10</option>';
echo '<option value="30">30</option>';
echo '<option value="60">60</option>';
echo '<option value="120">120</option>';
echo '</select><br/>';

echo 'Интервал шутника (мин.):<br/>';

echo '<select name="shuttime">';
if($set['shuttime'] === '600'){
echo '<option value="600">10</option>';
}
elseif($set['shuttime'] === '1800'){
echo '<option value="1800">30</option>';
}
elseif($set['shuttime'] === '3600'){
echo '<option value="3600">60</option>';
}
elseif($set['shuttime'] === '7200'){
echo '<option value="7200">120</option>';
}
echo '<option value="600">10</option>';
echo '<option value="1800">30</option>';
echo '<option value="3600">60</option>';
echo '<option value="7200">120</option>';
echo '</select><br/>';

echo 'Интервал знатока (мин.):<br/>';
echo '<select name="znatoktime">';
if($set['znatoktime'] === '600'){
echo '<option value="600">10</option>';
}
elseif($set['znatoktime'] === '1800'){
echo '<option value="1800">30</option>';
}
elseif($set['znatoktime'] === '3600'){
echo '<option value="3600">60</option>';
}
elseif($set['znatoktime'] === '7200'){
echo '<option value="7200">120</option>';
}
echo '<option value="600">10</option>';
echo '<option value="1800">30</option>';
echo '<option value="3600">60</option>';
echo '<option value="7200">120</option>';
echo '</select><br/>';

echo 'Интервал анекдота (мин.):<br/>';
echo '<select name="anektime">';
if($set['anektime'] === '600'){
echo '<option value="600">10</option>';
}
elseif($set['anektime'] === '1800'){
echo '<option value="1800">30</option>';
}
elseif($set['anektime'] === '3600'){
echo '<option value="3600">60</option>';
}
elseif($set['anektime'] === '7200'){
echo '<option value="7200">120</option>';
}
echo '<option value="600">10</option>';
echo '<option value="1800">30</option>';
echo '<option value="3600">60</option>';
echo '<option value="7200">120</option>';
echo '</select><br/>';

echo 'Цена ответа в викт.(постов):<br/>';
echo '<input type="text" name="cena_answ" value="'.$set['cena_answ'].'" maxlength="20" title="Цена"/><br/>';

if ($ver=='wml'){
echo '<br /><anchor title="go">Добавить<go href="bots?mod='.$mod.'&amp;act=add&amp;'.SID.'" method="post">';
echo '
<postfield name="vict" value="$vict"/>
<postfield name="znatoktime" value="$znatoktime"/>
<postfield name="shuttime" value="$shuttime"/>
<postfield name="victint" value="$victint"/>
<postfield name="anektime" value="$anektime"/>
<postfield name="cena_answ" value="$cena_answ"/>
</go></anchor>';
echo '<br/><br />';
}else{
echo '<br /><input type="submit" class="ibutton" value="Добавить"/></form><br /><br />';
}


}else{


$vict = check($_POST['vict']);
$znatoktime = check($_POST['znatoktime']);
$shuttime = check($_POST['shuttime']);
$victint = check($_POST['victint']);
$anektime = check($_POST['anektime']);
$cena_answ = check($_POST['cena_answ']);

$db->sql_query("UPDATE `setting` SET `vict` = '$vict', `znatoktime` = '$znatoktime', `shuttime` = '$shuttime', `victint` = '$victint', `anektime` = '$anektime', `cena_answ` = '$cena_answ' WHERE `ids` = '1'");
header ('Location: bots?'.SID);  exit;

}

break;

case names:

if (empty($act)){
if ($ver!='wml')echo '<form action="bots?mod='.$mod.'&amp;act=act&amp;'.SID.'" method="post">';

echo 'Система:<br/>';
echo '<input type="text" name="system" value="'.$set['system'].'" maxlength="50" title="Система"/><br/>';
echo 'Умник:<br/>';
echo '<input type="text" name="umnik" value="'.$set['umnik'].'" maxlength="50" title="Умник"/><br/>';
echo 'Барыга:<br/>';
echo '<input type="text" name="baryga" value="'.$set['baryga'].'" maxlength="50" title="Барыга"/><br/>';
echo 'Шутник:<br/>';
echo '<input type="text" name="shut" value="'.$set['shut'].'" maxlength="50" title="Шутник"/><br/>';
echo 'Анекдот:<br/>';
echo '<input type="text" name="anek" value="'.$set['anek'].'" maxlength="50" title="Анекдот"/><br/>';
echo 'Тупица:<br/>';
echo '<input type="text" name="tupica" value="'.$set['tupica'].'" maxlength="50" title="Тупица"/><br/>';
echo 'Священник:<br/>';
echo '<input type="text" name="svaisch" value="'.$set['svaisch'].'" maxlength="50" title="Священник"/><br/>';
echo 'Знаток:<br/>';
echo '<input type="text" name="znatok" value="'.$set['znatok'].'" maxlength="50" title="Знаток"/><br/>';
echo 'Флудер:<br/>';
echo '<input type="text" name="flyder" value="'.$set['flyder'].'" maxlength="50" title="Флудер"/><br/>';

echo 'Мафиози:<br/>';
echo '<input type="text" name="mafiozi" value="'.$set['mafiozi'].'" maxlength="50" title="Флудер"/><br/>';

if ($ver=='wml'){
echo '<br /><anchor title="go">Добавить<go href="bots?mod='.$mod.'&amp;act=add&amp;'.SID.'" method="post">';
echo '
<postfield name="umnik" value="$umnik"/>
<postfield name="shut" value="$shut"/>
<postfield name="anek" value="$anek"/>
<postfield name="tupica" value="$tupica"/>
<postfield name="svaisch" value="$svaisch"/>
<postfield name="system" value="$system"/>
<postfield name="baryga" value="$baryga"/>
<postfield name="znatok" value="$znatok"/>
<postfield name="flyder" value="$flyder"/>
<postfield name="mafiozi" value="$mafiozi"/>
</go></anchor>';
echo '<br/><br />';
}else{
echo '<br /><input type="submit" class="ibutton" value="Добавить"/></form><br /><br />';
}


}else{


$umnik = check($_POST['umnik']);
$shut = check($_POST['shut']);
$anek = check($_POST['anek']);
$tupica = check($_POST['tupica']);
$svaisch = check($_POST['svaisch']);
$system = check($_POST['system']);
$baryga = check($_POST['baryga']);
$znatok = check($_POST['znatok']);
$flyder = check($_POST['flyder']);
$mafiozi = check($_POST['mafiozi']);

$db->sql_query("UPDATE `setting` SET `umnik` = '$umnik', `shut` = '$shut', `anek` = '$anek', `tupica` = '$tupica', `svaisch` = '$svaisch', `system` = '$system', `baryga` = '$baryga', `znatok` = '$znatok', `flyder` = '$flyder', `mafiozi` = '$mafiozi' WHERE `ids` = '1'");
header ('Location: bots?'.SID);  exit;

}

break;
/////////////////////////////Бот наливайко//////////////////////////////////////
case 'bot_bartender':
if (isset($_GET['ok'])) echo '<b style="color:red">Готово</b><br/>';
echo '<a href="bots?mod=strip">Управление стриптизом</a><br/>
      <a href="bots?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: bots?mod=bot_bartender&ok');
}

//Добавление раздела
if (isset($_GET['new_cat']) && !empty($_POST['name'])) {
	$_POST['name'] = mysql_real_escape_string(check($_POST['name'], 'UTF-8'));
	mysql_query("INSERT INTO bot_bartender_cat SET name = '".$_POST['name']."'");
	header('Location: bots?mod=bot_bartender&ok');
}
echo '
<form action="bots?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="bots?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="../utils/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="bots?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: bots?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(check($_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: bots?mod=bot_bartender&ok&');
}

echo '
<hr />
<form action="bots?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="bots?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: bots?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: bots?mod=strip&ok&reset_tmp&page='.(int)$_GET['page'].'&');
}

echo '
<form action="bots?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))){
	include '../class/navigator.php';
	$n = new navigator($count, 5, 'bots?mod=strip&amp;'.$ses.$takep.'&amp;');
	for($i = $n->start; $i<$n->end; $i++){
		echo '<img src="../utils/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 />';
	}
	echo '<br />'.$n->navi();
}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="bots?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="bots?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;
////////////////////////////////////////////////////////////////////////////////

}

if ($mod)echo gb.'<a href="bots">Управление ботами</a>'.div;

echo gb.'<a href="index">В админку</a><br />'.div;
echo gb.'<a href="'.H.'enter">Прихожая</a>'.div;
foot();

?>
