Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Re: Some suggestions on coding style - a chance to critique

by Juerd (Abbot)
on Jun 26, 2002 at 22:18 UTC ( #177564=note: print w/ replies, xml ) Need Help??


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.


Comment on Re: Some suggestions on coding style - a chance to critique
Download Code
Re: Re: Some suggestions on coding style - a chance to critique
by emilford (Friar) on Jun 26, 2002 at 22:47 UTC
    Thanks for the tip on allowing someone to specify their own sendmail. I didn't realize the huge security issue involved with that. I'll be sure to make changes on that asap.

    return defined $thing and length $thing ? 1 : 0;

    What exactly does this do? Is this one of those awkward ways to write an if statement as in old-style C? What is the purpose of the ? 1 : 0?

    Finally, what do you have against CGI.pm? I'm looking for both reasons to use it and reasons not to use it. You obvioulsy have a strong opinion in one direction.

    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.

      Is this one of those awkward ways to write an if statement as in old-style C? What is the purpose of the ? 1 : 0?

      It's the tenary operator. Very C-ish, yes.
      The ? 1 : 0 isn't really needed here, as set() is only used in boolean context, but this clarifies the return value: true or false. Sometimes I write  !! $foo (double boolean negation), sometimes I write  $foo ? 1 : 0 , and sometimes just  $foo .

      Finally, what do you have against CGI.pm? I'm looking for both reasons to use it and reasons not to use it. You obvioulsy have a strong opinion in one direction.

      The short version:
      It is slow (huge), has awful parsing (I just don't like it), a very annoying hybrid interface and too much junk code that is there for (backwards) compatibility. And it doesn't use strict, and has many globals.

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

        CGI::Lite is a speedier version you may find useful. Reinventing the wheel is fine if there's a need to do so. An awful lot of work and testing went into that wheel in the first place, though, so keep that in mind when you write your replacement. And please release it so the rest of us can take advantage of what you've done.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://177564]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (6)
As of 2014-08-29 08:04 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (277 votes), past polls