#! /usr/bin/perl -w
#
#   Copyright 1997 Webwork Solutions Inc.
#
#   Written by G. E. Terry
#


sub BEGIN {
    unshift @INC, '..';
  }
use Mysql;
use CGI qw(:cgi-lib);
&ReadParse (*input);

# Variables
$host = "YourDomain.Com";
$database = "YourAuthDataBase";
$table = "YourAuthTable";
$userfield = "username";
$passfield = "passwd";
$timestampfield = "datestamp";
$returnURL= "http://yourdomain.com/yourform.html";

# Form Variables
$username = $input{'username'};
$passwd = $input{'password'};
$timeincrease = $input{'timeincrease'};


#Get current time and split it into it's parts.

my $time = time;
my ($sec, $min, $hour, $mday, $mon, $year) = localtime ($time);

if ($mon == 0){
    $daysinmonth = 31;
  }
elsif ($mon == 1){
    $daysinmonth = 28;
  }
elsif ($mon == 2){
    $daysinmonth = 31;
}
elsif ($mon == 3){
  $daysinmonth = 30;
}
elsif ($mon == 4){
  $daysinmonth = 31;
}
elsif ($mon == 5){
  $daysinmonth = 30;
}
elsif ($mon == 6){
  $daysinmonth = 31;
}
elsif ($mon == 7){
  $daysinmonth = 31;
}
elsif ($mon == 8){
  $daysinmonth = 30;
}
elsif ($mon == 9){
  $daysinmonth = 31;
}
elsif ($mon == 10){
  $daysinmonth = 30;
}
elsif ($mon == 11){
  $daysinmonth = 31;
}

if ($timeincrease > 23){
    $timetoexpire = $timeincrease % 24;
    $date_tmp = $timeincrease - $timetoexpire;
    $dateincrease = $date_tmp / 24;
    $time_expire = $hour + $timetoexpire;

            if ($time_expire > 24){
                $time_expire = $time_expire - 24;
             }
  }
else{
     $dateincrease = 0;
     $timetoexpire = $hour + $timeincrease;
            if ($timeincrease + $hour > 23){
                $expiretime_tmp = 24 - $hour;
                $expire_tmp = $timeincrease - $expiretime_tmp;
                $dateincrease = $dateincrease + 1;
                $time_expire = $expire_tmp;
             }
             else{
                  $time_expire = $hour + $timeincrease;
      }
  }

$expiredate = $mday + $dateincrease;
$newyear = $year;
$newmonth = $mon;

if ($expiredate > $daysinmonth){
    $newmonth = $mon + 1;
    if ($newmonth == 12){
    $newmonth = 0;
  }
}

if ($newmonth == 0){
    $newyear = $year + 1;
  }


# Format Time
            if ($sec < 10){
                    $sec = "0$sec",
            }
            if ($min < 10){
                    $min = "0$min"
            }
            if ($time_expire < 10){
                    $time_expire = "0$time_expire"
            }
            if ($expiredate < 10){
                    $expiredate = "0$expiredate"
            }
            if ($newmonth < 10){
                    $newmonth = "0$newmonth"
            }
            if ($newyear < 10){
                    $newyear = "0$newyear"
            }

$timestamp = "19$newyear$newmonth$expiredate$time_expire$min$sec";

# Connect to database

my $dbh = Mysql->Connect($host);
die "connect failed" unless $dbh;

# Select the authentication database

my $rv = $dbh->SelectDB ($database);
die "SelectDB failed" unless $rv;

# Call Sub for password encryption
&CryptData;

# Insert data into MySQL Database

my $sh =  $dbh->query ("insert into $table ($userfield, $passfield, $timestampfield) values ('$username', '$password', $timestamp)");
die "query failed: ", $dbh->errmsg unless $sh;
undef $sh;

# Return user back to admin page

      print "Content-type: text/html\n\n";
      print "<META HTTP-EQUIV=\"REFRESH\" CONTENT=\"0;URL=$returnURL\">";



sub CryptData {
            $password = crypt($passwd, Mx);
            }

exit;
