Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- use \Functions\Functions;
- use \ElephantIO\Client as Elephant;
- use \ElephantIO\Engine\SocketIO\Version1X as Version1X;
- class Socket {
- private $model;
- function __construct($model)
- {
- $this->model = $model;
- switch ($this->model->method)
- {
- case 'tradeoffer_verification':
- $this->tradeoffer_verification();
- break;
- case 'sell_skins':
- $this->sell_skins();
- break;
- case 'get_tradeoffer_items':
- $this->get_tradeoffer_items($_POST['tradeofferid'],
- $_POST['bot_id'],
- $_POST['steamid_other'],
- $_POST['time_created'],
- $_POST['time_updated'],
- $_POST['items_to_receive'],
- $_POST['hash']);
- break;
- }
- }
- function tradeoffer_check_data()
- {
- global $db, $member_id;
- if (!$member_id->authed) $this->model->_echo($this->model->message(AUTH_ERROR));
- if (!isset($this->model->params[0])) $this->model->_echo($this->model->message(TRADEOFFER_IS_NULL_ERROR));
- }
- function get_tradeoffer_bot()
- {
- global $db, $member_id;
- $q = $db->query('select server, id from bots where busy = 0 limit 0,1');
- return $db->get_row($q);
- }
- function tradeoffer_verification()
- {
- global $db, $member_id;
- $this->tradeoffer_check_data();
- $tradeoffer = $this->model->params[0];
- $bot = $this->get_tradeoffer_bot();
- $client = new Elephant(new Version1X('http://' . $bot['server'] . ':4002'));
- $client->initialize();
- $client->emit('tradeoffer_verification', [
- 'user_id' => $member_id->user_id,
- 'tradeoffer' => $tradeoffer,
- ]);
- $client->close();
- $this->model->_echo(
- array(
- 'tradeoffer' => $tradeoffer,
- )
- );
- }
- function sell_skins()
- {
- global $db, $member_id;
- $this->tradeoffer_check_data();
- $tradeoffer = $member_id->tradeoffer;
- $items = $this->filter_skins( $this->model->params[0] );
- $currency = $this->model->params[1];
- $bot = $this->get_tradeoffer_bot();
- $query = "insert ignore into `bot_inventory`(`item_id`, `market_hash_name`)
- values";
- foreach ($items as $item)
- $query .= "({$item['itemid']}, '{$item['market_hash_name']}'),";
- // заменяем , на ; в конце запроса
- $query = substr($query, 0, strlen($query) - 1) . ';';
- $q = $db->query($query);
- $client = new Elephant(new Version1X('http://' . $bot['server'] . ':4002'));
- $client->initialize();
- $client->emit('sell_skins', [
- 'user_id' => $member_id->user_id,
- 'items' => $items,
- 'currency' => $currency,
- ]);
- $client->close();
- $this->model->_echo(
- array(
- 'items' => $items,
- 'tradeoffer' => $tradeoffer,
- 'currency' => $currency
- )
- );
- }
- function filter_skins($skins)
- {
- global $member_id;
- $filtered_skins = array();
- $inventory = $this->load_inventory($member_id->uid);
- foreach ($inventory as $item)
- {
- // Отсекаем предметы, цена которых меньше миниальной
- if (in_array($item->itemid, $skins) && $item->can_be_sold)
- {
- $filtered_skins[] = array('itemid' => $item->itemid,
- 'price' => $item->price,
- 'market_hash_name' => $item->market_hash_name);
- }
- }
- return $filtered_skins;
- }
- function get_tradeoffer_items($tradeofferid, $bot_id, $steamid_other, $time_created, $time_updated, $items_to_receive, $hash)
- {
- global $db, $member_id;
- $apiSecret = '9ej^nOM1l&!JPErRzl7eRLfM';
- if ($tradeofferid == '' || $tradeofferid == NULL) { echo 'No tradeoffer provided'; exit; }
- if ($bot_id == '' || $bot_id == NULL || $bot_id < 0) { echo 'No bot_id provided'; exit; }
- if ($steamid_other == '' || $steamid_other == NULL) { echo 'No steamid_other provided'; exit; }
- if ($time_created == '' || $time_created == NULL) { echo 'No time_created provided'; exit; }
- if ($time_updated == '' || $time_updated == NULL) { echo 'No time_updated provided'; exit; }
- if ($items_to_receive == '' || $items_to_receive == NULL) { echo 'No items_to_receive provided'; exit; }
- if ($hash == '' || $hash == NULL) { echo 'No hash provided'; exit; }
- $our_hash = hash('sha256', $tradeofferid.$steamid_other.$items_to_receive.$apiSecret);
- if ($hash != $our_hash) { echo 'Hashes doesn\'t equal.'; exit; }
- $tradeofferid = $db->safesql($tradeofferid);
- $bot_id = $db->safesql($bot_id);
- $steamid_other = $db->safesql($steamid_other);
- $time_created = $db->safesql($time_created);
- $time_updated = $db->safesql($time_updated);
- $items_to_receive = $db->safesql($items_to_receive);
- $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");
- $total_price = $db->get_row($total_price_q)['value'];
- if ($total_price == NULL) $total_price = 0;
- $items_to_receive = str_replace(' ','', $items_to_receive);
- $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}')");
- $trade_i = $db->query("update `bot_inventory` set `received` = 1 where `item_id` in ({$items_to_receive});");
- $user_id = $db->get_row($db->query("select id from users where uid = '{$steamid_other}'"))['id'];
- $this->notify_recipient($tradeofferid, $steamid_other, $total_price);
- $this->model->_echo(
- array(
- 'status' => 'OK',
- 'user_id' => $user_id,
- )
- );
- }
- private function notify_recipient($tradeofferid, $steamid_other, $total_price)
- {
- global $apiSecret;
- if ($tradeofferid == '' || $tradeofferid == NULL) { echo 'No tradeoffer provided'; exit; }
- if ($steamid_other == '' || $steamid_other == NULL) { echo 'No steamid_other provided'; exit; }
- if ($total_price == NULL || $total_price == 0) { echo 'No total_price provided'; exit; }
- $hash = hash('sha256', $tradeofferid . $steamid_other . $total_price . $apiSecret);
- $params = array('tradeofferid' => $tradeofferid,
- 'steamid_other' => $steamid_other,
- 'total_price' => $total_price,
- 'hash' => $hash);
- file_get_contents('http://farmskins.com/skin2pay.php?' . http_build_query($params));
- }
- function load_inventory($user_id)
- {
- if ($user_id == NULL)
- exit;
- global $db, $lang;
- DEFINE('PRICE_COEFFICIENT', 0.7);
- $curl = curl_init();
- curl_setopt_array($curl, array(
- CURLOPT_URL => 'http://steamcommunity.com/profiles/' . $user_id . '/inventory/json/730/2',
- CURLOPT_RETURNTRANSFER => 1,
- CURLOPT_FOLLOWLOCATION => 1,
- CURLOPT_HEADER => 0,
- ));
- $skins = json_decode(curl_exec($curl), true);
- curl_close($curl);
- if (!$skins['success']) {
- echo '<p class="warning">Profile is private</p>';
- exit;
- }
- $new_skins = array();
- foreach ($skins['rgDescriptions'] as $key)
- {
- $key['market_hash_name'] = $db->safesql($key['market_hash_name']);
- $q = $db->query("select price_usd, price_rub, price_eur from skins_prices where market_hash_name='{$key['market_hash_name']}'");
- $skins_prices = $db->get_row($q);
- $market_hash_name_locale = $key['market_hash_name'];
- foreach ($key['tags'] as $k => $tag)
- {
- $tag_cat = mb_strtolower($tag['category']);
- switch($tag_cat)
- {
- case 'rarity':
- $rarity = mb_strtolower($tag['name']);
- if(stristr($rarity, 'base') != FALSE) {
- $skins_prices['price_usd'] = 0;
- $skins_prices['price_rub'] = 0;
- $skins_prices['price_eur'] = 0;
- }
- $key['rarity'] = $rarity;
- break;
- case 'exterior':
- $key['exterior'] = $tag['name'];
- break;
- }
- }
- if (!isset($key['rarity']))
- $key['rarity'] = '';
- if (!isset($key['exterior']))
- $key['exterior'] = '';
- $filter_words = array('sticker',
- 'наклейка',
- 'aufkleber');
- foreach ($filter_words as $word)
- {
- if (stristr(mb_strtolower($market_hash_name_locale), $word) != FALSE)
- {
- $skins_prices['price_usd'] = 0;
- $skins_prices['price_rub'] = 0;
- $skins_prices['price_eur'] = 0;
- }
- }
- $skins_arr[] = array('market_hash_name_locale' => htmlspecialchars($market_hash_name_locale),
- 'price_usd' => $skins_prices['price_usd'] * PRICE_COEFFICIENT,
- 'price_rub' => $skins_prices['price_rub'] * PRICE_COEFFICIENT,
- 'price_eur' => $skins_prices['price_eur'] * PRICE_COEFFICIENT,
- 'classid' => $key['classid'],
- 'rarity' => $key['rarity'],
- 'exterior' => $key['exterior']
- );
- }
- foreach ($skins['rgInventory'] as $key) {
- $skins_itemid[] = array('itemid' => $key['id'],
- 'classid' => $key['classid']);
- }
- $i = 0;
- foreach ($skins_itemid as $item)
- {
- $skin_item = $item;
- foreach ($skins_arr as $skin)
- {
- if ($skin['classid'] == $skin_item['classid'])
- {
- foreach($skin as $key => $v)
- $skins_itemid[$i][$key] = $v;
- break;
- }
- }
- $new_skins[] = new Skin($skins_itemid[$i]);
- $i++;
- }
- return $new_skins;
- }
- }
- $socket = new Socket($model, $member_id);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement