#!/usr/bin/perl

# ezpiii.pl
# (c) 1999-2000 Useful Utilities
# http://www.usefulutilities.com

# This script uses the Innovative Interfaces Inc. (III) patron authentication
# interface to authenticate users.

# Uncomment the "$debugfile = " line (and change the filename if you want) to
# have debugging information recorded
# $debugFile = "/tmp/ezproxy.dbg";

# ezpauth.pl really should be moved to a directory other than your
# CGI directory.  If you do this, be sure to change the following
# line to reflect this new directory
require "ezpauth.pl";

sub ShowForm;
sub ExpiredID;
sub WrongPatronType;

# To link EZproxy to this script, you need to make an entry in ezproxy.usr
# that looks like:
#
#     cgiuser:cgipass:cgi=http://auth.mylib.org/cgi-bin/ezpiii.cgi?
#
# This line indicates that all new authentication requests should be
# handed off to the specified CGI script.  The ? at the end is required
# since EZproxy will then append &url= followed by the web site the
# user wants to visit.
#
# $ezpuser and $ezppass have to be set to match the username and password
# from the ezproxy.usr entry.  $ezphost must be your EZproxy server's
# host name.
$ezpuser = "cgiuser";
$ezppass = "cgipass";
$ezphost = "ezproxy.mylib.org";

ParseFields();

# If the user field is undefined, show the main form
if (! defined $in{"idnumber"}) {
  ShowForm(0);
  exit(1);
}

# It's a good idea to validate your library card number first as a protection
# for the III interace.  This checks that there are 1 to 20 digits.  If that
# passes, then we let III finish validation.
if ($in{"idnumber"} =~ /^\d{1,20}$/) {
  $result = AuthIII("iii.mylib.org", $in{"idnumber"}, $in{"lastn"});
} else {
# If input user is not ten digits, it must be invalid so set $result to
# the invalid library card code of 1.
  $result = 1;
}

# If the user was validated, call StartSession to have EZproxy create a new
# new session for the user then redirect the user on to the desired URL
if ($result == 0) {
  StartSession();
} elsif ($result == 2) {
# The library card number is expired, so report this
  ExpiredID();
} elsif ($result == 3) {
  WrongPatronType();
} else {
# The library card number was invalid, so report this and let the user try
# again
  ShowForm(1);
}

sub ShowForm
{
  print <<EOF;
Content-Type: text/html
Cache-control: no-cache
Cache-control: no-store
Pragma: no-cache

<html>
<head>
<title>Patron Authentication</title>
</head>
<body>
<h1>Patron Authentication</h1>
<p>
EOF

  if ($_[0]) {
    print <<EOF;
The information you entered was invalid, please try again.<p>
EOF
  }

  $encurl = FormEncode($in{"url"});

  print <<EOF;
<form method="post" action="$action">
<input type="hidden" name="url" value="$encurl">
<table border=0 cellpadding=4 align=center bgcolor=#eeeeee>
<tr><td><font face=arial><b>First Name:</b></td><td><input type="text" name="firstn" size=15 maxlength=24></td></tr>
<tr><td><font face=arial><b>Last Name:</b></td><td><input type=text name=lastn size=15 maxlength=35></td></tr>
<tr><td><font face=arial><b>CSU ID Number:</b></td><td><input type=password name=idnumber size=15 maxlength=15></td></tr>
</table>
<p>
<input type=submit value=Submit>
<input type=reset value=Clear>
<p>
</form>
</body>
</html>
EOF
}

sub ExpiredID
{
  print <<EOF;
Content-Type: text/html

<html>
<head>
<title>Expired</title>
</head>
<body>
Your library card has expired.  Please bring your card to the library to
have it renewed.
</body>
</html>
EOF
}

sub WrongPatronType
{
  print <<EOF;
Content-type: text/html

<html>
<head>
<title>Unauthorized Patron Class</title>
</head>
<body>
You are not authorized for remote access to this database.
</body>
</html>
EOF
}
