<?php
//die(print_r($_POST));
if (isset($_COOKIE['econtoken']) && $_COOKIE['econtoken'] != "") { // van token
    if ($_SERVER['REQUEST_URI'] == "/logout") {
        require_once('templates/template/logout.php');
    } else {
        $token = $_COOKIE['econtoken'];
        $filter = ['econtoken' => $token];
        //print_r($filter);
        $result = $auth->getApiDataFilter('get/checktoken', $filter);
        //print_r($result);
        if (!isset($result->status)) {
            //print_r($result);
            setrawcookie('econtoken', '', time() + 60 * 60 * 8, '/');
            require_once('templates/login/login.php');
            die();
        }
    }
} else if (isset($_POST['econusername']) && isset($_POST['econpassword'])) {
    if (strpos($_POST['econusername'], '@') > 0) {
        $filter = ['email' => stripslashes($_POST['econusername']), 'password' => stripslashes($_POST['econpassword'])];
    } else {
        $filter = ['username' => stripslashes($_POST['econusername']), 'password' => stripslashes($_POST['econpassword'])];
    }
    $result = $auth->getApiDataFilter('get/checklogin', $filter);
    //
    if (!isset($result->errorcode)) {
        //print_r($result);
        setrawcookie('econtoken', $result->token, time() + 60 * 60 * 8, '/');
    }
    //print_r($_SERVER);
    /*print_r($result);
    die();*/
    header("Location: " . $_SERVER['HTTP_REFERER']);
} else {
    require_once('templates/login/login.php');
    die();
}
