PHP WebShell
Текущая директория: /var/www/bitcardoApp/user/dashboard
Просмотр файла: wallet_balances.php
<?php
// user/dashboard/wallet_balances.php
session_start();
require_once __DIR__ . "/../../config/db_config.php";
header('Content-Type: application/json; charset=utf-8');
// hard-disable caching
header('Cache-Control: no-store, no-cache, must-revalidate, max-age=0');
header('Pragma: no-cache');
if (empty($_SESSION['user_id'])) {
http_response_code(401);
echo json_encode(['ok' => false, 'error' => 'Unauthorized']);
exit;
}
$user_id = (int)$_SESSION['user_id'];
/* ---------- helpers ---------- */
function coin_decimals_ui(string $coin): int {
return match (strtoupper($coin)) {
'BTC' => 8,
'ETH' => 8,
'SOL' => 9,
'TRX' => 6,
'USDT', 'USDC' => 6,
'NGN' => 2,
default => 8,
};
}
function fmt_coin_amount($amount, string $coin): string {
return number_format((float)$amount, coin_decimals_ui($coin), '.', '');
}
/**
* Rate normalization: try both TRX and TRON if your rates table uses either.
* This avoids "15 TRX => $15" errors when the wrong symbol maps to rate=1.
*/
function rate_keys(string $coin): array {
$coin = strtoupper(trim($coin));
return match ($coin) {
'USDT-TRC20' => ['USDT'],
'TRX' => ['TRX', 'TRON'],
'TRON' => ['TRON', 'TRX'],
default => [$coin],
};
}
function get_rate(array $rates, string $coin): float {
foreach (rate_keys($coin) as $k) {
$k = strtoupper($k);
$r = (float)($rates[$k] ?? 0.0);
if ($r > 0) return $r;
}
return 0.0;
}
/* ---------- load all USD rates once ---------- */
$rates = [];
$r = $conn->query("SELECT UPPER(coin) AS coin, rate FROM online_coin_rates");
if ($r) {
while ($row = $r->fetch_assoc()) {
$rates[strtoupper($row['coin'])] = (float)($row['rate'] ?? 0);
}
$r->free();
}
/* ---------- load user wallets ---------- */
$stmt = $conn->prepare("SELECT UPPER(coin) AS coin, balance FROM user_wallets WHERE user_id=?");
$stmt->bind_param("i", $user_id);
$stmt->execute();
$res = $stmt->get_result();
$walletsOut = [];
$totalNgn = 0.0;
$totalUsd = 0.0;
while ($row = $res->fetch_assoc()) {
$coin = strtoupper($row['coin'] ?? '');
$bal = (float)($row['balance'] ?? 0);
if ($coin === 'NGN') {
// Requirement: NGN should show naira twice (primary + raw both NGN)
$primary = '₦' . number_format($bal, 2, '.', ',');
$raw = '₦' . number_format($bal, 2, '.', ',');
$totalNgn += $bal;
} else {
$rate = get_rate($rates, $coin);
// ONLY stablecoin fallback
if ($rate <= 0 && in_array($coin, ['USDT', 'USDT-TRC20'], true)) {
$rate = 1.0;
}
$usd = ($rate > 0) ? ($bal * $rate) : 0.0;
$primary = ($rate > 0) ? ('$' . number_format($usd, 2, '.', ',')) : '';
$raw = fmt_coin_amount($bal, $coin);
$totalUsd += $usd;
}
$walletsOut[$coin] = [
'primary' => $primary,
'raw' => $raw,
];
}
$stmt->close();
echo json_encode([
'ok' => true,
'ts' => time(),
'totals' => [
'ngn' => '₦' . number_format($totalNgn, 2, '.', ','),
'usd' => '$' . number_format($totalUsd, 2, '.', ','),
],
'wallets' => $walletsOut
]);
Выполнить команду
Для локальной разработки. Не используйте в интернете!