1 | <?php |
||||||
2 | |||||||
3 | include 'CloudflareConnection.php'; |
||||||
4 | |||||||
5 | class cloudflare |
||||||
6 | { |
||||||
7 | public $Error; |
||||||
8 | public $Warning; |
||||||
9 | public $Success; |
||||||
10 | |||||||
11 | public function __construct() |
||||||
12 | { |
||||||
13 | $this->Error = []; |
||||||
14 | $this->Warning = []; |
||||||
15 | $this->Success = []; |
||||||
16 | |||||||
17 | $this->loadLanguageArray(LANGUAGE_CODE); |
||||||
18 | } |
||||||
19 | |||||||
20 | /** Settings for the integration, these are shown when you add/edit a DNS integration |
||||||
21 | * |
||||||
22 | * @return string |
||||||
23 | */ |
||||||
24 | public function getPlatformSettings() |
||||||
25 | { |
||||||
26 | // example of html for the integration settings |
||||||
27 | $html = ''; |
||||||
28 | $html .= '<strong class="title">'.__('username', 'cloudflare').'</strong>'; |
||||||
29 | $html .= '<input type="text" class="text1 size1" name="module[dnsmanagement][Settings][username]" value="'.((isset($this->Settings->username)) ? htmlspecialchars($this->Settings->username) : '').'"><br><br>'; |
||||||
30 | $html .= '<strong class="title">'.__('api_key', 'cloudflare').'</strong>'; |
||||||
31 | $html .= '<input type="text" class="text1 size1" name="module[dnsmanagement][Settings][api_key]" value="'.((isset($this->Settings->api_key)) ? htmlspecialchars($this->Settings->api_key) : '').'"><br><br>'; |
||||||
32 | $html .= '<strong class="title">'.__('account_id', 'cloudflare').'</strong>'; |
||||||
33 | $html .= '<input type="text" class="text1 size1" name="module[dnsmanagement][Settings][account_id]" value="'.((isset($this->Settings->account_id)) ? htmlspecialchars($this->Settings->account_id) : '').'"><br><br>'; |
||||||
34 | $html .= '<strong class="title">'.__('allow_zone_removal', 'cloudflare').'</strong>'; |
||||||
35 | $html .= '<select class="text1 size4" name="module[dnsmanagement][Settings][allow_zone_removal]"> |
||||||
36 | <option value="no" '.($this->Settings->allow_zone_removal === 'no' ? 'selected' : '').'>'.__('no', 'cloudflare').'</option> |
||||||
37 | <option value="yes" '.($this->Settings->allow_zone_removal === 'yes' ? 'selected' : '').'>'.__('yes', 'cloudflare').'</option> |
||||||
38 | </select/><br><br>'; |
||||||
39 | |||||||
40 | return $html; |
||||||
41 | } |
||||||
42 | |||||||
43 | /** Get the DNS templates from the DNS platform |
||||||
44 | * |
||||||
45 | * @return array|bool |
||||||
46 | */ |
||||||
47 | public function getDNSTemplates() |
||||||
48 | { |
||||||
49 | // if the DNS platform does not support DNS templates by it self, just do a: |
||||||
50 | return false; |
||||||
51 | // the WeFact Hosting user then has to create the DNS templates with each DNS record in WeFact Hosting |
||||||
52 | } |
||||||
53 | |||||||
54 | /** This function is called before a add/edit/show of a DNS integration |
||||||
55 | * For example, you can use this to encrypt a password. |
||||||
56 | * |
||||||
57 | * @param $edit_or_show |
||||||
58 | * @param $settings |
||||||
59 | * |
||||||
60 | * @return mixed |
||||||
61 | */ |
||||||
62 | public function processSettings($edit_or_show, $settings) |
||||||
0 ignored issues
–
show
|
|||||||
63 | { |
||||||
64 | return $settings; |
||||||
65 | } |
||||||
66 | |||||||
67 | /** Create a DNS zone with DNS records on the DNS platform |
||||||
68 | * |
||||||
69 | * @param $domain |
||||||
70 | * @param $dns_zone |
||||||
71 | * |
||||||
72 | * @return bool |
||||||
73 | */ |
||||||
74 | public function createDNSZone($domain, $dns_zone) |
||||||
75 | { |
||||||
76 | $cloudflare_connection = new CloudflareConnection($this->Settings->username, $this->Settings->api_key); |
||||||
77 | |||||||
78 | $zone = $cloudflare_connection->createZone($domain, $this->Settings->account_id); |
||||||
79 | |||||||
80 | if ($zone['success'] === true) { |
||||||
81 | return $this->saveDNSZone($domain, $dns_zone); |
||||||
82 | } |
||||||
83 | |||||||
84 | return false; |
||||||
85 | } |
||||||
86 | |||||||
87 | /** This function will be called when a domain register, transfer or nameserver change has failed |
||||||
88 | * It can be used to revert any data that is set by the createDNSZone function (eg the creation of a DNS zone). |
||||||
89 | * |
||||||
90 | * @param $domain |
||||||
91 | * @param $create_dns_zone_data |
||||||
92 | * |
||||||
93 | * @return bool |
||||||
94 | */ |
||||||
95 | public function undoCreateDNSZone($domain, $create_dns_zone_data) |
||||||
0 ignored issues
–
show
The parameter
$domain is not used and could be removed.
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
This check looks for parameters that have been defined for a function or method, but which are not used in the method body. ![]() The parameter
$create_dns_zone_data is not used and could be removed.
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
This check looks for parameters that have been defined for a function or method, but which are not used in the method body. ![]() |
|||||||
96 | { |
||||||
97 | return false; |
||||||
98 | } |
||||||
99 | |||||||
100 | /** Retrieve the DNS zone with its DNS records from the DNS platform |
||||||
101 | * |
||||||
102 | * @param $domain |
||||||
103 | * |
||||||
104 | * @return array|bool |
||||||
105 | */ |
||||||
106 | public function getDNSZone($domain) |
||||||
107 | { |
||||||
108 | $cloudflare_connection = new CloudflareConnection($this->Settings->username, $this->Settings->api_key); |
||||||
109 | $zones = $cloudflare_connection->getZones(['name' => $domain]); |
||||||
110 | |||||||
111 | if ($zones['success'] === true && count($zones['result']) === 1) { |
||||||
112 | $zone = $zones['result'][0]; |
||||||
113 | $dns_records = $cloudflare_connection->getDnsRecordsForZone($zone['id'], []); |
||||||
114 | |||||||
115 | if ($dns_records['success'] !== true) { |
||||||
116 | return false; |
||||||
117 | } |
||||||
118 | |||||||
119 | $dns_zone = []; |
||||||
120 | $i = 0; |
||||||
121 | foreach ($dns_records['result'] as $record) { |
||||||
122 | // if the record is not supported, it should be marked as readonly |
||||||
123 | if (!in_array(strtoupper($record['type']), $this->SupportedRecordTypes, true)) { |
||||||
124 | $record_type = 'records_readonly'; |
||||||
125 | } else { |
||||||
126 | $record_type = 'records'; |
||||||
127 | } |
||||||
128 | |||||||
129 | $dns_zone[$record_type][$i]['id'] = $record['id']; |
||||||
130 | $dns_zone[$record_type][$i]['name'] = $record['name']; |
||||||
131 | $dns_zone[$record_type][$i]['type'] = $record['type']; |
||||||
132 | $dns_zone[$record_type][$i]['value'] = $record['content']; |
||||||
133 | $dns_zone[$record_type][$i]['priority'] = $record['priority'] ?? ''; |
||||||
134 | $dns_zone[$record_type][$i]['ttl'] = $record['ttl']; |
||||||
135 | $i++; |
||||||
136 | } |
||||||
137 | |||||||
138 | return $dns_zone; |
||||||
139 | } |
||||||
140 | |||||||
141 | return false; |
||||||
142 | } |
||||||
143 | |||||||
144 | /** Edit the DNS zone at the DNS platform |
||||||
145 | * |
||||||
146 | * @param $domain |
||||||
147 | * @param $dns_zone |
||||||
148 | * |
||||||
149 | * @return bool |
||||||
150 | */ |
||||||
151 | public function saveDNSZone($domain, $dns_zone) |
||||||
152 | { |
||||||
153 | if (isset($dns_zone['records']) && count($dns_zone['records']) > 0) { |
||||||
154 | $cloudflare_connection = new CloudflareConnection($this->Settings->username, $this->Settings->api_key); |
||||||
155 | $zones = $cloudflare_connection->getZones(['name' => $domain]); |
||||||
156 | |||||||
157 | if ($zones['success'] === true && count($zones['result']) === 1) { |
||||||
158 | $zone = $zones['result'][0]; |
||||||
159 | $dns_records = $cloudflare_connection->getDnsRecordsForZone($zone['id'], []); |
||||||
160 | |||||||
161 | if ($dns_records['success'] !== true) { |
||||||
162 | return false; |
||||||
163 | } |
||||||
164 | |||||||
165 | $cf_record_ids = array_column($dns_records['result'], 'id'); |
||||||
166 | $updated_record_ids = array_column($dns_zone['records'], 'id'); |
||||||
167 | $deleted_record_ids = array_diff($cf_record_ids, $updated_record_ids); |
||||||
168 | |||||||
169 | foreach ($deleted_record_ids as $id) { |
||||||
170 | $cloudflare_connection->deleteDnsRecord($zone['id'], $id); |
||||||
171 | } |
||||||
172 | |||||||
173 | foreach ($dns_zone['records'] as $record) { |
||||||
174 | $cf_record = []; |
||||||
175 | $cf_record['name'] = $record['name']; |
||||||
176 | $cf_record['type'] = $record['type']; |
||||||
177 | $cf_record['content'] = $record['value']; |
||||||
178 | $cf_record['ttl'] = $record['ttl']; |
||||||
179 | $cf_record['priority'] = trim($record['priority']) === '' ? 1 : $record['priority']; |
||||||
180 | |||||||
181 | if (isset($record['id'])) { |
||||||
182 | $cloudflare_connection->updateDnsRecord($zone['id'], $record['id'], $cf_record); |
||||||
183 | } else { |
||||||
184 | $cloudflare_connection->createDnsRecord($zone['id'], $cf_record); |
||||||
185 | } |
||||||
186 | } |
||||||
187 | } |
||||||
188 | } |
||||||
189 | |||||||
190 | return true; |
||||||
191 | } |
||||||
192 | |||||||
193 | /** This function is called when a domain is removed directly or by a termination procedure |
||||||
194 | * |
||||||
195 | * @param $domain |
||||||
196 | * |
||||||
197 | * @return bool |
||||||
198 | */ |
||||||
199 | public function removeDNSZone($domain) |
||||||
200 | { |
||||||
201 | if ($this->Settings->allow_zone_removal === 'yes') { |
||||||
202 | $cloudflare_connection = new CloudflareConnection($this->Settings->username, $this->Settings->api_key); |
||||||
203 | |||||||
204 | $zones = $cloudflare_connection->getZones(['name' => $domain]); |
||||||
205 | |||||||
206 | if ($zones['success'] === true && count($zones['result']) === 1) { |
||||||
207 | $zone = $zones['result'][0]; |
||||||
208 | $zone_delete_result = $cloudflare_connection->deleteZone($zone['id']); |
||||||
209 | |||||||
210 | return $zone_delete_result['success']; |
||||||
211 | } |
||||||
212 | |||||||
213 | return false; |
||||||
214 | } |
||||||
215 | |||||||
216 | return true; |
||||||
217 | } |
||||||
218 | |||||||
219 | /** |
||||||
220 | * @param $language_code |
||||||
221 | */ |
||||||
222 | public function loadLanguageArray($language_code) |
||||||
223 | { |
||||||
224 | $_LANG = []; |
||||||
225 | |||||||
226 | switch ($language_code) { |
||||||
227 | case 'nl_NL': |
||||||
228 | $_LANG['dns templates could not be retrieved'] = 'De DNS templates konden niet worden opgehaald van het DNS platform of er zijn er geen aanwezig'; |
||||||
229 | $_LANG['username'] = 'Gebruikersnaam'; |
||||||
230 | $_LANG['api_key'] = 'API Key'; |
||||||
231 | $_LANG['account_id'] = 'Account ID'; |
||||||
232 | $_LANG['allow_zone_removal'] = 'DNS zone verwijderen bij verwijderen domein'; |
||||||
233 | $_LANG['yes'] = 'Ja'; |
||||||
234 | $_LANG['no'] = 'Nee'; |
||||||
235 | break; |
||||||
236 | |||||||
237 | default: // In case of other language, use English |
||||||
238 | $_LANG['dns templates could not be retrieved'] = 'DNS template could not be retrieved from the DNS platform or there were no DNS templates'; |
||||||
239 | $_LANG['username'] = 'Username'; |
||||||
240 | $_LANG['api_key'] = 'API Key'; |
||||||
241 | $_LANG['account_id'] = 'Account ID'; |
||||||
242 | $_LANG['allow_zone_removal'] = 'Remove DNS zone at domain deletion'; |
||||||
243 | $_LANG['yes'] = 'Ja'; |
||||||
244 | $_LANG['no'] = 'Nee'; |
||||||
245 | break; |
||||||
246 | } |
||||||
247 | |||||||
248 | // Save to global array |
||||||
249 | global $_module_language_array; |
||||||
250 | $_module_language_array['cloudflare'] = $_LANG; |
||||||
251 | } |
||||||
252 | |||||||
253 | /** |
||||||
254 | * Use this function to prefix all errors messages with your VPS platform. |
||||||
255 | * |
||||||
256 | * @param string $message The error message |
||||||
257 | * |
||||||
258 | * @return bool Always false |
||||||
259 | */ |
||||||
260 | private function __parseError($message) |
||||||
261 | { |
||||||
262 | $this->Error[] = 'Cloudflare: '.$message; |
||||||
263 | |||||||
264 | return false; |
||||||
265 | } |
||||||
266 | |||||||
267 | /** This function is used to check if the login credentials for the DNS platform are correct |
||||||
268 | * |
||||||
269 | * @return bool |
||||||
270 | */ |
||||||
271 | public function validateLogin() |
||||||
272 | { |
||||||
273 | $cloudflare_connection = new CloudflareConnection($this->Settings->username, $this->Settings->api_key); |
||||||
274 | |||||||
275 | return $cloudflare_connection->validateLogin(); |
||||||
276 | } |
||||||
277 | } |
||||||
278 |
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.