in reply to Some suggestions on coding style - a chance to critique
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.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Re: Some suggestions on coding style - a chance to critique
by emilford (Friar) on Jun 26, 2002 at 22:47 UTC | |
by Juerd (Abbot) on Jun 26, 2002 at 23:03 UTC | |
by Fastolfe (Vicar) on Jun 30, 2002 at 04:55 UTC | |
by Juerd (Abbot) on Jun 30, 2002 at 13:48 UTC |