-
Notifications
You must be signed in to change notification settings - Fork 59
/
class_cloudflare.php
347 lines (314 loc) · 11.8 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
<?php
/**
* CloudFlare Client API
*
*
* @author AzzA <[email protected]>
* @copyright omgwtfhax inc. 2011
* @version 1.0
*/
class cloudflare_api {
//The URL of the API
private $URL = array('USER' => 'https://www.cloudflare.com/api_json.html', 'HOST' => 'https://api.cloudflare.com/host-gw.html');
//Timeout for the API requests in seconds
const TIMEOUT = 5;
//Stores the api key
private $token_key;
private $host_key;
//Stores the email login
private $email;
//If you want to output an array instead of an object
private $out_array = false;
//Data to post
private $data = array();
/**
* 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;
}
//Set to output an associative array instead of an object
public function useArray($value = true) {
$this->out_array = $value;
}
/**
* Stats about the zone
*
* @param string $domain - zone name (example.com)
* @param int $interval - 10 = 1 year, 20 = 30 day, 30 = 7 day, 40 = 24 hours, 100 = < 24 hours, 110 = <12 hours, 120 = <6 hours
* @return array - associative array
*/
public function stats($domain, $interval = 20) {
$data['a'] = "stats";
$data['z'] = $domain;
$data['interval'] = $interval;
return $this->http_post($data);
}
/**
* 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.
*
* @param bool $mode - true for on, false for off
* @param string $domain - zone to applt to (e.g. example.com)
* @return array - associative array
*/
public function devmode($mode, $domain) {
$data['a'] = "devmode";
$data['z'] = $domain;
$data['v'] = ($mode == true) ? 1 : 0;
return $this->http_post($data);
}
/**
* 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 so this function should be used sparingly.
*
* @param bool $mode - whether to actually purge the cache //TODO: is this necessary?
* @param type $domain - zone (example.com)
* @return array - associative array
*/
public function purge_cache($mode, $domain) {
$data['a'] = "fpurge_ts";
$data['z'] = $domain;
$data['v'] = ($mode == true) ? 1 : 0;
return $this->http_post($data);
}
/**
* TODO: Docs say this should be a GET and returns "OK" not JSON.
* Whitelist an IP on your whole account
*
* @param string $ip - the IP address you want to whitelist
* @return array - associative array
*/
public function whitelist_ip($ip) {
$data['a'] = "wl";
$data['key'] = $ip;
return $this->http_post($data);
}
/**
* TODO: Docs say this should be a GET and returns "OK" not JSON.
* Ban an IP on your whole account
*
* @param string $ip - the IP address you want to whitelist
* @return array - associative array
*/
public function blacklist_ip($ip) {
$data['a'] = "ban";
$data['key'] = $ip;
return $this->http_post($data);
}
/**
* This function sets the Caching Level to Aggressive or Basic.
*
* @param string $mode - "agg" or "basic"
* @param string $domain - zone (example.com)
* @return array - associative array
*/
public function set_cache_lvl($mode, $domain) {
$data['a'] = "cache_lvl";
$data['z'] = $domain;
$data['v'] = ($mode == 'agg') ? 'agg' : 'basic';
return $this->http_post($data);
}
/**
* This function sets the Basic Security Level to HIGH / MEDIUM / LOW / ESSENTIALLY OFF.
*
* @param string $mode - one of (high|med|low|eoff)
* @param string $domain - zone (example.com)
* @return array - associative array
*/
public function set_security_lvl($mode, $domain) {
$data['a'] = "sec_lvl";
$data['z'] = $domain;
$data['v'] = $mode;
return $this->http_post($data);
}
/**
* Returns a list of IP addresses which hit your site classified by type.
*
* @param int $zoneid - ID of the zone you would like to check.
* @param int $hours - Number of hours to go back. Default is 24, max is 48.
* @param char $class - Restrict the result set to a given class. Currently r|s|t, for regular, crawler, threat resp.
* @param type $geo - Optional token. Add to add longitude and latitude information to the response. 0,0 means no data.
* @return array - associative array
*/
public function get_zone_ips($zoneid, $hours, $class, $geo = '0,0') {
$data['a'] = 'zone_ips';
$data['zid'] = $zoneid;
$data['hours'] = $hours;
$data['class'] = $class;
$data['geo'] = $geo;
return $this->http_post($data);
}
/**
* Creates a new DNS record for your site. This can be either a CNAME or A record.
*
* @param string $zone - zone (example.com)
* @param string $type - one of (A|CNAME)
* @param string $content - The value of the cname or IP address (the destination).
* @param string $name - The name of the record you wish to create.
* @param bool $mode - false means CloudFlare is off (grey cloud) for the new zone, while true means a happy orange cloud.
* @return array - associative array
*/
public function add_dns_record($zone, $type, $content, $name, $mode) {
$data['a'] = 'rec_set';
$data['zone'] = $zone;
$data['type'] = ($type == 'A') ? 'A' : 'CNAME';
$data['content'] = $content;
$data['name'] = $name;
$data['service_mode'] = ($mode == true) ? 1 : 0;
return $this->http_post($data);
}
/**
* Update an existing DNS record - Update a DNS record for your site. This needs to be an A record.
* $ip = The value of the IP address (the destination).
* $hosts = The name of the record you wish to create.
*/
public function update_dns_record($ip, $hosts) {
$data['a'] = "DIUP";
$data['ip'] = $ip;
$data['hosts'] = $hosts;
return $this->http_post($data);
}
/**
* Toggle IPv6 support for your site - Toggles ipv6 support for a site.
*/
public function toggle_ipv6($zone, $mode) {
$data['a'] = 'ipv46';
$data['z'] = $zone;
$data['v'] = ($mode == true) ? 1 : 0;
return $this->http_post($data);
}
/**
* Update the snapshot of your site for CloudFlare's challenge page
* 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['a'] = 'zone_grab';
$data['zid'] = $zoneid;
return $this->http_post($data);
}
public function zone_check($zones) {
if (is_array($zones))
$zones = implode(",", $zones);
$data['a'] = 'zone_check';
$data['zones'] = $zones;
return $this->http_post($data);
}
public function del_dns($zone, $name) {
$data['a'] = 'rec_del';
$data['zone'] = $zone;
$data['name'] = $name;
return $this->http_post($data);
}
public function update_dns($host, $ip) {
$data['a'] = 'DIUP';
$data['ip'] = $ip;
$data['hosts'] = $host;
return $this->http_post($data);
}
public function threat_score($ip) {
$data['a'] = 'ip_lkup';
$data['ip'] = $ip;
return $this->http_post($data);
}
// HOST SECTION
public function user_create($email, $password, $username = '', $id = '') {
$data['act'] = 'user_create';
$data['cloudflare_email'] = $email;
$data['cloudflare_pass'] = $password;
$data['cloudflare_username'] = $username;
$data['unique_id'] = $id;
return $this->http_post($data, 'HOST');
}
public function zone_set($key, $zone, $resolve_to, $subdomains) {
if (is_array($subdomains))
$sudomains = implode(",", $subdomains);
$data['act'] = 'zone_set';
$data['user_key'] = $key;
$data['zone_name'] = $zone;
$data['resolve_to'] = $resolve_to;
$data['subdomains'] = $subdomains;
return $this->http_post($data, 'HOST');
}
public function user_lookup($email, $isID = false) {
$data['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, $pass, $id = '') {
$data['act'] = 'user_auth';
$data['cloudflare_email'] = $email;
$data['cloudflare_pass'] = $pass;
$data['unique_id'] = $id;
return $this->http_post($data, 'HOST');
}
public function zone_lookup($zone, $user_key) {
$data['act'] = 'zone_lookup';
$data['user_key'] = $user_key;
$data['zone_name'] = $zone;
return $this->http_post($data, 'HOST');
}
public function zone_delete($zone, $user_key) {
$data['act'] = 'zone_delete';
$data['user_key'] = $user_key;
$data['zone_name'] = $zone;
return $this->http_post($data, 'HOST');
}
/**
* 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, $this->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, $this->out_array);
}
}
}