#!/usr/bin/perl
print "Content-type: text/html;\n\n";
require "pvd-lib.pl";
getheader();
require "pvd-index.cfg";
open(TEMP,"$message{dbpath}/pvd-index.patt");while (<TEMP>) {$main_counter+=1;$main_pattern.=$_}; close TEMP;
open(TEMP,"$message{dbpath}/pvd-index.msg");@gb_messages=<TEMP>; close TEMP;
if (($#gb_messages+1)>$message{kolvo}) {kill_old_msg()}

@gb_messages=reverse(@gb_messages);
$main_pattern=~ s/<%form%>/post_gbform()/eigm;

if (int(($#gb_messages+1)/$message{list})<(($#gb_messages+1)/$message{list})) {
$temp_lists=int(($#gb_messages+1)/$message{list})+1;
} else {$temp_lists=int(($#gb_messages+1)/$message{list})}

if ($in{list}=~ /(\d+)/ and $in{list}>0) {
    if ($temp_lists==$in{list}) {$temp_target_list=$temp_lists} 
    elsif ($temp_lists>$in{list}) {$temp_target_list=$in{list}} 
    else {$temp_target_list=1}
} else {$temp_target_list=1}

$temp_msg_start=($temp_target_list-1)*$message{list};
$temp_msg_end=($temp_target_list)*$message{list}-1;
if ($temp_msg_end>$#gb_messages) {$temp_msg_end=$#gb_messages}

if (!$list_no_show or ($#gb_messages+1)>$message{list}) {
    $main_pattern=~ s/<%lists%>/post_lists()/eigm;
} else {$main_pattern=~ s/<%lists%>//igm}

$temp_messages='';
for $ti ($temp_msg_start..$temp_msg_end) {$temp_messages.=post_message($gb_messages[$ti])}
$main_pattern=~ s/<%messages%>/$temp_messages/igm;

print $main_pattern;

kill_old_fmsg();


#------------------------------------------------------#
sub kill_old_msg {
open(TEMP,">$message{dbpath}/pvd-index.msg");
flock(TEMP,2); binmode TEMP;
for ($i=$#gb_messages-($message{kolvo}-1);$i<=$#gb_messages;$i++) {
print TEMP $gb_messages[$i];
}
close TEMP;
}

#------------------------------------------------------#
sub kill_old_fmsg {
opendir (DATA, "temp") || die "Can`t open folder temp";
my @filenames = grep {!/^[\.]/} readdir (DATA); closedir (DATA);
for (0..$#filenames) {
    if ($filenames[$_]<(time-1800)) {unlink "temp/$filenames[$_]"}
}
}

#------------------------------------------------------#
sub post_message {
my $temp_msg_string=shift;
$temp_msg_string=chdel($temp_msg_string);
@temp_msg_stringm=split(/\x01/,$temp_msg_string);
$msg_pattern0=$msg_pattern;
$msg_pattern0=~ s/<%time%>/time_print($temp_msg_stringm[0])/eigm;
for (0..$#temp_msg_stringm-1) {
   if ($inputs_types{$_}=~ /^textarea/i) {
       if ($in{kill}) {
       if (!$mycgifullurl) {
           $temp_kill=substr($ENV{SCRIPT_NAME},0,rindex($ENV{SCRIPT_NAME},"/")+1)."pvd-kill.pl"}
       else {$temp_kill="$mycgifullurl/pvd-kill.pl"}
       $temp_kill="<a href=\"$temp_kill?$temp_msg_stringm[0]\" target=_blank><small><b>K</b></small></a>"}
       $msg_pattern0=~ s/<%message$_%>/$temp_kill.msg_print($temp_msg_stringm[$_])/eigm;
   }
   elsif ($inputs_types{$_}=~ /^text/i) {
       $msg_pattern0=~ s/<%message$_%>/$temp_msg_stringm[$_]/igm;
   }
   elsif ($inputs_types{$_}=~ /^etext/i) {
       if (!$temp_msg_stringm[$_]) {$msg_pattern0=~ s/<%message$_%>//igm}
       else {
       if ($msg_pattern0=~ /mailto:<%message$_%>/i) {
       $msg_pattern0=~ s/<%message$_%>/$temp_msg_stringm[$_]/igm}
       else {
       $msg_pattern0=~ s/<%message$_%>/<a href=\"mailto:$temp_msg_stringm[$_]\">$temp_msg_stringm[$_]<\/a>/igm}
       }
   }
   elsif ($inputs_types{$_}=~ /^utext/i) {
       if (!$temp_msg_stringm[$_]) {$msg_pattern0=~ s/<%message$_%>//igm}
       else {
       if ($msg_pattern0=~ /href=<%message$_%>|href=\"<%message$_%>|href=\'<%message$_%>/i) {
       $msg_pattern0=~ s/<%message$_%>/$temp_msg_stringm[$_]/igm}
       else {
       $msg_pattern0=~ s/<%message$_%>/<a href=\"$temp_msg_stringm[$_]\">$temp_msg_stringm[$_]<\/a>/igm}
       }
   }
   elsif ($inputs_types{$_}=~ /^select/i) {
       $msg_pattern0=~ s/<%message$_%>/$temp_msg_stringm[$_]/igm;
   }
   elsif ($inputs_types{$_}=~ /^radio/i) {
       $msg_pattern0=~ s/<%message$_%>/$temp_msg_stringm[$_]/igm;
   }
   elsif ($inputs_types{$_}=~ /^iradio/i) {
       $msg_pattern0=~ s/<%message$_%>/<img src=\"$temp_msg_stringm[$_]\" border=0>/igm;
   }
}
return $msg_pattern0;
}

#------------------------------------------------------#
sub post_lists {
$lists_pattern=~ s/<%all%>/($#gb_messages+1)/eigm;

$temp_text='';
if ($listsf_pattern=~ /^http/i) {$listsf_pattern="<img src=\"$listsf_pattern\" border=0>"}
if ($listsb_pattern=~ /^http/i) {$listsb_pattern="<img src=\"$listsb_pattern\" border=0>"}
if ($mygbfullurl) {$ENV{DOCUMENT_URI}=$mygbfullurl}
if ($in{kill}) {$temp_kill="\&kill=1"}
if ($temp_target_list<$temp_lists) {
    $temp_target_list0=$temp_target_list+1;
    $lists_pattern=~ s/<%back%>/<a href=\"$ENV{DOCUMENT_URI}?list=$temp_target_list0$temp_kill\">$listsf_pattern<\/a>/igm;
} else {
    $lists_pattern=~ s/<%back%>/$listsf_pattern/igm;
}
if ($temp_target_list>1) {
    $temp_target_list0=$temp_target_list-1;
    $lists_pattern=~ s/<%forward%>/<a href=\"$ENV{DOCUMENT_URI}?list=$temp_target_list0$temp_kill\">$listsb_pattern<\/a>/igm;
} else {
    $lists_pattern=~ s/<%forward%>/$listsb_pattern/igm;
}

for ($i=$temp_lists;$i>=1;$i--) {
    $list_pattern0=$list_pattern;
    if ($i!=$temp_target_list) {
        $list_pattern0=~ s/<%list%>/<a href=\"$ENV{DOCUMENT_URI}?list=$i$temp_kill\">$i<\/a>/igm;
        $temp_text.=$list_pattern0;
    } else {
        $list_pattern0=~ s/<%list%>/$i/igm;
        $temp_text.=$list_pattern0;
    }
}
$lists_pattern=~ s/<%lists%>/$temp_text/igm;
return $lists_pattern;
}

#------------------------------------------------------#
sub post_gbform {
if (!$mycgifullurl) {
    $temp_add_name=substr($ENV{SCRIPT_NAME},0,rindex($ENV{SCRIPT_NAME},"/")+1)."pvd-add.pl"}
else {$temp_add_name="$mycgifullurl/pvd-add.pl"}
my $temp_form=qq~
<form action="$temp_add_name" method=post name=pvdgb>
<input type=hidden name=coder>
<table border=0 cellspacing=0 cellpadding=2 width=$form_width>~;
if ($in{message}) {
    get_my_message();
    if ($cookie{errmessage}) {$temp_form.="<tr><td colspan=2><b>$cookie{errmessage}</td></tr>"}
}
foreach(sort {$a <=> $b} @gbform) {
   @ttemp_mas=split(/\,/);
   $temp_mess="message$ttemp_mas[0]";
   $inputs_types{$ttemp_mas[0]}=$ttemp_mas[4];
   if ($ttemp_mas[4]=~ /^textarea/i) {
      $temp_form.="<tr><td colspan=2>$ttemp_mas[3]<br><textarea name=$temp_mess cols=$ttemp_mas[6] rows=$ttemp_mas[7]>".($cookie{$temp_mess}?$cookie{$temp_mess}:"")."</textarea></td></tr>\n";
   }
   elsif ($ttemp_mas[4]=~ /^text/i) {
      $temp_form.="<tr><td>$ttemp_mas[3]</td><td><input size=$ttemp_mas[6] type=text maxlength=$ttemp_mas[5] name=$temp_mess value=\"".($cookie{$temp_mess}?$cookie{$temp_mess}:"")."\"></td></tr>\n";
   }
   elsif ($ttemp_mas[4]=~ /^etext/i) {
      $temp_form.="<tr><td>$ttemp_mas[3]</td><td><input size=$ttemp_mas[6] type=text maxlength=$ttemp_mas[5] name=$temp_mess value=\"".($cookie{$temp_mess}?$cookie{$temp_mess}:"")."\"></td></tr>\n";
   }
   elsif ($ttemp_mas[4]=~ /^utext/i) {
      $temp_form.="<tr><td>$ttemp_mas[3]</td><td><input size=$ttemp_mas[6] type=text maxlength=$ttemp_mas[5] name=$temp_mess value=\"".($cookie{$temp_mess}?$cookie{$temp_mess}:"")."\"></td></tr>\n";
   }
   elsif ($ttemp_mas[4]=~ /^select/i) {
      $temp_form.="<tr><td>$ttemp_mas[3]</td><td><select name=$temp_mess>";
      for $ti (5..$#ttemp_mas) {$temp_form.="<option value=\"$ttemp_mas[$ti]\" ".((!$cookie{$temp_mess} and $ti==5)||($cookie{$temp_mess} and "$cookie{$temp_mess}" eq "$ttemp_mas[$ti]")?"selected":"").">$ttemp_mas[$ti]</option>\n"}
      $temp_form.="</select></td></tr>\n";
   }
   elsif ($ttemp_mas[4]=~ /^radio/i) {
      $temp_form.="<tr><td>$ttemp_mas[3]</td><td>";
      for $ti (5..$#ttemp_mas) {$temp_form.="<input type=radio name=$temp_mess ".((!$cookie{$temp_mess} and $ti==5)||($cookie{$temp_mess} and "$cookie{$temp_mess}" eq "$ttemp_mas[$ti]")?"checked":"")." value=\"$ttemp_mas[$ti]\">$ttemp_mas[$ti]\n"}
      $temp_form.="</td></tr>\n";
   }
   elsif ($ttemp_mas[4]=~ /^iradio/i) {
      $temp_form.="<tr><td>$ttemp_mas[3]</td><td>";
      for $ti (5..$#ttemp_mas) {$temp_form.="<input type=radio name=$temp_mess ".((!$cookie{$temp_mess} and $ti==5)||($cookie{$temp_mess} and "$cookie{$temp_mess}" eq "$ttemp_mas[$ti]")?"checked":"")." value=\"$ttemp_mas[$ti]\"><img src=\"$ttemp_mas[$ti]\" border=0>\n"}
      $temp_form.="</td></tr>\n";
   }
}
if (!$mygbfullurl) {
    $temp_kill_url="http://$ENV{'HTTP_HOST'}$ENV{'DOCUMENT_URI'}\?$ENV{'QUERY_STRING'}&kill=1";}
else {$temp_kill_url="$mygbfullurl\?$ENV{'QUERY_STRING'}&kill=1"}
$temp_kill_url=~ s/&kill=1&kill=1//i;
$temp_form.=qq~
<tr><td colspan=2><input type=submit value="Сказать" $button_style>&nbsp;<a href="$temp_kill_url">K</a></td></tr>
</table></form>~.post_jscript(); return $temp_form;
}

#------------------------------------------------------#
sub time_print {
my $temp_time=shift;
($sec,$min,$hour,$mday,$mon,$year,$wday) = (localtime($temp_time))[0,1,2,3,4,5,6];
$year=$year+1900; $mon+=1;
$mon=sprintf("%.02d",$mon);
$mday=sprintf("%.02d",$mday);
$hour=sprintf("%.02d",$hour);
$min=sprintf("%.02d",$min);
$sec=sprintf("%.02d",$sec);
return "$hour:$min $mday-$mon-$year";
}

#------------------------------------------------------#
sub msg_print {
my $string=shift;
$string=~ s/\x02/ <br> /g;
        @temp_info_msg=split(/ /,$string); my $mainMMessage='';
        for ($i_msg=0;$i_msg<=$#temp_info_msg;$i_msg++) {
          my $info_go=$temp_info_msg[$i_msg];
          if ($info_go) {
            if (length($info_go)>$message{sword} and substr($info_go,0,5) ne "href=") {
                $to_prn="";
                for ($hj=0;$hj<=int(length($info_go)/$message{sword})-1;$hj++) {
                    $to_p=substr($info_go,$hj*$message{sword},$message{sword});
                    $to_prn="$to_prn$to_p <br>\n";
                }
                $hj=($hj)*$message{sword};
                $to_p=substr($info_go,$hj,length($info_go)-$hj);
                $to_prn="$to_prn$to_p\n";
                $mainMMessage.=" $to_prn";
            }
            else {
                $mainMMessage.=" $info_go";
            }
          }
        }
return $mainMMessage;
}

#------------------------------------------------------#
sub get_my_message {
if ($in{message}!~ /(\d+)/) {$cookie{errmessage}="File: /etc/passwd - Not found! Fuck out little cracker ;)"}
if (-e "temp/$in{message}") {
open TEMP,"temp/$in{message}";
while (<TEMP>) {
    if (!$cookie{errmessage}) {$cookie{errmessage}=$_}
    else {
        my $temp_string=$_;
        $temp_string=chdel($_);
        my ($name,$value)=split(/\x01/,$temp_string);
        $value=~ s/\x02/\n/g;
        $cookie{$name}=$value if ($value);
    }
}
close TEMP;
unlink "temp/$in{message}";
}
}

sub post_jscript {
return qq~
<script LANGUAGE="JavaScript">
    ua=navigator.userAgent;
    v=navigator.appVersion.substring(0,1);
    ch=document.charset;
    if ((ua.lastIndexOf("MSIE")!=-1) && (v!='1') && (v!='2') && (v!='3')) {
      if ((ch!="windows-1251") && (ch!="koi8-r") && (ch!="cp866") && (ch!="iso-8859-5"))
        document.pvdgb.coder.value="windows-1251";
      else
        document.pvdgb.coder.value=document.charset;
    }
    else {
    if (ua.indexOf("Win") != -1)
       document.pvdgb.coder.value="windows-1251";
    else if (ua.indexOf("DOS") != -1 || ua.indexOf("OS/2") != -1)
       document.pvdgb.coder.value="cp866";
    else if (ua.indexOf("Mac") != -1)
        document.pvdgb.coder.value="koi8-r";
        else
        document.pvdgb.coder.value="windows-1251";
    }
</script>
~;
}

