## OpenCA - Command
## (c) 1998-2001 by Massimiliano Pala and OpenCA Group
## (c) Copyright 2002-2004 The OpenCA Project
##
##   File Name: listCSR
##       Brief: Build CSR Listing
## Description: Build CSR Listing given correct dataType
##  Parameters: viewFrom, dataType, ra

use strict;

sub cmdListCSR {

    our ($query, $db, $self);

    # our $DEBUG =1;

    my $from     = ( $query->param( 'viewFrom' ) or 0 );
    my $dataType = $query->param( 'dataType' );
    my $rows = $query->param( 'rows' );

    my ($name, $exp);
    my ($item_list, $cmd_panel) = (undef, undef);

    my ( $dateColTitle, @cols, @list );

    my ( $searchParams, $rowParams );

    $item_list->{CLASS} = "lists";

    ## Differentiate the list parameters
    if(  $dataType eq "PENDING_REQUEST" ) {
        $name = "Pending Certificate Signing Requests";
        $item_list->{HEAD}->[0] = gettext ("Serial");
        $item_list->{HEAD}->[1] = gettext ("Submit Name");
        $item_list->{HEAD}->[2] = gettext ("Submitted On");
        $item_list->{HEAD}->[3] = gettext ("Role");
        $item_list->{HEAD}->[4] = gettext ("LOA");
    } elsif( $dataType eq "NEW_REQUEST" ) {
        $name = "New Certificate Signing Requests";
        $item_list->{HEAD}->[0] = gettext ("Serial");
        $item_list->{HEAD}->[1] = gettext ("Submit Name");
        $item_list->{HEAD}->[2] = gettext ("Submitted On");
        $item_list->{HEAD}->[3] = gettext ("Role");
        $item_list->{HEAD}->[4] = gettext ("LOA");
    } elsif	( $dataType eq "RENEW_REQUEST" ) {
        $name = "Renewed Certificate Signing Requests";
        $item_list->{HEAD}->[0] = gettext ("Operator");
        $item_list->{HEAD}->[1] = gettext ("Serial");
        $item_list->{HEAD}->[2] = gettext ("Submit Name");
        $item_list->{HEAD}->[3] = gettext ("Renewed On");
        $item_list->{HEAD}->[4] = gettext ("Role");
        $item_list->{HEAD}->[5] = gettext ("LOA");
    } elsif	( $dataType eq "RENEW_APPROVED" ) {
        $name = "Approved Renewal Certificate Requests";
        $item_list->{HEAD}->[0] = gettext ("Operator");
        $item_list->{HEAD}->[1] = gettext ("Serial");
        $item_list->{HEAD}->[2] = gettext ("Submit Name");
        $item_list->{HEAD}->[3] = gettext ("Renewed On");
        $item_list->{HEAD}->[4] = gettext ("Role");
        $item_list->{HEAD}->[5] = gettext ("LOA");
    } elsif( $dataType eq  "SIGNED_REQUEST" ) {
        $name = "Signed Certificate Signing Requests";
        $item_list->{HEAD}->[0] = gettext ("Operator");
        $item_list->{HEAD}->[1] = gettext ("Serial");
        $item_list->{HEAD}->[2] = gettext ("Submit Name");
        $item_list->{HEAD}->[3] = gettext ("Signed On");
        $item_list->{HEAD}->[4] = gettext ("Role");
        $item_list->{HEAD}->[5] = gettext ("LOA");
    } elsif( $dataType eq  "APPROVED_REQUEST" ) {
        $name = "Approved Certificate Signing Requests";
        $item_list->{HEAD}->[0] = gettext ("Operator");
        $item_list->{HEAD}->[1] = gettext ("Serial");
        $item_list->{HEAD}->[2] = gettext ("Submit Name");
        $item_list->{HEAD}->[3] = gettext ("Approved On");
        $item_list->{HEAD}->[4] = gettext ("Role");
        $item_list->{HEAD}->[5] = gettext ("LOA");
    } elsif ( $dataType eq "DELETED_REQUEST" ) {
        $name = "Deleted Certificate Signing Requests";
        $item_list->{HEAD}->[0] = gettext ("Operator");
        $item_list->{HEAD}->[1] = gettext ("Serial");
        $item_list->{HEAD}->[2] = gettext ("Submit Name");
        $item_list->{HEAD}->[3] = gettext ("Deleted On");
        $item_list->{HEAD}->[4] = gettext ("Role");
        $item_list->{HEAD}->[5] = gettext ("LOA");
    } elsif ( $dataType eq "ARCHIVED_REQUEST" ) {
        $name = "Archived Certificate Signing Requests";
        $item_list->{HEAD}->[0] = gettext ("Operator");
        $item_list->{HEAD}->[1] = gettext ("Serial");
        $item_list->{HEAD}->[2] = gettext ("Submit Name");
        $item_list->{HEAD}->[3] = gettext ("Archived On");
        $item_list->{HEAD}->[4] = gettext ("Role");
        $item_list->{HEAD}->[5] = gettext ("LOA");
    } else {
	configError(gettext("The requested list is not available."));
    }

    ## Get required parameters ( return only the value string )
    my $maxItems = getRequired( "maxReturnedItems" );
    # my $elements = $db->elements( DATATYPE=>$dataType );

    my $ra  = ( $query->param('ra') or "All" );
    my $loa = ( $query->param('loa') or "" );
    my $role = ( $query->param('role') or "All" );

    my $search = undef;

    $ra = "All" if $ra eq gettext("All");
    $role = "All" if $role eq gettext("All");
    $loa = "All" if $loa eq gettext("All");

    if ( ($ra ne "") and ($ra !~ /All/i) ) {
	$search->{RA} = $query->param('ra');
    }

    if ( ($loa ne "") and ($loa !~ /All/i ) ) {
	$search->{LOA} = $query->param('loa');
    }

    if ( ($role ne "") and ($role !~ /All/i ) ) {
	$search->{ROLE} = $query->param('role');
    }

    if ( $rows eq "" ) {
		$rowParams->{DATATYPE} = $dataType;
		$rowParams->{MODE} = 'COUNT';

		$rows = $db->searchItems ( %{ $rowParams }, %{ $search } );
    }

    # Now let's put all the parameters for the search
    $searchParams->{DATATYPE} = $dataType;
    $searchParams->{ITEMS}    = $maxItems;
    $searchParams->{FROM}     = $from;

    @list = $db->searchItems ( %{ $searchParams }, %{ $search } );

    if ( scalar @list < 1 ) {

        $cmd_panel->[0] = libBuildRefs(
			DATATYPE  => $dataType,
                        ELEMENTS  => $rows,
                        MAXITEMS  => $maxItems,
                        MODE      => "EXP",
                        NOW_FIRST => $from,
                        NOW_LAST  => 0,
                        FIRST     => 0,
                        LAST      => 0);

    } else {

	my $firstItem = undef;
	my $lastItem = undef;

	my $firstSerial = 0;
	my $lastSerial = 0;

	my $nowFirst = 0;
	my $nowLast = 0;

	$firstItem = libDBGetFirstItem ( $dataType );
	$lastItem = libDBGetLastItem ( $dataType );

	if( $firstItem ) {
		$firstSerial = $firstItem->getSerial( $dataType );
	};

	$lastSerial = $firstSerial;

	if( $lastItem ) {
		$lastSerial = $lastItem->getSerial( $dataType );
	};

	if( $list[0] ) {
		$nowFirst = $list[0]->getSerial( $dataType );
	} else {
		$nowFirst = $firstSerial;
	};

	if ( $list[ scalar (@list) - 1] ) {
		$nowLast = $list[ scalar (@list) - 1]->getSerial($dataType);
	} else {
		$nowLast = $lastSerial;
	};

        $cmd_panel->[0] = libBuildRefs(
                        DATATYPE  => $dataType,
                        ELEMENTS  => $rows,
                        MAXITEMS  => $maxItems,
                        MODE      => "EXP",
                        NOW_FIRST => $nowFirst,
                        NOW_LAST  => $nowLast,
                        FIRST     => $firstSerial,
                        LAST      => $lastSerial,
			SEARCH	  => $search );

    }

    ## get list of the LOAs type
    my  $loaOption = getRequired('USE_LOAS');
    my ($loaTwig, $xmlLOA, %LOALevels, );
    if ($loaOption =~ m/yes/i)
    {
        $loaTwig = loadConfigXML ('LOAConfiguration');
        if (not $loaTwig) {
            generalError (gettext ("Cannot load menu configration"));
        }

        #$xmlLOA = $twig->get_xpath('loa');
        my @loaList;
        for my $al ($loaTwig->get_xpath("loa"))
        {
            $xmlLOA = gettext(($al->first_child('name'))->field);

            $LOALevels{gettext(($al->first_child('level'))->field)}=$xmlLOA;

            push (@loaList, $xmlLOA);
            debug_cmds ("listCSR: \@loadList @loaList");
            debug_cmds ("listCSR: LOALevel 10: $LOALevels{10}");
        }
    }

    ## Process all CSRs
    $item_list->{BODY} = [];
    foreach my $req ( @list ) {
        my ( $head, $parsed, $format, $key, $ser_col, $serial, $date, $loa  );
        my @vals;

        ## We have no problem either if it is a PEM or SPKAC
        ## request as the REQ mod will try to recognize it.
        ## my $req = new OpenCA::REQ( SHELL=>$openssl,
        ##                         DATA=>$reqData);

        next if (not $req);

        my $pos = scalar @{$item_list->{BODY}};
        my $index = 0;
        $parsed = $req->getParsed();
        $head   = $parsed->{HEADER};
        $key    = $req->getSerial();

        $serial = $req->getSerial();
        $date   =  ( $parsed->{NOTBEFORE} or $head->{NOTBEFORE} or "---" );

        if ( not $serial ) {
            $serial = "<CENTER>---</CENTER>";
        };

        my $lnk = new CGI({cmd=>"viewCSR", dataType=>"$dataType", key=>$key});
        $ser_col = $lnk->a({-class=>"list", -href=>"?".$lnk->query_string()}, "$serial");

        ## get the name of the loa to display
        if ($loaOption =~ m/yes/i)
        {	
            $loa = $LOALevels{$head->{LOA}};
            debug_cmds ("listCSR: LOA in head is $head->{LOA} and loa is $loa");
        }

        if ($dataType !~ /(NEW|PENDING)/) {
            ## get operator from first signature
            if (my $sig = libGetSignatureObject ( OBJECT => $req )) {
                if (my $cert = libGetSignerCertificateDB( SIGNATURE=> $sig )) {
                    $item_list->{BODY}->[$pos]->[$index++] =
                        "<a class=\"list\" href=\"$self?cmd=viewCert&key=".
                        $cert->getSerial()."\">".
                        $cert->getSerial()."</a>";
                } else {
                    $item_list->{BODY}->[$pos]->[$index++] =
                        gettext ("Cannot determine certificate from signature!");
                    print STDERR i18nGettext ("Cannot determine certificate from signature (CRR: __SERIAL__).",
                                              "__SERIAL__", $key)."\n";
                }
           	} else {
                if (not $req->getParsed()->{SIGNATURE}) {
                    my $operator = ( $parsed->{OPERATOR} or $head->{OPERATOR} or gettext("n/a"));
                    if ($operator =~ /n\/a/i) {
                        $operator = gettext("n/a");
                    }
                    $item_list->{BODY}->[$pos]->[$index++] = $operator;
                } else {
                    $item_list->{BODY}->[$pos]->[$index++] = 
                        gettext ("Cannot build PKCS#7-object from signature!");
                    print STDERR i18nGettext ("Cannot build object from signature (CSR: __SERIAL__).",
                                              "__SERIAL__", $key)."\n";
                }
            }
        }

        $item_list->{BODY}->[$pos]->[$index++] = $ser_col;
        if ( $head->{SUBJECT} =~ /CN=([^\,]+)/gi ) {
            $item_list->{BODY}->[$pos]->[$index++] = $1;
        } elsif ( $parsed->{CN} ) {
            $item_list->{BODY}->[$pos]->[$index++] = $parsed->{CN};
        } elsif ( $parsed->{DN} =~ /CN=([^\,]+)/gi ) {
            $item_list->{BODY}->[$pos]->[$index++] = $1;
        } elsif ( $parsed->{DN} ) {
            $item_list->{BODY}->[$pos]->[$index++] = $parsed->{DN};
        } else {
            $item_list->{BODY}->[$pos]->[$index++] = "<CENTER>---</CENTER>";
        }

        if ( $dataType !~ /(NEW|PENDING|RENEW)/i ) {
            $item_list->{BODY}->[$pos]->[$index++] =
                ( $head->{DELETED} or $head->{APPROVED} or gettext("n/a"));
        } else {
            $item_list->{BODY}->[$pos]->[$index++] =
                ( $head->{NOTBEFORE} or gettext("n/a"));
        }
        $item_list->{BODY}->[$pos]->[$index++] =
            ($head->{ROLE} or gettext("n/a"));
			
        $item_list->{BODY}->[$pos]->[$index++] =
            ($loa or gettext("n/a"));
    }

    return libSendReply (
                         "NAME"      => gettext ($name),
                         "ITEM_LIST" => $item_list,
                         "CMD_PANEL" => $cmd_panel,
                         "TIMESTAMP" => 1
                        );
}

1;

