<?php
define('NTLCMS', 1);
$module = 'panel';
include ('../system/start.php');
include ('../system/function.php');
include ('../system/antidos.php');
include ('../system/head.php');
################################################################################
if ($active && $_USER['admin'] == '1'){
################################################################################
echo '<div class="title"><b>'.razd.' Админ-Панель | Удаление юзера</b></div>';
################################################################################

$error = null;
				
if (isset($_POST['name'])){

$login = isset($_POST['name']) ? filter_var($_POST['name'], FILTER_SANITIZE_STRING) : false;
$access = DB :: run() -> query("select (`admin`) from `users` where `login` = ? limit 1", array($login)) -> fetch(PDO::FETCH_OBJ);

if (empty($login)) {
          $error.= 'Поле логин обязательно для заполнения!<br/>';
} elseif(!preg_match('#[a-zA-Z0-9_-]{3,15}#i', $login)){
          $error.= 'Логин должен содержать от 3 до 15 символов!<br/>';
} elseif(!DB :: run() -> querySingle("select (`login`) from `users` where `login` = ? limit 1", array($login))){
          $error.= 'Пользователь с логином '.$login.' не найден!<br/>';
} elseif ($access -> admin > $_USER['admin']){
          $error.= 'Вы не можете удалять старшего Вас по должности!<br/>';
} elseif ($login == ADMINS()){
          $error.= 'Суперадмина нельзя удалить!<br/>';
} elseif ($ulogin == $login){
          $error.= 'Вы не можете себя удалить!<br/>';
}

if ($error){
echo '<div class="post"><center><i class="fa fa-close fa-fw"></i> '.$error.'</center></div>';
} else {

################################################################################
$who = DB :: run() -> query("select * from `ref` where `login` = ? limit 1", array($login)) -> fetch(PDO::FETCH_OBJ);
DB :: run() -> query("update `users` set `ref` = `ref` - ? where `login` = ?", array($who -> who, 1));

DB :: run() -> query("delete from `sites` where `login` = ?", array($login));
DB :: run() -> query("delete from `ref` where `who` = ?", array($login));
DB :: run() -> query("delete from `ref` where `login` = ?", array($login));
DB :: run() -> query("delete from `payment` where `login` = ?", array($login));
DB :: run() -> query("delete from `money_in` where `login` = ?", array($login));
DB :: run() -> query("delete from `merchant` where `login` = ?", array($login));
DB :: run() -> query("delete from `inmoney` where `login` = ?", array($login));
DB :: run() -> query("delete from `click` where `login` = ?", array($login));
DB :: run() -> query("delete from `chat` where `login` = ?", array($login));
DB :: run() -> query("delete from `ban` where `who` = ?", array($login));
DB :: run() -> query("delete from `users` where `login` = ? limit 1", array($login));
DB :: run() -> query("delete from `auth` where `login` = ?", array($login));
DB :: run() -> query("delete from `comment` where `login` = ?", array($login));
DB :: run() -> query("delete from `news` where `author` = ?", array($login));
DB :: run() -> query("delete from `like` where `login` = ?", array($login));
DB :: run() -> query("delete from `views` where `login` = ?", array($login));
DB :: run() -> query("delete from `mbstats` where `login` = ?", array($login));

if (is_file(FOLDER.'/avatars/'.$login.'.jpg')) unlink(FOLDER.'/avatars/'.$login.'.jpg');
if (is_file(FOLDER.'/avatars/small/'.$login.'.png')) unlink(FOLDER.'/avatars/small/'.$login.'.png');
################################################################################
header('Location: '.URL.'/_delete');
$_SESSION['success'] = 'Пользователь успешно удален!';
exit;
}
}					
echo '<div class="menu"><form name="form" action="'.URL.'/_delete" method="post">
<b>* Логин юзера:</b><br/><input type="text" name="name" maxlength="15"/><br/> 
<input type="submit" value="Удалить юзера"/></form></div>';

echo '<div class="block"><i class="fa fa-home fa-fw"></i> <a href="'.URL.'/cabinet">В кабинет</a> | <a href="'.URL.'/_control">Админ-Панель</a></div>';			
################################################################################
} else {
echo '<div class="title"><center>Проверка прав доступа</center></div>';
echo '<div class="menu"><center><i class="fa fa-close fa-fw"></i> Доступ запрещен!</center></div>';
echo '<div class="block"><i class="fa fa-home fa-fw"></i> '.(isset($active) ? '<a href="'.URL.'/cabinet">В кабинет</a>' : '<a href="'.URL.'">На главную</a>').'</div>';
}

include ('../system/foot.php');
?>