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

$id = intval($_GET['id']);
$wall = mysql_fetch_array(mysql_query("SELECT * FROM `wall` WHERE `id` = '" . intval($_GET['id']) . "';"));
$ank = get_user($wall['id_user']);
$set['title'] = lang('Запись') . ' - ' . $ank['nick'] . '';

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

aut();
err();
$wall = mysql_fetch_array(mysql_query("SELECT * FROM `wall` WHERE `id` = '" . intval($_GET['id']) . "';"));

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

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

        while ($postlike = mysql_fetch_assoc($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']) {
                    mysql_query("INSERT INTO `notification` (`id_user`, `id_kont`, `msg`, `time`) VALUES ('$user[id]', '$userlike[id]', '$not', '$time')");
                }
            }
        }
    }
}

#Список тех, кому понравилась новость
if (isset($_GET['like_all'])) {
    $k_post = mysql_result(mysql_query("SELECT COUNT(*) FROM `wall_like` WHERE `id_komm` = '" . 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'];
    $k_sisa = $k_post - 1;
    $q = mysql_query("SELECT * FROM `wall_like` WHERE `id_komm` = '" . intval($_GET['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='/info.php?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 = mysql_fetch_assoc($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>";
            echo "<div class='hr'></div>";
        } 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>";
            echo "<div class='hr'></div>";
        }
    }

    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='/info.php?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 style = 'float:right' id = 'hide'>";
    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 = mysql_query("SELECT * FROM `wall_files` WHERE `id_wall` = '" . intval($_GET['id']) . "'");
while ($file = mysql_fetch_assoc($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($_GET['id']) && !is_numeric($_GET['id'])) {
    header("Location: index.php?" . SID);
    exit;
}
if (mysql_result(mysql_query("SELECT COUNT(*) FROM `wall` WHERE `id` = '" . intval($_GET['id']) . "' LIMIT 1", $db), 0) == 0) {
    header("Location: index.php?" . SID);
    exit;
}

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

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

        mysql_query("INSERT INTO `wall` (`user_id`, `who`, `time`) values('$user_id', '" . $user['id'] . "', '" . time() . "')");
        if (isset($user) && $user['id'] != $ank['id']) {
            $mess = "$SexOst " . lang('комментарий в Вашей') . " [url=/user/wall/comments.php?id=$wall[id]&page=end]" . lang('записи') . "[/url]. ";
            mysql_query("INSERT INTO `notification` (`id_user`, `id_kont`, `msg`, `time`) values('$user[id]', '$ank[id]', '$mess', '$time')");
        }
        $komm = mysql_query("SELECT * FROM `wall_comments` WHERE `id_wall` = '$wall[id]' ORDER BY `id`");
        while ($posts = mysql_fetch_assoc($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']) {
                    mysql_query("INSERT INTO `notification` (`id_user`, `id_kont`, `msg`, `time`) VALUES ('$user[id]', '$userkomm[id]', '$not', '$time')");
                }
            }
        }
    }
} elseif (isset($_GET['del']) && mysql_result(mysql_query("SELECT COUNT(*) FROM `wall_comments` WHERE `id` = '" . intval($_GET['del']) . "' && `id_wall` = '" . intval($_GET['id']) . "'"), 0)) {
    if (isset($user) && ($user['level'] >= 4) || isset($user) && ($user['id'] = $wall['id_user'])) {
        mysql_query("DELETE FROM `wall_comments` WHERE `id` = '" . intval($_GET['del']) . "' LIMIT 1");
        header("Location: comments.php?id=" . intval($_GET['id']) . "" . SID);
        exit;
    }
}


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

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

$like = mysql_result(mysql_query("SELECT COUNT(*) FROM `wall_like` WHERE `id_komm` = '" . intval($_GET['id']) . "'"), 0);

if ($like == 0) {
    $l = null;
} else {
    $l = mysql_result(mysql_query("SELECT COUNT(*) FROM `wall_like` WHERE `id_komm` = '" . intval($_GET['id']) . "'"), 0);
}

if (mysql_result(mysql_query("SELECT COUNT(*) FROM `wall_like` WHERE `id_komm` = '" . intval($_GET['id']) . "' AND `id_user` = '$user[id]' LIMIT 1"), 0) == 0) {
    echo "<a href='comments.php?id=" . intval($_GET['id']) . "&amp;like'><i class='fa fa-heart-o fa-fw'></i> " . lang('Нравится') . " $l</a>   &nbsp;  ";
} else {
    echo "<a href='comments.php?id=" . intval($_GET['id']) . "&amp;unlike'><i class='fa fa-heart fa-fw'></i> " . lang('Нравится') . " $l</a>   &nbsp;  ";
}

$kum = @mysql_result(mysql_query("SELECT id_user FROM `wall_like` WHERE `id_komm` = '" . intval($_GET['id']) . "'"), 0);
$kom = @mysql_result(mysql_query("SELECT nick FROM `user` WHERE `id` = '$kum' LIMIT 1"), 0);
$kem = $like - 1;

#Выводит тех, кому понравилась новость
if ($user['id'] == $kum) {
    if ($like == 1) {
        $vam = lang('Вам это нравится');
    } elseif ($like >= 2) {
        $vam = lang('Вам и ещё') . " <a href='?id=" . intval($_GET['id']) . "&amp;like_all'>" . sklon_text($kem, array(lang('пользователю'), lang('пользователям'), lang('пользователям'))) . "</a> " . lang('это нравится');
    }
} else {
    $vam = "<a href='?id=" . intval($_GET['id']) . "&amp;like_all'>" . sklon_text($like, array(lang('пользователю'), lang('пользователям'), lang('пользователям'))) . "</a> " . lang('это нравится');
}

if ($like == 1) {
    echo "<hr>";
    echo "<font color='#B2B2B2'><i class='fa fa-heart fa-fw'></i> $vam</font>";
} elseif ($like >= 2) {
    echo "<hr>";
    echo "<font color='#B2B2B2'><i class='fa fa-heart fa-fw'></i> $vam</font>";
}
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 = mysql_fetch_assoc($q)) {
    $ank = get_user($post['id_user']);

    echo "<table class='list-group-item-null'><tr><td class='icon14'>";
    avatar($ank['id'], '40', 'border-radius: 2px;');
    echo "</td><td class='null'>";
    echo user($ank['id']) . " <span class='hint--top' data-hint='" . date::time($post['time']) . "' style='color:grey;'><small>" . date::times($post['time']) . "</small></span><br />";

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

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

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

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



