PageRenderTime 38ms CodeModel.GetById 11ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://gitlab.com/juanito.abelo/nlmobile
PHP | 178 lines | 90 code | 35 blank | 53 comment | 15 complexity | f7087c696bdaece09656549de5e634ad MD5 | raw file
  1. <?php
  2. /**
  3. * Varnish purge object
  4. */
  5. /**
  6. * Class W3_Varnish
  7. */
  8. class W3_Varnish {
  9. /**
  10. * Debug flag
  11. *
  12. * @var bool
  13. */
  14. var $_debug = false;
  15. /**
  16. * Varnish servers
  17. *
  18. * @var array
  19. */
  20. var $_servers = array();
  21. /**
  22. * Operation timeout
  23. *
  24. * @var int
  25. */
  26. var $_timeout = 30;
  27. /**
  28. * Advanced cache config
  29. *
  30. * @var W3_Config
  31. */
  32. var $_config = null;
  33. /**
  34. * PHP5-style constructor
  35. */
  36. function __construct() {
  37. $this->_config = w3_instance('W3_Config');
  38. $this->_debug = $this->_config->get_boolean('varnish.debug');
  39. $this->_servers = $this->_config->get_array('varnish.servers');
  40. $this->_timeout = $this->_config->get_integer('timelimit.varnish_purge');
  41. }
  42. /**
  43. * Purge URI
  44. *
  45. * @param string $url
  46. * @return boolean
  47. */
  48. protected function _purge($url) {
  49. w3_require_once(W3TC_INC_DIR . '/functions/http.php');
  50. @set_time_limit($this->_timeout);
  51. foreach ((array) $this->_servers as $server) {
  52. $response = $this->_request($server, $url);
  53. if (is_wp_error($response)) {
  54. $this->_log($url, sprintf('Unable to send request: %s.', implode('; ', $response->get_error_messages())));
  55. return false;
  56. }
  57. if ($response['response']['code'] !== 200) {
  58. $this->_log($url, 'Bad response code: ' . $response['response']['code']);
  59. return false;
  60. }
  61. $this->_log($url, 'PURGE OK');
  62. }
  63. return true;
  64. }
  65. /*
  66. * Sends purge request. Cannt use default wp HTTP implementation
  67. * if we send request to different host than specified in $url
  68. *
  69. * @param $url string
  70. */
  71. function _request($varnish_server, $url) {
  72. $parse_url = @parse_url($url);
  73. if (!$parse_url || !isset($parse_url['host']))
  74. return new WP_Error('http_request_failed', 'Unrecognized URL format ' . $url);
  75. $host = $parse_url['host'];
  76. $port = (isset($parse_url['port']) ? (int) $parse_url['port'] : 80);
  77. $path = (!empty($parse_url['path']) ? $parse_url['path'] : '/');
  78. $query = (isset($parse_url['query']) ? $parse_url['query'] : '');
  79. $request_uri = $path . ($query != '' ? '?' . $query : '');
  80. if (strpos($varnish_server, ':'))
  81. list($varnish_host, $varnish_port) = explode(':', $varnish_server);
  82. else {
  83. $varnish_host = $varnish_server;
  84. $varnish_port = 80;
  85. }
  86. // if url host is the same as varnish server - we can use regular
  87. // wordpress http infrastructure, otherwise custom request should be
  88. // sent using fsockopen, since we send request to other server than
  89. // specified by $url
  90. if ($host == $varnish_host && $port == $varnish_port)
  91. return w3_http_request($url, array('method' => 'PURGE'));
  92. $request_headers_array = array(
  93. sprintf('PURGE %s HTTP/1.1', $request_uri),
  94. sprintf('Host: %s', $host),
  95. sprintf('User-Agent: %s', W3TC_POWERED_BY),
  96. 'Connection: close'
  97. );
  98. $request_headers = implode("\r\n", $request_headers_array);
  99. $request = $request_headers . "\r\n\r\n";
  100. // log what we are about to do
  101. $this->_log($url, sprintf('Connecting to %s ...', $varnish_host));
  102. $this->_log($url, sprintf('PURGE %s HTTP/1.1', $request_uri));
  103. $this->_log($url, sprintf('Host: %s', $host));
  104. $errno = null;
  105. $errstr = null;
  106. $fp = @fsockopen($varnish_host, $varnish_port, $errno, $errstr, 10);
  107. if (!$fp)
  108. return new WP_Error('http_request_failed', $errno . ': ' . $errstr);
  109. @stream_set_timeout($fp, 60);
  110. @fputs($fp, $request);
  111. $response = '';
  112. while (!@feof($fp))
  113. $response .= @fgets($fp, 4096);
  114. @fclose($fp);
  115. list($response_headers, $contents) = explode("\r\n\r\n", $response, 2);
  116. $matches = null;
  117. if (preg_match('~^HTTP/1.[01] (\d+)~', $response_headers, $matches)) {
  118. $status = (int)$matches[1];
  119. $return = array(
  120. 'response' => array(
  121. 'code' => $status));
  122. return $return;
  123. }
  124. return new WP_Error('http_request_failed',
  125. 'Unrecognized response header' . $response_headers);
  126. }
  127. /**
  128. * Write log entry
  129. *
  130. * @param string $url
  131. * @param string $msg
  132. * @return bool|int
  133. */
  134. function _log($url, $msg) {
  135. if ($this->_debug) {
  136. $data = sprintf("[%s] [%s] %s\n", date('r'), $url, $msg);
  137. $data = strtr($data, '<>', '..');
  138. $filename = w3_debug_log('varnish');
  139. return @file_put_contents($filename, $data, FILE_APPEND);
  140. }
  141. return true;
  142. }
  143. }