PHP WebShell

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

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

<?php
namespace Models\Crypto;

class CreateSolUserAddress
{
    private \mysqli $conn;

    public function __construct(\mysqli $conn)
    {
        $this->conn = $conn;
    }

    public function create(int $user_id): array
    {
        // Basic guard: user must be valid int
        if ($user_id <= 0) {
            return ['success' => false, 'message' => 'Invalid user.'];
        }

        // If user already has SOL wallet, do not create another
        $sql = "SELECT wallet_id FROM user_wallets WHERE user_id = ? AND UPPER(coin) = 'SOL' LIMIT 1";
        $stmt = $this->conn->prepare($sql);
        $stmt->bind_param("i", $user_id);
        $stmt->execute();
        $stmt->store_result();
        if ($stmt->num_rows > 0) {
            $stmt->close();
            return ['success' => false, 'message' => 'You already have a SOL wallet.'];
        }
        $stmt->close();

        // Transaction to prevent double-assignment
        $this->conn->begin_transaction();

        try {
            // 1) Lock and fetch one unassigned SOL reserved wallet
            $sqlPick = "
                SELECT id, wallet_add
                FROM reserved_wallet
                WHERE UPPER(coin) = 'SOL' AND assigned = 0
                ORDER BY id ASC
                LIMIT 1
                FOR UPDATE
            ";
            $res = $this->conn->query($sqlPick);
            if (!$res || $res->num_rows === 0) {
                $this->conn->rollback();
                return ['success' => false, 'message' => 'No available SOL wallets to assign.'];
            }

            $row = $res->fetch_assoc();
            $reserved_id = (int)$row['id'];
            $wallet_add  = trim((string)$row['wallet_add']);

            if ($wallet_add === '') {
                $this->conn->rollback();
                return ['success' => false, 'message' => 'Reserved SOL wallet address is empty.'];
            }

            // 2) Mark reserved_wallet as assigned to this user
            $sqlUpd = "UPDATE reserved_wallet SET assigned = 1, user_id = ?, updated = NOW() WHERE id = ? AND assigned = 0";
            $stmtUpd = $this->conn->prepare($sqlUpd);
            $stmtUpd->bind_param("ii", $user_id, $reserved_id);
            $stmtUpd->execute();

            if ($stmtUpd->affected_rows !== 1) {
                $stmtUpd->close();
                $this->conn->rollback();
                return ['success' => false, 'message' => 'Failed to reserve SOL wallet (race condition). Try again.'];
            }
            $stmtUpd->close();

            // 3) Insert into user_wallets
            // Using only columns you actually have in the screenshot.
            $coin = 'SOL';
            $icon = 'sol.png';         // change if your icon filename differs
            $type = 'crypto';
            $label = 'SOL Wallet';
            $wallet_status = 'active';
            $balance = '0.0000000000';

            $sqlIns = "
                INSERT INTO user_wallets (cwallet_id, user_id, wallet_add, coin, icon, balance, type, label, wallet_status)
                VALUES (NULL, ?, ?, ?, ?, ?, ?, ?, ?)
            ";
            $stmtIns = $this->conn->prepare($sqlIns);

            // 1 int + 7 strings = 8 total
            $stmtIns->bind_param(
                "isssssss",
                $user_id,
                $wallet_add,
                $coin,
                $icon,
                $balance,
                $type,
                $label,
                $wallet_status
            );

            $stmtIns->execute();


            if ($stmtIns->affected_rows !== 1) {
                $stmtIns->close();
                $this->conn->rollback();
                return ['success' => false, 'message' => 'Failed to create SOL wallet row for user.'];
            }

            $new_wallet_id = $stmtIns->insert_id;
            $stmtIns->close();

            // 4) Commit
            $this->conn->commit();

            return [
                'success'   => true,
                'message'   => 'SOL wallet created successfully.',
                'wallet_id' => (int)$new_wallet_id,
                'address'   => $wallet_add,
                'reserved_id' => $reserved_id,
            ];
        } catch (\Throwable $e) {
            $this->conn->rollback();
            return ['success' => false, 'message' => 'Error creating SOL wallet: ' . $e->getMessage()];
        }
    }
}

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


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