<?php

include("./lib/xmlrpc.inc");

$GLOBALS['xmlrpc_internalencoding']='UTF-8';

//FIXME: change email and password
$uni_url="https://email:password@ws.universign.eu/sign/rpc";


//Read the document and delete the temporary file
$doc_content=file_get_contents("default-contract.pdf");

//create the request
$c = new xmlrpc_client($uni_url);

$options = array(
	// position of the signature field
	"signatureField" => new xmlrpcval(array(
		"page" => new xmlrpcval(1, "int"),
		"x" => new xmlrpcval(50, "int"),
		"y" => new xmlrpcval(10, "int")
	), "struct")
);

$f = new xmlrpcmsg('signer.signWithOptions', array(
	new xmlrpcval($doc_content, "base64"),
	new xmlrpcval($options, "struct")));

//SSL verification (should be enabled in production)
$c->setSSLVerifyHost(0);
$c->setSSLVerifyPeer(0);

//Debug flag
$c->setDebug(0);


//Send request an analyse response
$r =&$c->send($f);
if($r->faultCode()) {
	//error
	print "An error occurred: ";
	print "Code: " . $r->faultCode()
		. " Reason: '" . $r->faultString();
} else {
	$doc =  $r->value()->scalarVal();
	header("Pragma: no-cache");
	header("Content-Type: application/pdf");
	header("Content-Disposition: attachment; filename=contract.pdf");
	header("Content-Length: " . strlen($doc));
	ob_clean();
	flush();
	print $doc;
}
?>
