home *** CD-ROM | disk | FTP | other *** search
/ Internet 1996 World Exposition / park.org.s3.amazonaws.com.7z / park.org.s3.amazonaws.com / cgi-bin / guestmail.pl < prev    next >
Perl Script  |  2017-09-21  |  13KB  |  439 lines

  1. #!/usr/local/bin/perl
  2. ##############################################################################
  3. # FormMail            Version 1.5                     #
  4. # Copyright 1996 Matt Wright    mattw@worldwidemart.com                 #
  5. # Created 6/9/95                Last Modified 2/5/96                 #
  6. # Scripts Archive at:        http://www.worldwidemart.com/scripts/         #
  7. ##############################################################################
  8. # COPYRIGHT NOTICE                                                           #
  9. # Copyright 1996 Matthew M. Wright  All Rights Reserved.                     #
  10. #                                                                            #
  11. # FormMail may be used and modified free of charge by anyone so long as this #
  12. # copyright notice and the comments above remain intact.  By using this      #
  13. # code you agree to indemnify Matthew M. Wright from any liability that      #  
  14. # might arise from it's use.                                                 #  
  15. #                                         #
  16. # Selling the code for this program without prior written consent is         #
  17. # expressly forbidden.  In other words, please ask first before you try and  #
  18. # make money off of my program.                             #
  19. #                                                                            #
  20. # Obtain permission before redistributing this software over the Internet or #
  21. # in any other medium.    In all cases copyright and header must remain intact #
  22. ##############################################################################
  23. # Define Variables 
  24. #     Detailed Information Found In README File.
  25.  
  26. # $mailprog defines the location of your sendmail program on your unix 
  27. # system.
  28.  
  29. $mailprog = '/usr/lib/sendmail';
  30.  
  31. # @referers allows forms to be located only on servers which are defined 
  32. # in this field.  This fixes a security hole in the last version which 
  33. # allowed anyone on any server to use your FormMail script.
  34.  
  35. @referers = ('park.org','nikhef.nl');
  36.  
  37. # Done
  38. #############################################################################
  39.  
  40. # Check Referring URL
  41. &check_url;
  42.  
  43. # Retrieve Date
  44. &get_date;
  45.  
  46. # Parse Form Contents
  47. &parse_form;
  48.  
  49. # Check Required Fields
  50. &check_required;
  51.  
  52. # Return HTML Page or Redirect User
  53. &return_html;
  54.  
  55. # Send E-Mail
  56. &send_mail;
  57.  
  58. sub check_url {
  59.  
  60.    if ($ENV{'HTTP_REFERER'}) {
  61.       foreach $referer (@referers) {
  62.          if ($ENV{'HTTP_REFERER'} =~ /$referer/i) {
  63.             $check_referer = '1';
  64.         last;
  65.          }
  66.       }
  67.    }
  68.    else {
  69.       $check_referer = '1';
  70.    }
  71.  
  72.    if ($check_referer != 1) {
  73.       &error('bad_referer');
  74.    }
  75.  
  76. }
  77.  
  78. sub get_date {
  79.  
  80.    @days = ('Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday');
  81.    @months = ('January','February','March','April','May','June','July',
  82.           'August','September','October','November','December');
  83.  
  84.    ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time);
  85.    if ($hour < 10) { $hour = "0$hour"; }
  86.    if ($min < 10) { $min = "0$min"; }
  87.    if ($sec < 10) { $sec = "0$sec"; }
  88.  
  89.    $date = "$days[$wday], $months[$mon] $mday, 19$year at $hour\:$min\:$sec";
  90.  
  91. }
  92.  
  93. sub parse_form {
  94.  
  95.    if ($ENV{'REQUEST_METHOD'} eq 'GET') {
  96.       # Split the name-value pairs
  97.       @pairs = split(/&/, $ENV{'QUERY_STRING'});
  98.    }
  99.    elsif ($ENV{'REQUEST_METHOD'} eq 'POST') {
  100.       # Get the input
  101.       read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});
  102.  
  103.       # Split the name-value pairs
  104.       @pairs = split(/&/, $buffer);
  105.    }
  106.    else {
  107.       &error('request_method');
  108.    }
  109.  
  110.    foreach $pair (@pairs) {
  111.       ($name, $value) = split(/=/, $pair);
  112.  
  113.       $name =~ tr/+/ /;
  114.       $name =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
  115.  
  116.       $value =~ tr/+/ /;
  117.       $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
  118.  
  119.       # If they try to include server side includes, erase them, so they
  120.       # arent a security risk if the html gets returned.  Another 
  121.       # security hole plugged up.
  122.  
  123.       $value =~ s/<!--(.|\n)*-->//g;
  124.  
  125.       # Create two associative arrays here.  One is a configuration array
  126.       # which includes all fields that this form recognizes.  The other
  127.       # is for fields which the form does not recognize and will report 
  128.       # back to the user in the html return page and the e-mail message.
  129.       # Also determine required fields.
  130.  
  131.       if ($name eq 'recipient' ||
  132.       $name eq 'subject' ||
  133.       $name eq 'email' ||
  134.       $name eq 'realname' ||
  135.       $name eq 'redirect' ||
  136.       $name eq 'bgcolor' ||
  137.       $name eq 'background' ||
  138.       $name eq 'link_color' ||
  139.       $name eq 'vlink_color' ||
  140.           $name eq 'text_color' ||
  141.          $name eq 'alink_color' ||
  142.       $name eq 'title' ||
  143.       $name eq 'sort' ||
  144.       $name eq 'print_config' ||
  145.       $name eq 'return_link_title' ||
  146.       $name eq 'return_link_url' && ($value)) {
  147.          
  148.      $CONFIG{$name} = $value;
  149.       }
  150.       elsif ($name eq 'required') {
  151.          @required = split(/,/,$value);
  152.       }
  153.       elsif ($name eq 'env_report') {
  154.          @env_report = split(/,/,$value);
  155.       }
  156.       else {
  157.          if ($FORM{$name} && ($value)) {
  158.         $FORM{$name} = "$FORM{$name}, $value";
  159.      }
  160.          elsif ($value) {
  161.             $FORM{$name} = $value;
  162.          }
  163.       }
  164.    }
  165. }
  166.  
  167. sub check_required {
  168.  
  169.    foreach $require (@required) {
  170.       if ($require eq 'recipient' ||
  171.           $require eq 'subject' ||
  172.           $require eq 'email' ||
  173.           $require eq 'realname' ||
  174.           $require eq 'redirect' ||
  175.           $require eq 'bgcolor' ||
  176.           $require eq 'background' ||
  177.           $require eq 'link_color' ||
  178.           $require eq 'vlink_color' ||
  179.           $require eq 'alink_color' ||
  180.           $require eq 'text_color' ||
  181.       $require eq 'sort' ||
  182.           $require eq 'title' ||
  183.           $require eq 'print_config' ||
  184.           $require eq 'return_link_title' ||
  185.           $require eq 'return_link_url') {
  186.  
  187.          if (!($CONFIG{$require}) || $CONFIG{$require} eq ' ') {
  188.             push(@ERROR,$require);
  189.          }
  190.       }
  191.       elsif (!($FORM{$require}) || $FORM{$require} eq ' ') {
  192.          push(@ERROR,$require);
  193.       }
  194.    }
  195.  
  196.    if (@ERROR) {
  197.       &error('missing_fields', @ERROR);
  198.    }
  199.  
  200. }
  201.  
  202. sub return_html {
  203.  
  204.    if ($CONFIG{'redirect'} =~ /http\:\/\/.*\..*/) {
  205.  
  206.       # If the redirect option of the form contains a valid url,
  207.       # print the redirectional location header.
  208.  
  209.       print "Location: $CONFIG{'redirect'}\n\n";
  210.    }
  211.    else {
  212.  
  213.       print "Content-type: text/html\n\n";
  214.       print "<html>\n <head>\n";
  215.  
  216.       # Print out title of page
  217.       if ($CONFIG{'title'}) {
  218.      print "  <title>$CONFIG{'title'}</title>\n";
  219.       }
  220.       else {
  221.          print "  <title>Thank You</title>\n";
  222.       }
  223.  
  224.       print " </head>\n <body";
  225.  
  226.       # Get Body Tag Attributes
  227.       &body_attributes;
  228.  
  229.       # Close Body Tag
  230.       print ">\n  <center>\n";
  231.  
  232.       if ($CONFIG{'title'}) {
  233.          print "   <h1>$CONFIG{'title'}</h1>\n";
  234.       }
  235.       else {
  236.          print "   <h1>Thank You For Filling Out This Form</h1>\n";
  237.       }
  238.       print "</center>\n";
  239.  
  240.       print "Below is what you submitted to $CONFIG{'recipient'} on ";
  241.       print "$date<p><hr size=7 width=75\%><p>\n";
  242.  
  243.       if ($CONFIG{'sort'} eq 'alphabetic') {
  244.          foreach $key (sort keys %FORM) {
  245.             # Print the name and value pairs in FORM array to html.
  246.             print "<b>$key:</b> $FORM{$key}<p>\n";
  247.          }
  248.       }
  249.       elsif ($CONFIG{'sort'} =~ /^order:.*,.*/) {
  250.          $sort_order = $CONFIG{'sort'};
  251.          $sort_order =~ s/order://;
  252.          @sorted_fields = split(/,/, $sort_order);
  253.          foreach $sorted_field (@sorted_fields) {
  254.             # Print the name and value pairs in FORM array to html.
  255.             if ($FORM{$sorted_field}) {
  256.                print "<b>$sorted_field:</b> $FORM{$sorted_field}<p>\n";
  257.          }
  258.          }
  259.       }
  260.       else {
  261.          foreach $key (keys %FORM) {
  262.             # Print the name and value pairs in FORM array to html.
  263.             print "<b>$key:</b> $FORM{$key}<p>\n";
  264.          }
  265.       }
  266.  
  267.       print "<p><hr size=7 width=75%><p>\n";
  268.  
  269.       # Check for a Return Link
  270.       if ($CONFIG{'return_link_url'} =~ /http\:\/\/.*\..*/ && $CONFIG{'return_link_title'}) {
  271.          print "<ul>\n";
  272.          print "<li><a href=\"$CONFIG{'return_link_url'}\">$CONFIG{'return_link_title'}</a>\n";
  273.          print "</ul>\n";
  274.       }
  275.       print "</body>\n</html>";
  276.    }
  277. }
  278.  
  279. sub send_mail {
  280.    # Open The Mail Program
  281.  
  282.    open(MAIL,"|$mailprog -t");
  283.  
  284.    print MAIL "To: $CONFIG{'recipient'}\n";
  285.    print MAIL "From: $CONFIG{'email'} ($CONFIG{'realname'})\n";
  286.  
  287.    # Check for Message Subject
  288.    if ($CONFIG{'subject'}) {
  289.       print MAIL "Subject: $CONFIG{'subject'}\n\n";
  290.    }
  291.    else {
  292.       print MAIL "Subject: WWW Form Submission\n\n";
  293.    }
  294.  
  295.    print MAIL "Below is the result of your feedback form.  It was ";
  296.    print MAIL "submitted by $CONFIG{'realname'} ($CONFIG{'email'}) on ";
  297.    print MAIL "$date\n";
  298.    print MAIL "---------------------------------------------------------------------------\n\n";
  299.  
  300.    if ($CONFIG{'print_config'}) {
  301.       @print_config = split(/,/,$CONFIG{'print_config'});
  302.       foreach $print_config (@print_config) {
  303.          if ($CONFIG{$print_config}) {
  304.             print MAIL "$print_config: $CONFIG{$print_config}\n\n";
  305.          }
  306.       }
  307.    }
  308.  
  309.    if ($CONFIG{'sort'} eq 'alphabetic') {
  310.       foreach $key (sort keys %FORM) {
  311.          # Print the name and value pairs in FORM array to mail.
  312.          print MAIL "$key: $FORM{$key}\n\n";
  313.       }
  314.    }
  315.    elsif ($CONFIG{'sort'} =~ /^order:.*,.*/) {
  316.       $CONFIG{'sort'} =~ s/order://;
  317.       @sorted_fields = split(/,/, $CONFIG{'sort'});
  318.       foreach $sorted_field (@sorted_fields) {
  319.          # Print the name and value pairs in FORM array to mail.
  320.          if ($FORM{$sorted_field}) {
  321.             print MAIL "$sorted_field: $FORM{$sorted_field}\n\n";
  322.          }
  323.       }
  324.    }
  325.    else {
  326.       foreach $key (keys %FORM) {
  327.          # Print the name and value pairs in FORM array to html.
  328.             print MAIL "$key: $FORM{$key}\n\n";
  329.       }
  330.    }
  331.  
  332.    print MAIL "---------------------------------------------------------------------------\n";
  333.  
  334.    # Send Any Environment Variables To Recipient.
  335.    foreach $env_report (@env_report) {
  336.       print MAIL "$env_report: $ENV{$env_report}\n";
  337.    }
  338.  
  339.    close (MAIL);
  340. }
  341.  
  342. sub error {
  343.  
  344.    ($error,@error_fields) = @_;
  345.  
  346.    print "Content-type: text/html\n\n";
  347.  
  348.    if ($error eq 'bad_referer') {
  349.       print "<html>\n <head>\n  <title>Bad Referrer - Access Denied</title>\n </head>\n";
  350.       print " <body>\n  <center>\n   <h1>Bad Referrer - Access Denied</h1>\n  </center>\n";
  351.       print "The form that is trying to use this <a href=\"http://www.worldwidemart.com/scripts/\">FormMail Program</a>\n";
  352.       print "resides at: $ENV{'HTTP_REFERER'}, which is not allowed to access this cgi script.<p>\n";
  353.       print "Sorry!\n";
  354.       print "</body></html>\n";
  355.    }
  356.  
  357.    elsif ($error eq 'request_method') {
  358.       print "<html>\n <head>\n  <title>Error: Request Method</title>\n </head>\n";
  359.       print "</head>\n <body";
  360.  
  361.       # Get Body Tag Attributes
  362.       &body_attributes;
  363.  
  364.       # Close Body Tag
  365.       print ">\n <center>\n\n";
  366.  
  367.       print "   <h1>Error: Request Method</h1>\n  </center>\n\n";
  368.       print "The Request Method of the Form you submitted did not match\n";
  369.       print "either GET or POST.  Please check the form, and make sure the\n";
  370.       print "method= statement is in upper case and matches GET or POST.\n";
  371.       print "<p><hr size=7 width=75%><p>\n";
  372.       print "<ul>\n";
  373.       print "<li><a href=\"$ENV{'HTTP_REFERER'}\">Back to the Submission Form</a>\n";
  374.       print "</ul>\n";
  375.       print "</body></html>\n";
  376.    }
  377.  
  378.    elsif ($error eq 'missing_fields') {
  379.  
  380.       print "<html>\n <head>\n  <title>Error: Blank Fields</title>\n </head>\n";
  381.       print " </head>\n <body";
  382.       
  383.       # Get Body Tag Attributes
  384.       &body_attributes;
  385.          
  386.       # Close Body Tag
  387.       print ">\n  <center>\n";
  388.  
  389.       print "   <h1>Error: Blank Fields</h1>\n\n";
  390.       print "The following fields were left blank in your submission form:<p>\n";
  391.  
  392.       # Print Out Missing Fields in a List.
  393.       print "<ul>\n";
  394.       foreach $missing_field (@error_fields) {
  395.          print "<li>$missing_field\n";
  396.       }
  397.       print "</ul>\n";
  398.  
  399.       # Provide Explanation for Error and Offer Link Back to Form.
  400.       print "<p><hr size=7 width=75\%><p>\n";
  401.       print "These fields must be filled out before you can successfully submit\n";
  402.       print "the form.  Please return to the <a href=\"$ENV{'HTTP_REFERER'}\">Fill Out Form</a> and try again.\n";
  403.       print "</body></html>\n";
  404.    }
  405.    exit;
  406. }
  407.  
  408. sub body_attributes {
  409.    # Check for Background Color
  410.    if ($CONFIG{'bgcolor'}) {
  411.       print " bgcolor=\"$CONFIG{'bgcolor'}\"";
  412.    }
  413.  
  414.    # Check for Background Image
  415.    if ($CONFIG{'background'} =~ /http\:\/\/.*\..*/) {
  416.       print " background=\"$CONFIG{'background'}\"";
  417.    }
  418.  
  419.    # Check for Link Color
  420.    if ($CONFIG{'link_color'}) {
  421.       print " link=\"$CONFIG{'link_color'}\"";
  422.    }
  423.  
  424.    # Check for Visited Link Color
  425.    if ($CONFIG{'vlink_color'}) {   
  426.       print " vlink=\"$CONFIG{'vlink_color'}\"";
  427.    }
  428.  
  429.    # Check for Active Link Color
  430.    if ($CONFIG{'alink_color'}) {
  431.       print " alink=\"$CONFIG{'alink_color'}\"";
  432.    }
  433.  
  434.    # Check for Body Text Color
  435.    if ($CONFIG{'text_color'}) {
  436.       print " text=\"$CONFIG{'text_color'}\"";
  437.    }
  438. }
  439.