<?php

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

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

only_reg();

$set['title'] = lang('Друзья');

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

if (isset($_GET['id'])) {
    $sid = intval($_GET['id']);
} else {
    $sid = $user['id'];
}

$ank = get_user($sid);

aut();

if (isset($_POST['password'])) {
    $set_cook = $_POST['password'];
    setcookie("passfriend$ank[id]", $set_cook);
    if (isset($_POST['password']) && $_POST['password'] == $ank['friends_password']) {
        header("Location: ?id=$ank[id]");
    }
}
if ($ank['friends_access'] == 'only_me') {
    if ($ank['id'] == $user['id'] && isset($user) || $user['level'] >= 3) {
        
    } else {
        echo "<div class='list-group-item-null list-group-item-info'>";
        echo lang('Доступ к списку друзей ограничен');
        echo "</div>";

        $doc->Link('list-group-item-null', "/id$ank[id]", 'arrow-left', "Вернуться к $ank[nick]");

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

        exit();
    }
} elseif ($ank['friends_access'] == 'friends') {
    $fr = $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['level'] >= 3 || $ank['id'] == $user['id'] && isset($user) || isset($umodd) || $fr['count'] != 0) {
        
    } else {
        echo "<div class='list-group-item-null list-group-item-info'>";
        echo lang('Доступ к списку друзей ограничен');
        echo "</div>";

        $doc->Link('list-group-item-null', "/id$ank[id]", 'arrow-left', "Вернуться к $ank[nick]");

        require_once H . 'sys/inc/tfoot.php';
        exit();
    }
} elseif ($ank['friends_access'] == 'pass') {
    if (isset($_COOKIE["passfriend$ank[id]"]) && $_COOKIE["passfriend$ank[id]"] == $ank['friends_password'] || $ank['id'] == $user['id'] && isset($user) || $user['level'] >= 3) {
        
    } else {
        if (isset($_POST['password']) && $_POST['password'] != $ank['friends_password']) {
            $err[] = lang("Неверный пароль");
        }
        err();

        echo "<form class = 'list-group-item-null' action='' method='post'>";
        echo lang('Список друзей пользователя') . " <b>$ank[nick]</b> " . lang('доступен только по паролю') . "<br/>";

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

        echo "</form>";

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

#Навигация
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 lang('Друзья');
echo "</small></div><br />";

$k = $db->super_query("SELECT COUNT(*) as count FROM `friends` INNER JOIN `user` ON `friends`.`friends`=`user`.`id` WHERE `friends`.`user` = '$ank[id]' AND `friends`.`i` = '1' AND `user`.`date_last`>'" . (time() - 300) . "'");
$k_online = (($k['count'] == 0) ? null : "$k[count]");
$k = $db->super_query("SELECT COUNT(*) as count FROM `friends` WHERE `user` = '$ank[id]' AND `i` = '1'");
$k_post = (($k['count'] == 0) ? null : "$k[count]");
$k = $db->super_query("SELECT COUNT(id) as count FROM `friends_new` WHERE `to` = '$ank[id]'");
$k_new = (($k['count'] == 0) ? null : "<b>$k[count]</b>");

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

echo "<ul class='nav nav-tabs' style='margin-bottom: -11px;'>";
echo "<li class='nav-item'><a class='nav-link active'>" . lang('Друзья') . "</a></li>";

if (isset($user) && $user['id'] == $ank['id']) {
    echo "<li class='nav-item'><a class='nav-link' href='new.php'>" . lang('Заявки') . " $k_new</a></li>";
    echo "<li class='nav-item'><a class='nav-link' href='settings.php'>" . lang('Настройки') . "</a></li>";
}
echo "</ul>";
echo "</div>";

echo "<div class='list-group-item-null'>";
echo "<ul class='nav nav-pills'>";
echo "<li class='nav-item'><a class='nav-link active'>" . lang('Все друзья') . " $k_post</a></li>";
echo "<li class='nav-item'><a class='nav-link' href='online.php?id=$ank[id]'>" . lang('Онлайн') . " $k_online</a></li>";
echo "</ul></div>";

if ($k_post == 0) {
    echo "<br />";
    $doc->NoResult();
}

$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 `friends` WHERE `user` = '$ank[id]' AND `i` = '1' ORDER BY time DESC LIMIT $start, $set[p_str]");

while ($f = $db->get_array($q)) {
    $friends = $db->get_array($db->query("SELECT * FROM `user` WHERE `id` = '$f[friends]' LIMIT 1"));
    echo "<table class='list-group-item-null-mini list-group-item-grey dop'><tr><td class='icon14'>";
    avatar($friends['id'], '48', 'border-radius: 2px;');
    echo "</td><td class='null'>";
    if ($ank['id'] == $user['id']) {
        echo "<a href='new.php?del=$friends[id]' class='hint--left right grey' data-hint='" . lang('Удалить') . "'><i class='fa fa-trash-o fa-fw'></i></a>";
    }
    user($friends['id']);
    echo "<br />";

    if (isset($user) && $user['id'] == $friends['id']) {
        echo "<small class='grey'><i class='fa fa-envelope fa-fw'></i> " . lang('Сообщение') . "</small>";
    } else {
        echo "<small><a href='/user/mail/messageList.php?contact=$friends[id]'><i class='fa fa-envelope fa-fw'></i> " . lang('Сообщение') . "</a></small>";
    }

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

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

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