<?php


$object = $request['object'];
switch ($request['header-type']) {
    case 'login':
        if (isset($object)) {
            $select = $DLA->Select("Select * From system_user WHERE user_name='{$object['username']}'");
            $select = $select[0];
            if ($select['pass_word'] == md5($object['password'])) {
                logOut($_COOKIE["user"]);
                $select = $DLA->Select("Select * From system_user_access WHERE aua_id='{$select['ID']}'");
                if ($select) {
                    $select = $select[0];
                    setcookie("user", $object['username'], time() + (3600), "/");
                    session_start();
                    $uniqId = uniqid();
                    $_SESSION[$object['username']]['uniqid'] = $uniqId;
                    $_SESSION[$object['username']][$uniqId]['login'] = true;
                    $_SESSION[$object['username']][$uniqId]['access-item'] = $select['aua_item'];
                    $_SESSION[$object['username']][$uniqId]['access-id'] = $select['aua_id'];
                    $_SESSION[$object['username']][$uniqId]['access-type'] = $select['aua_type'];
                    echo 1;
                } else {
                    echo 'سطح دسترسی از بین رفته است';
                }

            } else {
                echo 'نام کاربری پیدا نشد' . "error : 1";
            }
        } else {
            echo 'مقادیر لازم است' . "error : 0";
        }
        break;
    case
    'logout':
        echo(logOut($_COOKIE["user"]) ? 1 : 0);
        break;
    default:
        break;
}

function logOut($user)
{
    session_start();
    $uniqID = $_SESSION[$user]['uniqid'];
    if (isset($uniqID)) {
        if (isset($_SESSION[$user][$uniqID]['login']) and $_SESSION[$user][$uniqID]['login']) {
            unset($_SESSION[$user][$uniqID]['access-item']);
            unset($_SESSION[$user][$uniqID]['access-id']);
            unset($_SESSION[$user][$uniqID]['access-type']);
            unset($_SESSION[$user][$uniqID]);
            unset($_SESSION[$user]);
            setcookie("user", null, -1, '/');
            return true;
        }
        return false;
    }
    return false;
}