Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

comment on

( [id://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":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others chilling in the Monastery: (6)
As of 2024-03-28 13:40 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found