-
Notifications
You must be signed in to change notification settings - Fork 5
/
class.rage4.php
442 lines (377 loc) · 16.2 KB
/
class.rage4.php
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
<?php
/*
Rage4 DNS PHP5 class
This is a PHP5 wrapper to easily integrate Rage4 DNS service
(www.rage4.com) easily. There is no official PHP SDK at the
moment so this class can help fill the gap in the meantime.
Note: Number of API calls is not limited at the moment hence
no mechanism added to track/limit the same.
------------------------------------------------------------
Author : Asim Zeeshan (www.asim.pk)
Email : [email protected]
Twitter : @asimzeeshan
Usage instruction & download : https://github.com/asimzeeshan/php-rage4-dns
------------------------------------------------------------
*/
class rage4 {
private $username = "";
private $password = "";
private $valid_record_types = array(1 => "NS", 2 => "A", 3 => "AAAA", 4 => "CNAME", 5 => "MX", 6 => "TXT", 7 => "SRV", 8 => "PTR");
/*
THE CONSTRUCTOR
All API calls uses BASIC authentication using user's
- email address as username
- Account Key as password
Note: Account Key is available in User Profile section of
Rage4 DNS control panel.
------------------------------------------------------------
Parameters: $user and $pass
*/
public function __construct($user, $pass) {
if (empty($user) || empty($pass)){
$this->throwError("Username and Password cannot be empty!");
} else if (!empty($user) && !empty($pass)){
$this->username = $this->cleanInput($user);
$this->password = $this->cleanInput($pass);
}
}
// Internal method
// TODO: Instead of printing, I need to "return" back the error messages
private function throwError($err) {
echo "<b><font color='#FF0000'>Error:</font></b> ".$err;
exit;
}
// Utility functions
private function cleanInput($i) {
return trim($i);
}
// Internal method to debug code, I will leave it here for now
public function debug() {
echo "<br />";
echo "Username: ".$this->username."<br />";
echo "Password: ".$this->password."<br />";
}
// Internal method to debug code, I will leave it here for now
private function dump($obj) {
echo "<br /><pre>";
print_r($obj);
echo "</pre>";
}
/*
Core function that queries the API and renders results
------------------------------------------------------------
Parameters: $method (it includes the method and/or querystring)
*/
private function doQuery($method) {
//echo "Trying ... https://secure.rage4.com/rapi/$method <br />";
$ch = curl_init("https://secure.rage4.com/rapi/".$method);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HEADER, FALSE);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_USERPWD, $this->username.":".$this->password);
//echo $this->username.":".$this->password."<br />";
//echo "HTTPCODE=".$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE)."<br />";
$result = curl_exec($ch);
//$this->dump($result);
//exit;
return $result;
}
/*
GET DOMAINS
Get all domain names in your Rage4.com account
------------------------------------------------------------
Parameters: None
*/
public function getDomains() {
$response = $this->doQuery("getdomains");
$response = json_decode($response, true);
if (isset($response['error']) && $response['error']!="") {
return $response['error'];
} else {
return $response;
}
}
/*
CREATE A DOMAIN NAME
Create a new domain name (zone) in your Rage4.com account
------------------------------------------------------------
Parameters: (all required)
$name (string) = domain name
$email (string) = regular email address of the domain / NOC manager
*/
public function createDomain($domain_name, $email) {
if (empty($domain_name) || empty($email)) {
$this->throwError("(method: createDomain) Domain name and Email address is required");
}
$response = $this->doQuery("createregulardomain/?name=$domain_name&email=$email");
$response = json_decode($response, true);
if (isset($response['error']) && $response['error']!="") {
return $response['error'];
} else {
return $response;
}
}
/*
CREATE A REVERSE IPv4 DOMAIN
Create a reverse IPv4 domain name (zone) in your Rage4.com account
------------------------------------------------------------
Parameters: (all required)
$name (string) = domain name (for reverse domains: ip6.arpa or in-addr.arpa)
$email (string) = owner's email
$subnet (int) = valid subnet mask
*/
public function createReverseDomain4($domain_name, $email, $subnet) {
if (empty($domain_name) || empty($email) || empty($subnet)) {
$this->throwError("(method: createReverseDomain4) Domain name, Email address and subnet is required");
}
$response = $this->doQuery("createreversedomain4/?name=$domain_name&email=$email&subnet=$subnet");
$response = json_decode($response, true);
if (isset($response['error']) && $response['error']!="") {
return $response['error'];
} else {
return $response;
}
}
/*
CREATE A REVERSE IPv6 DOMAIN
Create a reverse IPv6 domain name (zone) in your Rage4.com account
------------------------------------------------------------
Parameters: (all required)
$name (string) = domain name (for reverse domains: ip6.arpa or in-addr.arpa)
$email (string) = owner's email
$subnet (int) = valid subnet mask
*/
public function createReverseDomain6($domain_name, $email, $subnet) {
if (empty($domain_name) || empty($email) || empty($subnet)) {
$this->throwError("(method: createReverseDomain6) Domain name, Email address and subnet is required");
}
$response = $this->doQuery("createreversedomain6/?name=$domain_name&email=$email&subnet=$subnet");
$response = json_decode($response, true);
if (isset($response['error']) && $response['error']!="") {
return $response['error'];
} else {
return $response;
}
}
/*
DELETE A DOMAIN NAME
Delete a new domain name using its unique identifier in the
system. To know the unqiue identifier, GetDomains() must be
called first
------------------------------------------------------------
Parameters: (all required)
$domain_id (int) = domain id
*/
public function deleteDomain($domain_id) {
// explicitly typecast into integer
$domain_id = (int)$domain_id;
if (empty($domain_id)) {
$this->throwError("(method: deleteDomain) Domain id must be a number");
}
$response = $this->doQuery("deletedomain/$domain_id");
$response = json_decode($response, true);
if (isset($response['error']) && $response['error']!="") {
return $response['error'];
} else {
return (bool)$response['status'];
}
}
/*
IMPORT A DOMAIN NAME
Importing a domain name including zone data into the system
Note! You need to allow AXFR transfers
Note! Only regular domains are supported
------------------------------------------------------------
Parameters: (all required)
$domain (string) = domain
*/
public function importDomain($domain) {
// explicitly typecast into string
$domain = (string)$domain;
if (empty($domain)) {
$this->throwError("(method: importDomain) Domain must be a valid string");
}
$response = $this->doQuery("importdomain/?name=$domain");
$response = json_decode($response, true);
if (isset($response['error']) && $response['error']!="") {
return $response['error'];
} else {
return (bool)$response['status'];
}
}
/*
GET RECORDS OF A DOMAIN NAME
Get all records (A, AAAA etc) of a particular domain name
------------------------------------------------------------
Parameters: (all required)
$domain_id (int) = domain id
*/
public function getRecords($domain_id) {
// explicitly typecast into integer
$domain_id = (int)$domain_id;
if (empty($domain_id)) {
$this->throwError("(method: getRecords) Domain id must be a number");
}
$response = $this->doQuery("getrecords/$domain_id");
$response = json_decode($response, true);
//$this->dump($response);
if (isset($response['error']) && $response['error']!="") {
return $response['error'];
} else {
return $response;
}
}
/*
CREATE NEW RECORD
Create new record for a specific domain name
------------------------------------------------------------
Parameters: (all required except where mentioned)
$domain_id (int) = domain id
$name (string) = name of the record
$content (string) = content of the record
$type (string) = type, should be one of the following
1 = NS
2 = A
3 = AAAA
4 = CNAME
5 = MX
6 = TXT
7 = SRV
8 = PTR
$priority (int) = priority of the record being created [OPTIONAL!!]
$failover (bool) = Failure support? Yes/No
$failovercontent (string) = Failure IP / content
$ttl (int) = TTL
*/
public function createRecord($domain_id, $name, $content, $type="TXT", $priority="", $failover="", $failovercontent="", $ttl=3600) {
// explicitly typecast into required types
$domain_id = (int)$domain_id;
$name = (string)$this->cleanInput($name);
$content = (string)$this->cleanInput($content);
$type = $this->cleanInput($type);
$priority = $this->cleanInput($priority);
//$failover = (bool)$this->cleanInput($failover);
$failovercontent = (string)$this->cleanInput($failovercontent);
$ttl = (int)$ttl;
if (empty($domain_id)) {
$this->throwError("(method: createRecord) Domain id must be a number");
}
if (empty($name)) {
$this->throwError("(method: createRecord) Name cannot be empty");
}
if (empty($content)) {
$this->throwError("(method: createRecord) Content cannot be empty");
}
if ($failover=="") {
$failover = "false";
} else {
$failover = "true";
}
$query_string = "$domain_id?";
if (!empty($name)) {
$query_string .= "name=".$name;
}
if (!empty($content)) {
$query_string .= "&content=".$content;
}
if (in_array($type, $this->valid_record_types)) {
$type_id = array_search($type, $this->valid_record_types);
$query_string .= "&type=".$type_id;
} else {
$this->throwError("(method: createRecord) Type must be a valid option from the following NS, MX, A, AAAA, CNAME, TXT, SRV");
}
if (!empty($priority)) {
$priority = (int)$priority;
}
$query_string .= "&priority=$priority&failover=$failover&failovercontent=$failovercontent&ttl=$ttl";
$response = $this->doQuery("createrecord/$query_string");
$response = json_decode($response, true);
if (isset($response['error']) && $response['error']!="") {
return $response['error'];
} else if (isset($response['status']) && $response['status']!="") {
return "Record added with id ".$response['id'];
} else {
return $response;
}
}
/*
UPDATE EXISTING RECORD
Update an existing record for a specific domain name (no need to mention domain name)
------------------------------------------------------------
Parameters: (all required except where mentioned)
$record_id (int) = record id that you wish to update
$name (string) = name of the record
$content (string) = content of the record
$priority (int) = priority of the record being created [OPTIONAL!!]
$failover (bool) = Failure support? Yes/No
$failovercontent (string) = Failure IP / content
$ttl (int) = TTL
*/
public function updateRecord($record_id, $name, $content, $priority="", $failover="", $failovercontent="", $ttl=3600) {
// explicitly typecast into required types
$record_id = (int)$record_id;
$name = (string)$this->cleanInput($name);
$content = (string)$this->cleanInput($content);
$priority = $this->cleanInput($priority);
//$failover = (bool)$this->cleanInput($failover);
$failovercontent = (string)$this->cleanInput($failovercontent);
$ttl = (int)$ttl;
if (empty($record_id)) {
$this->throwError("(method: updateRecord) Record id must be a number");
}
if (empty($name)) {
$this->throwError("(method: updateRecord) Name cannot be empty");
}
if (empty($content)) {
$this->throwError("(method: updateRecord) Content cannot be empty");
}
if ($failover=="") {
$failover = "false";
} else {
$failover = "true";
}
$query_string = "$record_id?";
if (!empty($name)) {
$query_string .= "name=".$name;
}
if (!empty($content)) {
$query_string .= "&content=".$content;
}
if (!empty($priority)) {
$priority = (int)$priority;
}
$query_string .= "&priority=$priority&failover=$failover&failovercontent=$failovercontent&ttl=$ttl";
$response = $this->doQuery("updaterecord/$query_string");
$response = json_decode($response, true);
if (isset($response['error']) && $response['error']!="") {
return $response['error'];
} else if (isset($response['status']) && $response['status']!="") {
return "Record updated with id ".$response['id'];
} else {
return $response;
}
}
/*
DELETE A RECORD
Delete a record in an existing domain name (zone) in your
account
------------------------------------------------------------
Parameters: (all required)
$record_id (int) = record id
*/
public function deleteRecord($record_id) {
// explicitly typecast into integer
$record_id = (int)$record_id;
if (empty($record_id)) {
$this->throwError("(method: deleteRecord) Record id must be a number");
}
$response = $this->doQuery("deleterecord/$record_id");
$response = json_decode($response, true);
if (isset($response['error']) && $response['error']!="") {
return $response['error'];
} else {
return $response['status'];
}
}
}