<?php

include '../common.php';
use Ministra\Lib\w13ec15f0067ea75861e61e27c990e4ba\d3507c2999d38573a0a0c91771d70af2;
if (empty($_REQUEST['login']) || empty($_REQUEST['password'])) {
    echo '{"status":"ERROR","results":false,"error":"Login and password required"}';
    exit;
}
\sleep(1);
$login = $_REQUEST['login'];
$password = $_REQUEST['password'];
$mac = isset($_REQUEST['mac']) ? $_REQUEST['mac'] : '';
$possible_user = \Ministra\Lib\w13ec15f0067ea75861e61e27c990e4ba\d3507c2999d38573a0a0c91771d70af2::getInstance()->from('users')->where(['login' => $login])->get()->first();
if (\md5(\md5($password) . $possible_user['id']) === $possible_user['password']) {
    $user = $possible_user;
}
if (empty($user)) {
    echo \error('User not exist or login-password mismatch');
} else {
    if ($mac) {
        \Ministra\Lib\w13ec15f0067ea75861e61e27c990e4ba\d3507c2999d38573a0a0c91771d70af2::getInstance()->update('users', ['mac' => '', 'device_id' => '', 'device_id2' => '', 'access_token' => ''], ['mac' => $mac]);
    }
    \Ministra\Lib\w13ec15f0067ea75861e61e27c990e4ba\d3507c2999d38573a0a0c91771d70af2::getInstance()->update('users', ['mac' => $mac], ['id' => $user['id']]);
    echo '{"status":"OK","results":true}';
}
