## OpenCA - CA Command
## (c) 1998-2001 by Massimiliano Pala and OpenCA Group
##
##   File Name: viewCRL
##       Brief: Display CRL
## Description: Display requested CRL
##  Parameters: dataType, key

sub cmdViewCRL {

## Original command viewCRL (rvr@ulpgc.es)
## Modified for new modular version of OpenCA by Massimiliano Pala
## (madwolf@openca.org)

## Variable Definition
my ( @vals, $rev, $page );

## Get Required Parameters
my $doc = getRequired( "viewCrlSheet" );

## Get passed parameters
my $dataType = $query->param('dataType');
my $key = $query->param('key');
my $server   = getRequired ('CgiServerType');

configError( gettext ("Missing required parametes (dataType/key)!") )
		if( not ( $dataType and $key ) );
    
## Get the base Page ( got in $page variable )
if( not $page = $tools->getFile( $doc ) ) {
	configError( i18nGettext("File not Found : __FILE__", "__FILE__", $doc) );
}

## Get the CRL from the DB.
my $crl = $db->getItem( DATATYPE=>$dataType, KEY=>$key);

generalError( i18nGettext ("Cannot retrieve __KEY__ object from __DATATYPE__ dB!",
                           "__KEY__", $key,
                           "__DATATYPE__", $dataType) )
	if( not $crl );

my $parsed = $crl->getParsed();
my $issuer = $parsed->{ISSUER};

$issuer =~ s/^\///;
$issuer =~ s/\//<BR>\n/g;

my $buttonTable = "";
if ( $server =~ /^(LDAP)$/i ) {
        $buttonTable .= "<form method=\"POST\">\n";
        $buttonTable .= "<input type=hidden name=\"cmd\" value=\"\">";
        $buttonTable .= "<input type=hidden name=\"key\" value=\"$key\">";
        $buttonTable .= "<input type=hidden name=\"dn\" value=\"".$issuer."\">";
        $buttonTable .= "<input type=hidden name=\"new_dn\" value=\"\">";
}
$buttonTable .= $query->startTable( COLS=>[ @cols ],
				WIDTH=>"100%",
				BGCOLOR=>"#EEEEF1",
				TITLE_BGCOLOR=>"#DDCCFF" );
## prepare the actualization of the LDAP
if (($server =~ /^(LDAP)$/i) and (getRequired ('LDAP') =~ /y/i)) {
        $buttonTable .= $query->addTableLine(DATA=>[ "<B>".gettext(
           "WARNING: The LDAP only has one CRL so if you add this CRL to LDAP then you will overwrite every other CRL.")."</B>" ]);
        ## update cert on LDAP
        my $ldap_button = "<input ".
                        "TYPE=\"Submit\" ".
                        "Name=\"Submit\" ".
                        "Value=\"".gettext("Add to LDAP")."\" ".
                        "onClick=\"cmd.value='addCrlLDAP'\">";
        $buttonTable .= $query->addTableLine(DATA=>[ "<B>".gettext("Add the CRL to LDAP:")."</B>",
                        ($ldap_button or gettext("n/a")) ]);
        ## update cert on LDAP with modified DN
        my $ldap_button = "<input ".
                        "TYPE=\"Button\" ".
                        "Name=\"Submit\" ".
                        "Value=\"".gettext("Add to LDAP with modified DN")."\" ".
                        "onClick=\"getParams( 'addCrlLDAPbyName', this.form ); return false\">";
        $buttonTable .= $query->addTableLine(DATA=>[ "<B>".gettext("Add the CRL to LDAP but with changed issuer:")."</B>",
                        ($ldap_button or gettext("n/a")) ]);
}
$buttonTable .= $query->endTable();
if ( $server =~ /^(LDAP)$/i ) {
        $buttonTable .= "</form>\n";
}


my $certTable = "";
foreach $rev ( @{$crl->getParsed()->{LIST}} ) {
	if( $rev->{SERIAL} ) {
		$certTable .= $query-> addTableLine( DATA => [
			"<B>".gettext ("Serial:")."</B> <a href=\"$self?cmd=viewCert&" .
			"dataType=REVOKED_CERTIFICATE&key=" .
			$rev->{SERIAL} . "\">" . 
			( $rev->{SERIAL} or gettext("n/a")) . "</a>",
			"<B>".gettext ("Revoked On:")."</B> " . ($rev->{DATE} or gettext("n/a") )] );
	} else {
		$certTable .= $query-> addTableLine( DATA => [
						"<B>".gettext("Serial:")."</B> " . ($rev->{SERIAL} or gettext("n/a")) ,
			"<B>".gettext("Revoked On:")."</B> " . ($rev->{DATE} or gettext("n/a") )] );
	}
}

$page = $tools->subVar( PAGE=>$page, NAME=>'@_SERIAL@',
			VALUE=>( $crl->getSerial () or gettext("n/a") ) );
$page = $tools->subVar( PAGE=>$page, NAME=>'@_VERSION@',
			VALUE=>( $parsed->{VERSION} or gettext("n/a") ) );
$page = $tools->subVar( PAGE=>$page, NAME=>'@_ALG@',
			VALUE=>( $parsed->{ALGORITHM} or gettext("n/a") ) );
$page = $tools->subVar( PAGE=>$page, NAME=>'@_ISSUER@',
			VALUE=>( $issuer or gettext("n/a") ) );
$page = $tools->subVar( PAGE=>$page, NAME=>'@_LASTUPDATE@', 
			VALUE=>( $parsed->{LAST_UPDATE} or gettext("n/a") ) );
$page = $tools->subVar( PAGE=>$page, NAME=>'@_NEXTUPDATE@',
			VALUE=>( $parsed->{NEXT_UPDATE} or gettext("n/a") ) );
$page = $tools->subVar( PAGE=>$page, NAME=>'@_TIME@',
			VALUE=>( $tools->getDate() or gettext("n/a") ) );
$page = $tools->subVar( PAGE=>$page, NAME=>'@_CERTSTABLE@',
			VALUE=>( $certTable or gettext("No Revoked Certificates") ) );
print "$page";

}

1;

