Gossamer Forum
Home : General : Perl Programming :

Help with script please...

Quote Reply
Help with script please...
I can't get the script below to work. Nor can anyone else, judging by the postings at the script's support forum! I've checked that all permissions are correct and the path variables are correct. It is the admin utility for a script Called Tip o the Day, which posts a new daily tip every day to a web page. The admin script (below) allows the user to add new daily tips for the main script to display. I can anly assume there's something uncool about the code. When I tried running the script via Telnet, the debug text pointed to an error near line 30 (The section that's headed "Don't Alter Anything Below This Line") However, I know those debug messages are often misguided. I've tried running it on unix and Win95 servers.

Here's the script.. THANKS A MILLION for any help:

#!/usr/bin/perl
############################################
## ##
## WebHints (Add) ##
## by Darryl Burgdorf ##
## (e-mail burgdorf@awsd.com) ##
## ##
## version: 1.02 ##
## last modified: 1/4/00 ##
## copyright (c) 2000 ##
## ##
## latest version is available from ##
## http://awsd.com/scripts/ ##
## ##
############################################

# ASSIGN THE FOLLOWING VARIABLES

$HintsDir = "hints";
$NewHintsDir = "newhints";
$DataFile = "newhints/lasthint.txt";

$AddURL = "http://127.0.0.1/cgi-bin/daytips/hints_add.pl";

$HintsTitle = "Tip of the Day";
$BodySpec = "";

# DON'T ALTER ANYTHING BELOW THIS LINE

@months =
(January,February,March,April,May,June,
July,August,September,October,November,December);

read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});
@pairs = split(/&/, $buffer);
foreach $pair (@pairs){
($val1, $val2) = split(/=/, $pair);
$val1 =~ tr/+/ /;
$val1 =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
$val2 =~ tr/+/ /;
$val2 =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
if ($FORM{$val1}) {
$FORM{$val1} = "$FORM{$val1}, $val2";
}
else {
$FORM{$val1} = $val2;
}
}

print "Content-type: text/html\n\n";

&Get_Date;

if ($FORM{'Hint'}) {
&AddHint;
}

print "<HTML><HEAD><TITLE>$HintsTitle</TITLE></HEAD>\n";
print "<BODY>\n";
print "<H1 ALIGN=CENTER>Add New Entry</H1>\n";
print "<H2 ALIGN=CENTER>for $date</H2>\n";
print "<CENTER><FORM METHOD=POST ACTION=\"$AddURL\">\n";
print "<INPUT TYPE=HIDDEN NAME=\"Hint\" VALUE=\"$hint\">\n";
print "<P>Title: <INPUT TYPE=TEXT NAME=\"Title\" SIZE=40>\n";
print "<P>Body:\n";
print "<BR><TEXTAREA COLS=80 ROWS=10 NAME=\"Body\" WRAP=VIRTUAL>\n";
print "</TEXTAREA>\n";
print "<P>Does the above text already include HTML tags?\n";
print "<BR><INPUT TYPE=RADIO NAME=\"AddHTML\" VALUE=\"No\"> Yes ";
print "<INPUT TYPE=RADIO NAME=\"AddHTML\" VALUE=\"Yes\" CHECKED> No\n";
print "<P><INPUT TYPE=SUBMIT VALUE=\"Add Entry\">\n";
print "</P></FORM></CENTER></BODY></HTML>\n";

exit;

sub Get_Date {
if ($FORM{'Hint'}) {
$hint = $FORM{'Hint'};
}
elsif (!$hint) {
open (LASTHINT,"$DataFile") || &Error_File;
$hint = <LASTHINT>;
close (LASTHINT);
$hint = $hint+86400;
$time = time;
unless ($hint > $time) {
$hint = $time;
}
}
else {
$hint = $hint+86400;
}
($mday,$mon,$year) = (localtime($hint))[3,4,5];
$year += 1900;
$date = "$mday $months[$mon] $year";
$mon = $mon+1;
if ($mon < 10) { $mon = "0".$mon; }
if ($mday < 10) { $mday = "0".$mday; }
$filename = "$year"."$mon"."$mday".".txt";
if ((!$FORM{'Hint'}) && (-e "$NewHintsDir/$filename")) {
&Get_Date;
}
}

sub AddHint {
$FORM{'Title'} =~ s/\&/\&amp\;/g;
$FORM{'Title'} =~ s/"/\&quot\;/g;
$FORM{'Title'} =~ s/</\&lt\;/g;
$FORM{'Title'} =~ s/>/\&gt\;/g;
if ($FORM{'AddHTML'} eq "Yes") {
$FORM{'Body'} =~ s/\&/\&amp\;/g;
$FORM{'Body'} =~ s/"/\&quot\;/g;
$FORM{'Body'} =~ s/</\&lt\;/g;
$FORM{'Body'} =~ s/>/\&gt\;/g;
$FORM{'Body'} =~ s/\cM\n/<BR>/g;
$FORM{'Body'} =~ s/\n\cM/<BR>/g;
$FORM{'Body'} =~ s/\cM/<BR>/g;
$FORM{'Body'} =~ s/\n/<BR>/g;
$FORM{'Body'} =~ s/<BR>\s\s\s+/<BR><BR>/g;
$FORM{'Body'} =~ s/<BR>\t/<BR><BR>/g;
$FORM{'Body'} =~ s/\s+/ /g;
$FORM{'Body'} =~ s/<BR>\s/<BR>/g;
$FORM{'Body'} =~ s/\s<BR>/<BR>/g;
$FORM{'Body'} =~ s/<BR><BR>/<P>/g;
$FORM{'Body'} =~ s/<P><BR>/<P>/g;
$FORM{'Body'} =~ s/<BR>/ /g;
$FORM{'Body'} =~ s/<P>/\n<P>/g;
$FORM{'Body'} =~ s/<P>\n/\n/g;
$FORM{'Body'} = "<P>"."$FORM{'Body'}";
}
unless ($FORM{'Body'} && $FORM{'Title'}) {
print "<HTML><HEAD><TITLE>Oops!</TITLE></HEAD>\n";
print "<BODY $BodySpec>\n";
print "<H1 ALIGN=CENTER>Incomplete Submission!</H1>\n";
print "<P ALIGN=CENTER>Sorry, but your submission must ";
print "include both a <STRONG>Title</STRONG> and a ";
print "<STRONG>Body</STRONG>! ";
print "Please go "Back" and try again!</P>\n";
print "</BODY></HTML>\n";
exit;
}
open (HINT,">$NewHintsDir/$filename") || &Error_File;
print HINT "TITLE: $FORM{'Title'}\n\n";
print HINT "$FORM{'Body'}\n";
close (HINT);
open (LASTHINT,">$DataFile") || &Error_File;
print LASTHINT "$FORM{'Hint'}";
close (LASTHINT);
print "<HTML><HEAD><TITLE>Entry Added!</TITLE></HEAD>\n";
print "<BODY $BodySpec>\n";
print "<H1 ALIGN=CENTER>The New Entry Has Been Added!</H1>\n";
print "<P ALIGN=CENTER>"$FORM{'Title'}" ";
print "has successfully been added to the database. ";
print "It will appear on $date.\n";
print "<P ALIGN=CENTER><STRONG>[ To add another entry, ";
print "just "reload" this page! ]</STRONG></P>\n";
print "</BODY></HTML>\n";
exit;
}

sub Error_File {
print "<HTML><HEAD><TITLE>Oops!</TITLE></HEAD>\n";
print "<BODY $BodySpec>\n";
print "<H1 ALIGN=CENTER>File Error!</H1>\n";
print "<P ALIGN=CENTER>The server encountered an error while ";
print "trying to access a data file! Make sure that the files ";
print "and directories are correctly assigned in the script ";
print "and that all permissions are correct. (The hints ";
print ""counter" file and both hints directories ";
print "should be set world-writable!)</P>\n";
print "</BODY></HTML>\n";
exit;
}

#-------------------------End of script TIA, Alan A

Quote Reply
Re: Help with script please... In reply to
In your print statements you will need to escape the " sign like this
print "<INPUT TYPE=HIDDEN NAME=\"Hint\" VALUE=\"$hint\">\n";
So change this
print "Please go "Back" and try again!</P>\n";

to this
print "Please go \"Back\" and try again!</P>\n";

and this "<P ALIGN=CENTER>"$FORM{'Title'}" ";
to this
print "<P ALIGN=CENTER>\"$FORM{'Title'}\" \"";

etc
I think there are about three or four to change.


"just \"reload\" this page! ]</STRONG></P>\n";
"\"counter\" file and both hints directories ";



The print statement prints whatever is between two " ",
any more than two within the outer and inner need escaping.

rog


Quote Reply
Re: Help with script please... In reply to
For good coding practice, not to mention readability and less typing, you can use the 'qq' operator when printing.
so, for example, rather than having:

print "<HTML><HEAD><TITLE>Oops!</TITLE></HEAD>\n";
print "<BODY $BodySpec>\n";
print "<H1 ALIGN=CENTER>Incomplete Submission!</H1>\n";
print "<P ALIGN=CENTER>Sorry, but your submission must ";
print "include both a <STRONG>Title</STRONG> and a ";
print "<STRONG>Body</STRONG>! ";
print "Please go "Back" and try again!</P>\n";
print "</BODY></HTML>\n";

You could have:

print qq~
<HTML><HEAD><TITLE>Oops!</TITLE></HEAD>
<BODY $BodySpec>
<H1 ALIGN=CENTER>Incomplete Submission!</H1>
<P ALIGN=CENTER>Sorry, but your submission must
include both a <STRONG>Title</STRONG> and a
<STRONG>Body</STRONG>!
Please go "Back" and try again!</P>
</BODY></HTML>
~;

The first thing i would do would be replace all pieces of your code that have print statements with this method.

-- Gordon


s/(\d{2})/chr($1)/ge + print if $_ = '8284703280698276687967';
Quote Reply
Re: Help with script please... In reply to
One thing that you all over looked is the @months. Those should all be strings. Put single quotes on each one.

@months =
('January','February','March','April','May','June',
'July','August','September','October','November','December');




James Hall

<a href="mailto:james@de-inc.com">james@de-inc.com</a>

ICQ: 72409850
Quote Reply
Re: Help with script please... In reply to
Since the script doesn't run under 'strict', the quotes don't technically need to be around the words, however it IS bad form not to use them.

My personal preference (one which quotes are NOT required) is:

my @months = qw(January February March April...);

which will run under strict, and looks cleaner. However, that format is no good if the array elements will have spaces in them, so the regular quoted way would be needed.

--mark

Installation support is provided via ICQ at UIN# 53788453. I will only respond on that number.