home *** CD-ROM | disk | FTP | other *** search
/ Internet 1996 World Exposition / park.org.s3.amazonaws.com.7z / park.org.s3.amazonaws.com / Guests / GreekMarble / scripts / formmail.pl next >
Perl Script  |  2017-09-21  |  14KB  |  440 lines

  1. #!/usr/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 = ('www.worldwidemart.com','worldwidemart.com','206.31.72.203');
  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 "<a href=\"http://www.worldwidemart.com/scripts/formmail.shtml\">FormMail</a> Created by Matt Wright and can be found at <a href=\"http://www.worldwidemart.com/scripts/\">Matt's Script Archive</a>.\n";
  276.       print "</body>\n</html>";
  277.    }
  278. }
  279.  
  280. sub send_mail {
  281.    # Open The Mail Program
  282.  
  283.    open(MAIL,"|$mailprog -t");
  284.  
  285.    print MAIL "To: $CONFIG{'recipient'}\n";
  286.    print MAIL "From: $CONFIG{'email'} ($CONFIG{'realname'})\n";
  287.  
  288.    # Check for Message Subject
  289.    if ($CONFIG{'subject'}) {
  290.       print MAIL "Subject: $CONFIG{'subject'}\n\n";
  291.    }
  292.    else {
  293.       print MAIL "Subject: WWW Form Submission\n\n";
  294.    }
  295.  
  296.    print MAIL "Below is the result of your feedback form.  It was ";
  297.    print MAIL "submitted by $CONFIG{'realname'} ($CONFIG{'email'}) on ";
  298.    print MAIL "$date\n";
  299.    print MAIL "---------------------------------------------------------------------------\n\n";
  300.  
  301.    if ($CONFIG{'print_config'}) {
  302.       @print_config = split(/,/,$CONFIG{'print_config'});
  303.       foreach $print_config (@print_config) {
  304.          if ($CONFIG{$print_config}) {
  305.             print MAIL "$print_config: $CONFIG{$print_config}\n\n";
  306.          }
  307.       }
  308.    }
  309.  
  310.    if ($CONFIG{'sort'} eq 'alphabetic') {
  311.       foreach $key (sort keys %FORM) {
  312.          # Print the name and value pairs in FORM array to mail.
  313.          print MAIL "$key: $FORM{$key}\n\n";
  314.       }
  315.    }
  316.    elsif ($CONFIG{'sort'} =~ /^order:.*,.*/) {
  317.       $CONFIG{'sort'} =~ s/order://;
  318.       @sorted_fields = split(/,/, $CONFIG{'sort'});
  319.       foreach $sorted_field (@sorted_fields) {
  320.          # Print the name and value pairs in FORM array to mail.
  321.          if ($FORM{$sorted_field}) {
  322.             print MAIL "$sorted_field: $FORM{$sorted_field}\n\n";
  323.          }
  324.       }
  325.    }
  326.    else {
  327.       foreach $key (keys %FORM) {
  328.          # Print the name and value pairs in FORM array to html.
  329.             print MAIL "$key: $FORM{$key}\n\n";
  330.       }
  331.    }
  332.  
  333.    print MAIL "---------------------------------------------------------------------------\n";
  334.  
  335.    # Send Any Environment Variables To Recipient.
  336.    foreach $env_report (@env_report) {
  337.       print MAIL "$env_report: $ENV{$env_report}\n";
  338.    }
  339.  
  340.    close (MAIL);
  341. }
  342.  
  343. sub error {
  344.  
  345.    ($error,@error_fields) = @_;
  346.  
  347.    print "Content-type: text/html\n\n";
  348.  
  349.    if ($error eq 'bad_referer') {
  350.       print "<html>\n <head>\n  <title>Bad Referrer - Access Denied</title>\n </head>\n";
  351.       print " <body>\n  <center>\n   <h1>Bad Referrer - Access Denied</h1>\n  </center>\n";
  352.       print "The form that is trying to use this <a href=\"http://www.worldwidemart.com/scripts/\">FormMail Program</a>\n";
  353.       print "resides at: $ENV{'HTTP_REFERER'}, which is not allowed to access this cgi script.<p>\n";
  354.       print "Sorry!\n";
  355.       print "</body></html>\n";
  356.    }
  357.  
  358.    elsif ($error eq 'request_method') {
  359.       print "<html>\n <head>\n  <title>Error: Request Method</title>\n </head>\n";
  360.       print "</head>\n <body";
  361.  
  362.       # Get Body Tag Attributes
  363.       &body_attributes;
  364.  
  365.       # Close Body Tag
  366.       print ">\n <center>\n\n";
  367.  
  368.       print "   <h1>Error: Request Method</h1>\n  </center>\n\n";
  369.       print "The Request Method of the Form you submitted did not match\n";
  370.       print "either GET or POST.  Please check the form, and make sure the\n";
  371.       print "method= statement is in upper case and matches GET or POST.\n";
  372.       print "<p><hr size=7 width=75%><p>\n";
  373.       print "<ul>\n";
  374.       print "<li><a href=\"$ENV{'HTTP_REFERER'}\">Back to the Submission Form</a>\n";
  375.       print "</ul>\n";
  376.       print "</body></html>\n";
  377.    }
  378.  
  379.    elsif ($error eq 'missing_fields') {
  380.  
  381.       print "<html>\n <head>\n  <title>Error: Blank Fields</title>\n </head>\n";
  382.       print " </head>\n <body";
  383.       
  384.       # Get Body Tag Attributes
  385.       &body_attributes;
  386.          
  387.       # Close Body Tag
  388.       print ">\n  <center>\n";
  389.  
  390.       print "   <h1>Error: Blank Fields</h1>\n\n";
  391.       print "The following fields were left blank in your submission form:<p>\n";
  392.  
  393.       # Print Out Missing Fields in a List.
  394.       print "<ul>\n";
  395.       foreach $missing_field (@error_fields) {
  396.          print "<li>$missing_field\n";
  397.       }
  398.       print "</ul>\n";
  399.  
  400.       # Provide Explanation for Error and Offer Link Back to Form.
  401.       print "<p><hr size=7 width=75\%><p>\n";
  402.       print "These fields must be filled out before you can successfully submit\n";
  403.       print "the form.  Please return to the <a href=\"$ENV{'HTTP_REFERER'}\">Fill Out Form</a> and try again.\n";
  404.       print "</body></html>\n";
  405.    }
  406.    exit;
  407. }
  408.  
  409. sub body_attributes {
  410.    # Check for Background Color
  411.    if ($CONFIG{'bgcolor'}) {
  412.       print " bgcolor=\"$CONFIG{'bgcolor'}\"";
  413.    }
  414.  
  415.    # Check for Background Image
  416.    if ($CONFIG{'background'} =~ /http\:\/\/.*\..*/) {
  417.       print " background=\"$CONFIG{'background'}\"";
  418.    }
  419.  
  420.    # Check for Link Color
  421.    if ($CONFIG{'link_color'}) {
  422.       print " link=\"$CONFIG{'link_color'}\"";
  423.    }
  424.  
  425.    # Check for Visited Link Color
  426.    if ($CONFIG{'vlink_color'}) {   
  427.       print " vlink=\"$CONFIG{'vlink_color'}\"";
  428.    }
  429.  
  430.    # Check for Active Link Color
  431.    if ($CONFIG{'alink_color'}) {
  432.       print " alink=\"$CONFIG{'alink_color'}\"";
  433.    }
  434.  
  435.    # Check for Body Text Color
  436.    if ($CONFIG{'text_color'}) {
  437.       print " text=\"$CONFIG{'text_color'}\"";
  438.    }
  439. }
  440.