in reply to Re: Re: Re: Just a simple task
in thread Just a simple task

That's what I was thinking as well but it tested out when printing to the databases. There are other scripts involved
in the program but following is the code for this one that is relevant. It's a bit long:

#!/usr/local/bin/perl -w ######################################### # poetry_publisher.cgi v1.0 # # # #Written by John Drury - 2003 # #Email johnadrury@brighterhorizons.com # # # #You may use and alter this script in # #any way. # # # # # # ######################################### use Socket; $|=1; ####################################### # PROGRAM CONFIGURATION SECTION ####################################### @okaydomains=(); $DAYS=30000; # USE EITHER SMTP OR SEND_MAIL $SMTP_SERVER="localhost"; #$SEND_MAIL="/usr/lib/sendmail -t"; $HOMEDIR="/home/brighterhorizons/www"; $BASEDIR="/home/brighterhorizons/www/poems"; $SITEURL="http://brighterhorizons.com"; $BASEURL="$SITEURL/poems"; $SITENAME="Brighter Horizons Community"; $EXT=".htm"; $PROGNAME="/cgi-bin/poetry_test.cgi"; $MAILLOG="/data/authorlog.txt"; $MAINDB="/cgi-bin/rating/db/main.db"; $basedata="/cgi-bin/rating/db"; $SUBJECT ="Your poem has been published!"; ####################################### &main_driver; ####################################### ####################################### sub poem_published { ####################################### if ($MAILLOG ne "") { open (ML,">>$BASEDIR/$MAILLOG"); print ML "$fields{'author_name'}|$fields{'author_email'}|$fields{'p +oem_title'}|$URL_NAME\n"; close(ML); } if ($MAINDB ne "") { open (ML,">>$HOMEDIR/$MAINDB"); print ML "$id\|$fields{'poem_title'}\n"; close(ML); } open(NEW,">$HOMEDIR/$basedata/$id.db") || &error("The ID database f +ile failed to be created."); print NEW "$id\|$fields{'poem_title'}\|0\|0\|0"; close(NEW); open(NEWIP,">$HOMEDIR/$basedata/ip$id.db") || &error("The IP Log da +tabase file failed to be created."); print NEWIP ""; close(NEWIP); print "Content-type: text/html\n\n"; print <<__STOP_OF_PUBLISHED__; <CENTER> <H1><B>Congratulations!</B></H1> Your poem has been published.<BR> You may view your poem anytime at:<P> <A HREF="$URL_NAME">$URL_NAME</A> <P> <B>Alternatively, you may find your poem by clicking <A HREF="$SITEURL/cgi-bin/index.cgi?action=poem_search">HERE</A>.</B> <P> __STOP_OF_PUBLISHED__ } ####################################### sub setup_letter { ####################################### $msgtext =<<__STOP_OF_MESSAGE__; Greetings $fields{'author_name'}, You have just published "$fields{'poem_title'}" in the online poetry section of Brighter Horizons community. All rights to your poetry shall remain yours. Your poem has been published on $date EST. We have formatted a permanent webpage for $fields{'poem_title'} at $URL_NAME. We recommend that you save this email since it also serves as a dated proof of copyright for your poem. __STOP_OF_MESSAGE__ } ####################################### sub poem_page { ####################################### $poembody =<<__END_OF_POEM_BODY__; <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <HTML> <HEAD> <title>$fields{'poem_title'} by $fields{'author_name'}</title> <link rel="stylesheet" type="text/css" href="/poems/poem_font.css" /> <SCRIPT LANGUAGE="JavaScript"> <!-- if(navigator.userAgent.indexOf("MSIE") != -1) document.writeln (''); else document.writeln ('<EMBED SRC="$BASEURL/$fields{'midifile'}" AUTOSTART +="true" HIDDEN="true" VOLUME="60%">'); //--> </SCRIPT> </head> <BGSOUND SRC="$BASEURL/$fields{'midifile'}"> $BODYTAG $params <CENTER> <P> <TABLE WIDTH=584 BGCOLOR=WHITE BACKGROUND="/IMAGES/2x2.gif" BORDER=0 C +ELLPADDING=0 CELLSPACING=0> <TR> <TD ALIGN=CENTER VALIGN=MIDDLE height="70" BACKGROUND="$BASEURL/theme +s/$fields{'theme'}/head_bg.jpg" class="$fields{'font_family'}_title"> $fields{'poem_title'} </TD> </TR> <TR> <TD ALIGN=CENTER VALIGN=TOP> <TABLE> <TR> <TD WIDTH="30" align="right">&nbsp;</TD> <TD ALIGN="justify" VALIGN="TOP" class="$fields{'font_family'}"><d +iv align="justify"> <BR> <PRE align="justify" class="$fields{'font_family'}">$fields{'poem_ +body'}</PRE> </div> </TD> <TD WIDTH="30" align="right">&nbsp;</TD> </TR> </TABLE> </TD> </TR> <TR> <TD ALIGN=LEFT VALIGN=MIDDLE> <TABLE> <TR> <TD WIDTH="250" align="right" height="50">&nbsp;</TD> <TD WIDTH="250" ALIGN="left" class="$fields{'font_family'}"> - $fields{'author_name'} </TD> </TR> </TABLE> </TD> </TR> <TR> <TD ALIGN="CENTER" VALIGN="BOTTOM"><font face="arial" size="1" color= +"#444444">Copyright &copy; 2003</font> <p> <!--#include virtual="/cgi-bin/rating/rate_display.cgi?java=yes&produc +t=Name_of_Product&id=$id"--> </TD> </TR> </TABLE> </CENTER> </BODY> </HTML> __END_OF_POEM_BODY__ } ####################################### sub pass_params { ####################################### $params=<<__END_OF_PARAMS__; <CENTER> <TABLE WIDTH=500> <TR> <TD align="center"> <FONT FACE="ARIAL" size="2" color="#FFFFFF"><B> You agree that by publishing this poem that you are the author and that this is an original work written by you. To publish your poem, click on the "Publish" button. To return to the poem edit screen without sending, please press your browser's "BACK" button. </B> <P> <CENTER> <FORM METHOD="POST" ACTION="$PROGNAME"> <INPUT TYPE="HIDDEN" NAME="action_code" VALUE="APPROVED"> <INPUT TYPE="HIDDEN" VALUE="$fields{'pic_select'}" NAME="pic_select"> <INPUT TYPE="HIDDEN" VALUE="$fields{'sender_name'}" NAME="sender_name" +> <INPUT TYPE="HIDDEN" VALUE="$fields{'sender_email'}" NAME="sender_emai +l"> <INPUT TYPE="HIDDEN" VALUE="$fields{'author_name'}" NAME="author_name" +> <INPUT TYPE="HIDDEN" VALUE="$fields{'author_email'}" NAME="author_emai +l"> <INPUT TYPE="HIDDEN" VALUE="$fields{'poem_title'}" NAME="poem_title"> <INPUT TYPE="HIDDEN" VALUE="$fields{'theme'}" NAME="theme"> <INPUT TYPE="HIDDEN" VALUE="$fields{'poem_body'}" NAME="poem_body"> <INPUT TYPE="HIDDEN" VALUE="$fields{'font_family'}" NAME="font_family" +> <INPUT TYPE="HIDDEN" VALUE="$fields{'pub_date'}" NAME="pub_date"> <INPUT TYPE="HIDDEN" VALUE="$fields{'midifile'}" NAME="midifile"> <INPUT TYPE="HIDDEN" VALUE="$fields{'background'}" NAME="background"> <INPUT TYPE="HIDDEN" VALUE="$ENV{'HTTP_REFERER'}" NAME="parent"> <INPUT TYPE="submit" VALUE="Publish"> </FORM> </CENTER> </TD> </TR> </TABLE> __END_OF_PARAMS__ } ####################################### sub sendmail { ####################################### # 1 success # -1 $smtphost unknown # -2 socket() failed # -3 connect() failed # -4 service not available # -5 unspecified communication error # -6 local user $to unknown on host $smtp # -7 transmission of message failed # -8 argument $to empty # # Sample call: # # &sendmail($from, $reply, $to, $smtp, $subject, $message ); # # Note that there are several commands for cleaning up possible bad i +nputs - if you # are hard coding things from a library file, so of those are unneces +ssary # my ($fromaddr, $replyaddr, $to, $smtp, $subject, $message) = @_; $to =~ s/[ \t]+/, /g; # pack spaces and add comma $fromaddr =~ s/.*<([^\s]*?)>/$1/; # get from email address $replyaddr =~ s/.*<([^\s]*?)>/$1/; # get reply email address $replyaddr =~ s/^([^\s]+).*/$1/; # use first address $message =~ s/^\./\.\./gm; # handle . as first character $message =~ s/\r\n/\n/g; # handle line ending $message =~ s/\n/\r\n/g; $smtp =~ s/^\s+//g; # remove spaces around $smtp $smtp =~ s/\s+$//g; if (!$to) { return(-8); } if ($SMTP_SERVER ne "") { my($proto) = (getprotobyname('tcp'))[2]; my($port) = (getservbyname('smtp', 'tcp'))[2]; my($smtpaddr) = ($smtp =~ /^(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})$/) ? pack('C4',$1,$2,$3,$4) : (gethostbyname($smtp))[4]; if (!defined($smtpaddr)) { return(-1); } if (!socket(MAIL, AF_INET, SOCK_STREAM, $proto)) { return(-2); } if (!connect(MAIL, pack('Sna4x8', AF_INET, $port, $smtpaddr))) { return(-3); } my($oldfh) = select(MAIL); $| = 1; select($oldfh); $_ = <MAIL>; if (/^[45]/) { close(MAIL); return(-4); } print MAIL "helo $SMTP_SERVER\r\n"; $_ = <MAIL>; if (/^[45]/) { close(MAIL); return(-5); } print MAIL "mail from: <$fromaddr>\r\n"; $_ = <MAIL>; if (/^[45]/) { close(MAIL); return(-5); } foreach (split(/, /, $to)) { print MAIL "rcpt to: <$_>\r\n"; $_ = <MAIL>; if (/^[45]/) { close(MAIL); return(-6); } } print MAIL "data\r\n"; $_ = <MAIL>; if (/^[45]/) { close MAIL; return(-5); } } if ($SEND_MAIL ne "") { open (MAIL,"| $SEND_MAIL"); } print MAIL "To: $to\n"; print MAIL "From: $fromaddr\n"; print MAIL "Reply-to: $replyaddr\n" if $replyaddr; print MAIL "X-Mailer: Perl Powered Socket Mailer\n"; print MAIL "Subject: $subject\n\n"; print MAIL "$message"; print MAIL "\n.\n"; if ($SMTP_SERVER ne "") { $_ = <MAIL>; if (/^[45]/) { close(MAIL); return(-7); } print MAIL "quit\r\n"; $_ = <MAIL>; } close(MAIL); return(1); } ####################################### sub no_email { ####################################### print <<__STOP_OF_NOMAIL__; Content-type: text/html <FONT SIZE="+1"> <B> SORRY! Your request could not be processed because of missing e-mail address(es). Please use your browser's back button to return to the poem entry page. </B> </FONT> __STOP_OF_NOMAIL__ } ####################################### sub send_mail { ####################################### &setup_letter; $mailresult=&sendmail($fields{sender_email}, $fields{sender_email}, $f +ields{author_email}, $SMTP_SERVER, $SUBJECT, $msgtext); } ####################################### sub poem_expire { ####################################### local(@items, $item); opendir(POEMDIR, "$BASEDIR"); @items = grep(/[0-9]$EXT/,readdir(POEMDIR)); closedir(POEMDIR); foreach $item (@items) { if (-M "$BASEDIR/$item" > $DAYS) { unlink("$BASEDIR/$item"); } } } ####################################### sub valid_address { ####################################### $testmail = $fields{'author_email'}; if ($testmail =~ /(@.*@)|(\.\.)|(@\.)|(\.@)|(^\.)/ || $testmail !~ /^.+\@(\[?)[a-zA-Z0-9\-\.]+\.([a-zA-Z]{2,3}|[0-9]{1,3}) +(\]?)$/) { return 0; } else { return 1; } } ####################################### sub bad_email { ####################################### print <<__STOP_OF_BADMAIL__; Content-type: text/html <FONT SIZE="+1"> <B> SORRY! Your request could not be processed because of an improper e-mail address. Please use your back button to return to the card screen and try again! </B> </FONT> __STOP_OF_BADMAIL__ } ####################################### sub test_basedir { ####################################### if (not -w $BASEDIR) { print <<__STOP_OF_BADBASE__; Content-type: text/html <FONT SIZE="+1"> <B> The script cannot either find or write to the<BR> $BASEDIR directory. Please check this setting if the BASEDIR variable, and the permissions of the directory. If you have them set to 755, please change them to 777. </B> </FONT> __STOP_OF_BADBASE__ exit; } } ####################################### sub valid_page { ####################################### if (@okaydomains == 0) {return;} $DOMAIN_OK=0; $RF=$ENV{'HTTP_REFERER'}; $RF=~tr/A-Z/a-z/; foreach $ts (@okaydomains) { if ($RF =~ /$ts/) { $DOMAIN_OK=1; } } if ( $DOMAIN_OK == 0) { print "Content-type: text/html\n\n Sorry, This script cannot be + run from here...."; exit; } } ####################################### sub decode_vars { ####################################### #This part of the program splits up our data and gets it #ready for formatting. $i=0; read(STDIN,$temp,$ENV{'CONTENT_LENGTH'}); @pairs=split(/&/,$temp); foreach $item(@pairs) { ($key,$content)=split(/=/,$item,2); $content=~tr/+/ /; $content=~s/%(..)/pack("c",hex($1))/ge; $content=~s/\0//g; #strip nulls $content =~ s/<!--(.|\n)*-->//g; $fields{$key}=$content; $i++; $item{$i}=$key; $response{$i}=$content; } } ####################################### sub get_file_name { ####################################### $proc=$$; $newnum=time; $newnum=substr($newnum,4,5); $date=localtime(time); ($day, $month, $num, $time, $year) = split(/\s+/,$date); $month=~tr/A-Z/a-z/; $PREF = "$month$num-"; $id="$PREF$newnum$proc"; $FILE_NAME="$BASEDIR/$PREF$newnum$proc$EXT"; $URL_NAME="$BASEURL/$PREF$newnum$proc$EXT"; } ####################################### sub create_file { ####################################### open(OUTFILE,">$FILE_NAME") ; print OUTFILE "$poembody\n"; close (OUTFILE); } ####################################### sub do_preview { ####################################### $fields{'poem_body'} =~s/\"/\'/g; &pass_params; &poem_page; print "Content-type: text/html\n\n"; print "$poembody\n"; } ####################################### sub main_driver { ####################################### &valid_page; &test_basedir; &decode_vars; if ($fields{'author_email'} eq "") { &no_email; exit; } if (&valid_address == 0) { &bad_email; exit; } if ($fields{'sender_email'} eq "") { &no_email; exit; } if ($fields{'background'} ne "") { $BODYTAG="<BODY BACKGROUND=\"$BASEURL/$fields{'background'}\">"; +} else { $BODYTAG="<BODY BGCOLOR=\"#000000\" LEFTMARGIN=\"0\" TOPMA +RGIN=\"0\">" ; } if ($fields{'action_code'} eq "NEW") { &do_preview; } if ($fields{'action_code'} eq "APPROVED") { &poem_page; &get_file_name; &create_file; &setup_letter; $mailresult=&sendmail($fields{sender_email}, $fields{sender_emai +l}, $fields{author_email}, $SMTP_SERVER, $SUBJECT, $msgtext); &poem_published; if ($DAYS > 0) {&poem_expire;} } }

I tried the code you suggested but the result was still the same unless there is something else i need to place in
the "".

The printed html output is as follows:

<!--#include virtual="/cgi-bin/rating/rate_display.cgi?java=yes&produc +t=Name_of_Product&id="-->

All perfect but the $id. If that prints then the page calls a rating script which embeds into that page and
tracks just that page.

Replies are listed 'Best First'.
Re^5: Just a simple task
by blue_cowdawg (Monsignor) on Aug 08, 2003 at 17:58 UTC

    Humph... and you said this was simple. :-)

    I still say I smell problems with scoping here.

    Have you considered using the module CGI? It might save you some grief. Not to mention HTML::Template.


    Peter @ Berghold . Net

    Sieze the cow! Bite the day!

    Nobody expects the Perl inquisition!

    Test the code? We don't need to test no stinkin' code!
    All code posted here is as is where is unless otherwise stated.

    Brewer of Belgian style Ales

      Problem resolved.

      In the Main Driver I had the calling order reversed for &get_file_name; and &poem_page;

      All is working perfectly now.

      Thanks for the help.

      John...