<?PHP		#setup.php of VIVOS (Tabelio), maybe authentrus
#	This is used once and discarded
/*echo "Hello, world.";
$allsites="/var/www/sites/allsites";
require_once("$allsites/.htfunctions.inc.php");
$self=$_SERVER["SERVER_NAME"].$_SERVER["PHP_SELF"];

if (!function_exists('dberrors'))
{
function dberrors($res)		#$res is an error object found by pearcheck.
{
	$a=print_r($res->getDebugInfo(),TRUE);
	if ($res->getDebugInfo() != ($res->getUserInfo()))
		$a.="\n".print_r($res->getUserInfo(),TRUE);
	return $a;
}
}

$openssldir="/usr/bin";
$dsn["phptype"]="pgsql";
$dsn["username"]="control";
$dsn["password"]="Rumney";
#$dsn["hostspec"]="localhost";
$dsn["database"]="control";
$dsn["port"]="5432";
if (pearcheck($db =& DB::connect($dsn, $options)))
{
        echo "We are having trouble accessing the databases.";
	echo dberrors($db);
        die ("Please come back a bit later.");
}
$db->setFetchMode(DB_FETCHMODE_ASSOC);

if (pearcheck($res=$db->query("\\i /home/phadley/schema/authentrus.pgsql")))
{
	echo "We couldn't create the new database.";
	echo dberrors($res);
	die ("There isn't muchpoint in going any further.");
}
if (pearcheck($res=$db->query("\\i /home/phadley/schema/authentrus.rootcert.pgsql")))
{
	echo "We couldn't create the default signer.";
	echo dberrors($res);
	die ("There isn't much point in going any further.");
}
$query="insert into officer (officerid,serial_number,common_name,certificate,public_key,status) values (0,".hexdec($_SERVER["SSL_CLIENT_M_SERIAL"]).",'".$_SERVER["SSL_CLIENT_S_DN_CN"]."','".$_SERVER["SSL_CLIENT_CERT"]."','','gd')";
if (pearcheck($res=$db->query($query)))
{
	echo "We couldn't appoint you as an officer.  You'll have to do this yourself.";
	echo dberrors($res);
}

$db->disconnect();
*/

require_once(".htfolder.inc.php");

$biofiles_dir=HARDTOGET."webvivos/bio";
$namefiles_dir=HARDTOGET."names";
$certs_dir=HARDTOGET."webvivos";
if (mkdir($certs_dir,0755))
{
	echo "\n<BR>WE have created the $certs_dir directory.";
}
else
{
	echo "\n<BR>We could not create the $certs_dir directory.";
}
if (mkdir($biofiles_dir,0755))
{
	echo "\n<BR>WE have created the $biofiles_dir directory.";
}
else
{
	echo "\n<BR>We could not create the $biofiles_dir directory.";
}
if (mkdir($namefiles_dir,0755))
{
	echo "\n<BR>WE have created the $namefiles_dir directory.";
}
else
{
	echo "\n<BR>We could not create the $namefiles_dir directory.";
}

if (pearcheck($row=$db->getRow("select certificate,private_key from signer where signerid=0")))
{
	echo dberrors($row);
	die("We could not get the signing certificate from the database.");
}



		$certsdir="$certs_dir/$signerid";
		if (!mkdir($certsdir))
		{
			$errors.=addError("We could not create the new directory for your signing certificate.");
			$display="newsigner";
			loggywoggy("Could not create directory $certsdir for officer $surferid.");
			break;
		}
		if (!mkdir("$certsdir/names"))
		{
			$errors.=addError("We messed up our directory structure.  Please call us so we can fix it right away.");
			loggywoggy("Unable to make the names directory for signer $signerid.");
		}
		if (!mkdir("$certsdir/certs"))
		{
			$errors.=addError("We messed up our directory structure.  Please call us so we can fix it right away.");
			loggywoggy("Unable to make the certs directory for signer $signerid.");
			break;
		}
		$rootfile="$certsdir/certs/root.pem";
		if (file_put_contents($rootfile,$row["certificate"]) == 0)
		{
			$errors.=addError("I'm sorry, but we were unable to put your certificate in its new folder.");
			$display="newsigner";
			loggywoggy("Could not spill signing certificate $signerid to $rootfile.");
			break;
		}
		$rootkeyfile="$certsdir/certs/rootkey.pem";
		if (file_put_contents($rootkeyfile,$row["private_key"],0400) == 0)
		{
			$errors.=addError("I'm sorry, but we were unable to put your file in its new folder.");
			$display="newsigner";
			loggywoggy("Could not spill signing certificate $signerid to $rootfile.");
			break;
		}

if (isset($display))
{
	echo page_top($site_name,$display);
	if (strlen($errors) > 0) echo "<FONT color=red size=+3>$errors</FONT>";
	if (strlen($msgs) > 0) echo "$msgs";
	#if (isset($_POST)) echo "<BR>The variables posted are:<PRE>".print_r($_POST,TRUE)."</PRE>";
	#if (isset($_FILES)) echo "<BR>The files uploaded are:<PRE>".print_r($_FILES,TRUE)."</PRE>";
	echo "<BR>";
switch($display)
{
	case "newsigner":
		echo "<FORM method=post action='https://$self'>";
		echo "\n<TABLE>";
		echo "\n<TR>".formTag("Common Name")."<TD><INPUT name='common_name' id='common_name'></TD></TR>";
		echo "\n<TR>".formTag("Certificate")."<TD><TEXTAREA name='certificate' id='certificate'></TEXTAREA></TD><TD>The actual certificate</TD></TR>";
		echo "\n<TR>".formTag("Private Key")."<TD><TEXTAREA name='private_key' id='private_key'></TEXTAREA></TD></TR>";
		echo "\n<TR>".formTag("Password")."<TD><INPUT name='password' id='password'></TD></TR>";
		echo "\n<TR>".formTag("Valid Period")."<TD><INPUT name='days' id='days'></TD><TD>Number of days until a new certificate expires</TD></TR>";
		echo "\n<TR><TD></TD><TD><INPUT type=submit name=command value='".NEWSIGNER."'></TD></TR>";
		echo "<\n</TABLE></FORM>";
		break;
	default:
		echo "<H2>Welcome ".make_name($surfer)."</H2>";
		echo "What would you like to do, today?";
		echo "<BR>Just click the appropriate menu item above or below.";
		loggywoggy("We did not process the display $display.");
}
}
else	##No requested display
{
	echo page_top($site_name);
	echo "<H2>$site_name for $name</H2>";
	#if (isset($_POST)) echo "<BR>The variables posted are:<PRE>".print_r($_POST,TRUE)."</PRE>";
	#if (isset($_FILES)) echo "<BR>The files uploaded are:<PRE>".print_r($_FILES,TRUE)."</PRE>";
	$signers=$db->query("select * from signer where officerid=$surferid");
	if (($n=$signers->numRows()) > 0)
	{
		echo "\n<BR>You have ".if_s($n>1,"these certificates","this certificate").":<UL>";
		while ($row=$signers->fetchRow())
		{
			$signerid=$row["signerid"];
			echo "\n<LI><A href=$self&display=viewcert&id=$signerid>$signerid-".$row["common_name"]."</A></LI>";
		}
		echo "</UL>";
	}
	unset($signers);
	$subjects=$db->query("select * from subject where officerid=$surferid");
	if (($n=nrows($subjects)) > 0)
	{
		if ($n>TOOMANYTOLIST)
		{
			echo "<BR>You have enrolled $n subjects so far.";
		}
		else
		{
			echo "\n<BR>You have enrolled ".if_s($n>1,"these subjects","this subject").":<UL>";
			while ($row=$certs->fetchRow())
			{
				$certid=$row["certificateid"];
				echo "\n<LI><A href=$self?display=viewcert&id=$certid>$certid-".make_name($row)."</A></LI>";
			}
			echo "<\n</UL>";
		}
	}
	$certs=$db->query("select * from certificate where officerid=$surferid");
	if (($n=nrows($certs)) > 0)
	{
		if ($n>TOOMANYTOLIST)
		{
			echo "<BR>You have authorized $n certificates so far.";
		}
		else
		{
			echo "\n<BR>You have authorized ".if_s($n>1,"these certificates","this certificate").":<UL>";
			while ($row=$certs->fetchRow())
			{
				$certid=$row["certificateid"];
				echo "\n<LI><A href=$self?display=viewcert&id=$certid>$certid-".make_name($row)."</A></LI>";
			}
			echo "<\n</UL>";
		}
	}
	echo "";
}
#echo phpinfo();
?>