#!/usr/bin/perl

#Settings (see README for more information)
#1. What is your site's name?
$bookname = "EMS";

#2.What color background do you want?
$color = "white";

#3. Where is the data file?
$gdatl = "gbook.dat";

#4. Enter words you would like
# filtered from you guest book
# REMEMBER: it is case sensitive.
@badwords = ("bleep", "company-a");

#5. You can enter a custom header
# on the lines between the two
# EOT's.
$header = <<"EOT";

<center><font size="5" color="#033333">Гостевая книга.</font></center>
<BR><BR>


EOT

#6. You can enter a custom footer
# on the lines between the two
# EOT's.
$footer = <<"EOT";

<center><i>Custom Footer Goes Here.</i></center>

EOT

#7. Password for clearing Guest-
# Call messages.
$password = "default";

#8. Are you using Win32?
$win32 = "0";

#9. How many postings should be
# displayed on one page?
$displaynum = "10";

##################################
#your done! your done! your done!#
##################################


#print "Content-type: text/plain\n\n";

$passok = "${password}ok";
$displaynum--;

@months = ('Янв','Фев','Мар','Апр','Май','Июн','Июл','Авг','Сен','Окт','Ноя','Дек');

@days = ('Воскресенье','Понедельник','Вторник','Среда','Четверг','Пятница','Суббота');

($sec,$min,$hour,$mday,$mon,$year,$wday) = (localtime(time))[0,1,2,3,4,5,6];

$year += 1900;

if ($min < 10){
   $min = "0$min";
}


if ($hour < 12){
   
   $ampm = "A.M.";
   
}
else{

   $ampm = "P.M.";
   
}

if ($hour > 12) {
   
   $hour -= 12;
   
}

if ($hour < 1){

   $hour = 12;
   
}


    

$date = "$days[$wday], $months[$mon] $mday, $year в $hour:$min $ampm";

open (data,"$gdatl");
   @rawdat = <data>;
close (data);

foreach $dat (@rawdat){
   $dat =~ s/\r\n//g;
   $dat =~ s/\r//g;
   $dat =~ s/\n//g;     
   $dat_wo_lf .= $dat;
}
   
@gdat = split (/\&\t\&/, $dat_wo_lf);
   

if (@gdat eq ""){

   $gdatnum = 0;
   
}
else{

   $gdatnum = $#gdat + 1;

}

read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});

@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;
    $value =~ s/~!/ ~!/g; 
    $FORM{$name} = $value;
}


if ($FORM{'snd'} eq ""){

   if ($ENV{'QUERY_STRING'} eq "log") {
      
      #User wants to see logs.
       
      print ("Content-type: text/html\n\n");
      print "<html><head><META charset=windows-1251><title>Log file</title></head><body bgcolor = '$color'>You currently have $gdatnum messages.<BR>";
      print "<tr><table border=\"1\" width = \"100%\"><td>Message No.</td><td>User's Name</td><td>User's Client</td><td>Date written</td></tr>";
      print "<tr><td colspan = 5><HR></td></tr>";
      
      for $i (0...$#gdat){
         ($name, $email, $pdate, $comments, $useragent) = split (":\t:", $gdat[$i]);
         $pnum = $i + 1;
         print "<tr><td>$pnum</td><td>$name ($email)</td><td>$useragent</td><td>$pdate</td></tr>";
      }
      
      print "</table>Processed on $date.</body></html>";

   }
   elsif ($ENV{'QUERY_STRING'} eq $password){
      print ("Content-type: text/html\n\n");
      print "<html><head><META charset=windows-1251><title>Erase Messages?</title></head>";
      print "<body bgcolor=\"#FFFFFF\"><h2>Erase Messages?</h2>";
      print "Are you sure you want to erase all of your ";
      print "GuestCall Messages?<HR><a href=\"$ENV{'SCRIPT_NAME'}\?$passok\">OK</a></body></html>";
   }
   elsif ($ENV{'QUERY_STRING'} eq $passok){
      
      open (mordat, ">$gdatl");
      
         unless ($win32){
            flock mordat, 2;
            flock mordat, 8;
         }
         
      close(mordat);
      
      print ("Content-type: text/html\n\n");
      print "<html><head><META charset=windows-1251><title>Messages Erased</title></head>";
      print "<body bgcolor=\"#FFFFFF\"><h2>Messages Erased</h2>";
      print "GuestCall has deleted all of the postings in your ";
      print "guest book.</body></html>";      
   
   }
   else{
      
      #User wants to see guest book.
      
      for $i (0...$#gdat){
         $gdat[$i] =~ s/\</\&lt\;/g;
         $gdat[$i] =~ s/\>/\&gt\;/g; 
         $gdat[$i] =~ s/\:ctrln\:/\<BR\>/g;
         
         for $ii (0...$#badwords){
            $gdat[$i] =~ s/$badwords[$ii]/\(censored\)/g;
         }
         
      }
   
   
      if ($FORM{'displaynum'}) {
         $startnum = $FORM{'displaynum'};
         $stopnum = $FORM{'displaynum'} + $displaynum;
      }
      else{
         $FORM{'displaynum'} = 0;
         $startnum = "0";
         $stopnum = $displaynum;

         
      }
      
      if ($displaynum < $#gdat) {
         
         $prenum = $startnum - ($displaynum + 1);
         $nextnum = $stopnum + 1;
         
         if ($FORM{'displaynum'} > 0){
            $previous="<center><form action=\"$ENV{'SCRIPT_NAME'}\" method=\"post\"><input type=\"hidden\" name=\"displaynum\" value=\"$prenum\"><input type=\"submit\" value=\"Следующие 10\"></form></center>";
         }
         
         if (($FORM{'displaynum'} + 10) < $#gdat){
            $next="<center><form action=\"$ENV{'SCRIPT_NAME'}\" method=\"post\"><input type=\"hidden\" name=\"displaynum\" value=\"$nextnum\"><input type=\"submit\" value=\"Предыдущие 10\"></form></center>";
         }  
      
      }
      
      for $i ($startnum...$stopnum){
         $selectgdat[$i] = $gdat[$i];
      }
   
      print ("Content-type: text/html\n\n");
      print "<head><META charset=windows-1251><title>$bookname</title><link href=\"ems.css\"  rel=\"stylesheet\" type=\"text/css\"></head><body bgcolor = '$color'>";

      print "<table width=100% height=100% border=0>";
      print "<tr><td width=150 height=100 bgcolor=#D0D090 rowspan=2 valign=top>";
      print "<img src=images/big2.gif><br>";
      print "<br>";
      print "<table><tr><td width=10><td>";
#      print "<tr><td valign=top>";
      print "<p><a href=index.html><img src=images/main.gif  border=0 alt=\"Главная\"></a>";
      print "<p><a href=history.html><img src=images/history.gif border=0 alt=\"История\"></a>";
      print "<p><a href=news.html><img src=images/news.gif border=0 alt=\"Новости\">";
      print "<p><a href=product.html><img src=images/product.gif border=0 alt=\"Продукция\"></a>";
      print "<p><a href=okb.html><img src=images/facility.gif border=0 alt=\"Услуги\"></a>";
      print "<p><a href=partnership.html><img src=images/partnership.gif border=0 alt=\"Сотрудничество\"></a>";
      print "<p><a href=vacancy.html><img src=images/vacancy.gif border=0 alt=\"Вакансии\"></a>";
      print "<p><a href=contact.html><img src=images/contact.gif border=0 alt=\"Контакты\"></a>";
      print "<p><img src=images/guest_r.gif border=0 alt=\"Гостевая\">";
      print "</table><td><tr><td valign=\"top\">";
      print "<table width=80% align=center boorder=1><tr><td>";
      print "Всего $gdatnum сообщений<BR><BR>$previous";
	      

     foreach $dat (@selectgdat) {
         if ($dat){
            ($name, $email, $pdate, $comments, $useragent) = split (":\t:", $dat);
            print "<HR width=\"100%\"><b>$pdate, <a href=\"mailto:$email\">$name</a> said:</b><BR>$comments<BR>";
         }
      }
   
      print "$next<BR><HR width=\"75%\"><FORM ACTION='$ENV{'SCRIPT_NAME'}' METHOD=POST><table><tr><td>Ваше Имя:<td><Input type = 'text' name = 'name' SIZE='45'><tr><td>Адрес (e-mail):<td><Input type = 'text' name = 'email' SIZE='45'><tr><td>Сообщение:<td><TEXTAREA width = 100% height = 100% name='comment' wrap=soft cols=60 rows=15></TEXTAREA></table><Input type = 'Hidden' name = 'snd' value='2444211'><BR><center><input type='submit' name='submit' value='Добавить сообщение'> <input type='reset'  name='reset'  value='Очистить форму'></center><BR></form><BR></body></html>";
      print "</table>";
      print "</table>";
   }
}
else{

   #Add message.
   $i = $i + 1;
   open (mordat, "$gdatl");
      @current_messages = <mordat>;
   close(mordat);
   
   open (mordat, ">$gdatl");
   
      unless ($win32){
         flock mordat, 2;
      
         #Check for change made during locking process.
         seek mordat, 0, 2;
      }
        
      $FORM{'comment'} =~ s/\r\n/\:ctrln\:/g;
      $FORM{'comment'} =~ s/\r/\:ctrln\:/g;
      $FORM{'comment'} =~ s/\n/\:ctrln\:/g;
   
      print mordat "$FORM{'name'}:\t:$FORM{'email'}:\t:$date:\t:$FORM{'comment'}:\t:$ENV{'useragent'}&\t&";
      
      if (@current_messages){
         print mordat "\n@current_messages";
      }
      
      unless ($win32){
         flock mordat, 8;
      }
      
   close(mordat);
   
   $FORM{'comment'} =~ s/\</\&lt\;/g;
   $FORM{'comment'} =~ s/\>/\&gt\;/g; 
   $FORM{'comment'} =~ s/\:ctrln\:/\<BR\>/g;
   
   print ("Content-type: text/html\n\n");
   print "<html><head><META charset=windows-1251><title>Сообщение отправлено</title></head><body bgcolor = '$color'><font size=\"4\">Сообщение отправлено</font><BR>Содержание:<BR><B>Имя:</B>$FORM{'name'}<BR><B>Адрес (e-mail):</B>$FORM{'email'}<BR><B>Сообщение:</B><BR>$FORM{'comment'}<BR><a href = \"$ENV{'HTTP_REFERER'}\">Вернуться в гостевую</a></body></html>";
}