<?php

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

if (!isset($user) && !isset($_GET['id_user'])) {
    header("Location: /modules/foto/?" . SID);
    exit;
}

if (isset($user)) {
    $ank['id'] = $user['id'];
}

if (isset($_GET['id_user'])) {
    $ank['id'] = intval($_GET['id_user']);
}

$q = $db->super_query("SELECT COUNT(*) as count FROM `user` WHERE `id` = '$ank[id]' LIMIT 1");
if ($q['count'] == 0) {
    header("Location: " . DIR_FOTO . "?" . SID);
    exit;
}
unset($q);

$ank = $db->get_row($db->query("SELECT * FROM `user` WHERE `id` = $ank[id] LIMIT 1"));
$gallery['id'] = intval($_GET['id_gallery']);

$q = $db->super_query("SELECT COUNT(*) as count FROM `gallery` WHERE `id` = '$gallery[id]' AND `id_user` = '$ank[id]' LIMIT 1");
if ($q['count'] == 0) {
    header("Location: " . DIR_FOTO . "$ank[id]/?" . SID);
    exit;
}
unset($q);

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

$foto['id'] = intval($_GET['id_foto']);

$q = $db->super_query("SELECT COUNT(*) as count FROM `gallery_foto` WHERE `id` = '$foto[id]' LIMIT 1");
if ($q['count'] == 0) {
    header("Location: " . DIR_FOTO . "$ank[id]/$gallery[id]/?" . SID);
    exit;
}
unset($q);

$foto = $db->get_row($db->query("SELECT * FROM `gallery_foto` WHERE `id` = '$foto[id]'  LIMIT 1"));

$set['title'] = $ank['nick'] . ' - ' . $gallery['name'] . ' - ' . $foto['name'] . ' - ' . lang('Комментарии'); // заголовок страницы

require_once H . 'sys/inc/thead.php';

aut();
err();

#Навигация
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 . "$ank[id]/'>" . lang('Альбомы') . " $ank[nick]</a> <i class='fa fa-angle-right fa-fw'></i> ";
echo "<a href='" . DIR_FOTO . "$ank[id]/$gallery[id]/'>" . cutStr($gallery['name'], 30) . "</a> <i class='fa fa-angle-right fa-fw'></i> ";
echo "<a href='" . DIR_FOTO . "$ank[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 />";

if (isset($_GET['delete'])) {
    $q = $db->super_query("SELECT COUNT(*) as count FROM `gallery_komm` WHERE `id`='" . intval($_GET['delete']) . "' AND `id_foto`='$foto[id]' LIMIT 1");

    if ((access('foto_foto_komm_del') || $ank['id'] == $user['id']) && isset($_GET['delete']) && $q['count'] != 0) {
        $db->query("DELETE FROM `gallery_komm` WHERE `id`='" . intval($_GET['delete']) . "' LIMIT 1");
        admin_log('Фотоальбомы', 'Удаление сообщения', "Удаление сообщения от $anks[nick]");

        $_SESSION['message'] = lang('Сообщение удалено');
        header("Location: " . DIR_FOTO . "$ank[id]/$gallery[id]/komm/$foto[id]/");
        exit;
    }
}

if (isset($_POST['msg']) && isset($user)) {
    $msg = $_POST['msg'];
    $mat = antimat($msg);
    if ($mat) {
        $_SESSION['err'] = lang("В тексте сообщения обнаружен мат: $mat");
    }

    if (utf8_strlen($msg) > 10024) {
        $_SESSION['err'] = lang("Сообщение слишком длинное");
    } elseif (utf8_strlen($msg) < 2) {
        $_SESSION['err'] = lang("Короткое сообщение");
    }
    $q = $db->super_query("SELECT COUNT(*) as count FROM `gallery_komm` WHERE `id_foto` = '$foto[id]' AND `id_user` = '$user[id]' AND `msg` = '" . $db->safesql($msg) . "' LIMIT 1");
    if ($q['count'] != 0) {
        $_SESSION['err'] = lang("Ваше сообщение повторяет предыдущее");
    } elseif (!isset($err)) {

        if ($ank['id'] != $user['id']) {
            $db->query("INSERT INTO `notification` (`id_user`, `id_kont`, `id_img`, `msg`, `time`) values('$user[id]', '$ank[id]', '3', '$SexOst [url=/modules/foto/$ank[id]/$gallery[id]/komm/$foto[id]/]комментарий к Вашему фото[/url]', '$time')");
        }

        $db->query("INSERT INTO `gallery_komm` (`id_foto`, `id_user`, `time`, `msg`) values('$foto[id]', '$user[id]', '$time', '" . $db->safesql($msg) . "')");
        $db->query("UPDATE `user` SET `money` = '" . ($user['money'] + 3) . "', `activity` = '" . ($user['activity'] + 2) . "' WHERE `id` = '$user[id]' LIMIT 1");

        $_SESSION['message'] = lang('Сообщение успешно добавлено');
        header("Location: " . DIR_FOTO . "$ank[id]/$gallery[id]/komm/$foto[id]/");
    }
}

$k_post = $db->super_query("SELECT COUNT(*) as count FROM `gallery_komm` WHERE `id_foto` = '$foto[id]'");
$k_post = $k_post['count'];
$k_page = k_page($k_post, $set['p_str']);
$page = page($k_page);
$start = $set['p_str'] * $page - $set['p_str'];

if (isset($user)) {
    $q = $db->super_query("SELECT COUNT(*) as count FROM `friends` WHERE (`user` = '$user[id]' AND `friends` = '$ank[id]') OR (`user` = '$ank[id]' AND `friends` = '$user[id]')");
    if ($foto['komm'] == 'all' || $foto['komm'] == 'only_me' && ($user['id'] == $ank['id'] || $user['level'] >= 3) || $foto['komm'] == 'friends' && ($ank['id'] == $user['id'] || $user['level'] >= 3 || $q['count'] != 0)) {

        echo "<form method='post' class='list-group-item-null' name='message' action='?$passgen'>\n";
        echo auto_bb("message", "msg");
        $doc->Textarea('msg', 'Сообщение', 10024);
        echo "<br />";
        $doc->Button('btn btn-primary btn-sm', null, 'pencil', 'Опубликовать');

        echo "</form>";
    } else {
        echo "<div class='list-group-item-null list-group-item-info'>";
        echo lang('Автор ограничил круг лиц, которые могут комментировать фотографию');
        echo "</div>";
    }
}
if ($k_post == 0) {
    $doc->NoResult();
}

$q = $db->query("SELECT * FROM `gallery_komm` WHERE `id_foto` = '$foto[id]' ORDER BY `id` ASC LIMIT $start, $set[p_str]");
while ($post = $db->get_row($q)) {
    $ank2 = $db->get_row($db->query("SELECT * FROM `user` WHERE `id` = '$post[id_user]' LIMIT 1"));
    echo "<table class='list-group-item-null list-group-item-grey dop'><tr><td class='icon14'>";
    avatar($ank2['id'], '40', 'border-radius: 2px;');
    echo "</td><td class='null'>\n";
    echo "<div style='float:right;' title='" . date::time($post['time']) . "'><small>" . date::timek($post['time']) . "</small></div>";
    user($ank2['id']);
    echo "<br />";
    if (access('foto_foto_komm_del') || $ank['id'] == $user['id']) {
        echo "<span style='float: right;'>";
        echo "<a href='?delete=$post[id]'><i class='fa fa-trash-o'></i></a>";
        echo "</span>";
    }
    echo toOutput($post['msg']);
    echo "</td></tr></table>\n";
}

if ($k_page > 1) {
    str('?', $k_page, $page);
}

require_once H . 'sys/inc/tfoot.php';
exit;
