<?php
error_reporting(E_ERROR | E_PARSE);

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

$param          = isset($_GET['param']) ? $_GET['param'] : '';
$session        = isset($_GET['session']) ? $_GET['session'] : '';
$verID          = isset($_GET['ver']) ? $_GET['ver'] : '';
$region         = isset($_GET['rgn']) ? strtolower($_GET['rgn']) : 'int';
$decodedSession = base64_decode($session);

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

$password       = Webform::get_valueFromStringUrl($decoded_param, "pwd");
$credentials    = crm_hardlock::GetFileTransferCredentials($verID, $region);

$protocol   = $credentials['protocol'];
$server     = $credentials['server'];
$account    = $credentials['account'];
$pwd        = $credentials['pwd'];
$aml        = '';
$resultXml  = '';

$aml .= AML::add_atribute("PR", $protocol);
$aml .= AML::add_atribute("SV", $server);
$aml .= AML::add_atribute("ACC", $account);
$aml .= AML::add_atribute("PW", $pwd);

$stream     = AML::GetStream();
$privateKey = Webform::GetWebformPrivateKey();
$paramAML   = Webform::encode($privateKey, $decodedSession, $stream);

$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;
