<?php
error_reporting(E_ERROR | E_PARSE);

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

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

$privatekey     = Webform::GetWebformPrivateKey();
$decoded_param  = Webform::decode($privatekey, $decodedSession, $param);
$decoded_param  = "?{$decoded_param}";

$serial = Webform::get_valueFromStringUrl($decoded_param, "serial");
$lang   = Webform::get_valueFromStringUrl($decoded_param, "lang");

if (crm_hardlock::isEmergencyServer($_GET['secondary'])) {
    $packages = array();
} else {
    $year       = crm_hardlock::GetLaunchYearFromVer8And9(crm_hardlock::GetVer8And9FromPassword($serial));
    $packages   = crm_hardlock::getPackage($year, $lang);
}

foreach ($packages as $package) {
    AML::add_atribute('SF', 'https://www.archlinexp.com' . $package['sever_location']);
    AML::add_atribute('DS', $package['archline_location']);
    AML::add_atribute('ID', $package['id']);
    AML::add_atribute('V', $package['version']);

    $stream = AML::GetStream();
    AML::emptyAML();
    $aml .= '{P:' . strlen($stream) . '}' . $stream;
}

$privateKey = Webform::GetWebformPrivateKey();
$paramAML   = Webform::encode($privateKey, $decodedSession, $aml);

$result = 'result_ok';

$resultXml .= '<' . '?' . 'xml version="1.0"' . '?' . '><WebFormResult>';
$resultXml .= '?session=' . $session . '&' . $result . '=' . $paramAML . '&';
$resultXml .= '</WebFormResult>';

header('Content-Type: text/xml; charset=utf-8');
header('Content-Disposition: inline; filename=response.xml');

echo $resultXml;
