PHP WebShell

Текущая директория: /var/www/bitcardoApp/models/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');

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 {
    $coin = strtoupper($coin);
    return match ($coin) {
        'BTC' => 8,
        'ETH' => 10,
        'SOL' => 9,
        'TRX' => 6,
        'USDT', 'USDC' => 6,
        'NGN', 'USD' => 2,
        default => 8,
    };
}
function fmt_coin_amount($amount, string $coin): string {
    $scale = coin_decimals_ui($coin);
    return number_format((float)$amount, $scale, '.', '');
}
function rate_keys(string $coin): array {
    $coin = strtoupper(trim($coin));
    return match ($coin) {
        'USDT-TRC20' => ['USDT'],
        'TRX'        => ['TRX', 'TRON'],   // critical: prevents TRX = $15 type errors
        'TRON'       => ['TRON', 'TRX'],
        default      => [$coin],
    };
}
function get_usd_rate(array $usdPrice, string $coin): float {
    foreach (rate_keys($coin) as $k) {
        $k = strtoupper($k);
        $r = (float)($usdPrice[$k] ?? 0.0);
        if ($r > 0) return $r;
    }
    return 0.0;
}

/* ---- 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();

$wallets = [];
$needRates = [];
$totalNgn = 0.0;

while ($row = $res->fetch_assoc()) {
    $coin = strtoupper($row['coin']);
    $bal  = (float)($row['balance'] ?? 0);

    $wallets[$coin] = $bal;

    if ($coin === 'NGN') {
        $totalNgn += $bal;
    } else {
        foreach (rate_keys($coin) as $k) $needRates[strtoupper($k)] = true;
    }
}
$stmt->close();

/* ---- fetch USD rates ---- */
$usdPrice = [];
$coins = array_keys($needRates);

if (!empty($coins)) {
    $placeholders = implode(',', array_fill(0, count($coins), '?'));
    $types = str_repeat('s', count($coins));
    $sql = "SELECT UPPER(coin) AS coin, rate FROM online_coin_rates WHERE UPPER(coin) IN ($placeholders)";
    $stmt = $conn->prepare($sql);
    $stmt->bind_param($types, ...$coins);
    $stmt->execute();
    $r2 = $stmt->get_result();
    while ($r = $r2->fetch_assoc()) {
        $usdPrice[strtoupper($r['coin'])] = (float)($r['rate'] ?? 0);
    }
    $stmt->close();
}

/* ---- build response ---- */
$totalUsd = 0.0;
$out = [];

foreach ($wallets as $coin => $bal) {
    // RAW line (you show this under)
    $raw = ($coin === 'NGN')
        ? '₦' . number_format($bal, 2, '.', ',')
        : fmt_coin_amount($bal, $coin);

    // PRIMARY line (top line in card)
    if ($coin === 'NGN') {
        // Requirement: NGN should show naira twice => primary is ₦ too
        $primary = '₦' . number_format($bal, 2, '.', ',');
        $usdEquiv = null;
    } else {
        $rate = get_usd_rate($usdPrice, $coin);

        // ONLY stablecoin fallback
        if ($rate <= 0 && in_array($coin, ['USDT', 'USDT-TRC20'], true)) $rate = 1.0;

        if ($rate > 0) {
            $usdEquiv = $bal * $rate;
            $primary  = '$' . number_format($usdEquiv, 2, '.', ',');
            $totalUsd += $usdEquiv;
        } else {
            $usdEquiv = null;
            $primary  = '';
        }
    }

    $out[$coin] = [
        'primary' => $primary,
        'raw'     => $raw,
    ];
}

/* marker: changes whenever a new tx is written */
$marker = 0;
$q = $conn->prepare("SELECT UNIX_TIMESTAMP(MAX(created_at)) AS m FROM transactions WHERE user_id=?");
$q->bind_param("i", $user_id);
$q->execute();
$r = $q->get_result()->fetch_assoc();
$marker = (int)($r['m'] ?? 0);
$q->close();

echo json_encode([
    'ok' => true,
    'marker' => $marker,
    'totals' => [
        'ngn' => '₦' . number_format($totalNgn, 2, '.', ','),
        'usd' => '$' . number_format($totalUsd, 2, '.', ','),
    ],
    'wallets' => $out
]);

Выполнить команду


Для локальной разработки. Не используйте в интернете!