#!/usr/local/bin/perl -- -*-perl-*- # # Mogens Sept 94 # Appends comments to $COMMENT file # Path Info: Comments file # Form fields: # realname - text Full name of submitter - defaults to 'Anonymous' # username - text e-mail address of submitter. # comments - TextArea comments to be added # mailto - hidden e-mail address to CC new comments to - none. # # Christian Mogensen - mogens@cs.stanford.edu # # ------------------------------------------------------------ # Define fairly-constants local(%FORM); $CRLF="\r\n"; $DATE= &getlocaldate; $COMMENT= "/usr/local/etc/httpd/htdocs/default-comments.html"; $URL="/default-comments.html"; $BUTTONURL="/gifs/button.graffiti.gif"; $ALTTEXT="[ADD COMMENT] "; $SENDMAIL="/usr/lib/sendmail -t"; $PATH= $ENV{'PATH_TRANSLATED'}; if ((-r $PATH) && (length $PATH > 3)) { $COMMENT= $ENV{'PATH_TRANSLATED'}; $URL= $ENV{'PATH_INFO'}; } if ($ENV{'REQUEST_METHOD'} ne 'POST') { print "Location: $URL$CRLF"; print "Content-type: text/html$CRLF$CRLF"; print "The comments are here\n

\n"; exit; } # Print out what we need print "Content-type: text/html$CRLF$CRLF"; # Get the input read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'}); # Split the name-value pairs @pairs = split(/&/, $buffer); foreach $pair (@pairs) { ($name, $value) = split(/=/, $pair); $value =~ tr/+/ /; $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg; # Stop people from using subshells to execute commands $value =~ s/~!/ ~!/g; # Uncomment for debugging purposes # print "Setting $name to $value

"; $FORM{$name} = $value; } # Sanity check $FORM{'realname'}= "Anonymous" if ! $FORM{'realname'}; if (! $FORM{'comments'}) { print "Missing text\n"; print "

Sorry

\n"; print "It might be best if you actually typed something "; print "
\n"; print ""; print "\"$ALTTEXT\" View the comments"; print ""; exit; } # do a little pretty conversion - blank lines are paragraphs... $FORM{'comments'}=~ s/\r\n/\n/g; $FORM{'comments'}=~ s/\r/\n/g; $FORM{'comments'}=~ s/\n\n/\n

\n/g; # Now add the comment to the list. open (FILE, ">>$COMMENT") || die "Can't open comment file
$COMMENT!\n"; &lock_excl(FILE); print FILE "


"; print FILE "From: $FORM{'realname'} "; print FILE "<$FORM{'username'}>\n" if $FORM{'username'}; print FILE "
\n"; print FILE "Date: $DATE
" if $DATE; print FILE "$FORM{'comments'}\n

\n"; close (FILE); &lock_unlock(FILE); if ($FORM{'mailto'}) { open (FILE, "| $SENDMAIL") || die "Can't open pipe to sendmail $SENDMAIL!\n"; print FILE "To: $FORM{'mailto'}\n"; print FILE "From: $FORM{'realname'} "; print FILE "<$FORM{'username'}>" if $FORM{'username'}; print FILE "\n"; print FILE "Subject: Comment gateway on $ENV{'HOST'}\n"; print FILE "\n\n"; print FILE "$FORM{'comments'}\n\n"; print FILE "Host: $ENV{'REMOTE_HOST'}\n\n"; close (FILE); } print "Thank you\n"; print "

Thank you

\n"; print "Thank you for sending comments"; print "


\n"; print "From: $FORM{'realname'} "; print "<$FORM{'username'}>\n" if $FORM{'username'}; print "

\n"; print "$FORM{'comments'}\n"; print "


\n"; print ""; print "\"View"; print ""; exit; ##### sub getlocaldate { local($sec, $min, $hour, $mday, $month, $year, $wday, $yday, $isdst)= localtime(time); $month++; # months range 0..1 $min= "00" if $min == 0; $min= "0$min" if ($min < 10); local($mon)= "MONTH"; $mon="Jan" if $month== 1; $mon="Feb" if $month== 2; $mon="Mar" if $month== 3; $mon="Apr" if $month== 4; $mon="May" if $month== 5; $mon="Jun" if $month== 6; $mon="Jul" if $month== 7; $mon="Aug" if $month== 8; $mon="Sep" if $month== 9; $mon="Oct" if $month==10; $mon="Nov" if $month==11; $mon="Dec" if $month==12; return "$mday. $mon $year $hour:$min"; } ####################################################################### sub lock_excl { # File locking constants $LOCK_EXCL = 2; flock($_[0], $LOCK_EXCL); # seek($_[0], 0, 2); # Seek to end (end=2) } ####################################################################### sub lock_share { # File locking constants $LOCK_SHARED = 1; flock($_[0], $LOCK_SHARED); } ####################################################################### sub lock_unlock { # File locking constants $LOCK_UNLOCK = 8; flock($_[0], $LOCK_UNLOCK); } #######################################################################