<?php

/* DCMS Special
 * Дата последнего редактирования 27.01.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']);
}

if (mysql_result(mysql_query("SELECT COUNT(*) FROM `user` WHERE `id` = '$ank[id]' LIMIT 1"), 0) == 0) {
    header("Location: /modules/foto/?" . SID);
    exit;
}

$ank = mysql_fetch_assoc(mysql_query("SELECT * FROM `user` WHERE `id` = $ank[id] LIMIT 1"));
$gallery['id'] = intval($_GET['id_gallery']);

if (mysql_result(mysql_query("SELECT COUNT(*) FROM `gallery` WHERE `id` = '$gallery[id]' AND `id_user` = '$ank[id]' LIMIT 1"), 0) == 0) {
    header("Location: /modules/foto/$ank[id]/?" . SID);
    exit;
}

$gallery = mysql_fetch_assoc(mysql_query("SELECT * FROM `gallery` WHERE `id` = '$gallery[id]' AND `id_user` = '$ank[id]' LIMIT 1"));

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

if (mysql_result(mysql_query("SELECT COUNT(*) FROM `gallery_foto` WHERE `id` = '$foto[id]' LIMIT 1"), 0) == 0) {
    header("Location: /modules/foto/$ank[id]/$gallery[id]/?" . SID);
    exit;
}
$foto = mysql_fetch_assoc(mysql_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 ((access('foto_foto_komm_del') || $ank['id'] == $user['id']) && isset($_GET['delete']) && mysql_result(mysql_query("SELECT COUNT(*) FROM `gallery_komm` WHERE `id`='" . intval($_GET['delete']) . "' AND `id_foto`='$foto[id]' LIMIT 1"), 0) != 0) {
    mysql_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) {
        $err[] = lang('В тексте сообщения обнаружен мат') . ': ' . $mat;
    }

    if (utf8_strlen($msg) > 10024) {
        $err = lang('Сообщение слишком длинное');
    } elseif (utf8_strlen($msg) < 2) {
        $err = lang('Короткое сообщение');
    } elseif (mysql_result(mysql_query("SELECT COUNT(*) FROM `gallery_komm` WHERE `id_foto` = '$foto[id]' AND `id_user` = '$user[id]' AND `msg` = '" . mysql_real_escape_string($msg) . "' LIMIT 1"), 0) != 0) {
        $err = lang('Ваше сообщение повторяет предыдущее');
    } elseif (!isset($err)) {
        ###########################################7.2.1
        if ($ank['id'] != $user['id']) {
            mysql_query("INSERT INTO `notification` (`id_user`, `id_kont`, `id_img`, `msg`, `time`) values('$user[id]', '$ank[id]', '4', '$SexOst [url=/modules/foto/$ank[id]/$gallery[id]/komm/$foto[id]/]" . lang('комментарий к Вашему фото') . "[/url]', '$time')");
        }
        #############################################
        mysql_query("INSERT INTO `gallery_komm` (`id_foto`, `id_user`, `time`, `msg`) values('$foto[id]', '$user[id]', '$time', '" . mysql_real_escape_string($msg) . "')");
        mysql_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]/");
        exit;
    }
}

$k_post = mysql_result(mysql_query("SELECT COUNT(*) FROM `gallery_komm` WHERE `id_foto` = '$foto[id]'"), 0);
$k_page = k_page($k_post, $set['p_str']);
$page = page($k_page);
$start = $set['p_str'] * $page - $set['p_str'];
if (isset($user)) {
    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 || mysql_result(mysql_query("SELECT COUNT(*) FROM `friends` WHERE (`user` = '$user[id]' AND `friends` = '$ank[id]') OR (`user` = '$ank[id]' AND `friends` = '$user[id]')"), 0) != 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='alert alert-info'>";
        echo lang('Автор ограничил круг лиц, которые могут оставлять комментарии');
        echo "</div>";
    }
}
if ($k_post == 0) {
    $doc->NoResult();
}

$q = mysql_query("SELECT * FROM `gallery_komm` WHERE `id_foto` = '$foto[id]' ORDER BY `id` ASC LIMIT $start, $set[p_str]");
while ($post = mysql_fetch_assoc($q)) {
    $ank2 = mysql_fetch_assoc(mysql_query("SELECT * FROM `user` WHERE `id` = '$post[id_user]' LIMIT 1"));
    echo "<table class='list-group-item-null'><tr><td class='icon14'>";
    avatar($ank2['id'], '40', 'border-radius: 2px;');
    echo "  </td><td class='null'>\n";
    echo "<div style='float:right;' id='hides' 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;' id='hides'>";
        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";
    echo "<div class='hr'></div>";
}

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

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