Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

bulletin board

by Anonymous Monk
on Dec 05, 2000 at 08:22 UTC ( #44937=perlquestion: print w/ replies, xml ) Need Help??
Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

I made a script that should act as a bulletin board, but doesn't seem to work right. Could somebody check my script for any problems, I'd really appreciate it. Here's the script:
#!/usr/bin/perl require "cgi-lib.pl"; require "subroutines.pl"; &ReadParse; print &PrintHeader; $url="http://soccermb.netfirms.com/board/bb.htm"; $bbfile1 = "$ENV{'DOCUMENT_ROOT'}/www/board/box1.htm"; $bbfile2 = "$ENV{'DOCUMENT_ROOT'}/www/board/box2.htm"; $bbfile3 = "$ENV{'DOCUMENT_ROOT'}/www/board/box3.htm"; $bbfile4 = "$ENV{'DOCUMENT_ROOT'}/www/board/box4.htm"; unless (-e $bbfile1) { print <<"PrintTag"; <html><body> <h2>$bbfile1 not present.</h2> <p>Did you upload the file? If so, the server path to your box1.htm file is incorrect in your CGI program. Check with your hosting service to find the server path to your HTML files.</p> </body></html> PrintTag exit(0); } unless (-e $bbfile2) { print <<"PrintTag"; <html><body> <h2>$bbfile2 not present.</h2> <p>Did you upload the file? If so, the server path to your box1.htm file is incorrect in your CGI program. Check with your hosting service to find the server path to your HTML files.</p> </body></html> PrintTag exit(0); } unless (-e $bbfile3) { print <<"PrintTag"; <html><body> <h2>$bbfile3 not present.</h2> <p>Did you upload the file? If so, the server path to your box1.htm file is incorrect in your CGI program. Check with your hosting service to find the server path to your HTML files.</p> </body></html> PrintTag exit(0); } unless (-e $bbfile4) { print <<"PrintTag"; <html><body> <h2>$bbfile4 not present.</h2> <p>Did you upload the file? If so, the server path to your box1.htm file is incorrect in your CGI program. Check with your hosting service to find the server path to your HTML files.</p> </body></html> PrintTag exit(0); } $tempfile1="$ENV{'DOCUMENT_ROOT'}/www/board/bbtemp1.htm"; $tempfile2="$ENV{'DOCUMENT_ROOT'}/www/board/bbtemp2.htm"; $tempfile3="$ENV{'DOCUMENT_ROOT'}/www/board/bbtemp3.htm"; $tempfile4="$ENV{'DOCUMENT_ROOT'}/www/board/bbtemp4.htm"; if (($in{'visitor_name'} eq "") || ($in{'visitor_email_address'} eq "" +) || ($in{'radioset'} eq "") || ($in{'Comment'} eq "")) { print <<"PrintTag"; <html><body> <h2>Data Incomplete</h2> <p>Your request to send comments to the board has been rejected due to insufficient information. To properly send your comments, please fill out:</p> <ul> PrintTag if ($in{'visitor_name'} eq "") { print "\n<li> Your Name\n"; } if ($in{'visitor_email_address'} eq "") { print "\n<li> Your Email Address\n"; } if ($in{'radioset'} eq "") { print "\n<li> Your county\n"; } if ($in{'Comment'} eq "") { print "\n<li> Your comments\n"; } print "</ul>\n</p>\n</body>\n</html>\n"; exit(0); } #STOP HERE FOR NOW if (($in{'Comments'} =~ /damn/) || ($in{'Comments'} =~ /darn/) || ($in{'Comments'} =~ /dang/) || ($in{'Comments'} =~ /shit/) || ($in{'Comments'} =~ /basterd/) || ($in{'Comments'} =~ /fuck/) || ($in{'Comments'} =~ /fuckin'/) || ($in{'Comments'} =~ /fucking/) || ($in{'Comments'} =~ /freak/) || ($in{'Comments'} =~ /freakin'/) || ($in{'Comments'} =~ /freaking/) || ($in{'Comments'} =~ /bitch/) || ($in{'Comments'} =~ /bitchin'/) || ($in{'Comments'} =~ /bitching/)) { print<<"PrintTag"; <html><body> <h2>Request denied-improper language.</h2> </body></html> PrintTag exit(0); } $in{'visitor_name'} =~ s/\</&lt;/g; $in{'visitor_name'} =~ s/\>/&gt;/g; $in{'visitor_email_address'} =~ s/\</&lt;/g; $in{'visitor_email_address'} =~ s/\>/&gt;/g; $in{'Comment'} =~ s/\</&lt;/g; $in{'Comment'} =~ s/\>/&gt;/g; if (-e "lock.fil") { print <<"PrintTag"; <HTML> <BODY> <H2>Try again!</H2> <p> The bulletin board is in use. Please try again later. </p> </BODY> </HTML> PrintTag exit(0); } open(LOCKFILE, ">lock.fil"); #Start subroutines if ($in{'radioset'} eq "Kern" || "San Luis Obispo" || "Santa Barbara") { &box1 } if ($in{'radioset'} eq "Riverside" || "San Diego" || "Imperial") { &box4 } if ($in{'radioset'} eq "Orange" || "San Bernadino") { &box3 } if ($in{'radioset'} eq "Ventura" || "Los Angeles") { &box2 } #open(BBFILE,"$bbfile") || die "Can't find bulletin board.\n"; #@indata = <BBFILE>; #close(BBFILE); #open(TEMPFILE,">$tempfile") || die "Can't find temp file.\n"; #foreach $i (@indata) #{ #chop($i); #print TEMPFILE "$i\n"; #if ($i =~ /<!-- BEGINNING -->/i) #{ #print TEMPFILE "<p><a href=\"mailto:$in{'email'}\">"; #print TEMPFILE "$in{'name'}"; #print TEMPFILE "</a>"; #print TEMPFILE "<br>"; #print TEMPFILE "$in{'comments'}</p><hr>\n\n"; #} #} #refresh bulletin board #close(TEMPFILE); #rename("$bbfile", "$bbfile.old"); #rename("$tempfile", "$bbfile"); #close(LOCKFILE); #unlink("lock.fil"); #Thank you page #print <<"PrintTag"; #<html><body> #<h2>Bulletin Board Entry Accepted</h2> #<p>View <A HREF="$url">Bulletin Board</A></p> #</body></html> #PrintTag
And here's the subroutine script:
sub box1 { $url="http://soccermb.netfirms.com/board/box1.htm"; $bbfile1 = "$ENV{'DOCUMENT_ROOT'}/www/board/box1.htm"; $tempfile1="$ENV{'DOCUMENT_ROOT'}/www/board/bbtemp1.htm"; open(BBFILE,"$bbfile1") || die "Can't find bulletin board.\n"; @indata = <BBFILE>; close(BBFILE); open(TEMPFILE,">$tempfile1") || die "Can't find temp file.\n"; foreach $i (@indata) { chop($i); print TEMPFILE "$i\n"; if ($in{'radioset'} eq "Kern") { if ($i =~ /<!-- BEGINNING -->/i) { print TEMPFILE "<p><a href=\"mailto:$in{'visitor_email_address'}\"> +"; print TEMPFILE "$in{'visitor_name'}"; print TEMPFILE "</a>"; print TEMPFILE "<br>"; print TEMPFILE "$in{'Comment'}</p><hr>\n\n"; close(TEMPFILE); rename("$bbfile1", "$bbfile.old"); rename("$tempfile1", "$bbfile1"); close(LOCKFILE); unlink("lock.fil"); #Thank you page print <<"PrintTag"; <html><body> <h2>Bulletin Board Entry Accepted</h2> <p>View <A HREF="$url">Bulletin Board</A></p> </body></html> PrintTag } } } if ($in{'radioset'} eq "San Luis Obispo") { if ($i =~ /<!-- BEGINNING1 -->/i) { print TEMPFILE "<p><a href=\"mailto:$in{'visitor_email_address'}\"> +"; print TEMPFILE "$in{'visitor_name'}"; print TEMPFILE "</a>"; print TEMPFILE "<br>"; print TEMPFILE "$in{'Comment'}</p><hr>\n\n"; close(TEMPFILE); rename("$bbfile1", "$bbfile.old"); rename("$tempfile1", "$bbfile1"); close(LOCKFILE); unlink("lock.fil"); #Thank you page print <<"PrintTag"; <html><body> <h2>Bulletin Board Entry Accepted</h2> <p>View <A HREF="$url">Bulletin Board</A></p> </body></html> PrintTag } } if ($in{'radioset'} eq "Santa Barbara") { if ($i =~ /<!-- BEGINNING3 -->/i) { print TEMPFILE "<p><a href=\"mailto:$in{'visitor_email_address'}\"> +"; print TEMPFILE "$in{'visitor_name'}"; print TEMPFILE "</a>"; print TEMPFILE "<br>"; print TEMPFILE "$in{'Comment'}</p><hr>\n\n"; close(TEMPFILE); rename("$bbfile1", "$bbfile.old"); rename("$tempfile1", "$bbfile1"); close(LOCKFILE); unlink("lock.fil"); #Thank you page print <<"PrintTag"; <html><body> <h2>Bulletin Board Entry Accepted</h2> <p>View <A HREF="$url">Bulletin Board</A></p> </body></html> PrintTag } } } } #refresh bulletin board #close(TEMPFILE); #rename("$bbfile", "$bbfile.old"); #rename("$tempfile", "$bbfile"); #close(LOCKFILE); #unlink("lock.fil"); #Thank you page #print <<"PrintTag"; #<html><body> #<h2>Bulletin Board Entry Accepted</h2> #<p>View <A HREF="$url">Bulletin Board</A></p> #</body></html> #PrintTag sub box2 { $url2 = "http://soccermb.netfirms.com/board/box2.htm"; $bbfile2 = "$ENV{'DOCUMENT_ROOT'}/www/board/box2.htm"; $tempfile2="$ENV{'DOCUMENT_ROOT'}/www/board/bbtemp2.htm"; open(BBFILE,"$bbfile2") || die "Can't find bulletin board.\n"; @indata = <BBFILE>; close(BBFILE); open(TEMPFILE,">$tempfile2") || die "Can't find temp file.\n"; foreach $i (@indata) { chop($i); print TEMPFILE "$i\n"; if ($in{'radioset'} eq "Ventura") { if ($i =~ /<!-- BEGINNING -->/i) { print TEMPFILE "<p><a href=\"mailto:$in{'visitor_email_address'}\"> +"; print TEMPFILE "$in{'visitor_name'}"; print TEMPFILE "</a>"; print TEMPFILE "<br>"; print TEMPFILE "$in{'Comment'}</p><hr>\n\n"; close(TEMPFILE); rename("$bbfile2", "$bbfile.old"); rename("$tempfile2", "$bbfile2"); close(LOCKFILE); unlink("lock.fil"); #Thank you page print <<"PrintTag"; <html><body> <h2>Bulletin Board Entry Accepted</h2> <p>View <A HREF="$url">Bulletin Board</A></p> </body></html> PrintTag } } if ($in{'radioset'} eq "Los Angeles") { if ($i =~ /<!-- BEGINNING1 -->/i) { print TEMPFILE "<p><a href=\"mailto:$in{'visitor_email_address'}\"> +"; print TEMPFILE "$in{'visitor_name'}"; print TEMPFILE "</a>"; print TEMPFILE "<br>"; print TEMPFILE "$in{'Comment'}</p><hr>\n\n"; close(TEMPFILE); rename("$bbfile2", "$bbfile.old"); rename("$tempfile2", "$bbfile2"); close(LOCKFILE); unlink("lock.fil"); #Thank you page print <<"PrintTag"; <html><body> <h2>Bulletin Board Entry Accepted</h2> <p>View <A HREF="$url">Bulletin Board</A></p> </body></html> PrintTag } } } } sub box3 { $url3 = "http://soccermb.netfirms.com/board/box1.htm"; $bbfile3 = "$ENV{'DOCUMENT_ROOT'}/www/board/box3.htm"; $tempfile3="$ENV{'DOCUMENT_ROOT'}/www/board/bbtemp3.htm"; open(BBFILE,"$bbfile3") || die "Can't find bulletin board.\n"; @indata = <BBFILE>; close(BBFILE); open(TEMPFILE,">$tempfile3") || die "Can't find temp file.\n"; foreach $i (@indata) { chop($i); print TEMPFILE "$i\n"; if ($in{'radioset'} eq "Orange") { if ($i =~ /<!-- BEGINNING -->/i) { print TEMPFILE "<p><a href=\"mailto:$in{'visitor_email_address'}\"> +"; print TEMPFILE "$in{'visitor_name'}"; print TEMPFILE "</a>"; print TEMPFILE "<br>"; print TEMPFILE "$in{'Comment'}</p><hr>\n\n"; close(TEMPFILE); rename("$bbfile3", "$bbfile.old"); rename("$tempfile3", "$bbfile3"); close(LOCKFILE); unlink("lock.fil"); #Thank you page print <<"PrintTag"; <html><body> <h2>Bulletin Board Entry Accepted</h2> <p>View <A HREF="$url">Bulletin Board</A></p> </body></html> PrintTag } } if ($in{'radioset'} eq "San Bernadino") { if ($i =~ /<!-- BEGINNING1 -->/i) { print TEMPFILE "<p><a href=\"mailto:$in{'visitor_email_address'}\"> +"; print TEMPFILE "$in{'visitor_name'}"; print TEMPFILE "</a>"; print TEMPFILE "<br>"; print TEMPFILE "$in{'Comment'}</p><hr>\n\n"; close(TEMPFILE); rename("$bbfile3", "$bbfile.old"); rename("$tempfile3", "$bbfile3"); close(LOCKFILE); unlink("lock.fil"); #Thank you page print <<"PrintTag"; <html><body> <h2>Bulletin Board Entry Accepted</h2> <p>View <A HREF="$url">Bulletin Board</A></p> </body></html> PrintTag } } } } sub box4 { $url4 = "http://soccermb.netfirms.com/board/box4.htm" $bbfile4 = "$ENV{'DOCUMENT_ROOT'}/www/board/box4.htm"; $tempfile4="$ENV{'DOCUMENT_ROOT'}/www/board/bbtemp4.htm"; open(BBFILE,"$bbfile4") || die "Can't find bulletin board.\n"; @indata = <BBFILE>; close(BBFILE); open(TEMPFILE,">$tempfile4") || die "Can't find temp file.\n"; foreach $i (@indata) { chop($i); print TEMPFILE "$i\n"; if ($in{'radioset'} eq "Riverside") { if ($i =~ /<!-- BEGINNING -->/i) { print TEMPFILE "<p><a href=\"mailto:$in{'visitor_email_address'}\"> +"; print TEMPFILE "$in{'visitor_name'}"; print TEMPFILE "</a>"; print TEMPFILE "<br>"; print TEMPFILE "$in{'Comment'}</p><hr>\n\n"; close(TEMPFILE); rename("$bbfile4", "$bbfile.old"); rename("$tempfile4", "$bbfile4"); close(LOCKFILE); unlink("lock.fil"); #Thank you page print <<"PrintTag"; <html><body> <h2>Bulletin Board Entry Accepted</h2> <p>View <A HREF="$url">Bulletin Board</A></p> </body></html> PrintTag } } if ($in{'radioset'} eq "San Diego") { if ($i =~ /<!-- BEGINNING1 -->/i) { print TEMPFILE "<p><a href=\"mailto:$in{'visitor_email_address'}\"> +"; print TEMPFILE "$in{'visitor_name'}"; print TEMPFILE "</a>"; print TEMPFILE "<br>"; print TEMPFILE "$in{'Comment'}</p><hr>\n\n"; close(TEMPFILE); rename("$bbfile4", "$bbfile.old"); rename("$tempfile4", "$bbfile4"); close(LOCKFILE); unlink("lock.fil"); #Thank you page print <<"PrintTag"; <html><body> <h2>Bulletin Board Entry Accepted</h2> <p>View <A HREF="$url">Bulletin Board</A></p> </body></html> PrintTag } } if ($in{'radioset'} eq "Imperial") { if ($i =~ /<!-- BEGINNING3 -->/i) { print TEMPFILE "<p><a href=\"mailto:$in{'visitor_email_address'}\"> +"; print TEMPFILE "$in{'visitor_name'}"; print TEMPFILE "</a>"; print TEMPFILE "<br>"; print TEMPFILE "$in{'Comment'}</p><hr>\n\n"; close(TEMPFILE); rename("$bbfile4", "$bbfile.old"); rename("$tempfile4", "$bbfile4"); close(LOCKFILE); unlink("lock.fil"); #Thank you page print <<"PrintTag"; <html><body> <h2>Bulletin Board Entry Accepted</h2> <p>View <A HREF="$url">Bulletin Board</A></p> </body></html> PrintTag } } } } 1;

Comment on bulletin board
Select or Download Code
Re: bulletin board
by extremely (Priest) on Dec 05, 2000 at 08:36 UTC
    What doesn't it do? What does it do instead? Are there any errors you can report from the error log? What does the user see? Have you tried running the script from the command line (setting env variables you need first)? What was the result of that? Did you write this script yourself or "borrow" it? Have you tried "perl -cw script.pl" from the commandline to ensure that the script compiles correctly and without warnings?

    Oh yeah, "use strict;" and "use CGI;" and so on and so forth. "#!/usr/bin/perl -w" and

    --
    $you = new YOU;
    honk() if $you->love(perl)

Re (tilly) 1: bulletin board
by tilly (Archbishop) on Dec 05, 2000 at 08:44 UTC
    If you want good answers, you might wish to take the advice offered here.

    Aside from that, at a glance, you don't use strict, you are using cgi-lib.pl rather than CGI, your temp file handling has a ton of race conditions, your lock file handling means that if anything went wrong it would tend to randomly fall over.

    I don't know what specifically causes the (unspecified) symptoms that lead you to post, nor do I feel inclined to dig into it deeply enough to figure that out...

Re: bulletin board
by repson (Chaplain) on Dec 05, 2000 at 09:33 UTC
    This program does seem to have a few problems in the general design area (I have not looked at every line of code).
    Heres a few points to look into.
    • this is one of the tasks I used to learn perl :). Since then I have made more advanced versions using relational databases and message threads, it is something you can rewrite again and again as learning progresses.
    • warnings and strict save lots of time in debugging and sanity.
    • cgi-lib.pl is out of date, use CGI.pm or if you can't maybe CGI_Lite.pm
    • why do you use a seperate subroutines.pl?
    • why do you have four subs (box1,box2,box3,box4) which seem to repeat most of the functionality of each other. Maybe have one sub and pass it an argument for box number and have interpolation and a few if statements to handle any differences between the boxes.
    • your language regex's could be joined and should ignore case ie $in{Comments} =~ /dang|damn|bloody|darn/i
    • maybe you could have a common error reporting sub instead of pasting it everywhere you need it
    • databases - even DBD::CSV - are good :)
    • for a full discussion on a working (but not too complex) bulletin board look at merlyn's column.
    • try, try again
    • if you have any specific problems, please clarify
Re: bulletin board
by turnstep (Parson) on Dec 05, 2000 at 23:00 UTC

    As the others have said, the code above has many problems. If you just want a good working bulletin board, you should probably look for one of the many that others have already written. If you are doing it for a learning experience, try to post smaller portions of code and ask about a specific problem instead of posting the entire code and asking for "any problems."

    In general, writing a good bulletin board is a tricky thing, and can be much harder than it looks. I've written my own and it's now at 4000 lines (and growing), and I tend to write small, efficient code. :) Here are some things to keep in mind when rolling your own:

    • Use a database if possible. It saves a lot of trouble.
    • If you can't use a database, keep the data separate from the html files that are displayed. Keep the html out of your code as much as possible. Break up the data into multiple files, and create your own indexes (subsets of your data) to allow quick accessing of the data.
    • Check and double-check your file locking. This part will really get you in trouble: since multiple processes are writing to the same file, a simple mistake can cause your files to be erased.
    • Some sort of login/authentication will be needed eventually. Design it that way from the beginning.
    • Abstract everything as much as possible. Adding or removing an entire table, for example, should involve changing a configuration file and NOT the code. Things such as the error messages, the names of the boards/threads, the "improper language" words, etc. should not be inside the main code itself.
    • Perlmonks is a great resource, especially for specific problems in large projects. :)

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (5)
As of 2014-09-30 23:12 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (385 votes), past polls