#!/usr/bin/perl -w
use DBI;
use IO::Handle;
use GnuPG::Interface;
use strict;
my $dbh = ''; my $sth = ''; my $ref;# database handlers
my $user = 'dclug';
my $password = 'LDEc001';
my $homedir = "/home/clients/lug/.gnupg";
my $gnupg;
my $input   = IO::Handle->new();
my $error   = IO::Handle->new();
my $output  = IO::Handle->new();
my $listhandles = GnuPG::Handles->new();
my $encrypthandles = GnuPG::Handles->new( stdin  => $input,
	stdout => $output, stderr => $error );
my $pid;
my @keylist;
my $idlist;
my @ciphertext;

my @gnupg_options = qw(--no-permission-warning );

$gnupg = GnuPG::Interface->new();
$gnupg->options->hash_init( armor => 1, homedir => $homedir, always_trust => 1 );
$gnupg->options->meta_interactive( 0 );
$gnupg->options->extra_args( @gnupg_options );

$dbh = DBI->connect("DBI:mysql:database=dclug;host=localhost",$user,$password, {'RaiseError'=>1});
$sth = $dbh->prepare("SELECT keyid from subscribers where keyid != \"\" order by id;");
$sth->execute();
while ($ref = $sth->fetchrow_hashref()) {
	push(@keylist, $ref->{'keyid'});
}
$sth->finish();
$dbh->disconnect();

print "Content-type: text/html\n\n";
# ===============================================
# Update the database keys in the local keyring
foreach $idlist (@keylist) {
$pid = $gnupg->recv_keys( handles => $encrypthandles,
	command_args => "0x$idlist" );
#$pid = $gnupg->recv_keys( handles => $encrypthandles,
#	command_args => @keylist );
@ciphertext = <$error>;
close $output;
close $error;
waitpid $pid, 3;
}

# ===============================================
my $keyid; my @a;
$idlist = "";
foreach $keyid (@keylist) {
	push(@a, "\'$keyid\'");
}
print<<STARTRING;
<html>
<head>
<title>D&amp;C GLUG GnuPG Keyring</title>
</head>
<body>
<pre>
STARTRING
# ===============================================
# Export the database keys from the local keyring
$pid = $gnupg->export_keys( handles => $listhandles,
	command_args => [ @keylist ] );
waitpid $pid, 0;
# ===============================================
print<<ENDRING
</pre>
</body>
</html>
ENDRING
