<?php
	require_once('lib/nusoap.php');
	require('lib/functions.php');
	

	$server = new nusoap_server();
	
	$server->register('GetReservation');
	
	function http_request($url,$timeout){
		//Realiza una petición HTTP GET y devuelve lo obtenido.
		$ch=curl_init();
		curl_setopt($ch,CURLOPT_URL,$url);
		curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
		curl_setopt($ch,CURLOPT_TIMEOUT,$timeout);
		$result=curl_exec($ch);
		curl_close($ch);
		return $result;
	}

	$HTTP_RAW_POST_DATA = isset($HTTP_RAW_POST_DATA) ? $HTTP_RAW_POST_DATA : '';
	$server->service($HTTP_RAW_POST_DATA);
	
	//Guardamos petición en log
	//$path = 'log/'.date("Y").'/'.date("m").'/';
	//if(!file_exists($path)) { mkdir($path, 0777, 1);  }
	//$file_name = 'RQ'.file_name();
	//file_put_contents($path.$file_name, $HTTP_RAW_POST_DATA);
	
	function file_name(){
		//Genera un nombre de fichero a partir de la hora actual en microsegundos
		$file = microtime();
		$file = str_replace(' ','', $file);
		$file = str_replace('.','', $file); 
		$file .= '.xml';
		return $file;
	}

	function GetReservation($params){
		$url_web_ghot = 'http://10.11.200.101/cgi-vel/GHOTv2/booking-notification-tradyso.pro';
		$timeout = 10;
		
		$xml = $params;
		//$xml = preg_replace_callback("/(&#[0-9a-zA-Z]+;)/", function($m) { return ''; }, $xml);
		//$xml = preg_replace_callback("/[^0-9A-Za-z# &\"\+\\-\.\/:;<=>?@_]/", create_function('$m','return "";'), $xml);
		$xml = preg_replace("/[^0-9A-Za-z# &\"\+\\-\.\/:;<=>?@_]/", "", $xml);

		
		$xml = html_entity_decode($xml, ENT_COMPAT, "UTF-8");
		$xml = elimina_acentos($xml);

		//Guardamos petición en log
		$path = 'log/'.date("Y").'/'.date("m").'/';
		if(!file_exists($path)) { mkdir($path, 0777, 1);  }
		$file_name = 'RQ2'.file_name();
		file_put_contents($path.$file_name, $xml);

		$xml = simplexml_load_string($xml);

		
		$token = $xml['TransactionIdentifier'];
		$timestamp = date("Y-m-d").'T'.date("H:i:s");
		$sirius_id = $xml->HotelReservations->HotelReservation->ResGlobalInfo->HotelReservationIDs->HotelReservationID['ResID_Value'];
		$globales_id = 'GLOB'.$sirius_id;

		if($xml->HotelReservations->HotelReservation->TPA_Extensions->Action['Type'] == 'A'){

			$request = parse_ghot($xml->asXML());

			$bError = true;
			$file_name = file_name();
		
			//Guardamos fichero
			if(file_put_contents('../'.$file_name, $request)){
			
				$url = $url_web_ghot.'?url='.$file_name;
			
				//Avisamos a GHOT
				$response = http_request($url, $timeout);
				if($response=='CONFIRMADA'){ $bError = false; }
			
				//Borramos el fichero
				unlink('../'.$file_name);
			}
		}




		$response = '<?xml version="1.0" encoding="UTF-8"?><OTA_HotelResNotifRS xmlns="http://www.opentravel.org/OTA/2003/05" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.opentravel.org/OTA/2003/05 http://xmlgatewaytest.tradyso.com/xmlgateway/xsd/FS_OTA_HotelResNotifRQ.xsd" EchoToken="'.$token.'" TimeStamp="'.$timestamp.'+01:00" Target="Production" Version="1.0"><Success/><HotelReservations><HotelReservation><ResGlobalInfo><HotelReservationIDs><HotelReservationID ResID_Value="'.$sirius_id.'" ResID_Source="SIRIUS"/><HotelReservationID ResID_Value="'.$globales_id.'" ResID_Source="HOTELES GLOBALES"/></HotelReservationIDs></ResGlobalInfo></HotelReservation></HotelReservations></OTA_HotelResNotifRS>';

		//Guardamos petición en log
		//$path = 'log/'.date("Y").'/'.date("m").'/';
		//if(!file_exists($path)) { mkdir($path, 0777, 1);  }
		//$file_name = 'RS'.file_name();
		//file_put_contents($path.$file_name, $response."\nbERROR: ".$bError);

		return $response;
	}

	function elimina_acentos($cadena){
		$tofind = "ÀÁÂÃÄÅàáâãäåÒÓÔÕÖØòóôõöøÈÉÊËèéêëÇçÌÍÎÏìíîïÙÚÛÜùúûüÿÑñªº";
		$replac = "AAAAAAaaaaaaOOOOOOooooooEEEEeeeeCcIIIIiiiiUUUUuuuuyNnao";
		return(strtr($cadena,$tofind,$replac));
	}

?>