Advertisement
Guest User

Untitled

a guest
Dec 21st, 2015
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 9.79 KB | None | 0 0
  1. <?php
  2. require(__DIR__.'/globals.php');
  3. print "<h3>Item Market</h3>";
  4. $_GET['action'] = isset($_GET['action']) && ctype_alphanum($_GET['action']) ? strtolower(trim($_GET['action'])) : false;
  5. switch($_GET['action'])
  6. {
  7.     case "buy": item_buy(); break;
  8.     case "gift1": item_gift1(); break;
  9.     case "gift2": item_gift2(); break;
  10.     case "remove": itemm_remove(); break;
  11.     default: imarket_index(); break;
  12. }
  13. // going to add the code for get id here as its used multiple times
  14. $_GET['ID'] = isset($_GET['ID']) && ctype_digit($_GET['ID']) ? abs(intval($_GET['ID'])) : 0;
  15. // or simple
  16. // $_GET['ID'] = abs(intval($_GET['ID']));
  17. function imarket_index()
  18. {
  19.     global $db,$ir,$c,$userid,$h;
  20.     // whats this for? $check = $db->query("SELECT im.*,i.* FROM `itemmarket` im LEFT JOIN `items` i ON im.imITEM=i.itmid WHERE `imID` = {$_GET['ID']} AND `imADDER` = $userid");
  21.     print "Viewing all listings...
  22.     <table width=75% cellspacing=1 class='table'>
  23.         <tr style='background:gray'>
  24.             <th>Adder</th>
  25.             <th>Item</th>
  26.             <th>Price</th>
  27.             <th>Links</th>
  28.         </tr>";
  29.         $q = $db->query("SELECT im.*, i.*, u.*,it.* FROM `itemmarket` im LEFT JOIN `items` i ON im.imITEM=i.itmid LEFT JOIN `users` u ON u.userid=im.imADDER LEFT JOIN itemtypes it ON i.itmtype=it.itmtypeid ORDER BY i.itmtype, i.itmname ASC");
  30.         $lt = "";
  31.         while($r=$db->fetch_row($q))
  32.         {
  33.             if($lt!=$r['itmtypename'])
  34.             {
  35.                 $lt = $r['itmtypename'];
  36.                 print "\n<tr style='background: gray;'><th colspan=4>{$lt}</th></tr>";
  37.             }
  38.             if($r['imCURRENCY']=="money")  
  39.                 $price="\$".number_format($r['imPRICE']);  
  40.             else
  41.                 $price=number_format($r['imPRICE'])." crystals";
  42.             if($r['imADDER'] == $userid)
  43.                 $link = "[<a href='itemmarket.php?action=remove&ID={$r['imID']}'>Remove</a>]";
  44.             else
  45.                 $link = "[<a href='itemmarket.php?action=buy&ID={$r['imID']}'>Buy</a>] [<a href='itemmarket.php?action=gift1&ID={$r['imID']}'>Gift</a>]";
  46.             print "\n
  47.             <tr>
  48.                 <td><a href='viewuser.php?u={$r['userid']}'>{$r['username']}</a> [{$r['userid']}]</td>
  49.                 <td>{$r['itmname']}</td>
  50.                 <td>$price</td>
  51.                 <td>[<a href='iteminfo.php?ID={$r['itmid']}'>Info</a>] $link</td>
  52.             </tr>";
  53.         }
  54.     print "</table>";
  55. }
  56. function itemm_remove()
  57. {
  58.     global $db,$ir,$c,$userid,$h;
  59.     $q = $db->query("SELECT im.*,i.* FROM itemmarket im LEFT JOIN items i ON im.imITEM=i.itmid WHERE imID={$_GET['ID']} AND imADDER=$userid");
  60.     if(!$db->num_rows($q))
  61.     {
  62.         print "Error, either this item does not exist, or you are not the owner.<br /> <a href='itemmarket.php'>&gt; Back</a>";
  63.         $h->endpage();
  64.         exit;
  65.     }
  66.     $r = $db->fetch_row($q);
  67.     item_add($userid, $r['imITEM'], 1);
  68.     $i = ($db->insert_id()) ? $db->insert_id() : 99999;
  69.     $db->query("DELETE FROM itemmarket WHERE imID={$_GET['ID']}");
  70.     $db->query("INSERT INTO imremovelogs VALUES ('', {$r['imITEM']}, {$r['imADDER']}, $userid, {$r['imID']}, $i, unix_timestamp(), '{$ir['username']} removed a {$r['itmname']} from the item market.')");
  71.     print "Item removed from market!<br /> <a href='itemmarket.php'>&gt; Back</a>";
  72. }
  73. function item_buy()
  74. {
  75.     global $db,$ir,$c,$userid,$h;
  76.     $q = $db->query("SELECT * FROM itemmarket im LEFT JOIN items i ON i.itmid=im.imITEM WHERE imID={$_GET['ID']}",$c);
  77.     if(!$db->num_rows($q))
  78.     {
  79.         print "Error, either this item does not exist, or it has already been bought.<br /> <a href='itemmarket.php'>&gt; Back</a>";
  80.         $h->endpage();
  81.         exit;
  82.     }
  83.     $r = $db->fetch_row($q);
  84.     $curr = $r['imCURRENCY'];
  85.     if($r['imPRICE'] > $ir[$curr])
  86.     {
  87.         print "Error, you do not have the funds to buy this item.<br /> <a href='itemmarket.php'>&gt; Back</a>";
  88.         $h->endpage();
  89.         exit;
  90.     }
  91.     item_add($userid, $r['imITEM'], 1);
  92.     $i=($db->insert_id()) ? $db->insert_id() : 99999;
  93.     $db->query("DELETE FROM itemmarket WHERE imID={$_GET['ID']}");
  94.     $db->query("UPDATE users SET $curr=$curr-{$r['imPRICE']} where userid=$userid");
  95.     $db->query("UPDATE users SET $curr=$curr+{$r['imPRICE']} where userid={$r['imADDER']}");
  96.     if($curr == "money")
  97.     {
  98.         event_add($r['imADDER'],"<a href='viewuser.php?u=$userid'>{$ir['username']}</a> bought your {$r['itmname']} item from the market for \$".number_format($r['imPRICE']).".",$c);
  99.         $db->query("INSERT INTO imbuylogs VALUES ('', {$r['imITEM']}, {$r['imADDER']}, $userid,  {$r['imPRICE']}, {$r['imID']}, $i, unix_timestamp(), '{$ir['username']} bought a {$r['itmname']} from the item market for \${$r['imPRICE']} from user ID {$r['imADDER']}')");
  100.         print "You bought the {$r['itmname']} from the market for ".money_formatter($r['imPRICE']).".";
  101.     }
  102.     else
  103.     {
  104.         event_add($r['imADDER'],"<a href='viewuser.php?u=$userid'>{$ir['username']}</a> bought your {$r['itmname']} item from the market for ".number_format($r['imPRICE'])." crystals.",$c);
  105.         $db->query("INSERT INTO imbuylogs VALUES ('', {$r['imITEM']}, {$r['imADDER']}, $userid,  {$r['imPRICE']}, {$r['imID']}, $i, unix_timestamp(), '{$ir['username']} bought a {$r['itmname']} from the item market for {$r['imPRICE']} crystals from user ID {$r['imADDER']}')");
  106.         print "You bought the {$r['itmname']} from the market for ".money_formatter($r['imPRICE'], '')." crystals.";
  107.     }
  108. }
  109. function item_gift1()
  110. {
  111.     global $db,$ir,$c,$userid,$h;
  112.     $q=$db->query("SELECT * FROM itemmarket im LEFT JOIN items i ON i.itmid=im.imITEM WHERE imID={$_GET['ID']}");
  113.     if(!$db->num_rows($q))
  114.     {
  115.         print "Error, either this item does not exist, or it has already been bought.<br /> <a href='itemmarket.php'>&gt; Back</a>";
  116.         $h->endpage();
  117.         exit;
  118.     }
  119.     $r = $db->fetch_row($q);
  120.     $curr = $r['imCURRENCY'];
  121.     if($r['imPRICE'] > $ir[$curr])
  122.     {
  123.         print "Error, you do not have the funds to buy this item.<br /> <a href='itemmarket.php'>&gt; Back</a>";
  124.         $h->endpage();
  125.         exit;
  126.     }
  127.     if($curr == "money")
  128.     {
  129.         print "Buying the <b>{$r['itmname']}</b> for ".money_formatter($r['imPRICE'])." as a gift...<br />
  130.         <form action='itemmarket.php?action=gift2' method='post'>
  131.             <input type='hidden' name='ID' value='{$_GET['ID']}' />
  132.             User to give gift to: ".user_dropdown($c,'user')."<br />
  133.             <input type='submit' value='Buy Item and Send Gift' />
  134.         </form>";
  135.     }
  136.     else
  137.     {
  138.         print "Buying the <b>{$r['itmname']}</b> for ".money_formatter($r['imPRICE'], '')." crystals as a gift...<br />
  139.         <form action='itemmarket.php?action=gift2' method='post'>
  140.             <input type='hidden' name='ID' value='{$_GET['ID']}' />
  141.             User to give gift to: ".user_dropdown($c,'user')."<br />
  142.             <input type='submit' value='Buy Item and Send Gift' />
  143.         </form>";
  144.     }
  145. }
  146. $usercheck = $db->query("SELECT `userid` FROM `users` WHERE `userid` = ".$_POST['userid']);
  147. // Preform check using num_rows()
  148. if(!$db->num_rows($usercheck)) {
  149. // error
  150. $h->endpage();
  151. exit;
  152. }  
  153. function item_gift2()
  154. {
  155.     global $db,$ir,$c,$userid,$h;
  156.     $_POST['user'] = isset($_POST['user']) && ctype_digit($_POST['user']) ? abs(intval($_POST['user'])) : 0;
  157.     if(empty($_POST['user']) || empty($_POST['ID']))
  158.     {
  159.         echo "Something went wrong. <a href='itemmarket.php'>&gt; Back</a>";
  160.         $h->endpage();
  161.         exit;
  162.     }
  163.     $q = $db->query("SELECT * FROM itemmarket im LEFT JOIN items i ON i.itmid=im.imITEM WHERE imID={$_POST['ID']}");
  164.     if(!$db->num_rows($q))
  165.     {
  166.         print "Error, either this item does not exist, or it has already been bought.<br /> <a href='itemmarket.php'>&gt; Back</a>";
  167.         $h->endpage();
  168.         exit;
  169.     }
  170.     $r = $db->fetch_row($q);
  171.     $curr = $r['imCURRENCY'];
  172.     if($r['imPRICE'] > $ir[$curr])
  173.     {
  174.         print "Error, you do not have the funds to buy this item.<br /> <a href='itemmarket.php'>&gt; Back</a>";
  175.         $h->endpage();
  176.         exit;
  177.     }
  178.     item_add($_POST['user'], $r['imITEM'], 1);
  179.     $i=($db->insert_id()) ? $db->insert_id() : 99999;
  180.     $db->query("DELETE FROM itemmarket WHERE imID={$_POST['ID']}");
  181.     $db->query("UPDATE users SET $curr=$curr-{$r['imPRICE']} where userid=$userid");
  182.     $db->query("UPDATE users SET $curr=$curr+{$r['imPRICE']} where userid={$r['imADDER']}");
  183.     if($curr == "money")
  184.     {
  185.         event_add($r['imADDER'],"<a href='viewuser.php?u=$userid'>{$ir['username']}</a> bought your {$r['itmname']} item from the market for \$".number_format($r['imPRICE']).".",$c);
  186.         event_add($_POST['user'], "<a href='viewuser.php?u=$userid'>{$ir['username']}</a> bought you a {$r['itmname']} from the item market as a gift.",$c);
  187.         $u=$db->query("SELECT username FROM users WHERE userid={$_POST['user']}");
  188.         if(!$db->num_rows($u))
  189.         {
  190.             echo "Invalid User.";
  191.             $h->endpage();
  192.             exit;
  193.         }
  194.         $uname = ($db->num_rows($u)) ? $db->fetch_single($u) : "Error Invalid User";
  195.         $db->query("INSERT INTO imbuylogs VALUES ('', {$r['imITEM']}, {$r['imADDER']}, $userid,  {$r['imPRICE']}, {$r['imID']}, $i, unix_timestamp(), '{$ir['username']} bought a {$r['itmname']} from the item market for \${$r['imPRICE']} from user ID {$r['imADDER']} as a gift for $uname [{$_POST['user']}]')");
  196.         print "You bought the {$r['itmname']} from the market for \$".number_format($r['imPRICE'])." and sent the gift to $uname.";
  197.     }
  198.     else
  199.     {
  200.         event_add($r['imADDER'],"<a href='viewuser.php?u=$userid'>{$ir['username']}</a> bought your {$r['itmname']} item from the market for ".number_format($r['imPRICE'])." crystals.",$c);
  201.         event_add($_POST['user'], "<a href='viewuser.php?u=$userid'>{$ir['username']}</a> bought you a {$r['itmname']} from the item market as a gift.",$c);
  202.         $u=$db->query("SELECT username FROM users WHERE userid={$_POST['user']}");
  203.         if(!$db->num_rows($u))
  204.         {
  205.             echo "Invalid User.";
  206.             $h->endpage();
  207.             exit;
  208.         }
  209.         $uname = ($db->num_rows($u)) ? $db->fetch_single($u) : "Error Invalid User";
  210.         $db->query("INSERT INTO imbuylogs VALUES ('', {$r['imITEM']}, {$r['imADDER']}, $userid,  {$r['imPRICE']}, {$r['imID']}, $i, unix_timestamp(), '{$ir['username']} bought a {$r['itmname']} from the item market for {$r['imPRICE']} crystals from user ID {$r['imADDER']} as a gift for $uname [{$_POST['user']}]')");
  211.         print "You bought the {$r['itmname']} from the market for ".number_format($r['imPRICE'])." crystals and sent the gift to $uname.";
  212.     }
  213. }
  214. $h->endpage();
  215. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement