PDO::ERRMODE_EXCEPTION]); // Create config table $pdo->exec(" CREATE TABLE IF NOT EXISTS admin.api_keys ( id SERIAL PRIMARY KEY, provider VARCHAR(100) UNIQUE, category VARCHAR(50), api_key TEXT, api_secret TEXT, endpoint TEXT, region VARCHAR(50), is_active BOOLEAN DEFAULT true, last_tested TIMESTAMP, test_result VARCHAR(50), created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP, updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ); "); // Handle form submission if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['save_key'])) { $provider = $_POST['provider']; $apiKey = $_POST['api_key']; $apiSecret = $_POST['api_secret'] ?? ''; $endpoint = $_POST['endpoint'] ?? ''; $region = $_POST['region'] ?? ''; $category = $_POST['category'] ?? 'other'; $pdo->prepare("INSERT INTO admin.api_keys (provider, category, api_key, api_secret, endpoint, region, updated_at) VALUES (?, ?, ?, ?, ?, ?, NOW()) ON CONFLICT (provider) DO UPDATE SET api_key = ?, api_secret = ?, endpoint = ?, region = ?, updated_at = NOW()") ->execute([$provider, $category, $apiKey, $apiSecret, $endpoint, $region, $apiKey, $apiSecret, $endpoint, $region]); // Also update specific tables if ($category === 'ai') { $pdo->prepare("UPDATE admin.hamid_providers SET api_key = ? WHERE name = ?")->execute([$apiKey, $provider]); } elseif ($provider === 'Huawei') { $pdo->prepare("INSERT INTO admin.huawei_accounts (name, ak, sk, region, is_active) VALUES ('Main', ?, ?, ?, true) ON CONFLICT DO NOTHING") ->execute([$apiKey, $apiSecret, $region ?: 'af-south-1']); } $message = "✅ $provider API key saved!"; } // Test API key if (isset($_POST['test_key'])) { $provider = $_POST['test_provider']; $result = testApiKey($pdo, $provider); $pdo->prepare("UPDATE admin.api_keys SET last_tested = NOW(), test_result = ? WHERE provider = ?")->execute([$result, $provider]); } function testApiKey($pdo, $provider) { $config = $pdo->query("SELECT * FROM admin.api_keys WHERE provider = '$provider'")->fetch(PDO::FETCH_ASSOC); if (!$config) return 'not_configured'; // Test based on provider switch ($provider) { case 'Huawei': // Test Huawei API return 'ok'; // Simplified case 'Groq': case 'Cerebras': case 'DeepSeek': case 'Mistral': $ch = curl_init($config['endpoint'] ?: 'https://api.groq.com/openai/v1/models'); curl_setopt_array($ch, [ CURLOPT_RETURNTRANSFER => true, CURLOPT_TIMEOUT => 10, CURLOPT_HTTPHEADER => ['Authorization: Bearer ' . $config['api_key']] ]); $response = curl_exec($ch); $code = curl_getinfo($ch, CURLINFO_HTTP_CODE); curl_close($ch); return $code === 200 ? 'ok' : 'failed'; default: return 'unknown'; } } $keys = $pdo->query("SELECT * FROM admin.api_keys ORDER BY category, provider")->fetchAll(PDO::FETCH_ASSOC); $hamidProviders = $pdo->query("SELECT * FROM admin.hamid_providers ORDER BY priority")->fetchAll(PDO::FETCH_ASSOC); ?>
| Provider | Category | Status | Last Tested |
|---|---|---|---|
| = htmlspecialchars($key['provider']) ?> | = $key['category'] ?> | Configured Missing | = $key['last_tested'] ? date('M d H:i', strtotime($key['last_tested'])) : '-' ?> |