116 lines
4.0 KiB
PHP
116 lines
4.0 KiB
PHP
<?php
|
|
// Generated by setup_mariadb_project.sh — edit as needed.
|
|
// Baota / Local Deployment Settings - Change these to match your database
|
|
date_default_timezone_set('Asia/Shanghai');
|
|
define('DB_HOST', '127.0.0.1');
|
|
define('DB_NAME', 'app_38451');
|
|
define('DB_USER', 'app_38451');
|
|
define('DB_PASS', '05a5bcfb-fa2e-4781-9241-aec4868564f1');
|
|
|
|
function db() {
|
|
static $pdo;
|
|
if (!$pdo) {
|
|
$pdo = new PDO('mysql:host='.DB_HOST.';dbname='.DB_NAME.';charset=utf8mb4', DB_USER, DB_PASS, [
|
|
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
|
|
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
|
|
]);
|
|
}
|
|
return $pdo;
|
|
}
|
|
|
|
function getRealIP() {
|
|
if (!empty($_SERVER['HTTP_CF_CONNECTING_IP'])) {
|
|
return $_SERVER['HTTP_CF_CONNECTING_IP'];
|
|
}
|
|
if (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
|
|
$ips = explode(',', $_SERVER['HTTP_X_FORWARDED_FOR']);
|
|
return trim($ips[0]);
|
|
}
|
|
return $_SERVER['REMOTE_ADDR'];
|
|
}
|
|
|
|
function getUserTotalRecharge($userId) {
|
|
$stmt = db()->prepare("SELECT SUM(amount) FROM finance_requests WHERE user_id = ? AND type='recharge' AND status='3' AND symbol='USDT'");
|
|
$stmt->execute([$userId]);
|
|
return (float)$stmt->fetchColumn() ?: 0;
|
|
}
|
|
|
|
function getAutoVipLevel($totalRecharge) {
|
|
if ($totalRecharge >= 250000) return 5;
|
|
if ($totalRecharge >= 180000) return 4;
|
|
if ($totalRecharge >= 100000) return 3;
|
|
if ($totalRecharge >= 50000) return 2;
|
|
if ($totalRecharge >= 10000) return 1;
|
|
return 0;
|
|
}
|
|
|
|
if (!function_exists('getSetting')) {
|
|
function getSetting($key, $default = null) {
|
|
try {
|
|
$stmt = db()->prepare("SELECT setting_value FROM system_settings WHERE setting_key = ?");
|
|
$stmt->execute([$key]);
|
|
$val = $stmt->fetchColumn();
|
|
return ($val !== false && $val !== '') ? $val : $default;
|
|
} catch (Exception $e) {
|
|
return $default;
|
|
}
|
|
}
|
|
}
|
|
|
|
// Ensure database schema is up to date (Automatic Migration)
|
|
function ensureSchema() {
|
|
try {
|
|
$db = db();
|
|
|
|
// --- finance_requests table ---
|
|
$stmt = $db->query("DESCRIBE finance_requests");
|
|
$columns = $stmt->fetchAll(PDO::FETCH_COLUMN);
|
|
|
|
$finance_needed = [
|
|
'account_bank' => "VARCHAR(255) DEFAULT NULL",
|
|
'account_name' => "VARCHAR(255) DEFAULT NULL",
|
|
'account_number' => "VARCHAR(255) DEFAULT NULL",
|
|
'fiat_amount' => "DECIMAL(20,8) DEFAULT 0",
|
|
'fiat_currency' => "VARCHAR(10) DEFAULT NULL",
|
|
'ip_address' => "VARCHAR(45) DEFAULT NULL",
|
|
'payment_method' => "VARCHAR(100) DEFAULT NULL",
|
|
'payment_details' => "TEXT DEFAULT NULL"
|
|
];
|
|
|
|
foreach ($finance_needed as $col => $type) {
|
|
if (!in_array($col, $columns)) {
|
|
$db->exec("ALTER TABLE finance_requests ADD COLUMN $col $type");
|
|
}
|
|
}
|
|
|
|
// --- transactions table ---
|
|
$stmt = $db->query("DESCRIBE transactions");
|
|
$columns = $stmt->fetchAll(PDO::FETCH_COLUMN);
|
|
|
|
$trans_needed = [
|
|
'ip_address' => "VARCHAR(45) DEFAULT NULL",
|
|
'status' => "VARCHAR(20) DEFAULT '0'"
|
|
];
|
|
|
|
foreach ($trans_needed as $col => $type) {
|
|
if (!in_array($col, $columns)) {
|
|
$db->exec("ALTER TABLE transactions ADD COLUMN $col $type");
|
|
}
|
|
}
|
|
|
|
// --- chat_visitors table ---
|
|
$stmt = $db->query("SHOW INDEX FROM chat_visitors WHERE Key_name = 'session_id_unique'");
|
|
if (!$stmt->fetch()) {
|
|
try {
|
|
// First delete duplicates to allow adding unique index
|
|
$db->exec("DELETE c1 FROM chat_visitors c1 INNER JOIN chat_visitors c2 WHERE c1.id < c2.id AND c1.session_id = c2.session_id");
|
|
$db->exec("ALTER TABLE chat_visitors ADD UNIQUE KEY `session_id_unique` (`session_id`)");
|
|
} catch (Exception $e) {}
|
|
}
|
|
|
|
} catch (Exception $e) {
|
|
// Silently fail
|
|
}
|
|
}
|
|
ensureSchema();
|