PageRenderTime 25ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/content/plugins/w3-total-cache/lib/NetDNA/NetDNA.php

https://gitlab.com/karlen/ayo_wp
PHP | 485 lines | 269 code | 57 blank | 159 comment | 38 complexity | e27d3418800bd789e11421f1f3927fe4 MD5 | raw file
  1. <?php
  2. if (!defined('ABSPATH')) {
  3. die();
  4. }
  5. require_once(W3TC_LIB_DIR . '/OAuth/W3tcOAuth.php');
  6. require_once('W3tcWpHttpException.php');
  7. /**
  8. * NetDNA REST Client Library
  9. *
  10. * @copyright 2012
  11. * @author Karlo Espiritu
  12. * @version 1.0 2012-09-21
  13. */
  14. class NetDNA {
  15. public $alias;
  16. public $key;
  17. public $secret;
  18. public $netdnarws_url = 'https://rws.netdna.com';
  19. static public function create($authorization_key) {
  20. $keys = explode('+', $authorization_key);
  21. $alias = '';
  22. $consumerkey = '';
  23. $consumersecret = '';
  24. if (sizeof($keys) == 3)
  25. list($alias, $consumerkey, $consumersecret) = $keys;
  26. $api = new NetDNA($alias, $consumerkey, $consumersecret);
  27. return $api;
  28. }
  29. /**
  30. * @param string $alias
  31. * @param string $key
  32. * @param string $secret
  33. */
  34. public function __construct($alias, $key, $secret) {
  35. $this->alias = $alias;
  36. $this->key = $key;
  37. $this->secret = $secret;
  38. }
  39. public function get_zone_domain($name) {
  40. return $name . '.' . $this->alias . '.netdna-cdn.com';
  41. }
  42. public function is_valid() {
  43. return !empty($this->alias) && !empty($this->key) &&
  44. !empty($this->secret);
  45. }
  46. /**
  47. * @param $selected_call
  48. * @param $method_type
  49. * @param $params
  50. * @return string
  51. * @throws W3tcWpHttpException
  52. */
  53. private function execute($selected_call, $method_type, $params) {
  54. //increase the http request timeout
  55. add_filter('http_request_timeout', array($this, 'filter_timeout_time'));
  56. add_filter('https_ssl_verify', array($this, 'https_ssl_verify'));
  57. $consumer = new W3tcOAuthConsumer($this->key, $this->secret, NULL);
  58. // the endpoint for your request
  59. $endpoint = "$this->netdnarws_url/$this->alias$selected_call";
  60. //parse endpoint before creating OAuth request
  61. $parsed = parse_url($endpoint);
  62. if (array_key_exists("parsed", $parsed)) {
  63. parse_str($parsed['query'], $params);
  64. }
  65. //generate a request from your consumer
  66. $req_req = W3tcOAuthRequest::from_consumer_and_token($consumer, NULL, $method_type, $endpoint, $params);
  67. //sign your OAuth request using hmac_sha1
  68. $sig_method = new W3tcOAuthSignatureMethod_HMAC_SHA1();
  69. $req_req->sign_request($sig_method, $consumer, NULL);
  70. $request = array();
  71. $request['sslverify'] = false;
  72. $request['method'] = $method_type;
  73. if ($method_type == "POST") {
  74. $request['body'] = $req_req->to_postdata();
  75. $request['headers']['Content-Type'] =
  76. 'application/x-www-form-urlencoded; charset=' . get_option('blog_charset');
  77. $url = $req_req->get_normalized_http_url();
  78. } else {
  79. // notice GET, PUT and DELETE both needs to be passed in URL
  80. $url = $req_req->to_url();
  81. }
  82. $response = wp_remote_request($url, $request);
  83. $json_output = '';
  84. if (!is_wp_error($response)) {
  85. // make call
  86. $result = wp_remote_retrieve_body($response);
  87. $headers = wp_remote_retrieve_headers($response);
  88. $response_code = wp_remote_retrieve_response_code($response);
  89. // $json_output contains the output string
  90. $json_output = $result;
  91. } else {
  92. $response_code = $response->get_error_code();
  93. }
  94. remove_filter('https_ssl_verify', array($this, 'https_ssl_verify'));
  95. remove_filter('http_request_timeout', array($this, 'filter_timeout_time'));
  96. // catch errors
  97. if(is_wp_error($response)) {
  98. throw new W3tcWpHttpException("ERROR: {$response->get_error_message()}, Output: $json_output", $response_code, null, $headers);
  99. }
  100. return $json_output;
  101. }
  102. /**
  103. * @param $selected_call
  104. * @param array $params
  105. * @return string
  106. * @throws W3tcWpHttpException
  107. */
  108. public function get($selected_call, $params = array()){
  109. return $this->execute($selected_call, 'GET', $params);
  110. }
  111. /**
  112. * @param $selected_call
  113. * @param array $params
  114. * @return string
  115. * @throws W3tcWpHttpException
  116. */
  117. public function post($selected_call, $params = array()){
  118. return $this->execute($selected_call, 'POST', $params);
  119. }
  120. /**
  121. * @param $selected_call
  122. * @param array $params
  123. * @return string
  124. * @throws W3tcWpHttpException
  125. */
  126. public function put($selected_call, $params = array()){
  127. return $this->execute($selected_call, 'PUT', $params);
  128. }
  129. /**
  130. * @param $selected_call
  131. * @param array $params
  132. * @return string
  133. * @throws W3tcWpHttpException
  134. */
  135. public function delete($selected_call, $params = array()){
  136. return $this->execute($selected_call, 'DELETE', $params);
  137. }
  138. /**
  139. * Finds the zone id that matches the provided url.
  140. * @param $url
  141. * @return null|int
  142. * @throws W3tcWpHttpException
  143. */
  144. public function get_zone_id($url) {
  145. $zone_id = null;
  146. $pull_zones = json_decode($this->get('/zones/pull.json'));
  147. if (preg_match("(200|201)", $pull_zones->code)) {
  148. foreach ($pull_zones->data->pullzones as $zone) {
  149. if (trim($zone->url, '/') != trim($url, '/'))
  150. continue;
  151. else {
  152. $zone_id = $zone->id;
  153. break;
  154. }
  155. }
  156. } else
  157. return null;
  158. return $zone_id;
  159. }
  160. /**
  161. * Retrieves statistics for the zone id
  162. * @param $zone_id
  163. * @return null|array
  164. * @throws W3tcWpHttpException
  165. */
  166. public function get_stats_per_zone($zone_id) {
  167. $api_stats = json_decode($this->get("/reports/{$zone_id}/stats.json"), true);
  168. if (preg_match("(200|201)", $api_stats['code'])) {
  169. $summary = $api_stats['data']['summary'];
  170. return $summary;
  171. } else
  172. return null;
  173. }
  174. /**
  175. * Returns list of files for the zone id
  176. * @param $zone_id
  177. * @return null|array
  178. * @throws W3tcWpHttpException
  179. */
  180. public function get_list_of_file_types_per_zone($zone_id) {
  181. $api_list = json_decode($this->get("/reports/pull/{$zone_id}/filetypes.json"), true);
  182. if (preg_match("(200|201)", $api_list['code'])) {
  183. $stats['total'] = $api_list['data']['total'];
  184. foreach($api_list['data']['filetypes'] as $filetyp) {
  185. $stats['filetypes'][] = $filetyp;
  186. }
  187. $stats['summary'] = $api_list['data']['summary'];
  188. return $stats;
  189. } else
  190. return null;
  191. }
  192. /**
  193. * Retrieves a list of popular files for zone id
  194. *
  195. * @param $zone_id
  196. * @return null|array
  197. * @throws W3tcWpHttpException
  198. */
  199. public function get_list_of_popularfiles_per_zone($zone_id) {
  200. $api_popularfiles = json_decode($this->get("/reports/{$zone_id}/popularfiles.json"), true);
  201. if (preg_match("(200|201)", $api_popularfiles['code'])) {
  202. $popularfiles = $api_popularfiles['data']['popularfiles'];
  203. return $popularfiles;
  204. } else
  205. return null;
  206. }
  207. /**
  208. * Retrieves an account connected with the authorization key
  209. *
  210. * @throws Exception
  211. * @return null|string
  212. */
  213. public function get_account() {
  214. $api_account = json_decode($this->get("/account.json"), true);
  215. if (preg_match("(200|201)", $api_account['code'])) {
  216. $account = $api_account['data']['account'];
  217. return $account;
  218. } else
  219. throw new Exception($api_account['error']['message']);
  220. }
  221. /**
  222. * Retrieves a pull zone
  223. * @param $zone_id
  224. * @throws Exception
  225. * @return null|string
  226. */
  227. public function get_pull_zone($zone_id) {
  228. $api_pull_zone = json_decode($this->get("/zones/pull.json/{$zone_id}"), true);
  229. if (preg_match("(200|201)", $api_pull_zone['code'])) {
  230. $pull_zone = $api_pull_zone['data']['pullzone'];
  231. return $pull_zone;
  232. } else
  233. throw new Exception($api_pull_zone['error']['message']);
  234. }
  235. /**
  236. * Creates a pull zone
  237. * @param $zone
  238. * @return mixed
  239. * @throws Exception
  240. */
  241. public function create_pull_zone($zone) {
  242. $zone_data = json_decode($this->post('/zones/pull.json', $zone), true);
  243. if (preg_match("(200|201)", $zone_data['code'])) {
  244. return $zone_data['data']['pullzone'];
  245. } else
  246. throw new Exception($zone_data['error']['message']);
  247. }
  248. /**
  249. * Returns all zones connected to an url
  250. * @param $url
  251. * @throws Exception
  252. * @return array|null
  253. */
  254. public function get_zones_by_url($url) {
  255. $zone_id = null;
  256. $pull_zones = json_decode($this->get('/zones/pull.json'), true);
  257. $zones = array();
  258. if (preg_match("(200|201)", $pull_zones['code'])) {
  259. foreach ($pull_zones ['data']['pullzones'] as $zone) {
  260. if (trim($zone['url'], '/') != trim($url, '/'))
  261. continue;
  262. else {
  263. $zones[] = $zone;
  264. }
  265. }
  266. } else
  267. throw new Exception($pull_zones['error']['message']);
  268. return $zones;
  269. }
  270. /**
  271. * Retrieves pull zones
  272. * @throws Exception
  273. * @return array|null
  274. */
  275. public function get_pull_zones() {
  276. $pull_zones = json_decode($this->get('/zones/pull.json'), true);
  277. $zones = array();
  278. if (preg_match("(200|201)", $pull_zones['code'])) {
  279. foreach ($pull_zones ['data']['pullzones'] as $zone) {
  280. $zones[] = $zone;
  281. }
  282. } else {
  283. throw new Exception($pull_zones['error']['message']);
  284. }
  285. return $zones;
  286. }
  287. /**
  288. * Increase http request timeout to 60 seconds
  289. * @param int $time
  290. * @return int
  291. */
  292. public function filter_timeout_time($time) {
  293. return 60;
  294. }
  295. /**
  296. * Don't check certificate, some users have limited CA list
  297. */
  298. public function https_ssl_verify($v) {
  299. return false;
  300. }
  301. /**
  302. * Update a pull zone
  303. * @param $zone_id
  304. * @param $zone
  305. * @throws Exception
  306. * @return
  307. */
  308. public function update_pull_zone($zone_id, $zone) {
  309. $zone_data = json_decode($this->put("/zones/pull.json/$zone_id", $zone), true);
  310. if (preg_match("(200|201)", $zone_data['code'])) {
  311. return $zone_data['data']['pullzone'];
  312. } else
  313. throw new Exception($zone_data['error']['message']);
  314. }
  315. /**
  316. * Creates a new pull zone with default settings
  317. * @param string $url the sites url 4-100 chars; only valid URLs accepted
  318. * @param null|string $name 3-32 chars; only letters, digits, and dash (-)accepted
  319. * @param null|string $label length: 1-255 chars
  320. * @param array $zone_settings custom settings
  321. * @return string
  322. */
  323. public function create_default_pull_zone($url, $name = null, $label = null, $zone_settings=array()) {
  324. $zone_defaults = array();
  325. if (is_null($name)) {
  326. $name = md5($url);
  327. $len = strlen($name)>24 ? 24 : strlen($name);
  328. $name = substr($name, 0, $len);
  329. }
  330. if (is_null($label))
  331. $label = sprintf(__('Zone for %s was created by W3 Total Cache', 'w3-total-cache'), $url);
  332. $zone_defaults['name'] = $name;
  333. $zone_defaults['label'] = $label;
  334. $zone_defaults['url'] = $url;
  335. $zone_defaults['use_stale'] = 0;
  336. $zone_defaults['queries'] = 1;
  337. $zone_defaults['compress'] = 1;
  338. $zone_defaults['backend_compress'] = 1;
  339. $zone_defaults['disallow_robots'] = 1;
  340. $zone_defaults = array_merge( $zone_defaults, $zone_settings);
  341. $response = $this->create_pull_zone($zone_defaults);
  342. return $response;
  343. }
  344. /**
  345. * Returns number of zones
  346. * @throws Exception
  347. * @return array
  348. */
  349. public function get_zone_count() {
  350. $pull_zones = json_decode($this->get('/zones.json/count'), true);
  351. if (preg_match("(200|201)", $pull_zones['code'])) {
  352. return intval($pull_zones ['data']['count']);
  353. } else
  354. throw new Exception($pull_zones['error']['message']);
  355. }
  356. /**
  357. * Creates custom domains
  358. * @param $zone_id
  359. * @throws Exception
  360. * @return array|null
  361. */
  362. public function create_custom_domain($zone_id, $custom_domain) {
  363. $custom_domain = json_decode($this->post("/zones/pull/$zone_id/customdomains.json", array(
  364. 'custom_domain' => $custom_domain)), true);
  365. if (preg_match("(200|201)", $custom_domain['code'])) {
  366. return $custom_domain;
  367. } else
  368. throw $this->to_exception($custom_domain);
  369. }
  370. private function to_exception($response) {
  371. $message = $response['error']['message'];
  372. if (isset($response['data']) && isset($response['data']['errors'])) {
  373. foreach ($response['data']['errors'] as $field => $error)
  374. $message .= '. ' . $field . ': ' . $error;
  375. }
  376. return new Exception($message);
  377. }
  378. /**
  379. * Returns custom domains
  380. * @param $zone_id
  381. * @throws Exception
  382. * @return array|null
  383. */
  384. public function get_custom_domains($zone_id) {
  385. $custom_domains = json_decode($this->get("/zones/pull/$zone_id/customdomains.json"), true);
  386. $domains = array();
  387. if (preg_match("(200|201)", $custom_domains['code'])) {
  388. foreach ($custom_domains['data']['customdomains'] as $domain) {
  389. $domains[] = $domain['custom_domain'];
  390. }
  391. } else
  392. throw new Exception($custom_domains['error']['message']);
  393. return $domains;
  394. }
  395. /**
  396. * Returns the zone data for the provided zone id
  397. *
  398. * @param int $zone_id
  399. * @throws Exception
  400. * @return array
  401. */
  402. public function get_zone($zone_id) {
  403. $zone_data = json_decode($this->get("/zones/pull.json/$zone_id"), true);
  404. if (preg_match("(200|201)", $zone_data['code'])) {
  405. return $zone_data['data']['pullzone'];
  406. } else
  407. throw new Exception($zone_data['error']['message']);
  408. }
  409. /**
  410. * Deletes files from cache
  411. * @param $zone_id
  412. * @param $files array of relative paths to files to delete
  413. * Deletes whole zone if empty list passed
  414. **/
  415. public function cache_delete($zone_id, $files = array()) {
  416. if (empty($files))
  417. $params = array();
  418. else
  419. $params = array('files' => $files);
  420. $response = json_decode($this->delete(
  421. '/zones/pull.json/' . $zone_id . '/cache',
  422. $params), true);
  423. if (preg_match("(200|201)", $response['code'])) {
  424. return true;
  425. } else
  426. throw $this->to_exception($response);
  427. }
  428. }