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

$encodedSession = isset($_GET['session']) ? $_GET['session'] : '';
$authCode       = isset($_GET['authCode']) ? $_GET['authCode'] : '';
$session        = base64_decode($encodedSession);
$result         = '';
$resultXml      = '';

$auth = new Authenticate();
$auth->setAuthCode($authCode);
$auth->setAuthSession($encodedSession);
$auth->auth();

$query = "SELECT * FROM `" . CRMADATBAZIS . "`.`webform_results` WHERE session_id = ?";
Database::getInstance()->query($query, array('s', $session));
$res = Database::getInstance()->fetchNext();

header('Cache-Control: no-store, no-cache, must-revalidate');

if (!empty($res)) {
    $resultXml .= '<' . '?' . 'xml version="1.0"' . '?' . '><WebFormResult>';
    $resultXml .= '?session=' . $encodedSession . '&' . $res['result'];
    $resultXml .= '</WebFormResult>';
    header('Content-Type: text/xml; charset=utf-8');
    header('Content-Disposition: inline; filename=response.xml');

    echo $resultXml;
}
