<?php
$service_url = 'https://integracao.dli.minhabiblioteca.com.br/DigitalLibraryIntegrationService/AuthenticatedUrl';
$curl = curl_init($service_url);
$nome = explode(' ', $_POST['nome'], 2);
$firstName= $nome[0];
$lastName = $nome[1];
$email = $_POST['email'];
if($lastName !=""){
	$curl_post_data = "<?xml version=\"1.0\" encoding=\"utf-8\"?>
	<CreateAuthenticatedUrlRequest
	xmlns=\"http://dli.zbra.com.br\"
	xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\"
	xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\">
	<FirstName>$firstName</FirstName>
	<LastName>$nome[1]</LastName>
	<Email>$email</Email>
	<CourseId xsi:nil=\"true\"/>
	<Tag xsi:nil=\"true\"/>
	<Isbn xsi:nil=\"true\"/>
	</CreateAuthenticatedUrlRequest>
	";
	$content_size = strlen($curl_post_data);
	curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
	curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "POST");
	curl_setopt($curl, CURLOPT_HTTPHEADER, array(
	"Content-Type: application/xml; charset=utf-8",
	"Host: integracao.dli.minhabiblioteca.com.br",
	"Content-Length: $content_size",
	"Expect: 100-continue",
	"Accept-Encoding: gzip, deflate",
	"Connection: Keep-Alive",
	"X-DigitalLibraryIntegration-API-Key: f815857c-90d9-4980-a035-aa71d9662fa5")
	);
	curl_setopt($curl, CURLOPT_POSTFIELDS, $curl_post_data);
	$curl_response = curl_exec($curl);
	//echo $curl_response; 
	$search = 'not have access';
    //echo preg_match("/{$search}/i", $curl_response)	;
	if(preg_match("/{$search}/i", $curl_response)===0){
	if ($curl_response === false) {
	echo curl_error($curl);
	curl_close($curl);
	die();
	}
	curl_close($curl);
	$xml = new SimpleXMLElement($curl_response);
	if ($xml->Success != 'true') {
	echo htmlspecialchars($result);
	die();
	}
	// user code below to redirect browser to the authenticated URL
	echo header('Location: ' . $xml->AuthenticatedUrl);
	echo $xml->AuthenticatedUrl;
	die();
}else{
	echo "Você esta sem permissão para acessar a biblioteca digital. Entre em contato com a Diretoria de Tecnologia da Informação (TI) e informe o seguinte endereço de e-mail: ".$email;
}
}else{
	echo "Parametros não enviados para autenticação. Entre em contato com a Diretoria de Tecnologia da Informação (TI).";
	echo  "Nome ".$firstName ."Sobrenome ".$lastName ."Email ".$email;
}
?>
