#!/usr/bin/perl
use strict 'refs';
use lib '..';
use CGI qw(:standard);
use CGI::Carp qw/fatalsToBrowser/;

sub authenticate {
  %credentials = cookie('splitreffile');
    my $the_cookie = cookie(-name=>"splitreffile",
                     -path=>"/",
                     -value=>\%credentials,
                     -expires=>"+4h",
                     -secure=>"secure");

  print header(-cookie=>$the_cookie);

  open PASSWD, '/usr/local/apache/passwd.txt';
  my @password = <PASSWD>;
  close PASSWD;

  my $uname = my $pword = '%%%%%%%%%%%%%';

  my $authenticated="0";

  auth: until ($uname eq $credentials{'name'} && $pword eq $credentials{'pass'}) {
    my $line;
    if(!($line = pop @password) || $credentials{'name'}.$credentials{'pass'} eq "") {
      my $basedir = "";
      last 'auth';
    }
    my $line =~ s/[\r\n]//g;
    ($uname, $pword, $basedir) = split ":", $line;
  }
  if($basedir eq "/usr/local/apache/htdocs") {$authenticated = "yes";}
  $credentials{'basedir'} = $basedir;
  return $authenticated;
}
