<?php
error_reporting(E_ERROR | E_PARSE);

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

//error_log(print_r($_GET, TRUE), 3, "../tmp/error_tips.log");

$datas      = array();
$tipData    = array();
$aml        = '';
$resultXml  = '';

$datas['ctrname'] = 'unknown';
$datas['user_IP'] = $_SERVER['REMOTE_ADDR'];

//get country name from ip
/*$locale = file_get_contents("http://freegeoip.app/xml/".$user_IP, FALSE, stream_context_create(array('http'=>array('timeout'=>2)) ) );
if (strstr($locale, "CountryName")) {
    $tomb               = json_decode(json_encode(simplexml_load_string($locale)), TRUE);
    $datas['ctrname']   = $tomb["CountryName"];
}*/

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

if (!isset($datas['ctrname']) || $datas['ctrname'] == '') $datas['ctrname'] = 'unknown';

$datas['pwd']                   = Webform::get_valueFromStringUrl($decoded_param, "serialNumber");
$datas['compID']                = Webform::get_valueFromStringUrl($decoded_param, "winchesterAdatai");
$datas['build']                 = Webform::get_valueFromStringUrl($decoded_param, "build");
$datas['ctrID']                 = Webform::get_valueFromStringUrl($decoded_param, "countryPhoneID");
$datas['telepitesikod']         = Webform::get_valueFromStringUrl($decoded_param, "installationCode");
$datas['osdata']                = Webform::get_valueFromStringUrl($decoded_param, "osMajorMinorBuild");
/*$datas['usGCMan']               = Webform::get_valueFromStringUrl($decoded_param, "vidMan");
$datas['usGCType']              = Webform::get_valueFromStringUrl($decoded_param, "vidType");
$datas['usGCDriverVer']         = Webform::get_valueFromStringUrl($decoded_param, "vidDrv");*/
$datas['npID']                  = Webform::get_valueFromStringUrl($decoded_param, "installationID");
$datas['partnerID']             = Webform::get_valueFromStringUrl($decoded_param, "partnerID");
$datas['name']                  = Webform::get_valueFromStringUrl($decoded_param, "clientName");
$datas['email']                 = Webform::get_valueFromStringUrl($decoded_param, "clientEmail");
$datas['company']               = Webform::get_valueFromStringUrl($decoded_param, "clientCompany");
$datas['phone']                 = Webform::get_valueFromStringUrl($decoded_param, "clientTelephone");
$datas['newsletter']            = Webform::get_valueFromStringUrl($decoded_param, "newsletter");
$datas['reactivationCounter']   = Webform::get_valueFromStringUrl($decoded_param, "reenabledCounter");
$datas['isTwTw']                = Webform::get_valueFromStringUrl($decoded_param, "twentyTwenty");
$datas['lang']                  = Webform::get_valueFromStringUrl($decoded_param, "languageID");
$datas['wentToGray']            = Webform::get_valueFromStringUrl($decoded_param, "wentToGray");
$datas['startCounter']          = Webform::get_valueFromStringUrl($decoded_param, "startCounter");
$datas['op64Bit']               = Webform::get_valueFromStringUrl($decoded_param, "op64Bit");
$datas['wt']                    = Webform::get_valueFromStringUrl($decoded_param, "wt");
$datas['seatID']                = Webform::get_valueFromStringUrl($decoded_param, "seatID");
$datas['floating']              = Webform::get_valueFromStringUrl($decoded_param, "fl");
$datas['offline']               = Webform::get_valueFromStringUrl($decoded_param, "om");
$datas['se']                    = Webform::get_valueFromStringUrl($decoded_param, "se");

$op64Bit            = $datas['op64Bit'] == 'true' ? 1 : 0;
$wentToGray         = $datas['wentToGray'] == 'false' ? 'green' : 'gray';
$datas['osdata']   .= '.' . $op64Bit . '.' . $wentToGray . '.' . $datas['startCounter'];

$tipData = crm_hardlock::startQuery($datas, $_GET['secondary']);

$loginEnabled = $tipData['hlNum'] == '360006' ? 'true' : 'false';

$aml .= AML::add_atribute("LOG", $tipData['logfilter']);
$aml .= AML::add_atribute("LE", $loginEnabled);

if (isset($tipData['actCode']))             $aml .= AML::add_atribute("AC", $tipData['actCode']);
if (isset($tipData['virtualization']))      $aml .= AML::add_atribute("VIR", $tipData['virtualization']);
if (isset($tipData['usstatid']))            $aml .= AML::add_atribute("USI", $tipData['usstatid']);
if (isset($tipData['moduleList']))          $aml .= AML::add_atribute("MOD", $tipData['moduleList']);
if (isset($tipData['regenr']))              $aml .= AML::add_atribute("REG", $tipData['regenr']);
if (isset($tipData['tid']))                 $aml .= AML::add_atribute("TID", $tipData['tid']);
if (isset($tipData['isFloating']))          $aml .= AML::add_atribute("FL", $tipData['isFloating']);
if (isset($tipData['isOffline']))           $aml .= AML::add_atribute("OL", $tipData['isOffline']);
if (isset($tipData['floatingFreq']))        $aml .= AML::add_atribute("FF", $tipData['floatingFreq']);
if (isset($tipData['flTolerancy']))         $aml .= AML::add_atribute("FT", $tipData['flTolerancy']);
if (isset($tipData['askForPin']))           $aml .= AML::add_atribute("P", $tipData['askForPin']);
if (isset($tipData['error']))               $aml .= AML::add_atribute("ER", $tipData['error']);
if (isset($tipData['errorDlgFreq']))        $aml .= AML::add_atribute("FD", $tipData['errorDlgFreq']);
if (isset($tipData['errorTolerancy']))      $aml .= AML::add_atribute("FDT", $tipData['errorTolerancy']);
if (isset($tipData['floatingError']))       $aml .= AML::add_atribute("FE", $tipData['floatingError']);
if (isset($tipData['statisticInterval']))   $aml .= AML::add_atribute("SI", $tipData['statisticInterval']);
if (isset($tipData['floatingTryNumber']))   $aml .= AML::add_atribute("FTN", $tipData['floatingTryNumber']);
if (isset($tipData['floatingDecTime']))     $aml .= AML::add_atribute("FDO", $tipData['floatingDecTime']);
if (isset($tipData['fddo']))                $aml .= AML::add_atribute("FDDO", $tipData['fddo']);
//if (isset($tipData['parallelDownload']))    $aml .= AML::add_atribute("PLD", $tipData['parallelDownload']);

/*
    Lehetseges plusz parameterek:
        - IT (Ignore tests)
        - DG (Dark Grey)
        - GL (Grey Link)
        - PM (Personal Message)
        - PL (Personal Link)
*/

// Ha lejart az adott idointervallum (programverzio, 2 ev), akkor nem kuldd crash riportot, viszont ezzel felul lehet biralni.
/*if ($tipData['hlNum'] == '950148') {
    $aml .= AML::add_atribute("FC", true);
}*/

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