#!/usr/bin/perl

# SnorComments
# Version 2.00.0000
# by Leo Dillon
# This script may be edited as long as this notice is left in-tact

$Check=1;
require 'settings.cgi';


@QueryString=split(/\&/,$ENV{'QUERY_STRING'});

foreach $Query(@QueryString)
 {
 ($varName,$var)=split(/\=/,$Query);

 $Query{$varName}=$var;
 }


unless($Query{'id'}){&Error("No CommentID was specified.");}
unless($Query{'id'}=~/^\d+$/){&Error("Invalid CommentID.");}

open(GetSettings,"$ServerPath{'commentsettings'}/commentsettings/$Query{'id'}.cgi")||&Error("Couldn't open comment settings file!<br>$!!");
 flock(GetSettings,2);
 chomp(@Settings=<GetSettings>);
close(GetSettings);

if($Query{'action'} eq 'getjava')
 {
 open(GetNumbers,"$ServerPath{'comments'}/comments/$Query{'id'}.txt")||&Error("Couldn't open comment count file!<br>$!!");
  flock(GetNumbers,2);
  chomp(@GetNumbers=<GetNumbers>);
 close(GetNumbers);

 foreach$Numbers(@GetNumbers)
  {
  ($itemNumber,$commentNumber)=split(/\|/,$Numbers,2);
  push @pCount, "pCount[$itemNumber]=$commentNumber;\n";
  }

 print <<"END";
Content-type: text/html

var pCount = new Array();

@pCount

function postCount(item)
 {
 if(pCount[item])
  {
  document.write(pCount[item]);
  }
 else
  {
  document.write('$Settings[4]');
  }
 }

function viewComments(thingy)
 {
 var source = "$WebPath{'commentscript'}?action=start&id=$Query{'id'}&item=" + thingy;
 window.open(source,"SnorComments2$Query{'id'}"+thingy,"width=$Settings[2],height=$Settings[3],scrollbars");
 }

END
 exit;
 }
elsif($Query{'action'} eq 'start')
 {
 &OutputComments;

 exit;
 }
else
 {
 if($Query{'action'} eq 'post')
  {
  use CGI;
  $FormInput=new CGI;

  unless(length($Query{'id'}) < 1)
   {
   $Query{'id'}=~s/\W/x/g;
   }
  else
   {
   &Error('Sorry, but not enough information was sent to the server to complete your request.');
   }

  unless(length($Query{'item'}) < 1)
   {
   $Query{'item'}=~s/\W/x/g;
   }
  else
   {
   &Error('Sorry, but not enough information was sent to the server to complete your request.');
   }

  $FormName=$FormInput->param('name');
  $FormEmail=$FormInput->param('email');
  $FormHomepage=$FormInput->param('homepage');
  $FormComment=$FormInput->param('comment');

  if($FormComment eq ''){&Error('You did not enter a comment.');}

  ($FormComment,$FormName,$FormEmail,$FormHomepage)=&checkStrings($FormComment,$FormName,$FormEmail,$FormHomepage);

  open(GetNumbers,"$ServerPath{'comments'}/comments/$Query{'id'}.txt")||&Error("Couldn't open commment count file! $!!");
   flock(GetNumbers,2);
   chomp(@GetNumbers=<GetNumbers>);
  close(GetNumbers);

  open(ReprintNumbers,">$ServerPath{'comments'}/comments/$Query{'id'}.txt")||&Error("Couldn't open commment count file for writing! $!!");
   flock(ReprintNumbers,2);
   foreach$Numbers(@GetNumbers)
    {
    if($Numbers=~/^$Query{'item'}\|/)
     {
     ($postNumber,$commentNumber)=split(/\|/,$Numbers,2);
     $commentNumber++;
     print ReprintNumbers "$postNumber|$commentNumber\n";
     $done=1;
     }
    else
     {
     print ReprintNumbers "$Numbers\n";
     }
    }

   unless($done==1)
    {
    print ReprintNumbers "$Query{'item'}|1\n";
    }
  close(ReprintNumbers);

  $time=time;

  open(WriteComment,">>$ServerPath{'comments'}/comments/$Query{'id'}/$Query{'item'}.txt")||&Error("Couldn't open comment file for writing! $!!");
   flock(WriteComment,2);
   print WriteComment "$FormName|$FormEmail|$FormHomepage|$FormComment|$ENV{'REMOTE_ADDR'}|$time\n";
  close(WriteComments);

  if($FormInput->param('remember') eq '1')
   {
   &WriteCookie;
   print "Content-type: text/html\n\n";
   print "A cookie has been sent to your computer storing the information from the form so that you won't have to fill it in next time.<br><br>Your comment has been posted.<br><br><a href=\"$WebPath{'commentscript'}?action=output&id=$Query{'id'}&item=$Query{'item'}\">Click here to continue</a>";
   exit;
   }
  else
   {
   print "Location: $WebPath{'commentscript'}?action=output&id=$Query{'id'}&item=$Query{'item'}\n\n";
   }

  exit;
  }
 elsif($Query{'action'} eq 'output')
  {
  &OutputComments;

  exit;
  }

 }










sub OutputComments
 {
 $CookieName='SnCo2';

 local(@Cookies)=split(/; /,$ENV{'HTTP_COOKIE'});
 foreach$Cookie(@Cookies)
  {
  local($CrumbName,$CrumbValue)=split(/=/,$Cookie,2);
  if($CrumbName eq $CookieName)
   {
   $SavedInfo=$CrumbValue;
   }
  }

 ($Saved{'name'},$Saved{'email'},$Saved{'homepage'})=split(/\|\^\|/,$SavedInfo,3);

 open(GetComments,"$ServerPath{'comments'}/comments/$Query{'id'}/$Query{'item'}.txt");
  flock(GetComments,2);
  chomp(@Comments=<GetComments>);
 close(GetComments);

 require "$ServerPath{'templates'}/templates/$Settings[1].cgi";
 }


sub Error
 {
 print qq~Content-type: text/html\n\n

<b>And error has occured</b>
<br><br>
@_[0]
<br><br>
<a href="javascript:history.go(-1)">Go Back</a>~;

 exit;
 }


sub checkStrings
 {
 foreach$stringToCheck(@_)
  {
  $stringToCheck=~s/\&/\&amp\;/g;
  $stringToCheck=~s/</\&lt\;/g;
  $stringToCheck=~s/>/\&gt\;/g;
  $stringToCheck=~s/\|/\Ś/g;
  $stringToCheck=~s/\n/<br>/g;
  $stringToCheck=~s/\"/\&quot\;/g;
  push @checkedStrings, $stringToCheck;
  }
 return @checkedStrings;
 }


sub WriteCookie
 {
 @Days=('Sun','Mon','Tue','Wed','Thu','Fri','Sat');
 @Months=('Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec');

 $CookieName='SnCo2';
 $CookieInfo="$FormName|^|$FormEmail|^|$FormHomepage";
 $BestBefore=300;

 $CookieLife=$BestBefore*86400;
 @ExTime=(localtime(time+$CookieLife))[0,1,2,3,4,5,6];

 $CookieYear=1900+@ExTime[5];
 $CookieDate=@ExTime[3];
 $CookieMonth=@Months[@ExTime[4]];
 $CookieDay=@Days[@ExTime[6]];
 if($CookieDate<10)
  {
  $CookieDate="0$CookieDate";
  }

 print "Set-Cookie: $CookieName=$CookieInfo; expires=$CookieDay, $CookieDate-$CookieMonth-$CookieYear 00:00:00 GMT\n";
 }

