<?

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

$res = mysql_query("SELECT * FROM lottery_config") or sqlerr(__FILE__, __LINE__);
while ($arr = mysql_fetch_assoc($res))
$arr_config[$arr['name']] = $arr['value'];

$endday = $arr_config['end_date'];

if (!$arr_config["enable"])
stderr("Sorry", "Lotterie ist geschlossen.");

dbconn();

loggedinorreturn();

$ticket_amount_display = $arr_config['ticket_amount'];

if ($arr_config["ticket_amount_type"] == GB)
$arr_config['ticket_amount'] = 1024 * 1024 * 1024 * $arr_config['ticket_amount'];
else if ($arr_config["ticket_amount_type"] == MB)
$arr_config['ticket_amount'] = 1024 * 1024 * $arr_config['ticket_amount'];
$size = $arr_config['ticket_amount'];

$minupload = $size; //Minimum Upload Required to Buy Ticket!

stdhead("Tickets Page");

if (get_user_class() < 0)
{
print("<h1>Sorry</h2><p>Du musst regestriert sein <a href=faq.php><b>FAQ</b></a> um zu sehen welche User Klassen Spielen dürfen</p>");
die();
}

if (get_date_time() > $arr_config['end_date'])
{
print ("Sorry ich kann dir keine Tickets mehr verkaufen!");
die();
}

$res = mysql_query("SELECT downloaded, uploaded FROM users WHERE id = $CURUSER[id]") or die(mysql_error());
$result = mysql_fetch_assoc($res);


$purchaseable = $arr_config['user_tickets'];
if (($result2['tickets'] + $_REQUEST['number']) > $purchaseable || $_REQUEST['number'] < 1 )
{
print("<table class=frame width=737 cellspacing=0 cellpadding=5><tr><td><table class=main width=100% cellspacing=0 cellpadding=5><tr><td class=colhead align=left>ERROR</td></tr><tr><td>Die maximale Anzahl an Tickets die Du kaufen kannst ist $purchaseable<br></td></tr></table></td></tr></table>");
stdfoot();
die;
}

if (($minupload * $_REQUEST['number']) > $result["uploaded"] )
{
print("<table class=frame width=737 cellspacing=0 cellpadding=5><tr><td><table class=main width=100% cellspacing=0 cellpadding=5><tr><td class=colhead align=left>ERROR</td></tr><tr><td>Du hast nicht Upload auf deinem Konto um ein Ticket zu kaufen<br></td></tr></table></td></tr></table>");
stdfoot();
die;
}
$upload = $result["uploaded"] - ($minupload * $_REQUEST['number']);
mysql_query("UPDATE users SET uploaded=$upload WHERE id=". $CURUSER["id"]) or die(mysql_error());
$tickets = $_REQUEST['number'];
for ($i = 0; $i < $tickets; $i++)
mysql_QUERY("INSERT INTO tickets(user) VALUES($CURUSER[id])");
$me = mysql_num_rows(mysql_query("SELECT * FROM tickets WHERE user=" . $CURUSER["id"]));
print("<br>\n");

?>
<table border=1 width=600 cellspacing=0 cellpadding=5>
<tr><td class=tabletitle width=600 align=center>Army of Hell Lotterie</td></tr>
<tr><td class=tableb align=left>
Du hast  <?= $_REQUEST["number"]; ?> ticket gekauft<? if ($_REQUEST["number"] > 1) echo "s"; ?>!<br>
Du hast jetzt insgesamt <?= $me; ?> ticket(s) gekauft<??>!<br>
Dein Upload ist jetzt <?= mksize($upload); ?>!<br><br>
<a href=tickets.php>Zurück</a>
</td></tr></table>

<?
print("<br>\n");

stdfoot();
die;

?>
