<?PHP
	#This is the attempt to build functions for ALLSITES which will generate beautiful certificates.
	
	include_once("htwiie.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 (osbrcertsend($serial))
			{
				$dsp="certsent";
				die();
			}
			else
				$dsp="notsent";
			break;
		case "makecert":
			$cereal=time();
			$msgs.="<br>Using serial number $cereal.";
			osbrmakereqfile(CERTSDIR."/$cereal.req");
			$msgs.="\n<br>Should have file ".CERTSDIR."/$cereal.req";
			$count=osbrmakecert($cereal);
			$msgs.="\n<br>After making the certificate $cereal became $count.";
			$dsp="certdone";
			break;
		default:
	}
	
	echo "<html><head>";
	switch ($dsp)
	{
		case "form":
		default:
			echo osbrcertheader();
	}
	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 osbrautocertsend($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.";
			#osbrcertsend($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 osbrcertform("wiie.php");
			echo osbrcertkeysize();
			echo "<table>";
			foreach (explode(",",REQPARTS) as $name)
			echo "<tr><td align=right>$name:</td><td><input name=$name></td></tr>";
			echo "</table>";
			echo osbrcertsubmit();
			echo "</form>";
	}
	die("\n<br>That's it, what will you do?");
	echo "</body></html>";
	?>