<?

/*
Автор скрипта https://bymas.ru/id85823
Скрипты, программы на заказ.
Быстро, качественно, недорого.
*/

include_once '../sys/inc/start.php';
include_once '../sys/inc/compress.php';
include_once '../sys/inc/sess.php';
include_once '../sys/inc/home.php';
include_once '../sys/inc/settings.php';
include_once '../sys/inc/db_connect.php';
include_once '../sys/inc/ipua.php';
include_once '../sys/inc/fnc.php';
include_once '../sys/inc/user.php';
$set['title'] = 'Искусственный интеллект';
include_once '../sys/inc/thead.php';
err();
aut();

    error_reporting(0);
    
	if ($user['imitation'] == null){
		
    mysql_query("ALTER TABLE `user` ADD `imitation` INT(11) NOT NULL DEFAULT '0'");
    header('Location: index.php');
    exit;
	
	}

    if ($user['level'] < 8){
        
    header('Location: /index.php');
    exit;
    
    }

    $act = esc($_GET['act']);
    
    ?>
    
    <style>
    
.imitation {	
    width:100%;	
    background: url(images/body.png) repeat-x;
    text-align: center;
}    

.imitation_b {
color: #6c6e71;
border-top: 1px #fffefb solid;
border-bottom: 1px #d4cdba solid;
background: #e8e1cf repeat-x;
padding: 7px;
font-weight:bold;
}

    </style>
    
    <div class="imitation"><a href="index.php"><h2><font color="white">Управление искусственным интеллектом</font></h2></a><br /></div>
        
    <?
    
	switch ($act) {
	    
	default:

    ?>
    
    <div class="imitation_b">

    <img src="images/icon.png" alt="*" width="16" height="16"> <a href="?act=bots">Список ботов</a> (<?=mysql_result(mysql_query("select count(*) from user where imitation = 1"), 0);?>)<br />
    <img src="images/icon.png" alt="*" width="16" height="16"> <a href="?act=msg">База фраз</a> (<?=mysql_result(mysql_query("select count(*) from base"), 0);?>)<br />
    <img src="images/icon.png" alt="*" width="16" height="16"> <a href="?act=add">Добавить бота</a><br />
    <img src="images/icon.png" alt="*" width="16" height="16"> <a href="?act=settings">Настройки</a><br />
    
    </div>
    
    <?

    break;
    
    case 'bots':
	
	if (isset($_GET['off']))    {
	    
	    mysql_query("UPDATE `user` SET `imitation` = '0' WHERE `id` = '".abs(intval($_GET['off']))."'");
	    header('Location: /imitation/?act=bots');
	    exit;
	}
	
	$k_post = mysql_result(mysql_query("SELECT COUNT(*) FROM `user` WHERE `imitation` = '1'"), 0);
    $k_page = k_page($k_post,$set['p_str']);
    $page = page($k_page);
    $start = $set['p_str']*$page-$set['p_str'];   
	  
	echo '<table class="post">';

    if ($k_post == 0) echo '<div class="mess">Список ботов пуст</div>';
    
    ?>
    
    <div class="imitation_b">
    
    <?
    
    $q = mysql_query("SELECT id, pol, date_last FROM `user` WHERE `imitation` = '1' ORDER BY `date_last` DESC LIMIT $start, $set[p_str]");

    while ($bot = mysql_fetch_assoc($q)) {
        
        echo user::avatar($bot['id'], 0) . user::nick($bot['id'], 1, 1, 1). otkuda($bot['url']) . ' 
        [<a href="?act=bots&off='.$bot['id'].'"><font color="red">отключить</font></a>]<br /><br />';
        
    }
    
    echo '</table>';
    
    ?>
    
    </div>
    
    <?
    
    if ($k_page > 1) str('?act=bots&', $k_page, $page);
    
	break;
	
	case 'msg':
	   
	$k_post = mysql_result(mysql_query("SELECT COUNT(*) FROM `base`"), 0);
    $k_page = k_page($k_post,$set['p_str']);
    $page = page($k_page);
    $start = $set['p_str']*$page-$set['p_str'];   
	  
	echo '<table class="post">';

    if ($k_post == 0) echo '<div class="mess">Список фраз пуст</div>';
    
    ?>
    
    <div class="imitation_b">
    
    <?
    
    $q = mysql_query("SELECT id, i FROM `base` ORDER BY `id` DESC LIMIT $start, $set[p_str]");

    while ($i = mysql_fetch_assoc($q)) {
        
        echo $i['i'].'<br /><br />';
        
    }
    
    echo '</table>';
    
    ?>
    
    </div>
    
    <?
    
    if ($k_page > 1) str('?act=msg&', $k_page, $page);
    
	break;
	
	case 'add':

	if (isset($_POST['nick'])){
	
	include_once 'classSimpleImage.php';
	
	if (mysql_result(mysql_query("SELECT COUNT(*) FROM `user` WHERE `nick` = '".my_esc($_POST['nick'])."'"),0) > 0) $err = 'Ник "'.stripcslashes(htmlspecialchars($_POST['nick'])).'" уже зарегистрирован';
    if (strlen2($_POST['nick']) < 3) $err = 'Короткий ник';
    if (strlen2($_POST['nick']) > 32) $err = 'Длина ника превышает 32 символа';
	if( !preg_match("#^([A-zА-я0-9\-\_\ ])+$#ui", $_POST['nick'])) $err = 'В нике присутствуют запрещенные символы';
	if (preg_match("#(^\ )|(\ $)#ui", $_POST['nick'])) $err = 'Запрещено использовать пробел в начале и конце ника';

    $filename = esc(stripcslashes(htmlspecialchars(preg_replace('#\.[^\.]*$#i', NULL, $_FILES['file']['name'])))); 
    $strtolower = strtolower(strrchr($_FILES['file']['name'], '.'));
    $size = $_FILES['file']['size'];
    $list = array('.png', '.gif', '.jpg', '.jpeg');

    if (preg_match('/.php/i', $filename) || preg_match('/.pl/i', $filename) || $filename == '.htaccess' || !in_array($strtolower, $list))
    $err = 'Ошибка при загрузке аватара'.$strtolower;
    
	if (!isset($err)){
	    
    echo '<div class="mess">Бот добавлен</div>';
    
    mysql_query("INSERT INTO `user` (`nick`, `date_reg`, `date_last`, `pol`, `imitation`) VALUES ('".my_esc($_POST['nick'])."', '$time', '$time', '".intval($_POST['pol'])."', '1')");
	
	$id_bot = mysql_insert_id();

	mysql_query("UPDATE `gallery_foto` SET `avatar` = '0' WHERE `id_user` = '$id_bot'");
	mysql_query("INSERT INTO `gallery_foto` (`id_gallery`, `name`, `ras`, `type`, `opis`, `id_user`,`avatar`, `metka`, `time`) values ('0', 'Мой аватар', 'jpg', 'image/jpeg', 'Фото на аватар', '$id_bot','1', '0', '".time()."')");

    $id_step = mysql_insert_id();

    if ($size > 10000) {
    
    /*   
    move_uploaded_file($_FILES['file']['tmp_name'], $_SERVER['DOCUMENT_ROOT'].'/sys/gallery/48/'.$id_step.'.jpg');
    move_uploaded_file($_FILES['file']['tmp_name'], $_SERVER['DOCUMENT_ROOT'].'/sys/gallery/50/'.$id_step.'.jpg');
    move_uploaded_file($_FILES['file']['tmp_name'], $_SERVER['DOCUMENT_ROOT'].'/sys/gallery/128/'.$id_step.'.jpg');
    move_uploaded_file($_FILES['file']['tmp_name'], $_SERVER['DOCUMENT_ROOT'].'/sys/gallery/640/'.$id_step.'.jpg');
    move_uploaded_file($_FILES['file']['tmp_name'], $_SERVER['DOCUMENT_ROOT'].'/sys/gallery/foto/'.$id_step.'.jpg');
    */
    
	file_put_contents($_SERVER['DOCUMENT_ROOT'].'/sys/gallery/48/'.$id_step.'.jpg', file_get_contents($_FILES['file']['tmp_name']));
	file_put_contents($_SERVER['DOCUMENT_ROOT'].'/sys/gallery/50/'.$id_step.'.jpg', file_get_contents($_FILES['file']['tmp_name']));
	file_put_contents($_SERVER['DOCUMENT_ROOT'].'/sys/gallery/128/'.$id_step.'.jpg', file_get_contents($_FILES['file']['tmp_name']));
	file_put_contents($_SERVER['DOCUMENT_ROOT'].'/sys/gallery/640/'.$id_step.'.jpg', file_get_contents($_FILES['file']['tmp_name']));
	file_put_contents($_SERVER['DOCUMENT_ROOT'].'/sys/gallery/foto/'.$id_step.'.jpg', file_get_contents($_FILES['file']['tmp_name']));

    $image = new SimpleImage();
    
    $image->load($_SERVER['DOCUMENT_ROOT'].'/sys/gallery/48/'.$id_step.'.jpg');
    $image->resize(64, 64);
    $image->save($_SERVER['DOCUMENT_ROOT'].'/sys/gallery/48/'.$id_step.'.jpg');
  
    $image->load($_SERVER['DOCUMENT_ROOT'].'/sys/gallery/50/'.$id_step.'.jpg');
    $image->resize(64, 64);
    $image->save($_SERVER['DOCUMENT_ROOT'].'/sys/gallery/50/'.$id_step.'.jpg');

    $image->load($_SERVER['DOCUMENT_ROOT'].'/sys/gallery/128/'.$id_step.'.jpg');
    $image->resize(64, 64);
    $image->save($_SERVER['DOCUMENT_ROOT'].'/sys/gallery/128/'.$id_step.'.jpg');
    
    $image->load($_SERVER['DOCUMENT_ROOT'].'/sys/gallery/640/'.$id_step.'.jpg');
    $image->resize(64, 64);
    $image->save($_SERVER['DOCUMENT_ROOT'].'/sys/gallery/640/'.$id_step.'.jpg');

    $image->load($_SERVER['DOCUMENT_ROOT'].'/sys/gallery/foto/'.$id_step.'.jpg');
    $image->resize(64, 64);
    $image->save($_SERVER['DOCUMENT_ROOT'].'/sys/gallery/foto/'.$id_step.'.jpg');
    
    }
    
	} else echo '<div class="err">'.$err.'</div>';
    
	}
	
	?>
	
	<div class="imitation_b">
	    
	<form method='post' enctype="multipart/form-data">
	Придумайте ник <br /><br />
	<input type='text' name='nick' maxlength='32' /><br /><br />
	Пол<br /><br />
	<select name='pol'>
	<option value='1'>Мужской</option>
	<option value='0'>Женский</option>
	</select><br /><br />
	Аватар<br /><br />
	<input name="file" type="file" /><br /><br />
	<input type='submit' value='Добавить' />
    </form>
    
    </div>
	
	<?
	
	break;
	
	case 'settings':
	
	if (isset($_POST['working'])){
	
	if (mysql_result(mysql_query("SELECT COUNT(*) FROM `forum_t` WHERE `id` = '".intval($_POST['id_them'])."'"),0) == 0) $err = 'Такой темы не существует';
	
	if (!isset($err)){
	    
    echo '<div class="mess">Настройки сохранены</div>';
    
    mysql_query("UPDATE `imitation` SET `chat_post` = '".intval($_POST['chat_post'])."', `id_forum` = '".intval($_POST['id_forum'])."', `id_razdel` = '".intval($_POST['id_razdel'])."', `id_them` = '".intval($_POST['id_them'])."', `working` = '".intval($_POST['working'])."' WHERE `id` = '1'");
    
	} else echo '<div class="err">'.$err.'</div>';
    
	}
	
	$imitation = mysql_fetch_assoc(mysql_query("SELECT * FROM `imitation` WHERE `id` = '1' LIMIT 1"));
	
	?>
	
	<div class="imitation_b">
	    
	<form method='post'>
	ID темы <br /><br />
	<input type='text' name='id_them' maxlength='4' value='<?=$imitation['id_them']?>' /><br /><br />
	
    ID форума <br /><br />
	<input type='text' name='id_forum' maxlength='3' value='<?=$imitation['id_forum']?>' /><br /><br />
	
	ID раздела <br /><br />
	<input type='text' name='id_razdel' maxlength='3' value='<?=$imitation['id_razdel']?> '/><br /><br />
	
	ID чата <br /><br />
	<input type='text' name='chat_post' maxlength='3' value='<?=$imitation['chat_post']?> '/><br /><br />
	
    <?
    if ($imitation['working'] == 1){
    ?>

    Статус<br /><br />
    <input type="radio" name="working" value="1" checked="checked" /> вкл.
    <input type="radio" name="working" value="0" /> выкл.<br /><br />

    <?
    }else{
    ?>

    Статус<br /><br />
    <input type="radio" name="working" value="1" /> вкл.
    <input type="radio" name="working" value="0" checked="checked" /> выкл.<br /><br />

    <?
    }
    ?>

	<input type='submit' value='Сохранить' />
    </form>
    
    </div>
	
	<?
	
	break;
    
    }
    
include_once '../sys/inc/tfoot.php';