<?php

require "include/bittorrent.php";
dbconn();
loggedinorreturn();

 $res2=mysql_query("select konto from users where id=".$CURUSER[id]."");
       $r2=mysql_fetch_assoc($res2);

$datum2 = date("Y-m-d");

if($_SERVER["REQUEST_METHOD"]==="POST") {
  $destid=$_POST["destid"]+0;
  $gb=trim($_POST["gbgift"])+0;
  $ok=(ereg("^[0-9]+$",$gb)&&$gb>0);
  $faktor=$_POST["gbfaktor"]+0;

if ($gb <0)
          stderr("Sorry","Aber es ist nicht erlaubt negative Zahlen einzutragen");

  $touser = $_POST["touser"];


 if($_POST["toupload"]=="ja" && $datum2 != $CURUSER["kontodate"])
 {
    $sum=$gb*$faktor*1024;
    $nachricht = "Überweisung auf den Upload";
    if($sum <= 16106127360 && $CURUSER["konto"]-$sum >=$sum)
    {
    mysql_query("INSERT INTO konto (date, text, amount, userid) VALUES('" . get_date_time() . "','".$nachricht."','".$sum."','" . $CURUSER[id] . "')") ;

      mysql_query("update users set konto=konto-$sum where id=$CURUSER[id]");
      mysql_query("update users set uploaded=uploaded+$sum, kontodate='" . get_date_time() . "' where id=$CURUSER[id]");


      header("Location: konto.php");
    }
    else
      stderr("Sorry","Dein Konto erlaubt eine solche Überweisung nicht/Nur 15 GB pro Tag");
  }

  if($_POST["tokonto"]=="ja")
  {
    $sum=$gb*$faktor*1024;
    $nachricht = "Upload Überweisung";
    if($CURUSER["uploaded"]-$CURUSER["downloaded"]>=$sum)
    {
    mysql_query("INSERT INTO konto (date, text, amount, userid) VALUES('" . get_date_time() . "','".$nachricht."','".$sum."','" . $CURUSER[id] . "')") ;

      mysql_query("update users set konto=konto+$sum where id=$CURUSER[id]");
      mysql_query("update users set uploaded=uploaded-$sum where id=$CURUSER[id]");


      header("Location: konto.php");
    }
    else
      stderr("Sorry", "Deine Ratio erlaubt eine solche Spende nicht");
  }

}


stdhead("Konto");

begin_frame("Spielkonto");


$res=mysql_query("select * from konto where userid=".$CURUSER[id]." ORDER BY date DESC Limit 10");

if(mysql_num_rows($res)==0)
    {
    ?>

      <table cellpadding="4" cellspacing="1" border="0" style="width:700px" class="tableinborder">
      <tr class="tabletitle" width="100%">
      <td colspan="10" width="100%"><span class="smallfont"><center><b>Kontobewegungen</b></center></span></td></tr>
          <tr><td width="100%" class="tablea">

    <?



   $datum = date("d.m.Y");

     echo "keine Kontobewegungen</td><tr>";
   print ("<tr class=tablea><td class=tableb>$datum</td><td class=tablea>Kontostand</td><td class=tablea>".mksize($r2[konto])."");

    }
    else

    {

  ?>

  <table align="center" cellpadding="4" cellspacing="1" border="0" style="width:700px" class="tableinborder">
  <tr class="tabletitle" width="100%">
  <td colspan="10" width="100%"><span class="smallfont"><center>
  <b>Kontobewegungen</b>
</center></span></td></tr><tr><td width="100%" class="tablea">

 <table border="0" style="width:100%" class="tableinborder">
 <tr class="tabletitle" width="100%">
 <td width="30%" class="tableb">Datum</td><td width="50%" class="tablea">Zweck</td><td width="20%" class="tablea">Betrag</td></tr>

  <?


   while($r=mysql_fetch_assoc($res)) {


   print ("<tr><td class=tableb>".$r[date]."</td><td  class=tablea>".$r[text]."</td><td  class=tablea>".mksize($r[amount])."</td></tr>");


   }

   $res2=mysql_query("select konto from users where id=".$CURUSER[id]."");
   $r2=mysql_fetch_assoc($res2);


   $datum = date("d.m.Y");

   print ("<tr class=tablea><td class=tableb>$datum</td><td class=tablea>Kontostand</td><td class=tablea><center><b><font color=green>".mksize($r2[konto])."</font></b></center></td></tr>");

}
?>
</td></tr></table></table><br>


 <table align="center" cellpadding="4" cellspacing="1" border="0" style="width:700px" class="tableinborder">
  <tr class="tabletitle" width="100%">
  <td colspan="10" width="100%"><span class="smallfont"><center><b>Kontoüberweisungen</b></center></span></td></tr>

 <form action="konto.php" method="post">

    <input type="hidden" name="destid" value="<?=$user["id"]?>"/>
    <input type="hidden" name="tokonto" value="ja"/>
    <tr valign=top><td class=tableb>GB's aufs Spielkonto überweisen:
    <br/>
    <br/>
    <b>Max:<font color="red"> <?=mksize($CURUSER["uploaded"]-$CURUSER["downloaded"])?></font></b>
    </td><td class=tablea align=left>
    Von deinem Upload auf dein Spielkonto überweisen.<br/>
    <input type="text" name="gbgift"/>
    <select name="gbfaktor">
    <option value="1024">MB</option>
    <?php if($CURUSER["uploaded"]-$CURUSER["downloaded"]>1073741824) { ?>
    <option value="1048576">GB</option>
    <?php } ?>
    </select>
    <br/>
    <br/>
    <input type=submit class=btn style="height: 23px" value="OK"/>
    </form>
    </td></tr></table>
    <?

end_frame();
stdfoot();
?>