<?php

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

if (isset($_GET['id']) && $_GET['id'] != NULL) {
    $ank = get_user(intval($_GET['id']));
} elseif (isset($user)) {
    $ank = get_user($user['id']);
}

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

    aut();

    echo "<div class='list-group-item-null list-group-item-warning'>";
    echo "<i class='fa fa-user fa-fw'></i> " . lang('Пользователь не найден');
    echo "</div>";

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

if (isset($_GET['set']) && $ank['id'] == $user['id'] && isset($user)) {
    if (isset($_POST['save']) && $_POST['mdp'] == md5($user['pass'])) {
        if (in_array($_POST['access'], array('all', 'only_me', 'friends', 'pass', 'auth'))) {
            $access = $_POST['access'];
        } else {
            $access = 'all';
        }
        $pass = NULL;

        if ($access == 'pass') {
            if (utf8_strlen($_POST['password']) < 1) {
                $err[] = lang('Введите пароль');
            }
            if (utf8_strlen($_POST['password']) > 20) {
                $err[] = lang('Пароль слишком длинный');
            }
            $pass = $_POST['password'];
        }

        if (in_array($_POST['komm'], array('all', 'only_me', 'friends'))) {
            $komm = $_POST['komm'];
        } else {
            $komm = 'all';
        }
        if (!isset($err)) {
            $db->query("UPDATE `user` SET `wall_access` = '$access', `wall_password` = '" . $db->safesql($pass) . "' WHERE `id` = '$ank[id]'");
            $db->query("UPDATE `user` SET `wall_komm` = '$komm' WHERE `id` = '$ank[id]'");

            $ank['wall_password'] = $pass;
            $ank['wall_access'] = $access;
            $ank['wall_komm'] = $komm;

            $_SESSION['message'] = lang('Изменения сохранены');
            header("Location: ?id=$ank[id]&act=wall");
            exit;
        }
    }

    err();

    echo "<form method='post' class='list-group-item-null' action=''>";
    echo "<b>" . lang('Кто видет записи стены') . "?</b><br />";

    $doc->Radio('access', 'Все', 'all', "" . ($ank['wall_access'] == 'all' ? " checked='checked'" : null) . "", 'globe');
    $doc->Radio('access', 'Только я', 'only_me', "" . ($ank['wall_access'] == 'only_me' ? " checked='checked'" : null) . "", 'lock');
    $doc->Radio('access', 'Только авторизованные', 'auth', "" . ($ank['wall_access'] == 'auth' ? " checked='checked'" : null) . "", 'user');
    $doc->Radio('access', 'Только друзья', 'friends', "" . ($ank['wall_access'] == 'friends' ? " checked='checked'" : null) . "", 'users');
    $doc->Radio('access', 'Только по паролю', 'pass', "" . ($ank['wall_access'] == 'pass' ? " checked='checked'" : null) . "", 'key');

    $doc->Input('password', 'Пароль', 16, "" . htmlspecialchars($user['wall_password']) . "");

    echo "<br /><b>" . lang('Кто может оставлять записи') . "?</b><br />";

    $doc->Radio('komm', 'Все', 'all', "" . ($ank['wall_komm'] == 'all' ? " checked='checked'" : null) . "", 'globe');
    $doc->Radio('komm', 'Только я', 'only_me', "" . ($ank['wall_komm'] == 'only_me' ? " checked='checked'" : null) . "", 'lock');
    $doc->Radio('komm', 'Только друзья', 'friends', "" . ($ank['wall_komm'] == 'friends' ? " checked='checked'" : null) . "", 'users');

    echo "<br />";
    $doc->Button('btn btn-success btn-sm', 'save', 'save', 'Сохранить');

    echo "<input type='hidden' name='mdp' value='" . md5($user['pass']) . "'/></form>";

    $doc->Link('list-group-item-null', "?id=$ank[id]&amp;act=wall", 'arrow-left', 'Назад');

    require_once H . 'sys/inc/tfoot.php';
    exit;
}
if (isset($_POST['password'])) {
    $set_cook = $_POST['password'];
    setcookie("passgb[$ank[id]]", $set_cook);
    if (isset($_POST['password']) && $_POST['password'] == $ank['wall_password']) {
        header("Location:?id=$ank[id]&act=wall&enter=ok");
    }
}
if ($ank['wall_access'] == 'only_me') {
    if ($ank['id'] == $user['id'] && isset($user) || $user['group_access'] >= 7) {
        
    } else {
        echo "<div class='list-group-item-null list-group-item-info'><i class='fa fa-lock fa-fw'></i> " . lang('Доступ к записям ограничен') . "!</div>";

        require_once H . 'sys/inc/tfoot.php';
        exit;
    }
} elseif ($ank['wall_access'] == 'friends') {
    $friend = $db->super_query("SELECT COUNT(*) as count FROM `friends` WHERE (`user` = '$user[id]' AND `friends` = '$ank[id]') OR (`user` = '$ank[id]' AND `friends` = '$user[id]')");

    if ($ank['id'] == $user['id'] && isset($user) || $user['group_access'] >= 7 || $friend['count'] != 0) {
        
    } else {
        echo "<div class='list-group-item-null list-group-item-info'>" . lang('Доступ к записям на стене открыт только друзьям автора') . "!</div>";

        require_once H . 'sys/inc/tfoot.php';
        exit;
    }
} elseif ($ank['wall_access'] == 'pass') {
    if (isset($_COOKIE['passgb'][$ank['id']]) && $_COOKIE['passgb'][$ank['id']] == $ank['wall_password'] || $ank['id'] == $user['id'] && isset($user) || $user['group_access'] >= 7) {
        
    } else {
        echo "<div class='list-group-item-null list-group-item-info'>";
        if (isset($_POST['password']) && $_POST['password'] != $ank['wall_password']) {
            $_SESSION['err'] = lang('Неверный пароль');
        }
        echo lang('Доступ открыт только по паролю') . "!<br /> " . lang('Чтобы просматривать записи стены, введите пароль') . ":";

        echo "<form action='' method=\"post\">";

        $doc->Input('password', 'Пароль', 16);
        echo "<br />";
        $doc->Button('btn btn-success btn-sm', 'go', null, 'Войти');

        echo "</form>";
        echo "</div>";

        require_once H . 'sys/inc/tfoot.php';
        exit;
    }
} elseif ($ank['wall_access'] == 'auth') {
    if (isset($user)) {
        
    } else {
        echo "<div class='list-group-item-null list-group-item-info'>";
        echo lang("Доступ к стене открыт только авторизированным пользователям");
        echo "</div>";

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

if (isset($_GET['clean']) && isset($user) && ($user['group_access'] >= 7 || $user['id'] == $ank['id'])) {
    if (isset($_GET['all'])) {
        if (isset($_GET['ok'])) {
            $db->query("DELETE FROM `wall` WHERE `id_wall` = '$ank[id]'");

            $_SESSION['message'] = lang('Стена успешно очищена');
        } else {
            echo "<div class='list-group-item-null'>" . lang('Очистить стену от записей') . "?<br />";

            $doc->Link('btn btn-success btn-sm', "?id=$ank[id]&amp;act=wall&amp;clean&amp;all&amp;ok", null, 'Да');
            $doc->Link('btn btn-secondary btn-sm', "?id=$ank[id]&amp;act=wall", null, 'Нет');

            echo "</div>";

            require_once H . 'sys/inc/tfoot.php';
            exit;
        }
    } else {
        if (isset($_GET['ok'])) {
            $ch = intval($_POST['ch']);
            $mn = intval($_POST['mn']);
            $nt = $ch * $mn * 3600;
            $nt = $time - $nt;
            $db->query("DELETE FROM `wall` WHERE `time` < '$nt'");

            $_SESSION['message'] = lang('Записи успешно очищены');
        } else {
            $doc->Link('list-group-item-null', "?id=$ank[id]&amp;act=wall&amp;clean&amp;all", 'trash', 'Очистить стену полностью?');

            echo "<form method='post' class='list-group-item-null' action='?id=$ank[id]&amp;act=wall&amp;clean&amp;ok'>";
            echo lang('Будут удалены записи, написанные ... тому назад') . ":<br />";

            echo lang('Количество') . "<br />";
            echo "<input name='ch' type='text' value='1' class='form-control'><br />";

            echo "<select class='form-control' name='mn'>";
            echo "<option value='1' selected='selected'>" . lang('Часов') . "</option>";
            echo "<option value='24'>" . lang('Дней') . "</option>";
            echo "<option value='168'>" . lang('Недель') . "</option>";
            echo "<option value='744'>" . lang('Месяцев') . "</option>";
            echo "</select><br />";

            $doc->Button('btn btn-success btn-sm', null, 'trash-o', 'Очистить');
            echo "</form>";

            $doc->Link('list-group-item-null list-group-item-grey', "?id=$ank[id]&amp;act=wall", 'arrow-left', 'Назад');

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

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

$kf = $db->super_query("SELECT COUNT(*) as count FROM `friends` WHERE (`user` = '$user[id]' AND `friends` = '$ank[id]') OR (`user` = '$ank[id]' AND `friends` = '$user[id]')");

if (isset($_POST['msg']) && isset($user) && ($ank['wall_komm'] == 'all' || $ank['wall_komm'] == 'only_me' && ($user['id'] == $ank['id'] || $user['group_access'] >= 7) || $ank['wall_komm'] == 'friends' && ($ank['id'] == $user['id'] || $user['group_access'] >= 7 || $kf['count'] != 0))) {
    $msg = $_POST['msg'];

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

    if (isset($_POST['privat_komm'])) {
        if ($_POST['privat_komm'] == 1) {
            $privat_komm = '1';
        } else {
            $privat_komm = '0';
        }
    } else {
        $privat_komm = '0';
    }
    if (!isset($err)) {
        if ($ank['id'] != $user['id']) {
            $m = "$SexOpub " . lang('запись на Вашей') . " [url=/info.php?id=" . $ank['id'] . "&act=wall]" . lang('стене') . "[/url]";
            $db->query("INSERT INTO `notification` (`id_user`, `id_kont`, `id_img`, `msg`, `time`) values('" . $user['id'] . "', '$ank[id]', '4', '$m', '$time')");
        }
        $msg = $db->safesql($msg);

        $db->query("INSERT INTO `wall` (id_user,id_wall, time, msg, privat_komm) values('$user[id]', '$ank[id]', '$time', '$msg', '$privat_komm')");
        $db->query("UPDATE `user` SET `money` = '" . ($user['money'] + 3) . "', `activity` = '" . ($user['activity'] + 3) . "' WHERE `id` = '$user[id]' LIMIT 1");

        $q3 = NULL;
        $qq = $db->query("SELECT * FROM `wall` WHERE `id_wall` = '$ank[id]'");

        while ($ppost = $db->get_array($qq)) {
            $a = get_user($ppost['id_user']);
            if ($a) {
                $array = explode(";", $q3);

                foreach ($array as $key => $value) {
                    if ($value == $a['id']) {
                        $g = 1;
                    }
                }

                if (!isset($g)) {
                    $q3 = "" . ($q3 != NULL ? "$q3;" : null) . "$a[id]";
                }

                if (isset($g)) {
                    unset($g);
                }
            }
        }
        $array = explode(";", $q3);
        foreach ($array as $key => $value) {
            $a = get_user($value);

            if ($value != NULL && $a) {
                if ($a['id'] != $ank['id'] && $a['id'] != $user['id']) {
                    $not = "$SexOst " . lang('запись на') . " [url=/info.php?id=" . $ank['id'] . "&act=wall]" . lang('стене') . "[/url]";
                    $db->query("INSERT INTO `notifications` (`id_user`, `id_kont`, `msg`, `time`) values('" . $user['id'] . "', '$a[id]', '$not', '$time')");
                }
            }
        }

        $_SESSION['message'] = lang('Запись опубликована');
    }
    header("Location:?id=$ank[id]&act=wall");
} elseif (isset($_GET['hide']) && isset($user)) {
    $w = $db->super_query("SELECT COUNT(*) as count FROM `wall` WHERE `id` = '" . intval($_GET['hide']) . "' AND `hide_user` = '0' AND `id_wall` = '$ank[id]' LIMIT 1");
    if ($w['count'] != 0) {
        $komm = $db->get_array($db->query("SELECT * FROM `wall` WHERE `id` = '" . intval($_GET['hide']) . "' AND `id_wall` = '$ank[id]' LIMIT 1"));
    }
    $ank2 = get_user($komm['id_user']);

    if ($user['group_access'] >= 7) {
        $db->query("UPDATE `wall` SET `hide_user` = '1', `hide_user` = '$user[id]' WHERE `id` = '" . intval($_GET['hide']) . "' AND `id_wall` = '$ank[id]' LIMIT 1");
    }
} elseif (isset($_GET['show']) && isset($user)) {
    $w = $db->super_query("SELECT COUNT(*) as count FROM `wall` WHERE `id` = '" . intval($_GET['show']) . "' AND `hide_user` = '1' AND `id_wall` = '$ank[id]' LIMIT 1");
    if ($w['count'] != 0) {
        $komm = $db->get_array($db->query("SELECT * FROM `wall` WHERE `id` = '" . intval($_GET['show']) . "' AND `id_wall` = '$ank[id]' LIMIT 1"));
    }
    $ank2 = get_user($komm['id_user']);

    if ($user['group_access'] >= 7) {
        $db->query("UPDATE `wall` SET `hide_user` = '0' WHERE `id` = '" . intval($_GET['show']) . "' AND `id_wall` = '$ank[id]' LIMIT 1");
    }
}
err();

$friend_w = $db->super_query("SELECT COUNT(*) as count FROM `friends` WHERE (`user` = '$user[id]' AND `friends` = '$ank[id]') OR (`user` = '$ank[id]' AND `friends` = '$user[id]')");

if ($ank['wall_komm'] == 'all' || $ank['wall_komm'] == 'only_me' && ($user['id'] == $ank['id'] || $user['group_access'] >= 7) || $ank['wall_komm'] == 'friends' && ($ank['id'] == $user['id'] || $user['group_access'] >= 7 || $friend_w['count'] != 0)) {

    echo "<form method='post' class='list-group-item-null-mini list-group-item-grey dop' action='?id=$ank[id]&amp;act=wall' name='message'>";
    if (isset($user) && $user['id'] != $ank['id']) {
        echo auto_bb("message", "msg");
        echo "<textarea placeholder='" . lang('Напишите что-нибудь') . "...' name='msg' class='form-control'></textarea><br />";
    } else {
        echo auto_bb("message", "msg");
        echo "<textarea placeholder='" . lang('О чем Вы думаете?') . "' name='msg' class='form-control'></textarea><br />";
    }

    $doc->Checkbox('privat_komm', 'Запретить комментировать', 1);
    echo "<br />";

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

    echo "</form>";
}

$k_post = $db->super_query("SELECT COUNT(*) as count FROM `wall` WHERE `id_wall` = '$ank[id]'" . ($user['group_access'] < 7 ? " AND `hide_user` = '0'" : null) . "");
$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'];

if ($k_post == 0) {
    $doc->NoResult();
}
$q = $db->query("SELECT * FROM `wall` WHERE `id_wall` = '$ank[id]'" . ($user['group_access'] < 7 ? " AND `hide_user` = '0'" : null) . " ORDER BY id DESC LIMIT $start, $set[p_str]");
while ($post = $db->get_array($q)) {
    $ank2 = get_user($post['id_user']);
    $wall_like = $db->super_query("SELECT COUNT(*) as count FROM `wall_like` WHERE `id_komm` = '$post[id]'");
    $wall_like = (($wall_like['count'] == 0) ? null : "$wall_like[count]");
    $cmn = $db->super_query("SELECT COUNT(*) as count FROM `wall_comments` WHERE `id_wall` = '$post[id]'");
    $cmn = (($cmn['count'] == 0) ? null : "$cmn[count]");

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

    echo "<br /><br />";
    echo "<a href='/user/wall/comments.php?id=$post[id]' class='btn btn-secondary btn-sm'><i class='fa fa-comments fa-fw grey'></i> $cmn</a> ";

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

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

    if ($wall_like > 0) {

        if (IS_WEB) {
            $q_like = $db->query("SELECT * FROM `wall_like` WHERE `id_komm` = '$post[id]' ORDER BY id DESC LIMIT 10");
        } else {
            $q_like = $db->query("SELECT * FROM `wall_like` WHERE `id_komm` = '$post[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> ";
        }
    }

    ########

    include 'mini_form.php';

    echo "</td></tr></table>";
}
if ($k_page > 1) {
    str("/info.php?id=" . $ank['id'] . "&act=wall&amp;", $k_page, $page);
}

if ($user['group_access'] >= 7) {
    $doc->Link('list-group-item-null list-group-item-info dop', "?id=$ank[id]&amp;act=wall&amp;clean", 'trash-o', 'Очистить');
}
if ($ank['id'] == $user['id'] && isset($user)) {
    $doc->Link('list-group-item-null list-group-item-info dop', "?id=$ank[id]&amp;act=wall&amp;set", 'cog', 'Настройки');
}



