#---------------------------------------------------------------------
# GetLogon
#---------------------------------------------------------------------
#
# Gets the logged-on userid for the admin section
#
# Decodes it from the HTTP_AUTHORIZATION header, which is the
# base64 encoding of userid:password (for BASIC AUTH).
#
#---------------------------------------------------------------------
sub GetLogon {
my($authtype,$authstring) = split(' ', $ENV{'HTTP_AUTHORIZATION'});
return "unknown" unless $authtype =~ /basic/i;
# We COULD use the following two lines, but it would include
# code that we don't really need much.
#use MIME::Base64 ();
#$decoded = MIME::Base64::decode($encoded);
# The following code comes from perlfaq9
$authstring =~ tr[A-Za-z0-9+/][]cd; # remove non-base64 chars
$authstring =~ tr[A-Za-z0-9+/][ -_]; # convert to uuencoded format
$len = pack("c", 32 + 0.75*length($authstring)); # compute length byte
my $authstring = unpack("u", $len . $authstring); # uudecode and print
# Now we have userid:password. Split off just the userid
my ($user,undef) = split(':', $authstring);
return $user;
} # GetLogon