WP_Http_Streams

Core class used to integrate PHP Streams as an HTTP transport.

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_Streams { 
  2. /** 
  3. * Send a HTTP request to a URI using streams with fopen(). 
  4. * @access public 
  5. * @since 2.7.0 
  6. * @param string $url 
  7. * @param str|array $args Optional. Override the defaults. 
  8. * @return array 'headers', 'body', 'cookies' and 'response' keys. 
  9. */ 
  10. function request($url, $args = array()) { 
  11. $defaults = array( 
  12. 'method' => 'GET', 'timeout' => 5,  
  13. 'redirection' => 5, 'httpversion' => '1.0',  
  14. 'blocking' => true,  
  15. 'headers' => array(), 'body' => null, 'cookies' => array() 
  16. ); 
  17.  
  18. $r = wp_parse_args( $args, $defaults ); 
  19.  
  20. if ( isset($r['headers']['User-Agent']) ) { 
  21. $r['user-agent'] = $r['headers']['User-Agent']; 
  22. unset($r['headers']['User-Agent']); 
  23. } else if( isset($r['headers']['user-agent']) ) { 
  24. $r['user-agent'] = $r['headers']['user-agent']; 
  25. unset($r['headers']['user-agent']); 
  26.  
  27. // Construct Cookie: header if any cookies are set 
  28. WP_Http::buildCookieHeader( $r ); 
  29.  
  30. $arrURL = parse_url($url); 
  31.  
  32. if ( false === $arrURL ) 
  33. return new WP_Error('http_request_failed', sprintf(__('Malformed URL: %s'), $url)); 
  34.  
  35. if ( 'http' != $arrURL['scheme'] && 'https' != $arrURL['scheme'] ) 
  36. $url = preg_replace('|^' . preg_quote($arrURL['scheme'], '|') . '|', 'http', $url); 
  37.  
  38. // Convert Header array to string. 
  39. $strHeaders = ''; 
  40. if ( is_array( $r['headers'] ) ) 
  41. foreach ( $r['headers'] as $name => $value ) 
  42. $strHeaders .= "{$name}: $value\r\n"; 
  43. else if ( is_string( $r['headers'] ) ) 
  44. $strHeaders = $r['headers']; 
  45.  
  46. $is_local = isset($args['local']) && $args['local']; 
  47. $ssl_verify = isset($args['sslverify']) && $args['sslverify']; 
  48. if ( $is_local ) 
  49. $ssl_verify = apply_filters('https_local_ssl_verify', $ssl_verify); 
  50. elseif ( ! $is_local ) 
  51. $ssl_verify = apply_filters('https_ssl_verify', $ssl_verify); 
  52.  
  53. $arrContext = array('http' => 
  54. array( 
  55. 'method' => strtoupper($r['method']),  
  56. 'user_agent' => $r['user-agent'],  
  57. 'max_redirects' => $r['redirection'],  
  58. 'protocol_version' => (float) $r['httpversion'],  
  59. 'header' => $strHeaders,  
  60. 'timeout' => $r['timeout'],  
  61. 'ssl' => array( 
  62. 'verify_peer' => $ssl_verify,  
  63. 'verify_host' => $ssl_verify 
  64. ); 
  65.  
  66. $proxy = new WP_HTTP_Proxy(); 
  67.  
  68. if ( $proxy->is_enabled() && $proxy->send_through_proxy( $url ) ) { 
  69. $arrContext['http']['proxy'] = 'tcp://' . $proxy->host() . ':' . $proxy->port(); 
  70. $arrContext['http']['request_fulluri'] = true; 
  71.  
  72. // We only support Basic authentication so this will only work if that is what your proxy supports. 
  73. if ( $proxy->use_authentication() ) 
  74. $arrContext['http']['header'] .= $proxy->authentication_header() . "\r\n"; 
  75.  
  76. if ( ! is_null($r['body']) && ! empty($r['body'] ) ) 
  77. $arrContext['http']['content'] = $r['body']; 
  78.  
  79. $context = stream_context_create($arrContext); 
  80.  
  81. if ( ! defined('WP_DEBUG') || ( defined('WP_DEBUG') && false === WP_DEBUG ) ) 
  82. $handle = @fopen($url, 'r', false, $context); 
  83. else 
  84. $handle = fopen($url, 'r', false, $context); 
  85.  
  86. if ( ! $handle) 
  87. return new WP_Error('http_request_failed', sprintf(__('Could not open handle for fopen() to %s'), $url)); 
  88.  
  89. // WordPress supports PHP 4.3, which has this function. Removed sanity checking for 
  90. // performance reasons. 
  91. stream_set_timeout($handle, $r['timeout'] ); 
  92.  
  93. if ( ! $r['blocking'] ) { 
  94. stream_set_blocking($handle, 0); 
  95. fclose($handle); 
  96. return array( 'headers' => array(), 'body' => '', 'response' => array('code' => false, 'message' => false), 'cookies' => array() ); 
  97.  
  98. $strResponse = stream_get_contents($handle); 
  99. $meta = stream_get_meta_data($handle); 
  100.  
  101. fclose($handle); 
  102.  
  103. $processedHeaders = array(); 
  104. if ( isset( $meta['wrapper_data']['headers'] ) ) 
  105. $processedHeaders = WP_Http::processHeaders($meta['wrapper_data']['headers']); 
  106. else 
  107. $processedHeaders = WP_Http::processHeaders($meta['wrapper_data']); 
  108.  
  109. if ( ! empty( $strResponse ) && isset( $processedHeaders['headers']['transfer-encoding'] ) && 'chunked' == $processedHeaders['headers']['transfer-encoding'] ) 
  110. $strResponse = WP_Http::chunkTransferDecode($strResponse); 
  111.  
  112. if ( true === $r['decompress'] && true === WP_Http_Encoding::should_decode($processedHeaders['headers']) ) 
  113. $strResponse = WP_Http_Encoding::decompress( $strResponse ); 
  114.  
  115. return array('headers' => $processedHeaders['headers'], 'body' => $strResponse, 'response' => $processedHeaders['response'], 'cookies' => $processedHeaders['cookies']); 
  116.  
  117. /** 
  118. * Whether this class can be used for retrieving an URL. 
  119. * @static 
  120. * @access public 
  121. * @since 2.7.0 
  122. * @return boolean False means this class can not be used, true means it can. 
  123. */ 
  124. function test($args = array()) { 
  125. if ( ! function_exists('fopen') || (function_exists('ini_get') && true != ini_get('allow_url_fopen')) ) 
  126. return false; 
  127.  
  128. if ( version_compare(PHP_VERSION, '5.0', '<') ) 
  129. return false; 
  130.  
  131. //HTTPS via Proxy was added in 5.1.0 
  132. $is_ssl = isset($args['ssl']) && $args['ssl']; 
  133. if ( $is_ssl && version_compare(PHP_VERSION, '5.1.0', '<') ) { 
  134. $proxy = new WP_HTTP_Proxy(); 
  135. /** 
  136. * No URL check, as its not currently passed to the ::test() function 
  137. * In the case where a Proxy is in use, Just bypass this transport for HTTPS. 
  138. */ 
  139. if ( $proxy->is_enabled() ) 
  140. return false; 
  141.  
  142. return apply_filters('use_streams_transport', true, $args);