<?PHP
	//error reporting
	ini_set('display_errors',1); 
	error_reporting(E_ALL);

	$count=time();
	$certs_dir="/home/phadley/websites/certs/1";
	$openssldir="/usr/bin";
	$os="ma";$br="ff";$osbr="maff";
	
	$days=731;
	$CApassword="1273!Main";
	
	$email=$_REQUEST["email"];
	
	include(".htfolder.inc.php");

	/*
#	header("Content-Length: ".strlen($cert));
#	header("Location: $PHP_SELF?command=info");
*/

$CA="$openssldir/openssl ca";
$CRL2PKCS7 = "$openssldir/openssl crl2pkcs7";
$DAYS = "-days $days";
$CAFILE = "$certs_dir/root.pem";
$KEYFILE= "$certs_dir/rootkey.pem";
$hexcount=sprintf("%04X",$count);	#dechex?
$ERRORFILE = "$certs_dir/errors";
$srlfile="$certs_dir/$count.srl";
$cnffile="$certs_dir/$count.cnf";
$crlfile="$certs_dir/crl.pem";
$req_file = "$certs_dir/$count.req"; # certificate request filename
$result_file = "$certs_dir/$count.result"; # certificate filename
$pkcs7_file = "$certs_dir/$count.pkcs";
$dbfile="$certs_dir/$count.txt";
	
file_put_contents($srlfile,$hexcount);
shell_exec("touch $dbfile");
cnf_make($cnffile,$count,$certs_dir,$email);

$CRL = "$certs_dir/crl.pem";
$CACERT = "-cert $CAFILE";
$CAKEY = "-key $CApassword";
$part="makecert";
	
	##echo "<br/>We are about to make the certificate with $osbr.inc.php.";
	$msgs.="<BR>We are at makecert in maff.";
	$req_names = array('commonName', 'emailAddress', 'organizationName',
					   'organizationalUnitName', 'localityName', 'stateOrProvinceName',
					   'countryName');
	$reqp=fopen($req_file,"w");
/*
	$nnames=count($req_names);
	for ($i=0;$i<$nnames;$i++)
	{
		$id=$req_names[$i];
		$value=str_replace("\r\n","",$_REQUEST[$id]);
		fwrite($reqp,sprintf("%s = %s\n",$id,$value));
	}
 */
	fwrite($reqp,sprintf("%s = %s\n","emailAddress",$email));
	fwrite($reqp,sprintf("%s = %s\n","commonName",$email));
	fwrite($reqp,sprintf("SPKAC = %s\n",str_replace("\r\n","",$_REQUEST["SPKAC"])));
	fclose($reqp);
	$req=file_get_contents($req_file);

	/* $cmd="$openssldir/openssl ca -batch -config $cnffile -extensions "
	."certificate_extensions $CACERT "
	."$CAKEY "
	."-verbose -spkac $req_file $DAYS "
	."2>$ERRORFILE"; */

	$cmd="$openssldir/openssl ca -batch -config $cnffile -extensions "
	."certificate_extensions $CACERT "
        ."$CAKEY "
        ."-verbose -spkac -out $req_file $DAYS "
        ."2>$ERRORFILE"; */

	$cert = shell_exec ($cmd);
	header("Content-Type: application/x-x509-user-cert");
	echo $cert;
?>
<!DOCTYPE html>
	<head></head>
	<body>
		<iframe width="1" height="1" style="display: none;" src="data:application/x-x509-user-cert;base64,MII"></iframe>
	</body>
</html>
