public static function getCurrent($currency_id, $currency_abbr = false) { global $CFG; $usd_info = $CFG->currencies['USD']; $usd_field = 'usd_ask'; $currency_id = $currency_id > 0 ? preg_replace("/[^0-9]/", "", $currency_id) : $usd_info['id']; $currency_abbr = preg_replace("/[^a-zA-Z]/", "", $currency_abbr); if ($currency_abbr) { $currency_info = $CFG->currencies[strtoupper($currency_abbr)]; $currency_id = $currency_info['id']; } elseif ($currency_id > 0) { $currency_info = $CFG->currencies[$currency_id]; } if ($CFG->memcached) { $cached = $CFG->m->get('stats_' . $currency_info['currency']); if ($cached) { return $cached; } } $bid_ask = Orders::getBidAsk(false, $currency_id); $bid = $bid_ask['bid']; $ask = $bid_ask['ask']; $price_str = '(CASE WHEN transactions.currency = ' . $currency_info['id'] . ' THEN transactions.btc_price WHEN transactions.currency1 = ' . $currency_info['id'] . ' THEN transactions.orig_btc_price ELSE (transactions.orig_btc_price * (CASE transactions.currency1 '; foreach ($CFG->currencies as $curr_id => $currency1) { if (is_numeric($curr_id) || $currency1['currency'] == 'BTC') { continue; } if (!empty($currency_info) && $currency1['id'] == $currency_info['id']) { continue; } $conversion = empty($currency_info) || $currency_info['currency'] == 'USD' ? $currency1[$usd_field] : $currency1[$usd_field] / $currency_info[$usd_field]; $price_str .= ' WHEN ' . $currency1['id'] . ' THEN ' . $conversion . ' '; } $price_str .= ' END)) END)'; $sql = 'SELECT r2.btc_price AS btc_price2, r3.btc_price AS btc_price3, r2.last_transaction_type AS last_transaction_type2, r2.last_transaction_currency AS last_transaction_currency2, r3.last_transaction_currency AS last_transaction_currency3, r4.total_btc_traded, r5.max, r5.min, current_stats.total_btc, current_stats.market_cap, current_stats.trade_volume FROM current_stats '; $sql_arr[] = "LEFT JOIN (SELECT IF(transactions.currency = {$currency_id},transactions.btc_price,transactions.orig_btc_price) AS btc_price, IF(transactions.transaction_type = {$CFG->transactions_buy_id},'BUY','SELL') AS last_transaction_type, IF(transactions.currency != {$currency_id} AND transactions.currency1 != {$currency_id},transactions.currency1,{$currency_id}) AS last_transaction_currency FROM transactions WHERE 1 " . (!$CFG->cross_currency_trades ? "AND transactions.currency = {$currency_id}" : '') . " ORDER BY transactions.id DESC LIMIT 0,1) AS r2 ON (1)"; $sql_arr[] = "LEFT JOIN (SELECT IF(transactions.currency = {$currency_id},transactions.btc_price,transactions.orig_btc_price) AS btc_price, IF(transactions.currency != {$currency_id} AND transactions.currency1 != {$currency_id},transactions.currency1,{$currency_id}) AS last_transaction_currency FROM transactions WHERE transactions.date < DATE_SUB(DATE_ADD(NOW(), INTERVAL " . $CFG->timezone_offset / 60 / 60 . " HOUR), INTERVAL 1 DAY) " . (!$CFG->cross_currency_trades ? "AND transactions.currency = {$currency_id}" : '') . " ORDER BY transactions.id DESC LIMIT 0,1) AS r3 ON (1)"; $sql_arr[] = "LEFT JOIN (SELECT btc_24h AS total_btc_traded FROM status) AS r4 ON (1)"; $sql_arr[] = "LEFT JOIN (SELECT MAX(" . ($CFG->cross_currency_trades ? "ROUND({$price_str},2)" : 'transactions.btc_price') . ") AS `max`, MIN(" . ($CFG->cross_currency_trades ? "ROUND({$price_str},2)" : 'transactions.btc_price') . ") AS `min` FROM transactions WHERE transactions.date >= CURDATE() " . (!$CFG->cross_currency_trades ? "AND transactions.currency = {$currency_id}" : '') . " LIMIT 0,1) AS r5 ON (1)"; $sql .= implode(' ', $sql_arr) . ' WHERE current_stats.id = 1'; $result = db_query_array($sql); if ($result[0]['btc_price2']) { $result[0]['btc_price2'] = $result[0]['btc_price2'] * ($currency_info['currency'] == 'USD' ? $CFG->currencies[$result[0]['last_transaction_currency2']][$usd_field] : $CFG->currencies[$result[0]['last_transaction_currency2']][$usd_field] / $currency_info[$usd_field]); } if ($result[0]['btc_price3']) { $result[0]['btc_price3'] = $result[0]['btc_price3'] * ($currency_info['currency'] == 'USD' ? $CFG->currencies[$result[0]['last_transaction_currency3']][$usd_field] : $CFG->currencies[$result[0]['last_transaction_currency3']][$usd_field] / $currency_info[$usd_field]); } $stats['bid'] = $bid; $stats['ask'] = $ask; $stats['last_price'] = $result[0]['btc_price2'] ? $result[0]['btc_price2'] : $ask; $stats['last_transaction_type'] = $result[0]['last_transaction_type2']; $stats['last_transaction_currency'] = $result[0]['last_transaction_currency2']; $stats['daily_change'] = $result[0]['btc_price3'] > 0 && $result[0]['btc_price2'] > 0 ? $result[0]['btc_price2'] - $result[0]['btc_price3'] : '0'; $stats['daily_change_percent'] = $stats['last_price'] > 0 ? $stats['daily_change'] / $stats['last_price'] * 100 : 0; $stats['max'] = $result[0]['max'] > 0 ? $result[0]['max'] : $result[0]['btc_price2']; $stats['min'] = $result[0]['min'] > 0 ? $result[0]['min'] : $result[0]['btc_price2']; $stats['open'] = $result[0]['btc_price3'] > 0 ? $result[0]['btc_price3'] : $result[0]['btc_price2']; $stats['total_btc_traded'] = $result[0]['total_btc_traded']; $stats['total_btc'] = $result[0]['total_btc']; $stats['market_cap'] = $result[0]['market_cap']; $stats['trade_volume'] = $result[0]['trade_volume']; if ($CFG->memcached) { $key = 'stats_' . $currency_info['currency']; $set[$key] = $stats; memcached_safe_set($set, 300); } return $stats; }
public static function getBidAsk($currency = false, $currency_id = false, $absolute = false, $dont_cache = false) { global $CFG; if (empty($currency) && empty($currency_id)) { return false; } $currency = preg_replace("/[^a-zA-Z]/", "", $currency); $currency_id = preg_replace("/[^0-9]/", "", $currency_id); $currency_info = $currency_id > 0 ? $CFG->currencies[$currency_id] : $CFG->currencies[strtoupper($currency)]; $usd_field = 'usd_ask'; if (!empty(self::$bid_ask[$currency_info['currency']])) { return self::$bid_ask[$currency_info['currency']]; } if ($CFG->memcached && !$absolute) { $cached = $CFG->m->get('bid_ask_' . $currency_info['currency']); if ($cached) { self::$bid_ask[$currency_info['currency']] = $cached; return $cached; } } if ($CFG->cross_currency_trades) { $price_str_bid = '(orders.btc_price * CASE orders.currency WHEN ' . $currency_info['id'] . ' THEN 1'; $price_str_ask = '(orders.btc_price * CASE orders.currency WHEN ' . $currency_info['id'] . ' THEN 1'; foreach ($CFG->currencies as $curr_id => $currency1) { if (is_numeric($curr_id) || $currency1['currency'] == 'BTC') { continue; } if (!empty($currency_info) && $currency1['id'] == $currency_info['id']) { continue; } $conversion = empty($currency_info) || $currency_info['currency'] == 'USD' ? $currency1[$usd_field] : $currency1[$usd_field] / $currency_info[$usd_field]; $price_str_bid .= ' WHEN ' . $currency1['id'] . ' THEN ' . ($conversion - (!$absolute ? $conversion * $CFG->currency_conversion_fee : 0)) . ' '; $price_str_ask .= ' WHEN ' . $currency1['id'] . ' THEN ' . ($conversion + (!$absolute ? $conversion * $CFG->currency_conversion_fee : 0)) . ' '; } $price_str_bid .= ' END)'; $price_str_ask .= ' END)'; } else { $price_str_bid = 'orders.btc_price'; $price_str_ask = 'orders.btc_price'; } $sql = "SELECT ROUND(MAX(IF(orders.order_type = {$CFG->order_type_bid},{$price_str_bid},NULL)),2) AS bid, ROUND(MIN(IF(orders.order_type = {$CFG->order_type_ask},{$price_str_ask},NULL)),2) AS ask FROM orders WHERE 1 " . (!$CFG->cross_currency_trades ? "AND orders.currency = {$currency_info['id']}" : false) . " AND orders.btc_price > 0 LIMIT 0,1"; $result = db_query_array($sql); $res = $result[0] ? $result[0] : array('bid' => 0, 'ask' => 0); if ($res['bid'] > 0 && !$res['ask']) { $res['ask'] = $res['bid']; } if ($res['ask'] > 0 && !$res['bid']) { $res['bid'] = $res['ask']; } if (!$res['ask'] && !$res['bid']) { $sql = "SELECT currency, currency1, btc_price, orig_btc_price FROM transactions WHERE 1 " . (!$CFG->cross_currency_trades ? "AND transactions.currency = {$currency_info['id']}" : '') . " ORDER BY id DESC LIMIT 0,1"; $result = db_query_array($sql); if ($result) { if ($CFG->cross_currency_trades) { if ($result[0]['currency'] == $currency_info['id']) { $result[0]['fiat_price'] = $result[0]['btc_price']; } else { $result[0]['fiat_price'] = round($result[0]['orig_btc_price'] * (empty($currency_info) || $currency_info['currency'] == 'USD' ? $CFG->currencies[$result[0]['currency1']][$usd_field] : $CFG->currencies[$result[0]['currency1']][$usd_field] / $currency_info[$usd_field]), 2, PHP_ROUND_HALF_UP); } } } else { $currency_info1 = $CFG->currencies['USD']; $sql = "SELECT ROUND((btc_price/{$currency_info['usd_ask']}),2) AS fiat_price FROM transactions WHERE currency = {$currency_info1['id']} ORDER BY id DESC LIMIT 0,1"; $result = db_query_array($sql); if (!$result) { $sql = 'SELECT ROUND((usd /' . $currency_info['usd_ask'] . '),2) AS fiat_price FROM historical_data ORDER BY id DESC LIMIT 0,1'; $result = db_query_array($sql); } } if ($result) { $res = array('bid' => $result[0]['fiat_price'], 'ask' => $result[0]['fiat_price']); } } self::$bid_ask[$currency_info['currency']] = $res; if ($CFG->memcached && !$dont_cache) { memcached_safe_set(array('bid_ask_' . $currency_info['currency']), 300); } return $res; }
public static function get($count = false, $page = false, $per_page = false, $currency = false, $user = false, $start_date = false, $type = false, $order_by = false, $order_desc = false, $public_api_all = false, $dont_paginate = false) { global $CFG; if ($user && !(User::$info['id'] > 0)) { return false; } $page = preg_replace("/[^0-9]/", "", $page); $per_page = preg_replace("/[^0-9]/", "", $per_page); $per_page1 = $per_page == 1 || $per_page == 5 ? 5 : $per_page; $page = preg_replace("/[^0-9]/", "", $page); $currency = preg_replace("/[^a-zA-Z]/", "", $currency); $start_date = preg_replace("/[^0-9: \\-]/", "", $start_date); $page = $page > 0 ? $page - 1 : 0; $r1 = $page * $per_page; $order_arr = array('date' => 'transactions.id', 'btc' => 'transactions.btc', 'btcprice' => 'usd_price', 'fiat' => 'usd_amount', 'fee' => 'usd_fee'); $order_by = $order_by ? $order_arr[$order_by] : 'transactions.id'; $order_desc = $order_desc ? 'ASC' : 'DESC'; $user = $user ? User::$info['id'] : false; $usd_info = $CFG->currencies['USD']; $usd_field = 'usd_ask'; $currency_info = !empty($CFG->currencies[strtoupper($currency)]) ? $CFG->currencies[strtoupper($currency)] : false; if ($type == 'buy') { $type = $CFG->transactions_buy_id; } elseif ($type == 'sell') { $type = $CFG->transactions_sell_id; } else { $type = preg_replace("/[^0-9]/", "", $type); } if ($CFG->memcached) { $cached = null; if ($per_page == 5 && !$count && !$public_api_all) { $cached = $CFG->m->get('trans_l5_' . $currency_info['currency']); } elseif ($per_page == 1 && !$count && !$public_api_all) { $cached = $CFG->m->get('trans_l1_' . $currency_info['currency']); } elseif ($public_api_all) { $cached = $CFG->m->get('trans_api' . ($per_page ? '_l' . $per_page : '') . ($user ? '_u' . $user : '') . ($currency ? '_c' . $currency_info['currency'] : '') . ($type ? '_t' . $type : '')); } if (is_array($cached)) { if (count($cached) == 0) { return false; } return $cached; } } $price_str = '(CASE WHEN transactions.currency = ' . $currency_info['id'] . ' THEN transactions.btc_price WHEN transactions.currency1 = ' . $currency_info['id'] . ' THEN transactions.orig_btc_price ELSE (CASE transactions.currency1 '; $amount_str = '(CASE WHEN transactions.currency = ' . $currency_info['id'] . ' THEN (transactions.btc_price * transactions.btc) WHEN transactions.currency1 = ' . $currency_info['id'] . ' THEN (transactions.orig_btc_price * transactions.btc) ELSE (CASE transactions.currency1 '; $usd_str = '(CASE transactions.currency '; $currency_abbr = '(CASE IF(transactions.site_user = '******',transactions.currency,transactions.currency1) '; $currency_abbr1 = '(CASE transactions.currency '; $currency_abbr2 = '(CASE transactions.currency1 '; foreach ($CFG->currencies as $curr_id => $currency1) { if (is_numeric($curr_id) || $currency1['currency'] == 'BTC') { continue; } if (!empty($currency_info) && $currency1['id'] == $currency_info['id']) { continue; } $conversion = empty($currency_info) || $currency_info['currency'] == 'USD' ? $currency1[$usd_field] : $currency1[$usd_field] / $currency_info[$usd_field]; $price_str .= ' WHEN ' . $currency1['id'] . ' THEN (transactions.orig_btc_price * ' . $conversion . ')'; $amount_str .= ' WHEN ' . $currency1['id'] . ' THEN ((transactions.orig_btc_price * transactions.btc) * ' . $conversion . ')'; $usd_str .= ' WHEN ' . $currency1['id'] . ' THEN ' . $currency1[$usd_field] . ' '; $currency_abbr .= ' WHEN ' . $currency1['id'] . ' THEN "' . $currency1['currency'] . '" '; $currency_abbr1 .= ' WHEN ' . $currency1['id'] . ' THEN "' . $currency1['currency'] . '" '; $currency_abbr2 .= ' WHEN ' . $currency1['id'] . ' THEN "' . $currency1['currency'] . '" '; } $price_str .= ' END) END)'; $amount_str .= ' END) END)'; $usd_str .= ' END)'; $currency_abbr .= ' END)'; $currency_abbr1 .= ' END)'; $currency_abbr2 .= ' END)'; if (!$count && !$public_api_all) { $sql = "SELECT transactions.id,transactions.date,transactions.site_user,transactions.site_user1,transactions.btc,transactions.currency,transactions.currency1,transactions.btc_price,transactions.orig_btc_price,transactions.fiat, (UNIX_TIMESTAMP(transactions.date) - ({$CFG->timezone_offset})) AS time_since " . ($user > 0 ? ",IF(transactions.site_user = {$user},transaction_types.name_{$CFG->language},transaction_types1.name_{$CFG->language}) AS type, IF(transactions.site_user = {$user},transactions.fee,transactions.fee1) AS fee, IF(transactions.site_user = {$user},transactions.btc_net,transactions.btc_net1) AS btc_net, IF(transactions.site_user1 = {$user},transactions.orig_btc_price,transactions.btc_price) AS fiat_price, IF(transactions.site_user = {$user},transactions.currency,transactions.currency1) AS currency" : ", ROUND({$price_str},2) AS btc_price, LOWER(transaction_types1.name_en) AS maker_type") . ", UNIX_TIMESTAMP(transactions.date) AS datestamp " . ($order_by == 'usd_price' ? ', ROUND((' . $usd_str . ' * transactions.btc_price),2) AS usd_price' : '') . ($order_by == 'usd_amount' ? ', ROUND((' . $usd_str . ' * transactions.fiat),2) AS usd_amount' : ''); } elseif ($public_api_all && $user) { $sql = "SELECT transactions.id AS id, transactions.date AS date, UNIX_TIMESTAMP(transactions.date) AS `timestamp`, transactions.btc AS btc, LOWER(IF(transactions.site_user = {$user},transaction_types.name_{$CFG->language},transaction_types1.name_{$CFG->language})) AS side, IF(transactions.site_user1 = {$user},transactions.orig_btc_price,transactions.btc_price) AS price, ROUND((IF(transactions.site_user1 = {$user},transactions.orig_btc_price,transactions.btc_price) * IF(transactions.site_user = {$user},transactions.btc_net,transactions.btc_net1)),2) AS amount, ROUND((IF(transactions.site_user1 = {$user},transactions.orig_btc_price,transactions.btc_price) * IF(transactions.site_user = {$user},transactions.fee,transactions.fee1)),2) AS fee, {$currency_abbr} AS currency "; } elseif ($public_api_all && !$user && $currency) { $sql = "SELECT transactions.id AS id, transactions.date AS date, UNIX_TIMESTAMP(transactions.date) AS `timestamp`, transactions.btc AS btc, LOWER(transaction_types1.name_{$CFG->language}) AS maker_type, ROUND({$price_str},2) AS price, ROUND({$amount_str},2) AS amount, IF(transactions.currency != {$currency_info['id']} AND transactions.currency1 != {$currency_info['id']},{$currency_abbr2},'{$currency_info['currency']}') AS currency "; } elseif ($public_api_all && !$user) { $sql = "SELECT transactions.id AS id, transactions.date AS date, UNIX_TIMESTAMP(transactions.date) AS `timestamp`, transactions.btc AS btc, transactions.btc_price AS price, transactions.orig_btc_price AS price1, ROUND((transactions.btc_price * transactions.btc),2) AS amount, ROUND((transactions.orig_btc_price * transactions.btc),2) AS amount1, {$currency_abbr1} AS currency, {$currency_abbr2} AS currency1 "; } else { $sql = "SELECT COUNT(transactions.id) AS total "; } $sql .= " \n\t\tFROM transactions\n\t\tLEFT JOIN transaction_types ON (transaction_types.id = transactions.transaction_type)\n\t\tLEFT JOIN transaction_types transaction_types1 ON (transaction_types1.id = transactions.transaction_type1)\n\t\tWHERE 1 "; if ($user > 0) { $sql .= " AND (transactions.site_user = {$user} OR transactions.site_user1 = {$user}) "; } if ($start_date > 0) { $sql .= " AND transactions.date >= '{$start_date}' "; } if ($type > 0 && !$user) { $sql .= " AND (transactions.transaction_type = {$type} OR transactions.transaction_type1 = {$type}) "; } elseif ($type > 0 && $user) { $sql .= " AND IF(transactions.site_user = {$user},transactions.transaction_type,transactions.transaction_type1) = {$type} "; } if ($currency && $user) { $sql .= " AND transactions.currency = {$currency_info['id']} "; } if ($per_page > 0 && !$count && !$dont_paginate) { $sql .= " ORDER BY {$order_by} {$order_desc} LIMIT {$r1},{$per_page1} "; } if (!$count && $dont_paginate) { $sql .= " ORDER BY transactions.id DESC "; } $result = db_query_array($sql); if ($CFG->memcached) { if (!$result) { $result = array(); } $set = array(); if (($per_page == 5 || $per_page == 1) && !$count && !$public_api_all) { $key = 'trans_l5_' . $currency_info['currency']; $set[$key] = $result; $result1 = array_slice($result, 0, 1); $key = 'trans_l1_' . $currency_info['currency']; $set[$key] = $result1; } elseif ($public_api_all) { $key = 'trans_api' . ($per_page ? '_l' . $per_page : '') . ($user ? '_u' . $user : '') . ($currency ? '_c' . $currency_info['currency'] : '') . ($type ? '_t' . $type : ''); $set[$key] = $result; } memcached_safe_set($set, 300); } if ($result && count($result) == 0) { return false; } if (!$count) { return $result; } else { return $result[0]['total']; } }