Let's see if we can work it out.
Code:
sub auto_delete {
# ------------------------------------------
# Automatically removes entries older then $remove
# days old.
#
my $remove = 30; # Number of days old.
my $date_field = 5; # Position of date field.
my $today = &date_to_unix(&get_date);
my $removeby = $today - ($remove * 86400);
my (@lines, @values);
open (DB, $db_file_name) or &cgierr ("Can't open: $db_file_name. Reason: $!");
if ($db_use_flock) { flock (DB, 1); }
@lines = <DB>;
close DB;
open (DB, ">$db_file_name") or &cgierr ("Can't open: $db_file_name. Reason: $!");
if ($db_use_flock) { flock (DB, 2); }
foreach (@lines) {
next if /^#/;
next if /^\s*$/;
chomp;
@values = &split_decode ($_);
if (($removeby > &date_to_unix($values[$date_field])) && ($values[2] eq 'Guest')) {
open (PASS, "<$auth_pw_file") or &cgierr ("unable to open: $auth_pw_file.\nReason: $!");
if ($db_use_flock) { flock(PASS, 1) }
@passlines = <PASS>;
close PASS;
open (PASS, ">$auth_pw_file") or &cgierr ("unable to open: $auth_pw_file.\nReason: $!");
if ($db_use_flock) {
flock(PASS, 2) or &cgierr("unable to get exclusive lock on $auth_pw_file.\nReason: $!");
}
foreach $passline (@passlines) {
($line =~ /^$values[$auth_user_field]:/) ?
($found = 1) :
print PASS $passline;
}
close PASS;
}
else {
print DB $_, "\n";
}
}
close DB;
}
sub date_to_unix {
# --------------------------------------------------------
# This routine must take your date format and return the time a la UNIX time().
# Some things to be careful about..
# int your values just in case to remove spaces, etc.
# catch the fatal error timelocal will generate if you have a bad date..
# don't forget that the month is indexed from 0!
#
my ($date) = $_[0];
my ($time);
my ($mon, $day, $year) = split(/-/, $_[0]);
unless ($day and $mon and $year) { return undef; }
use Time::Local;
eval {
$day = int($day); $year = int($year) - 1900;
$mon = int($mon) - 1;
$time = timelocal(0,0,0,$day, $mon, $year);
};
if ($@) { return undef; } # Could return 0 if you want.
return ($time);
}
This should do what you want. I did check it for syntax errors, but be sure that you back up your data before you run it, just in case there's a problem.
------------------
JPD