<?PHP
	#This is the attempt to build functions for ALLSITES which will generate beautiful certificates.
	if (!defined("ALLSITES")) define("ALLSITES","/var/www/sites/allsites");
	include_once (ALLSITES."/.htclient.inc.php");
	
	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); 
		}
	}
function osbrfile($osbr)
	{
		return file_exists("ht".$osbr.".inc.php");
	}

if (!function_exists('osbrChoice'))
{
	function osbrChoice($osd,$brd,$isitthere,$href=".",$other="")
	{
		global $browsers,$oses;
		$a="<table>";
		foreach($browsers as $br=>$browser)
		{
			$a.="<tr>";
			foreach($oses as $os=>$opsys)
			{
				if ($isitthere($os.$br))
					$a.="<td><a href=$href?osbr=$os"."$br".$other.">$browser on $opsys</a></td>";
				else
					$a.="<td>$browser on $opsys</td>";
			}
			$a.="</tr>";
		}
		$a.="</table>";
		return $a;
	}
}
	
	##############################################################################################
	
	if (!function_exists("UDECHEX"))
	{
		function UDECHEX($in)
		{
			return strtoupper(dechex($in));
		}
	}
	
	##############################################################################################
	function certprogress($input,$errorfile,$line="",$file="")
	{
		$msg=date("d M Y-H:i:s-");
		if (strlen($file)>0) $msg.="$file-";
		if (strlen($line)>0) $msg.="$line-";
		$msg.=print_r($input,true);
		error_log("\n".$msg,3,$errorfile);
		return;
	}

	##########################################################################################

	list($os,$br,$opsys,$browser)=osbr();
	$osbr=$os.$br;
	echo "\n<br/>os=$os, br=$br, op sys=$opsys, browser=$browser, osbr=$osbr";
	include_once("ht$osbr.inc.php");
	$cmd=fromREQUEST("cmd");
	$dsp=fromREQUEST("dsp");
	$msgs="";
	
	switch ($cmd)
	{
		case "osbr":
			$dsp="form";
			break;
		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>";
	echo "<title>Making a Certificate for ".$browsers[$br]." running on ".$oses[$os]."</title>";
	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,$osbr);
			echo "You should get an alert box telling you that the certificate is installed.";
			echo "Enjoy your new certificate.";
			break;
		case "form":
		default:
			echo "We have determined that you want a certificate for your ".$browsers[$br]." browser running under your ".$oses[$os]." operating system. ";
			echo "If you would like to alter this, you may choose another browser/operating system pair from the highlighted options in the table below. ";
			echo "If your system is not available, you might attempt to use a similar pair.  The browser is usually more important than the operating system. ";
			echo osbrChoice($os,$br,"osbrfile",$_SERVER["PHP_SELF"],"&cmd=osbr");
			echo "<p>This should give you a form to click on because the display was $dsp.";
			echo osbrcertform($_SERVER["PHP_SELF"]);
			echo osbrcertkeysize();
			echo "<input type=hidden name=osbr value=$osbr>";
			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>";
	}

	echo "<img src=FastCerts.jpg>";
	echo "</body></html>";
	?>