<?php

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

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

if (isset($user)) {
    $ank['id'] = $user['id'];
}
if (isset($_GET['id'])) {
    $ank['id'] = intval($_GET['id']);
} elseif (isset($_GET['u'])) {
    $array['user_url'] = $_GET['u'];
    $user_url = $db->safesql($array['user_url']);
    $result = $db->query("SELECT `id` FROM `user` WHERE `user_url`='" . $user_url . "' LIMIT 1");

    if ($db->num_rows($result) > '0') {
        $arr_url = $db->get_row($result);
        $ank['id'] = $arr_url['id'];
    } else {
        header("Location: /" . SID);

        include_once 'sys/inc/tfoot.php';
        exit;
    }
}

only_reg('index.php');

if ($ank['id'] == 0) {
    $ank = get_user($ank['id']);
    $set['title'] = $ank['nick'] . ' - Профиль ';

    require_once 'sys/inc/thead.php';

    aut();

    echo "<div class='list-group-item-null'>";
    echo $ank['group_name'] . "<br />\n";

    if ($ank['ank_about_me'] != NULL) {
        echo "О себе: " . $ank['ank_about_me'] . "<br />";
    }
    echo "</div>";

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

$ank = get_user($ank['id']);
if (!$ank) {
    header("Location: /index.php?" . SID);
    exit;
}

$res_a = $db->super_query("SELECT SUM(`rating`) as rating FROM `user_voice2` WHERE `id_kont` = '$ank[id]'");
$ank['rating'] = $res_a['rating'];
$set['title'] = $ank['nick'] . ' - анкета '; // заголовок страницы

require_once 'sys/inc/thead.php';

if (isset($_GET['fav']) && isset($user)) {
    $b = $db->super_query("SELECT COUNT(*) as count FROM `bookmark_user` WHERE `id_user` = '" . $user['id'] . "' AND `id_people` = '" . $ank['id'] . "' LIMIT 1");

    if ($b['count'] == 0 && $_GET['fav'] == 1) {
        $db->query("INSERT INTO `bookmark_user` (`id_people`, `id_user`, `time`) VALUES ('" . $ank['id'] . "', '" . $user['id'] . "', '$time')");
    }
    $b = $db->super_query("SELECT COUNT(*) as count FROM `bookmark_user` WHERE `id_user` = '" . $user['id'] . "' AND `id_people` = '" . $ank['id'] . "' LIMIT 1");

    if ($b['count'] == 1 && $_GET['fav'] == 0) {
        $db->query("DELETE FROM `bookmark_user` WHERE `id_user` = '" . $user['id'] . "' AND  `id_people` = '" . $ank['id'] . "'");
    }

    header("Location: /id" . $ank['id'] . "");
    exit;
}

aut();

#Добавляем рейтинг
$res = $db->super_query("SELECT SUM(`rating`) as rating FROM `user_voice2` WHERE `id_kont` = '$user[id]'");
$res_u = $res['rating'];
if (isset($_GET['rating']) && isset($user) && $user['id'] != $ank['id'] && $user['money'] >= 10 && $res_u >= 0) {
    $my_rr = $db->super_query("SELECT `rating` as rating FROM `user_voice2` WHERE `id_user` = '$user[id]' AND `id_kont` = '$ank[id]'");
    $my_r = intval($my_rr['rating']);
    if ($my_r > '0') {
        $_SESSION['message'] = lang("Вы уже голосовали");
        header("Location: /id" . $ank['id'] . "");
        exit;
    } else {
        $new_r = min(max(@intval($_GET['rating']), 1), 5);
        $db->query("DELETE FROM `user_voice2` WHERE `id_user` = '" . $user['id'] . "' AND `id_kont` = '" . $ank['id'] . "' LIMIT 1");

        if ($new_r) {
            $db->query("INSERT INTO `user_voice2` (`rating`, `id_user`, `id_kont`) VALUES ('$new_r','" . $user['id'] . "','" . $ank['id'] . "')");
        }
        $res_a = $db->super_query("SELECT SUM(`rating`) as rating FROM `user_voice2` WHERE `id_kont` = '$ank[id]'");
        $ank['rating'] = $res_a['rating'];
        $db->query("UPDATE `user` SET `rating` = '$ank[rating]' WHERE `id` = '$ank[id]' LIMIT 1");

        if ($new_r == 1) {
            $db->query("INSERT INTO `notification` (`id_user`, `id_kont`, `msg`, `time`) values ('" . $user['id'] . "', '$ank[id]', '" . ($user['sex'] ? 'проголосовал' : 'проголосовала') . " за Ваш профиль! Оценка [green]+1[/green]', '$time')");
        }
        if ($new_r == 2) {
            $db->query("INSERT INTO `notification` (`id_user`, `id_kont`, `msg`, `time`) values('" . $user['id'] . "', '$ank[id]', '" . ($user['sex'] ? 'проголосовал' : 'проголосовала') . " за Ваш профиль! Оценка [green]+2[/green]', '$time')");
        }
        if ($new_r == 3) {
            $db->query("INSERT INTO `notification` (`id_user`, `id_kont`, `msg`, `time`) values('" . $user['id'] . "', '$ank[id]', '" . ($user['sex'] ? 'проголосовал' : 'проголосовала') . " за Ваш профиль! Оценка [green]+3[/green]', '$time')");
        }
        if ($new_r == 4) {
            $db->query("INSERT INTO `notification` (`id_user`, `id_kont`, `msg`, `time`) values('" . $user['id'] . "', '$ank[id]', '" . ($user['sex'] ? 'проголосовал' : 'проголосовала') . " за Ваш профиль! Оценка [green]+4[/green]', '$time')");
        }
        if ($new_r == 5) {
            $db->query("INSERT INTO `notification` (`id_user`, `id_kont`, `msg`, `time`) values('" . $user['id'] . "', '$ank[id]', '" . ($user['sex'] ? 'проголосовал' : 'проголосовала') . " за Ваш профиль! Оценка [green]+5[/green]', '$time')");
        }

        $_SESSION['message'] = lang("Вы успешно проголосовали");
        header("Location: /id" . $ank['id'] . "");
        exit;
    }
}

$sql = $db->query("SELECT * FROM `user_blacklist` WHERE `id_user` = '" . $db->safesql($ank['id']) . "' AND `id_ank` = '" . $db->safesql($user['id']) . "';");
if ($db->num_rows($sql) != 0 AND $user['level'] < 3) {
    echo "<div class='list-group-item-null list-group-item-info'>$ank[nick] " . ($ank['sex'] ? 'ограничил' : 'ограничила') . " " . lang('доступ к своей странице') . ".</div>";

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

if (isset($_POST['password'])) {
    $set_cook = $_POST['password'];
    setcookie("passprofile" . $ank['id'] . "", $set_cook);
    if (isset($_POST['password']) && $_POST['password'] == $ank['profile_password']) {
        header("Location: ?id=" . $ank['id'] . "");
    }
}
if ($ank['profile_access'] == 'only_me') {
    if ($ank['id'] == $user['id'] && isset($user) || $user['level'] >= 3) {
        
    } else {
        echo "<table class='list-group-item-null'><tr><td class='icon14'>";
        avatar($ank['id'], '60', 'border-radius: 2px;');
        echo "</td><td class='null'>";
        echo lang("Доступ к анкете") . " <b>$ank[nick]</b> " . lang('закрыт') . ".<br />";
        $doc->Link('btn btn-primary btn-sm', "/user/mail/messageList.php?contact=$ank[id]", 'envelope', 'Сообщение');

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

        if (isset($user) && $f['count'] == 0) {
            $doc->Link('btn btn-primary btn-sm', "/user/friends/add.php?id=$ank[id]", 'user-plus', 'Добавить в друзья');
        } else {
            $doc->Link('btn btn-primary btn-sm', "/user/friends/new.php?del=$ank[id]", 'user', 'Удалить из друзей');
        }
        echo "</td></tr></table>";

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

        exit();
    }
} elseif ($ank['profile_access'] == 'friends') {
    $k = $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) || $k['count'] != 0) {
        
    } else {
        echo "<table class='list-group-item-null'><tr><td class='icon14'>";
        avatar($ank['id'], '60', 'border-radius: 2px;');
        echo "</td><td class='null'>";
        echo lang("Доступ к анкете") . " <b>$ank[nick]</b> " . lang('открыт только для друзей пользователя') . ".<br />";
        $doc->Link('btn btn-primary btn-sm', "/user/mail/messageList.php?contact=$ank[id]", 'envelope', 'Сообщение');

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

        if (isset($user) && $f['count'] == 0) {
            $doc->Link('btn btn-primary btn-sm', "/user/friends/add.php?id=$ank[id]", 'user-plus', 'Добавить в друзья');
        } else {
            $doc->Link('btn btn-primary btn-sm', "/user/friends/new.php?del=$ank[id]", 'user', 'Удалить из друзей');
        }
        echo "</td></tr></table>";

        require_once H . 'sys/inc/tfoot.php';
        exit();
    }
} elseif ($ank['profile_access'] == 'pass') {
    if (isset($_COOKIE["passprofile$ank[id]"]) && $_COOKIE["passprofile$ank[id]"] == $ank['profile_password'] || $ank['id'] == $user['id'] && isset($user) || $user['level'] >= 3) {
        
    } else {
        if (isset($_POST['password']) && $_POST['password'] != $ank['profile_password']) {
            $_SESSION['err'] = lang('Неверный пароль');
        }
        echo "<table class='list-group-item'><tr><td class='icon14'>";
        avatar($ank['id'], '60', 'border-radius: 2px;');
        echo "</td><td class='null'>";
        echo "<form action='' method='post'>";
        echo lang("Доступ к анкете") . " <b>$ank[nick]</b> " . lang('доступен только по паролю') . ":<br/>";
        $doc->Input('password', 'Пароль', 100);
        $doc->Button('btn btn-primary btn-sm', 'submited', null, 'Войти');
        echo "</form><br />";
        $doc->Link('btn btn-primary btn-sm', "/user/mail/messageList.php?contact=$ank[id]", 'envelope', 'Сообщение');

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

        if (isset($user) && $f['count'] == 0) {
            $doc->Link('btn btn-primary btn-sm', "/user/friends/add.php?id=$ank[id]", 'user-plus', 'Добавить в друзья');
        } else {
            $doc->Link('btn btn-primary btn-sm', "/user/friends/new.php?del=$ank[id]", 'user', 'Удалить из друзей');
        }
        echo "</td></tr></table>";

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

#Определение гостей
if (isset($user) && $user['id'] != $ank['id']) {
    $g = $db->super_query("SELECT COUNT(*) as count FROM `myguests` WHERE `id_ank` = '$ank[id]' AND `id_user`='$user[id]' LIMIT 5");

    if ($g['count'] == 0) {
        $db->query("INSERT INTO `myguests` (`id_ank`, `id_user`, `time`) VALUES ('$ank[id]', '$user[id]', '$time')");
    } else {
        $guest = $db->get_array($db->query("SELECT * FROM `myguests` WHERE `id_ank` = '$ank[id]' AND `id_user`='$user[id]' LIMIT 5"));
        $db->query("UPDATE `myguests` SET `count` = '" . ($guest['count'] + 1) . "', `time`='$time' WHERE `id` = '$guest[id]' LIMIT 5");
    }
}

echo "<div class='list-group-item-null list-group-item-grey'>";
echo "<span style='float:right;'>$ank[group_name]</span>";
echo user($ank['id']);
echo "</div>";

echo '<div style="background: url(/user/fon/img/' . $ank['id'] . '.png) center; background-size: 100%;  padding: 10px;">';
if (isset($user) && $user['id'] == $ank['id']) {
    echo "<a style='float: right;' class='hint--left' data-hint='" . lang('Обновить обложку') . "' href='/user/fon/'><i class='fa fa-camera fa-fw'></i></a>";
}
if (IS_WEB) {
    avatar($ank['id'], '150');
} else {
    avatar($ank['id'], '100');
}
$a = $db->get_array($db->query("SELECT * FROM `gallery_foto` WHERE `id_user`='$ank[id]' AND `avatar`='1' LIMIT 1"));
if (is_file(H . "sys/gallery/avatar/$a[id].$a[ras]")) {
    /*
    if (isset($user) && $user['id'] != $ank['id']) {
        echo "<div class='photo_profile'>";
        echo "<a href='/modules/foto/$ank[id]/' class='btn btn-sm hint--top' data-hint='" . lang('Мне нравится') . "'><i class='fa fa-heart-o fa-fw' style='color: white;'></i></a>";
        echo "</div>";
    }
     * 
     */
} else {
    if (isset($user) && $user['id'] == $ank['id']) {
        echo "<div class='photo_block' style='margin-top: -6px;'><a href='/modules/foto/$ank[id]/' class='hint--top-right' data-hint='" . lang('Загрузите фото в альбом. В выбранном фото нажмите Сделать фотографией профиля') . "'><i class='fa fa-camera fa-fw'></i> " . lang('Загрузить') . "</a></div>";
    }
}

echo "</div>";

echo "<div class='list-group-item-null'>";
echo "<div class='user__status user__status-message'>";
if (isset($user) && $user['id'] == $ank['id']) {
    if (isset($_POST['save_status'])) {
        if (isset($_POST['welcome']) && utf8_strlen($_POST['welcome']) <= 512) {
            $name = $db->safesql(trim($_POST['welcome']));
            $user['welcome'] = $_POST['welcome'];
            $db->query("UPDATE `user` SET `welcome` = '" . $name . "' WHERE `id` = '$user[id]' LIMIT 1");
        } else {
            $err[] = lang('Ошибка в статусе');
        }

        $_SESSION['message'] = lang('Изменения сохранены');
        header("Location: ?" . SID);
    }
    echo "<div id='accordion' aria-multiselectable='true'>";
    echo "<div role='tab' id='headingTwo'>";
    echo "<span class='hint--top right' data-hint='" . lang('Обновить статус') . "'><a class='collapsed' data-toggle='collapse' data-parent='#accordion' href='#status' aria-expanded='false' aria-controls='collapseTwo'><i class='fa fa-pencil'></i></a></span>";
    echo "</div>";
    echo "<div id='status' class='panel-collapse collapse' aria-labelledby='status'>";
    echo "<form method='post' action=''>\n";
    echo "<textarea name='welcome' class='form-control'>$user[welcome]</textarea><br />";
    echo "<button name='save_status' class='btn btn-success btn-sm'><i class='fa fa-save fa-fw'></i> Сохранить</button>";

    echo "</form><br />";
    echo "</div>";
    echo "</div>";
}
if ($ank['welcome'] != NULL) {
    echo toOutput($ank['welcome']);
} else {
    echo lang('Статус не заполнен');
}
echo "</div>";
echo "</div>";

$res = $db->super_query("SELECT SUM(`rating`) as rating FROM `user_voice2` WHERE `id_kont` = '$user[id]'");
$res_u = $res['rating'];
if (isset($user) && $user['id'] != $ank['id'] && $user['money'] >= 10 && $res_u >= 0) {

    $my_rr = $db->super_query("SELECT `rating` as rating FROM `user_voice2` WHERE `id_user` = '$user[id]' AND `id_kont` = '$ank[id]'");
    $my_r = intval($my_rr['rating']); //
    if ($my_r > '0') {
        echo "<div class='list-group-item-null'><center>";
        if ($my_r == 1) {
            echo "<a class='btn btn-secondary btn-sm'><i class='fa fa-star fa-fw'></i> <i class='fa fa-star-o fa-fw'></i> <i class='fa fa-star-o fa-fw'></i> <i class='fa fa-star-o fa-fw'></i> <i class='fa fa-star-o fa-fw'></i></a>";
        }
        if ($my_r == 2) {
            echo "<a class='btn btn-secondary btn-sm'><i class='fa fa-star fa-fw'></i> <i class='fa fa-star fa-fw'></i> <i class='fa fa-star-o fa-fw'></i> <i class='fa fa-star-o fa-fw'></i> <i class='fa fa-star-o fa-fw'></i></a>";
        }
        if ($my_r == 3) {
            echo "<a class='btn btn-secondary btn-sm'><i class='fa fa-star fa-fw'></i> <i class='fa fa-star fa-fw'></i> <i class='fa fa-star fa-fw'></i> <i class='fa fa-star-o fa-fw'></i> <i class='fa fa-star-o fa-fw'></i></a>";
        }
        if ($my_r == 4) {
            echo "<a class='btn btn-secondary btn-sm'><i class='fa fa-star fa-fw'></i> <i class='fa fa-star fa-fw'></i> <i class='fa fa-star fa-fw'></i> <i class='fa fa-star fa-fw'></i> <i class='fa fa-star-o fa-fw'></i></a>";
        }
        if ($my_r == 5) {
            echo "<a class='btn btn-secondary btn-sm'><i class='fa fa-star fa-fw'></i> <i class='fa fa-star fa-fw'></i> <i class='fa fa-star fa-fw'></i> <i class='fa fa-star fa-fw'></i> <i class='fa fa-star fa-fw'></i></a>";
        }
        echo "</center></div>";
    } else {
        echo "<div class='list-group-item-null'><center>";
        echo "<a class='btn btn-secondary btn-sm' href='/info.php?id=$ank[id]&amp;rating=1' title='+1'><i class='fa fa-star-o fa-fw'></i></a> ";
        echo "<a class='btn btn-secondary btn-sm' href='/info.php?id=$ank[id]&amp;rating=2' title='+2'><i class='fa fa-star-o fa-fw'></i></a> ";
        echo "<a class='btn btn-secondary btn-sm' href='/info.php?id=$ank[id]&amp;rating=3' title='+3'><i class='fa fa-star-o fa-fw'></i></a> ";
        echo "<a class='btn btn-secondary btn-sm' href='/info.php?id=$ank[id]&amp;rating=4' title='+4'><i class='fa fa-star-o fa-fw'></i></a> ";
        echo "<a class='btn btn-secondary btn-sm' href='/info.php?id=$ank[id]&amp;rating=5' title='+5'><i class='fa fa-star-o fa-fw'></i></a> ";
        echo "</center></div>";
    }
}

if (isset($user) && $user['id'] != $ank['id']) {
    echo "<div class='list-group-item-null list-group-item-grey'>";
    echo "<div class='btn-group'>";
    #Отправить сообщение
    $doc->Link('btn btn-secondary btn-sm', "/user/mail/messageList.php?contact=$ank[id]", 'envelope', 'Сообщение');

    #Добавить в друзья
    $fr = $db->super_query("SELECT COUNT(*) as count FROM `friends_new` WHERE (`user` = '$user[id]' AND `to` = '$ank[id]') OR (`user` = '$ank[id]' AND `to` = '$user[id]') LIMIT 1");
    $frend_new = $fr['count'];
    $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]') LIMIT 1");
    $frend = $fr['count'];
    $friend = $db->get_array($db->query("SELECT * FROM `friends_new` WHERE `to` = '$ank[id]'"));
    $friend_yes = $db->get_array($db->query("SELECT * FROM `friends_new` WHERE `user` = '$ank[id]'"));

    if ($frend_new == 0 && $frend == 0) {
        if ($ank['friendship'] == 0) {
            $doc->Link('btn btn-secondary btn-sm', "/user/friends/add.php?id=$ank[id]", 'user-plus', 'Добавить в друзья');
        }
    } elseif ($frend_new == 1) {
        if ($user['id'] == $friend['user']) {
            echo "<div class='btn-group'>";
            echo '<button class="btn btn-secondary btn-sm dropdown-toggle" data-toggle="dropdown"><i class="fa fa-user fa-fw"></i> ' . lang('Заявка отправлена') . '</button>';
            echo "<div class='dropdown-menu'>";
            $doc->Link('dropdown-item', "/user/friends/new.php?not=$ank[id]", 'user-times', 'Отменить заявку');
            echo "</div>";
            echo "</div>";
        }
        if ($user['id'] == $friend_yes['to']) {
            $doc->Link('btn btn-success btn-sm', "/user/friends/new.php?oks=$ank[id]", 'user-plus', 'Подтвердить');
        }
    } elseif ($frend == 2) {
        echo "<div class='btn-group'>";
        echo '<button class="btn btn-secondary btn-sm dropdown-toggle" data-toggle="dropdown"><i class="fa fa-user fa-fw"></i> ' . lang('Друзья') . '</button>';
        echo "<div class='dropdown-menu'>";
        $doc->Link('dropdown-item', "/user/friends/new.php?delet=$ank[id]", 'user-times', 'Убрать из друзей');
        echo "</div>";
        echo "</div>";
    }

    echo "<div class='btn-group'>";
    echo '<button class="btn btn-secondary btn-sm dropdown-toggle" data-toggle="dropdown"><i class="fa fa-ellipsis-h fa-fw"></i></button>';
    echo "<div class='dropdown-menu dropdown-menu-right'>";
    #Добавить в закладки
    $b = $db->super_query("SELECT COUNT(*) as count FROM `bookmark_user` WHERE `id_user` = '" . $user['id'] . "' AND `id_people` = '" . $ank['id'] . "' LIMIT 1");

    if ($b['count'] == 0) {
        $doc->Link('dropdown-item', "?id=$ank[id]&amp;fav=1", 'bookmark-o', 'В закладки');
    } else {
        $doc->Link('dropdown-item', "?id=$ank[id]&amp;fav=0", 'bookmark', 'Убрать из закладок');
    }
    #Добавить в черный список
    $bl = $db->super_query("SELECT COUNT(*) as count FROM `user_blacklist` WHERE `id_user` = '$user[id]' AND `id_ank` = '$ank[id]'");

    if ($bl['count'] == 0) {
        $doc->Link('dropdown-item', "/user/blacklist/?add=$ank[id]", 'ban', 'Заблокировать');
    }

    if ($ank['id'] != $user['id']) {
        $doc->Link('dropdown-item', "/user/transfer/?id=$ank[id]", 'money', 'Перевести рублей');
    }
    if ($ank['id'] != $user['id']) {
        $doc->Link('dropdown-item', "/modules/gifts/?id=$ank[id]&amp;do", 'gift', 'Отправить подарок');
    }
    echo "</div>";
    echo "</div>";
    echo "</div>";
    echo "</div>";
}


$razdel = (isset($_GET['act'])) ? htmlspecialchars($_GET['act']) : null;

switch ($razdel) {
    case 'wall':

        echo "<div class='list-group-item-null list-group-item-grey'>";
        echo "<ul class='nav nav-pills'>";
        echo "<li class='nav-item'><a class='nav-link' href='/info.php?id=$ank[id]'>" . lang('Профиль') . "</a></li>";
        echo "<li class='nav-item'><a class='nav-link active'>" . lang('Стена') . "</a></li>";
        echo "</ul>";
        echo "</div>";


        require_once 'user/wall/index.php';

        break;

    default:

        echo "<div class='list-group-item-null list-group-item-grey'>";
        echo "<ul class='nav nav-pills'>";
        echo "<li class='nav-item'><a class='nav-link active'>" . lang('Профиль') . "</a></li>";
        echo "<li class='nav-item'><a class='nav-link' href='/info.php?id=$ank[id]&amp;act=wall'>" . lang('Стена') . "</a></li>";
        echo "</ul>";
        echo "</div>";


        require_once 'user/menu.php';

        break;
}

require_once 'sys/inc/tfoot.php';
