PHP WebShell

Текущая директория: /var/www/bitcardoApp/includes/common

Просмотр файла: user.php

<?php 
if(isset($_SESSION['user_id'])){
    $userid = $_SESSION['user_id'];
}else{
    header('location: ../../auth/login.php');
    exit;
} 

// Get user details
$queryUsers = $conn->prepare("SELECT email, phone, first_name, last_name, created_at FROM users WHERE user_id = ?");
$queryUsers->bind_param("i", $userid);
$queryUsers->execute();
$queryUsers->store_result();
$queryUsers->bind_result($userEmail, $userPhone, $userFName, $userLName, $userReg);

if ($queryUsers->num_rows > 0) {
    $queryUsers->fetch();
    $f = isset($userFName[0]) ? $userFName[0] : '';
    $l = isset($userLName[0]) ? $userLName[0] : '';
    $initials = strtoupper($f . $l);
}

// Fetch all wallets for display (with icon)
$sql = "SELECT wallet_id, coin, icon, wallet_add, balance, type, label FROM user_wallets WHERE user_id = ?";
$stmt = $conn->prepare($sql);
$stmt->bind_param("i", $userid);
$stmt->execute();
$resultWallet = $stmt->get_result();

$wallets = [];
$coinSymbols = [];

// Step 1: Build wallets list
while ($row = $resultWallet->fetch_assoc()) {
    $coin = strtoupper($row['coin']);
    $wallets[$coin] = [
        'wallet_id' => $row['wallet_id'],
        'coin' => $coin,
        'icon' => $row['icon'],
        'wallet_add' => $row['wallet_add'],
        'balance' => (float)$row['balance'],
        'type' => $row['type'],
        'label' => $row['label'],
        'usd_value' => 0 // will be filled later
    ];
    if ($coin !== 'NGN') {
        $coinSymbols[] = $coin;
    }
}
// -------------------- STEP 2: Fetch USD rates from DB coin_rates --------------------
/**
 * Reads coin_rates for the given symbols (case-insensitive).
 * Uses sell_rate preferentially (what user would get in USD when selling),
 * falls back to buy_rate if sell_rate is null/zero.
 * Returns array like ['BTC' => 65000.0, 'ETH' => 3000.0, ...]
 */
function getUsdRatesFromDb(mysqli $conn, array $symbols): array {
    $symbols = array_values(array_unique(array_map('strtoupper', $symbols)));
    if (empty($symbols)) return [];

    $placeholders = implode(',', array_fill(0, count($symbols), '?'));
    $types = str_repeat('s', count($symbols));

    $sql = "SELECT UPPER(coin) AS coin, sell_rate, buy_rate
              FROM coin_rates
             WHERE UPPER(coin) IN ($placeholders)";

    $stmt = $conn->prepare($sql);
    $stmt->bind_param($types, ...$symbols);
    $stmt->execute();
    $res = $stmt->get_result();

    $rates = [];
    while ($row = $res->fetch_assoc()) {
        $coin = strtoupper($row['coin']);
        $sell = isset($row['sell_rate']) ? (float)$row['sell_rate'] : 0.0;
        $buy  = isset($row['buy_rate'])  ? (float)$row['buy_rate']  : 0.0;

        // Prefer sell_rate; fallback to buy_rate
        $rate = $sell > 0 ? $sell : ($buy > 0 ? $buy : 0.0);
        if ($rate > 0) $rates[$coin] = $rate;
    }
    $stmt->close();

    // Sensible defaults for stables/USD if missing
    $rates['USD']  = 1.0;
    $rates['USDT'] = $rates['USDT'] ?? 1.0;
    $rates['USDC'] = $rates['USDC'] ?? 1.0;

    return $rates;
}

// Build the list of non-NGN coins we need rates for
$nonNgnSymbols = [];
foreach ($wallets as $c => $w) {
    $coin = strtoupper($w['coin']);
    if ($coin !== 'NGN') $nonNgnSymbols[] = $coin;
}
$usdRates = getUsdRatesFromDb($conn, $nonNgnSymbols);

// -------------------- STEP 3 & 4: Compute totals (USD sum excludes NGN) --------------------
$totalUsd = 0.0;
$totalNgn = 0.0;

foreach ($wallets as $coin => &$wallet) {
    $c = strtoupper($wallet['coin']);
    $bal = (float)$wallet['balance'];

    if ($c === 'NGN') {
        // NGN is shown separately and NOT included in USD total
        $wallet['usd_value'] = 0.0;
        $totalNgn += $bal;
        continue;
    }

    $rate = $usdRates[$c] ?? 0.0;       // USD per 1 coin
    $wallet['usd_value'] = $bal * $rate;
    $totalUsd += $wallet['usd_value'];
}
unset($wallet);

// -------------------- STEP 5: Sort wallets by USD value (desc) --------------------
usort($wallets, function($a, $b) {
    return ($b['usd_value'] <=> $a['usd_value']);
});


?>

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


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