<?php
class VsignServiceResponse
{
    private $serviceLocation;

    public function __construct()
    {
        $this->serviceLocation = Kit::GetVsignRoot();
    }

    /**
     * Outputs the WSDL
     */
    public function WSDL()
    {
        // We need to buffer the output so that we can send a Content-Length header with the WSDL
        ob_start();
        $wsdl = file_get_contents('lib/service/service.wsdl');
        $wsdl = str_replace('{{XMDS_LOCATION}}', $this->serviceLocation, $wsdl);
        echo $wsdl;

        // Get the contents of the buffer and work out its length
        $buffer = ob_get_contents();
        $length = strlen($buffer);

        // Output the headers
        header('Content-Type: text/xml; charset=ISO-8859-1\r\n');
        header('Content-Length: ' . $length);

        // Flush the buffer
        ob_end_flush();
        exit;
    }

    /**
     * Outputs the XRDS
     */
    public function XRDS()
    {
        header('Content-Type: application/xrds+xml');

        // TODO: Need to strip out the services.php part of serviceLocation - or work out a better way to do it.

        $xrds = file_get_contents('lib/service/services.xrds.xml');
        $xrds = str_replace('{{XRDS_LOCATION}}', $this->serviceLocation, $xrds);
        echo $xrds;

        die();
    }

    /**
     * Outputs an error
     */
    public function ErrorUnauthorized($message = 'Unauthorized')
    {
        header('HTTP/1.1 401 Unauthorized');
        header('Content-Type: text/plain');
        die($message);
    }

    /**
     * Outputs an internal server error
     */
    public function ErrorServerError($message = 'Unknown Error')
    {
        header('HTTP/1.1 500 Internal Server Error');
        header('Content-Type: text/plain');
        die($message);
    }

    /**
     * Success
     * @param <string> $response
     */
    public function Success($response)
    {
        echo $response;
        die();
    }
}
?>
