1  #! /usr/local/bin/perl -w
  2  
  3  #############################################################################
  4  #  
  5  #   File:    validate.pl
  6  #
  7  #   Input:   data from the HTML form register0.html
  8  #
  9  #   Output:  a confirmation page containing the input data *or*
 10  #            a page of error messages
 11  #
 12  #   From the HTML page register0.html, the user provides information
 13  #   that registers him or her for a software package.  This script
 14  #   validates the input data, returns a page of error messages if
 15  #   errors are detected, or asks for confirmation of the input data.
 16  #
 17  #############################################################################
 18  #  
 19  #   Initialization
 20  #
 21  #############################################################################
 22  
 23  $EOL = "\015\012";
 24  $SCRIPT = 
 25    "http://osprey7.npac.syr.edu:3768/users-cgi/trscavo/tutorials/CGI/examples/basic/register.pl";
 26  $HOSTPATH = "http://www.npac.syr.edu/projects/tutorials/CGI/examples/basic";
 27  
 28  #############################################################################
 29  #  
 30  #   Library Routines
 31  #
 32  #############################################################################
 33  
 34  require "ReadParse.pl" || die "error loading ReadParse.pl\n";
 35  
 36  #############################################################################
 37  #  
 38  #   Main Routines
 39  #
 40  #############################################################################
 41  
 42  ReadData();
 43  if ( ValidData() ) {
 44    ConfirmReg();
 45  } else {
 46    PrintErrors();
 47  }
 48  
 49  #############################################################################
 50  #  
 51  #   Subroutines
 52  #
 53  #############################################################################
 54  
 55  sub ReadData {
 56    my (%in);
 57    # ReadParse returns an associative array of key-value pairs:
 58    %in = ReadParse();
 59    $NAME = $in{'name'};
 60    $EMAIL = $in{'email'};
 61    $PLATFORM = $in{'platform'};
 62    $TEXAPP = $in{'TeXapp'};
 63    $OTHERAPP = $in{'otherApp'};
 64    return 1;
 65  }
 66  
 67  # Validate the input data and accumulate the error messages:
 68  sub ValidData {
 69    my ($isOkay);
 70    
 71    $isOkay = 1;
 72    $MSGS = "";
 73    if ( $NAME eq "" ) {
 74      $MSGS .= "error:  name omitted <P>$EOL";
 75      $isOkay = 0;
 76    }
 77    if ( $EMAIL eq "" ) {
 78      $MSGS .= "error:  e-mail omitted <P>$EOL";
 79      $isOkay = 0;
 80    }
 81    if ( $TEXAPP eq "other" && $OTHERAPP eq "" ) {
 82      $MSGS .= "error:  \"other\" TeX app omitted <P>$EOL";
 83      $isOkay = 0;
 84    }
 85    
 86    return $isOkay;
 87  }
 88  
 89  # No errors in the input data were found, so print an HTML page
 90  # and request confirmation from the user:
 91  sub ConfirmReg {
 92    my ($app);
 93    
 94    if ( $TEXAPP eq "other" ) {
 95      $app = $OTHERAPP;
 96    } else {
 97      $app = $TEXAPP;
 98    }
 99    
100    print qq{Content-type: text/html$EOL$EOL
101  <HTML>
102  <HEAD>
103  <TITLE>Confirmation Request</TITLE>
104  </HEAD>
105  <BODY BGCOLOR="#FFFFFF">
106  
107  <H1>Input Data</H1>
108  
109  <UL>
110    <LI>name = $NAME
111    <LI>e-mail = $EMAIL
112    <LI>platform = $PLATFORM
113    <LI>TeX app = $app
114  </UL>
115  
116  <FORM METHOD=POST ACTION="$SCRIPT">
117    <INPUT TYPE=hidden NAME=name VALUE="$NAME">
118    <INPUT TYPE=hidden NAME=email VALUE="$EMAIL">
119    <INPUT TYPE=hidden NAME=platform VALUE="$PLATFORM">
120    <INPUT TYPE=hidden NAME=TeXapp VALUE="$TEXAPP">
121    <INPUT TYPE=hidden NAME=otherApp VALUE="$OTHERAPP">
122    Is this correct? <P>
123    <INPUT TYPE=submit NAME=install VALUE=Yes>
124    <INPUT TYPE=submit NAME=install VALUE=No>
125  </FORM>
126  
127  </BODY>
128  </HTML>
129  };
130    
131  }  # end ConfirmReg
132  
133  # Print a page of error messages and let the user reload the HTML form:
134  sub PrintErrors {
135    print qq{Content-type: text/html$EOL$EOL
136  <HTML>
137  <HEAD>
138  <TITLE>Error Report</TITLE>
139  </HEAD>
140  <BODY BGCOLOR="#FFFFFF">
141  
142  <H1>Input errors detected</H1>
143  
144  $MSGS
145  
146  <P><HR><P>
147  
148  <A HREF="$HOSTPATH/register0.html"><EM>Try again!</EM></A>
149  
150  </BODY>
151  </HTML>
152  };
153    
154  }  # end PrintErrors