forked from dixcart/CloudFlare-API
-
Notifications
You must be signed in to change notification settings - Fork 0
/
class_cloudflare.php
567 lines (520 loc) · 15.1 KB
/
class_cloudflare.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
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
<?php
/**
* CloudFlare API
*
*
* @author AzzA <azza@broadcasthe.net>
* @copyright omgwtfhax inc. 2013
* @version 1.1
*/
class cloudflare_api
{
//The URL of the API
private static $URL = array(
'USER' => 'https://www.cloudflare.com/api_json.html',
'HOST' => 'https://api.cloudflare.com/host-gw.html'
);
//Service mode values.
private static $MODE_SERVICE = array('A', 'AAAA', 'CNAME');
//Prio values.
private static $PRIO = array('MX', 'SRV');
//Timeout for the API requests in seconds
const TIMEOUT = 5;
//Interval values for Stats
const INTERVAL_365_DAYS = 10;
const INTERVAL_30_DAYS = 20;
const INTERVAL_7_DAYS = 30;
const INTERVAL_DAY = 40;
const INTERVAL_24_HOURS = 100;
const INTERVAL_12_HOURS = 110;
const INTERVAL_6_HOURS = 120;
//Stores the api key
private $token_key;
private $host_key;
//Stores the email login
private $email;
/**
* Make a new instance of the API client
*/
public function __construct()
{
$parameters = func_get_args();
switch (func_num_args()) {
case 1:
//a host API
$this->host_key = $parameters[0];
break;
case 2:
//a user request
$this->email = $parameters[0];
$this->token_key = $parameters[1];
break;
}
}
public function setEmail($email)
{
$this->email = $email;
}
public function setToken($token_key)
{
$this->token_key = $token_key;
}
/**
* CLIENT API
* Section 3
* Access
*/
/**
* 3.1 - Retrieve Domain Statistics For A Given Time Frame
* This function retrieves the current stats and settings for a particular website.
* It can also be used to get currently settings of values such as the security level.
*/
public function stats($domain, $interval = 20)
{
$data = array(
'a' => 'stats',
'z' => $domain,
'interval' => $interval
);
return $this->http_post($data);
}
/**
* 3.2 - Retrieve A List Of The Domains
* This lists all domains in a CloudFlare account along with other data.
*/
public function zone_load_multi()
{
$data = array(
'a' => 'zone_load_multi'
);
return $this->http_post($data);
}
/**
* 3.3 - Retrieve DNS Records Of A Given Domain
* This function retrieves the current DNS records for a particular website.
*/
public function rec_load_all($domain)
{
$data = array(
'a' => 'rec_load_all',
'z' => $domain
);
return $this->http_post($data);
}
/**
* 3.4 - Checks For Active Zones And Returns Their Corresponding Zids
* This function retrieves domain statistics for a given time frame.
*/
public function zone_check($zones)
{
if (is_array($zones)) {
$zones = implode(',', $zones);
}
$data = array(
'a' => 'zone_check',
'zones' => $zones
);
return $this->http_post($data);
}
/**
* 3.6 - Check The Threat Score For A Given IP
* This function retrieves the current threat score for a given IP.
* Note that scores are on a logarithmic scale, where a higher score indicates a higher threat.
*/
public function threat_score($ip)
{
$data = array(
'a' => 'ip_lkup',
'ip' => $ip
);
return $this->http_post($data);
}
/**
* 3.7 - List All The Current Settings
* This function retrieves all the current settings for a given domain.
*/
public function zone_settings($domain)
{
$data = array(
'a' => 'zone_settings',
'z' => $domain
);
return $this->http_post($data);
}
/**
* Undocumented method
* SEE: https://github.com/vexxhost/CloudFlare-API/pull/3
*/
public function zone_init($zone)
{
$data['a'] = 'zone_init';
$data['z'] = $zone;
return $this->http_post($data);
}
/**
* CLIENT API
* Section 4
* Modify
*/
/**
* 4.1 - Set The Security Level
* This function sets the Basic Security Level to I'M UNDER ATTACK! / HIGH / MEDIUM / LOW / ESSENTIALLY OFF.
* The switches are: (help|high|med|low|eoff).
*/
public function sec_lvl($domain, $mode)
{
$data = array(
'a' => 'sec_lvl',
'z' => $domain,
'v' => $mode
);
return $this->http_post($data);
}
/**
* 4.2 - Set The Cache Level
* This function sets the Caching Level to Aggressive or Basic.
* The switches are: (agg|basic).
*/
public function cache_lvl($domain, $mode)
{
$data = array(
'a' => 'cache_lvl',
'z' => $domain,
'v' => (strtolower($mode) == 'agg') ? 'agg' : 'basic'
);
return $this->http_post($data);
}
/**
* 4.3 - Toggling Development Mode
* This function allows you to toggle Development Mode on or off for a particular domain.
* When Development Mode is on the cache is bypassed.
* Development mode remains on for 3 hours or until when it is toggled back off.
*/
public function devmode($domain, $mode)
{
$data = array(
'a' => 'devmode',
'z' => $domain,
'v' => ($mode == true) ? 1 : 0
);
return $this->http_post($data);
}
/**
* 4.4 - Clear CloudFlare's Cache
* This function will purge CloudFlare of any cached files.
* It may take up to 48 hours for the cache to rebuild and optimum performance to be achieved.
* This function should be used sparingly.
*/
public function fpurge_ts($domain)
{
$data = array(
'a' => 'fpurge_ts',
'z' => $domain,
'v' => 1
);
return $this->http_post($data);
}
/**
* 4.5 - Purge A Single File In CloudFlare's Cache
* This function will purge a single file from CloudFlare's cache.
*/
public function zone_file_purge($domain, $url)
{
$data = array(
'a' => 'zone_file_purge',
'z' => $domain,
'url' => $url
);
return $this->http_post($data);
}
/**
* 4.6 - Update The Snapshot Of Your Site
* This snapshot is used on CloudFlare's challenge page
* This function tells CloudFlare to take a new image of your site.
* Note that this call is rate limited to once per zone per day.
* Also the new image may take up to 1 hour to appear.
*/
public function update_image($zoneid)
{
$data = array(
'a' => 'zone_grab',
'zid' => $zoneid
);
return $this->http_post($data);
}
/**
* 4.7a - Whitelist IPs
* You can add an IP address to your whitelist.
*/
public function wl($ip)
{
$data = array(
'a' => 'wl',
'key' => $ip
);
return $this->http_post($data);
}
/**
* 4.7b - Blacklist IPs
* You can add an IP address to your blacklist.
*/
public function ban($ip)
{
$data = array(
'a' => 'ban',
'key' => $ip
);
return $this->http_post($data);
}
/**
* 4.7c - Unlist IPs
* You can remove an IP address from the whitelist and the blacklist.
*/
public function nul($ip)
{
$data = array(
'a' => 'nul',
'key' => $ip
);
return $this->http_post($data);
}
/**
* 4.8 - Toggle IPv6 Support
* This function toggles IPv6 support.
*/
public function ipv46($domain, $mode)
{
$data = array(
'a' => 'ipv46',
'z' => $domain,
'v' => ($mode == true) ? 1 : 0
);
return $this->http_post($data);
}
/**
* 4.9 - Set Rocket Loader
* This function changes Rocket Loader setting.
*/
public function async($domain, $mode)
{
$data = array(
'a' => 'async',
'z' => $domain,
'v' => $mode
);
return $this->http_post($data);
}
/**
* 4.10 - Set Minification
* This function changes minification settings.
*/
public function minify($domain, $mode)
{
$data = array(
'a' => 'minify',
'z' => $domain,
'v' => $mode
);
return $this->http_post($data);
}
/**
* CLIENT API
* Section 5
* DNS Record Management
*/
/**
* 5.1 - Add A New DNS Record
* This function creates a new DNS record for a zone.
* See http://www.cloudflare.com/docs/client-api.html#s5.1 for documentation.
*/
public function rec_new($domain, $type, $name, $content, $ttl = 1, $mode = 1, $prio = 1, $service = 1, $srvname = 1, $protocol = 1, $weight = 1, $port = 1, $target = 1)
{
$data = array(
'a' => 'rec_new',
'z' => $domain,
'type' => $type,
'name' => $name,
'content' => $content,
'ttl' => $ttl
);
if (in_array($type, self::$MODE_SERVICE))
$data['service_mode'] = ($mode == true) ? 1 : 0;
else if (in_array($type, self::$PRIO)) {
$data['prio'] = $prio;
if ($type == 'SRV') {
$data = array_merge($data, array(
'service' => $service,
'srvname' => $srvname,
'protocol' => $protocol,
'weight' => $weight,
'port' => $port,
'target' => $target
));
}
}
return $this->http_post($data);
}
/**
* 5.2 - Edit A DNS Record
* This function edits a DNS record for a zone.
* See http://www.cloudflare.com/docs/client-api.html#s5.1 for documentation.
*/
public function rec_edit($domain, $type, $id, $name, $content, $ttl = 1, $mode = 1, $prio = 1, $service = 1, $srvname = 1, $protocol = 1, $weight = 1, $port = 1, $target = 1)
{
$data = array(
'a' => 'rec_edit',
'z' => $domain,
'type' => $type,
'id' => $id,
'name' => $name,
'content' => $content,
'ttl' => $ttl
);
if (in_array($type, self::$MODE_SERVICE))
$data['service_mode'] = ($mode == true) ? 1 : 0;
else if (in_array($type, self::$PRIO)) {
$data['prio'] = $prio;
if ($type == 'SRV') {
$data = array_merge($data, array(
'service' => $service,
'srvname' => $srvname,
'protocol' => $protocol,
'weight' => $weight,
'port' => $port,
'target' => $target
));
}
}
return $this->http_post($data);
}
/**
* 5.3 - Delete A DNS Record
* This function deletes a DNS record for a zone.
* $zone = zone
* $id = The DNS Record ID (Available by using the rec_load_all call)
* $type = A|CNAME
*/
public function delete_dns_record($domain, $id)
{
$data = array(
'a' => 'rec_delete',
'z' => $domain,
'id' => $id
);
return $this->http_post($data);
}
/**
* HOST API
* Section 3
* Specific Host Provider Operations
*/
public function user_create($email, $password, $username = '', $id = '')
{
$data = array(
'act' => 'user_create',
'cloudflare_email' => $email,
'cloudflare_pass' => $password,
'cloudflare_username' => $username,
'unique_id' => $id
);
return $this->http_post($data, 'HOST');
}
public function zone_set($key, $zone, $resolve_to, $subdomains)
{
if (is_array($subdomains))
$subdomains = implode(',', $subdomains);
$data = array(
'act' => 'zone_set',
'user_key' => $key,
'zone_name' => $zone,
'resolve_to' => $resolve_to,
'subdomains' => $subdomains
);
return $this->http_post($data, 'HOST');
}
public function user_lookup($email, $isID = false)
{
$data = array(
'act' => 'user_lookup'
);
if ($isID) {
$data['unique_id'] = $email;
} else {
$data['cloudflare_email'] = $email;
}
return $this->http_post($data, 'HOST');
}
public function user_auth($email, $password, $id = '')
{
$data = array(
'act' => 'user_auth',
'cloudflare_email' => $email,
'cloudflare_pass' => $password,
'unique_id' => $id
);
return $this->http_post($data, 'HOST');
}
public function zone_lookup($zone, $user_key)
{
$data = array(
'act' => 'zone_lookup',
'user_key' => $user_key,
'zone_name' => $zone
);
return $this->http_post($data, 'HOST');
}
public function zone_delete($zone, $user_key)
{
$data = array(
'act' => 'zone_delete',
'user_key' => $user_key,
'zone_name' => $zone
);
return $this->http_post($data, 'HOST');
}
public function zone_list()
{
$data = array(
'act' => 'zone_list',
'one_status' => 'ALL',
'sub_status' => 'ALL'
);
return $this->http_post($data, 'HOST');
}
/**
* GLOBAL API CALL
* HTTP POST a specific task with the supplied data
*/
private function http_post($data, $type = 'USER')
{
switch ($type) {
case 'USER':
$data['u'] = $this->email;
$data['tkn'] = $this->token_key;
break;
case 'HOST':
$data['host_key'] = $this->host_key;
break;
}
$ch = curl_init();
curl_setopt($ch, CURLOPT_VERBOSE, 0);
curl_setopt($ch, CURLOPT_FORBID_REUSE, true);
curl_setopt($ch, CURLOPT_URL, self::$URL[$type]);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_TIMEOUT, self::TIMEOUT);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
$http_result = curl_exec($ch);
$error = curl_error($ch);
$http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
if ($http_code != 200) {
return array(
'error' => $error
);
} else {
return json_decode($http_result);
}
}
}