<?php
require("inc/include.php");
if(isset($_SESSION['user']))
{
    $user = unserialize($_SESSION['user']);
    if($user->getId() > 0)
    {
        header("Location: index.php");
        exit;
    }
}
require_once('inc/func.php');
include_once("inc/steamSignIn.class.php");
$steamId = SteamSignIn::validate();//*/0;
if($steamId > 0)
{
    $ipAddress = getIp();
    $userFile = file_get_contents('http://api.steampowered.com/ISteamUser/GetPlayerSummaries/v0002/?key=' . $config['steamApiKey'] . '&steamids=' . $steamId);
    $userJson = json_decode($userFile);
    $steamFailed = false;
    if($userJson != null) {
        $userAvatar = $userJson->response->players[0]->avatarmedium;
    } else {
        $userAvatar = "http://cdn.akamai.steamstatic.com/steamcommunity/public/images/avatars/fe/fef49e7fa7e1997310d705b2a6158ff8dc1cdfeb_full.jpg";
        $steamFailed = true;
    }
    if($db->userSteamIdExists($steamId) && !$db->isUserBanned($steamId))
    {
        $db->userConnected($ipAddress, $steamId);
        if(!$steamFailed)
            $db->updateUserAvatar($userAvatar, $steamId);
        $user = $db->getUserFromSteamId($steamId);
        $_SESSION['user'] = serialize($user);
    }
    elseif(!$db->isUserBanned($steamId))
    {
        $referrerId = null;
        if (!empty($_SESSION['ref']) && !$db->isExistingIp($ipAddress) && $db->userIdExists($_SESSION['ref']))
        {
            $referrerId = $_SESSION['ref'];
        }
        $user = new User(-1, $steamId, 0, $userAvatar, $referrerId);
        $_SESSION['user'] = serialize($user);
    }
    else
    {
        header("Location: index.php?err=You are banned.");
        exit;
    }
    if(isset($user) && $user->getId() == -1)
    {
        header("Location: register.php");
        exit;
    }
    elseif(isset($user) && $user->getId() > 0)
    {
        header("Location: index.php");
        exit;
    }
    //setcookie("authToken", $db->getAuthTokenForUser($steamId, "mléç45Dk45£%ù"), time() + 3600 * 24 * 365, null, null, false, true);
}
if(!empty($_POST))
{
    $hash = $db->getUserHashFromUsername($_POST['username']);
    if($hash != null && password_verify($_POST['password'], $hash))
    {
        $user = $db->getUserFromUsername($_POST['username']);
        $_SESSION['user'] = serialize($user);
        header("Location: index.php");
        exit;
    }
    else
    {
        $error = $l->getString("Your username or password is incorrect.");
    }
}
require('inc/header.php');
?>
<div class="b-container">
    <div class="b-items-group b-items-5 b-items-cases" style="width:100%;">
        <br>
        <h1><span class="b-user-profile-name" style="text-align:center;"><?php echo $l->getString("Log In"); ?></span></h1>
        <?php
        if(isset($err) && !empty($err))
        {
            ?>
            <div class="alert alert-danger"><?php echo $err; ?></div>
            <?php
        }
        ?>
        <div class="b-trade-url-input-wrapper">
            <form method="POST" action="login.php">
                <label form="username">- <?php echo $l->getString("Username"); ?></label> ( <?php echo $l->getString("Between 4 and 12 characters"); ?> )<br>
                <input name="username" id="username" type="text">
                <br><br>
                <label for="password">- <?php echo $l->getString("Password"); ?></label> ( <?php echo $l->getString("Between %d and %d characters", $config['forms']['passwordMin'], $config['forms']['passwordMax']); ?> )<br>
                <input name="password" id="password" type="password">
                <br><br>
                <button type="submit"><?php echo $l->getString("Log In"); ?></button>
                <br><br>
            </form>
        </div>
    </div>
    <div class="b-footer-wrapper">
        <div class="b-container">
<?php
require("inc/footer.php");
?>