<?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['dtype']) && isset($_GET['file_id']) && isset($_GET['hash'])) {
    $dtype = $_GET['dtype'];
    $file_id = $_GET['file_id'];
    $hash = $_GET['hash'];
    
    if ($dtype == 'photo') {
        $strops = explode('_', $hash);
        $photo = mysql_fetch_assoc(mysql_query("SELECT * FROM gallery_foto WHERE id = " . (int) $file_id));
        
        if (isset($photo['id']) && is_array($strops) && filesize(H.'sys/gallery/foto/' . $file_id . '.jpg') == $strops[0]) {
            if ($strops[1] == 640) {
                $cat = '640';
            } elseif ($strops[1] == 128) {
                $cat = '128';
            } elseif ($strops[1] == 50) {
                $cat = '50';
            } else {
                $cat = 'foto';
            }
            
            include_once H.'sys/inc/downloadfile.php';
        		DownloadFile(H.'sys/gallery/' . $cat . '/' . $file_id . '.jpg', 'Фото.jpg', ras_to_mime('jpg'));
        		exit;
        }
    }
}

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

$location = $tools->keyGlobals('location', 'get', '0');

if ($location == '0' || !preg_match('/^([A-z0-9\-\_]+)/i', $location) || !isset($_SESSION[$location]['return'])) {
    header('Location: /index.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('inc/' . $type . '.act.php')) {
    require '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 = 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[$location]['attachments']['photo'][$attach_id] = array(
                'fileId' => $photo['id'],
                'filePatch' => H.'sys/gallery/foto/' . $photo['id'] . '.' . $photo['ras'],
                'fileRoot' => '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[$location])) {
        header('Location: ' . $_SESSION[$location]['return']);
        exit;
    }
}

if (isset($_GET['delete'])) {
    $attach_id = (int) $_GET['delete'];
    if (isset($_SESSION[$location]['attachments'][$type][$attach_id])) {
        unset($_SESSION[$location]['attachments'][$type][$attach_id]);
    }
    
    header('Location: ' . $_SESSION[$location]['return']);
    exit;
}

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