Guest User

lottery.php

a guest
Jun 5th, 2015
378
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 5.54 KB | None | 0 0
  1. <?php
  2.  
  3. include(__DIR__.'/globals.php');
  4.  
  5. if($lottoconfig['pagedisable'] == 'Yes' && $ir['user_level'] != 2)
  6. {
  7.     echo "<span style='color:red'>Sorry but admins have disbaled the lottery system.</span>";
  8.     $h->endpage();
  9.     exit;
  10. }
  11.  
  12. $_GET['action'] = isset($_GET['action']) && ctype_alpha($_GET['action']) ? strtolower(trim($_GET['action'])) : '';
  13. switch($_GET['action'])
  14. {
  15.     case 'buy': buy_tickets(); break;
  16.     default: lotto(); break;
  17. }
  18.  
  19. function lotto()
  20. {
  21.     global $db,$ir,$set,$h,$lottoconfig,$allowed;
  22.     $count = $db->query("SELECT COUNT(`user`) FROM `lottery_players`");
  23.     $amt = $db->fetch_single($count);
  24.     echo "<h4>{$set['game_name']} Lottery</h4>
  25.         <div style='background:#ececec; width: 400px; padding: 7px; border: 1px solid gray;'>
  26.             <img src='http://4e7221.medialib.glogster.com/media/89879e031a5dbed8d1bf0e877ee49cb2a02e02f15f87bde3408ccfcc30c163b1/dollar-sign.png'
  27.                 width='200px' height='100px' /><br /><br />
  28.             <span>Welcome to the lottery</span>
  29.             <span>Each ticket costs <strong>".money_formatter($lottoconfig['ticketprice'])."</strong></span><br />
  30.             <span>Maximum of <strong>".money_formatter($lottoconfig['maxtickets'], '')."</strong> tickets per person</span>
  31.             <hr width='75%' align='center'>
  32.             <strong>Loto Info</strong><br /><br />
  33.             <span>Jackpot: ".money_formatter($lottoconfig['ticketprice'] * $amt)."</span><br />
  34.             <span>Tickets Purchased: ".money_formatter($amt, '')."</span><br />
  35.             <span>Your Tickets: ".money_formatter($ir['tickets'], '')."</span><br /><br />
  36.             <form method='post' action='lottery.php?action=buy'>
  37.                 Buy Tickets: <input style='background: #BCC6CC;' type='text' name='amount' value='1' maxlength='2' />
  38.                 <button style='background: #BCC6CC; width: 100px; padding: 5px;'>Buy</button>
  39.             </form>";
  40.             if($ir['user_level'] == 2)
  41.             {
  42.                 if(isset($_POST['updatesettings']))
  43.                 {
  44.                     $_POST['tp'] = isset($_POST['tp']) && ctype_digit($_POST['tp']) ? abs(intval($_POST['tp'])) : 0;
  45.                     $_POST['mut'] = isset($_POST['mut']) && ctype_digit($_POST['mut']) ? abs(intval($_POST['mut'])) : 0;
  46.                    
  47.                     if(empty($_POST['tp']) || empty($_POST['mut']))
  48.                     {
  49.                         echo "You have missed a required field.";
  50.                         $h->endpage();
  51.                         exit;
  52.                     }
  53.                    
  54.                     $check = $db->query("SELECT `ticketprice`,`maxtickets` FROM `lottery_config`");
  55.                     if($db->num_rows($check))
  56.                         $db->query("UPDATE `lottery_config` SET `ticketprice` = {$_POST['tp']}, `maxtickets` = {$_POST['mut']}");
  57.                     else
  58.                         $db->query("INSERT INTO `lottery_config` VALUES('{$_POST['tp']}','{$_POST['mut']}')");
  59.                     echo "Info has been updated.";
  60.                 }
  61.                 else if(isset($_GET['disable']))
  62.                 {
  63.                     $select = $db->query("SELECT `pagedisable` FROM `lottery_config`");
  64.                     if($db->fetch_single($select) == 'Yes')
  65.                     {
  66.                         $db->query("UPDATE `lottery_config` SET `pagedisable` = 'No' WHERE `pagedisable` = 'Yes'");
  67.                         echo "You have enabled the page again users can access the lottery.";
  68.                     }
  69.                     else
  70.                     {
  71.                         $db->query("UPDATE `lottery_config` SET `pagedisable` = 'Yes' WHERE `pagedisable` = 'No'");
  72.                         echo "You have disabled the page users can't access the lottery.";
  73.                     }
  74.                 }
  75.                 else
  76.                 {
  77.                     echo "<br /><br /><hr width='50%' align='center'>
  78.                     <h3>Lottery Admin Panel</h3>
  79.                     <p>Fill out the form below to create / update the price and max tickets.</p><br />
  80.                     <form method='post'>
  81.                         Cost Per Ticket: $<input type='text' name='tp' value='1' /><br /><br />
  82.                         Max Tickets Per User: <input type='text' name='mut' value='1' /><br /><br />
  83.                         <input type='submit' name='updatesettings' value='Update Lottery Settings' />
  84.                     </form><br /><br />
  85.                     <a href='lottery.php?disable'>[Turn On/Off Lottery Page]</a>";
  86.                 }
  87.             }
  88.         echo "</div>";
  89. $h->endpage();
  90. }
  91.  
  92. function buy_tickets()
  93. {
  94.     global $db,$ir,$h,$lottoconfig;
  95.     $_POST['amount'] = isset($_POST['amount']) && ctype_digit($_POST['amount']) ? abs(intval($_POST['amount'])) : 0;
  96.     if(empty($_POST['amount']))
  97.     {
  98.         echo "Invalid Format.";
  99.         $h->endpage();
  100.         exit;
  101.     }
  102.     $deficit = $lottoconfig['maxtickets'] - $ir['tickets'];
  103.     if($ir['tickets'] + $_POST['amount'] > $lottoconfig['maxtickets'])
  104.     {
  105.         echo "The max tickets is ".number_format($lottoconfig['maxtickets']).".
  106.         This means you can only buy ".number_format($lottoconfig['maxtickets'] - $ir['tickets'])." more ticket".($deficit == 1 ? '' : 's');
  107.         $h->endpage();
  108.         exit;
  109.     }
  110.     if($_POST['amount'] > $lottoconfig['maxtickets'])
  111.     {
  112.         echo "You can't purchase this amount of tickets..";
  113.         $h->endpage();
  114.         exit;
  115.     }
  116.     if($ir['money'] < $lottoconfig['ticketprice'] * $_POST['amount'])
  117.     {
  118.         echo "You don't have enough you need ".money_formatter($lottoconfig['ticketprice'] * $_POST['amount'] - $ir['money']) ." more.";
  119.         $h->endpage();
  120.         exit;
  121.     }
  122.     if($ir['money'] >= $lottoconfig['ticketprice'] * $_POST['amount'] && $_POST['amount'] <= $lottoconfig['maxtickets'])
  123.     {  
  124.         $ir['money'] -= $lottoconfig['ticketprice'] * $_POST['amount'];
  125.         $ir['tickets'] += $_POST['amount'];
  126.         for($i = 1; $i <= $_POST['amount']; $i++)
  127.             $db->query("INSERT INTO `lottery_players` VALUES('{$ir['userid']}')");
  128.         $db->query("UPDATE `users` SET `money` = {$ir['money']}, `tickets` = {$ir['tickets']} WHERE `userid` = {$ir['userid']}");
  129.         echo "You have purchased ".money_formatter($_POST['amount'], '')." tickets for ".money_formatter($lottoconfig['ticketprice'] * $_POST['amount']);
  130.     }
  131. $h->endpage();
  132. }
Advertisement
Add Comment
Please, Sign In to add comment