/**
  * Makes ip_lkup API request
  *
  * @param string  $ip
  * @return object
  */
 public function ip_lkup($ip)
 {
     @set_time_limit($this->_timelimit_api_request);
     $response = Util_Http::request('https://www.cloudflare.com/api_json.html', array('method' => 'POST', 'body' => array('a' => 'ip_lkup', 'tkn' => $this->_key, 'email' => $this->_email, 'ip' => $ip)));
     if (isset($response['body'])) {
         return @json_decode($response['body']);
     }
     return null;
 }
Пример #2
0
 function _request($varnish_server, $url)
 {
     $parse_url = @parse_url($url);
     if (!$parse_url || !isset($parse_url['host'])) {
         return new \WP_Error('http_request_failed', 'Unrecognized URL format ' . $url);
     }
     $host = $parse_url['host'];
     $port = isset($parse_url['port']) ? (int) $parse_url['port'] : 80;
     $path = !empty($parse_url['path']) ? $parse_url['path'] : '/';
     $query = isset($parse_url['query']) ? $parse_url['query'] : '';
     $request_uri = $path . ($query != '' ? '?' . $query : '');
     if (strpos($varnish_server, ':')) {
         list($varnish_host, $varnish_port) = explode(':', $varnish_server);
     } else {
         $varnish_host = $varnish_server;
         $varnish_port = 80;
     }
     // if url host is the same as varnish server - we can use regular
     // wordpress http infrastructure, otherwise custom request should be
     // sent using fsockopen, since we send request to other server than
     // specified by $url
     if ($host == $varnish_host && $port == $varnish_port) {
         return Util_Http::request($url, array('method' => 'PURGE'));
     }
     $request_headers_array = array(sprintf('PURGE %s HTTP/1.1', $request_uri), sprintf('Host: %s', $host), sprintf('User-Agent: %s', W3TC_POWERED_BY), 'Connection: close');
     $request_headers = implode("\r\n", $request_headers_array);
     $request = $request_headers . "\r\n\r\n";
     // log what we are about to do
     $this->_log($url, sprintf('Connecting to %s ...', $varnish_host));
     $this->_log($url, sprintf('PURGE %s HTTP/1.1', $request_uri));
     $this->_log($url, sprintf('Host: %s', $host));
     $errno = null;
     $errstr = null;
     $fp = @fsockopen($varnish_host, $varnish_port, $errno, $errstr, 10);
     if (!$fp) {
         return new \WP_Error('http_request_failed', $errno . ': ' . $errstr);
     }
     @stream_set_timeout($fp, 60);
     @fputs($fp, $request);
     $response = '';
     while (!@feof($fp)) {
         $response .= @fgets($fp, 4096);
     }
     @fclose($fp);
     list($response_headers, $contents) = explode("\r\n\r\n", $response, 2);
     $matches = null;
     if (preg_match('~^HTTP/1.[01] (\\d+)~', $response_headers, $matches)) {
         $code = (int) $matches[1];
         $a = explode("\n", $response_headers);
         $status = count($a) >= 1 ? $a[0] : '';
         $return = array('response' => array('code' => $code, 'status' => $status));
         return $return;
     }
     return new \WP_Error('http_request_failed', 'Unrecognized response header' . $response_headers);
 }