Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Comment on

( #3333=superdoc: print w/ replies, xml ) Need Help??

For something this simple, I would not use subroutines. It only makes things harder to read and harder to maintain if you use subs for code that gets executed only once. A rule of thumb: subroutines should return something and the return value must be useful.

You let the web user define the path to the sendmail program. That is EXTREMELY INSECURE. Imagine what would happen if someone has rm -rf /; as $formdata{sendmail}.

Here's a complete (UNTESTED) rewrite of your script, with some suggestions and remarks:


#!/usr/bin/perl -w

# This script has never been tested

use CGI;
use strict;

# Suggested improvement: use a hash for these

# URL to redirect to after form submission
my $redirect = 'http://www.yoursite.com/';

# Path to the sendmail executable
my $sendmail = '/usr/sbin/sendmail';

# Default subject
my $subject = 'Form Submission Results';

# List of recipients
my @recipients = ('webmaster@yoursite.com');

# Required fields
my @required = ();


my $cgi = CGI->new();

my %formdata = $cgi->Vars;


sub set {
    my ($thing) = @_;
    return defined $thing and length $thing ? 1 : 0;
}

@required   = split /,/, $formdata{required}  if set $formdata{required};
$redirect   =            $formdata{redirect}  if set $formdata{redirect};
#  !!! NEVER EVER LET THE USER CHOOSE THE PROGRAM THAT GETS EXECUTED !!!
$subject    =            $formdata{subject}   if set $formdata{subject};
@recipients = split /,/, $formdata{recipient} if set $formdata{recipient};

CHECK: {
    my @errors;
    push @errors, 'Redirection URL seems to be invalid'
        unless $redirect =~ m!^http://!;
    for (@recipients) {
        push @errors, "E-Mailaddress $_ seems to be invalid"
            unless /^[\w.-]+\@([\w.-]+\z/;
    }
    for (@required) {
        push @errors, "Field '$_' is a required field" unless set $formdata{$_};
    }
    last CHECK unless @errors;

    # Suggested improvement: a templating module
    print "Content-Type: text/html\n\n";
    print '<html><body><h1>Errors:</h1><ul>', map "<li>$_</li>", @errors;
    print '</ul></body></html>';

    exit;
}


# E-Mail already has a Date: header. Why bother having your own?

delete @formdata{ qw/recipient subject required redirect sendmail/ };

# Suggested improvement: an E-mail module
for my $recipient (@recipients) {
    open my $mail, '|-', $sendmail, '-t', '-oi' or die "Couldn't start sendmail: $!";
    print $mail join("\n",
        "To: $recipient",
        "From: webmaster\@yoursite.com",
        "Subject: $subject",
        '',
        "Remote host: $ENV{REMOTE_HOST} ($ENV{REMOTE_ADDR})",
        "Server: $ENV{SERVER_NAME}",
        '',
        map "$_: $formdata{$_}\n", keys %formdata
    );
}

$cgi->redirect( -url => $redirect );

As you can see, there's still a lot of room for improvement.

Note for PM regulars: no, I still don't like CGI.pm, but I thought it'd be a lot better than the current solution.

- Yes, I reinvent wheels.
- Spam: Visit eurotraQ.


In reply to Re: Some suggestions on coding style - a chance to critique by Juerd
in thread Some suggestions on coding style - a chance to critique by emilford

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • Outside of code tags, you may need to use entities for some characters:
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others rifling through the Monastery: (7)
    As of 2014-08-23 02:05 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      The best computer themed movie is:











      Results (171 votes), past polls