<?php

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

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

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

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

$ank = get_user($ank['id']);

if (!$ank) {
    header("Location: " . DIR_FOTO . "?" . SID);
    exit;
}

$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']);

if (isset($user) && isset($_GET['save']) && $user['id'] == $ank['id']) {
    crop(H . "sys/gallery/avatar/$foto[id].jpg", H . "sys/gallery/50/$foto[id].tmp.jpg");
    resize(H . "sys/gallery/50/$foto[id].tmp.jpg", H . "sys/gallery/50/$foto[id].jpg", 50, 50);

    @chmod(H . "sys/gallery/50/$foto[id].jpg", 0777);
    @unlink(H . "sys/gallery/50/$foto[id].tmp.jpg");
    $_SESSION['message'] = lang('Изменения сохранены');
    header("Location: ?" . SID);
    exit;
}

$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;
}


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

if (isset($_GET['fav']) && $_GET['fav'] == 1) {
    $b = $db->super_query("SELECT COUNT(*) as count FROM `bookmark_foto` WHERE `id_user` = '" . $user['id'] . "' AND `id_foto` = '" . $foto['id'] . "' LIMIT 1");

    if ($b['count'] == 0) {
        $db->query("INSERT INTO `bookmark_foto` (`id_foto`, `id_user`, `time`) VALUES ('$foto[id]', '$user[id]', '$time')");

        $_SESSION['message'] = lang('Фото добавлено в закладки');
        header("Location: " . DIR_FOTO . "$ank[id]/$gallery[id]/$foto[id]/?page=" . intval($_GET['page']));
        exit;
    }
}

if (isset($_GET['fav']) && $_GET['fav'] == 0) {
    $b = $db->super_query("SELECT COUNT(*) as count FROM `bookmark_foto` WHERE `id_user` = '" . $user['id'] . "' AND `id_foto` = '" . $foto['id'] . "' LIMIT 1");

    if ($b['count'] == 1) {
        $db->query("DELETE FROM `bookmark_foto` WHERE `id_user` = '$user[id]' AND  `id_foto` = '$foto[id]' ");
        $_SESSION['message'] = lang('Фото удалено из закладок');

        header("Location: " . DIR_FOTO . "$ank[id]/$gallery[id]/$foto[id]/?page=" . intval($_GET['page']));
        exit;
    }
}

if (isset($_GET['like']) && $_GET['like'] == 1) {
    $l = $db->super_query("SELECT COUNT(*) as count FROM `gallery_like` WHERE `id_foto` = '$foto[id]' AND `id_user` = '$user[id]' LIMIT 1");

    if ($l['count'] != 0) {
        
    } else {
        $db->query("INSERT INTO `gallery_like` (`id_user`, `id_foto`, `like`) values('$user[id]', '$foto[id]', '1')");
    }
    if (isset($user) && $user['id'] != $ank['id']) {
        $msg = "$SexOcen " . lang('Ваше') . " [url=" . DIR_FOTO . "$ank[id]/$gallery[id]/$foto[id]/]" . lang('фото') . "[/url]
        [img]" . DIR_FOTO . "foto50/$foto[id].$foto[ras][/img].";
        $db->query("INSERT INTO `notification` (`id_user`, `id_kont`, `id_img`, `msg`, `time`) values('$user[id]', '$ank[id]', '1', '$msg', '$time')");
    }
    header("Location: " . DIR_FOTO . "$ank[id]/$gallery[id]/$foto[id]/?page=" . intval($_GET['page']));
    exit;
}

if (isset($_GET['like']) && $_GET['like'] == 0) {
    $db->query("DELETE FROM `gallery_like` WHERE `id_user` = '$user[id]' AND `id_foto` = '$foto[id]' LIMIT 1");

    header("Location: " . DIR_FOTO . "$ank[id]/$gallery[id]/$foto[id]/?page=" . intval($_GET['page']));
    exit;
}

$set['title'] = $ank['nick'] . ' - ' . $gallery['name'] . ' - ' . $foto['name'];

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

aut();
err();

if (access('foto_foto_edit') && $ank['level'] > $user['level'] || isset($user) && $ank['id'] == $user['id']) {
    include 'inc/gallery_show_foto_act.php';
}

$gallery_rating = $db->super_query("SELECT COUNT(*) as count FROM `gallery_rating` WHERE `id_user` = '$user[id]' AND `id_foto` = '$foto[id]'");

if (isset($user) && $user['id'] != $ank['id'] && $gallery_rating['count'] == 0) {
    if (isset($_GET['rating']) && $_GET['rating'] > 0 && $_GET['rating'] < 7) {
        $c = $db->super_query("SELECT COUNT(*) as count FROM `user_set` WHERE `ocenka_user` = '$user[id]' AND `ocenka_time` > '$time'");
        $c = $c['count'];

        if ($c == 0 && $_GET['rating'] == 6) {
            $_SESSION['message'] = lang('Необходимо активировать услугу');
            header("Location: /modules/shop/plus5.php");
            exit;
        }

        $db->query("INSERT INTO `gallery_rating` (`id_user`, `id_foto`, `like`, `time`, `avtor`) values('$user[id]', '$foto[id]', '" . intval($_GET['rating']) . "', '$time', $foto[id_user])", $db);
        $db->query("UPDATE `gallery_foto` SET `rating` = '" . ($foto['rating'] + intval($_GET['rating'])) . "' WHERE `id` = '$foto[id]' LIMIT 1", $db);
        $_SESSION['message'] = lang('Ваша оценка принята');
        header("Location: " . DIR_FOTO . "$ank[id]/$gallery[id]/$foto[id]/");
        exit;
    }
}

if (IS_WEB) {
    $di = '640';
    $width = '350';
} else {
    $width = '';
    $di = '128';
}

#Навигация
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 cutStr($foto['name'], 30);
echo "</small></div><br />";

if (isset($_POST['password'])) {
    setcookie("passfoto[$gallery[id]]", $_POST['password']);
    if (isset($_POST['password']) && $_POST['password'] == $gallery['password']) {
        header("Location:?enter=ok");
    }
}

if ($gallery['access'] == 'only_me') {
    if ($ank['id'] == $user['id'] && isset($user) || $user['level'] >= 3 && isset($user) || isset($umodd)) {
        
    } else {
        echo "<div class='alert alert-danger'>";
        echo "<i class='fa fa-lock fa-fw'></i> " . lang('Вам запрещено просматривать это фото');
        echo "</div>";

        require_once H . 'sys/inc/tfoot.php';
        exit;
    }
} elseif ($gallery['access'] == 'friends') {
    $f = $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 ($ank['id'] == $user['id'] && isset($user) || isset($umodd) || $f['count'] != 0) {
        
    } else {
        echo "<div class='alert alert-danger'>";
        echo "<i class='fa fa-users fa-fw'></i> " . lang('Фото доступно только друзьям автора');
        echo "</div>";

        require_once H . 'sys/inc/tfoot.php';
        exit;
    }
} elseif ($gallery['access'] == 'pass') {
    if (isset($_COOKIE['passfoto'][$gallery['id']]) && $_COOKIE['passfoto'][$gallery['id']] == $gallery['password'] || $ank['id'] == $user['id'] && isset($user) || isset($umodd)) {
        
    } else {
        if (isset($_POST['password']) && $_POST['password'] != $gallery['password']) {
            $_SESSION['err'] = lang('Пароль неправильный');
        }
        err();

        echo "<form class='list-group-item-null' method='post'>";


        echo lang('Чтобы просмотреть фотоальбом, введите пароль') . ":<br />\n";
        $doc->Input('password', 'Пароль', 16);
        $doc->Button('btn btn-primary btn-sm', null, null, 'Далее');

        echo "</form>";

        require_once H . 'sys/inc/tfoot.php';
        exit;
    }
} elseif ($gallery['access'] == 'auth') {
    if (isset($user)) {
        
    } else {
        echo "<div class='alert alert-danger'><i class='fa fa-user fa-fw'></i> " . lang('Фотоальбом доступен только авторизованным пользователям') . "!</div>";

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

if (isset($user)) {
    $user['ank_age'] = 0;
    if ($user['ank_d_r'] != NULL && $user['ank_m_r'] != NULL && $user['ank_g_r'] != NULL) {
        $user['ank_age'] = date("Y") - $user['ank_g_r'];
        if (date("n") < $user['ank_m_r']) {
            $user['ank_age'] = $user['ank_age'] - 1;
        } elseif (date("n") == $user['ank_m_r'] && date("j") < $user['ank_d_r']) {
            $user['ank_age'] = $user['ank_age'] - 1;
        }
    }
}
if (!isset($_SESSION['foto_adult']) && !isset($_GET['passed']) && $user['ank_age'] < 18 && $foto['adult'] == 1) {
    if (!isset($user)) {
        echo "<div class='list-group-item-null'>";
        echo lang('Фото с меткой') . " <font color='red'>(+18)</font> " . lang('доступны только для авторизованных пользователей. Пожалуйста, пройдите процесс') . " <a href='/aut.php'>" . lang('авторизации') . "</a> или <a href='/reg.php'>" . lang('регистрации') . "</a>";
        echo "</div>";

        require_once H . 'sys/inc/tfoot.php';
        exit;
    }
    echo "<div class='list-group-item-null'>";
    echo lang('Внимание') . '! ' . lang('Это содержимое категории') . ' 18+!<br />' . lang('После нажатия, Ваш выбор будет отправлен администрации') . "<br/>";
    echo lang('Нажимая ДА, Вы подтверждаете, что Вам 18 или более лет') . ".<br />" . lang('Если Вам менее 18 лет - нажмите НЕТ') . ".<br /><br />";

    $doc->Link('btn btn-primary btn-sm', '?passed', null, 'Подтверждаю');
    $doc->Link('btn btn-sm btn-secondary', "" . DIR_FOTO . "$ank[id]/", null, 'Нет');

    echo "</div>";

    require_once H . 'sys/inc/tfoot.php';
    exit;
}
if (!isset($_SESSION['foto_adult']) && isset($_GET['passed']) && $foto['adult'] == 1) {
    $_SESSION['foto_adult'] = 1;
}

$foto = $db->get_row($db->query("SELECT * FROM `gallery_foto` WHERE `id` = $foto[id] LIMIT 1"));
$rat = $db->super_query("SELECT COUNT(*) as count FROM `gallery_rating` WHERE `id_foto` = $foto[id] AND `like` = '6'");
$rat = $rat['count'];
$r = $db->get_row($db->query("SELECT * FROM `gallery_foto` WHERE `id_gallery` = '$gallery[id]' AND `id` < '$foto[id]' ORDER BY `id` DESC LIMIT 1"));
$l = $db->get_row($db->query("SELECT * FROM `gallery_foto` WHERE `id_gallery` = '$gallery[id]' AND `id` > '$foto[id]' ORDER BY `id`  ASC LIMIT 1"));

if ($r['id'] || $l['id']) {
    echo "<div class=\"list-group-item-null list-group-item-info\">\n";
    if ($l['id']) {
        echo "<a href='" . DIR_FOTO . "$ank[id]/$gallery[id]/$l[id]/' style='float: left'><i class='fa fa-chevron-left fa-fw'></i> Пред.</a>";
    }

    if ($r['id']) {
        echo "<a href='" . DIR_FOTO . "$ank[id]/$gallery[id]/$r[id]/' style='float: right'>След. <i class='fa fa-chevron-right fa-fw'></i></a>";
    }
    echo "<div class='clear'></div>";
    echo "</div>\n";
}

echo "<div style='font-size: 18px;font-weight: bold;' class = 'list-group-item-null'>" . lang('Фотография') . " <a style='float:right;' href = '" . DIR_FOTO . "foto0/$foto[id].$foto[ras]'><i class='fa fa-search-plus fa-fw'></i></a></div>";

echo "<div class='list-group-item-null' style='text-align: center;'>\n";

if (IS_WEB) {
    echo "<div style='display:inline;'><img class='show_foto' src='" . DIR_FOTO . "foto0/$foto[id].$foto[ras]' style = '-moz-box-shadow: 0 0 10px rgba(0,0,0,0.5);-webkit-box-shadow: 0 0 10px rgba(0,0,0,0.5);box-shadow: 0 0 10px rgba(0,0,0,0.5);border: 1px solid #CCDDED; padding: 2px; border-radius: 5px; max-width: 450px; max-height: 450px; ' alt='$foto[name]' /></div>\n";
} else {
    echo "<div style='display:inline;'><img class='show_foto' src='" . DIR_FOTO . "foto0/$foto[id].$foto[ras]' style = '-moz-box-shadow: 0 0 10px rgba(0,0,0,0.5);-webkit-box-shadow: 0 0 10px rgba(0,0,0,0.5);box-shadow: 0 0 10px rgba(0,0,0,0.5);border: 1px solid #CCDDED; padding: 2px; border-radius: 5px; max-width: 200px; max-height: 200px;' alt='$foto[name]' /></div>\n";
}
if ($rat > 0) {
    echo "<div style='display:inline;margin-left:-25px;vertical-align:top;'><img style='padding-top:10px;' src='/style/icons/6.png'/></div>";
}
if ($foto['opis'] != null) {
    echo "<br />" . toOutput($foto['opis']) . "<br />\n";
}
echo "</div>";

$k_p = $db->super_query("SELECT COUNT(*) as count FROM `gallery_frend` where `id_foto` = '$foto[id]'");
$k_p = $k_p['count'];

if ($k_p > 0) {
    echo "<div class='list-group-item-null'>";

    if ($k_p == 1) {
        $ot = lang('отмечен');
    } else {
        $ot = lang('отмечены');
    }
    echo "<small style='color: grey;'>" . lang('На этом фото') . " $ot </small><br />";
    $q = $db->query("SELECT * FROM `gallery_frend` where `id_foto` = '$foto[id]'");
    $n = 0;
    while ($post = $db->get_row($q)) {
        $n++;
        $f = get_user($post['id_user']);
        if ($f['id'] != $user['id']) {
            echo " <a href='/id$f[id]'>$f[nick]</a>, ";
        } else {
            echo "<b>Только Вы</b>";
        }
    }
    echo "</div>";
}
if (isset($user)) {

    $like = $db->super_query("SELECT COUNT(*) as count FROM `gallery_like` WHERE `id_foto` = '$foto[id]' LIMIT 1");
    $like = $like['count'];

    if ($like > 0) {
        $like = $like;
    } else {
        $like = null;
    }

    $bookmark = $db->super_query("SELECT COUNT(*) as count FROM `bookmark_foto` WHERE `id_foto` = '$foto[id]' LIMIT 1");
    $bookmark = $bookmark['count'];
    $gallery_like = $db->super_query("SELECT COUNT(*) as count FROM `gallery_like` WHERE `id_foto` = '$foto[id]' LIMIT 1");
    $gallery_like = $gallery_like['count'];

    echo "<div class='list-group-item-null list-group-item-grey'>";

    $q = $db->super_query("SELECT COUNT(*) as count FROM `bookmark_foto` WHERE `id_user` = '" . $user['id'] . "' AND `id_foto` = '" . $foto['id'] . "' LIMIT 1");
    if ($q['count'] == 0) {
        echo "<a href='?fav=1' class='btn btn-secondary btn-sm hint--top' data-hint='" . lang('Добавить в закладки') . "'><i class='fa fa-bookmark-o fa-fw'></i> $bookmark</a> ";
    } else {
        echo "<a href='?fav=0' class='btn btn-secondary btn-sm hint--top' data-hint='" . lang('Убрать из закладок') . "'><i class='fa fa-bookmark fa-fw'></i> $bookmark</a> ";
    }
    $q = $db->super_query("SELECT COUNT(*) as count FROM `gallery_like` WHERE `id_foto` = '$foto[id]' AND `id_user` = '$user[id]' LIMIT 1");

    if ($q['count'] == 0) {
        echo "<a href='?like=1' class='btn btn-secondary btn-sm hint--top' data-hint='" . lang('Мне нравится') . "'><i class='fa fa-heart-o fa-fw'></i> $like</a> ";
    } else {
        echo "<a href='?like=0' class='btn btn-secondary btn-sm hint--top' data-hint='" . lang('Не нравится') . "'><i class='fa fa-heart fa-fw'></i> $like</a> ";
    }
    echo "<a class='btn btn-secondary btn-sm hint--top' data-hint='" . lang('Рейтинг') . "'><i class='fa fa-star fa-fw'></i> $foto[rating]</a> ";

    echo "</div>";
}
$q = $db->super_query("SELECT COUNT(*) as count FROM `gallery_rating` WHERE `id_user` = '$user[id]' AND `id_foto` = '$foto[id]'");

if (isset($user) && $user['id'] != $ank['id'] && $q['count'] == 0) {
    echo "<div class='list-group-item-null list-group-item-info'><center>";
    echo "<a href=\"?rating=1\" class='hint--top' data-hint='" . lang('Ваша оценка 1?') . "'><img src='/style/icons/1.png' alt=''/></a> ";
    echo "<a href=\"?rating=2\" class='hint--top' data-hint='" . lang('Ваша оценка 2?') . "'><img src='/style/icons/2.png' alt=''/></a> ";
    echo "<a href=\"?rating=3\" class='hint--top' data-hint='" . lang('Ваша оценка 3?') . "'><img src='/style/icons/3.png' alt=''/></a> ";
    echo "<a href=\"?rating=4\" class='hint--top' data-hint='" . lang('Ваша оценка 4?') . "'><img src='/style/icons/4.png' alt=''/></a> ";
    echo "<a href=\"?rating=5\" class='hint--top' data-hint='" . lang('Ваша оценка 5?') . "'><img src='/style/icons/5.png' alt=''/></a> ";
    echo "<a href=\"?rating=6\" class='hint--top' data-hint='" . lang('Ваша оценка 5') . "+?'><img src='/style/icons/6.png' alt=''/></a> ";
    echo "</center></div>";
} else {
    $rate = $db->get_row($db->query("SELECT * FROM `gallery_rating` WHERE `id_foto` = $foto[id] AND `id_user` = '$user[id]' LIMIT 1"));
    if (isset($user) && $user['id'] != $ank['id']) {
        echo "<div class='list-group-item-null list-group-item-info'>";
        echo lang('Ваша оценка') . " <img style='vertical-align:middle;' src='/style/icons/$rate[like].png' alt=''/></a>";
        echo "</div>";
    }
    unset($rate);
}

if (access('foto_foto_edit') && $ank['level'] > $user['level'] || isset($user) && $ank['id'] == $user['id']) {
    include 'inc/gallery_show_foto_form.php';
}

$komm = $db->super_query("SELECT COUNT(*) as count FROM `gallery_komm` WHERE `id_foto` = '$foto[id]'");

echo "<div class='list-group-item-null list-group-item-grey'>";
$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 "<a href='" . DIR_FOTO . "$ank[id]/$gallery[id]/komm/$foto[id]/'>";
    echo "<i class='fa fa-comments fa-fw'></i> " . lang('Показать все комментарии') . " <span class='tag tag-info'>$komm[count]</span>";
    echo "</a>";
} else {
    echo "<i class='fa fa-comments fa-fw'></i> " . lang('Комментарии') . " <span class='tag tag-info'>$komm[count]</span>";
}
unset($q);
echo "</div>";


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: ?");
    }
}

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: ?");
        exit;
    }
}

err();

$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 ($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);
}
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', null, 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>";
    }
}

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