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

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

if (isset($_GET['post'])) {
    $file = $db->get_row($db->query("SELECT f.* FROM `mail_files` AS f LEFT JOIN `mail` AS e ON f.`email_id` = e.`id` WHERE f.`md5` = '" . $db->safesql($_GET['md5']) . "' AND e.`time` = '" . (int) $_GET['post'] . "' LIMIT 1"));

    if (!isset($file['id'])) {
        header('Location: /user/mail/messageList.php');
        exit;
    }
}

if (isset($file['id'])) {
    include_once H . 'sys/inc/downloadfile.php';
    if ($file['type'] == 'photo') {
        DownloadFile(H . 'sys/gallery/foto/' . $file['md5'] . '.jpg', retranslit($file['name']) . '.' . $file['ras'], ras_to_mime($file['ras']));
    } else {
        DownloadFile(H . 'sys/mail/' . $file['md5'] . '.dat', retranslit($file['name']) . '.' . $file['ras'], ras_to_mime($file['ras']));
    }
    exit;
}

include_once H . 'sys/inc/user.php';
only_reg();

if (isset($_SESSION['mail'])) {
    $contact['to'] = $_SESSION['mail']['to'];
    $contact['msg'] = $_SESSION['mail']['msg'];
    $contact['attachments'] = $_SESSION['mail']['attachments'];
} else {
    header('Location: /user/mail/messageList.php');
    exit;
}

$config = array(
    'type' => array(
        'photo' => lang('Фото'),
    ),
);

$type = (isset($_GET['type']) && array_key_exists($_GET['type'], $config['type']) ? $_GET['type'] : 'photo');

#Загрузка файла
if (isset($_FILES['file'])) {
    if (is_file(H . 'user/mail/inc/' . $type . '.act.php')) {
        require H . 'user/mail/inc/' . $type . '.act.php';

        if (isset($attach_id)) {
            $_GET['attachments_id'] = $attach_id;
        }
    }
}

if (isset($_GET['attachments_id'])) {
    $attach_id = (int) $_GET['attachments_id'];

    if ($type == 'photo') {

        $photo = $db->get_row($db->query("SELECT * FROM `gallery_foto` WHERE `id` = '$attach_id' AND `id_user` = '$user[id]' LIMIT 1"));

        if (isset($photo['id'])) {
            $_SESSION['mail']['attachments']['photo'][$attach_id] = array(
                'fileId' => $photo['id'],
                'filePatch' => H . 'sys/gallery/foto/' . $photo['id'] . '.' . $photo['ras'],
                'fileSize' => filesize(H . 'sys/gallery/foto/' . $photo['id'] . '.' . $photo['ras']),
                'screenPatch' => '/modules/foto/foto48/' . $photo['id'] . '.' . $photo['ras'],
                'fileNameSend' => retranslit($photo['name']) . '.' . $photo['ras'],
                'fileName' => $photo['name'],
                'fileType' => $photo['type'],
                'fileRas' => $photo['ras'],
                'fileUrl' => '/modules/foto/' . $user['id'] . '/' . $photo['id_gallery'] . '/' . $photo['id'] . '/',
            );
        }
    }

    if (isset($_SESSION['mail']['to'])) {
        header('Location: messageList.php?contact=' . $_SESSION['mail']['to']);
        exit;
    }
}

if (isset($_GET['delete'])) {
    $attach_id = (int) $_GET['delete'];
    if (isset($_SESSION['mail']['attachments'][$type][$attach_id])) {
        unset($_SESSION['mail']['attachments'][$type][$attach_id]);
    }
    header('Location: messageList.php?contact=' . $_SESSION['mail']['to']);
    exit;
}


$set['title'] = stripcslashes(htmlspecialchars($contact['to'])) . '/Вложения';
require_once H . 'sys/inc/thead.php';

aut();
err();
?>
<link rel="stylesheet" href="style/css/email.css" type="text/css" />

<div class="list-group-item-null">Фото</div>
<?php
if (is_file(H . 'user/mail/inc/' . $type . '.form.php')) {
    require H . 'user/mail/inc/' . $type . '.form.php';
}

echo "<div class='list-group-item-null list-group-item-info'>" . lang('Новый файл будет загружен в специальную директорию - Вложения, в которую заходите только вы. Вы всегда сможете перенести его оттуда') . ".</div>";

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