Advertisement
Guest User

Untitled

a guest
Aug 31st, 2016
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 11.76 KB | None | 0 0
  1. <?php
  2. use \Functions\Functions;
  3. use \ElephantIO\Client as Elephant;
  4. use \ElephantIO\Engine\SocketIO\Version1X as Version1X;  
  5.  
  6. class Socket {
  7.     private $model;
  8.    
  9.     function __construct($model)
  10.     {
  11.         $this->model = $model;
  12.        
  13.         switch ($this->model->method)
  14.         {
  15.             case 'tradeoffer_verification':
  16.                 $this->tradeoffer_verification();
  17.                 break;
  18.             case 'sell_skins':
  19.                 $this->sell_skins();
  20.                 break;
  21.             case 'get_tradeoffer_items':
  22.                 $this->get_tradeoffer_items($_POST['tradeofferid'],
  23.                                             $_POST['bot_id'],
  24.                                             $_POST['steamid_other'],
  25.                                             $_POST['time_created'],
  26.                                             $_POST['time_updated'],
  27.                                             $_POST['items_to_receive'],
  28.                                             $_POST['hash']);
  29.                 break;
  30.         }
  31.     }
  32.  
  33.     function tradeoffer_check_data()
  34.     {
  35.         global $db, $member_id;
  36.  
  37.         if (!$member_id->authed) $this->model->_echo($this->model->message(AUTH_ERROR));
  38.         if (!isset($this->model->params[0])) $this->model->_echo($this->model->message(TRADEOFFER_IS_NULL_ERROR));
  39.     }
  40.  
  41.     function get_tradeoffer_bot()
  42.     {
  43.         global $db, $member_id;
  44.         $q = $db->query('select server, id from bots where busy = 0 limit 0,1');
  45.         return $db->get_row($q);
  46.     }
  47.  
  48.     function tradeoffer_verification()
  49.     {
  50.         global $db, $member_id;
  51.  
  52.         $this->tradeoffer_check_data();  
  53.  
  54.         $tradeoffer = $this->model->params[0];          
  55.         $bot = $this->get_tradeoffer_bot();
  56.        
  57.         $client = new Elephant(new Version1X('http://' . $bot['server'] . ':4002'));
  58.  
  59.         $client->initialize();
  60.  
  61.         $client->emit('tradeoffer_verification', [
  62.                                                     'user_id' => $member_id->user_id,
  63.                                                     'tradeoffer' => $tradeoffer,
  64.                                                 ]);
  65.         $client->close();
  66.        
  67.         $this->model->_echo(
  68.             array(
  69.                 'tradeoffer' => $tradeoffer,
  70.             )
  71.         );  
  72.     }
  73.  
  74.  
  75.     function sell_skins()
  76.     {
  77.         global $db, $member_id;
  78.  
  79.         $this->tradeoffer_check_data();  
  80.  
  81.         $tradeoffer = $member_id->tradeoffer;
  82.  
  83.         $items = $this->filter_skins( $this->model->params[0] );  
  84.  
  85.         $currency = $this->model->params[1];
  86.         $bot = $this->get_tradeoffer_bot();
  87.  
  88.         $query = "insert ignore into `bot_inventory`(`item_id`, `market_hash_name`)
  89.                            values";
  90.  
  91.         foreach ($items as $item)
  92.            $query .= "({$item['itemid']}, '{$item['market_hash_name']}'),";
  93.  
  94.         // заменяем , на ; в конце запроса
  95.         $query = substr($query, 0, strlen($query) - 1) . ';';
  96.  
  97.         $q = $db->query($query);
  98.  
  99.         $client = new Elephant(new Version1X('http://' . $bot['server'] . ':4002'));
  100.         $client->initialize();
  101.  
  102.         $client->emit('sell_skins', [
  103.                                     'user_id' => $member_id->user_id,
  104.                                     'items' => $items,
  105.                                     'currency' => $currency,
  106.                                 ]);
  107.         $client->close();
  108.  
  109.         $this->model->_echo(
  110.             array(
  111.                 'items' => $items,
  112.                 'tradeoffer' => $tradeoffer,
  113.                 'currency' => $currency
  114.             )
  115.         );
  116.     }
  117.  
  118.     function filter_skins($skins)
  119.     {
  120.         global $member_id;
  121.  
  122.         $filtered_skins = array();
  123.  
  124.         $inventory = $this->load_inventory($member_id->uid);
  125.  
  126.         foreach ($inventory as $item)
  127.         {
  128.             // Отсекаем предметы, цена которых меньше миниальной
  129.             if (in_array($item->itemid, $skins) && $item->can_be_sold)
  130.             {
  131.                 $filtered_skins[] = array('itemid' => $item->itemid,
  132.                                           'price' => $item->price,
  133.                                           'market_hash_name' => $item->market_hash_name);
  134.             }
  135.         }
  136.  
  137.         return $filtered_skins;
  138.     }
  139.  
  140.     function get_tradeoffer_items($tradeofferid, $bot_id, $steamid_other, $time_created, $time_updated, $items_to_receive, $hash)
  141.     {
  142.         global $db, $member_id;
  143.  
  144.         $apiSecret = '9ej^nOM1l&!JPErRzl7eRLfM';
  145.        
  146.        
  147.         if ($tradeofferid == '' || $tradeofferid == NULL) { echo 'No tradeoffer provided'; exit; }
  148.         if ($bot_id == '' || $bot_id == NULL || $bot_id < 0) { echo 'No bot_id provided'; exit; }
  149.         if ($steamid_other == '' || $steamid_other == NULL) { echo 'No steamid_other provided'; exit; }
  150.         if ($time_created == '' || $time_created == NULL) { echo 'No time_created provided'; exit; }
  151.         if ($time_updated == '' || $time_updated == NULL) { echo 'No time_updated provided'; exit; }
  152.         if ($items_to_receive == '' || $items_to_receive == NULL) { echo 'No items_to_receive provided'; exit; }
  153.         if ($hash == '' || $hash == NULL) { echo 'No hash provided'; exit; }
  154.        
  155.        
  156.         $our_hash = hash('sha256', $tradeofferid.$steamid_other.$items_to_receive.$apiSecret);
  157.        
  158.  
  159.         if ($hash != $our_hash) { echo 'Hashes doesn\'t equal.'; exit; }
  160.        
  161.         $tradeofferid     = $db->safesql($tradeofferid);
  162.         $bot_id           = $db->safesql($bot_id);
  163.         $steamid_other    = $db->safesql($steamid_other);
  164.         $time_created     = $db->safesql($time_created);
  165.         $time_updated     = $db->safesql($time_updated);
  166.         $items_to_receive = $db->safesql($items_to_receive);       
  167.        
  168.         $total_price_q = $db->query("select (sum(skins_prices.price_usd) * 0.7) as value from bot_inventory, skins_prices where bot_inventory.item_id in ({$items_to_receive}) and skins_prices.market_hash_name = bot_inventory.market_hash_name");
  169.  
  170.         $total_price = $db->get_row($total_price_q)['value'];
  171.  
  172.         if ($total_price == NULL) $total_price = 0;
  173.  
  174.         $items_to_receive = str_replace(' ','', $items_to_receive);
  175.        
  176.         $trade_q = $db->query("insert into `bots_tradeoffers`(`tradeofferid`, `bot_id`, `tradeoffer_state`, `steamid_other`, `time_created`, `time_updated`, `items_to_receive`, `time_received`, `total_price`) values ('{$tradeofferid}', {$bot_id}, 3, '{$steamid_other}', from_unixtime({$time_created}), from_unixtime({$time_updated}), '{$items_to_receive}', NOW(), '{$total_price}')");
  177.  
  178.         $trade_i = $db->query("update `bot_inventory` set `received` = 1 where `item_id` in ({$items_to_receive});");
  179.  
  180.         $user_id = $db->get_row($db->query("select id from users where uid = '{$steamid_other}'"))['id'];
  181.  
  182.        
  183.         $this->notify_recipient($tradeofferid, $steamid_other, $total_price);
  184.         $this->model->_echo(
  185.             array(
  186.                 'status' => 'OK',
  187.                 'user_id' => $user_id,
  188.             )
  189.         );
  190.        
  191.     }
  192.  
  193.     private function notify_recipient($tradeofferid, $steamid_other, $total_price)
  194.     {
  195.         global $apiSecret;
  196.  
  197.         if ($tradeofferid == '' || $tradeofferid == NULL) { echo 'No tradeoffer provided'; exit; }
  198.         if ($steamid_other == '' || $steamid_other == NULL) { echo 'No steamid_other provided'; exit; }
  199.         if ($total_price == NULL || $total_price == 0) { echo 'No total_price provided'; exit; }
  200.  
  201.         $hash = hash('sha256', $tradeofferid . $steamid_other . $total_price . $apiSecret);
  202.  
  203.         $params = array('tradeofferid' => $tradeofferid,
  204.                         'steamid_other' => $steamid_other,
  205.                         'total_price' => $total_price,
  206.                         'hash' => $hash);
  207.  
  208.         file_get_contents('http://farmskins.com/skin2pay.php?' . http_build_query($params));
  209.     }
  210.  
  211.     function load_inventory($user_id)
  212.     {
  213.  
  214.         if ($user_id == NULL)
  215.             exit;
  216.  
  217.         global $db, $lang;
  218.  
  219.         DEFINE('PRICE_COEFFICIENT', 0.7);
  220.  
  221.         $curl = curl_init();
  222.         curl_setopt_array($curl, array(
  223.             CURLOPT_URL => 'http://steamcommunity.com/profiles/' . $user_id . '/inventory/json/730/2',
  224.             CURLOPT_RETURNTRANSFER => 1,
  225.             CURLOPT_FOLLOWLOCATION => 1,
  226.             CURLOPT_HEADER => 0,
  227.         ));
  228.         $skins = json_decode(curl_exec($curl), true);
  229.         curl_close($curl);
  230.  
  231.         if (!$skins['success']) {
  232.             echo '<p class="warning">Profile is private</p>';
  233.             exit;
  234.         }
  235.  
  236.         $new_skins = array();
  237.  
  238.         foreach ($skins['rgDescriptions'] as $key)
  239.         {
  240.             $key['market_hash_name'] = $db->safesql($key['market_hash_name']);
  241.             $q = $db->query("select price_usd, price_rub, price_eur from skins_prices where market_hash_name='{$key['market_hash_name']}'");
  242.  
  243.             $skins_prices = $db->get_row($q);
  244.  
  245.             $market_hash_name_locale = $key['market_hash_name'];
  246.  
  247.             foreach ($key['tags'] as $k => $tag)
  248.             {
  249.  
  250.                 $tag_cat = mb_strtolower($tag['category']);
  251.                 switch($tag_cat)
  252.                 {
  253.                     case 'rarity':
  254.                         $rarity = mb_strtolower($tag['name']);
  255.                         if(stristr($rarity, 'base') != FALSE) {
  256.                             $skins_prices['price_usd'] = 0;
  257.                             $skins_prices['price_rub'] = 0;
  258.                             $skins_prices['price_eur'] = 0;
  259.                         }
  260.                         $key['rarity'] = $rarity;
  261.                         break;
  262.                     case 'exterior':
  263.                         $key['exterior'] = $tag['name'];
  264.                         break;
  265.                 }
  266.             }
  267.  
  268.             if (!isset($key['rarity']))
  269.                 $key['rarity'] = '';
  270.  
  271.             if (!isset($key['exterior']))
  272.                 $key['exterior'] = '';
  273.  
  274.             $filter_words = array('sticker',
  275.                                 'наклейка',
  276.                                 'aufkleber');
  277.  
  278.             foreach ($filter_words as $word)
  279.             {
  280.                 if (stristr(mb_strtolower($market_hash_name_locale), $word) != FALSE)
  281.                 {
  282.                     $skins_prices['price_usd'] = 0;
  283.                     $skins_prices['price_rub'] = 0;
  284.                     $skins_prices['price_eur'] = 0;
  285.                 }
  286.             }
  287.            
  288.  
  289.             $skins_arr[] = array('market_hash_name_locale' => htmlspecialchars($market_hash_name_locale),
  290.                                 'price_usd' => $skins_prices['price_usd'] * PRICE_COEFFICIENT,
  291.                                 'price_rub' => $skins_prices['price_rub'] * PRICE_COEFFICIENT,
  292.                                 'price_eur' => $skins_prices['price_eur'] * PRICE_COEFFICIENT,
  293.                                 'classid' => $key['classid'],
  294.                                 'rarity' => $key['rarity'],
  295.                                 'exterior' => $key['exterior']
  296.                                 );
  297.         }
  298.  
  299.  
  300.         foreach ($skins['rgInventory'] as $key) {
  301.             $skins_itemid[] = array('itemid' => $key['id'],
  302.                                     'classid' => $key['classid']);
  303.         }
  304.  
  305.         $i = 0;
  306.         foreach ($skins_itemid as $item)
  307.         {
  308.             $skin_item = $item;
  309.             foreach ($skins_arr as $skin)
  310.             {
  311.                 if ($skin['classid'] == $skin_item['classid'])
  312.                 {
  313.                     foreach($skin as $key => $v)
  314.                         $skins_itemid[$i][$key] = $v;
  315.                     break;
  316.                 }
  317.             }
  318.             $new_skins[] = new Skin($skins_itemid[$i]);
  319.             $i++;
  320.         }
  321.         return $new_skins;  
  322.     }
  323.  
  324. }
  325.  
  326. $socket = new Socket($model, $member_id);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement