Listing 2: submit_request.pl
#!/usr/bin/perl
# submit_request.pl by johnk@dop.water.ca.gov created 1-23-95
#
# parts of this program were originally written for a text
# based support system by Sam Lamtinga <slouken@dop.water.ca.gov>
#
$phonebook="/support/phone.txt"; # this is our user information database.
$supportaddr='johnk'; # admin's email address
$mailer='/usr/bin/mailx' # set to the mailer of your system
$ORG='DOP'; # organization name
############################################### Begin main code.
$length=$ENV{CONTENT_LENGTH}; # Length of passed data
$have_uname=0;
$ident=$ENV{REMOTE_IDENT}; # Try to get the users' uname.
if (@pwent=getpwnam($ident))
{ # We have the uname.
$have_uname=1; # Set this so we won't look for info later.
$uname=$ident;
($rname)=split(/\,/, $pwent[6]);
&lookup($rname);
}
$INPUT=<STDIN>; # Get the input sent by httpd.
chop $INPUT; # Remove the <cr>.
&convert_escapes; # Convert escape codes to characters.
&parse_info; # Parse up the information from the form.
&send_request; # Send in the request.
&print_done; # Let user know everything's ok.
################################################ End main code.
# subroutines in alpha order.
sub convert_escapes # converts escape codes to characters.
{
$pos=(-1); # set position to beginning of line
while(($x=index($INPUT, "%", $pos+1))>=0)
{
# first convert the 2 digit value from hex to character.
$char=sprintf("%c", hex(substr($INPUT, $x+1, $x+2)));
#remove from the % to the end of the line.
$temp=substr($INPUT, 0, $x);
$temp.=$char; # add the character to the end of the new string
$temp.=substr($INPUT, $x+3); # then add on the rest of the string
$INPUT=$temp; # copy the temp string back to $longdesc
$pos=$x;
}
}
sub cut # Snatches the entry for the first field in $INPUT.
{
$end=index($INPUT, '&');
if ( $end == -1 ) # check if this is the last field.
{
$end=$length; # if so, set the end to the length of the string.
}
$temp=substr($INPUT, 0, $end);
$INPUT=substr($INPUT, $end+1, $length);
$length-= $end;
$begin=index($temp, '=');
$temp=substr($temp, $begin+1, $end);
$temp=~ tr/+/ /;
$temp # return temp
}
sub error # print out error message in HTML and quit
{
print "<H1>Request: ERROR\!</H1>\n";
print "<H2>@_[0]\n</H2>\n"; # print the error passed to &error.
print "<br><br><br><H2>Please report this error to a Systems
Admin.</H2>\n";
print "<HR>\n";
exit;
}
# lookup is a heavy reworking on a subroutine written
# by Sam Lantinga <slouken@dop.water.ca.gov>
sub lookup
{
$whoiam=$_[0]; # The user to be looked up is the argument.
if (! open(WP, "$phonebook"))
{
&error "Can't open $phonebook: $!\n";
}
($firstname, $lastname)=split(/ /, $rname);
$matched=0;
while (<WP>)
{
s///; # Remove leading carriage return
# Split on 2 or more occurences of whitespace
($lname,$fname,$phonef,$orgf,$locf,$room)=split(/\s{2,}/);
if ( $lname eq $lastname && $fname eq $firstname )
{
$matched=1;
last;
}
}
close(WP);
if(! $matched)
{
return;
}
# Extract the phone number and then the extension.
($numone, $acode, $ournum)=split(/ /, $phonef);
($pref, $extension)=split(/-/, $ournum);
}
sub parse_info # parse up what httpd fed us.
{
if (! $have_uname) # If we couldn't get the users' name...
{
$uname=&cut; # Pick up each field.
$rname=&cut; # (Each call to cut returns one field)
$extension=&cut;
$room=&cut;
}
$shortdesc=&cut;
if (! $shortdesc) # We want a one line description.
{
&error("A one line description must be entered.");
}
$level=&cut;
$longdesc=&cut;
$shortdesc=~ s/\"/\\\"/g; # Remove quotes
$shortdesc=~ s/\$/\\\$/g; # variables
$shortdesc=~ s/\'/\\\'/g; # and commands.
}
sub print_done # called if request was successfully sent to admin
{
print "Content-type: text/html\n\n\n";
print "<TITLE>Result of Request Submission</TITLE>\n";
print "<Body>\n";
print "<H3>Your request has been submitted.<br>";
print "The request database will be updated within 5 minutes.</H3>\n";
print "<HR>\n";
print "</Body>\n";
exit;
}
sub send_request # Mail off the request
{
($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time);
$time=sprintf("%d:%2d", $hour, $min);
$time=~ s/ /0/;
$now=sprintf("%d/%d/%d %s", $mon+1, $mday, $year, $time);
if (!open (MAIL, "|$mailer -s \"$shortdesc\" '$supportaddr"))
{
&error ("Unable to mail Request form.");
}
print MAIL
"
$ORG OPERATIONS
REQUEST FORM
========================= REQUESTOR INFORMATION =======================
NAME: $rname
DATE: $now
EXTENSION: $extension
LOCATION: $room
E-MAIL: $uname
Priority: $level
======================= WORK REQUEST INFORMATION ======================
Work Request: $shortdesc
Description of Request:
$longdesc
";
close (MAIL);
}
|