-
Notifications
You must be signed in to change notification settings - Fork 264
Expand file tree
/
Copy pathDNS.php
More file actions
177 lines (149 loc) · 4.83 KB
/
DNS.php
File metadata and controls
177 lines (149 loc) · 4.83 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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
<?php
/**
* Created by PhpStorm.
* User: junade
* Date: 09/06/2017
* Time: 15:14
*/
namespace Cloudflare\API\Endpoints;
use Cloudflare\API\Adapter\Adapter;
use Cloudflare\API\Traits\BodyAccessorTrait;
class DNS implements API
{
use BodyAccessorTrait;
private $adapter;
public function __construct(Adapter $adapter)
{
$this->adapter = $adapter;
}
/**
* @SuppressWarnings(PHPMD.BooleanArgumentFlag)
*
* @param string $zoneID
* @param string $type
* @param string $name
* @param string $content
* @param int $ttl
* @param bool $proxied
* @param string $priority
* @param array $data
* @return bool
*/
public function addRecord(
string $zoneID,
string $type,
string $name,
string $content,
int $ttl = 0,
bool $proxied = true,
string $priority = '',
array $data = []
): bool {
$options = [
'type' => $type,
'name' => $name,
'content' => $content,
'proxied' => $proxied
];
if ($ttl > 0) {
$options['ttl'] = $ttl;
}
if (is_numeric($priority)) {
$options['priority'] = (int)$priority;
}
if (!empty($data)) {
$options['data'] = $data;
}
$user = $this->adapter->post('zones/' . $zoneID . '/dns_records', $options);
$this->body = json_decode($user->getBody());
if (isset($this->body->result->id)) {
return true;
}
return false;
}
/**
* Get a List of Records that matches the specified filters
* @param string $zoneID
* @param string $type
* @param string $name Only one name is allowed as of 2025-02-21 | For filtering, use parameter $nameFilterMode
* @param string $content Only one content value is allowed as of 2025-02-21 | For filtering, use parameter $contentFilterMode
* @param int $page
* @param int $perPage
* @param string $order
* @param string $direction
* @param string $match
* @param string $nameFilterMode Can be either of: contains | starts_with | ends_with
* @param string $contentFilterMode Can be either of: contains | starts_with | ends_with
* @return \stdClass
*/
public function listRecords(
string $zoneID,
string $type = '',
string $name = '',
string $content = '',
int $page = 1,
int $perPage = 20,
string $order = '',
string $direction = '',
string $match = 'all',
string $nameFilterMode = '',
string $contentFilterMode = ''
): \stdClass {
$query = [
'page' => $page,
'per_page' => $perPage,
'match' => $match
];
if (!empty($type)) {
$query['type'] = $type;
}
if (!empty($name) && !empty($nameFilterMode)) {
$query["name.$nameFilterMode"] = $name;
} elseif (!empty($name)) {
$query['name'] = $name;
}
if (!empty($content) && !empty($contentFilterMode)) {
$query["content.$contentFilterMode"] = $content;
} elseif (!empty($content)) {
$query['content'] = $content;
}
if (!empty($order)) {
$query['order'] = $order;
}
if (!empty($direction)) {
$query['direction'] = $direction;
}
$user = $this->adapter->get('zones/' . $zoneID . '/dns_records', $query);
$this->body = json_decode($user->getBody());
return (object)['result' => $this->body->result, 'result_info' => $this->body->result_info];
}
public function getRecordDetails(string $zoneID, string $recordID): \stdClass
{
$user = $this->adapter->get('zones/' . $zoneID . '/dns_records/' . $recordID);
$this->body = json_decode($user->getBody());
return $this->body->result;
}
public function getRecordID(string $zoneID, string $type = '', string $name = ''): string
{
$records = $this->listRecords($zoneID, $type, $name);
if (isset($records->result[0]->id)) {
return $records->result[0]->id;
}
return false;
}
public function updateRecordDetails(string $zoneID, string $recordID, array $details): \stdClass
{
$response = $this->adapter->put('zones/' . $zoneID . '/dns_records/' . $recordID, $details);
$this->body = json_decode($response->getBody());
return $this->body;
}
public function deleteRecord(string $zoneID, string $recordID): bool
{
$user = $this->adapter->delete('zones/' . $zoneID . '/dns_records/' . $recordID);
$this->body = json_decode($user->getBody());
if (isset($this->body->result->id)) {
return true;
}
return false;
}
}