-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi_helper.php
More file actions
126 lines (104 loc) · 3.3 KB
/
Copy pathapi_helper.php
File metadata and controls
126 lines (104 loc) · 3.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
<?php
session_start();
// Check if user is logged in
if (!isset($_SESSION['logged_in'])) {
http_response_code(403);
echo json_encode(['success' => false, 'error' => 'Not logged in']);
exit;
}
// Load configuration
function loadConfig() {
if (!file_exists('config.json')) {
return ['api_token' => ''];
}
return json_decode(file_get_contents('config.json'), true);
}
$config = loadConfig();
if (empty($config['api_token'])) {
echo json_encode(['success' => false, 'error' => 'No API token configured']);
exit;
}
$action = $_GET['action'] ?? '';
switch ($action) {
case 'get_zones':
getZones($config['api_token']);
break;
case 'get_records':
$zoneId = $_GET['zone_id'] ?? '';
if (empty($zoneId)) {
echo json_encode(['success' => false, 'error' => 'Zone ID missing']);
exit;
}
getRecords($config['api_token'], $zoneId);
break;
default:
echo json_encode(['success' => false, 'error' => 'Unknown action']);
break;
}
function getZones($apiToken) {
$url = "https://api.cloudflare.com/client/v4/zones";
$options = [
"http" => [
"header" => [
"Authorization: Bearer $apiToken",
"Content-Type: application/json"
],
"method" => "GET"
]
];
$context = stream_context_create($options);
$result = @file_get_contents($url, false, $context);
if (!$result) {
echo json_encode(['success' => false, 'error' => 'Network error while fetching zones']);
return;
}
$response = json_decode($result, true);
if (!$response['success']) {
$errors = isset($response['errors']) ? array_column($response['errors'], 'message') : ['Unknown error'];
echo json_encode(['success' => false, 'error' => implode(', ', $errors)]);
return;
}
// Sort zones by name
$zones = $response['result'];
usort($zones, function($a, $b) {
return strcmp($a['name'], $b['name']);
});
echo json_encode([
'success' => true,
'zones' => $zones
]);
}
function getRecords($apiToken, $zoneId) {
$url = "https://api.cloudflare.com/client/v4/zones/$zoneId/dns_records?type=A";
$options = [
"http" => [
"header" => [
"Authorization: Bearer $apiToken",
"Content-Type: application/json"
],
"method" => "GET"
]
];
$context = stream_context_create($options);
$result = @file_get_contents($url, false, $context);
if (!$result) {
echo json_encode(['success' => false, 'error' => 'Network error while fetching records']);
return;
}
$response = json_decode($result, true);
if (!$response['success']) {
$errors = isset($response['errors']) ? array_column($response['errors'], 'message') : ['Unknown error'];
echo json_encode(['success' => false, 'error' => implode(', ', $errors)]);
return;
}
// Sort records by name
$records = $response['result'];
usort($records, function($a, $b) {
return strcmp($a['name'], $b['name']);
});
echo json_encode([
'success' => true,
'records' => $records
]);
}
?>