#!/usr/local/bin/perl #=============================================================================# # # # This CGI script submits comments to the Ken Kopp's comment board. # # # #=============================================================================# # # # Both old and new comments files must be world-writeable. # # # #=============================================================================# # Author: Don Thomson # #=============================================================================# use strict; use CGI; use CGI::Carp qw(fatalsToBrowser confess); use IO::File; my($Comment_Dir, $Comment_File, $Mail_To, $Test, $Title, %Fields); my $SEND_MAIL = 1; my $KEN_KOPPS_DIR = 'ken_kopps'; #=============================================================================# # Main body # #=============================================================================# my $q = new CGI; initialize(); print $q->header; print $q->start_html(-title=>$Title, -bgcolor=>'#FFFFCC', -link=>'#008000', -vlink=>'#800080'), "\n"; display_file('header.html'); if ($q->param('submit')) { submit_form(); } else { display_form(); } display_file('footer.shtml', '/; } print $new_fh $q->hr, "\n", $q->b(trim_whitespace($q->param('comment'))), "\n", $q->p, "\n", $q->param('submitter_name'), "\n"; my $submitter_email = $q->param('submitter_email'); if ($submitter_email) { print $new_fh $q->br, "\n", $q->a({-href=>"mailto:$submitter_email"}, $submitter_email) , "\n"; } print $new_fh $q->p({-align=>'right'}, $q->font({-size=>'-1'}, $date_time), "\n", ), "\n\n"; while ($line = ) { print $new_fh $line; } close $new_fh; system("/bin/cp $new_comment_file $old_comment_file") && die "Can't copy '$new_comment_file' to '$old_comment_file' ($!)"; if ($SEND_MAIL) { my $sendmail = '/usr/lib/sendmail -t'; open(MAIL, "| $sendmail") || die "Can't fork $sendmail: $!\n"; print MAIL "To: $Mail_To\n", "Subject: Ken Kopp's Comment Submission\n\n", "Ken Kopp's Comment Submission\n\n", ' (Submitted: ', $date_time, ' from: "', $q->remote_host, '")', "\n"; foreach my $field (sort { $Fields{$a}{order} <=> $Fields{$b}{order} } keys %Fields) { $label = $Fields{$field}{label}; $label = uc $label; if ($sub = $Fields{$field}{str_sub}) # Call sub to construct # string { $value = &$sub(0); } else { $value = trim_whitespace($q->param($field)); } $newline = $Fields{$field}{newline}; $label .= ': '; $label .= "\n\n" if $newline; if ($value) { print MAIL "\n", $label, $value; print MAIL "\n" if $value !~ /\n$/; } } close(MAIL); } } #=============================================================================# # Trim leading and trailing whitespace from string # #=============================================================================# sub trim_whitespace { my($str) = @_; $str =~ s/^\s+|\s+$//g; return $str; } #=============================================================================# # Create formatted date/time string from time value # #=============================================================================# sub date_time { my($time) = @_; my @MONTHS = qw(Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec); my @DAYS = qw(Sun Mon Tue Wed Thu Fri Sat); my($sec, $min, $hour, $mday, $mon, $year, $wday) = localtime($time); return sprintf("%s-%02d-%d (%s) %02d:%02d", $MONTHS[$mon], $mday, $year + 1900, $DAYS[$wday], $hour, $min); }