<?php
  if (isset($a))
  {
    if (!isset($already))
    {
      myflock();
      $sss=file("votion.dat");
      $f=fopen("votion.dat","w");
      $i=0;
      for ($i=0; $i<count($sss); $i++) $sss[$i]=str_replace(chr(13).chr(10),"",$sss[$i]);
      $sss[$sss[0]+$a+2]+=1;
      for ($i=0; $i<count($sss); $i++) $sss[$i].=chr(13).chr(10);
      for ($i=0; $i<count($sss); $i++) fputs($f, $sss[$i]);
      fclose($f);
      myfunlock();
      setcookie("already",0);
    } else unset($a);
  }

  function myflock()
  {
    $z=array();
    while (count($z)==0)
    {
      $z=file("flock.dat");
      if (count($z)==0)
      sleep(1);
    }
    $f=fopen("flock.dat","w");
    fclose($f);
  }

  function myfunlock()
  {
    $f=fopen("flock.dat","w");
    fwrite($f,"!");
    fclose($f);
  }

  function vote()
  {
    myflock();
    $i=0; $qall=0;
    $vnq=array();
    $vnn=array();
    $f=fopen("votion.dat","r");
    $vn=fgets($f,10);
    $vq=fgets($f,100);
    for ($i=1; $i<=$vn; $i++)
    {
      $t=fgets($f,100);
      array_push($vnq,$t);
    }
    for ($i=1; $i<=$vn; $i++)
    {
      $t=fgets($f,5);
      array_push($vnn,$t);
      $qall+=$t;
    }
    fclose($f);
    myfunlock();

    echo("<form action='' name=a><b>$vq</b><br>");
    for ($i=0; $i<$vn; $i++)
    {
      if ($qall>0) echo("<input type=radio value=$i name=a> $vnq[$i] (".ceil(($vnn[$i]/$qall)*100)."%)<br>"); else
      echo("<input type=radio value=$i name=a> $vnq[$i] (0%)<br>");
    }
    echo("<hr>Answers: <font size=1 face=marlett><a href=voteadm.php>".
    "q</a></font>$qall<br><input type=submit value='Vote'></form>");
  }

  function legal($a)
  {
    if (file_exists("votepwd.dat"))
    $pwd=file("votepwd.dat");
    if ($pwd[0]=md5($a)) return true; else
    {
      echo "Incorrect password. Try again...<br>";
      return false;
    }
  }
?>
