<?php
	require_once(__DIR__ . "/../config/connect.php");
	require_once(__DIR__ . "/../global_functions.php");

	define('LIZENZ_URL_ZENTRALSYSTEM','https://portal.sitpos.at/lizenzen_4/synchronize/export_xml.php');
	//define('LIZENZ_URL_ZENTRALSYSTEM','https://localhost/lizenzen_4/synchronize/export_xml.php');
	define('LIZENZ_TOKEN','myVktr9IkfP0peRLDQJs');
	//$hostid="123456789012";		
		#$hostid="F894C2167268";	
	function transfer_lizenz_get_data($hostid)
	{
			#file_put_contents("kpcheckit.txt",basename( __FILE__) ." ". __LINE__ ." ".date('Y-m-d H:i:s')."hhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh\n", FILE_APPEND );
		$post_fields = array(
            'host_id' => $hostid,
            'token' => LIZENZ_TOKEN,
			'funktion' => 'get_license',
        );
		
		$post_fields_string = http_build_query($post_fields);
		
		$ch = curl_init();
		$url = LIZENZ_URL_ZENTRALSYSTEM;
		//$url=$url."?token=".LIZENZ_TOKEN."&host_id=".$hostid;
		//echo $url;
		$token=LIZENZ_TOKEN;
		
		curl_setopt($ch, CURLOPT_URL, $url);
		
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
		curl_setopt($ch, CURLOPT_HEADER, FALSE);

		curl_setopt($ch, CURLOPT_POST, TRUE);
		curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
		curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
		
		curl_setopt($ch, CURLOPT_POSTFIELDS, $post_fields_string);
		
		$errorMsg="";

		$response = curl_exec($ch);
		if ($response === false) $errorMsg.= 'Curl-Fehler: ' . curl_error($ch);
		curl_close($ch);

		//var_dump($response);
		libxml_use_internal_errors(true);
		$xml = simplexml_load_string($response, "SimpleXMLElement", LIBXML_NOCDATA);
		
		if ($xml === false){
			$errorMsg.= "Failed loading XML\n";
				# so wie es ausschaut fuktioniert das eh nicht
			foreach(libxml_get_errors() as $error) {
				$errorMsg.= "\t". $error->message;
			}
			#var_dump($response);
			#exit;
			$array= array('Error', $errorMsg, "");
		}
		else if($xml !== false && $xml->error != ''){
			if(strpos($xml->error, strtolower('hostid')) !== false && strpos($xml->error, strtolower('nicht gefunden')) > 0 ){
				$zulu=$xml->error;
				file_put_contents("kpcheckit.txt",basename( __FILE__) ." ". __LINE__ ." ".date('Y-m-d H:i:s')."\t".$zulu."\n", FILE_APPEND );
				$array = array('hostIdError', $zulu, ""); 
			}	
		}
		else{
			$json = json_encode($xml);
			$array = array('xmlSuccess', json_decode($json, TRUE));
		}
		return($array);
	}
	
	function info_von_kasse_fuer_server_auslesen()
	{

		$XMLDoc = new SimpleXMLElement("<?xml version='1.0' standalone='yes'?><envelope></envelope>");
		$XMLDoc->addChild("AUSRUCK_KOPFZEILE_1", urlencode(AUSRUCK_KOPFZEILE_1));
		$XMLDoc->addChild("AUSRUCK_KOPFZEILE_2", urlencode(AUSRUCK_KOPFZEILE_2));
		$XMLDoc->addChild("AUSRUCK_KOPFZEILE_3", urlencode(AUSRUCK_KOPFZEILE_3));
		$XMLDoc->addChild("AUSRUCK_KOPFZEILE_4", urlencode(AUSRUCK_KOPFZEILE_4));
		$XMLDoc->addChild("AUSRUCK_KOPFZEILE_5", urlencode(AUSRUCK_KOPFZEILE_5));
		$XMLDoc->addChild("AUSRUCK_KOPFZEILE_6", urlencode(AUSRUCK_KOPFZEILE_6));
		$XMLDoc->addChild("AUSRUCK_KOPFZEILE_7", urlencode(AUSRUCK_KOPFZEILE_7));
		$XMLDoc->addChild("AUSRUCK_KOPFZEILE_8", urlencode(AUSRUCK_KOPFZEILE_8));
		$XMLDoc->addChild("AUSRUCK_KOPFZEILE_9", urlencode(AUSRUCK_KOPFZEILE_9));
		$XMLDoc->addChild("AUSRUCK_KOPFZEILE_IBAN", urlencode(AUSRUCK_KOPFZEILE_IBAN));
		
		if(defined('KASSA_ID'))$kassaId=KASSA_ID;
		else $kassaId='undefined';
			$XMLDoc->addChild("KASSA_ID", urlencode($kassaId));
		
		if(defined('AES_CECKSUM'))$aesChecksum=AES_CECKSUM;
		else $aesChecksum='undefined';
			$XMLDoc->addChild("AES_CECKSUM", urlencode($aesChecksum));
		
		if(defined('AES_KEY'))$aesKey=AES_KEY;
		else $aesKey='undefined';
			$XMLDoc->addChild("AES_KEY", urlencode($aesKey));
		
		if(defined('ZDA_ID'))$zdaId=ZDA_ID;
		else $zdaId='undefined';		
			$XMLDoc->addChild("ZDA_ID", urlencode($zdaId));
		
		if(defined('ZERTIFIKATS_NR'))$zertifikatsNr=ZERTIFIKATS_NR;
		else $zertifikatsNr='undefined';		
			$XMLDoc->addChild("ZERTIFIKATS_NR", urlencode($zertifikatsNr));
		
		if(defined('SITPOS_VERSION'))$sitposVersion=SITPOS_VERSION;
		else $sitposVersion='undefined';		
			$XMLDoc->addChild("SITPOS_VERSION", urlencode($sitposVersion));
		
		if(defined('APPLICATION_VERSION'))$appVersion=APPLICATION_VERSION;
		else $appVersion='undefined';		
			$XMLDoc->addChild("APPLICATION_VERSION", urlencode($appVersion));
		
		if(defined('GASTRO_VERSION'))$gastroVersion=GASTRO_VERSION;
		else $gastroVersion='undefined';
			$XMLDoc->addChild("GASTRO_VERSION", urlencode($gastroVersion));
		
		$macAdr=getMacAddresses();
		$host_id="";
		if(is_array($macAdr)) $hostid=$macAdr[0];
		$XMLDoc->addChild("hostid", $hostid);
		$xml=$XMLDoc->asXML();
		return($xml);
	}
	
	function xml_zu_lizenzserver_transferieren($xml, $hostid)
	{

		$ch = curl_init();
		$url = LIZENZ_URL_ZENTRALSYSTEM;
		
		$post_fields = array(
            'host_id' => $hostid,
            'token' => LIZENZ_TOKEN,
			'funktion' => 'upload_data',
			'XML' => $xml 
        );
		$post_fields_string = http_build_query($post_fields);
		
		curl_setopt($ch, CURLOPT_URL, $url);
		
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
		curl_setopt($ch, CURLOPT_HEADER, FALSE);

		curl_setopt($ch, CURLOPT_POST, TRUE);
		curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
		curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
		
		curl_setopt($ch, CURLOPT_POSTFIELDS, $post_fields);

		$response = curl_exec($ch);
		if ($response === false) 
		{
			echo 'Curl-Fehler: ' . curl_error($ch);
			return (false);
		}
		curl_close($ch);

		$xml = simplexml_load_string($response, "SimpleXMLElement", LIBXML_NOCDATA);
		
		#$xml=false;
		
		if ($xml === false) 
		{
			$falseMsg="";

			$falseMsg.="Failed loading XML\n";
			foreach(libxml_get_errors() as $error) {
				$falseMsg.="\t". $error->message;
			}
				#var_dump($response);
				#exit;				
			return array("error"=>"true", "errorcode"=>$falseMsg);
		}
		else{
			$json = json_encode($xml);
			$array = json_decode($json,TRUE);
					
			return($array);
		}
	}

	

	?>