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

$saveEncoded    = TRUE;
$aktid          = (int)$_GET['id'];
$encoded        = trim(str_replace(" ", "+", $_GET['data']));
$decoded        = trim(base64_decode($encoded));
$temp           = simplexml_load_string(trim($decoded), "SimpleXMLElement", LIBXML_NOCDATA);
$data           = json_decode(json_encode($temp), true);

if ($aktid > 0 && $decoded > '') {
    $query = "SELECT * FROM `cl_hlusers`.`userstats2013` WHERE usID = ?";
    Database::getInstance()->query($query, array('i', $aktid));
    $res    = Database::getInstance()->fetchNext();
    $build  = !empty($res) ? (int)$res['usBuild'] : NULL;

    //az url parameterben (GET) kapott ket mezot mindenkeppen mentse le a tablaba
    $rec['usID']    = $aktid;
    $rec['strData'] = $decoded;

    $bind_type = 'iss';

    if ($saveEncoded) $rec['strEncoded'] = addslashes($encoded);
    if (isset($data['@attributes']['Pwd'])) {
        $rec['Pwd']     = trim(addslashes($data['@attributes']['Pwd']));
        $rec['hlNum']   = (int)substr($data['@attributes']['Pwd'], 0, 6);
        $rec['CtrCode'] = (int)substr($data['@attributes']['Pwd'], 0, 2);
        $rec['VerCode'] = (int)substr($data['@attributes']['Pwd'], 7, 2);

        if (substr($data['@attributes']['Pwd'], 6, 1) == 'L') $rec['PrgType'] = (int)76;
        else $rec['PrgType'] = (int)substr($data['@attributes']['Pwd'], 6, 1);

        $bind_type .= 'siiii';
    }
    if (isset($data['@attributes']['Res'])) {
        $rec['Res'] = trim(addslashes($data['@attributes']['Res']));
        $bind_type .= 's';
    }
    if (isset($data['@attributes']['Lang'])) {
        $rec['Lang'] = trim(addslashes($data['@attributes']['Lang']));
        $bind_type .= 's';
    }
    if (isset($data['@attributes']['Prot'])) {
        $rec['Prot'] = trim(addslashes($data['@attributes']['Prot']));
        $bind_type .= 'i';
    }
    if (isset($data['@attributes']['WBit64'])) {
        $rec['WBit64'] = (strtolower($data['@attributes']['WBit64']) == 'true' ? 'Y' : 'N');
        $bind_type .= 's';
    }
    if (isset($data['@attributes']['ABit64'])) {
        $rec['ABit64'] = (strtolower($data['@attributes']['ABit64']) == 'true' ? 'Y' : 'N');
        $bind_type .= 's';
    }
    if (isset($data['@attributes']['Rib'])) {
        $rec['Rib'] = (strtolower($data['@attributes']['Rib']) == 'true' ? 'Y' : 'N');
        $bind_type .= 's';
    }
    if (isset($data['@attributes']['CompID']))
        $CompID = trim(addslashes($data['@attributes']['CompID']));
    if (isset($data['@attributes']['OpVer'])) {
        $rec['OpVer'] = trim(addslashes($data['@attributes']['OpVer']));
        $bind_type .= 's';
    }
    if (isset($data['@attributes']['Ver'])) {
        $rec['Ver'] = trim(addslashes($data['@attributes']['Ver']));
        $bind_type .= 's';
    }
    if (isset($data['@attributes']['Build']) && (int)$data['@attributes']['Build'] > 0) {
        $rec['Build'] = (int)$data['@attributes']['Build'];
        $bind_type .= 'i';
    }
    if (!isset($rec['Build']) && $build > 0) {
        $rec['Build'] = $build;
        $bind_type .= 's';
    }


    if (isset($data['@attributes']['DiagStatus']) && $data['@attributes']['DiagStatus'] != '') {
        $rec['need_error'] = 1;
        $bind_type .= 'i';
    } else {
        $rec['need_error'] = 0;
        $bind_type .= 'i';
    }

    if (isset($data['@attributes']['GraphMode'])) {
        $graphmode = trim(addslashes($data['@attributes']['GraphMode']));
        if (in_array($graphmode, array('OGL', 'DX9', 'DX11', 'Vulkan', 'GDI'))) {
            $rec['GraphMode'] = $graphmode;
            $bind_type .= 's';
        }
    }
    if (isset($data['@attributes']['FalseTestResults']) && $data['@attributes']['FalseTestResults'] != '') {
        $test_results = $data['@attributes']['FalseTestResults'];

        $test_result = explode(";", $test_results);
        $resultArray = array();

        foreach ($test_result as $result) {
            $test = explode(":", $result);

            $resultArray[] = array($test[0] => $test[1]);
        }

        $maxs = array_keys($resultArray, max($resultArray));
        $maxs = $maxs[0];
    } else
        $maxs = -1;

    $query = "SELECT * FROM `" . CRMADATBAZIS . "`.`computers` WHERE computer_id = ?";
    Database::getInstance()->query($query, array('s', $CompID));
    $res2   = Database::getInstance()->fetchNext();
    $compid = $res2['id'];

    $query = "SELECT * FROM `" . CRMADATBAZIS . "`.`telemetria` WHERE Pwd = ? AND Build = ? AND computer_id = ?";
    Database::getInstance()->query($query, array('sii', $rec['Pwd'], $rec['Build'], $compid));
    $res3 = Database::getInstance()->fetchNext();

    if (empty($res3)) { // INSERT
        $rec['add_ip']          = $_SERVER['REMOTE_ADDR'];
        $rec['add_datetime']    = date('Y-m-d H:i:s', time());
        $rec['computer_id']     = $compid;
        $rec['mod_count']       = 1;
        $rec['error_code']      = $maxs;
        $bind_type             .= 'ssiii';
        $recID = Database::getInstance()->insert(CRMADATBAZIS . ".telemetria", $rec, $bind_type);
    } else { // UPDATE
        $rec['mod_count']       = $res3['mod_count'] + 1;
        $rec['mod_datetime']    = date('Y-m-d H:i:s', time());
        $rec['error_code']      = $maxs;
        $recID                  = $res3['id'];
        $bind_type             .= 'isii';
        Database::getInstance()->update(CRMADATBAZIS . ".telemetria", $rec, array("id" => $recID), $bind_type);
    }

    $status = crm_hardlock::getProgramStatus($rec['Prot'], $rec['Pwd']);
    Database::getInstance()->update(CRMADATBAZIS . ".userstats2013", array('status' => $status), array("usID" => $aktid), 'si');
}
