<?php

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

foreach (array('start', 'compress', 'sess', 'settings', 'db_connect', 'ipua', 'fnc', 'user') as $inc) {
    require_once "../../sys/inc/$inc.php";
}
$news = mysql_fetch_assoc(mysql_query("SELECT * FROM `news` WHERE `id` = '" . intval($_GET['news']) . "' LIMIT 1"));

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

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

#Список тех, кому понравилась новость
if (isset($_GET['like_all'])) {
    $k_post = mysql_result(mysql_query("SELECT COUNT(*) FROM `news_like` WHERE `id_news` = '" . intval($_GET['news']) . "'"), 0);
    $k_page = k_page($k_post, $set['p_str']);
    $page = page($k_page);
    $start = $set['p_str'] * $page - $set['p_str'];
    $k_sisa = $k_post - 1;
    $q = mysql_query("SELECT * FROM `news_like` WHERE `id_news` = '" . intval($_GET['news']) . "' ORDER BY `id` DESC LIMIT $start, $set[p_str]");

    #Навигация
    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_NEWS . "'>" . lang('Новости') . "</a> <i class='fa fa-angle-right fa-fw'></i> ";
    echo "<a href='" . DIR_NEWS . "?news=$news[id]'>" . cutStr($news['title'], 30) . "</a> <i class='fa fa-angle-right fa-fw'></i> ";
    echo lang('Им это нравится');
    echo "</div>";

    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'], '30', 'border-radius: 2px;');
        echo "</td><td class='null'>";
        user($ank['id']);
        echo "</td></tr></table>";
        echo "<div class='hr'></div>";
    }

    if ($k_page > 1) {
        echo $k_sisa;
    }

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

if (isset($_GET['like']) && isset($user)) {
    if (mysql_result(mysql_query("SELECT COUNT(*) FROM `news_like` WHERE `id_news` = '" . intval($_GET['news']) . "' AND `id_user` = '$user[id]' LIMIT 1"), 0) != 0) {
        
    } else {
        mysql_query("INSERT INTO `news_like` (`id_user`, `id_news`) values('$user[id]', '" . intval($_GET['news']) . "')");
    }
    #######################################################7.2.1
    $q = mysql_query("SELECT * FROM `friends` WHERE `user` = '$user[id]' AND `lenta_news` = '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"));

        $not_msg = "$SexOcen новость [url=" . DIR_NEWS . "?news=$news[id]&p=$pageEnd]" . htmlspecialchars($news['title']) . "[/url]";

        mysql_query("INSERT INTO `notifications` (`id_user`, `id_kont`, `id_img`, `msg`, `time`) values('$user[id]', '$a[id]', '1', '$not_msg', '$time')");
    }
    ############################################################
    header("Location: ?news=" . intval($_GET['news']) . "" . SID);
}
if (isset($_GET['likes']) && isset($user)) {
    if (mysql_result(mysql_query("SELECT COUNT(*) FROM `news_like` WHERE `id_news` = '" . intval($_GET['news']) . "' AND `id_user` = '$user[id]' LIMIT 1"), 0) != 0) {
        
    } else {
        mysql_query("INSERT INTO `news_like` (`id_user`, `id_news`) values('$user[id]', '" . intval($_GET['news']) . "')");
    }
    #########################################7.2.1
    $q = mysql_query("SELECT * FROM `friends` WHERE `user` = '$user[id]' AND `lenta_news` = '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"));

        $not_msg = "$SexOcen новость [url=" . DIR_NEWS . "?news=$news[id]&p=$pageEnd]" . htmlspecialchars($news['title']) . "[/url]";

        mysql_query("INSERT INTO `notifications` (`id_user`, `id_kont`, `id_img`, `msg`, `time`) values('$user[id]', '$a[id]', '1', '$not_msg', '$time')");
    }
    #####################################################
    header("Location: index.php?" . SID);
}

if (isset($_GET['unlike']) && isset($user)) {
    mysql_query("DELETE FROM `news_like` WHERE `id_user` = '$user[id]' AND `id_news` = '" . intval($_GET['news']) . "' LIMIT 1");
    header("Location: ?news=" . intval($_GET['news']) . "" . SID);
}
if (isset($_GET['unlikes']) && isset($user)) {
    mysql_query("DELETE FROM `news_like` WHERE `id_user` = '$user[id]' AND `id_news` = '" . intval($_GET['news']) . "' LIMIT 1");
    header("Location: index.php?" . SID);
}

if (isset($_POST['msg']) && isset($user) && $news['close'] == 0) {

    $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 `news_komm` WHERE `id_news` = '" . intval($_GET['news']) . "' AND `id_user` = '$user[id]' AND `msg` = '" . mysql_real_escape_string($msg) . "' LIMIT 1"), 0) != 0) {
        $err = lang('Ваше сообщение повторяет предыдущее');
    } elseif (!isset($err)) {
        mysql_query("INSERT INTO `news_komm` (`id_user`, `time`, `msg`, `id_news`) values('$user[id]', '$time', '" . mysql_real_escape_string($msg) . "', '" . intval($_GET['news']) . "')");
        #Добавляем рубли и активность
        mysql_query("UPDATE `user` SET `money` = '" . ($user['money'] + 3) . "', `activity` = '" . ($user['activity'] + 2) . "' WHERE `id` = '$user[id]' LIMIT 1");

        $q = mysql_query("SELECT * FROM `friends` WHERE `user` = '$user[id]' AND `lenta_news` = '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"));

            $not_msg = "$SexKomm новость [url=" . DIR_NEWS . "?news=$news[id]&p=$pageEnd]" . htmlspecialchars($news['title']) . "[/url]";

            mysql_query("INSERT INTO `notifications` (`id_user`, `id_kont`, `msg`, `time`) values('$user[id]', '$a[id]', '$not_msg', '$time')");
        }

        $_SESSION['message'] = lang('Сообщение успешно добавлено');

        header("Location: ?news=" . intval($_GET['news']) . "" . SID);
        exit;
    }
}
#Удаление комментария
if (isset($_GET['del']) && mysql_result(mysql_query("SELECT COUNT(*) FROM `news_komm` WHERE `id` = '" . intval($_GET['del']) . "'"), 0) && isset($user) && $user['level'] > 2) {
    mysql_query("DELETE FROM `news_komm` WHERE `id` = '" . intval($_GET['del']) . "'");
    $_SESSION['message'] = lang('Сообщение успешно удалено');
    header("Location: ?news=" . intval($_GET['news']) . "" . SID);
    exit;
}

err();

if (isset($user) && $user['id'] != $news['id']) {
    mysql_query("UPDATE `news` SET `prosm` = '" . ($news['prosm'] + 1) . "' WHERE `id` = '$news[id]' LIMIT 1");
}

$like = mysql_result(mysql_query("SELECT COUNT(*) FROM `news_like` WHERE `id_news` = '" . $news['id'] . "'"), 0);

#Навигация
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_NEWS . "'>" . lang('Новости') . "</a> <i class='fa fa-angle-right fa-fw'></i> ";
echo cutStr($news['title'], 30);
echo "</small></div><br />";

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

if (access('adm_news_edit') || access('adm_news_delete')) {
    echo "<span style='float:right;'>";
    echo "<div class='btn-group'>";
    echo "<a class='btn btn-info btn-sm' style='margin-right: -12px;' data-toggle='dropdown' aria-haspopup='true' aria-expanded='false'><i class='fa fa-chevron-down'></i></a>";
    echo "<div class='dropdown-menu dropdown-menu-right'>";
    if (access('adm_news_edit')) {
        echo "<a class='dropdown-item' href='?edit=$news[id]'><i class='fa fa-edit fa-fw'></i> " . lang('Редактировать') . "</a>";
    }
    if (access('adm_news_delete')) {
        echo "<a class='dropdown-item' href='?delete=$news[id]'><i class='fa fa-trash-o fa-fw'></i> " . lang('Удалить') . "</a>";
    }
    echo "</div>";
    echo "</div>";
    echo "</span>";
}

echo "<h4><a href='?news=$news[id]'>" . toOutput($news['title']) . "</a></h4>";

#Текст новости
echo toOutput($news['msg']) . "<br />";

#Изображение, если есть
if ($user['set_news'] == 1) {
    if (is_file(H . 'modules/news/images/' . $news['id'] . '.png')) {
        echo "<br /><img src='" . DIR_NEWS . "images/$news[id].png' style='border: 1px solid #CCDDED; padding: 2px; border-radius: 5px; max-width: 200px;' /><br />";
    }
}

echo "<small style='color: #9197a3;'>";
echo "<i class='fa fa-user fa-fw'></i> $news[avtor] &#183; <span title='" . date::time($news['time']) . "'>" . date::timek($news['time']) . "</span> ";
echo "</small>";

if (isset($user)) {
    echo "<hr>";

    if ($news['link'] != NULL) {
        echo "<a href='" . htmlentities($news['link'], ENT_QUOTES, 'UTF-8') . "' class='btn btn-info-outline btn-sm' data-toggle='tooltip' data-placement='right' title='" . lang('Перейти, чтобы узнать подробности') . "'><i class='fa fa-link fa-fw'></i> " . lang('Источник') . "</a> ";
    }
    if ($like == 0) {
        $l = null;
    } else {
        $l = mysql_result(mysql_query("SELECT COUNT(*) FROM `news_like` WHERE `id_news` = '$news[id]'"), 0);
    }

    if (mysql_result(mysql_query("SELECT COUNT(*) FROM `news_like` WHERE `id_news` = '$news[id]' AND `id_user` = '$user[id]' LIMIT 1"), 0) == 0) {
        echo "<a href='?news=$news[id]&amp;like' class='btn btn-info-outline btn-sm' title='" . lang('Мне нравится') . "' data-toggle='tooltip' data-placement='right'><i class='fa fa-heart-o fa-fw'></i> $l</a> ";
    } else {
        echo "<a href='?news=$news[id]&amp;unlike' class='btn btn-info-outline btn-sm' title='" . lang('Вам понравилась новость') . "' data-toggle='tooltip' data-placement='right'><i class='fa fa-heart fa-fw'></i> $l</a> ";
    }
    #Просмотры
    echo "<span title='" . lang('Просмотрели') . " " . sklon_text($news['prosm'], array(lang('раз'), lang('раза'), lang('раз'))) . "' class='btn btn-info-outline btn-sm' data-toggle='tooltip' data-placement='top'><i class='fa fa-eye fa-fw'></i> $news[prosm]</span>";

    if ($like > 0) {
        echo "<br /><font color='grey' size='2'>" . lang('Понравилось') . " " . sklon_text($like, array(lang('пользователю'), lang('пользователям'), lang('пользователям'))) . "</font><br />";

        if (IS_WEB) {
            $q_like = mysql_query("SELECT * FROM `news_like` WHERE `id_news` = '$news[id]' ORDER BY id DESC LIMIT 15");
        } else {
            $q_like = mysql_query("SELECT * FROM `news_like` WHERE `id_news` = '$news[id]' ORDER BY id DESC LIMIT 8");
        }
        while ($post_like = mysql_fetch_array($q_like)) {
            $ank = get_user($post_like['id_user']);
            echo "<a href='/info.php?id=$ank[id]'>";
            echo avatar($ank['id'], '30', 'border-radius: 2px;');
            echo "</a> ";
        }
    }
}

echo "</div>";

if ($news['close'] == 1) {
    echo "<div class='list-group-item-null list-group-item-warning'>";
    echo "<i class='fa fa-lock fa-fw'></i> " . lang('Новость закрыта для обсуждения');
    echo "</div>";
}

$k_post = mysql_result(mysql_query("SELECT COUNT(*) FROM `news_komm` WHERE `id_news` = '" . intval($_GET['news']) . "'"), 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 `news_komm` WHERE `id_news` = '" . intval($_GET['news']) . "' ORDER BY `id` $sort LIMIT $start, $set[p_str]");

if (isset($user)) {
    if (isset($user) && $news['close'] == 0) {
        echo "<form class='list-group-item-null' name='form' method='POST' action='?news=" . intval($_GET['news']) . "&amp;page=$page'>";
        echo auto_bb("form", "msg");
        echo "<textarea placeholder='" . lang('Напишите что-нибудь') . "...' name='msg' class='form-control'></textarea>";

        echo "<br />";
        $doc->Button('btn btn-primary btn-sm', 'ok', 'pencil', 'Опубликовать');

        echo "</form>";
    }
} else {
    echo "<div class='list-group-item-null list-group-item-grey-info'>";
    echo lang('Для того, чтобы комментировать - Вам необходимо авторизоваться или зарегистрироваться');
    echo "</div>";
}

if ($k_post == 0) {
    $doc->NoResult();
} else {
    if (isset($user)) {
        $li = "<li class='nav-item'><a class='nav-link' ";
        $lir = "<li class='nav-item'><a class='nav-link' ";

        echo "<div class='list-group-item-null'>";
        echo "<ul class='nav nav-tabs' style='margin-bottom: -11px;'>";

        if ($user['sort'] == 1) {
            $lir = "<li class='nav-item'><a class='nav-link active' ";
        } elseif ($user['sort'] == 0) {
            $li = "<li class='nav-item'><a class='nav-link active' ";
        } else {
            $li = "<li class='nav-item'><a class='nav-link' ";
        }

        echo "$lir href='?news=" . intval($_GET['news']) . "&amp;page=$page&amp;sort=1'>" . lang('Внизу') . "</a></li>";
        echo "$li href='?news=" . intval($_GET['news']) . "&amp;page=$page&amp;sort=0'>" . lang('Вверху') . "</a></li>";
        echo "</ul>";
        echo "</div>";
    }
}


while ($post = mysql_fetch_assoc($q)) {
    $ank = get_user($post['id_user']);

    echo "<table class='list-group-item-null'><tr><td class='icon14'>";
    echo avatar($ank['id'], '40', 'border-radius: 1px;');
    echo "</td><td class='null'>";
    if ($post['otvet'] != 0 && mysql_result(mysql_query("SELECT COUNT(*) FROM `user` WHERE `id`='$post[otvet]'"), 0) != 0) {
        $otvet = mysql_fetch_array(mysql_query("SELECT * FROM `user` WHERE `id` = '$post[otvet]'"));
    }

    echo user_one($ank['id'], 'user-name') . " <small data-toggle='tooltip' data-placement='right' title='" . date::time($post['time']) . "' style='color:grey;'>" . date::times($post['time']) . "</small><br /> ";

    echo "<div id='accordion' role='tablist' aria-multiselectable='true'>";
    echo "<div role='tab'>";
    echo "<span style='float: right;'><a class='collapsed' data-toggle='collapse' data-parent='#accordion' href='#post_$post[id]' aria-expanded='false'><i class='fa fa-ellipsis-v fa-fw'></i></a></span>";
    echo "</div>";
    if (isset($otvet)) {
        echo "$otvet[nick], ";
    }
    echo toOutput($post['msg']);

    if (isset($otvet)) {
        unset($otvet);
    }
    if (isset($user)) {
        echo "<div id='post_$post[id]' class='panel-collapse collapse' role='tabpanel' aria-labelledby='post_$post[id]'><small>";
        if (isset($user) && $news['close'] == 0 && $user['id'] != $ank['id']) {
            echo "<a href='?new_komm=$post[id]'>" . lang('Ответить') . "</a> ";
        }
        if (isset($user) && $user['level'] > 2 || $user['id'] == $post['id_user'] && $post['time'] > time() - 600) {
            echo "<a href='?edit_komm=$post[id]'>" . lang('Редактировать') . "</a> ";
        }
        if (access('adm_news_delete_comments')) {
            echo "<a href='?news=$news[id]&amp;del=$post[id]'>" . lang('Удалить') . "</a> ";
        }
        echo "</small></div>";
    }

    echo "</div>";
    echo "</td></tr></table><div class='hr'></div>";
}

if ($k_page > 1) {
    str("?news=" . intval($_GET['news']) . '&amp;', $k_page, $page);
}

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