WP_HTTP_Fsockopen

Deprecated HTTP Transport method which used fsockopen.

Defined (1)

The class is defined in the following location(s).

/bp-forums/bbpress/bb-includes/backpress/class.wp-http.php  
  1. class WP_Http_Fsockopen { 
  2. /** 
  3. * Send a HTTP request to a URI using fsockopen(). 
  4. * Does not support non-blocking mode. 
  5. * @see WP_Http::request For default options descriptions. 
  6. * @since 2.7 
  7. * @access public 
  8. * @param string $url URI resource. 
  9. * @param str|array $args Optional. Override the defaults. 
  10. * @return array 'headers', 'body', 'cookies' and 'response' keys. 
  11. */ 
  12. function request($url, $args = array()) { 
  13. $defaults = array( 
  14. 'method' => 'GET', 'timeout' => 5,  
  15. 'redirection' => 5, 'httpversion' => '1.0',  
  16. 'blocking' => true,  
  17. 'headers' => array(), 'body' => null, 'cookies' => array() 
  18. ); 
  19.  
  20. $r = wp_parse_args( $args, $defaults ); 
  21.  
  22. if ( isset($r['headers']['User-Agent']) ) { 
  23. $r['user-agent'] = $r['headers']['User-Agent']; 
  24. unset($r['headers']['User-Agent']); 
  25. } else if( isset($r['headers']['user-agent']) ) { 
  26. $r['user-agent'] = $r['headers']['user-agent']; 
  27. unset($r['headers']['user-agent']); 
  28.  
  29. // Construct Cookie: header if any cookies are set 
  30. WP_Http::buildCookieHeader( $r ); 
  31.  
  32. $iError = null; // Store error number 
  33. $strError = null; // Store error string 
  34.  
  35. $arrURL = parse_url($url); 
  36.  
  37. $fsockopen_host = $arrURL['host']; 
  38.  
  39. $secure_transport = false; 
  40.  
  41. if ( ! isset( $arrURL['port'] ) ) { 
  42. if ( ( $arrURL['scheme'] == 'ssl' || $arrURL['scheme'] == 'https' ) && extension_loaded('openssl') ) { 
  43. $fsockopen_host = "ssl://$fsockopen_host"; 
  44. $arrURL['port'] = 443; 
  45. $secure_transport = true; 
  46. } else { 
  47. $arrURL['port'] = 80; 
  48.  
  49. // There are issues with the HTTPS and SSL protocols that cause errors that can be safely 
  50. // ignored and should be ignored. 
  51. if ( true === $secure_transport ) 
  52. $error_reporting = error_reporting(0); 
  53.  
  54. $startDelay = time(); 
  55.  
  56. $proxy = new WP_HTTP_Proxy(); 
  57.  
  58. if ( !defined('WP_DEBUG') || ( defined('WP_DEBUG') && false === WP_DEBUG ) ) { 
  59. if ( $proxy->is_enabled() && $proxy->send_through_proxy( $url ) ) 
  60. $handle = @fsockopen( $proxy->host(), $proxy->port(), $iError, $strError, $r['timeout'] ); 
  61. else 
  62. $handle = @fsockopen( $fsockopen_host, $arrURL['port'], $iError, $strError, $r['timeout'] ); 
  63. } else { 
  64. if ( $proxy->is_enabled() && $proxy->send_through_proxy( $url ) ) 
  65. $handle = fsockopen( $proxy->host(), $proxy->port(), $iError, $strError, $r['timeout'] ); 
  66. else 
  67. $handle = fsockopen( $fsockopen_host, $arrURL['port'], $iError, $strError, $r['timeout'] ); 
  68.  
  69. $endDelay = time(); 
  70.  
  71. // If the delay is greater than the timeout then fsockopen should't be used, because it will 
  72. // cause a long delay. 
  73. $elapseDelay = ($endDelay-$startDelay) > $r['timeout']; 
  74. if ( true === $elapseDelay ) 
  75. backpress_add_option( 'disable_fsockopen', $endDelay, null, true ); 
  76.  
  77. if ( false === $handle ) 
  78. return new WP_Error('http_request_failed', $iError . ': ' . $strError); 
  79.  
  80. stream_set_timeout($handle, $r['timeout'] ); 
  81.  
  82. if ( $proxy->is_enabled() && $proxy->send_through_proxy( $url ) ) //Some proxies require full URL in this field. 
  83. $requestPath = $url; 
  84. else 
  85. $requestPath = $arrURL['path'] . ( isset($arrURL['query']) ? '?' . $arrURL['query'] : '' ); 
  86.  
  87. if ( empty($requestPath) ) 
  88. $requestPath .= '/'; 
  89.  
  90. $strHeaders = strtoupper($r['method']) . ' ' . $requestPath . ' HTTP/' . $r['httpversion'] . "\r\n"; 
  91.  
  92. if ( $proxy->is_enabled() && $proxy->send_through_proxy( $url ) ) 
  93. $strHeaders .= 'Host: ' . $arrURL['host'] . ':' . $arrURL['port'] . "\r\n"; 
  94. else 
  95. $strHeaders .= 'Host: ' . $arrURL['host'] . "\r\n"; 
  96.  
  97. if ( isset($r['user-agent']) ) 
  98. $strHeaders .= 'User-agent: ' . $r['user-agent'] . "\r\n"; 
  99.  
  100. if ( is_array($r['headers']) ) { 
  101. foreach ( (array) $r['headers'] as $header => $headerValue ) 
  102. $strHeaders .= $header . ': ' . $headerValue . "\r\n"; 
  103. } else { 
  104. $strHeaders .= $r['headers']; 
  105.  
  106. if ( $proxy->use_authentication() ) 
  107. $strHeaders .= $proxy->authentication_header() . "\r\n"; 
  108.  
  109. $strHeaders .= "\r\n"; 
  110.  
  111. if ( ! is_null($r['body']) ) 
  112. $strHeaders .= $r['body']; 
  113.  
  114. fwrite($handle, $strHeaders); 
  115.  
  116. if ( ! $r['blocking'] ) { 
  117. fclose($handle); 
  118. return array( 'headers' => array(), 'body' => '', 'response' => array('code' => false, 'message' => false), 'cookies' => array() ); 
  119.  
  120. $strResponse = ''; 
  121. while ( ! feof($handle) ) 
  122. $strResponse .= fread($handle, 4096); 
  123.  
  124. fclose($handle); 
  125.  
  126. if ( true === $secure_transport ) 
  127. error_reporting($error_reporting); 
  128.  
  129. $process = WP_Http::processResponse($strResponse); 
  130. $arrHeaders = WP_Http::processHeaders($process['headers']); 
  131.  
  132. // Is the response code within the 400 range? 
  133. if ( (int) $arrHeaders['response']['code'] >= 400 && (int) $arrHeaders['response']['code'] < 500 ) 
  134. return new WP_Error('http_request_failed', $arrHeaders['response']['code'] . ': ' . $arrHeaders['response']['message']); 
  135.  
  136. // If location is found, then assume redirect and redirect to location. 
  137. if ( isset($arrHeaders['headers']['location']) ) { 
  138. if ( $r['redirection']-- > 0 ) { 
  139. return $this->request($arrHeaders['headers']['location'], $r); 
  140. } else { 
  141. return new WP_Error('http_request_failed', __('Too many redirects.')); 
  142.  
  143. // If the body was chunk encoded, then decode it. 
  144. if ( ! empty( $process['body'] ) && isset( $arrHeaders['headers']['transfer-encoding'] ) && 'chunked' == $arrHeaders['headers']['transfer-encoding'] ) 
  145. $process['body'] = WP_Http::chunkTransferDecode($process['body']); 
  146.  
  147. if ( true === $r['decompress'] && true === WP_Http_Encoding::should_decode($arrHeaders['headers']) ) 
  148. $process['body'] = WP_Http_Encoding::decompress( $process['body'] ); 
  149.  
  150. return array('headers' => $arrHeaders['headers'], 'body' => $process['body'], 'response' => $arrHeaders['response'], 'cookies' => $arrHeaders['cookies']); 
  151.  
  152. /** 
  153. * Whether this class can be used for retrieving an URL. 
  154. * @since 2.7.0 
  155. * @static 
  156. * @return boolean False means this class can not be used, true means it can. 
  157. */ 
  158. function test( $args = array() ) { 
  159. if ( false !== ($option = backpress_get_option( 'disable_fsockopen' )) && time()-$option < 43200 ) // 12 hours 
  160. return false; 
  161.  
  162. $is_ssl = isset($args['ssl']) && $args['ssl']; 
  163.  
  164. if ( ! $is_ssl && function_exists( 'fsockopen' ) ) 
  165. $use = true; 
  166. elseif ( $is_ssl && extension_loaded('openssl') && function_exists( 'fsockopen' ) ) 
  167. $use = true; 
  168. else 
  169. $use = false; 
  170.  
  171. return apply_filters('use_fsockopen_transport', $use, $args);