<?php

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

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

$show_all = true; // показ для всех
$input_page = true;
require_once H . 'sys/inc/user.php';
only_unreg();

if (isset($_GET['id']) && isset($_GET['pass']) and $set['avto_login_set'] == 1) {
    $query = $db->super_query("SELECT COUNT(*) as count FROM user WHERE id = '" . intval($_GET['id']) . "' AND pass = '" . shif($_GET['pass']) . "' LIMIT 1");
    if ($query['count'] == 1) {
        $user = get_user($_GET['id']);
        $_SESSION['id_user'] = $user['id'];

        $db->query("UPDATE user SET date_aut = '" . time() . "', date_last = '" . time() . "' WHERE id = '$user[id]' LIMIT 1");
        $db->query("INSERT INTO user_log (id_user, time, ua, ip, method) values ('$user[id]', '$time', '$user[ua]', '$user[ip]', '0')");
    } else {
        $err[] = lang('Неправильный логин или пароль');
    }
    unset($query);
} elseif (isset($_POST['nick']) && isset($_POST['pass'])) {
    $query = $db->super_query("SELECT COUNT(*) as count FROM user WHERE nick = '" . $db->safesql($_POST['nick']) . "' AND pass = '" . shif($_POST['pass']) . "' LIMIT 1");
    if ($query['count']) {
        $user = $db->get_row($db->query("SELECT id FROM user WHERE nick = '" . $db->safesql($_POST['nick']) . "' AND pass = '" . shif($_POST['pass']) . "' LIMIT 1"));
        $_SESSION['id_user'] = $user['id'];
        $user = get_user($user['id']);

        $db->query("INSERT INTO user_log (id_user, time, ua, ip, method) values('$user[id]', '$time', '$user[ua]', '$user[ip]', '1')");
        $db->query("DELETE FROM guests WHERE ip = '$user[ip]' AND ua = '$user[ua]'");
        #сохранение данных в COOKIE
        if (isset($_POST['aut_save']) && $_POST['aut_save']) {
            setcookie('id_user', $user['id'], time() + 60 * 60 * 24 * 365);
            setcookie('pass', cookie_encrypt($_POST['pass'], $user['id']), time() + 60 * 60 * 24 * 365);
        }

        if ($set['antihah_hash'] == 1 and $user['hash_set'] == 1) {
            $hash = " hash = '" . md5(md5($ip . md5($ua) . $user['id'])) . "',";
        } else {
            $hash = null;
        }

        $db->query("UPDATE user SET browser = '" . (IS_WEB == 'web' ? 'web' : 'wap') . "' WHERE id = '" . $user['id'] . "'");
        $db->query("UPDATE user SET $hash date_aut = '$time', date_last = '$time' WHERE id = '$user[id]' LIMIT 1");
    } else {
        $err[] = lang('Неправильный логин или пароль');
    }
    unset($query);
} elseif (isset($_COOKIE['id_user'], $_COOKIE['pass']) && $_COOKIE['id_user'] && $_COOKIE['pass']) {
    cache_delete::user($_COOKIE['id_user']);

    $query = $db->super_query("SELECT COUNT(*) as count FROM user WHERE id = '" . intval($_COOKIE['id_user']) . "' AND hash_set = '1' LIMIT 1");
    if ($set['antihah_hash'] == 1 AND $query['count'] == 1) {
        $hash = " hash = '" . md5(md5($ip . md5($ua) . $_COOKIE['id_user'])) . "' AND ";
    } else {
        $hash = null;
    }

    $query = $db->super_query("SELECT COUNT(*) as count FROM `user` WHERE $hash id = '" . intval($_COOKIE['id_user']) . "' AND pass = '" . shif(cookie_decrypt($_COOKIE['pass'], intval($_COOKIE['id_user']))) . "' LIMIT 1");
    if ($query['count'] == 1) {
        $user = get_user($_COOKIE['id_user']);
        $_SESSION['id_user'] = $user['id'];

        $db->query("UPDATE user SET date_aut = '$time', date_last = '$time' WHERE id = '$user[id]' LIMIT 1");
        $db->query("INSERT INTO user_log (id_user, time, ua, ip, method) values('$user[id]', '$time', '$user[ua]' , '$user[ip]', '2')");
        $db->query("UPDATE user SET browser = '" . (IS_WEB == 'web' ? 'web' : 'wap') . "' WHERE id = '" . $user['id'] . "'");

        $user['type_input'] = 'cookie';
    } else {
        $err[] = lang('Ошибка авторизации по COOKIE');
        setcookie('id_user');
        setcookie('pass');
        header("Location: /aut.php");
        exit();
    }
    unset($query);
} else {
    $err[] = lang('Ошибка авторизации');
}

if (!isset($user)) {
    $set['title'] = lang('Авторизация');

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

    aut();
    err();

    $doc->Link('list-group-item-null', '/aut.php', 'arrow-left', 'Повторить попытку входа');

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

if (isset($_GET['return'])) {
    header('Location: ' . urldecode($_GET['return']));
} else {
    if ($set['aut_ref'] == 1 && isset($_SESSION['ref_loc'])) {
        exit(header("Location: " . $_SESSION['ref_loc'] . "?&aut_ref"));
    } else {
        exit(header("Location: /index.php"));
    }
}

exit;
