<?php

/* DCMS Special
 * Дата последнего редактирования 18.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'])) {
    header("Location: index.php?" . SID);
    exit;
}

if (isset($_GET['id'])) {
    $id = intval($_GET['id']);
} else {
    header("Location: /index.php");
}
$k = $db->super_query("SELECT COUNT(*) as count FROM `wall` WHERE `id` = '" . $id . "' LIMIT 1");

if ($k['count'] == 0) {
    header("Location: index.php?" . SID);
    exit;
}

$wall = $db->get_array($db->query("SELECT * FROM `wall` WHERE `id` = '$id'"));
$ank = get_user($wall['id_user']);
$set['title'] = lang('Запись') . ' - ' . $ank['nick'] . '';

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

aut();
err();

if (isset($_GET['unlike']) && isset($user)) {
    $db->query("DELETE FROM `wall_like` WHERE `id_user` = '$user[id]' AND `id_komm` = '" . $id . "' LIMIT 1");
}

if (isset($_GET['like']) && isset($user)) {
    $l = $db->super_query("SELECT COUNT(*) as count FROM `wall_like` WHERE `id_komm` = '" . $id . "' AND `id_user` = '$user[id]' LIMIT 1");
    if ($l['count'] != 0) {
        
    } else {
        $db->query("INSERT INTO `wall_like` (`id_user`, `id_komm`) values('$user[id]', '" . $id . "')");
        if (isset($user) && $user['id'] != $ank['id']) {
            $mess = "$SexOcen " . lang('Вашу') . " [url=/user/wall/comments.php?id=$wall[id]]" . lang('запись') . "[/url].";
            $db->query("INSERT INTO `notification` (`id_user`, `id_kont`, `msg`, `time`) values('$user[id]', '$ank[id]', '$mess', '$time')");
        }
        $like = $db->query("SELECT * FROM `wall_like` WHERE `id_komm` = '$wall[id]' ORDER BY `id`");

        while ($postlike = $db->get_row($like)) {
            $userlike = get_user($postlike['id_user']);
            if ($user['id'] != $userlike['id']) {
                if ($user['id'] == $ank['id']) {
                    $not = "$SexOcen " . lang('Вашу') . " [url=/user/wall/comments.php?id=$wall[id]]" . lang('запись') . "[/url].";
                } elseif ($user['id'] != $ank['id'] && $userlike['id'] != $ank['id']) {
                    $not = "$SexOcen " . lang('Вашу') . " [url=/user/wall/comments.php?id=$wall[id]]" . lang('запись') . "[/url].";
                }
                if ($userlike['id'] != $user['id'] && $userlike['id'] != $ank['id']) {
                    $db->query("INSERT INTO `notification` (`id_user`, `id_kont`, `msg`, `time`) VALUES ('$user[id]', '$userlike[id]', '$not', '$time')");
                }
            }
        }
    }
}

#Список тех, кому понравилась новость
if (isset($_GET['like_all'])) {
    $k_post = $db->super_query("SELECT COUNT(*) as count FROM `wall_like` WHERE `id_komm` = '" . $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'];
    $k_sisa = $k_post - 1;
    $q = $db->query("SELECT * FROM `wall_like` WHERE `id_komm` = '" . $id . "' ORDER BY `id` DESC LIMIT $start, $set[p_str]");

    $stena = intval($_GET['id']);

    #Навигация
    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='/id$ank[id]'>$ank[nick]</a> <i class='fa fa-angle-right fa-fw'></i> ";
    echo "<a href='/info.php?id=$ank[id]&amp;act=wall'>" . lang('Стена') . "</a> <i class='fa fa-angle-right fa-fw'></i> ";
    echo "<a href='/user/wall/comments.php?id=$stena'>" . cutStr($wall['msg'], 30) . "</a> <i class='fa fa-angle-right fa-fw'></i> ";
    echo lang('Оценили');
    echo "</small></div><br />";

    if ($k_post == 0) {
        $doc->NoResult();
    }

    while ($post = $db->get_row($q)) {
        $ank = get_user($post['id_user']);


        if ($ank['id'] == 0) {
            echo "<table class='list-group-item-null'><tr><td class='icon14'>";
            avatar(0, '30', 'border-radius: 2px;');
            echo "</td><td class='null'>";
            echo "<i class='fa fa-user fa-fw'></i> " . lang('Пользователь удален');
            echo "</td></tr></table>";
        } else {
            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>";
        }
    }

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

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

$user_id = $ank['id'];

#Навигация
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='/id$ank[id]'>$ank[nick]</a> <i class='fa fa-angle-right fa-fw'></i> ";
echo "<a href='/info.php?id=$ank[id]&amp;act=wall'>" . lang('Стена') . "</a> <i class='fa fa-angle-right fa-fw'></i> ";
echo cutStr($wall['msg'], 30);
echo "</small></div><br />";

echo "<div class='list-group-item-null'>";
if (($user['level'] >= 4) || ($user['id'] == $wall['id_user'])) {
    echo "<span class='right'>";
    echo "<a href='edit.php?id=$wall[id]' data-toggle='tooltip' data-placement='left' title='" . lang('Редактировать') . "'><i class='fa fa-edit fa-fw'></i></a> ";
    echo "<a href='edit.php?id=$wall[id]&amp;del' data-toggle='tooltip' data-placement='left' title='" . lang('Удалить') . "'><i class='fa fa-trash-o fa-fw'></i></a>";
    echo "</span>";
}
echo user($ank['id']);
echo " &nbsp; <span title='" . date::times($wall['time']) . "' style='color:grey;'>" . date::timek($wall['time']) . "</span>";
echo "</div><div class='list-group-item-null list-group-item-grey'>";
echo toOutput($wall['msg']) . " <br />";

$q_f = $db->query("SELECT * FROM `wall_files` WHERE `id_wall` = '" . intval($_GET['id']) . "'");
while ($file = $db->get_row($q_f)) {
    if (preg_match('#jpg|jpeg|gif|png#i', $file['ras'])) {
        if (isset($user) && $user['level'] > 1) {
            echo "<a href='/files/delete/$file[id]/' title = '" . lang('Удалить файл') . "'><img src = '/files/$file[id]/$file[name].$file[ras]' max-width = '100' height = '100' style = 'margin: 1px;'/></a>";
        } else {
            echo "<img src='/files/$file[id]/$file[name].$file[ras]' max-width = '100' height = '100' style = 'margin: 1px;'/>";
        }
    }
}

echo "</div>";

if (isset($_POST['msg']) && isset($user)) {
    $msg = $_POST['msg'];

    if (utf8_strlen($msg) > 10024) {
        $err = lang('Слишком длинное сообщение');
    } elseif (utf8_strlen($msg) < 2) {
        $err = lang('Короткое сообщение');
    }

    $q = $db->super_query("SELECT COUNT(*) as count FROM `wall_comments` WHERE `id_wall` = '$wall[id]' AND `id_user` = '$user[id]' AND `msg` = '" . $db->safesql($msg) . "' LIMIT 1");
    if ($q['count'] != 0) {
        $err = lang('Ваше сообщение повторяет предыдущее');
    }
    if (!isset($err)) {
        $db->query("INSERT INTO `wall_comments` (`id_user`, `time`, `msg`, `id_wall`) values('$user[id]', '$time', '" . $db->safesql($msg) . "', '$wall[id]')");
        $db->query("UPDATE `user` SET `money` = '" . ($user['money'] + 5) . "', `activity` = '" . ($user['activity'] + 2) . "' WHERE `id` = '$user[id]' LIMIT 1");

        $c = $db->super_query("SELECT COUNT(*) as count FROM `wall_count` WHERE `id_wall` = '$wall[id]' AND `id_user` = '$user[id]' LIMIT 1");

        if ($c['count'] == 0) {
            $db->query("INSERT INTO `wall_count` (`id_user`, `id_wall`) values('$user[id]', '$wall[id]')");
        }

        if (isset($user) && $user['id'] != $ank['id']) {
            $mess = "$SexOst " . lang('комментарий в Вашей') . " [url=/user/wall/comments.php?id=$wall[id]&page=end]" . lang('записи') . "[/url]. ";
            $db->query("INSERT INTO `notification` (`id_user`, `id_kont`, `msg`, `time`) values('$user[id]', '$ank[id]', '$mess', '$time')");
        }
        $komm = $db->query("SELECT * FROM `wall_comments` WHERE `id_wall` = '$wall[id]' ORDER BY `id`");
        while ($posts = $db->get_row($komm)) {
            $userkomm = get_user($posts['id_user']);
            if ($user['id'] != $userkomm['id']) {
                if ($user['id'] == $ank['id']) {
                    $not = "$SexKomm " . lang('Вашу') . " [url=/user/wall/comments.php?id=$wall[id]]" . lang('запись') . "[/url]. ";
                } elseif ($user['id'] != $ank['id'] && $userkomm['id'] != $ank['id']) {
                    $not = "$SexKomm " . lang('Вашу') . " [url=/user/wall/comments.php?id=$wall[id]]" . lang('запись') . "[/url]. ";
                }

                if ($userkomm['id'] != $user['id'] && $userkomm['id'] != $ank['id']) {
                    $db->query("INSERT INTO `notification` (`id_user`, `id_kont`, `msg`, `time`) VALUES ('$user[id]', '$userkomm[id]', '$not', '$time')");
                }
            }
        }
    }
}

if (isset($_GET['del'])) {
    $del = $db->super_query("SELECT COUNT(*) as count FROM `wall_comments` WHERE `id` = '" . intval($_GET['del']) . "' AND `id_wall` = '" . $id . "'");
    if ($del['count'] && isset($user) && ($user['level'] >= 4) || $del['count'] && isset($user) && ($user['id'] == $wall['id_user'])) {
        $db->query("DELETE FROM `wall_comments` WHERE `id` = '" . intval($_GET['del']) . "' LIMIT 1");
        $_SESSION['message'] = lang('Сообщение успешно удалено');
        header("Location: comments.php?id=" . $wall['id'] . "" . SID);
        exit;
    } else {
        $msg = "Совершил попытку удаления записи на стене [url=/user/wall/comments.php?id=" . $wall['id'] . "]здесь[/url]";
        $db->query("INSERT INTO `events` (`id_user`, `id_kont`, `msg`, `time`) values('$user[id]', '1', '" . $msg . "', '$time')");

        $_SESSION['message'] = lang('Администрация уведомлена');
        header("Location: comments.php?id=" . $wall['id'] . "" . SID);
        exit;
    }
}

$k_post = $db->super_query("SELECT COUNT(*) as count FROM `wall_comments` WHERE `id_wall` = '" . $wall['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'];
$q = $db->query("SELECT * FROM `wall_comments` WHERE `id_wall` = '" . $wall['id'] . "' ORDER BY `time` ASC LIMIT $start, $set[p_str]");

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

$like = $db->super_query("SELECT COUNT(id) as count FROM `wall_like` WHERE `id_komm` = '" . $wall['id'] . "'");
$like = (($like['count'] == 0) ? null : "$like[count]");

$like_u = $db->super_query("SELECT COUNT(id) as count FROM `wall_like` WHERE `id_komm` = '" . $wall['id'] . "' AND `id_user` = '$user[id]' LIMIT 1");

#Мне нравится
if ($like_u['count'] == 0) {
    echo "<a href='comments.php?id=" . $id . "&amp;like' class='hint--top-right btn btn-secondary btn-sm' data-hint='" . lang('Мне нравится') . "'><i class='fa fa-heart-o fa-fw grey'></i> $like</a> ";
} else {
    echo "<a href='comments.php?id=" . $id . "&amp;unlike' class='hint--top-right btn btn-secondary btn-sm' data-hint='" . lang('Вам понравилась запись') . "'><i class='fa fa-heart fa-fw grey'></i> $like</a> ";
}

if ($like > 0) {

    if (IS_WEB) {
        $q_like = $db->query("SELECT * FROM `wall_like` WHERE `id_komm` = '$wall[id]' ORDER BY id DESC LIMIT 10");
    } else {
        $q_like = $db->query("SELECT * FROM `wall_like` WHERE `id_komm` = '$wall[id]' ORDER BY id DESC LIMIT 5");
    }
    while ($post_like = $db->get_row($q_like)) {
        $ank = get_user($post_like['id_user']);
        echo "<a href='/id$ank[id]' class='hint--top' data-hint='$ank[nick]'>";
        echo avatar($ank['id'], '25', 'border-radius: 2px;');
        echo "</a> ";
    }
}

echo "</div>";

if ($wall['privat_komm'] == 1) {
    echo "<div class='list-group-item-null list-group-item-warning'>";
    echo lang('Автор') . " $SexZapr " . lang('комментировать');
    echo "</div>";
} else {
    if (isset($user)) {
        echo "<form method='post' name='message' class='list-group-item-null list-group-item-grey' action='?id=" . intval($_GET['id']) . "&amp;page=$page'>";
        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>";
    }
}

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'], '40', 'border-radius: 2px;');
    echo "</td><td class='null'>";
    echo user($ank['id']) . " <small class='hint--left right grey' data-hint='" . date::time($post['time']) . "'>" . date::timek($post['time']) . "</small><br />";

    echo toOutput($post['msg']);

    if (isset($user) && ($user['level'] >= 4) || isset($user) && ($user['id'] == $wall['id_user'])) {
        echo "<span class='right grey'>";
        echo "<a href='?id=" . intval($_GET['id']) . "&amp;del=$post[id]' class='hint--left' data-hint='" . lang('Удалить комментарий') . "'><i class='fa fa-trash-o fa-fw'></i></a>";
        echo "</span>";
    }

    echo "</td></tr></table>";
}

if ($k_page > 1) {
    str("?id=" . $id . '&amp;', $k_page, $page);
}

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



