<?php

class GMinhaBiblioteca
{
    private $url;
    private $apiKey;

    public function __construct($url, $apiKey)
    {
        $this->url = $url;
        $this->apiKey = $apiKey;
    }

    public function authenticate($firstName, $lastName, $email, $isbn=NULL)
    {
        $firstName = GUtil::unaccent($firstName);
        $lastName = GUtil::unaccent($lastName);
        $url = $this->url . '/AuthenticatedUrl';
        $curl = curl_init($url);

        $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>{$lastName}</LastName>
        <Email>{$email}</Email>
        <CourseId xsi:nil=\"true\"/>
        <Tag xsi:nil=\"true\"/>";

        
        if( $isbn )
        {
            $data .="<Isbn>{$isbn}</Isbn>";
        }
        else
        {
            $data .= "<Isbn xsi:nil=\"true\"/>";
        }
        
        $data .= "</CreateAuthenticatedUrlRequest>";

        $dataSize = strlen($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: {$dataSize}",
            "Expect: 100-continue",
            "Accept-Encoding: gzip, deflate",
            "Connection: Keep-Alive",
            "X-DigitalLibraryIntegration-API-Key: {$this->apiKey}")
        );

        curl_setopt($curl, CURLOPT_POSTFIELDS, $data);

        $response = curl_exec($curl);
        curl_close($curl);

        $xml = new SimpleXMLElement($response);

       return $xml;
    }

    public function createUser($firstName, $lastName, $username)
    {
        $url = $this->url . '/CreatePreRegisterUser';
        $curl = curl_init($url);

        $data = '<?' . "xml version=\"1.0\" encoding=\"utf-8\"" . '?>' ."
        <CreatePreRegisterUserRequest
        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>{$lastName}</LastName>
        <UserName>{$username}</UserName>
        </CreatePreRegisterUserRequest>
        ";

        $dataSize = strlen($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: {$dataSize}",
            "Expect: 100-continue",
            "Accept-Encoding: gzip, deflate",
            "Connection: Keep-Alive",
            "X-DigitalLibraryIntegration-API-Key: {$this->apiKey}")
        );

        curl_setopt($curl, CURLOPT_POSTFIELDS, $data);

        $response = curl_exec($curl);
        curl_close($curl);

        $xml = new SimpleXMLElement($response);

        return $xml;
    }

}

?>