#!/usr/bin/perl -w use strict; use Fcntl ':flock'; # import LOCK_* constants use CGI qw/:standard/; #then why the OO stuff, right? use CGI::Carp qw/fatalsToBrowser /; use POSIX qw(strftime); my $q = CGI->new(); #declare some vars my $mail ='weezelflesh@weezelflesh.com'; my $path_to_text="/home/weezelflesh/www/news/body.txt"; my $path_to_header="/home/weezelflesh/www/news/header.html"; my ($time_words,$topic,$words,$pass,$name,$user); my $url = "http://www.weezelflesh.com/cgi-bin/newspage.cgi"; my $goodpass="********"; parse_form(); show_news(); get_time(); if ($pass ne $goodpass){ bad_hacker_no_cookie(); }else { post_new_news(); } mail_routine(); exit; sub parse_form { my %FORM = $q->Vars('topic','words','pass','name'); $name= $q->param('name')|| "Sludge Factor"; chomp($name); $name =~ s/\s/_/g; $words = $q->param('words'); $words =~ s/\r\n/
/g; $topic= $q->param('topic'); chomp($topic); $user = $ENV{'REMOTE_ADDR'}; chomp($pass = $q->param('pass')); return [$topic,$words,$pass,$name,$user]; } sub get_time { my ($hour,$suffix); $hour= strftime ("%H", localtime ()); if ($hour >= 12) { $suffix="PM"; } else { $suffix = "AM"; } if ($hour > 12) { $hour -= 12; } $time_words= strftime (" at $hour:%M $suffix on %m\/%d\/%Y \n", localtime ()); $time_words; } sub post_new_news { open (FH, "+< $path_to_text") or die "where's the damn file? : $!"; flock (FH,LOCK_EX) or die "Couldn't flock: $!"; my @old_file = ; seek FH, 0, 0; truncate (FH,0) or die "Can't truncate: $!"; print FH "  

$topic
"; print FH "posted by $name"; print FH "$time_words."; print FH "

\n"; print FH "  $words

\n\n\n"; print FH @old_file; flock(FH,LOCK_UN); #unlock the file close FH or die "close damn you : $!"; print "Location: $url\n\n"; } sub bad_hacker_no_cookie { print "Content-type: text/html\n\n"; print " \n"; print " \n"; print "

!Unauthorized Access!\n"; print "
"; print "Hey bub, stop messin' with my News Page!\n"; print "
You are at IP address $user !"; print "

"; exit 0; } sub mail_routine { if ($pass ne $goodpass) { open MAIL,"|mail $mail" or die "mail problem : $!"; print MAIL "$user tried to get in using $pass for a password.\n"; close MAIL; } else { open MAIL,"|mail $mail" or die "mail problem : $!"; print MAIL "$user changed the menu $words.\n"; close MAIL; } } sub show_news { #This sub produces the current news page if ($topic eq "" && $words eq "") { open (FTXT, "$path_to_text") or die "where's the text file? : $!"; my @text_file = ; open (FHTML, "$path_to_header") or die "where's the html file? : $!"; my @html_file = ; print "Content-type: text/html\n\n"; print @html_file; print @text_file; print " \n"; exit; } else { return; } }