<?php

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

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

$foto['id'] = intval($_GET['foto']);
if (mysql_result(mysql_query("SELECT COUNT(*) FROM `gallery_foto` WHERE `id` = '$foto[id]' LIMIT 1"), 0) == 0) {
    header("Location: /sys/gallery/tmp/$user[id]/?" . SID);
    exit;
}
$foto = mysql_fetch_assoc(mysql_query("SELECT * FROM `gallery_foto` WHERE `id` = '$foto[id]'  LIMIT 1"));
$gallery = mysql_fetch_assoc(mysql_query("SELECT * FROM `gallery` WHERE `id` = '$foto[id_gallery]' LIMIT 1"));
if ($gallery['id_user'] != $user['id']) {
    header("Location: /sys/gallery/tmp/$user[id]/?" . SID);
    exit;
}

$set['title'] = lang('Редактор фото');
require_once H . 'sys/inc/thead.php';

if (!isset($_SESSION['y_o'])) {
    $_SESSION['y_o'] = 0;
}
if (!isset($_SESSION['x_o'])) {
    $_SESSION['x_o'] = 0;
}
if (!isset($_SESSION['w_o'])) {
    $_SESSION['w_o'] = 0;
}
if (!isset($_SESSION['id'])) {
    $_SESSION['id'] = 0;
}
$id = $_SESSION['id'];

define('DIR_FOTO', '/modules/foto/');

if (isset($_GET['get'])) {

    function resize($file_input, $file_output, $w_o, $h_o, $percent = false) {
        list($w_i, $h_i, $type) = getimagesize($file_input);
        if (!$w_i || !$h_i) {
            echo 'Невозможно получить длину и ширину изображения';
            return;
        }
        $types = array('', 'gif', 'jpeg', 'png');
        $ext = $types[$type];
        if ($ext) {
            $func = 'imagecreatefrom' . $ext;
            $img = $func($file_input);
        } else {
            echo 'Некорректный формат файла';
            return;
        }
        if ($percent) {
            $w_o *= $w_i / 100;
            $h_o *= $h_i / 100;
        } if (!$h_o) {
            $h_o = $w_o / ($w_i / $h_i);
        }
        if (!$w_o) {
            $w_o = $h_o / ($h_i / $w_i);
        }
        $img_o = imagecreatetruecolor($w_o, $h_o);

        if (isset($_GET['zoom']) && $_GET['zoom'] == 'increase') {
            $_SESSION['w_o'] = $_SESSION['w_o'] + 20;
        } else
        if (isset($_GET['zoom']) && $_GET['zoom'] == 'reduce') {
            $_SESSION['w_o'] = $_SESSION['w_o'] - 20;
        } else {
            $_SESSION['w_o'] = $_SESSION['w_o'];
        }
        $zoom = $_SESSION['w_o'];

        imagecopyresampled($img_o, $img, 0, 0, 0, 0, $w_o + $zoom, $h_o + $zoom, $w_i + $zoom, $h_i + $zoom);
        if ($type == 2) {
            return imagejpeg($img_o, $file_output, 100);
        } else {
            $func = 'image' . $ext;
            return $func($img_o, $file_output);
        }
    }

    function crop($file_input, $file_output, $crop = 'square', $percent = false) {
        list($w_i, $h_i, $type) = getimagesize($file_input);
        if (!$w_i || !$h_i) {
            echo 'Невозможно получить длину и ширину изображения';
            return;
        }
        $types = array('', 'gif', 'jpeg', 'png');
        $ext = $types[$type];
        if ($ext) {
            $func = 'imagecreatefrom' . $ext;
            $img = $func($file_input);
        } else {
            echo 'Некорректный формат файла';
            return;
        }
        if ($crop == 'square') {
            $min = $w_i;
            if ($w_i > $h_i) {
                $min = $h_i;
            }
            $w_o = $h_o = $min;
        } else {
            list($x_o, $y_o, $w_o, $h_o) = $crop;
            if ($percent) {
                $w_o *= $w_i / 100;
                $h_o *= $h_i / 100;
                $x_o *= $w_i / 100;
                $y_o *= $h_i / 100;
            }
            if ($w_o < 0) {
                $w_o += $w_i;
            }
            $w_o -= $x_o;
            if ($h_o < 0) {
                $h_o += $h_i;
            }
            $h_o -= $y_o;
        }

        if (isset($_GET['n_o'])) { // Вниз
            $_SESSION['y_o'] = $_SESSION['y_o'] + 25;
        } else
        if (isset($_GET['v_o'])) { // Вверх
            $_SESSION['y_o'] = $_SESSION['y_o'] - 25;
        } else {
            $_SESSION['y_o'] = $_SESSION['y_o'];
        }
        $y = $_SESSION['y_o'];

        if (isset($_GET['p_o'])) { // Вправо
            $_SESSION['x_o'] = $_SESSION['x_o'] + 25;
        } else
        if (isset($_GET['l_o'])) { // Влево
            $_SESSION['x_o'] = $_SESSION['x_o'] - 25;
        } else {
            $_SESSION['x_o'] = $_SESSION['x_o'];
        }
        $x = $_SESSION['x_o'];

        $img_o = imagecreatetruecolor($w_o, $h_o);
        imagecopy($img_o, $img, 0, 0, $x_o + $x, $y_o + $y, $w_o, $h_o);

        if ($type == 2) {
            return imagejpeg($img_o, $file_output, 100);
        } else {
            $func = 'image' . $ext;
            return $func($img_o, $file_output);
        }
    }

    @unlink(H . "sys/gallery/tmp/$foto[id].$id.jpg");
    $id = mt_rand(1000000, 9999999);
    $_SESSION['id'] = $id;

    if (isset($_GET['ok'])) {
        @unlink(H . "sys/gallery/avatar/$foto[id].jpg");
        @unlink(H . "sys/gallery/50/$foto[id].jpg");

        crop(H . "sys/gallery/640/$foto[id].jpg", H . "sys/gallery/avatar/$foto[id].tmp.jpg");
        resize(H . "sys/gallery/avatar/$foto[id].tmp.jpg", H . "sys/gallery/avatar/$foto[id].jpg", 150, 150);

        @chmod(H . "sys/gallery/avatar/$foto[id].jpg", 0777);
        @unlink(H . "sys/gallery/avatar/$foto[id].tmp.jpg");

        $_SESSION['y_o'] = 0;
        $_SESSION['x_o'] = 0;
        $_SESSION['w_o'] = 0;
        $_SESSION['id'] = 0;
        $_SESSION['message'] = lang('Изменения сохранены');
        header("Location: /modules/foto/$user[id]/$gallery[id]/$foto[id]/?save" . SID);
        exit;
    } elseif (isset($_GET['del'])) {
        $_SESSION['y_o'] = 0;
        $_SESSION['x_o'] = 0;
        $_SESSION['w_o'] = 0;
        $_SESSION['id'] = 0;

        header("Location: /modules/foto/$user[id]/$gallery[id]/$foto[id]/" . SID);
    } else {
        crop(H . "sys/gallery/640/$foto[id].jpg", H . "sys/gallery/tmp/$foto[id].$id.jpg");
        resize(H . "sys/gallery/tmp/$foto[id].$id.jpg", H . "sys/gallery/tmp/$foto[id].$id.jpg", 150, 150);
        @chmod(H . "sys/gallery/tmp/$foto[id].$id.jpg", 0777);
    }

    header("Location: ?foto=$foto[id]" . SID);
    exit;
}

err();

aut(); // форма авторизации

#Навигация
echo "<div class='list-group-item-null list-group-item-grey'><small>";
echo "<a href='/' class='hint--right' data-hint='" . lang('На главную') . "'><i class='fa fa-home fa-lg'></i></a> <i class='fa fa-angle-right fa-fw'></i> ";
echo "<a href='" . DIR_FOTO . "'>" . lang('Фотоальбомы') . "</a> <i class='fa fa-angle-right fa-fw'></i> ";
echo "<a href='" . DIR_FOTO . "$user[id]/'>" . lang('Альбомы') . " $user[nick]</a> <i class='fa fa-angle-right fa-fw'></i> ";
echo "<a href='" . DIR_FOTO . "$user[id]/$gallery[id]/'>" . cutStr($gallery['name'], 30) . "</a> <i class='fa fa-angle-right fa-fw'></i> ";
echo "<a href='" . DIR_FOTO . "$user[id]/$gallery[id]/$foto[id]/'>" . cutStr($foto['name'], 30) . "</a> <i class='fa fa-angle-right fa-fw'></i> ";
echo lang('Редактор');
echo "</small></div><br />";

echo "<div class='list-group-item-null'><center>";
echo "<table>";
echo "<tr>";
echo "<td class='list-group-item-null'>";
echo "<a href='?foto=$foto[id]&amp;get&amp;zoom=increase' class='btn btn-secondary btn-sm'><i class='fa fa-search-plus fa-fw'></i> Увеличить</a> ";

echo "<a href='?foto=$foto[id]&amp;get&amp;zoom=reduce' class='btn btn-secondary btn-sm'><i class='fa fa-search-minus fa-fw'></i> Уменьшить</a>";
echo "</td>";
echo "</tr>";
echo "</table>";

echo "<table>";
echo "<tr>";
echo "<td class='list-group-item-null'>";
echo "<a href='?foto=$foto[id]&amp;get&amp;v_o' class='btn btn-info btn-sm'><i class='fa fa-chevron-up fa-fw'></i></a>";
echo "</td>";
echo "</tr>";
echo "</table>";

echo "<table>";
echo "<tr>";
echo "<td class='list-group-item-null'>";
echo "<a href='?foto=$foto[id]&amp;get&amp;l_o' class='btn btn-info btn-sm'><i class='fa fa-chevron-left fa-fw'></i></a>";
echo "</td>";
echo "<td class='icon14'>";
echo "<img style='max-width:150px;' src='/modules/foto/foto777/$foto[id].$id.jpg' alt='Превью'>";
echo "</td>";
echo "<td class='list-group-item-null'>";
echo "<a href='?foto=$foto[id]&amp;get&amp;p_o' class='btn btn-info btn-sm'><i class='fa fa-chevron-right fa-fw'></i></a>";
echo "</td>";
echo "</tr>";
echo "</table>";

echo "<table>";
echo "<tr>";
echo "<td class='list-group-item-null'>";
echo "<a href='?foto=$foto[id]&amp;get&amp;n_o' class='btn btn-info btn-sm'><i class='fa fa-chevron-down fa-fw'></i></a><br />";
echo "</td>";
echo "</tr>";
echo "</table>";

echo "<table>";
echo "<tr>";
echo "<td class='list-group-item-null'>";
echo "<a href='?foto=$foto[id]&amp;get&amp;ok' class='btn btn-success'><i class='fa fa-save fa-fw'></i> Сохранить</a> ";

echo "<a href='?foto=$foto[id]&amp;get&amp;del' class='btn btn-secondary'>Отменить</a>";
echo "</td>";
echo "</tr>";
echo "</table>";
echo "</center></div>";

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