#!/usr/bin/perl

$GMT=0;
$limit = 20;
$dt = $ENV{'REMOTE_ADDR'};
$location = "forum.shtml";
$error_up = "error_up.dat";
$error_down = "error_down.dat";
$mail_flag = 0;
$name_flag = 2;
$comment_flag = 2;
$href_flag = 0;

@mons = ("января", "февраля", "марта", "апреля", "мая", "июня", "июля", "августа", "сентября", "октября", "ноября", "декабря");

($min,$hour,$mday,$mon) = (localtime(time+($GMT*3600)))[1,2,3,4];

if (length($hour)<2) {$hour="0".$hour}
if (length($min)<2) {$min="0".$min}

$date = $mday." ".$mons[$mon]." ".$hour.":".$min;
             
read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});
@pairs = split(/&/, $buffer);
foreach $pair (@pairs)
  {
   ($name, $value) = split(/=/, $pair);
   $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
   $value =~ tr/+/ /;
   $value =~ s/<!--(.|\n)*-->//g;
   $value =~ s/<([^>]|\n)*>//g;
   $value =~ s!\n!<br>!g;
   $FORM{$name} = $value;
  }

if ((length($FORM{'name'}) != 0) && ($FORM{'name'} ne " ")) {$name_flag = 1};
if ((length($FORM{'comment'}) != 0) && ($FORM{'comment'} ne " "))
 {
  $comment_flag = 1;
  $FORM{'comment'}=&findlinks($FORM{'comment'});
 }

$_=$FORM{'mail'};
if (length > 0) {if ((m/@/) && (length>6)) {$mail_flag = 1} else {$mail_flag = 2}};

$_=$FORM{'href'};
if (($FORM{'href'} eq "") || ($FORM{'href'} eq "http://")) {$href_flag = 0}
 elsif ((length > 11) && (m/http:\/\//) && (m/\./)) {$href_flag = 1}
 elsif ((length > 7) && (!m/http:\/\//) && (m/\./)) {$href_flag = 1}
 else {$href_flag = 2;};

$flags=$name_flag.$mail_flag.$href_flag.$comment_flag;

if (index($flags, "2") == -1)
{
 if ($mail_flag == 1) {$formlines[0] = "<p class=\"forum-text\"><a href=\"mailto:".lc($FORM{'mail'})."\" class=\"zagolovok\">".$FORM{'name'}."</a>&nbsp;&nbsp;&nbsp;<span class=\"date\">".$date."</span><br>\n"}
  else {$formlines[0] = "<p class=\"forum-text\"><span class=\"zagolovok\">".$FORM{'name'}."</span>&nbsp;&nbsp;&nbsp;<span class=\"date\">".$date."</span><br>\n"};
 if ($href_flag == 1)
 {
  $formlines[1] = "<a class=\"link-yellow\" href=\"".lc($FORM{'href'})."\">".&manychars($FORM{'href'})."</a><br>\n";
 }
 else
 {
  $formlines[1] = "<br>\n";
 }
 
 $formlines[2] = $FORM{'comment'}."</p>\n";
 $formlines[3] = "</td></tr><tr><td width=1 height=10 align=\"left\" valign=\"top\" background=\"\" bgcolor=\"#666666\"><img src=\"../pic/s.gif\" width=1 height=1 border=0 alt=\"\"></td><td width=428 height=10 align=\"right\" valign=\"top\" background=\"../pic/gb_back2.gif\"><img src=\"../pic/s.gif\" width=1 height=1 border=0 alt=\"\"></td><td width=1 height=10 align=\"left\" valign=\"top\" background=\"\" bgcolor=\"#666666\"><img src=\"../pic/s.gif\" width=1 height=1 border=0 alt=\"\"></td></tr><tr bgcolor=\"#8DB2CC\"><td width=430 colspan=3 align=\"left\" valign=\"top\">\n";

 open(COUNT,"count.dat");
 $count = <COUNT>;
 close(COUNT);
 if ($count =~ /\n$/) {chop($count);}
 
 open(FN, "forum.shtml");
 @lines=<FN>;
 close(FN);
 $lineslen=@lines;
                   
 $j=0;

 for ($i=0; $i<$lineslen; $i++)
 {
  $_ = $lines[$i];
  $newlines[$j] = $_;
  if (m/<!--HERE-->/)
   {
    $newlines[$j+1] = $formlines[0];
    $newlines[$j+2] = $formlines[1];
    $newlines[$j+3] = $formlines[2];
    $newlines[$j+4] = $formlines[3];
    $newlines[$j+5] = "<!--DELIMITER-->"."\n";
    $j=$j+5;
   }
  $j++;
 }

 

 if ($count>=$limit)
  {
   open (ARCHCOUNT,"arch_count.dat");
   $arch_count=<ARCHCOUNT>;
   close(ARCHCOUNT);
   if ($arch_count =~ /\n$/) {chop($arch_count);}
   $arch_count++;
   open (ARCHCOUNT,">arch_count.dat");
   print ARCHCOUNT $arch_count;
   close(ARCHCOUNT);
  
   $lineslen=@newlines;
   @lines="";
   
   $last_pos=0;
   $new_pos=0;
   $message="";
   $i=0;
   
   while ($i<$lineslen)
   {
    $_=$newlines[$i];
    if (m/<!--DELIMITER-->/)
     {
      $new_pos=$i;
      $_=$newlines[$i+1]; 
      if (m/<!--END-->/)
       {
        for ($j=$last_pos+1; $j<$new_pos; $j++)
         {
          if ($newlines[$j] =~ /\n$/) {chop($newlines[$j]);}
          $message=$message.$newlines[$j];
          $newlines[$j]="<!--EMPTY-->";
         }
        $newlines[$new_pos]="<!--EMPTY-->";
        open (ARCHIVE, ">>archive.dat");
        print ARCHIVE $message."\n";
        close(ARCHIVE);
        $i=$new_pos+1;
       }
      $last_pos=$i;
     }
    $i++;
   }

  }
  else
  {
   open(COUNT,">count.dat");
   print COUNT ++$count;
   close(COUNT);
  }





 open(FN, ">forum.shtml");
  foreach (@newlines)
    {
     if (!m/<!--EMPTY-->/) {print FN};
    }
 close(FN);


 print "Location: ".$location."\n\n";

}

else

{
 if (($name_flag == 2) || ($comment_flag == 2)) {$error_message = "Одно из обязательных полей было не заполнено."}
   elsif (($mail_flag == 2) && ($href_flag == 2)) {$error_message = "Проверьте поля почты и ресурса."}
   elsif ($mail_flag == 2) {$error_message = "Проверьте поле e-mail."}
   elsif ($href_flag == 2) {$error_message = "Проверьте поле ресурса."}
  else {$error_message = "Неизвестная ошибка."};

  open(FE, $error_up);
  @temp1 = <FE>;
  close(FE);
  
  open(FE, $error_down);
  @temp2 = <FE>;
  close(FE);

  print "Content-Type: text/html\n\n";

  print @temp1;
  print $error_message."\n";
  print @temp2;

}

exit;

sub findlinks
{
 $final_string="";
 @lines_array=split(/<br>/, $_[0]);
 for ($index=0; $index<@lines_array; $index++)
 {
  @words_array=split(/ /, $lines_array[$index]);
  for ($jndex=0; $jndex<@words_array; $jndex++)
  {
   $_=lc($words_array[$jndex]);
   if ((m/www./) && (!m/http:\/\//) && (!m/@/))
   {
    $words_array[$jndex]="<a class=\"link-yellow\" href=\"http://".$_."\">".&manychars($words_array[$jndex])."</a>";
   }
   elsif ((m/http:\/\//) && (m/./) && (!m/@/))
   {
    $words_array[$jndex]="<a class=\"link-yellow\" href=\"".$_."\">".&manychars($words_array[$jndex])."</a>";
   }
   elsif ((m/@/) && (m/./) && (!m/http:\/\//))
   {
    $words_array[$jndex]="<a class=\"link-yellow\" href=\"mailto:".$_."\">".&manychars($words_array[$jndex])."</a>";
   }
  }
  $final_string=$final_string.join(" ", @words_array)."<br>";
 }
 return $final_string;
}

sub manychars
{
 if (length($_[0])>40)
  {
   return(substr($_[0], 0, 38)."...");
  }
 else
  {
   return $_[0];
  }
}
