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. Advertising

  2. john.swilting wrote:

    > #!/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;
    > }

    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. Advertising

  3. Re: guestbook.cgi :p :p

    john.swilting wrote:


    > I post. it is my code. you can say to me what you think that will make me
    > progress.

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

    -berlin.de Guest

    john.swilting <> wrote in comp.lang.perl.misc:
    > john.swilting wrote:
    >
    > > #!/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


    [snip more somewhat dated (5.6.1) but reasonably well-written Perl]

    > I post. it is my code. you can say to me what you think that will make me
    > progress.


    John, don't lie! You know neither enough Perl nor English to be the
    author of that code.

    Anno
     
    -berlin.de, Mar 3, 2007
    #4
  5. -berlin.de wrote:
    > john.swilting <> wrote in comp.lang.perl.misc:
    >> john.swilting wrote:
    >>
    >>> #!/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

    >
    > [snip more somewhat dated (5.6.1) but reasonably well-written Perl]
    >
    >> I post. it is my code. you can say to me what you think that will make me
    >> progress.

    >
    > John, don't lie! You know neither enough Perl nor English to be the
    > author of that code.
    >


    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

    Guest

    On Mar 2, 5:46 am, "john.swilting" <> wrote:
    > john.swilting wrote:
    > I post. it is my code. you can say to me what you think that will make me
    > progress.


    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;
     
    , Mar 3, 2007
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. John ©
    Replies:
    5
    Views:
    4,916
    Skeleton Man
    Jun 24, 2004
  2. Oded Dror

    Guestbook Problems

    Oded Dror, Jan 31, 2006, in forum: ASP .Net
    Replies:
    1
    Views:
    1,669
    Juan T. Llibre
    Jan 31, 2006
  3. Johan

    guestbook

    Johan, Jul 22, 2003, in forum: HTML
    Replies:
    3
    Views:
    12,916
    Davmagic com
    Jul 22, 2003
  4. NK

    guestbook results

    NK, Aug 31, 2003, in forum: HTML
    Replies:
    3
    Views:
    1,388
    David Dorward
    Sep 1, 2003
  5. UkJay

    cgi-bin/guestbook.pl

    UkJay, Aug 15, 2006, in forum: Perl Misc
    Replies:
    4
    Views:
    3,079
    John Bokma
    Aug 16, 2006
Loading...

Share This Page