<?php defined('ACCESS') OR die('No direct script access...');
/**
 * Author - koder_alex
 * ISQ - 669906617
 * VK - https://vk.com/koder_alex
 * It is forbidden to give, sell, modify.
 */
if ($user['id'] == $peer['id'] || !$peer['id']) {
    DB::$pdo->query("DELETE FROM `mail` WHERE `id_user` = '$user[id]' AND `id_kont` = '$peer[id]' OR `id_user` = '$peer[id]' AND `id_kont` = '$user[id]'");
    $q = DB::$pdo->query("SELECT folder, tmp_name FROM mail_files_vk WHERE `action` = 'sent' AND `id_user` = '$user[id]' AND `id_kont` = '$peer[id]' OR `id_user` = '$peer[id]' AND `id_kont` = '$user[id]'");
    while($unlink = $q->fetch())
        unlink(FILES_PATH . 'mail/files/' . $unlink['folder'] . '/' . $unlink['tmp_name']);
    DB::$pdo->query("DELETE FROM `mail_files_vk` WHERE `action` = 'sent' AND `id_user` = '$user[id]' AND `id_kont` = '$peer[id]' OR `id_user` = '$peer[id]' AND `id_kont` = '$user[id]'");

} else {
    $del = DB::$pdo->prepare("DELETE FROM `mail` WHERE `unlink` = '$peer[id]'  AND `id_user` = '$user[id]' AND `id_kont` = '$peer[id]' OR `id_user` = '$peer[id]' AND `id_kont` = '$user[id]' AND `unlink` = '$peer[id]'");
    $del->execute(array());
    if($del->rowCount()) {
        $q = DB::$pdo->query("SELECT folder, tmp_name FROM mail_files_vk WHERE `action` = 'sent' AND `id_user` = '$user[id]' AND `id_kont` = '$peer[id]' OR `id_user` = '$peer[id]' AND `id_kont` = '$user[id]'");
        while($unlink = $q->fetch())
            unlink(FILES_PATH . 'mail/files/' . $unlink['folder'] . '/' . $unlink['tmp_name']);
        DB::$pdo->query("DELETE FROM `mail_files_vk` WHERE `action` = 'sent' AND `id_user` = '$user[id]' AND `id_kont` = '$peer[id]' OR `id_user` = '$peer[id]' AND `id_kont` = '$user[id]'");
    }
    DB::$pdo->query("UPDATE `mail` SET `unlink` = '$user[id]' WHERE  `id_user` = '$user[id]' AND `id_kont` = '$peer[id]' OR `id_user` = '$peer[id]' AND `id_kont` = '$user[id]'");
}
redirect('/mail/', '<b>История переписки удалена</b><br/>Все сообщения диалога с <a href="/id' . $peer['id'] . '">' . $peer['nick'] . '</a> были успешно удалены', 'ok');
?>