<?
include("const.inc");
If ($pwd <> "azaytsev") { exit; }

$dat = date( "l dS of F Y h:i:s A" );

If ($send == "ok") {
 echo "<p>Лотерея проведена!</p>";
 $ff = fopen($filedate, "w");
 fwrite($ff, $dat);
 fclose($ff);

 $fp = file($base);
 $i = 0;
 while ($fp[$i] <> null) {
  $i++;
 }

 srand((double)microtime()*1000000);
 $prize1 = rand(0,$i);

 for ($m = 0; $m < ceil($i * 0.01); $m++) {
  srand((double)microtime()*10000000);
  $tm1 = rand(0,$i);
  If ($tm1 = $prize1) { $tm1 = rand(0,$i); }
  $prize2[] = $tm1;

 }

 for ($m = 0; $m < ceil($i * 0.05); $m++) {
  srand((double)microtime()*1000000000);
  $tm1 = rand(0,$i);
  If ($tm1 = $prize1) { $tm1 = rand(0,$i); }
  $prize3[] = $tm1;
 }

 
 $fsw1 = $i * $lotsumm * 0.3;
 $fbi = fopen($filebilet, "w");
 echo "<p><b>[$prize1]</b> $fp[$prize1]\n";
 $msg = "1 выигрыш (" . $fsw1 . ")" . $fp[$prize1] . "\n";
 $findst = explode("^", $fp[$prize1]);
 fwrite($fbi, "1^$findst[4]");

 $r = 0;
 while($prize2[$r] <> null) {
  $nu = (integer) $prize2[$r];
  $findst = explode("^", $fp[$nu]);
  fwrite($fbi, "2^$findst[4]");
  echo "<p><b>[$nu]</b> $fp[$nu]\n";

  $kolvo = 0;
  while($prize2[$kolvo] <> null) {
   $kolvo++;
  }
  $fsw2 = ($i * $lotsumm * 0.2) / $kolvo;
  $msg = $msg . "2 выигрыш (" . (string) $fsw2 . ")" . $fp[$nu] . "\n";

  $r++;
 }

 $r = 0;
 while($prize3[$r] <> null) {
  $nu = (integer) $prize3[$r];
  $findst = explode("^", $fp[$nu]);
  fwrite($fbi, "3^$findst[4]");
  echo "<p><b>[$nu]</b> $fp[$nu]\n";

  $kolvo = 0;
  while($prize3[$kolvo] <> null) {
   $kolvo++;
  }
  $fsw3 = ($i * $lotsumm * 0.2) / $kolvo;
  $msg = $msg . "3 выигрыш (" . (string) $fsw3 . ")" . $fp[$nu] . "\n";

  $r++;
 }

 //mail("yrii@rubtsovsk.ru", "ВАЖНО!!! Выигрыши loto.bip.ru", "Выиграли следующие билеты:\n$msg");
 //$fmail = fopen("http://www.nps.ru/sendmail.php?subj=loto.bip.ru&msg=$msg", "r");

 $sv = fopen("lotery_result.txt","a");
 fwrite($sv,$msg);
 fwrite($sv,"============================\n");
 fclose($sv);

 fclose($fbi);

 //copy($base, $basesv);

 $del = fopen($base, "w");
 fclose($del);

}
ElseIf ($act == "print") {
 $fp = file($base);
 $i = 0;
 while ($fp[$i] <> null) {
  echo"<p style='margin-top: 2; margin-bottom: 2'><b>$i.</b> $fp[$i]</p>\n";
  $i++;
 }
}
Else {
 $fd = file($filedate);

 $fp = file($base);
 $i = 0;
 while ($fp[$i] <> null) {
  $i++;
 }

 echo "
 <p style='margin-top: 2; margin-bottom: 2'>последняя лотерея праведна
 <b>$fd[0]</b></p>
 <p style='margin-top: 2; margin-bottom: 2'>сегодня <b>$dat</b></p>
 <form method='POST'>
  <input type='hidden' value='ok' name='send'>
  <p style='margin-top: 2; margin-bottom: 2'><input type='submit' value='Провести!' name='B1'></p>
 </form>
 <p style='margin-top: 2; margin-bottom: 2'>билетов продано: <b>$i</b></p>
 <p style='margin-top: 2; margin-bottom: 2'><a href='?pwd=$pwd&act=print' target='_blank'>показать файл базы</a></p>";
}
?>