<?php
if (!class_exists('crm_hardlock')) require_once("common_cadline_libraries/crm_hardlock.class.php");

$lang = strtolower($_GET["lang"]);
switch ($lang) {
    case "hun":
        $lang_file = 'lang.hun.php';
        break;
    case "ita":
        break;
    case "esp":
        break;
    case "ger":
        break;
    case "cz":
        break;
    case "pol":
        break;
    case "cr":
        break;
    case "kor":
        break;
    case "chn":
        break;
    case "nor":
        break;
    case "fr":
        break;
    default:
        $lang_file = 'lang.eng.php';
        break;
}

include_once 'languages/' . $lang_file;

$download   = true;
$showError  = true;
$alert      = "info";

$errors = array(
    'crc_error'                         => $lang['ERROR_CRC'],
    'error: nem aktiv a program'        => $lang['ERROR_PROGRAM_DISABLED'],
    'error: masik gep van regisztralva' => $lang['ERROR_ANOTHER_COMPUTER_REGISTERED'],
    'error: tul sok telepites'          => $lang['ERROR_TOO_MANY_INSTALLATION'],
    'error: nincs ilyen password'       => $lang['ERROR_UNKNOWN_PASSWORD'],
    'error: hianyzo kod'                => $lang['ERROR_CODE_IS_MISSING'],
    'error: nincs vasarlasi idopont'    => $lang['ERROR_SELL_DATE_IS_MISSING'],
    'error: nem aktiv regisztracio'     => $lang['ERROR_INACTIVE_REGISTRATION']
);

$solutions = array(
    'crc_error'                         => $lang['SOLUTION_CRC'],
    'error: nem aktiv a program'        => $lang['SOLUTION_PROGRAM_DISABLED'],
    'error: masik gep van regisztralva' => $lang['SOLUTION_ANOTHER_COMPUTER_REGISTERED'],
    'error: tul sok telepites'          => $lang['SOLUTION_TOO_MANY_INSTALLATION'],
    'error: nincs ilyen password'       => $lang['SOLUTION_UNKNOWN_PASSWORD'],
    'error: hianyzo kod'                => $lang['SOLUTION_CODE_IS_MISSING'],
    'error: nincs vasarlasi idopont'    => $lang['SOLUTION_SELL_DATE_IS_MISSING'],
    'error: nem aktiv regisztracio'     => $lang['SOLUTION_INACTIVE_REGISTRATION']
);

$url        = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on' ? "https" : "http") . "://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
$url        = substr($url, 0, strpos($url, "&"));
$parsed_url = parse_url($url);
$data       = substr($parsed_url["query"], 5);

if (isset($data) && $data != '') {
    $data           = base64_decode($data);
    $xmldata        = simplexml_load_string($data) or die("Failed to load");
    $licenseData    = $xmldata["LicenceData"];
    $privateKey     = crm_hardlock::GetWebformPrivateKey();
    $publicKey      = substr($licenseData, 22, 22);
    $licenseData    = substr($licenseData, 44, -26);
    $oldCrc         = substr($xmldata["LicenceData"], -4);
    $oldEncoded     = substr($xmldata["LicenceData"], 0, -4);
    $oldmd5         = substr(md5($oldEncoded), -4);

    if ($oldCrc != $oldmd5) {
        $lang['MESSAGE']    = $errors['crc_error'] . "<br/><br/>" . $solutions['crc_error'];
        $alert              = "danger";
        $download           = false;
        $showError          = false;
    }

    $decoded            = crm_hardlock::decode($privateKey, $publicKey, $licenseData);
    $regData            = simplexml_load_string($decoded) or die("Failed to load");
    $password           = $regData["password"];
    $installationCode   = $regData["installationCode"];
    $npid               = $regData["npid"];
    $computerName       = $regData["computerName"];
    $isid               = crm_hardlock::GetIsidFromTimeCode($installationCode);

    $register_result = crm_hardlock::RegisterProgram($password, $isid, $installationCode, $npid, $computerName);
    if (substr($register_result, 0, 6) == "error:" && $showError) {
        $lang['MESSAGE']    = $errors[$register_result] . "<br/><br/>" . $solutions[$register_result];
        $alert              = "danger";
        $download           = false;
        $showError          = false;
    }

    $actCode = crm_hardlock::GetOrGenerateTimeCode($password, $isid, $installationCode, $npid);
    if (substr($actCode, 0, 6) == "error:" && $showError) {
        $lang['MESSAGE']    = $errors[$actCode] . "<br/><br/>" . $solutions[$actCode];
        $alert              = "danger";
        $download           = false;
        $showError          = false;
    }

    if ($computerName != "") {
        $comp       = "_" . $computerName;
        $dataComp   = ' computerName="' . $computerName . '"';
    }

    $ver8and9 = crm_hardlock::GetVer8And9FromPassword($password);
    crm_database::getInstance()->query("SELECT year FROM `" . CRMADATBAZIS . "`.`p_versions` WHERE verPassword8and9 = '" . $ver8and9 . "' LIMIT 1");
    $versions = crm_database::getInstance()->fetchAssoc();

    $year               = $versions[0]['year'];
    $data               = '<RegData password="' . $password . '" installationCode="' . $actCode . '" npid="' . $npid . '"' . $dataComp . ' />';
    $newPublicKey       = crm_hardlock::GenerateRandomString();
    $guid               = crm_hardlock::GenerateRandomString();
    $encodedlicenceData = crm_hardlock::encode($privateKey, $newPublicKey, $data);
    $md5                = md5($guid . $newPublicKey . $encodedlicenceData . $guid);
    $crc                = substr($md5, -4);
    $licenceData        = '<?xml version="1.0" encoding="UTF-8"?>\n<ARCHLineXP_Licence LicenceData="' . $guid . $newPublicKey . $encodedlicenceData . $guid . $crc . '"/>';
    $filename           = "ArchlineXP_" . $year . "_Licence_" . substr($password, 0, 6) . $comp . "_" . $isid . ".allic";
}
?>

<html>

<head>
    <meta charset="UTF-8" />
    <link rel="stylesheet" href="css/bootstrap.min.css" />
    <script src="js/download2.js"></script>

    <style>
        #alert-div {
            position: absolute;
            margin-left: 38%;
            margin-right: 10%;
            margin-top: 15%;
        }

        body {
            background-color: #202020;
        }

        #download-btn {
            margin-left: 40%;
            margin-top: 20%;
            background-color: #e47d25 !important;
            border-color: #e47d25 !important;
        }

        #download-btn:hover {
            opacity: 0.8;
        }
    </style>
</head>

<body>
    <div class="alert alert-<?= $alert ?>" role="alert" id="alert-div">
        <?= $lang['MESSAGE'] ?>
    </div>
    <?php if ($download) : ?><button class="btn btn-primary btn-lg" id="download-btn" onclick="downloadFile()"><?= $lang['DOWNLOAD_BUTTON'] ?></button><?php endif; ?>
</body>

</html>

<?php if ($download) : ?>
    <script>
        function downloadFile() {
            download('<?= $licenceData ?>', '<?= $filename ?>', 'text/xml');
        }
    </script>
<?php endif; ?>