<?PHP
	#This is the attempt to build functions for ALLSITES which will generate beautiful certificates.
	
	include_once("htgenerateCertificate.inc.php");
	if (!function_exists("fromREQUEST"))
	{
		function fromREQUEST($label)
		{
			#echo "Looking for $label.";
			return (array_key_exists($label,$_REQUEST)?$_REQUEST[$label]:null); 
		}
	}
	##########################################################################################

	$cmd=fromREQUEST("cmd");
	$dsp=fromREQUEST("dsp");
	$msgs="";

	switch ($cmd)
	{
		case "certsend":
			$serial=fromREQUEST("id");
			if (maffcertsend($serial))
			{
				$dsp="certsent";
				die();
			}
			else
				$dsp="notsent";
			break;
		case "makecert":
			$cereal=time();
			$msgs.="<br>Using serial number $cereal.";
			maffmakereqfile(CERTSDIR."/$cereal.req");
			$msgs.="\n<br>Should have file ".CERTSDIR."/$cereal.req";
			$count=maffmakecert($cereal);
			$msgs.="\n<br>After making the certificate $cereal became $count.";
			$dsp="certdone";
			break;
		default:
	}

	echo "<html><head>";
	echo "</head><body>";
	echo $msgs;
	echo "\n<br/>We are doing display $dsp.";
	switch($dsp)
	{
		case "notsent":
			echo "The certificate did not go.  We hope to fix this problem soon.";
			break;
		case "certsent":
			echo "It went ";
			break;
		case "certdone":
			echo "We have successfully created certificate $count (".UDECHEX($count).") and shall soon hereafter start installing it.";
			echo maffautocertsend($count);
/*			echo "<form action=. method=post id=fred>";
			echo "<input type=hidden name=cmd value=certsend>";
			echo "<input type=hidden name=id value=$count>";
			echo "</form>";
			echo "<script type=text/javascript>
			var keygen=document.getElementById(\"fred\");
											   keygen.submit();
											   </script>";
 */
			echo "You should get an alert box telling you that the certificate is installed.";
			#maffcertsend($count);
			echo "Enjoy your new certificate.";
			break;
		case "form":
		default:
	echo "<p>This should give you a form to click on because the display was $dsp.";
	echo maffcertform();
	echo maffcertkeysize();
			echo "<table>";
			foreach (explode(",",REQPARTS) as $name)
				echo "<tr><td align=right>$name:</td><td><input name=$name></td></tr>";
			echo "</table>";
	echo maffcertsubmit();
	echo "</form>";
	}
	die("\n<br>That's it, what will you do?");
	
	#The following commment lines are historical and cannot be trusted.
	#This is a response to the challenge that I couldn't deliver a certificate with only four clicks.
	#I think I did it or could do it with three but this goes a bit longer.
	#index.php (this file) asks for an email address.
	#mail.php sends a URI to that address which invokes getcert.php.
	#getcert.php does the key generation and all that and then sends it to getcert2.php.
	#getcer2.php makes the certificate and installs it.
	#final.php doesn't get used because I can't get the signal that the certificate is installed.
	echo "<html><head>";
	echo "</head><body>";
	echo "<p>To get your own certificate, use a Firefox browser, fill in your email below, and click the submit button.";
	echo "  Firefox must also be your default browser.</p>";
	echo "<form action=mail.php method=post>";
	echo "<input name=email>";
	echo "<br/>";
	echo "<input type=submit>";
	echo "</form>";
	echo "<img src=FastCerts.jpg>";
	echo "</body></html>";
?>