PDO::ERRMODE_EXCEPTION]); } return $pdo; } function cfAPI($endpoint, $email, $apiKey, $method = 'GET', $data = null) { $ch = curl_init("https://api.cloudflare.com/client/v4" . $endpoint); curl_setopt_array($ch, [ CURLOPT_RETURNTRANSFER => true, CURLOPT_HTTPHEADER => [ 'X-Auth-Email: ' . $email, 'X-Auth-Key: ' . $apiKey, 'Content-Type: application/json' ], CURLOPT_CUSTOMREQUEST => $method, CURLOPT_TIMEOUT => 30 ]); if ($data) curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data)); $resp = curl_exec($ch); curl_close($ch); return json_decode($resp, true); } // AJAX if (isset($_REQUEST['action'])) { header('Content-Type: application/json'); $pdo = getDB(); switch ($_REQUEST['action']) { case 'accounts': $stmt = $pdo->query(" SELECT ca.id, ca.email, ca.password, CASE WHEN ca.api_key IS NOT NULL AND ca.api_key != '' THEN true ELSE false END as has_api, ca.api_key, COUNT(cz.id) as zones_count FROM admin.cloudflare_accounts ca LEFT JOIN admin.cloudflare_zones cz ON cz.account_id = ca.id GROUP BY ca.id ORDER BY ca.id "); echo json_encode($stmt->fetchAll(PDO::FETCH_ASSOC)); break; case 'test_api': $email = $_POST['email'] ?? ''; $apiKey = $_POST['api_key'] ?? ''; $result = cfAPI('/user', $email, $apiKey); echo json_encode([ 'success' => $result['success'] ?? false, 'email' => $result['result']['email'] ?? null ]); break; case 'save_api': $id = $_POST['id'] ?? 0; $apiKey = $_POST['api_key'] ?? ''; $stmt = $pdo->prepare("UPDATE admin.cloudflare_accounts SET api_key = ?, last_api_check = NOW(), api_status = 'valid' WHERE id = ?"); $stmt->execute([$apiKey, $id]); echo json_encode(['success' => true]); break; case 'fetch_zones': $id = $_POST['id'] ?? 0; $stmt = $pdo->prepare("SELECT email, api_key FROM admin.cloudflare_accounts WHERE id = ?"); $stmt->execute([$id]); $acc = $stmt->fetch(PDO::FETCH_ASSOC); if (!$acc || !$acc['api_key']) { echo json_encode(['success' => false, 'error' => 'No API key']); break; } $result = cfAPI('/zones?per_page=50', $acc['email'], $acc['api_key']); if (!$result['success']) { echo json_encode(['success' => false, 'error' => $result['errors'][0]['message'] ?? 'API Error']); break; } $imported = 0; foreach ($result['result'] as $zone) { // Insert into cloudflare_zones $stmt = $pdo->prepare(" INSERT INTO admin.cloudflare_zones (account_id, zone_id, domain, status) VALUES (?, ?, ?, ?) ON CONFLICT (zone_id) DO UPDATE SET domain = EXCLUDED.domain, status = EXCLUDED.status "); $stmt->execute([$id, $zone['id'], $zone['name'], $zone['status']]); // Update domains_pool if exists $stmt = $pdo->prepare(" UPDATE admin.domains_pool SET zone_id = ?, cf_account_id = ?, provider = 'cloudflare' WHERE domain = ? OR domain LIKE ? "); $stmt->execute([$zone['id'], $id, $zone['name'], '%.' . $zone['name']]); $imported++; } // Update account domains count $stmt = $pdo->prepare("UPDATE admin.cloudflare_accounts SET domains_count = ? WHERE id = ?"); $stmt->execute([$imported, $id]); echo json_encode(['success' => true, 'imported' => $imported, 'zones' => $result['result']]); break; case 'get_zones': $id = $_POST['id'] ?? 0; $stmt = $pdo->prepare("SELECT zone_id, domain, status FROM admin.cloudflare_zones WHERE account_id = ?"); $stmt->execute([$id]); echo json_encode($stmt->fetchAll(PDO::FETCH_ASSOC)); break; case 'update_dns': $zoneId = $_POST['zone_id'] ?? ''; $domain = $_POST['domain'] ?? ''; $ip = $_POST['ip'] ?? '151.80.235.110'; $accountId = $_POST['account_id'] ?? 0; $stmt = $pdo->prepare("SELECT email, api_key FROM admin.cloudflare_accounts WHERE id = ?"); $stmt->execute([$accountId]); $acc = $stmt->fetch(PDO::FETCH_ASSOC); if (!$acc || !$acc['api_key']) { echo json_encode(['success' => false, 'error' => 'No API key']); break; } // Get existing A records $existing = cfAPI("/zones/$zoneId/dns_records?type=A&name=$domain", $acc['email'], $acc['api_key']); if (!empty($existing['result'])) { // Update existing $recordId = $existing['result'][0]['id']; $result = cfAPI("/zones/$zoneId/dns_records/$recordId", $acc['email'], $acc['api_key'], 'PUT', [ 'type' => 'A', 'name' => $domain, 'content' => $ip, 'ttl' => 1, 'proxied' => false ]); } else { // Create new $result = cfAPI("/zones/$zoneId/dns_records", $acc['email'], $acc['api_key'], 'POST', [ 'type' => 'A', 'name' => $domain, 'content' => $ip, 'ttl' => 1, 'proxied' => false ]); } echo json_encode($result); break; case 'create_subdomain': $zoneId = $_POST['zone_id'] ?? ''; $subdomain = $_POST['subdomain'] ?? 'track'; $ip = $_POST['ip'] ?? '151.80.235.110'; $accountId = $_POST['account_id'] ?? 0; $stmt = $pdo->prepare("SELECT email, api_key FROM admin.cloudflare_accounts WHERE id = ?"); $stmt->execute([$accountId]); $acc = $stmt->fetch(PDO::FETCH_ASSOC); // Get zone name $stmt = $pdo->prepare("SELECT domain FROM admin.cloudflare_zones WHERE zone_id = ?"); $stmt->execute([$zoneId]); $zoneName = $stmt->fetchColumn(); $fullDomain = $subdomain . '.' . $zoneName; $result = cfAPI("/zones/$zoneId/dns_records", $acc['email'], $acc['api_key'], 'POST', [ 'type' => 'A', 'name' => $fullDomain, 'content' => $ip, 'ttl' => 1, 'proxied' => false ]); if ($result['success']) { // Add to tracking_domains $pdo->prepare(" INSERT INTO admin.tracking_domains (domain, server_ip, is_primary, status) VALUES (?, ?, FALSE, 'standby') ON CONFLICT DO NOTHING ")->execute([$fullDomain, $ip]); } echo json_encode(array_merge($result, ['domain' => $fullDomain])); break; case 'bulk_fetch': // Fetch zones for ALL accounts with API keys $stmt = $pdo->query("SELECT id, email, api_key FROM admin.cloudflare_accounts WHERE api_key IS NOT NULL AND api_key != ''"); $accounts = $stmt->fetchAll(PDO::FETCH_ASSOC); $results = []; foreach ($accounts as $acc) { $zones = cfAPI('/zones?per_page=50', $acc['email'], $acc['api_key']); if ($zones['success']) { foreach ($zones['result'] as $zone) { $stmt = $pdo->prepare(" INSERT INTO admin.cloudflare_zones (account_id, zone_id, domain, status) VALUES (?, ?, ?, ?) ON CONFLICT (zone_id) DO UPDATE SET status = EXCLUDED.status "); $stmt->execute([$acc['id'], $zone['id'], $zone['name'], $zone['status']]); } $results[] = ['email' => $acc['email'], 'zones' => count($zones['result'])]; } } echo json_encode(['success' => true, 'results' => $results]); break; } exit; } ?>
Configurer les API keys et rapatrier les domaines automatiquement
| ID | Password | API Key | Zones | Actions |
|---|