home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 1998 May / Pcwk5b98.iso / WEBSERVE / SAMBAR / DATA.1 / book.pl < prev    next >
Text File  |  1997-05-19  |  3KB  |  112 lines

  1. #
  2. # Sample CGI/1.1 Perl Guestbook
  3. #
  4. # In the event of a failure, anything written to stderr can
  5. # be found in the "tmp" directory.
  6.  
  7. $bookfile = "../docs/samples/book.htm";
  8.  
  9. # Get the input
  10. read(STDIN, $data, $ENV{'CONTENT_LENGTH'});
  11.  
  12. # Split the name-value pairs
  13. @pairs = split(/&/, $data);
  14.  
  15. foreach $pair (@pairs) 
  16. {
  17.     ($name, $value) = split(/=/, $pair);
  18.  
  19.     # Convert the HTML encoding
  20.     $value =~ tr/+/ /;
  21.     $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
  22.     $value =~ s/<!--(.|\n)*-->//g;
  23.  
  24.     # Convert HTML stuff as necessary.
  25.     $value =~ s/<([^>]|\n)*>//g;
  26.  
  27.     $FORM{$name} = $value;
  28. }
  29.  
  30. # Verify that the required data has been received.
  31. &missing_name unless $FORM{'name'};
  32. &missing_msg unless $FORM{'msg'};
  33.  
  34. # Read in the book for editing
  35. # Note:  These actions on the book file should be atomic (flock()).
  36. open (FILE, "$bookfile") || die "Can't open $bookfile: $!\n";
  37. @LINES=<FILE>;
  38. close(FILE);
  39. $SIZE=@LINES;
  40.  
  41. # Open book file
  42. open (FILE, ">$bookfile") || die "Can't open $bookfile: $!\n";
  43.  
  44. for ($i = 0; $i <= $SIZE; $i++) 
  45. {
  46.     $_=$LINES[$i];
  47.  
  48.     if (/<!--top-->/) 
  49.     {
  50.         print FILE "<!--top-->\n";
  51.    
  52.         print FILE "<b>$FORM{'msg'}</b><br>\n";
  53.         print FILE "$FORM{'name'}";
  54.         if ($FORM{'email'})
  55.         {
  56.             print FILE " \<<A HREF=\"mailto:$FORM{'email'}\">";
  57.             print FILE "$FORM{'email'}</A>\>";
  58.         }
  59.  
  60.         print FILE "<HR>\n";
  61.     }
  62.     else 
  63.     {
  64.         print FILE $_;
  65.     }
  66. }
  67.  
  68. close (FILE);
  69.  
  70.  
  71. # Response message.
  72. print "<HTML><HEAD><TITLE>Thanks</TITLE></HEAD><BODY bgcolor=white>\n";
  73. print "<FONT SIZE=5 COLOR=#996633><B>Thanks for your message</B></FONT>\n";
  74. print "<BR><BR>Your entry has been added to our guest book:<HR>\n";
  75. print "<b>$FORM{'msg'}</b><br>\n";
  76. print "$FORM{'name'}";
  77.  
  78. if ($FORM{'email'})
  79. {
  80.     print " <<A HREF=\"mailto:$FORM{'email'}\">";
  81.     print "$FORM{'email'}</A>>";
  82. }
  83.  
  84. print "<P><HR>\n";
  85. print "<A HREF=\"/samples/index.htm\">Back to samples page.</A>\n";
  86. print "</BODY></HTML>\n";
  87.  
  88. exit;
  89.  
  90.  
  91. sub missing_name
  92. {
  93.     print "<HTML><HEAD><TITLE>Missing Name</TITLE></HEAD><BODY>\n";
  94.     print "<FONT SIZE=5 COLOR=#996633><B>Name field is blank...</B></FONT>\n";
  95.     print "<BR><BR>Please repost the name section of the guestbook.<P>\n";
  96.     print "Return to the <a href=\"/samples/book.htm\">Guest Book</a>.\n";
  97.     print "</BODY></HTML>\n";
  98.  
  99.     exit;
  100. }
  101.  
  102. sub missing_msg
  103. {
  104.     print "<HTML><HEAD><TITLE>Missing Comments</TITLE></HEAD>\n";
  105.     print "<FONT SIZE=5 COLOR=#996633><B>Comment field is blank...</B></FONT>\n";
  106.     print "<BR><BR>Please repost the comment section of the guestbook.<P>\n";
  107.     print "Return to the <a href=\"/samples/book.htm\">Guest Book</a>.\n";
  108.     print "</BODY></HTML>\n";
  109.  
  110.     exit;
  111. }
  112.