<?php

/* DCMS Special
 * Дата последнего редактирования 16.01.2016
 * Модифицировал densnet
 */

foreach (array('start', 'compress', 'sess', 'settings', 'db_connect', 'ipua', 'fnc', 'user') as $inc) {
    require_once "../../../sys/inc/$inc.php";
}

if (isset($_GET['id'])) {
    $id = intval($_GET['id']);
} else {
    header("Location: /index.php");
}

$set['title'] = 'Удаление &amp; ' . $_SERVER['HTTP_HOST'];

require_once H . 'sys/inc/thead.php';

aut();


if (!isset($user)) {
    echo '<div class = "errs">Доступ закрыт.</div>';
} else if ($id == 0 || $id < 0) {
    echo '<div class = "errs">Доступ закрыт.</div>';
} else if (isset($user) && $user['level'] < 4) {
    echo '<div class = "errs">Доступ закрыт.</div>';
} else {

    if ($_GET['act'] == 'dir') {

        $q = mysql_query("SELECT * FROM `smiles_spis` WHERE `id_dir` = '" . $id . "'");
        while ($sm = mysql_fetch_array($q)) {
            unlink(H . 'style/smiles/' . $sm['name'] . '.gif');
        }

        $q = mysql_query("SELECT * FROM `smiles_spis` WHERE `id_dir` = '" . $id . "'");
        while ($sm = mysql_fetch_array($q)) {
            mysql_query("DELETE FROM `smiles_spis` WHERE `id_dir` = '" . $id . "'");
        }

        mysql_query("DELETE FROM `smiles_dir` WHERE `id` = '" . $id . "'");
        mysql_query("OPTIMIZE TABLE `smiles_spis`, `smiles_dir`");
        header("Location: index.php?");
        
        $_SESSION['message'] = lang('Дирректория успешно удалена');
    } else if ($_GET['act'] == 'smile') {
        $smile = mysql_fetch_array(mysql_query("SELECT * FROM `smiles_spis` WHERE `id` = '" . $id . "'"));
        $dir = mysql_fetch_array(mysql_query("SELECT * FROM `smiles_spis` WHERE `id_dir` = '" . $id . "'"));
        unlink(H . 'style/smiles/' . $smile['name'] . '.gif');
        mysql_query("DELETE FROM `smiles_spis` WHERE `id` = '" . $id . "' LIMIT 1");
        mysql_query("OPTIMIZE TABLE `smiles_spis`");
        header("Location: index.php?");
        $_SESSION['message'] = lang('Смайл успешно удален');
    } else {
        echo "<div class='errs'>Неверный ввод</div>";
    }
}

require_once H . 'sys/inc/tfoot.php';
