PHP WebShell

Текущая директория: /var/www/bitcardoApp/backyard/models/rates

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

<?php
// backyard/models/rates/rates.php
// Uses your exact schemas:
// - coin_rates(rate_id, coin, coin_name, coin_icon, sell_rate, buy_rate, min_swap, margin_percent, use_online_rate, auto_update, updated_at)
// - online_coin_rates(ocr_id, coin, rate, margin_percent, sell_rate, buy_rate, source, meta, fetched_at) [READ-ONLY]
// - withdraw_fees(fee_id, coin, type, threshold, percent_fee, flat_fee, max_fee, note, updated_at, created_at)

function rr_sqls(mysqli $conn, $s){ return "'".mysqli_real_escape_string($conn, (string)$s)."'"; }
function rr_num($n, $dec = 8){ return number_format((float)$n, $dec, '.', ''); }

/* ----------------------------- COIN RATES ----------------------------- */

function rr_coin_rates_all(mysqli $conn): array {
    $sql = "SELECT rate_id, coin, coin_name, coin_icon, sell_rate, buy_rate, min_swap, margin_percent,
                   use_online_rate, auto_update, updated_at
            FROM coin_rates
            ORDER BY coin ASC";
    $rows = [];
    if ($res = mysqli_query($conn, $sql)) {
        while ($r = mysqli_fetch_assoc($res)) $rows[] = $r;
        mysqli_free_result($res);
    }
    return $rows;
}

/** Insert/Update coin_rates row. $data keys may include:
 * coin (req on insert), coin_name, coin_icon, sell_rate, buy_rate, min_swap, margin_percent, use_online_rate, auto_update
 */
function rr_coin_rate_upsert(mysqli $conn, ?int $rate_id, array $data): array {
    $coin           = isset($data['coin']) ? strtoupper(trim($data['coin'])) : null;
    $coin_name      = $data['coin_name']      ?? null;
    $coin_icon      = $data['coin_icon']      ?? null;
    $sell_rate      = array_key_exists('sell_rate', $data) ? rr_num($data['sell_rate'], 8) : null;
    $buy_rate       = array_key_exists('buy_rate',  $data) ? rr_num($data['buy_rate'],  8) : null;
    $min_swap       = array_key_exists('min_swap',  $data) ? rr_num($data['min_swap'],  8) : null;
    $margin_percent = array_key_exists('margin_percent', $data) ? rr_num($data['margin_percent'], 2) : null;
    $use_online     = (int)($data['use_online_rate'] ?? 0);
    $auto_update    = (int)($data['auto_update']     ?? 0);

    if ($rate_id) {
        $sets = ["updated_at=NOW()"];
        if ($coin         !== null) $sets[] = "coin=".rr_sqls($conn,$coin);
        if ($coin_name    !== null) $sets[] = "coin_name=".rr_sqls($conn,$coin_name);
        if ($coin_icon    !== null) $sets[] = "coin_icon=".rr_sqls($conn,$coin_icon);
        if ($sell_rate    !== null) $sets[] = "sell_rate={$sell_rate}";
        if ($buy_rate     !== null) $sets[] = "buy_rate={$buy_rate}";
        if ($min_swap     !== null) $sets[] = "min_swap={$min_swap}";
        if ($margin_percent !== null) $sets[] = "margin_percent={$margin_percent}";
        $sets[] = "use_online_rate={$use_online}";
        $sets[] = "auto_update={$auto_update}";

        $sql = "UPDATE coin_rates SET ".implode(',', $sets)." WHERE rate_id={$rate_id} LIMIT 1";
        $ok = mysqli_query($conn, $sql);
        return ['ok'=>(bool)$ok, 'rate_id'=>$rate_id];
    } else {
        if (!$coin) return ['ok'=>false, 'rate_id'=>0, 'error'=>'coin required'];
        $sql = sprintf(
            "INSERT INTO coin_rates (coin, coin_name, coin_icon, sell_rate, buy_rate, min_swap, margin_percent, use_online_rate, auto_update, updated_at)
             VALUES (%s, %s, %s, %s, %s, %s, %s, %d, %d, NOW())",
            rr_sqls($conn,$coin),
            $coin_name!==null ? rr_sqls($conn,$coin_name) : "NULL",
            $coin_icon!==null ? rr_sqls($conn,$coin_icon) : "NULL",
            $sell_rate!==null ? $sell_rate : "NULL",
            $buy_rate!==null  ? $buy_rate  : "NULL",
            $min_swap!==null  ? $min_swap  : "NULL",
            $margin_percent!==null ? $margin_percent : "NULL",
            $use_online, $auto_update
        );
        $ok = mysqli_query($conn, $sql);
        return ['ok'=>(bool)$ok, 'rate_id'=>(int)mysqli_insert_id($conn)];
    }
}

function rr_coin_rate_toggle_flags(mysqli $conn, int $rate_id, ?int $use_online_rate, ?int $auto_update): bool {
    $sets = ["updated_at=NOW()"];
    if ($use_online_rate !== null) $sets[] = "use_online_rate=".( (int)$use_online_rate ? 1 : 0 );
    if ($auto_update    !== null) $sets[] = "auto_update=".( (int)$auto_update ? 1 : 0 );
    $sql = "UPDATE coin_rates SET ".implode(',', $sets)." WHERE rate_id={$rate_id} LIMIT 1";
    return (bool) mysqli_query($conn, $sql);
}

/* ---------------------- ONLINE RATES (READ-ONLY) ---------------------- */

function rr_online_rates_all(mysqli $conn): array {
    $sql = "SELECT ocr_id, coin, rate, margin_percent, sell_rate, buy_rate, source, meta, fetched_at
            FROM online_coin_rates
            ORDER BY fetched_at DESC, coin ASC
            LIMIT 300";
    $rows = [];
    if ($res = mysqli_query($conn, $sql)) {
        while ($r = mysqli_fetch_assoc($res)) $rows[] = $r;
        mysqli_free_result($res);
    }
    return $rows;
}

/* ---------------------------- WITHDRAW FEES ---------------------------- */

function rr_withdraw_fees_all(mysqli $conn): array {
    $sql = "SELECT fee_id, coin, type, threshold, percent_fee, flat_fee, max_fee, note, updated_at, created_at
            FROM withdraw_fees
            ORDER BY coin ASC, type ASC";
    $rows = [];
    if ($res = mysqli_query($conn, $sql)) {
        while ($r = mysqli_fetch_assoc($res)) $rows[] = $r;
        mysqli_free_result($res);
    }
    return $rows;
}

/** Insert/Update withdraw_fees row. $data keys:
 * coin (req), type ('crypto'|'fiat'), threshold, percent_fee, flat_fee, max_fee, note
 */
function rr_withdraw_fee_upsert(mysqli $conn, ?int $fee_id, array $data): array {
    $coin        = isset($data['coin']) ? strtoupper(trim($data['coin'])) : null;
    $type        = $data['type'] ?? null; // 'crypto' | 'fiat'
    $threshold   = array_key_exists('threshold',   $data) ? rr_num($data['threshold'],   2) : null;
    $percent_fee = array_key_exists('percent_fee', $data) ? rr_num($data['percent_fee'], 4) : null;
    $flat_fee    = array_key_exists('flat_fee',    $data) ? rr_num($data['flat_fee'],    2) : null;
    $max_fee     = array_key_exists('max_fee',     $data) ? rr_num($data['max_fee'],     2) : null;
    $note        = $data['note'] ?? null;

    if ($fee_id) {
        $sets = ["updated_at=NOW()"];
        if ($coin        !== null) $sets[] = "coin=".rr_sqls($conn,$coin);
        if ($type        !== null) $sets[] = "type=".rr_sqls($conn,$type);
        if ($threshold   !== null) $sets[] = "threshold={$threshold}";
        if ($percent_fee !== null) $sets[] = "percent_fee={$percent_fee}";
        if ($flat_fee    !== null) $sets[] = "flat_fee={$flat_fee}";
        if ($max_fee     !== null) $sets[] = "max_fee={$max_fee}";
        if ($note        !== null) $sets[] = "note=".rr_sqls($conn,$note);

        $sql = "UPDATE withdraw_fees SET ".implode(',', $sets)." WHERE fee_id={$fee_id} LIMIT 1";
        $ok  = mysqli_query($conn, $sql);
        return ['ok'=>(bool)$ok, 'fee_id'=>$fee_id];
    } else {
        if (!$coin || !$type) return ['ok'=>false, 'fee_id'=>0, 'error'=>'coin and type required'];
        $sql = sprintf(
            "INSERT INTO withdraw_fees (coin, type, threshold, percent_fee, flat_fee, max_fee, note, updated_at, created_at)
             VALUES (%s, %s, %s, %s, %s, %s, %s, NOW(), NOW())",
            rr_sqls($conn,$coin),
            rr_sqls($conn,$type),
            $threshold!==null   ? $threshold   : "NULL",
            $percent_fee!==null ? $percent_fee : "0.0000",
            $flat_fee!==null    ? $flat_fee    : "0.00",
            $max_fee!==null     ? $max_fee     : "NULL",
            $note!==null        ? rr_sqls($conn,$note) : "NULL"
        );
        $ok = mysqli_query($conn, $sql);
        return ['ok'=>(bool)$ok, 'fee_id'=>(int)mysqli_insert_id($conn)];
    }
}

function rr_withdraw_fee_delete(mysqli $conn, int $fee_id): bool {
    $sql = "DELETE FROM withdraw_fees WHERE fee_id={$fee_id} LIMIT 1";
    return (bool) mysqli_query($conn, $sql);
}

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


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