## OpenCA - Command
## (c) 1998-2001 by Massimiliano Pala and OpenCA Group
##
##   File Name: crlList
##       Brief: list CRLs
##    Revision: $Revision: 1.4 $
## Description: list all available CRLs
##  Parameters: none

sub cmdCrlList {

## Reserved variables
my ( $page, $dataType, $crlData );

## We need some parameters to generate the list because of
## we do not want to generate a list of ALL issued certificates
## so get the numer of results we should give away, then the
## starting serial number.
my $from     = $query->param( 'viewFrom' );
my $to       = $query->param( 'viewTo' );
my $matched  = $query->param( 'rows' );
my $dataType = "VALID_CRL";

## Get required parameters ( return only the value string )
my $sheet    = getRequired( 'CRLBaseSheet' );
my $maxItems = getRequired( 'MaxReturnedItems' );

## Get the base Page ( got in $page variable )
if( not $page = $query->getFile( "$sheet" ) ) {
	configError (i18nGettext ("File not Found : __FILE__", "__FILE__", $sheet));
};
my $uptime = $tools->getDate();

## Set the column titles
my @cols, @crlList;

push( @cols, gettext ("Ver.") );
push( @cols, gettext ("Ser.") );
push( @cols, gettext ("Last Update") );
push( @cols, gettext ("Next Update") );

if ( not $matched ) {
	$matched  = $db->elements( DATATYPE=>$dataType );
};

@crlList = $db->listItems( DATATYPE=>$dataType,
				FROM=>$from, ITEMS=>$maxItems);

if (not @crlList) {
	$table = $query->buildRefs(
			ELEMENTS  => $matched,
			MAXITEMS  => $maxItems,
			FACTOR    => 5,
			MODE      => "EXP",
			NOW_FIRST => 0,
			NOW_LAST  => 0,
			FIRST     => 0,
			LAST      => 0);
} else {
	$table = $query->buildRefs(
			ELEMENTS  => $matched,
			MAXITEMS  => $maxItems,
			FACTOR    => 5,
			MODE      => "EXP",
			NOW_FIRST => $crlList[0]->getSerial($dataType),
			NOW_LAST  => $crlList[scalar (@crlList) -1]->getSerial($dataType),
			FIRST     => libDBGetFirstItem ($dataType)->getSerial($dataType),
			LAST      => libDBGetLastItem ($dataType)->getSerial($dataType));
}

$table .= $query->startTable( COLS=>[ @cols ], WIDTH=>"100%",
						TITLE_BGCOLOR=>"#DDCCFF" );

## Process all Files
foreach $crl ( @crlList ) {

	my ( $format, $key, $type, $ser_col );
	my @vals;

	$key  = $crl->getSerial();
	$type = $dataType;

	$ser_col = "<CENTER><a href=\"$self?cmd=viewCRL&dataType=$type".
			"&key=$key\">".$crl->getParsed()->{VERSION} .
			"</a></CENTER>";
	push( @vals, $ser_col );

	## Standard to be suported extention is the SERIAL of CRL...
	push( @vals, $crl->getSerial() );
	push( @vals, $crl->getParsed()->{LAST_UPDATE} );
	push( @vals, $crl->getParsed()->{NEXT_UPDATE});

	$table .= $query->addTableLine( DATA =>[ @vals ]);
}

## Close the Table
$table .= $query->endTable();
$table .= getCopyRight();

## Substitute the Variables in the $page
$page = $query->subVar( $page, '$uptime', $uptime );
$page = $query->subVar( $page, '$table', $table );

print "$page";

}

1;

