NetDNA.php
14.6 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
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
<?php
if (!defined('ABSPATH')) {
die();
}
require_once(W3TC_LIB_DIR . '/OAuth/W3tcOAuth.php');
require_once('W3tcWpHttpException.php');
/**
* NetDNA REST Client Library
*
* @copyright 2012
* @author Karlo Espiritu
* @version 1.0 2012-09-21
*/
class NetDNA {
public $alias;
public $key;
public $secret;
public $netdnarws_url = 'https://rws.netdna.com';
static public function create($authorization_key) {
$keys = explode('+', $authorization_key);
$alias = '';
$consumerkey = '';
$consumersecret = '';
if (sizeof($keys) == 3)
list($alias, $consumerkey, $consumersecret) = $keys;
$api = new NetDNA($alias, $consumerkey, $consumersecret);
return $api;
}
/**
* @param string $alias
* @param string $key
* @param string $secret
*/
public function __construct($alias, $key, $secret) {
$this->alias = $alias;
$this->key = $key;
$this->secret = $secret;
}
public function get_zone_domain($name) {
return $name . '.' . $this->alias . '.netdna-cdn.com';
}
public function is_valid() {
return !empty($this->alias) && !empty($this->key) &&
!empty($this->secret);
}
/**
* @param $selected_call
* @param $method_type
* @param $params
* @return string
* @throws W3tcWpHttpException
*/
private function execute($selected_call, $method_type, $params) {
//increase the http request timeout
add_filter('http_request_timeout', array($this, 'filter_timeout_time'));
add_filter('https_ssl_verify', array($this, 'https_ssl_verify'));
$consumer = new W3tcOAuthConsumer($this->key, $this->secret, NULL);
// the endpoint for your request
$endpoint = "$this->netdnarws_url/$this->alias$selected_call";
//parse endpoint before creating OAuth request
$parsed = parse_url($endpoint);
if (array_key_exists("parsed", $parsed)) {
parse_str($parsed['query'], $params);
}
//generate a request from your consumer
$req_req = W3tcOAuthRequest::from_consumer_and_token($consumer, NULL, $method_type, $endpoint, $params);
//sign your OAuth request using hmac_sha1
$sig_method = new W3tcOAuthSignatureMethod_HMAC_SHA1();
$req_req->sign_request($sig_method, $consumer, NULL);
$request = array();
$request['sslverify'] = false;
$request['method'] = $method_type;
if ($method_type == "POST") {
$request['body'] = $req_req->to_postdata();
$request['headers']['Content-Type'] =
'application/x-www-form-urlencoded; charset=' . get_option('blog_charset');
$url = $req_req->get_normalized_http_url();
} else {
// notice GET, PUT and DELETE both needs to be passed in URL
$url = $req_req->to_url();
}
$response = wp_remote_request($url, $request);
$json_output = '';
if (!is_wp_error($response)) {
// make call
$result = wp_remote_retrieve_body($response);
$headers = wp_remote_retrieve_headers($response);
$response_code = wp_remote_retrieve_response_code($response);
// $json_output contains the output string
$json_output = $result;
} else {
$response_code = $response->get_error_code();
}
remove_filter('https_ssl_verify', array($this, 'https_ssl_verify'));
remove_filter('http_request_timeout', array($this, 'filter_timeout_time'));
// catch errors
if(is_wp_error($response)) {
throw new W3tcWpHttpException("ERROR: {$response->get_error_message()}, Output: $json_output", $response_code, null, $headers);
}
return $json_output;
}
/**
* @param $selected_call
* @param array $params
* @return string
* @throws W3tcWpHttpException
*/
public function get($selected_call, $params = array()){
return $this->execute($selected_call, 'GET', $params);
}
/**
* @param $selected_call
* @param array $params
* @return string
* @throws W3tcWpHttpException
*/
public function post($selected_call, $params = array()){
return $this->execute($selected_call, 'POST', $params);
}
/**
* @param $selected_call
* @param array $params
* @return string
* @throws W3tcWpHttpException
*/
public function put($selected_call, $params = array()){
return $this->execute($selected_call, 'PUT', $params);
}
/**
* @param $selected_call
* @param array $params
* @return string
* @throws W3tcWpHttpException
*/
public function delete($selected_call, $params = array()){
return $this->execute($selected_call, 'DELETE', $params);
}
/**
* Finds the zone id that matches the provided url.
* @param $url
* @return null|int
* @throws W3tcWpHttpException
*/
public function get_zone_id($url) {
$zone_id = null;
$pull_zones = json_decode($this->get('/zones/pull.json'));
if (preg_match("(200|201)", $pull_zones->code)) {
foreach ($pull_zones->data->pullzones as $zone) {
if (trim($zone->url, '/') != trim($url, '/'))
continue;
else {
$zone_id = $zone->id;
break;
}
}
} else
return null;
return $zone_id;
}
/**
* Retrieves statistics for the zone id
* @param $zone_id
* @return null|array
* @throws W3tcWpHttpException
*/
public function get_stats_per_zone($zone_id) {
$api_stats = json_decode($this->get("/reports/{$zone_id}/stats.json"), true);
if (preg_match("(200|201)", $api_stats['code'])) {
$summary = $api_stats['data']['summary'];
return $summary;
} else
return null;
}
/**
* Returns list of files for the zone id
* @param $zone_id
* @return null|array
* @throws W3tcWpHttpException
*/
public function get_list_of_file_types_per_zone($zone_id) {
$api_list = json_decode($this->get("/reports/pull/{$zone_id}/filetypes.json"), true);
if (preg_match("(200|201)", $api_list['code'])) {
$stats['total'] = $api_list['data']['total'];
foreach($api_list['data']['filetypes'] as $filetyp) {
$stats['filetypes'][] = $filetyp;
}
$stats['summary'] = $api_list['data']['summary'];
return $stats;
} else
return null;
}
/**
* Retrieves a list of popular files for zone id
*
* @param $zone_id
* @return null|array
* @throws W3tcWpHttpException
*/
public function get_list_of_popularfiles_per_zone($zone_id) {
$api_popularfiles = json_decode($this->get("/reports/{$zone_id}/popularfiles.json"), true);
if (preg_match("(200|201)", $api_popularfiles['code'])) {
$popularfiles = $api_popularfiles['data']['popularfiles'];
return $popularfiles;
} else
return null;
}
/**
* Retrieves an account connected with the authorization key
*
* @throws Exception
* @return null|string
*/
public function get_account() {
$api_account = json_decode($this->get("/account.json"), true);
if (preg_match("(200|201)", $api_account['code'])) {
$account = $api_account['data']['account'];
return $account;
} else
throw new Exception($api_account['error']['message']);
}
/**
* Retrieves a pull zone
* @param $zone_id
* @throws Exception
* @return null|string
*/
public function get_pull_zone($zone_id) {
$api_pull_zone = json_decode($this->get("/zones/pull.json/{$zone_id}"), true);
if (preg_match("(200|201)", $api_pull_zone['code'])) {
$pull_zone = $api_pull_zone['data']['pullzone'];
return $pull_zone;
} else
throw new Exception($api_pull_zone['error']['message']);
}
/**
* Creates a pull zone
* @param $zone
* @return mixed
* @throws Exception
*/
public function create_pull_zone($zone) {
$zone_data = json_decode($this->post('/zones/pull.json', $zone), true);
if (preg_match("(200|201)", $zone_data['code'])) {
return $zone_data['data']['pullzone'];
} else
throw new Exception($zone_data['error']['message']);
}
/**
* Returns all zones connected to an url
* @param $url
* @throws Exception
* @return array|null
*/
public function get_zones_by_url($url) {
$zone_id = null;
$pull_zones = json_decode($this->get('/zones/pull.json'), true);
$zones = array();
if (preg_match("(200|201)", $pull_zones['code'])) {
foreach ($pull_zones ['data']['pullzones'] as $zone) {
if (trim($zone['url'], '/') != trim($url, '/'))
continue;
else {
$zones[] = $zone;
}
}
} else
throw new Exception($pull_zones['error']['message']);
return $zones;
}
/**
* Retrieves pull zones
* @throws Exception
* @return array|null
*/
public function get_pull_zones() {
$pull_zones = json_decode($this->get('/zones/pull.json'), true);
$zones = array();
if (preg_match("(200|201)", $pull_zones['code'])) {
foreach ($pull_zones ['data']['pullzones'] as $zone) {
$zones[] = $zone;
}
} else {
throw new Exception($pull_zones['error']['message']);
}
return $zones;
}
/**
* Increase http request timeout to 60 seconds
* @param int $time
* @return int
*/
public function filter_timeout_time($time) {
return 60;
}
/**
* Don't check certificate, some users have limited CA list
*/
public function https_ssl_verify($v) {
return false;
}
/**
* Update a pull zone
* @param $zone_id
* @param $zone
* @throws Exception
* @return
*/
public function update_pull_zone($zone_id, $zone) {
$zone_data = json_decode($this->put("/zones/pull.json/$zone_id", $zone), true);
if (preg_match("(200|201)", $zone_data['code'])) {
return $zone_data['data']['pullzone'];
} else
throw new Exception($zone_data['error']['message']);
}
/**
* Creates a new pull zone with default settings
* @param string $url the sites url 4-100 chars; only valid URLs accepted
* @param null|string $name 3-32 chars; only letters, digits, and dash (-)accepted
* @param null|string $label length: 1-255 chars
* @param array $zone_settings custom settings
* @return string
*/
public function create_default_pull_zone($url, $name = null, $label = null, $zone_settings=array()) {
$zone_defaults = array();
if (is_null($name)) {
$name = md5($url);
$len = strlen($name)>24 ? 24 : strlen($name);
$name = substr($name, 0, $len);
}
if (is_null($label))
$label = sprintf(__('Zone for %s was created by W3 Total Cache', 'w3-total-cache'), $url);
$zone_defaults['name'] = $name;
$zone_defaults['label'] = $label;
$zone_defaults['url'] = $url;
$zone_defaults['use_stale'] = 0;
$zone_defaults['queries'] = 1;
$zone_defaults['compress'] = 1;
$zone_defaults['backend_compress'] = 1;
$zone_defaults['disallow_robots'] = 1;
$zone_defaults = array_merge( $zone_defaults, $zone_settings);
$response = $this->create_pull_zone($zone_defaults);
return $response;
}
/**
* Returns number of zones
* @throws Exception
* @return array
*/
public function get_zone_count() {
$pull_zones = json_decode($this->get('/zones.json/count'), true);
if (preg_match("(200|201)", $pull_zones['code'])) {
return intval($pull_zones ['data']['count']);
} else
throw new Exception($pull_zones['error']['message']);
}
/**
* Creates custom domains
* @param $zone_id
* @throws Exception
* @return array|null
*/
public function create_custom_domain($zone_id, $custom_domain) {
$custom_domain = json_decode($this->post("/zones/pull/$zone_id/customdomains.json", array(
'custom_domain' => $custom_domain)), true);
if (preg_match("(200|201)", $custom_domain['code'])) {
return $custom_domain;
} else
throw $this->to_exception($custom_domain);
}
private function to_exception($response) {
$message = $response['error']['message'];
if (isset($response['data']) && isset($response['data']['errors'])) {
foreach ($response['data']['errors'] as $field => $error)
$message .= '. ' . $field . ': ' . $error;
}
return new Exception($message);
}
/**
* Returns custom domains
* @param $zone_id
* @throws Exception
* @return array|null
*/
public function get_custom_domains($zone_id) {
$custom_domains = json_decode($this->get("/zones/pull/$zone_id/customdomains.json"), true);
$domains = array();
if (preg_match("(200|201)", $custom_domains['code'])) {
foreach ($custom_domains['data']['customdomains'] as $domain) {
$domains[] = $domain['custom_domain'];
}
} else
throw new Exception($custom_domains['error']['message']);
return $domains;
}
/**
* Returns the zone data for the provided zone id
*
* @param int $zone_id
* @throws Exception
* @return array
*/
public function get_zone($zone_id) {
$zone_data = json_decode($this->get("/zones/pull.json/$zone_id"), true);
if (preg_match("(200|201)", $zone_data['code'])) {
return $zone_data['data']['pullzone'];
} else
throw new Exception($zone_data['error']['message']);
}
/**
* Deletes files from cache
* @param $zone_id
* @param $files array of relative paths to files to delete
* Deletes whole zone if empty list passed
**/
public function cache_delete($zone_id, $files = array()) {
if (empty($files))
$params = array();
else
$params = array('files' => $files);
$response = json_decode($this->delete(
'/zones/pull.json/' . $zone_id . '/cache',
$params), true);
if (preg_match("(200|201)", $response['code'])) {
return true;
} else
throw $this->to_exception($response);
}
}