<?php

/* DCMS Special
 * Дата последнего редактирования 21.01.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']) && mysql_result(mysql_query("SELECT COUNT(*) FROM `diary` WHERE `id` = '" . intval($_GET['id']) . "' LIMIT 1"), 0) != 0) {
    $diary = mysql_fetch_array(mysql_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';

    if (isset($_POST['msg']) && 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)) {
        $msg = esc(stripcslashes(htmlspecialchars($_POST['msg'])));

        if (utf8_strlen($msg) > 10024) {
            $err = lang('Сообщение слишком длинное');
        } elseif (utf8_strlen($msg) < 2) {
            $err = lang('Короткое сообщение');
        } elseif (mysql_result(mysql_query("SELECT COUNT(*) FROM `diary_komm` WHERE `id_diary` = '$diary[id]' AND `id_user` = '$user[id]' AND `msg` = '" . mysql_real_escape_string($msg) . "' LIMIT 1"), 0) != 0) {
            $err = lang('Ваше сообщение повторяет предыдущее');
        } else {
            mysql_query("INSERT INTO `diary_komm` (`id_diary`, `id_user`, `time`, `msg`) values('$diary[id]', '$user[id]', '$time', '" . mysql_real_escape_string($msg) . "')");
            // Начисление рублей за активность
            mysql_query("UPDATE `user` SET `money` = '" . ($user['money'] + 3) . "' WHERE `id` = '" . $user['id'] . "' LIMIT 1");
            mysql_query("UPDATE `user` SET `activity` = '" . ($user['activity'] + 2) . "' WHERE `id` = '" . $user['id'] . "' LIMIT 1");

            if ($user['id'] != $us['id']) {
                mysql_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 = mysql_query("SELECT * FROM `friends` WHERE `user` = '$user[id]' AND `lenta_diary` = '1' AND `i`='1'");
            while ($f = mysql_fetch_array($q)) {
                $a = mysql_fetch_array(mysql_query("SELECT * FROM `user` WHERE `id` = '$f[friends]' LIMIT 1"));
                $msg_lenta = "$SexDob комментарий в блоге [url=/modules/diary/$diary[name]/]$diary[name][/url]";
                mysql_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']) && mysql_result(mysql_query("SELECT COUNT(*) FROM `diary_komm` WHERE `id` = '" . intval($_GET['del']) . "' AND `id_diary` = '$diary[id]'"), 0)) {
        if (isset($user) && ($user['level'] >= 3 || $user['id'] = $diary['id_user'])) {
            mysql_query("DELETE FROM `diary_komm` WHERE `id` = '" . intval($_GET['del']) . "' LIMIT 1");

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

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

    $cat = mysql_fetch_assoc(mysql_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 />";

    $k_post = mysql_result(mysql_query("SELECT COUNT(*) FROM `diary_komm` WHERE `id_diary` = '" . intval($_GET['id']) . "'"), 0);
    $k_page = k_page($k_post, $set['p_str']);
    $page = page($k_page);
    $start = $set['p_str'] * $page - $set['p_str'];
    $q = mysql_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 = mysql_fetch_assoc($q)) {
        $ank = get_user($post['id_user']);

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

        echo "<br />\n";

        if (isset($user) && ($user['level'] >= 3 || $user['id'] == $diary['id_user'])) {
            echo "<span style='float: right;' id='hides'><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> <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></span>";
        }

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

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

    if (isset($_GET['komm']) && intval($_GET['komm']) && isset($user)) {
        $posts = mysql_fetch_array(mysql_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 list-group-item-grey' 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 list-group-item-grey' 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';


