<?php
require("inc/include.php");
if(!isset($_SESSION['user']))
{
    header("Location: index.php");
    exit;
}
$user = unserialize($_SESSION['user']);
if($user->getId() != -1)
{
    header("Location: index.php");
    exit;
}
if(!empty($_POST))
{
    $err = "";
    if(isset($_POST['mail']) && isset($_POST['country']) && isset($_POST['username']) && isset($_POST['password']) && isset($_POST['passwordCheck']))
    {
        if(($mail = filter_var($_POST['mail'], FILTER_VALIDATE_EMAIL)) === false)
        {
            $err .= $l->getString("The Email address is invalid.") . "<br>";
        }
        if($db->mailExists($mail))
        {
            $err .= $l->getString("This mail address is already used, maybe you're already registered.") . "<br>";
        }
        $countryId = intval($_POST['country']);
        if($countryId < 1 || $countryId > 245)
        {
            $err .= $l->getString("The country selected is invalid.") . "<br>";
        }
        $username = $_POST['username'];
        if(preg_match($config['forms']['usernameRegex'], strtolower($username)) == false)
        {
            $err .= $l->getString("Your username must be between 4 and 12 characters and only contain alphanumeric characters as well as hyphens and underscores.") . "<br>";
        }
        if($db->usernameExists($username))
        {
            $err .= $l->getString("This username is already used, please choose another one.") . "<br>";
        }
        $password = $_POST['password'];
        if(strlen($password) < $config['forms']['passwordMin'] || strlen($password) > $config['forms']['passwordMax'])
        {
            $err .= $l->getString("Your password must be between %d and %d characters.", $config['forms']['passwordMin'], $config['forms']['passwordMax']) . "<br>";
        }
        $passwordCheck = $_POST['passwordCheck'];
        if($password !== $passwordCheck)
        {
            $err .= $l->getString("Your passwords don't match.") . "<br>";
        }
        $tradeUrl = $_POST['tradeUrl'];
        $tradeUrlParams = parse_url($tradeUrl);
        if($tradeUrlParams === false || !isset($tradeUrlParams['query']))
        {
            $err .= $l->getString("This trade URL isn't correct.") . "<br>";
        }
        else
        {
            parse_str($tradeUrlParams['query'], $arrayParams);
            if(!isset($arrayParams['token']))
            {
                $err .= $l->getString("This trade URL isn't correct.") . "<br>";
            }
            else
            {
                $token = $arrayParams['token'];
            }
        }
        if(empty($err))
        {
            $passwordHash = password_hash($password, PASSWORD_DEFAULT, ['cost' => 12]);
            $userId = $db->insertUser($user->getSteamId(), $user->getAvatar(), $token, $countryId, $mail, $username, $passwordHash, getIp(), $user->getRef());
            if($userId)
            {
                $user = $db->getUser($userId);
                $_SESSION['user'] = serialize($user);
			    header("Location: index.php");
			    exit;
            }
            else
            {
                $err .= $l->getString("SQL registration failed.") . "<br>";
            }
        }
    }
    else
    {
        $err .= $l->getString("Some data is missing.") . "<br>";
    }
}
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("Register"); ?></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="register.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>
                <label for="passwordCheck">- <?php echo $l->getString("Retype password"); ?></label><br>
                <input name="passwordCheck" id="passwordCheck" type="password">
                <br><br>
                <label for="mail">- <?php echo $l->getString("Email address"); ?></label><br>
                <input name="mail" id="mail" type="email">
                <br><br>
                <label for="country">- <?php echo $l->getString("Country"); ?></label><br>
                <select id="country" name="country">
                    <option value="0"><?php echo $l->getString("Select a country"); ?></option>
                    <?php
                    $countries = $db->getAllCountries();
                    foreach($countries as $country)
                    {
                        echo "<option value='" . $country['id'] . "'>" . $country['country'] . "</option>";
                    }
                    ?>
                </select>
                <br><br>
                <label for="tradeUrl">- <?php echo $l->getString("Trade URL"); ?></label> ( <a href="https://steamcommunity.com/id/me/tradeoffers/privacy"><?php echo $l->getString("Where to find my trade URL ?"); ?></a>)<br>
                <input name="tradeUrl" id="tradeUrl" type="url">
                <br><br>
                <button type="submit"><?php echo $l->getString("Register"); ?></button>
                <br><br>
            </form>
        </div>
    </div>
    <div class="b-footer-wrapper">
        <div class="b-container">
<?php
require "inc/footer.php";
?>