<?php
/*
*----------------------------phpMyBitTorrent V 2.0-beta4-----------------------*
*--- The Ultimate BitTorrent Tracker and BMS (Bittorrent Management System) ---*
*--------------   Created By Antonio Anzivino (aka DJ Echelon)   --------------*
*-------------               http://www.p2pmania.it               -------------*
*------------ Based on the Bit Torrent Protocol made by Bram Cohen ------------*
*-------------              http://www.bittorrent.com             -------------*
*------------------------------------------------------------------------------*
*------------------------------------------------------------------------------*
*--   This program is free software; you can redistribute it and/or modify   --*
*--   it under the terms of the GNU General Public License as published by   --*
*--   the Free Software Foundation; either version 2 of the License, or      --*
*--   (at your option) any later version.                                    --*
*--                                                                          --*
*--   This program is distributed in the hope that it will be useful,        --*
*--   but WITHOUT ANY WARRANTY; without even the implied warranty of         --*
*--   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the          --*
*--   GNU General Public License for more details.                           --*
*--                                                                          --*
*--   You should have received a copy of the GNU General Public License      --*
*--   along with this program; if not, write to the Free Software            --*
*-- Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA --*
*--                                                                          --*
*------------------------------------------------------------------------------*
*------              ©2005 phpMyBitTorrent Development Team              ------*
*-----------               http://phpmybittorrent.com               -----------*
*------------------------------------------------------------------------------*
*-----------------   Sunday, September 14, 2008 9:05 PM   ---------------------*
*/

include("header.php");
global $db, $db_prefix;

function stderr($heading = "", $text, $sort = "") {
 
  OpenTable2("<font color=red>$sort: $heading</font>", center);
  echo $text;
  CloseTable2();
 
  die;
}
$res = $db->sql_query("SELECT * FROM ".$db_prefix."_lottery_config") or die(mysql_error());
while ($arr = $db->sql_fetchrow($res))
$arr_config[$arr['name']] = $arr['value'];

if (!$arr_config["enable"])
stderr("Sorry", "Lottery is disabled.");
$access_level = $arr_config["class_allowed"];
switch ($access_level) {
        case "user": {
                if (!$user->user) loginrequired("user");
                break;
        }
        case "premium": {
                if (!$user->premium) loginrequired("premium");
                break;
        }
        case "moderator": {
                if (!$user->moderator) loginrequired("moderator");
                break;
        }
        case "admin": {
                if (!$user->admin) loginrequired("admin");
                break;
        }
}




if (!$user->user) loginrequired("user");

$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'];

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

$ratioerr = "<font color=\"red\"><b>You must have uploaded atleast $arr_config[ticket_amount] $arr_config[ticket_amount_type] in order to buy a ticket!</b></font>";

OpenTable("Tickets Page");
Opentable2("Buy Lottery Tickets", center);
$total = $db->sql_numrows($db->sql_query("SELECT * FROM ".$db_prefix."_tickets"));
if ($arr_config["use_prize_fund"])
$pot = $prize_fund;
else
$pot = $total * $size;
$me = $db->sql_numrows($db->sql_query("SELECT * FROM ".$db_prefix."_tickets WHERE user=".getuserid($btuser).""));
$me2 = $db->sql_query("SELECT * FROM ".$db_prefix."_tickets WHERE user=".getuserid($btuser)." ORDER BY id ASC");
while ($myrow = $db->sql_fetchrow($me2))
$ticketnumbers .= "$myrow[id] ";

$purchaseable = $arr_config['user_tickets'] - $me;

if ($me >= $arr_config["user_tickets"])
$purchaseable = 0;

if (gmdate("Y-m-d H:i:s", time()) > $arr_config['end_date'])
$purchaseable = 0;

print("<br>\n");

?>


<table border=1 width=600 cellspacing=0 cellpadding=5>
<tr><td class=tabletitle width=600 align=center>MGA Lottery</td></tr>
<tr><td align=left class=tableb>
<ul>
<li>Tickets are non-refundable</li>
<li>Each ticket costs <?php echo $ticket_amount_display . ' ' . $arr_config['ticket_amount_type']; ?> which is taken from your upload amount</li>
<li>Purchaseable shows how many tickets you can afford</li>
<li>You can only buy upto your purchaseable amount.</li>
<li>The competiton will end: <?php echo $arr_config["end_date"]; ?></li>
<li>There will be <?php echo $arr_config['total_winners']; ?> winners who will be picked at random</li>
<li>Each winner will get <?php echo mksize($pot/$arr_config['total_winners']); ?> added to their upload amount</li>
<li>The Winners will be announced once the lottery has closed and posted on the home page.</li>
<?php
if (!$arr_config["use_prize_fund"])
{
?>
<li>The more tickets that are sold the bigger the pot will be!</li>
<?php
}
?>
<li>You own ticket numbers: <?= $ticketnumbers; ?></li>
</ul>
Good Luck!
<hr>
<table align=center width=40% class=frame border=1 cellspacing=0 cellpadding=10><tr><td align=center>
<table width=100% class=tableb class=main border=1 cellspacing=0 cellpadding=5>
<tr>
<td class=tableb>Total Pot</td>
<td class=tableb><?php echo mksize($pot); ?></td>
</tr>
<tr>
<td class=tableb>Total Tickets Purchased</td>
<td class=tableb align=right><?php echo $total; ?> Tickets</td>
</tr>
<tr>
<td class=tableb>Tickets Purchased by You</td>
<td class=tableb align=right><?php echo $me; ?> Tickets</td>

</tr>
<tr>
<td class=tableb>Purchaseable</td>
<td class=tableb align=right><?php echo $purchaseable; ?> Tickets</td>
</tr>
</table>
</table>
<hr>
<?php
if ($purchaseable > 0)
{
?>
<center>
<form method="post" action="purchasetickets.php">
Purchase <input type="text" name="number"> Tickets <input type="submit" value="Purchase">
</form>
</center>
<?php
}
else if (gmdate("Y-m-d H:i:s", time()) > $arr_config['end_date'])
{
?>
<center><h1><font color = "red">Lottery is closed!</font></h1>
<?php
}
?>
</td></tr></table>

<?php print("<br>\n");
CloseTable2();
CloseTable();
include("footer.php");
die;

?>

