Guest User

myshop.php

a guest
May 3rd, 2015
372
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 12.25 KB | None | 0 0
  1. <?php
  2.  
  3. /*
  4.  * User Shops v1.00
  5.  * This module is a free
  6.  * addon for mccodes v2
  7. */
  8.  
  9. require(__DIR__.'/globals.php');
  10. $user_shop = $db->query("SELECT * FROM `usershops` WHERE `owner` = {$ir['userid']}");
  11. if(!$db->num_rows($user_shop))
  12. {
  13.     $shop_cost = 250000;
  14.     if($ir['userid'] == 1)
  15.         $shop_cost = 1;
  16.     if(isset($_POST['create']))
  17.     {
  18.         if (!isset($_POST['verf']) || !verify_csrf_code('us_buyshop',stripslashes($_POST['verf'])))
  19.             csrf_error('buyshop');
  20.         if($ir['money'] < $shop_cost)
  21.         {
  22.             echo "You don't have enough money. You need ".money_formatter($shop_cost - $ir['money'])." more.";
  23.             $h->endpage();
  24.             exit;
  25.         }
  26.         $sn = isset($_POST['sname']) && preg_match("/^[a-z0-9_]+([\\s]{1}[a-z0-9_]|[a-z0-9_])+$/i",$_POST['sname'])
  27.                     ? $db->escape(strip_tags(stripslashes($_POST['sname']))) : '';
  28.             $sdesc = isset($_POST['sdesc']) ? $db->escape(strip_tags(stripslashes($_POST['sdesc']))) : '';
  29.         $status = isset($_POST['status']) && ctype_digit($_POST['status']) ? abs(intval($_POST['status'])) : 0;
  30.         $pic = isset($_POST['spic']) && is_string($_POST['spic']) ? stripslashes($_POST['spic']) : '';
  31.         if(!empty($pic))
  32.             {
  33.                 if(strlen($pic) < 8 || !(substr($pic, 0, 7) == 'http://' || substr($pic, 0, 8 == 'https://')))
  34.                 {
  35.                         echo "Invalid Image.<br />&gt; <a href='myshop.php'>Go Back</a>";
  36.                         $h->endpage();
  37.                         exit;
  38.                 }
  39.                 $sz = get_filesize_remote($pic);
  40.                 if($sz <= 0 || $sz >= 1048576)
  41.                 {
  42.                         echo "Invalid new pic entered.<br />&gt; <a href='myshop.php'>Back</a>";
  43.                         $h->endpage();
  44.                         exit;
  45.                 }
  46.                 $image = (@getimagesize($pic));
  47.                 if(!is_array($image))
  48.                 {
  49.                         echo "Invalid Image.<br />&gt; <a href='myshop.php'>Go Back</a>";
  50.                         $h->endpage();
  51.                         exit;
  52.                 }
  53.             }
  54.         if(empty($sn) || empty($sdesc) || empty($status))
  55.         {
  56.             echo "You have missed a required feild.<br />&gt; <a href='myshop.php'>Go Back</a>";
  57.             $h->endpage();
  58.                     exit;
  59.         }
  60.         else
  61.         {
  62.             $db->query("UPDATE `users` SET `money` = `money` - {$shop_cost} WHERE `userid` = {$ir['userid']}");
  63.             $db->query("INSERT INTO `usershops` (`name`,`desc`,`pic`,`status`,`owner`,`cdate`) "."
  64.                 VALUES('{$sn}','{$sdesc}','{$pic}','{$status}',{$ir['userid']},".time().")");
  65.             echo "You have paid ".money_formatter($shop_cost)." to buy a shop.";
  66.         }
  67.     }
  68.     else
  69.     {
  70.         $code = request_csrf_code('us_buyshop');
  71.         echo "<h3>Create Shop</h3>
  72.         <i>If you wish to create a shop it will cost you ".money_formatter($shop_cost)."</i><br /><br />
  73.         <form method='post'>
  74.             Shop Name: <input type='text' name='sname' /><br /><br />
  75.             Shop Desc: <input type='text' name='sdesc' /><br /><br />
  76.             Shop Pic: <input type='text' name='spic' /><br /><br />
  77.             Shop Closed: <input type='radio' name='status' value='1' checked='checked' />Yes <input type='radio' name='status' value='2' />No<br /><br />
  78.             <input type='hidden' name='verf' value='{$code}' />
  79.             <input type='submit' name='create' value='Create Shop' />
  80.         </form>";  
  81.     }
  82. }
  83. else
  84. {
  85.     $us = $db->fetch_row($user_shop);
  86.     if(isset($_POST['addtoshop']))
  87.     {
  88.         $wts = isset($_POST['wts']) && in_array($_POST['wts'], array('item','donatordays','crystals'),true) ? $_POST['wts'] : 'item';
  89.         $itmid = isset($_POST['itmid']) && ctype_digit($_POST['itmid']) ? abs(intval($_POST['itmid'])) : 0;
  90.         $qty = isset($_POST['qty']) && ctype_digit($_POST['qty']) ? abs(intval($_POST['qty'])) : 0;
  91.         $tcost = isset($_POST['tcost']) && ctype_digit($_POST['tcost']) ? abs(intval($_POST['tcost'])) : 0;
  92.        
  93.         if($_POST['wts'] == 'item')
  94.         {
  95.             if(empty($itmid) || empty($wts) || empty($qty) || empty($tcost))
  96.             {
  97.                 echo "Invalid.";
  98.                 $h->endpage();
  99.                 exit;
  100.             }
  101.             else
  102.             {
  103.                 $q = $db->query("SELECT `inv_qty`,`inv_itemid`,`inv_id`,`itmname` FROM `inventory` AS `iv`
  104.                                 INNER JOIN `items` AS `i` ON `iv`.`inv_itemid` = `i`.`itmid` = {$_POST['itmid']} AND `inv_userid` = $userid");
  105.                     if($db->num_rows($q) == 0)
  106.                     {
  107.                         $db->free_result($q);
  108.                         echo "Invalid Item ID. or you don't have any of those.";
  109.                     }
  110.                     else
  111.                     {
  112.                         $r = $db->fetch_row($q);
  113.                         $db->free_result($q);
  114.                         if($r['inv_qty'] < $_POST['qty'])
  115.                         {
  116.                                 echo 'You do not have enough of this item.';
  117.                                 $h->endpage();
  118.                                 exit;
  119.                         }
  120.                         $checkq = sprintf(
  121.                             'SELECT `id`,`sID` FROM `user_shopitems` WHERE `selling` = %u AND `tprice` = %u AND `sID` = %u',
  122.                                 $r['inv_itemid'], $tcost, $us['id']
  123.                         );
  124.                         $checkq = $db->query($checkq);
  125.                         if($db->num_rows($checkq) > 0)
  126.                         {
  127.                                 $cqty = $db->fetch_row($checkq);
  128.                                 $query = sprintf('UPDATE `user_shopitems` SET `sqty` = `sqty` + %u WHERE `sID` = %u
  129.                                 AND `id` = %u', $qty,$cqty['sID'],$cqty['id']);
  130.                                 $db->query($query);
  131.                         }
  132.                         else
  133.                                 $db->query("INSERT INTO `user_shopitems` VALUES (NULL,'{$wts}','{$r['inv_itemid']}',{$qty},{$tcost},{$us['id']})");
  134.                         $db->free_result($checkq);
  135.                         item_remove($userid, $r['inv_itemid'], $qty);
  136.                         echo "Item added to shop.";
  137.                 }
  138.             }
  139.         }
  140.         else if($_POST['wts'] == 'crystals')
  141.         {
  142.             if(!empty($itmid))
  143.                 echo "Item id slot must be empty when no item selected..";
  144.             else if(empty($wts) || empty($qty) || empty($tcost))
  145.                 echo "Invalid missed required feild..";
  146.             else
  147.             {
  148.                 if($ir['crystals'] < $_POST['qty'])
  149.                     {
  150.                             echo 'You do not have enough crystals you need '.number_format($_POST['qty'] - $ir['crystals']).' more.';
  151.                             $h->endpage();
  152.                             exit;
  153.                     }
  154.                     $checkq = sprintf(
  155.                         'SELECT `id`,`sID` FROM `user_shopitems` WHERE `tprice` = %u AND `sID` = %u',$tcost, $us['id']
  156.                     );
  157.                     $checkq = $db->query($checkq);
  158.                     if($db->num_rows($checkq) > 0)
  159.                     {
  160.                             $cqty = $db->fetch_row($checkq);
  161.                             $query = sprintf('UPDATE `user_shopitems` SET `sqty` = `sqty` + %u WHERE `sID` = %u
  162.                                 AND `id` = %u', $qty,$cqty['sID'],$cqty['id']);
  163.                             $db->query($query);
  164.                     }
  165.                     else
  166.                             $db->query("INSERT INTO `user_shopitems` (`stype`,`sqty`,`tprice`,`sID`) "." VALUES ('{$wts}',{$qty},{$tcost},{$us['id']})");
  167.                     $db->free_result($checkq);
  168.                     $query = sprintf(
  169.                         'UPDATE `users` SET `crystals` = `crystals` - %u WHERE `userid` = %u', $qty,$ir['userid']
  170.                     );
  171.                     $query = $db->query($query);
  172.                     $qty = number_format($qty);
  173.                     echo "[{$qty}] Crystals added to shop.";
  174.             }
  175.         }
  176.         else
  177.         {
  178.             if(!empty($itmid))
  179.                 echo "Item id slot must be empty when no item selected..";
  180.             else if(empty($wts) || empty($qty) || empty($tcost))
  181.                 echo "Invalid missed required feild..";
  182.             else
  183.             {
  184.                 if($ir['donatordays'] < $_POST['qty'])
  185.                     {
  186.                             echo 'You do not have enough donator days you need '.number_format($_POST['qty'] - $ir['donatordays']).' more.';
  187.                             $h->endpage();
  188.                             exit;
  189.                     }
  190.                     $checkq = sprintf(
  191.                         'SELECT `id`,`stype`,`tprice` FROM `user_shopitems`
  192.                             WHERE `stype` = "%s" AND `tprice` = %u AND `sID` = %u',$_POST['wts'], $tcost, $us['id']
  193.                     );
  194.                     $checkq = $db->query($checkq);
  195.                     if($db->num_rows($checkq) > 0)
  196.                     {
  197.                             $cqty = $db->fetch_row($checkq);
  198.                             $query = sprintf('UPDATE `user_shopitems` SET `sqty` = `sqty` + %u WHERE `sID` = %u
  199.                                 AND `id` = %u', $qty,$cqty['sID'],$cqty['id']);
  200.                             $db->query($query);
  201.                     }
  202.                     else
  203.                             $db->query("INSERT INTO `user_shopitems` (`stype`,`sqty`,`tprice`,`sID`) "." VALUES ('{$wts}',{$qty},{$tcost},{$us['id']})");
  204.                     $db->free_result($checkq);
  205.                     $query = sprintf(
  206.                         'UPDATE `users` SET `donatordays` = `donatordays` - %u WHERE `userid` = %u', $qty,$ir['userid']
  207.                     );
  208.                     $query = $db->query($query);
  209.                     $qty = number_format($qty);
  210.                     echo "[{$qty}] Donator Days added to shop.";
  211.             }
  212.         }
  213.     }
  214.     else
  215.     {
  216.         if(isset($_GET['remove']))
  217.         {
  218.             $_GET['remove'] = isset($_GET['remove']) && ctype_digit($_GET['remove']) ? abs(intval($_GET['remove'])) : 0;
  219.             if(empty($_GET['remove']))
  220.             {
  221.                 echo "Invalid Format..";
  222.                 $h->endpage();
  223.                 exit;
  224.             }
  225.             $check = $db->query("SELECT `owner` FROM `usershops` WHERE `owner` = {$ir['userid']}");
  226.             if(!$db->num_rows($check))
  227.                 echo "You don't have a shop..";
  228.             else
  229.             {
  230.                 $check_item = $db->query("SELECT * FROM `user_shopitems` WHERE `id` = {$_GET['remove']} AND `sID` = {$us['id']}");
  231.                 if(!$db->num_rows($check_item))
  232.                     echo "You don't have this item in your shop..";
  233.                 else
  234.                 {
  235.                     $itm = $db->fetch_row($check_item);
  236.                     if($itm['stype'] == 'item')
  237.                     {
  238.                         $itmname = $db->fetch_single($db->query("SELECT `itmname` FROM `items` WHERE `itmid` = {$itm['selling']}"));
  239.                         item_add($itm['selling'],$us['owner'],$itm['sqty']);
  240.                         $db->query("DELETE FROM `user_shopitems` WHERE `id` = {$_GET['remove']} AND `sID` = {$us['id']}");
  241.                         echo "You have removed the [x ".number_format($itm['sqty'])."] of {$itmname}  from the shop.";
  242.                     }
  243.                     else if($itm['stype'] == 'crystals')
  244.                     {  
  245.                         $db->query("UPDATE `users` SET `crystals` = `crystals` + {$itm['sqty']} WHERE `userid` = {$ir['userid']}");
  246.                         $db->query("DELETE FROM `user_shopitems` WHERE `id` = {$_GET['remove']} AND `sID` = {$us['id']}");
  247.                         echo "You have removed the [x ".number_format($itm['sqty'])."] Crystals  from the shop.";
  248.                     }
  249.                     else
  250.                     {
  251.                         $db->query("UPDATE `users` SET `donatordays` = `donatordays` + {$itm['sqty']} WHERE `userid` = {$ir['userid']}");
  252.                         $db->query("DELETE FROM `user_shopitems` WHERE `id` = {$_GET['remove']} AND `sID` = {$us['id']}");
  253.                         echo "You have removed the [x ".number_format($itm['sqty'])."] Donator Days  from the shop.";
  254.                     }
  255.                 }
  256.             }
  257.         }
  258.         echo "<h3>Your Shop</h3>";
  259.         $user_items = $db->query("SELECT * FROM `user_shopitems` WHERE `sID` = {$us['id']}");
  260.         if(!$db->num_rows($user_items))
  261.             echo "You don't have any items.<br /><br />";
  262.         echo "
  263.         <table width='90%' class='table' style='text-align:center;'>
  264.             <tr>
  265.                 <th>Item</th>
  266.                 <th>Qty</th>
  267.                 <th>Total Cost</th>
  268.                 <th>Action</th>
  269.             </tr>";
  270.             while($si = $db->fetch_row($user_items))
  271.             {
  272.                 if($si['stype'] == 'item')
  273.                 {
  274.                     $itm = $db->fetch_row($db->query("SELECT `itmid`,`itmname` FROM `items` WHERE `itmid` = {$si['selling']}"));
  275.                     $si['stype'] = $itm['itmname'];
  276.                 }
  277.                 else if($si['stype'] == 'crystals')
  278.                     $si['stype'] = "".number_format($si['sqty'])." Crystals";
  279.                 else
  280.                     $si['stype'] = "".number_format($si['sqty'])." Donator Days";
  281.                 echo "
  282.                 <tr>
  283.                     <td>{$si['stype']}</td>
  284.                     <td>".number_format($si['sqty'])."</td>
  285.                     <td>".money_formatter($si['tprice'])."</td>
  286.                     <td><a href='myshop.php?remove={$si['id']}'>[Remove]</td>
  287.                 </tr>";
  288.             }
  289.         echo "</table><br /><hr>
  290.         <h3>Add to shop</h3>
  291.         <script>
  292.                 $(document).ready(function () {
  293.                         $('#state').change(function() {
  294.                             // foo is the id of the other select box
  295.                     if ($(this).val() != 'item') {
  296.                             $('#foo').hide();
  297.                     } else {
  298.                             $('#foo').show();
  299.                     }
  300.                         });
  301.                 });
  302.         </script>
  303.         <form method='post'>
  304.             What you selling:
  305.             <select id='state' name='wts' type='dropdown'>
  306.                 <option value='item' selected>Item</option>
  307.                 <option value='donatordays'>Donator Days</option>
  308.                 <option value='crystals'>Crystals</option>
  309.             </select><br />
  310.             <div id='foo'><br />Item Id: <input type='text' name='itmid' value='0' /></div><br />
  311.             Qty: <input type='text' name='qty' value='1' /><br /><br />
  312.             Total Cost: <input type='text' name='tcost' value='0' /><br /><br />
  313.             <input type='submit' name='addtoshop' value='Add To your Shop' />
  314.         </form>";
  315.     }  
  316. }
  317. $h->endpage();
Advertisement
Add Comment
Please, Sign In to add comment