<?php
error_reporting(E_ERROR | E_PARSE);

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

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

if (!crm_hardlock::isEmergencyServer($_GET['secondary'])) {
    $auth = new Authenticate();
    $auth->setAuthCode($authCode);
    $auth->setAuthSession($session);
    $auth->auth();
}

$ctrID  = isset($_GET['cnt']) ? $_GET['cnt'] : '';
$param  = isset($_GET['param']) ? $_GET['param'] : '';
$region = isset($_GET['rgn']) ? strtoupper($_GET['rgn']) : 'INT';

if ($session == '' || $param == '') {
    die();
}

$btnClass       = 'blue';
$bodyClass      = 'default-body';
$privatekey     = Webform::GetWebformPrivateKey();
$decoded_param  = Webform::decode($privatekey, $decodedSession, $param); // Adat decode-olása
$decoded_param  = "?{$decoded_param}";
$ex             = Webform::get_valueFromStringUrl($decoded_param, "ex");
$trial          = Webform::get_valueFromStringUrl($decoded_param, "trial");
$seatID         = Webform::get_valueFromStringUrl($decoded_param, "seatID");
$se             = Webform::get_valueFromStringUrl($decoded_param, "se");
$custom_login   = Webform::get_valueFromStringUrl($decoded_param, "custom");

$se = $se == '' && $_GET['prod'] == '2' ? 'live' : $se;

if (!crm_hardlock::isEmergencyServer($_GET['secondary'])) {
    $query = "SELECT * FROM cl_hlusers.custom_login_urls urls WHERE urls.id = ? AND urls.enabled = ? AND process = ?";
    Database::getInstance()->query($query, array('sis', 'ita', 1, 'login'));
    $urls2 = Database::getInstance()->fetchNext();

    $ver = $_GET['ver'];

    if (!empty($urls2)) {
        $custom_url = base64_encode('https://www.archlinexp.com/maintenance/webform/progress.php');

        $test_decoded_param = $decoded_param;
        $test_decoded_param .= '&custom=ita';

        $test_encoded_param = Webform::encode($privatekey, $decodedSession, $test_decoded_param);

        $data           = new stdClass();
        $data->trial    = $trial == 'true';
        $data->ver      = (int)$ver;
        $data->id       = $test_encoded_param;
        $data->session  = $session;
        $data->url      = $custom_url;
        $data->se       = strval($se);

        $dataJson       = json_encode($data);
        $encoded_data   = base64_encode($dataJson);
        $url_test       = $urls2['login_url'] . "?param={$encoded_data}";
    }

    if ($custom_login != '') {
        $query = "SELECT * FROM cl_hlusers.custom_login_urls urls WHERE urls.id = ? AND urls.enabled = ? AND process = ?";
        Database::getInstance()->query($query, array('sis', $custom_login, 1, 'login'));
        $urls = Database::getInstance()->fetchNext();

        if (!empty($urls)) {
            $custom_url = base64_encode('https://www.archlinexp.com/maintenance/webform/progress.php');

            $data           = new stdClass();
            $data->trial    = $trial == 'true';
            $data->ver      = (int)$ver;
            $data->id       = $param;
            $data->session  = $session;
            $data->url      = $custom_url;
            $data->se       = strval($se);

            $dataJson       = json_encode($data);
            $encoded_data   = base64_encode($dataJson);
            $url            = $urls['login_url'] . "?param={$encoded_data}";

            header("Location: {$url}");
            exit();
        }
    }
}

if (!crm_hardlock::isEmergencyServer($_GET['secondary']))
    $resource = new Resource($ctrID, $region);

if ($ctrID == 36) {
    $lost_name  = 'https://www.archline.hu/lost-user-name/';
    $lost_pwd   = 'https://www.archline.hu/elfelejtett-jelszo/';
    $favicon    = 'favicon_1.ico';
    $logo       = 'images/logo_hun.png';
    $webPage    = 'https://www.archline.hu/';
    $lang       = 'hu-HU';
    $langFile   = 'hun';
    $error      = CRM_Error::serverDownHun;

    if (!crm_hardlock::isEmergencyServer($_GET['secondary'])) {
        $link = preg_replace("/\[LINK1\]([^\[]+)\[\/LINK1\]/", "<a href='https://www.archline.hu/jogi/adatvedelmi-nyilatkozat' target='_blank'>$1</a>", $resource->getDlgString('10758'));
        $link = preg_replace("/\[LINK2\]([^\[]+)\[\/LINK2\]/", "<a href='https://www.archline.hu/oktatas/workshopok/regisztracios-es-lemondasi-feltetelek' target='_blank'>$1</a>", $link);
    }
} else {
    $lost_name  = 'https://www.archlinexp.com/lost-user-name/';
    $lost_pwd   = 'https://www.archlinexp.com/lost-password/';
    $favicon    = 'favicon_2.ico';
    $logo       = 'images/logo_eng.png';
    $webPage    = 'https://www.archlinexp.com/';
    $lang       = 'en-GB';
    $langFile   = 'eng';
    $error      = CRM_Error::serverDownEng;

    if (!crm_hardlock::isEmergencyServer($_GET['secondary']))
        $link = preg_replace("/\[LINK1\]([^\[]+)\[\/LINK1\]/", "<a href='https://www.archlinexp.com/legal/privacy-policy' target='_blank'>$1</a>", $resource->getDlgString('10758'));
}

if ($se == 'LT') {
    $btnClass   = 'lt-blue';
    $logo       = '/images/2022/02/07/logo.png';
    $bodyClass  = 'lt-body';
    $inputFocus = 'lt-input-focus';
}

if ($_SERVER['SERVER_NAME'] == 'www.archline.hu')
    $captcha = '6Lc8nh8TAAAAAHn4ubhvltDcpcIyGqK7JKltJ0-0';
else
    $captcha = '6LeqnB8TAAAAAJXgoIg0gtTxwsjM4d22jZIs74hS';

if ($trial == 'true')
    $headerString = '11024';
else
    $headerString = '10292';
?>

<html>

<head>
    <title>Sign in</title>

    <link href="/public/img/icons/<?= $favicon ?>" rel="shortcut icon" type="image/vnd.microsoft.icon" />
    <link rel="stylesheet" href="../css/bootstrap.min.css" />
    <link rel="stylesheet" href="public/css/style.css?time=<?= time() ?>" />

    <script src="https://www.archlinexp.com/media/jui/js/jquery.min.js"></script>
    <script src="public/js/main.js?time=<?= time() ?>"></script>
    <script src="/media/plg_captcha_recaptcha/js/recaptcha.min.js?b2802ea557ed706aee0213f83ec66152" type="text/javascript"></script>
    <script src="https://www.google.com/recaptcha/api.js?onload=JoomlaInitReCaptcha2&render=explicit&hl=<?= $lang ?>" type="text/javascript"></script>

    <style>
        .link-btn {
            background: none !important;
            border: none;
            padding: 0 !important;
            color: rgb(56, 151, 221);
            cursor: pointer;
        }

        .link-btn:hover {
            color: #265a87;
        }

        #al_error {
            text-align: center;
        }
    </style>
</head>

<body class="<?= $bodyClass ?>">
    <a href="<?= $webPage ?>" target="_blank"><img src="<?= $logo ?>" style="margin-top: 1% !important;" /></a>

    <div class="container">
        <?php if (crm_hardlock::isEmergencyServer($_GET['secondary'])) : ?>
            <h1 id="al_error"><?= $error ?></h1>
        <?php else : ?>
            <div class="login_container form-div">
                <form id="login_form" action="progress.php" method="POST">
                    <input type="hidden" name="action" value="login" />
                    <input type="hidden" name="session" value="<?= $session ?>" />
                    <input type="hidden" name="ex" value="<?= $ex ?>" />
                    <input type="hidden" name="seatID" value="<?= $seatID ?>" />
                    <input type="hidden" name="lang" value="<?= $langFile ?>" />

                    <div class="header-div">
                        <h3><?= $resource->getDlgString($headerString, '') ?></h3>
                    </div>

                    <input id="login-username" data-id="login-data" type="text" class="form-control <?= $inputFocus ?>" placeholder="<?= $resource->getDlgString('10309') ?>" name="username" required /> <br />
                    <input data-id="login-data" type="password" class="form-control <?= $inputFocus ?>" placeholder="<?= $resource->getDlgString('115') ?>" name="password" required /> <br />
                    <input id="login-btn" type="submit" name="submit" value="<?= $resource->getDlgString('4276') ?>" class="btn btn-primary btn-lg btn-block <?= $btnClass ?>" />

                    <div class="form-links">
                        <a href="<?= $lost_name ?>" target="_blank"><?= $resource->getDlgString('10319') ?></a>
                        <a id="right-text" href="<?= $lost_pwd ?>" target="_blank"><?= $resource->getDlgString('10405') ?></a>
                    </div>

                    <?php if ($trial == 'true') : ?>
                        <p class="reg-text"><?= $resource->getDlgString('10479') ?> <a class="toggle-div"><?= $resource->getDlgString('11025') ?></a></p>
                    <?php endif; ?>
                </form>

                <?php if ($trial == 'true') : ?>
                    <form id="cancel_login" action="progress.php" method="POST">
                        <input type="hidden" name="action" value="cancel_login" />
                        <input type="hidden" name="session" value="<?= $session ?>" />

                        <table width="100%">
                            <tbody>
                                <tr>
                                    <td>
                                        <hr class="newhr">
                                    </td>
                                    <td style="width:1px; padding: 0 10px; white-space: nowrap;">
                                        <?= $resource->getDlgString('4261') ?></td>
                                    <td>
                                        <hr class="newhr">
                                    </td>
                                </tr>
                            </tbody>
                        </table>

                        <input type="submit" name="submit" value="<?= $resource->getDlgString('10475') ?>" class="btn btn-primary btn-lg btn-block <?= $btnClass ?> reg-text" />

                        <?php if ($trial == 'true') : ?>
                            <p class="reg-text"><?= $resource->getDlgString('10696') ?></p>
                        <?php endif; ?>
                    </form>
                <?php endif; ?>

                <?php if (!$ex) : ?>
                    <form action="progress.php" method="POST">
                        <input type="hidden" name="action" value="ext_browser">
                        <input type="hidden" name="session" value="<?= $session ?>" />

                        <!--<input type="submit" value="<?= $resource->getDlgString('10953') ?>" class="link-btn"> -->
                    </form>

                    <!-- Ez az url (pont) az olasz custom login tesztelésére lett bekötve (csak az olaszokra lehet rákötni) -->
                    <!-- <a href="<?= $url_test ?>">.</a> -->
                <?php endif; ?>
            </div>

            <?php if ($trial == 'true') : ?>
                <div class="reg_container form-div" style="margin-top: -50px;">
                    <div id="alert-div" class="alert"></div>

                    <div class="success_container">
                        <button id="continue-btn" class="btn btn-primary btn-lg btn-block <?= $btnClass ?> reg-text"><?= $resource->getDlgString('11168') ?></button>
                    </div>

                    <form id="reg_form" action="" method="POST">
                        <input type="hidden" id="ctrID" value="<?= $ctrID != '' ? $ctrID : 44 ?>" />
                        <input type="hidden" name="jform[name]" id="jform_name" value="9213f5df77bb90075dfe735aae18bf86" class="inputbox" />
                        <input type="hidden" name="jform[username]" id="jform_username" value="9213f5df77bb90075dfe735aae18bf86" class="validate-username required" />

                        <div class="header-div">
                            <h2><?= $resource->getDlgString('10738') ?></h2>
                        </div>

                        <div class="row">
                            <div class="form-group col-md-6">
                                <input id="jform_firstname" data-id="reg-data" type="text" class="form-control <?= $inputFocus ?>" placeholder="<?= $resource->getDlgString('10739') ?>" name="jform[firstname]" required />
                            </div>

                            <div class="form-group col-md-6">
                                <input id="jform_lastname" data-id="reg-data" type="text" class="form-control <?= $inputFocus ?>" placeholder="<?= $resource->getDlgString('10756') ?>" name="jform[lastname]" required />
                            </div>
                        </div>

                        <div class="form-group">
                            <input id="jform_email" data-id="reg-data" type="email" class="form-control <?= $inputFocus ?>" placeholder="<?= mb_substr($resource->getDlgString('5496'), 0, -1) ?>" name="jform[email]" required />
                        </div>

                        <div class="form-group">
                            <input id="jform_phone" data-id="reg-data" type="text" class="form-control <?= $inputFocus ?>" placeholder="<?= $resource->getDlgString('11174') ?>" name="jform[phone]" required />
                        </div>

                        <div class="form-group">
                            <input id="jform_password" data-id="reg-data" type="password" class="form-control <?= $inputFocus ?>" placeholder="<?= $resource->getDlgString('115') ?>" name="jform[password]" required />
                        </div>

                        <div class="form-group">
                            <input id="jform_password2" data-id="reg-data" type="password" class="form-control <?= $inputFocus ?>" placeholder="<?= $resource->getDlgString('10757') ?>" name="jform[password2]" required />
                        </div>

                        <div class="checkbox">
                            <label><input type="checkbox" value="1" name="jform[gdpr][]" required /><?= $link ?></label>
                        </div>

                        <div class="control-group">
                            <div class="controls">
                                <div id="jform_captcha" class="g-recaptcha required" data-sitekey="<?= $captcha ?>" data-theme="light" data-size="normal" required></div>
                            </div>
                        </div>

                        <input id="reg-btn" type="submit" name="submit" value="<?= $resource->getDlgString('10738') ?>" class="btn btn-primary btn-lg btn-block <?= $btnClass ?> reg-text" />

                        <p class="reg-text"><?= $resource->getDlgString('10697') ?> <a class="toggle-div"><?= $resource->getDlgString('4276') ?></a></p>
                    </form>
                </div>
            <?php endif; ?>
        <?php endif; ?>
    </div>
</body>

</html>