<?PHP
$stage=1;	# Using Authentify's test server.

/**
 * The user-facing portion of the PHP OpenID Server.
 */
require_once(".htfolder.inc.php");

loggywoggy("New run\n\n\n\n\n\n\n###########################################################################");
loggywoggy("http".((isset($_SERVER["HTTPS"]) && ($_SERVER["HTTPS"] == "on"))?("s"):(""))
	."://".$_SERVER["HTTP_HOST"].$_SERVER["REQUEST_URI"]." from ".$_SERVER["REMOTE_ADDR"]);
#if (isset($_POST)) loggywoggy("The POST data are:\n".print_r($_POST,TRUE));
#if (isset($_GET)) loggywoggy("The GET data are:\n".print_r($_GET,TRUE));


session_start();
#loggywoggy("AAAA");


require_once "config.php";
require_once "common.php";
#loggywoggy("BBBB");


// Create a page template.
$template =& new Template();
#loggywoggy("CCCC");


// First, get the request data.
list($method, $request) = Server_getRequest();
loggywoggy("DDDD-the method is ".print_r($method,TRUE));
loggywoggy("and the request is ".print_r($request,TRUE));
loggywoggy("The cookies are: ".print_r($_COOKIE,TRUE));
loggywoggy("The session variables are: ".print_r($_SESSION,TRUE));


// Initialize backends.
$auth =& Server_getAuthBackend();
#loggywoggy("EEEE-auth is ".print_r($auth,TRUE));

$storage =& Server_getStorageBackend();
#loggywoggy("FFFF-storage has become ".print_r($storage,TRUE));

if ($auth === null) {
    $template->addError("Could not connect to authentication server.");
	loggywoggy("Could not connect to authentication server.");
}

if ($storage === null) {
    $template->addError("Could not connect to OpenID storage server.");
    loggywoggy("Could not connect to OpenID storage server.");
}
loggywoggy("GGGG-all initialized");


if (isset($_SERVER['PATH_INFO']) && $_SERVER['PATH_INFO'] == '/serve') 
{
#	loggywoggy("HHHH-serve");

	require_once "render.php";
	render_serve($method, $request, $template);
	loggywoggy("We rendered the serve page and are leaving.");
	exit(0);
	// If it's a request for an identity URL, render that.
} 
else if (array_key_exists('user', $request) && $request['user']) 
{
#	loggywoggy("IIII-The user is ".$request["user"]);

    require_once "render.php";
    render_identityPage($method, $request, $template);
	loggywoggy("We have rendered the identity page for ".$request['user']." and are leaving.");
    exit(0);
// If it's a request for a user's XRDS, render that.
} 
else if (array_key_exists('xrds', $request) && $request['xrds']) 
{
	loggywoggy("JJJJ-xrds");

    require_once "render.php";
    render_XRDS($method, $request, $template);
	loggywoggy("We have rendered the XRDS page and are leaving.");
    exit(0);
}
#loggywoggy("KKKK");

// If any messages are pending, get them and display them.
$messages = Server_getMessages();
#loggywoggy("LLLL-the messages are ".print_r($messages,TRUE));

foreach ($messages as $m) {
    $template->addMessage($m);
}

Server_clearMessages();

if ($request === null)
{
	// Error; $method not supported.
	loggywoggy("Request method $method not supported.");
	$template->addError("Request method $method not supported.");
	$template->display();
}
else
{
	// Dispatch request to appropriate handler.
	$handler = Server_getHandler($request);
#loggywoggy("NNNN-the handler is ".print_r($handler,TRUE));

	if ($handler !== null)
	{
        	list($filename, $handler_function) = $handler;
		loggywoggy("We shall require file $filename and then apply the handler function ".print_r($handler_function,TRUE));
        	require_once $filename;
        	call_user_func_array($handler_function,array($method, $request, $template));
	}
	else
	{
		loggywoggy("We have reached the default state of displaying the main template.");
        	$template->display('main.tpl');
	}
}

?>
