<?php

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

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

if (isset($_GET['id']) && is_numeric($_GET['id'])) {
    $q = $db->super_query("SELECT COUNT(*) as count FROM `diary` WHERE `id` = '" . intval($_GET['id']) . "' LIMIT 1");
    if ($q['count'] != 0) {
        $diary = $db->get_row($db->query("SELECT * FROM `diary` WHERE `id`='" . intval($_GET['id']) . "'"));
        $us = get_user($diary['id_user']);
        $set['title'] = '' . $diary['name'] . ' - ' . lang('Комментарии');
        require_once H . 'sys/inc/thead.php';

        $q = $db->super_query("SELECT COUNT(*) as count FROM `friends` WHERE (`user` = '$user[id]' AND `friends` = '$us[id]') OR (`user` = '$us[id]' AND `friends` = '$user[id]')");
        if (isset($_POST['msg']) && isset($user) && ($us['id'] == $user['id'] || $user['level'] > $us['level'] || $diary['readers'] == 0 || ($diary['readers'] == 1 || $diary['readers'] == 2) && $q['count'] != 0)) {
            $msg = esc(stripcslashes(htmlspecialchars($_POST['msg'])));

            $q = $db->super_query("SELECT COUNT(*) as count FROM `diary_komm` WHERE `id_diary` = '$diary[id]' AND `id_user` = '$user[id]' AND `msg` = '" . $db->safesql($msg) . "' LIMIT 1");

            if (utf8_strlen($msg) > 10024) {
                $err = lang('Сообщение слишком длинное');
            } elseif (utf8_strlen($msg) < 2) {
                $err = lang('Короткое сообщение');
            } elseif ($q['count'] != 0) {
                $err = lang('Ваше сообщение повторяет предыдущее');
            } else {
                $db->query("INSERT INTO `diary_komm` (`id_diary`, `id_user`, `time`, `msg`) values('$diary[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");

                if ($user['id'] != $us['id']) {
                    $db->query("INSERT INTO `notification` (`id_user`, `id_kont`, `msg`, `time`) values('$user[id]', '$us[id]', '$SexOst комментарий в блоге [url=/modules/diary/$diary[name]/]$diary[name][/url]', '$time')");
                }
                $q = $db->query("SELECT * FROM `friends` WHERE `user` = '$user[id]' AND `lenta_diary` = '1' AND `i`='1'");
                while ($f = $db->get_row($q)) {
                    $a = $db->get_row($db->query("SELECT * FROM `user` WHERE `id` = '$f[friends]' LIMIT 1"));
                    $msg_lenta = "$SexDob комментарий в блоге [url=/modules/diary/$diary[name]/]$diary[name][/url]";
                    $db->query("INSERT INTO `notifications` (`id_user`, `id_kont`, `msg`, `time`) values('$user[id]', '$a[id]', '$msg_lenta', '$time')");
                }

                $_SESSION['message'] = lang('Комментарий успешно оставлен');

                header("Location: ?id=$diary[id]");
                exit();
            }
        } elseif (isset($_GET['del'])) {
            $q = $db->super_query("SELECT COUNT(*) as count FROM `diary_komm` WHERE `id` = '" . intval($_GET['del']) . "' AND `id_diary` = '$diary[id]'");
            if ($q['count']) {
                if (isset($user) && ($user['level'] >= 3 || $user['id'] = $diary['id_user'])) {
                    $db->query("DELETE FROM `diary_komm` WHERE `id` = '" . intval($_GET['del']) . "' LIMIT 1");

                    $_SESSION['message'] = lang('Комментарий успешно удален');

                    header("Location: ?id=$diary[id]");
                    exit();
                }
            }
        }
        aut();
        err();

        $cat = $db->get_row($db->query("SELECT * FROM `diary_cat` WHERE `id`='$diary[id_cat]' LIMIT 1"));

        #Навигация
        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='/modules/diary/'>" . lang('Блоги') . "</a> <i class='fa fa-angle-right fa-fw'></i> ";
        echo "<a href='/modules/diary/?r=$cat[id]'>" . cutStr($cat['name'], 30) . "</a> <i class='fa fa-angle-right fa-fw'></i> ";
        echo "<a href='/modules/diary/$diary[name]/'>" . cutStr($diary['name'], 30) . "</a> <i class='fa fa-angle-right fa-fw'></i> ";
        echo lang('Комментарии');
        echo "</small></div><br />";

        $q = $db->super_query("SELECT COUNT(*) as count FROM `diary_komm` WHERE `id_diary` = '" . intval($_GET['id']) . "'");
        $k_post = $q['count'];
        $k_page = k_page($k_post, $set['p_str']);
        $page = page($k_page);
        $start = $set['p_str'] * $page - $set['p_str'];
        $q = $db->query("SELECT * FROM `diary_komm` WHERE `id_diary` = '" . intval($_GET['id']) . "' ORDER BY `id` DESC LIMIT $start, $set[p_str]");

        if ($k_post == 0) {
            $doc->NoResult();
        }
        while ($post = $db->get_row($q)) {
            $ank = get_user($post['id_user']);

            echo "<table class='list-group-item-null-mini list-group-item-grey dop'><tr><td class='icon14'>";
            avatar($ank['id'], '48', 'border-radius: 2px;');
            echo "</td><td class='null'>";
            echo "<span class='right grey'><small class='hint--left' data-hint='" . date::time($post['time']) . "'>" . date::timek($post['time']) . "</small></span>";
            echo user($ank['id']) . " ";

            echo "<br />\n";


            if (isset($user)) {
                echo "<span class='right'>";
                if ($user['id'] != $diary['id_user']) {
                    echo " <a href='?id=$diary[id]&amp;komm=$post[id]' class='hint--left' data-hint='" . lang('Ответить') . "'><i class='fa fa-comment-o fa-fw'></i></a> ";
                }
                if ($user['level'] >= 3 || $user['id'] == $diary['id_user']) {
                    echo " <a href='?id=$diary[id]&amp;del=$post[id]' class='hint--left' data-hint='" . lang('Удалить') . "'><i class='fa fa-trash-o fa-fw'></i></a>";
                }
                echo "</span>";
            }

            echo toOutput($post['msg']) . "<br />\n";
            echo "</td></tr></table>";
        }

        if ($k_page > 1) {
            str("komm.php?id=$diary[id]&", $k_page, $page);
        }

        if (isset($_GET['komm']) && intval($_GET['komm']) && isset($user)) {
            $posts = $db->get_row($db->query("SELECT * FROM `diary_komm` WHERE `id`='" . intval($_GET['komm']) . "'"));
            $anks = get_user($posts['id_user']);
            if (isset($user) && ($us['id'] == $user['id'] || $user['level'] > $us['level'] || $diary['readers'] == 0 || ($diary['readers'] == 1 || $diary['readers'] == 2) && mysql_result(mysql_query("SELECT COUNT(*) FROM `friends` WHERE (`user` = '$user[id]' AND `friends` = '$us[id]') OR (`user` = '$us[id]' AND `friends` = '$user[id]')"), 0) != 0)) {
                echo "<form method='post' class='list-group-item-null' name='message' action='?id=$diary[id]&amp;go'>";
                echo auto_bb("message", "msg");
                echo "<textarea placeholder='" . lang('Введите сообщение') . "...' name='msg' class='form-control'>$anks[nick], </textarea><br />";

                $doc->Button('btn btn-primary btn-sm', null, 'pencil', 'Опубликовать');

                echo '</form>';
            }
        } else {
            if (isset($user) && ($us['id'] == $user['id'] || $user['level'] > $us['level'] || $diary['readers'] == 0 || ($diary['readers'] == 1 || $diary['readers'] == 2) && mysql_result(mysql_query("SELECT COUNT(*) FROM `friends` WHERE (`user` = '$user[id]' AND `friends` = '$us[id]') OR (`user` = '$us[id]' AND `friends` = '$user[id]')"), 0) != 0)) {
                echo "<form method='post' class='list-group-item-null' name='message' action='?id=$diary[id]'>";
                echo auto_bb("message", "msg");
                echo "<textarea placeholder='" . lang('Введите сообщение') . "...' name='msg' class='form-control'></textarea><br />";

                $doc->Button('btn btn-primary btn-sm', null, 'pencil', 'Опубликовать');

                echo '</form>';
            }
        }
    }
} else {
    header("Location: index.php?" . SID);
    exit;
}
require_once H . 'sys/inc/tfoot.php';


