guestbook.cgi :p

Discussion in 'Perl Misc' started by john.swilting, Mar 2, 2007.

  1. #!/usr/bin/perl -Tw
    use strict;
    use CGI qw/:standard escapeHTML/;
    use Fcntl qw/:flock/;
    $|++;

    # Config
    my $GUESTBOOK = '/var/www/cgi-bin/guestbook.txt';
    my $TITLE = 'Guestbook'; # Title of guestbook
    my $MAX_MSGS = 5; # Maximum number of messages displayed
    my $MAX_NAME = 50; # Maximum length of name field
    my $MAX_EMAIL = 50; # Maximum length of email field
    my $MAX_COMMENTS = 300; # Maximum length of comments field
    # End Config

    # Print header
    print header,
    start_html(-title=>$TITLE, -bgcolor=>'white'),
    h1($TITLE);

    # Get action
    my $action = param('action');

    # Check action
    if ($action =~ /^sign/i) {
    # Sign guestbook
    sign_guestbook();
    } elsif ($action =~ /^view/i) {
    # View guestbook
    view_guestbook();
    } else {
    print_form();
    }

    # End html
    print end_html;

    #######################################

    sub print_form {
    print hr,
    start_form,
    '<STRONG>Name: </STRONG>',
    br,
    textfield(-name=>'name', -size=>50),
    br,
    '<STRONG>E-Mail: </STRONG>',
    br,
    textfield(-name=>'email', -size=>50),
    br,
    '<STRONG>Comments: </STRONG>',
    br,
    textarea(-name=>'comments', -rows=>10,
    -columns=>50, -wrap=>1),
    br,
    submit(-name=>'action', -value=>'Sign Guestbook'),
    submit(-name=>'action', -value=>'View Guestbook'),
    reset,
    end_form;

    }

    sub sign_guestbook {
    my $time = localtime;
    my $name = param('name');
    my $email = param('email');
    my $comments = param('comments');

    # Check that name was entered
    if ($name eq '' or $name =~ /^\s+$/) {
    print_error('You must enter a name');
    }

    # Check lenghts of user input
    $name = substr($name, 0, $MAX_NAME);
    $email = substr($email, 0, $MAX_EMAIL);
    $comments = substr($comments, 0, $MAX_COMMENTS);

    # Remove leading/trailing white space
    $comments =~ s/^\s+//;
    $comments =~ s/\s+$//;

    # Escape HTML
    $name = escapeHTML($name);
    $email = escapeHTML($email);
    $comments = escapeHTML($comments);

    # Deal with line breaks
    $comments =~ s/(?:\015\012?|\012)/<BR>/g;

    open(FILE, ">>$GUESTBOOK") or
    die "Can't open $GUESTBOOK: $!\n";
    flock(FILE, LOCK_EX); # Exclusive lock for writing

    print FILE $time, ':::', $name, ':::', $email, ':::', $comments, "\n";

    flock(FILE, LOCK_UN); # Unlock the file
    close FILE;

    # Check size of message file
    max_msgs();

    my $script = url();
    print hr,
    'Thank you for signing my guestbook',
    p,
    a({-href=>"${script}?action=view"},'View Guestbook');

    }

    sub view_guestbook {
    my $script = url();

    print a({-href=>$script}, 'Sign Guestbook'),
    hr;

    # Check size of message file
    max_msgs();

    # Read message file
    open(FILE, "$GUESTBOOK") or
    die "Cannot open $GUESTBOOK: $!\n";
    flock(FILE, LOCK_SH); # Shared lock for reading

    my @messages = <FILE>;

    flock(FILE, LOCK_UN); # Unlock the file
    close (FILE);

    @messages = reverse (@messages);
    foreach my $item (@messages) {
    my($time, $name, $email, $comments) = split(':::', $item);

    # Format fields
    my $f_name = "<STRONG>$name</STRONG>";
    my $f_email = "<A HREF=mailto:$email>$email</A>";

    # Output a record
    print "$time - $f_name - $f_email",
    p(blockquote($comments)),
    hr;
    }

    }

    sub max_msgs {
    # If more than MAX_MSGS messages, delete oldest

    # Read message file
    open(FILE, "$GUESTBOOK") or
    die "Cannot open $GUESTBOOK: $!\n";
    flock(FILE, LOCK_SH); # Shared lock for reading

    my @messages = <FILE>;

    flock(FILE, LOCK_UN); # Unlock the file
    close (FILE);


    if(@messages > $MAX_MSGS) {
    open(FILE, ">$GUESTBOOK") or
    die "Cannot open $GUESTBOOK: $!\n";
    flock(FILE, LOCK_EX); # Exclusive lock for writing

    shift @messages while @messages > $MAX_MSGS;

    print FILE @messages;

    flock(FILE, LOCK_UN); # Unlock the file
    close FILE;
    }


    }

    sub print_error {
    my $error = shift;
    my $script = url();
    print hr,
    h2('Error'),
    p($error),
    a({href=>$script}, 'Try Again'),
    end_html;
    die $error;
    }
     
    john.swilting, Mar 2, 2007
    #1
    1. Advertisements

  2. I post. it is my code. you can say to me what you think that will make me
    progress.
     
    john.swilting, Mar 2, 2007
    #2
    1. Advertisements

  3. john.swilting wrote:

    I read posts with small images very to complicate. I cannot do it. somebody
    knows
     
    john.swilting, Mar 2, 2007
    #3
  4. john.swilting

    anno4000 Guest

    [snip more somewhat dated (5.6.1) but reasonably well-written Perl]
    John, don't lie! You know neither enough Perl nor English to be the
    author of that code.

    Anno
     
    anno4000, Mar 3, 2007
    #4
  5. Indeed, Googling for the MAX_MSGS line of code suggests it came from
    http://www.telegard.net/

    In http://www.telegard.net/tgfaq.html Tim Strike (apparently the current
    maintainer or owner of this code) says

    "I have no intentions of releasing any of the source code that I produce"

    "The Telegard 2.5g source code is floating around on various BBS and
    internet sites around the world. ... The legality of this source code is
    also in question"
     
    RedGrittyBrick, Mar 3, 2007
    #5
  6. john.swilting

    krakle Guest

    I think GuestBooks are dumb. The only 'people' who love to sign them
    are housewives who own 6+ cats, 13 year old girls, spambots, and
    probably Tad.

    I also think flat file databases are a thing of 1997. They are slow,
    less than effective and productive, and allow for to many security
    holes and bugs.

    I also think CGI.pm is worthless when it comes to printing HTML. John,
    get up with the times! Check out some template modules that enables
    the templates to be cached and ready on the fly! These days are all
    about REAL database driven sites that use templates. Check out MayPole
    or Catalyst.

    Oh and... don't waste your time with guest book scripts!

    Oh and... use English;
     
    krakle, Mar 3, 2007
    #6
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.
Similar Threads
There are no similar threads yet.
Loading...