PageRenderTime 42ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 1ms

/wp-content/plugins/w3-total-cache/lib/W3/Enterprise/SnsClient.php

https://gitlab.com/juanito.abelo/nlmobile
PHP | 378 lines | 179 code | 50 blank | 149 comment | 12 complexity | 38ff6e196be86d6584ffcd8b9c948369 MD5 | raw file
  1. <?php
  2. /**
  3. * Purge using AmazonSNS object
  4. */
  5. w3_require_once(W3TC_LIB_W3_DIR . '/Enterprise/SnsBase.php');
  6. /**
  7. * Class W3_Sns
  8. */
  9. class W3_Enterprise_SnsClient extends W3_Enterprise_SnsBase {
  10. private $messages = array();
  11. private $messages_by_signature = array();
  12. private $send_action_configured = false;
  13. /**
  14. * Sends subscription request
  15. */
  16. function subscribe($url, $topic_arn) {
  17. $this->_log('Sending subscription to ' . $topic_arn . ' ' . $url);
  18. $response = $this->_get_api()->subscribe($topic_arn, 'http', $url);
  19. if (!$response->isOK())
  20. throw new Exception('Subscription failed');
  21. }
  22. /**
  23. * Flushes DB caches
  24. *
  25. */
  26. function dbcache_flush() {
  27. $this->_prepare_message(array('action' => 'dbcache_flush'));
  28. }
  29. /**
  30. * Flushes minify caches
  31. *
  32. */
  33. function minifycache_flush() {
  34. $this->_prepare_message(array('action' => 'minifycache_flush'));
  35. }
  36. /**
  37. * Flushes object caches
  38. *
  39. */
  40. function objectcache_flush() {
  41. $this->_prepare_message(array('action' => 'objectcache_flush'));
  42. }
  43. /**
  44. * Flushes fragment caches
  45. *
  46. */
  47. function fragmentcache_flush() {
  48. $this->_prepare_message(array('action' => 'fragmentcache_flush'));
  49. }
  50. /**
  51. * Flushes fragment cache based on group
  52. *
  53. */
  54. function fragmentcache_flush_group($group, $global = false) {
  55. $this->_prepare_message(array('action' => 'fragmentcache_flush_group', 'group' => $group, 'global' => $global));
  56. }
  57. /**
  58. * Flushes query string
  59. *
  60. */
  61. function browsercache_flush() {
  62. $this->_prepare_message(array('action' => 'browsercache_flush'));
  63. }
  64. /**
  65. * Purges Files from Varnish (If enabled) and CDN
  66. *
  67. */
  68. function cdn_purge_files($purgefiles) {
  69. $this->_prepare_message(array('action' => 'cdn_purge_files', 'purgefiles' => $purgefiles));
  70. }
  71. /**
  72. * Flushes all caches
  73. * @return boolean
  74. */
  75. function pgcache_flush() {
  76. return $this->_prepare_message(array('action' => 'pgcache_flush'));
  77. }
  78. /**
  79. * Flushes post cache
  80. *
  81. * @param integer $post_id
  82. * @return boolean
  83. */
  84. function pgcache_flush_post($post_id) {
  85. return $this->_prepare_message(
  86. array('action' => 'pgcache_flush_post', 'post_id' => $post_id));
  87. }
  88. /**
  89. * Flushes post cache
  90. *
  91. * @param string $url
  92. * @return boolean
  93. */
  94. function pgcache_flush_url($url) {
  95. return $this->_prepare_message(
  96. array('action' => 'pgcache_flush_url', 'url' => $url));
  97. }
  98. /**
  99. * Performs garbage collection on the pgcache
  100. */
  101. function pgcache_cleanup() {
  102. $this->_prepare_message(array('action' => 'pgcache_cleanup'));
  103. }
  104. /**
  105. * Purges post from varnish cache
  106. * @param $post_id
  107. * @return mixed
  108. */
  109. function varnish_flush_post($post_id) {
  110. return $this->_prepare_message(array('action' => 'varnish_flush_post', 'post_id' => $post_id));
  111. }
  112. /**
  113. * Purges url from varnish cache
  114. * @param string $url
  115. * @return mixed
  116. */
  117. function varnish_flush_url($url) {
  118. return $this->_prepare_message(array('action' => 'varnish_flush_url', 'url' => $url));
  119. }
  120. /**
  121. * Purges varnish cache
  122. * @return mixed
  123. * @return boolean
  124. */
  125. function varnish_flush() {
  126. return $this->_prepare_message(array('action' => 'varnish_flush'));
  127. }
  128. /**
  129. * Purges post from CDN cache
  130. * @param $post_id
  131. * @return boolean
  132. */
  133. function cdncache_purge_post($post_id) {
  134. return $this->_prepare_message(array('action' => 'cdncache_purge_post', 'post_id' => $post_id));
  135. }
  136. /**
  137. * Purge CDN cache
  138. */
  139. function cdncache_purge() {
  140. return $this->_prepare_message(array('action' => 'cdncache_purge'));
  141. }
  142. /**
  143. * Purges post from CDN cache
  144. * @param $url
  145. * @return boolean
  146. */
  147. function cdncache_purge_url($url) {
  148. return $this->_prepare_message(array('action' => 'cdncache_purge_url', 'url' => $url));
  149. }
  150. /**
  151. * Flushes the system APC
  152. * @return bool
  153. */
  154. function apc_system_flush() {
  155. $this->_prepare_message(array('action' => 'apc_system_flush'));
  156. }
  157. /**
  158. * Reloads/compiles a PHP file.
  159. * @param string $filename
  160. * @return mixed
  161. */
  162. function apc_reload_file($filename) {
  163. return $this->_prepare_message(array('action' => 'apc_reload_file', 'filename' => $filename));
  164. }
  165. /**
  166. * Reloads/compiles a PHP file.
  167. * @param string[] $filenames
  168. * @return mixed
  169. */
  170. function apc_reload_files($filenames) {
  171. return $this->_prepare_message(array('action' => 'apc_reload_files', 'filenames' => $filenames));
  172. }
  173. /**
  174. * Deletes files based on regular expression matching.
  175. * @param string $mask
  176. * @return mixed
  177. */
  178. function apc_delete_files_based_on_regex($mask) {
  179. return $this->_prepare_message(array('action' => 'apc_delete_files_based_on_regex', 'regex' => $mask));
  180. }
  181. /**
  182. * Purges/Flushes post from page caches, varnish and cdncache
  183. * @param $post_id
  184. * @return boolean
  185. */
  186. function flush_post($post_id) {
  187. return $this->_prepare_message(array('action' => 'flush_post', 'post_id' => $post_id));
  188. }
  189. /**
  190. * Purges/Flushes page caches, varnish and cdncache
  191. * @return boolean
  192. */
  193. function flush() {
  194. return $this->_prepare_message(array('action' => 'flush'));
  195. }
  196. /**
  197. * Purges/Flushes all enabled caches
  198. * @return boolean
  199. */
  200. function flush_all() {
  201. return $this->_prepare_message(array('action' => 'flush_all'));
  202. }
  203. /**
  204. * Purges/Flushes url from page caches, varnish and cdncache
  205. * @param string $url
  206. * @return boolean
  207. */
  208. function flush_url($url) {
  209. return $this->_prepare_message(array('action' => 'flush_url', 'url' => $url));
  210. }
  211. /**
  212. * Makes get request to url specific to post, ie permalinks
  213. * @param $post_id
  214. * @return mixed
  215. */
  216. function prime_post($post_id) {
  217. return $this->_prepare_message(array('action' => 'prime_post', 'post_id' => $post_id));
  218. }
  219. /**
  220. * Setups message list and if it should be combined or separate
  221. * @param $message
  222. * @return boolean
  223. */
  224. private function _prepare_message($message) {
  225. $message_signature = json_encode($message);
  226. if (isset($this->messages_by_signature[$message_signature]))
  227. return true;
  228. $this->messages_by_signature[$message_signature] = '*';
  229. $this->messages[] = $message;
  230. $action = $this->_get_action();
  231. if (!$action) {
  232. $this->send_messages();
  233. return true;
  234. }
  235. if (!$this->send_action_configured) {
  236. add_action('w3_redirect', array(
  237. &$this,
  238. 'send_messages_w3_redirect'
  239. ), 100000, 0);
  240. add_filter('wp_redirect', array(
  241. &$this,
  242. 'send_messages_wp_redirect'
  243. ), 100000, 1);
  244. add_action('shutdown', array(
  245. &$this,
  246. 'send_messages'
  247. ), 100000, 0);
  248. $this->send_action_configured = true;
  249. }
  250. return true;
  251. }
  252. /**
  253. * Sends messages stored in $messages
  254. *
  255. * @return boolean
  256. */
  257. public function send_messages() {
  258. if (count($this->messages) <= 0)
  259. return true;
  260. $this->_log($this->_get_action() . ' sending messages');
  261. $message = array();
  262. $message['actions'] = $this->messages;
  263. $message['blog_id'] = w3_get_blog_id();
  264. $message['host'] = isset($_SERVER['HTTP_HOST']) ? $_SERVER['HTTP_HOST'] : null;
  265. $message['hostname'] = @gethostname();
  266. $v = json_encode($message);
  267. try {
  268. $api = $this->_get_api();
  269. if (defined('WP_CLI') && WP_CLI)
  270. $origin = 'WP CLI';
  271. else
  272. $origin = 'WP';
  273. $this->_log($origin . ' sending message ' . $v);
  274. $this->_log('Host: ' . $message['host']);
  275. if (isset($_SERVER['REQUEST_URI']))
  276. $this->_log('URL: ' . $_SERVER['REQUEST_URI']);
  277. if (function_exists('current_filter'))
  278. $this->_log('Current WP hook: ' . current_filter());
  279. $backtrace = debug_backtrace();
  280. $backtrace_optimized = array();
  281. foreach ($backtrace as $b) {
  282. $opt = isset($b['function']) ? $b['function'] . ' ' : '';
  283. $opt .= isset($b['file']) ? $b['file'] . ' ' : '';
  284. $opt .= isset($b['line']) ? '#' . $b['line'] . ' ' : '';
  285. $backtrace_optimized[] = $opt;
  286. }
  287. $this->_log('Backtrace ', $backtrace_optimized);
  288. $r = $api->publish($this->_topic_arn, $v);
  289. if ($r->status != 200) {
  290. $this->_log("Error: {$r->body->Error->Message}");
  291. return false;
  292. }
  293. } catch (Exception $e) {
  294. $this->_log('Error ' . $e->getMessage());
  295. return false;
  296. }
  297. // on success - reset messages array, but not hash (not resent repeatedly the same messages)
  298. $this->messages = array();
  299. return true;
  300. }
  301. /**
  302. * Send messages on wp_redirect
  303. * @param $location
  304. * @return mixed
  305. */
  306. public function send_messages_wp_redirect($location) {
  307. $this->_log($this->_get_action() . ' sending messages wp_redirect');
  308. $this->send_messages();
  309. return $location;
  310. }
  311. /**
  312. * Send messages on w3_redirect
  313. */
  314. public function send_messages_w3_redirect() {
  315. $this->_log($this->_get_action() . ' sending messages w3_redirect');
  316. $this->send_messages();
  317. }
  318. /**
  319. * Gets the current running WP action if any. Returns empty string if not found.
  320. * @return string
  321. */
  322. private function _get_action() {
  323. $action = '';
  324. if (function_exists('current_filter'))
  325. $action = current_filter();
  326. return $action;
  327. }
  328. }