PageRenderTime 460ms CodeModel.GetById 134ms RepoModel.GetById 127ms app.codeStats 0ms

/wp-content/plugins/w3-total-cache/lib/W3/Cdn/Mirror/Netdna.php

https://gitlab.com/juanito.abelo/nlmobile
PHP | 282 lines | 207 code | 48 blank | 27 comment | 62 complexity | 7deed78dedf0bb2f5217a32e97462819 MD5 | raw file
  1. <?php
  2. /**
  3. * W3 CDN Netdna Class
  4. */
  5. if (!defined('ABSPATH')) {
  6. die();
  7. }
  8. w3_require_once(W3TC_LIB_W3_DIR . '/Cdn/Mirror.php');
  9. define('W3TC_CDN_NETDNA_URL', 'netdna-cdn.com');
  10. /**
  11. * Class W3_Cdn_Mirror_Netdna
  12. */
  13. class W3_Cdn_Mirror_Netdna extends W3_Cdn_Mirror {
  14. /**
  15. * PHP5 Constructor
  16. *
  17. * @param array $config
  18. */
  19. function __construct($config = array()) {
  20. $config = array_merge(array(
  21. 'authorization_key' => '',
  22. 'alias' => '',
  23. 'consumerkey' => '',
  24. 'consumersecret' => '',
  25. 'zone_id' => 0
  26. ), $config);
  27. $split_keys = explode('+', $config['authorization_key']);
  28. if (sizeof($split_keys)==3)
  29. list($config['alias'], $config['consumerkey'], $config['consumersecret']) = $split_keys;
  30. parent::__construct($config);
  31. }
  32. /**
  33. * Purges remote files
  34. *
  35. * @param array $files
  36. * @param array $results
  37. * @return boolean
  38. */
  39. function purge($files, &$results) {
  40. if (empty($this->_config['authorization_key'])) {
  41. $results = $this->_get_results($files, W3TC_CDN_RESULT_HALT, __('Empty Authorization Key.', 'w3-total-cache'));
  42. return false;
  43. }
  44. if (empty($this->_config['alias']) || empty($this->_config['consumerkey']) || empty($this->_config['consumersecret'])) {
  45. $results = $this->_get_results($files, W3TC_CDN_RESULT_HALT, __('Malformed Authorization Key.', 'w3-total-cache'));
  46. return false;
  47. }
  48. if (!class_exists('NetDNA')) {
  49. w3_require_once(W3TC_LIB_NETDNA_DIR . '/NetDNA.php');
  50. }
  51. $api = new NetDNA($this->_config['alias'], $this->_config['consumerkey'], $this->_config['consumersecret']);
  52. $results = array();
  53. $local_path = $remote_path = '';
  54. $domain_is_valid = 0;
  55. $found_domain = false;
  56. try {
  57. if ($this->_config['zone_id'] != 0)
  58. $zone_id = $this->_config['zone_id'];
  59. else {
  60. $zone_id = $api->get_zone_id(w3_get_home_url());
  61. }
  62. if ($zone_id == 0) {
  63. $zone_id = $api->get_zone_id(w3_get_domain_url());
  64. }
  65. if ($zone_id == 0) {
  66. $zone_id = $api->get_zone_id(str_replace('://', '://www.', w3_get_domain_url()));
  67. }
  68. if ($zone_id == 0 || is_null($zone_id)) {
  69. if (w3_get_domain_url() == w3_get_home_url())
  70. $results[] = $this->_get_result($local_path, $remote_path, W3TC_CDN_RESULT_ERROR, sprintf(__('No zones match site: %s.', 'w3-total-cache'), trim(w3_get_home_url(), '/')));
  71. else
  72. $results[] = $this->_get_result($local_path, $remote_path, W3TC_CDN_RESULT_ERROR, sprintf(__('No zones match site: %s or %s.', 'w3-total-cache'), trim(w3_get_home_url(), '/'), trim(w3_get_domain_url(), '/')));
  73. return !$this->_is_error($results);
  74. }
  75. $pullzone = json_decode($api->get('/zones/pull.json/' . $zone_id));
  76. try {
  77. if (preg_match("(200|201)", $pullzone->code)) {
  78. $custom_domains_full = json_decode($api->get('/zones/pull/' . $pullzone->data->pullzone->id . '/customdomains.json'));
  79. $custom_domains = array();
  80. foreach ($custom_domains_full->data->customdomains as $custom_domain_full) {
  81. $custom_domains[]= $custom_domain_full->custom_domain;
  82. }
  83. foreach ($files as $file) {
  84. $local_path = $file['local_path'];
  85. $remote_path = $file['remote_path'];
  86. $domain_is_valid = 0;
  87. $found_domain = false;
  88. if ($pullzone->data->pullzone->name . '.' . $this->_config['alias'] . '.' . W3TC_CDN_NETDNA_URL === $this->get_domain($local_path)
  89. || in_array($this->get_domain($local_path), $custom_domains)) {
  90. try {
  91. $params = array('file' => '/' . $local_path);
  92. $file_purge = json_decode($api->delete('/zones/pull.json/' . $pullzone->data->pullzone->id . '/cache', $params));
  93. if(preg_match("(200|201)", $file_purge->code)) {
  94. $results[] = $this->_get_result($local_path, $remote_path, W3TC_CDN_RESULT_OK, 'OK');
  95. } else {
  96. if(preg_match("(401|500)", $file_purge->code)) {
  97. $results[] = $this->_get_result($local_path, $remote_path, W3TC_CDN_RESULT_ERROR, sprintf(__('Failed with error code %s Please check your alias, consumer key, and private key.', 'w3-total-cache'), $file_purge->code));
  98. } else {
  99. $results[] = $this->_get_result($local_path, $remote_path, W3TC_CDN_RESULT_ERROR, __('Failed with error code ', 'w3-total-cache') . $file_purge->code);
  100. }
  101. }
  102. $found_domain = true;
  103. } catch (W3tcWpHttpException $e) {
  104. $results[] = $this->_get_result($local_path, $remote_path, W3TC_CDN_RESULT_HALT, sprintf(__('Unable to purge (%s).', 'w3-total-cache'), $e->getMessage()));
  105. }
  106. } else {
  107. $results[] = $this->_get_result($local_path, $remote_path, W3TC_CDN_RESULT_ERROR, sprintf(__('No registered CNAMEs match %s.', 'w3-total-cache'), $this->get_domain($local_path)));
  108. $domain_is_valid++;
  109. break;
  110. }
  111. }
  112. } else {
  113. if (preg_match("(401|500)", $pullzone->code)) {
  114. $results[] = $this->_get_result($local_path, $remote_path, W3TC_CDN_RESULT_ERROR, sprintf(__('Failed with error code %s. Please check your alias, consumer key, and private key.', 'w3-total-cache'), $pullzone->code));
  115. } else {
  116. $results[] = $this->_get_result($local_path, $remote_path, W3TC_CDN_RESULT_ERROR, __('Failed with error code ', 'w3-total-cache') . $pullzone->code);
  117. }
  118. }
  119. } catch (W3tcWpHttpException $e) {
  120. $results[] = $this->_get_result($local_path, $remote_path, W3TC_CDN_RESULT_HALT, sprintf(__('Unable to purge (%s).', 'w3-total-cache'), $e->getMessage()));
  121. }
  122. } catch (W3tcWpHttpException $e) {
  123. $results[] = $this->_get_result($local_path, $remote_path, W3TC_CDN_RESULT_HALT, __('Failure to pull zone: ', 'w3-total-cache') . $e->getMessage());
  124. }
  125. if ($domain_is_valid > 0 && !$found_domain) {
  126. $results[] = $this->_get_result($local_path, $remote_path, W3TC_CDN_RESULT_ERROR, __('No zones match custom domain.', 'w3-total-cache'));
  127. } elseif (!$found_domain) {
  128. $results[] = $this->_get_result($local_path, $remote_path, W3TC_CDN_RESULT_ERROR, sprintf(__('No zones match site: %s.', 'w3-total-cache'), trim(w3_get_home_url(), '/')));
  129. }
  130. return !$this->_is_error($results);
  131. }
  132. /**
  133. * Purge CDN completely
  134. * @param $results
  135. * @return bool
  136. */
  137. function purge_all(&$results) {
  138. if (empty($this->_config['authorization_key'])) {
  139. $results = $this->_get_results(array(), W3TC_CDN_RESULT_HALT, __('Empty Authorization Key.', 'w3-total-cache'));
  140. return false;
  141. }
  142. if (empty($this->_config['alias']) || empty($this->_config['consumerkey']) || empty($this->_config['consumersecret'])) {
  143. $results = $this->_get_results(array(), W3TC_CDN_RESULT_HALT, __('Malformed Authorization Key.', 'w3-total-cache'));
  144. return false;
  145. }
  146. if (!class_exists('NetDNA')) {
  147. w3_require_once(W3TC_LIB_NETDNA_DIR . '/NetDNA.php');
  148. }
  149. $api = new NetDNA($this->_config['alias'], $this->_config['consumerkey'], $this->_config['consumersecret']);
  150. $results = array();
  151. $local_path = $remote_path = '';
  152. $domain_is_valid = 0;
  153. $found_domain = false;
  154. try {
  155. if ($this->_config['zone_id'] != 0)
  156. $zone_id = $this->_config['zone_id'];
  157. else {
  158. $zone_id = $api->get_zone_id(w3_get_home_url());
  159. }
  160. if ($zone_id == 0) {
  161. $zone_id = $api->get_zone_id(w3_get_domain_url());
  162. }
  163. if ($zone_id == 0) {
  164. $zone_id = $api->get_zone_id(str_replace('://', '://www.', w3_get_domain_url()));
  165. }
  166. if ($zone_id == 0 || is_null($zone_id)) {
  167. if (w3_get_domain_url() == w3_get_home_url())
  168. $results[] = $this->_get_result($local_path, $remote_path, W3TC_CDN_RESULT_ERROR, sprintf(__('No zones match site: %s.', 'w3-total-cache'), trim(w3_get_home_url(), '/')));
  169. else
  170. $results[] = $this->_get_result($local_path, $remote_path, W3TC_CDN_RESULT_ERROR, sprintf(__('No zones match site: %s or %s.', 'w3-total-cache'), trim(w3_get_home_url(), '/'), trim(w3_get_domain_url(), '/')));
  171. return !$this->_is_error($results);
  172. }
  173. $pullzone = json_decode($api->get('/zones/pull.json/' . $zone_id));
  174. try {
  175. if (preg_match("(200|201)", $pullzone->code)) {
  176. $custom_domains_full = json_decode($api->get('/zones/pull/' . $pullzone->data->pullzone->id . '/customdomains.json'));
  177. $custom_domains = array();
  178. foreach ($custom_domains_full->data->customdomains as $custom_domain_full) {
  179. $custom_domains[]= $custom_domain_full->custom_domain;
  180. }
  181. $local_path = 'all';
  182. $remote_path = 'all';
  183. $domain_is_valid = 0;
  184. $found_domain = false;
  185. if ($pullzone->data->pullzone->name . '.' . $this->_config['alias'] . '.' . W3TC_CDN_NETDNA_URL === $this->get_domain($local_path)
  186. || in_array($this->get_domain($local_path), $custom_domains)) {
  187. try {
  188. $params = array('file' => '/' . $local_path);
  189. $file_purge = json_decode($api->delete('/zones/pull.json/' . $pullzone->data->pullzone->id . '/cache'));
  190. if(preg_match("(200|201)", $file_purge->code)) {
  191. $results[] = $this->_get_result($local_path, $remote_path, W3TC_CDN_RESULT_OK, __('OK', 'w3-total-cache'));
  192. } else {
  193. if(preg_match("(401|500)", $file_purge->code)) {
  194. $results[] = $this->_get_result($local_path, $remote_path, W3TC_CDN_RESULT_ERROR, sprintf(__('Failed with error code %s. Please check your alias, consumer key, and private key.', 'w3-total-cache'), $file_purge->code));
  195. } else {
  196. $results[] = $this->_get_result($local_path, $remote_path, W3TC_CDN_RESULT_ERROR, __('Failed with error code ', 'w3-total-cache') . $file_purge->code);
  197. }
  198. }
  199. $found_domain = true;
  200. } catch (W3tcWpHttpException $e) {
  201. $results[] = $this->_get_result($local_path, $remote_path, W3TC_CDN_RESULT_HALT, sprintf(__('Unable to purge (%s).', 'w3-total-cache'), $e->getMessage()));
  202. }
  203. } else {
  204. $results[] = $this->_get_result($local_path, $remote_path, W3TC_CDN_RESULT_ERROR, sprintf(__('No registered CNAMEs match %s.', 'w3-total-cache'), $this->get_domain($local_path)));
  205. return !$this->_is_error($results);
  206. }
  207. } else {
  208. if (preg_match("(401|500)", $pullzone->code)) {
  209. $results[] = $this->_get_result($local_path, $remote_path, W3TC_CDN_RESULT_ERROR, sprintf(__('Failed with error code %s. Please check your alias, consumer key, and private key.', 'w3-total-cache'), $pullzone->code));
  210. } else {
  211. $results[] = $this->_get_result($local_path, $remote_path, W3TC_CDN_RESULT_ERROR, __('Failed with error code ', 'w3-total-cache') . $pullzone->code);
  212. }
  213. }
  214. } catch (W3tcWpHttpException $e) {
  215. $results[] = $this->_get_result($local_path, $remote_path, W3TC_CDN_RESULT_HALT, sprintf(__('Unable to purge (%s).', 'w3-total-cache'), $e->getMessage()));
  216. }
  217. } catch (W3tcWpHttpException $e) {
  218. $results[] = $this->_get_result($local_path, $remote_path, W3TC_CDN_RESULT_HALT, __('Failure to pull zone: ', 'w3-total-cache') . $e->getMessage());
  219. }
  220. if ($domain_is_valid > 0 && !$found_domain) {
  221. $results[] = $this->_get_result($local_path, $remote_path, W3TC_CDN_RESULT_ERROR, __('No zones match custom domain.', 'w3-total-cache'));
  222. } elseif (!$found_domain) {
  223. $results[] = $this->_get_result($local_path, $remote_path, W3TC_CDN_RESULT_ERROR, sprintf(__('No zones match site: %s.', 'w3-total-cache'), trim(w3_get_home_url(), '/')));
  224. }
  225. return !$this->_is_error($results);
  226. }
  227. /**
  228. * If the CDN supports fullpage mirroring
  229. * @return bool
  230. */
  231. function supports_full_page_mirroring() {
  232. return false;
  233. }
  234. }