<?php 

include_once '../sys/inc/home.php';
include_once H.'sys/inc/start.php';
include_once H.'sys/inc/sess.php';
include_once H.'sys/inc/settings.php';
include_once H.'sys/inc/db_connect.php';
include_once H.'sys/inc/ipua.php';
include_once H.'sys/inc/fnc.php'; 

if (isset($_GET['post'])) {
    $file = mysql_fetch_assoc(mysql_query("SELECT f.* FROM `mail_files` AS f
                                            LEFT JOIN `mail` AS e ON f.`email_id` = e.`id`
                                            WHERE f.`md5` = '" . my_esc($_GET['md5']) . "' AND e.`time` = '" . (int) $_GET['post'] . "'
                                            LIMIT 1"));
    if (!isset($file['id'])) {
        header('Location: /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/files/email/' . $file['md5'] . '.dat', retranslit($file['name']) . '.' . $file['ras'], ras_to_mime($file['ras']));
    }
    exit;
}

$config = array(
    'domain' =>  $set['mail_panel_domain'],
    'aliase' => explode("\n", $set['mail_panel_aliase']),
);

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: /mail/messageList.php');
    exit;
}

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

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

/**
* Загрузка файла
*/

if (isset($_FILES['file'])) {
  if (is_file(H.'mail/inc/attachments/' . $type . '.act.php')) {
    require H.'mail/inc/attachments/' . $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 = mysql_fetch_assoc(mysql_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' => '/foto/foto50/' . $photo['id'] . '.' . $photo['ras'],
                'fileNameSend' => retranslit($photo['name']) . '.' . $photo['ras'],
                'fileName' => $photo['name'],
                'fileType' => $photo['type'],
                'fileRas' => $photo['ras'],
                'fileUrl' => '/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'] = text($contact['to']) . '/Вложения';
include_once H.'sys/inc/thead.php';
title();
aut();
err();
?>
<link rel="stylesheet" href="style/css/email.css" type="text/css" />

<ul class="email-nav"> 
  <li class="active">Фото</li>
</ul>
<?
if (is_file(H.'mail/inc/attachments/' . $type . '.form.php')) {
  require H.'mail/inc/attachments/' . $type . '.form.php';
}
?>
<div class="email email-panel">
Новый файл будет загружен в специальную директорию "Вложения", в которую заходите только вы. Вы всегда сможете перенести его оттуда.
</div>
<?
include_once H.'sys/inc/tfoot.php';