#! /usr/sbin/perl
#
# 投票フォーム WebVOTE v2.0 is Free.
#
# Script written by Kazu Yanaka
# Created on: ９６年２月２２日
# Last Modified on: ９６年３月１７日
# I can be reached at: rescue@ask.or.jp
# Scripts Found at: http://www.ask.or.jp/~rescue/
#

require '../../../cgi-bin/jcode.pl'; 

#
# 初期設定
#

# 内容を入れるファイル指定します。vote.dat は chmod 666 に設定します。

$an_file = './vote38.dat';

$title = "連打勝負・封神演義〇〇限定大レース";
$an_title = "第３８回 ： 女禍・大爆笑";
$sub_title = "ギャグ強化月間です";

# 最大アンケート可能件数の設定です。

$rescue = "65535";

# グラフの「+」印は割合に応じた数だけ表示されますが、例えば90%の場合には90個
# 表示されてしまいます。次の値を 2 にすれば45個、3 にすれば30個、という様に
# 個数を次の値で割った数だけ表示されます。

$gyao = "5";

#
###########################################################################################

($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time);
if ($sec < 10)  { $sec = "0$sec";   }
if ($min < 10)  { $min = "0$min";   }
if ($hour < 10) { $hour = "0$hour"; }
$month = ($mon + 1);

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

$date = "$hour\:$min\:$sec $month\:$mday\:$year";
$date_now = "$year年$month月$mday日 $hour時$min分$sec秒";

if ($ENV{'REQUEST_METHOD'} eq "POST") { read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'}); }
else { $buffer = $ENV{'QUERY_STRING'}; }

if ($buffer eq "") { &html_header; &html_form; &html_trailer; }
else { $tnx = "1"; &an_w; &html_header; &html_trailer; }

sub html_header {

open(DB,"$an_file") || die "error\n";
@lines = <DB>;
close(DB);

$accesses = @lines;

if ($lines[0] =~ /\[(.*)\] (.*)/) {
   $first_date = $1;
      ($clock,$day) = split(/ /,$first_date);
      ($hour,$minute,$second) = split(/:/,$clock);
      ($month,$mday,$year) = split(/:/,$day);
   $first_date = "$year年$month月$mday日 $hour時$minute分";
}
else { $first_date = 0; }

if ($lines[($accesses - 1)] =~ /\[(.*)\] (.*)/) {
   $last_date = $1;
      ($clock,$day) = split(/ /,$last_date);
      ($hour,$minute,$second) = split(/:/,$clock);
      ($month,$mday,$year) = split(/:/,$day);
   $last_date = "$year年$month月$mday日 $hour時$minute分";
}
else { $last_date = 0; }

foreach $line (@lines) {

   if ($line =~ /\[(.*)\] (.*)/) {

      $an = $2;

      if ($an ne ' ' && $an ne '') {
         push(@AN, $an);
      }

   }
}

$i = 0;

foreach (@AN) { $ans{($_)[0]}++; $i++; }

   print "<html><head><title>$title</title></head>\n";
   print "<BODY BGCOLOR=\"#FFFFFF\" TEXT=\"#000000\" LINK=\"#229955\" VLINK=\"#999933\" ALINK=\"#FF0000\">\n";
   print "<h1>$title<hr></h1>\n";

   print "<h2>$an_title</h2>\n";
   print "$sub_title<p>\n";
   print "アンケート集計結果は以下のとおりとなりました。<br>\n";

   print "<b>全件数:<i><b>$accesses</b></i>　有効回答数:<i><b>$i</b></i><p>\n";
   print "<table border width=100%>\n";
   print "<tr><th>項目名</th><th>選択数</th><th>割合</th><th>グラフ<br></th></tr>\n";

   foreach (sort { $ans{$b} <=> $ans{$a} } keys %ans) {

        print "<tr>\n";
        $total_ans += $ans{$_};

        $percent_ans = int((int(10000 * ($ans{$_} / $i)) / 100));

        $ww=($percent_ans / $gyao);$kome = '+' x $ww;
        print "<th>$_<br></th><th>$ans{$_}</th><th>$percent_ans\% </th><td nowrap>$kome<br></td>\n";
        print "</tr>\n";
   }
   print "</table>\n";
}

sub html_trailer {
   print "<p><hr>\n";

   if ($tnx eq "1") {  print "<p><h2>ありがとうございました。</h2>\n"; }

  print "<h4 align=right><a href=\"http://www.ask.or.jp/~rescue/\">WebVOTE is Free.</a></h4>\n";
  print "</body></html>\n";

}

sub html_form {

    print "<form method=\"post\" action=\"vote38.cgi\">\n";
    print "<input type=hidden name=\"write\" value=\"write\">\n";

}

sub an_w {

@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/</&lt;/g; # ＳＳＩ安全対策
          $value =~ s/>/&gt;/g;
          $value =~ s/"/&quot;/g;

    if ($name ne "write") {
        &jcode'convert(*value,'sjis');
        $FORM{$name} = $value;
    }
}

    if ($FORM{'an'} eq "この中に該当があれば選択してください") { $FORM{'an'} = ""; }
    if ($FORM{'an'} ne "" && $FORM{'an2'} ne "") { $write = ""; }

    elsif ($FORM{'an'} ne "") { $write = "$FORM{'an'}"; }
    elsif ($FORM{'an2'} ne "") { $write = "$FORM{'an2'}"; }

    open(LOG,"$an_file") || die "error\n";
    @lines=<LOG>;
    close(LOG);

    $axs=@lines;
    $rescue2 = ($rescue - 1);

    if ($axs >= $rescue) {
      open(LOG,">$an_file") || die "error\n";
        foreach $axs (1..$rescue2) {
          print LOG $lines[$axs];
        }
    }
    else {
       open(LOG,">>$an_file") || die "error\n";
    }

    print LOG "[$date] $write\n";
    close(LOG);
}
# end of script.