Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > guestbook.cgi :p

Reply
Thread Tools

guestbook.cgi :p

 
 
john.swilting
Guest
Posts: n/a
 
      03-02-2007
#!/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;
}

 
Reply With Quote
 
 
 
 
john.swilting
Guest
Posts: n/a
 
      03-02-2007
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.
 
Reply With Quote
 
 
 
 
john.swilting
Guest
Posts: n/a
 
      03-02-2007
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
 
Reply With Quote
 
anno4000@radom.zrz.tu-berlin.de
Guest
Posts: n/a
 
      03-03-2007
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
 
Reply With Quote
 
RedGrittyBrick
Guest
Posts: n/a
 
      03-03-2007
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"
 
Reply With Quote
 
krakle@visto.com
Guest
Posts: n/a
 
      03-03-2007
On Mar 2, 5:46 am, "john.swilting" <john.swilt...@wanadoo.fr> 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;

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off




Advertisments