<?php
date_default_timezone_set('Europe/Madrid');
/////////////////////////CONEX/////////////////////////////////////////////////
include("../../../assets/conn/conexion.php"); 
include("funcSignLogalty.php");
$bd_conn = new connexion($host_avanws,$user_avanws,$pass_avanws,$name_avanws,$port_avanws);
$bd_conn->query("SET NAMES 'utf8'");
/////////////////////////VARIABLES/////////////////////////////////////////////
$cli_user = $_SERVER["HTTP_CLIENT_ID"];
$fecha = date("YmdHis");
$cod_firma = ! empty($uri[4]) ? $uri[4] : '';
$error = 0;
/////////////////////////ACCIONES//////////////////////////////////////////////
$host = 'https://www.logalty.es/lgt/lgtbus/public/UpdateService?wsdl'; 
chdir('system/controllers/signs/');
/////////////////////////CONSTRUCCION XML//////////////////////////////////
include("xmlCancelSignLogalty.php");
if ( $id_doc !== ''  ) {
    /////////////////////////CERTIFICADO DEL XML///////////////////////////////
    shell_exec( 'xmlsec1  --sign --output signed.xml --pkcs12 ./certificado/certificado.p12 --pwd logalty request_sign.xml');
    //se elimina primera línea del XML que contiene <?xml version="1.0" encoding="utf-8"?\>
    shell_exec( 'sed \'1d\' signed.xml > signed_send.xml');
    shell_exec( 'rm signed.xml');
    //se añade cabeceras SOAP
    shell_exec( 'echo \'<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"> <soapenv:Body>\' > SOAP.xml ');
    shell_exec( 'cat SOAP.xml signed_send.xml > signed_SOAP.xml');
    shell_exec( 'rm signed_send.xml');
    shell_exec( 'rm SOAP.xml');
    shell_exec( 'sed -e \'$a </soapenv:Body> </soapenv:Envelope>\' signed_SOAP.xml > signed_SOAP_send.xml');
    shell_exec( 'rm signed_SOAP.xml');
    //se obtiene el XML firmado
    $input_data = file_get_contents("signed_SOAP_send.xml");
    //HAGO EL POST DE LA FIRMA=================================================
    $request = 'POST';
    $curl = curl_init();
    curl_setopt($curl, CURLOPT_URL ,$host);
    curl_setopt($curl, CURLOPT_HEADER, 0);
    curl_setopt($curl, CURLOPT_HTTPHEADER,array('User-Agent: curl/7.15.5','Accept: */*','Content-Type: text/xml; charset=UTF-8','Content-Length: '.strlen($input_data)));
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($curl, CURLOPT_CONNECTTIMEOUT,5);
    curl_setopt($curl, CURLOPT_TIMEOUT,30);
    curl_setopt($curl, CURLOPT_CUSTOMREQUEST,$request);
    curl_setopt($curl, CURLOPT_POSTFIELDS, $input_data);
    $result = curl_exec($curl);
    //INTERPRETO LA RESPUESTA=======================================================
    $doc = new DOMDocument();
    libxml_use_internal_errors(true);
    $doc->loadHTML($result);
    libxml_clear_errors();
    $xml = $doc->saveXML($doc->documentElement);
    $xml = simplexml_load_string($xml);
    //
    $estado_logalty = '12';
    $resultado_logalty = '99932';
    $estado_p0 = '3';
    $guid = $guid_firma;
    $result_xml = $result;
    updateEstadosFirma($bd_conn, $estado_logalty, $resultado_logalty, $estado_p0, $guid, $result_xml );
    //print_r($xml);
    $data[] = array ( 
        "status" => "OK",
        "message" => 'completado con éxito',
        "respuesta" => $result
    );
    header("Content-type: application/json; charset=utf-8");
    echo json_encode($data); 
}else{
    $data[] = array ( 
        "status" => "NOT OK",
        "message" => 'error.'
    );
    header("Content-type: application/json; charset=utf-8");
    echo json_encode($data);
}
?>