<?php

require_once("common.inc");
session_start();

	if(!isset($openid_identifier)) {
        $error = "Expected an OpenID URL.";
        $app = "login";
  } else {
  	$openid = $openid_identifier;
  	$consumer = getConsumer();
  	$auth_request = $consumer->begin($openid);
  	if (!$auth_request) {
        $error = "Authentication error: ".$openid_identifier." is not a valid OpenID.";
        $app = "login";
  	} else {
  	$sreg_request = Auth_OpenID_SRegRequest::build(array('nickname'),array());	// nick required, no optionals 
  	if ($sreg_request) {
        $auth_request->addExtension($sreg_request);
  	}
  	if ($auth_request->shouldSendRedirect()) {
        $redirect_url = $auth_request->redirectURL(getTrustRoot(),
                                                   getReturnTo());
        if (Auth_OpenID::isFailure($redirect_url)) {
            $error = "Could not redirect to server: " . $redirect_url->message;
            $app = "2";
        } else {
            header("Location: ".$redirect_url);
        }
  	} else {
        // Generate form markup and render it.
        $form_id = 'openid_message';
        $form_html = $auth_request->formMarkup(getTrustRoot(), getReturnTo(),
                                               false, array('id' => $form_id));
        if (Auth_OpenID::isFailure($form_html)) {
            displayError("Could not redirect to server: " . $form_html->message);
        } else {
            $page_contents = array(
               "<html><head><title>",
               "OpenID transaction in progress",
               "</title></head>",
               "<body onload=\"document.getElementById('".$form_id."').submit()\">
								<basefont face=\"'Bitstream Vera Sans',Verdana,Arial,Serif\" color=\"#FFF\" size=\"1\">",
               $form_html,
               "</body></html>");

            print implode("\n", $page_contents);
            exit();
        }
			}
  	}
  }

?>