<?php
// FMPro XML-RPC Server
// (c) 2001, Bill Humphries

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

// Implement FMPro.allRecords

$allrecords_sig = array(array($xmlrpcString, $xmlrpcString));

$allrecords_doc = "Returns all records in the FMPro database named in the first parameter using the dso_xml format.";

function allrecords($m)
{
	global $xmlrpcerruser;
	$err = "";
	
	// decode parameters
	$dbName = xmlrpc_decode($m->getParam(0));
	
	$url = "http://localhost:471/FMPro?-db=".$dbName."&-format=-dso_xml&-find";
	//$url = "http://localhost:471/FMPro?-db=phonelist.fp5&-format=-dso_xml&-find";
	
	// now try to read from it
	// prepend @ to function to suppress errors to stdout
	$buffer = "";
	$fp = fopen($url,"r");
	
	xmlrpc_debugmsg("Got $fp from trying to open $url.");
	
	// read resposne
	if ($fp)
	{
		while(!feof($fp))
		{
			$next = fread($fp,4096);
			xmlrpc_debugmsg("Got $next from fread on $fp.");			
			$buffer = $buffer.$next;
		}
		fclose($fp);
	}
	else
	{
		$err = "Unable to connect or read from $url.";
	}
	
	// Return response or error.
	if ($err)
	{
		return new xmlrpcresp(0, $xmlrpcerruser, $err);
	}
	else
	{
		xmlrpc_debugmsg("Got from FMPro Server: $buffer");
		$base64String = base64_encode($buffer);
		return new xmlrpcresp(new xmlrpcval($base64String,$xmlrpcString));
	}
}

// instantiate server

$s = new xmlrpc_server(array("FMPro.allRecords" => 
				array(
					"function" => "allrecords",
					"signature" => $allrecords_sig,
					"docstring" => $allrecords_doc))
						);
?>