<?php
error_reporting(E_ERROR | E_PARSE);

if (!class_exists('Authenticate')) require_once("common_cadline_libraries/crm_test.class.php");

session_start();

$session        = isset($_GET['session']) ? $_GET['session'] : '';
$authCode       = isset($_GET['authCode']) ? $_GET['authCode'] : '';
$decodedSession = base64_decode($session);
$result         = '';
$resultXml      = '';

$auth = new Authenticate();
$auth->setAuthCode($authCode);
$auth->setAuthSession($session);
$auth->auth();

$_SESSION['nUserID']    = '';
$param                  = isset($_GET['param']) ? $_GET['param'] : '';

$privatekey     = Webform::GetWebformPrivateKey();
$decoded_param  = Webform::decode($privatekey, $decodedSession, $param); // Adat decode-olása
$decoded_param  = "?{$decoded_param}";
$serial         = Webform::get_valueFromStringUrl($decoded_param, "password");
$lang           = Webform::get_valueFromStringUrl($decoded_param, "lang");

$fileName = Webform::getLanguageFile($lang);

require_once($fileName);

$help = str_replace("[SERIAL]", $serial, $login_msg['help']);

if (isset($_POST['submit']) && $session != '' && $param != '') {
    require_once '../PasswordHash.php';

    $username = $_POST['username'];
    $query = "SELECT * FROM `" . JMLADATBAZIS . "`.`jml_users` WHERE username = ? OR email = ?";
    Database::getInstance()->query($query, array('ss', $username, $username));
    $res = Database::getInstance()->fetchNext();

    if (!empty($res)) {
        $db_password    = $res['password'];
        $nUserID        = $res['nUserID'];
        $name           = $res["name"];
        $password       = $_POST["password"];

        $phpass = new PasswordHash(10, true);
        $ok     = $phpass->CheckPassword($password, $db_password);

        if ($ok) {
            $_SESSION['nUserID'] = $nUserID;

            $query2 = "SELECT * FROM cl_hlusers.hardlock WHERE hlUser = ? AND hlNum = ?";
            Database::getInstance()->query($query2, array('ss', $nUserID, substr($serial, 0, 6)));
            $res2 = Database::getInstance()->fetchNext();

            if (empty($res2)) $error_msg = $login_msg['wrong_password'];
            else header("Location: https://www.archlinexp.com/maintenance/webform/progress.php?action=exchange_registration&session={$session}&param={$param}");
        } else $error_msg = $login_msg['wrong_password'];
    } else $error_msg = $login_msg['wrong_password'];
}
?>

<html>

<head>
    <title><?= $login_msg['title'] ?></title>

    <link href="/public/img/icons/<?= $common_msg['favicon'] ?>" rel="shortcut icon" type="image/vnd.microsoft.icon" />
    <link rel="stylesheet" href="../css/bootstrap.min.css" />

    <style>
        .form-control {
            width: 50%;
        }

        .container {
            margin-left: 15% !important;
        }

        .btn-block {
            max-width: 50% !important;
        }

        #login_form {
            margin-top: 20%;
            margin-left: 5%;
        }

        .alert-danger {
            margin-top: 10%;
            position: absolute;
            margin-left: 15.5%;
        }

        .alert-info {
            position: absolute;
            margin-left: 17%;
            margin-top: 5%;
        }

        img {
            position: absolute;
            margin-top: 2%;
            margin-left: 1%;
        }

        .btn-primary {
            background-color: #1E90FF !important;
            border-color: #1E90FF !important;
        }
    </style>
</head>

<body>
    <a href="<?= $common_msg['webpage'] ?>" target="_blank"><img src="images/<?= $common_msg['logo'] ?>" /></a>
    <div class="container">
        <br />

        <?php if (isset($_GET['param'])) : ?>
            <div class="alert alert-info" role="alert">
                <?= $help ?>
            </div>
            <br />
        <?php endif; ?>

        <?php if (isset($error_msg)) : ?>
            <div class="alert alert-danger" role="alert">
                <?= $error_msg ?>
            </div>
        <?php endif; ?>

        <form id="login_form" method="POST" action="">
            <div class="container">
                <input type="hidden" name="action" value="<?= $_GET['action'] ?>" />
                <input type="text" class="form-control" placeholder="<?= $login_msg['username'] ?>" name="username" required /> <br />
                <input type="password" class="form-control" placeholder="<?= $login_msg['password'] ?>" name="password" required /> <br />
                <input type="submit" name="submit" value="<?= $login_msg['login'] ?>" class="btn btn-primary btn-lg btn-block" />
            </div>
        </form>
    </div>
</body>

</html>