1  #! /usr/local/bin/perl -w
  2  
  3  #############################################################################
  4  #  
  5  #   File:    register.pl
  6  #
  7  #   Input:   data from the HTML form generated by validate.pl
  8  #
  9  #   Output:  depends on the input data (see below)
 10  #
 11  #   This script processes the HTML form generated by validate.pl.
 12  #   If the user presses the "Yes" button, the script writes an entry
 13  #   in the database and loads a courtesy HTML page.  (The production
 14  #   version of this script would load a more detailed page with links
 15  #   to download the requested software.)  If the user presses the "No"
 16  #   button, the script reloads the original form register0.html.
 17  #
 18  #   A typical entry in the database file looks like:
 19  #
 20  #     <record>
 21  #     name: Tom Scavo; email: trscavo@npac.syr.edu;
 22  #     platform: PowerMac; TeXapp: Textures;
 23  #     </record>
 24  #
 25  #   that is, key-value pairs are separated by a colon (:) while
 26  #   fields are separated by semicolons (;).
 27  #
 28  #############################################################################
 29  #  
 30  #   Initialization
 31  #
 32  #############################################################################
 33  
 34  $EOL = "\015\012";
 35  $DBFILE = "register.db";
 36  $HOSTPATH = "http://www.npac.syr.edu/projects/tutorials/CGI/examples/basic";
 37  
 38  #############################################################################
 39  #  
 40  #   Library Routines
 41  #
 42  #############################################################################
 43  
 44  require "ReadParse.pl" || die "error loading ReadParse.pl\n";
 45  
 46  #############################################################################
 47  #  
 48  #   Main Routines
 49  #
 50  #############################################################################
 51  
 52  ReadData();
 53  if ( $INSTALL eq "Yes" ) {
 54    ProcessData(); 
 55  } elsif ( $INSTALL eq "No" ) {
 56    ReloadForm();
 57  } else {
 58    # This can't happen!
 59  }
 60  
 61  #############################################################################
 62  #  
 63  #   Input
 64  #
 65  #############################################################################
 66  
 67  sub ReadData {
 68    my (%in);
 69    # ReadParse returns an associative array of key-value pairs:
 70    %in = ReadParse();
 71    $NAME = $in{'name'};
 72    $EMAIL = $in{'email'};
 73    $PLATFORM = $in{'platform'};
 74    $TEXAPP = $in{'TeXapp'};
 75    $OTHERAPP = $in{'otherApp'};
 76    $INSTALL = $in{'install'};
 77    return 1;
 78  }
 79  
 80  #############################################################################
 81  #  
 82  #   Subroutines
 83  #
 84  #############################################################################
 85  
 86  # Print a dummy HTML page (the production version of this page 
 87  # would provide links to the *requested* pieces of software):
 88  sub ProcessData {
 89    
 90    # Update the database:
 91    UpdateDBFile();
 92    
 93    # Print an HTML page:
 94    SendConfirm();
 95      
 96    return 1;
 97  }
 98  
 99  # Update the database with the input data:
100  sub UpdateDBFile {
101    open (DB, ">>$DBFILE") || die "Unable to open $DBFILE\n";
102    # Obtain an exclusive lock:
103    flock (DB, 2) || die "Unable to lock $DBFILE\n";
104    print DB "<record>\n";
105    print DB "name: $NAME; email: $EMAIL;\n";
106    print DB "platform: $PLATFORM; "; 
107    if ( $TEXAPP eq "other" ) {
108      print DB "TeXapp: $OTHERAPP;\n";
109    } else {
110      print DB "TeXapp: $TEXAPP;\n";
111    }
112    print DB "</record>\n";
113    # Release the lock:
114    flock (DB, 8) || die "Unable to unlock $DBFILE\n";
115    close (DB) || die "Unable to close $DBFILE\n";
116      
117    return 1;
118  }
119  
120  # Reload the original HTML form:
121  sub ReloadForm {
122    print "Location: $HOSTPATH/register0.html$EOL$EOL";    
123    return 1;
124  }
125  
126  sub SendConfirm {
127    print qq{Content-type: text/html$EOL$EOL
128  <HTML>
129  <HEAD>
130  <TITLE>Download Alpha LaTeX</TITLE>
131  </HEAD>
132  <BODY BGCOLOR="#FFFFFF">
133  
134  Thank you for registering your copy of Alpha LaTeX!
135  
136  <P><HR><P>
137  
138  Return to
139  <A HREF="$HOSTPATH/register0.html">HTML form</A>
140  
141  </BODY>
142  </HTML>
143  };
144  
145  }  # end SendConfirm