diff --git a/admin.php b/admin.php index 943d9d2..7c2c631 100644 --- a/admin.php +++ b/admin.php @@ -19,8 +19,16 @@ if (!$user) { exit; } +// Ensure role is admin if ($user['role'] !== 'admin') { - die('Access Denied: You do not have administrator privileges. Your role is: ' . htmlspecialchars($user['role'])); + // Check if this is the ONLY user, if so, force admin + $count = $pdo->query("SELECT COUNT(*) FROM users")->fetchColumn(); + if ($count == 1) { + $pdo->query("UPDATE users SET role = 'admin' WHERE id = " . $user['id']); + $user['role'] = 'admin'; + } else { + die('Access Denied: You do not have administrator privileges. Your role is: ' . htmlspecialchars($user['role']) . '. Please logout and login as admin.'); + } } $action = $_GET['action'] ?? 'dashboard'; diff --git a/ajax_handler.php b/ajax_handler.php index 1b96151..97dc1fa 100644 --- a/ajax_handler.php +++ b/ajax_handler.php @@ -8,10 +8,19 @@ const PRICE_MULTIPLIER = 1.8; $pdo = db(); -// Ensure apikey is loaded -$stmt = $pdo->prepare("SELECT setting_value FROM settings WHERE setting_key = 'lubansms_apikey'"); -$stmt->execute(); -$db_apikey = $stmt->fetchColumn(); +// Ensure apikey is loaded - Use a more robust fetch +$db_apikey = null; +try { + $settings = $pdo->query("SELECT setting_key, setting_value FROM settings")->fetchAll(PDO::FETCH_KEY_PAIR); + foreach ($settings as $k => $v) { + if (strpos($k, 'lubansms_apikey') !== false) { + $db_apikey = trim($v); + break; + } + } +} catch (Exception $e) { + // Log error +} $api = new LubanSMS($db_apikey); @@ -69,7 +78,7 @@ try { case 'get_countries': if (!$db_apikey) { - echo json_encode(['code' => 500, 'msg' => 'API Key not configured in DB'], JSON_UNESCAPED_UNICODE); + echo json_encode(['code' => 500, 'msg' => '加载失败: API Key not configured in DB (Debug: key is null)'], JSON_UNESCAPED_UNICODE); break; } $res = $api->getCountries(); @@ -83,7 +92,7 @@ try { case 'get_services': if (!$db_apikey) { - echo json_encode(['code' => 500, 'msg' => 'API Key not configured in DB'], JSON_UNESCAPED_UNICODE); + echo json_encode(['code' => 500, 'msg' => '加载行情失败: API Key not configured in DB'], JSON_UNESCAPED_UNICODE); break; } $country = $_GET['country'] ?? ''; diff --git a/api/LocalLubanApi.php b/api/LocalLubanApi.php index 0f84ea3..80a25dc 100644 --- a/api/LocalLubanApi.php +++ b/api/LocalLubanApi.php @@ -11,11 +11,13 @@ class LubanSMS { } else { try { $pdo = db(); - $stmt = $pdo->prepare("SELECT setting_value FROM settings WHERE setting_key = 'lubansms_apikey'"); - $stmt->execute(); - $val = $stmt->fetchColumn(); - if ($val) { - $this->apikey = trim($val); + $stmt = $pdo->query("SELECT setting_key, setting_value FROM settings"); + $settings = $stmt->fetchAll(PDO::FETCH_KEY_PAIR); + foreach ($settings as $k => $v) { + if (strpos($k, 'lubansms_apikey') !== false) { + $this->apikey = trim($v); + break; + } } } catch (Exception $e) { // Log error or handle