| 1 | <?php |
|---|
| 2 | /** |
|---|
| 3 | * Simple HTTP request fallback system |
|---|
| 4 | * |
|---|
| 5 | * @package WordPress |
|---|
| 6 | * @subpackage HTTP |
|---|
| 7 | * @since {@internal Version Unknown}} |
|---|
| 8 | * @author Jacob Santos <wordpress@santosj.name> |
|---|
| 9 | */ |
|---|
| 10 | |
|---|
| 11 | /** |
|---|
| 12 | * Abstract class for all of the fallback implementation |
|---|
| 13 | * classes. The implementation classes will extend this class |
|---|
| 14 | * to keep common API methods universal between different |
|---|
| 15 | * functionality. |
|---|
| 16 | * |
|---|
| 17 | * @package WordPress |
|---|
| 18 | * @subpackage HTTP |
|---|
| 19 | * @since {@internal Version Unknown}} |
|---|
| 20 | */ |
|---|
| 21 | class WP_Http |
|---|
| 22 | { |
|---|
| 23 | |
|---|
| 24 | /** |
|---|
| 25 | * PHP4 style Constructor - Calls PHP5 Style Constructor |
|---|
| 26 | * |
|---|
| 27 | * @since {@internal Version Unknown}} |
|---|
| 28 | * @return WP_Http |
|---|
| 29 | */ |
|---|
| 30 | function WP_Http() |
|---|
| 31 | { |
|---|
| 32 | $this->__construct(); |
|---|
| 33 | } |
|---|
| 34 | |
|---|
| 35 | /** |
|---|
| 36 | * PHP5 style Constructor - Setup available transport if not available. |
|---|
| 37 | * |
|---|
| 38 | * @since {@internal Version Unknown}} |
|---|
| 39 | * @return WP_Http |
|---|
| 40 | */ |
|---|
| 41 | function __construct() |
|---|
| 42 | { |
|---|
| 43 | WP_Http::_getTransport(); |
|---|
| 44 | } |
|---|
| 45 | |
|---|
| 46 | /** |
|---|
| 47 | * Tests the WordPress HTTP objects for an object to use and returns it. |
|---|
| 48 | * |
|---|
| 49 | * Tests all of the objects and returns the object that passes. Also caches |
|---|
| 50 | * that object to be used later. |
|---|
| 51 | * |
|---|
| 52 | * @since {@internal Version Unknown}} |
|---|
| 53 | * @access private |
|---|
| 54 | * |
|---|
| 55 | * @return object|null Null if no transports are available, HTTP transport object. |
|---|
| 56 | */ |
|---|
| 57 | function &_getTransport() |
|---|
| 58 | { |
|---|
| 59 | static $working_transport; |
|---|
| 60 | |
|---|
| 61 | if( is_null($working_transport) ) { |
|---|
| 62 | if( true === WP_Http_Streams::test() ) |
|---|
| 63 | $working_transport = new WP_Http_Streams(); |
|---|
| 64 | else if( true === WP_Http_Fopen::test() ) |
|---|
| 65 | $working_transport = new WP_Http_Fopen(); |
|---|
| 66 | else if( true === WP_Http_ExtHttp::test() ) |
|---|
| 67 | $working_transport = new WP_Http_ExtHttp(); |
|---|
| 68 | else if( true === WP_Http_Fsockopen::test() ) |
|---|
| 69 | $working_transport = new WP_Http_Fsockopen(); |
|---|
| 70 | } |
|---|
| 71 | |
|---|
| 72 | return $working_transport; |
|---|
| 73 | } |
|---|
| 74 | |
|---|
| 75 | /** |
|---|
| 76 | * Retrieve the location and set the class properties after the site has been retrieved. |
|---|
| 77 | * |
|---|
| 78 | * @access public |
|---|
| 79 | * @since {@internal Version Unknown}} |
|---|
| 80 | * |
|---|
| 81 | * @param string $url |
|---|
| 82 | * @param string|array $headers Optional. Either the header string or array of Header name and value pairs. Expects sanitized. |
|---|
| 83 | * @param string $body Optional. The body that should be sent. Expected to be already processed. |
|---|
| 84 | * @param str|array $type Optional. Should be an already processed array with HTTP arguments. |
|---|
| 85 | * @return boolean |
|---|
| 86 | */ |
|---|
| 87 | function request($url, $headers=null, $body=null, $args=array()) |
|---|
| 88 | { |
|---|
| 89 | global $wp_version; |
|---|
| 90 | |
|---|
| 91 | $defaults = array( |
|---|
| 92 | 'method' => 'GET', 'timeout' => 3, |
|---|
| 93 | 'redirection' => 5, 'redirected' => false, |
|---|
| 94 | 'httpversion' => '1.0' |
|---|
| 95 | ); |
|---|
| 96 | |
|---|
| 97 | $r = wp_parse_args( $args, $defaults ); |
|---|
| 98 | |
|---|
| 99 | if( !is_null($headers) ) { |
|---|
| 100 | $processedHeaders = WP_Http::processHeaders($headers); |
|---|
| 101 | $headers = $processedHeaders['headers']; |
|---|
| 102 | |
|---|
| 103 | if( !isset($headers['user-agent']) ) |
|---|
| 104 | $headers['user-agent'] = apply_filters('http_headers_useragent', 'WordPress/'.$wp_version ); |
|---|
| 105 | } else { |
|---|
| 106 | $headers = array(); |
|---|
| 107 | $headers['user-agent'] = apply_filters('http_headers_useragent', 'WordPress/'.$wp_version ); |
|---|
| 108 | } |
|---|
| 109 | |
|---|
| 110 | $transport = WP_Http::_getTransport(); |
|---|
| 111 | return $transport->request($url, $headers, $body, $r); |
|---|
| 112 | } |
|---|
| 113 | |
|---|
| 114 | /** |
|---|
| 115 | * Uses the POST HTTP method. |
|---|
| 116 | * |
|---|
| 117 | * Used for sending data that is expected to be in the body. |
|---|
| 118 | * |
|---|
| 119 | * @access public |
|---|
| 120 | * @since {@internal Version Unknown}} |
|---|
| 121 | * |
|---|
| 122 | * @param string $url The location of the site and page to retrieve. |
|---|
| 123 | * @param string|array $headers Optional. Either the header string or array of Header name and value pairs. |
|---|
| 124 | * @param string $body Optional. The body that should be sent. Expected to be already processed. |
|---|
| 125 | * @return boolean |
|---|
| 126 | */ |
|---|
| 127 | function post($url, $headers=null, $body=null, $args=array()) |
|---|
| 128 | { |
|---|
| 129 | $defaults = array('method' => 'POST'); |
|---|
| 130 | $r = wp_parse_args( $args, $defaults ); |
|---|
| 131 | return $this->request($url, $headers, $body, $r); |
|---|
| 132 | } |
|---|
| 133 | |
|---|
| 134 | /** |
|---|
| 135 | * Uses the GET HTTP method. |
|---|
| 136 | * |
|---|
| 137 | * Used for sending data that is expected to be in the body. |
|---|
| 138 | * |
|---|
| 139 | * @access public |
|---|
| 140 | * @since {@internal Version Unknown}} |
|---|
| 141 | * |
|---|
| 142 | * @param string|array $headers Optional. Either the header string or array of Header name and value pairs. |
|---|
| 143 | * @param string $body Optional. The body that should be sent. Expected to be already processed. |
|---|
| 144 | * @return boolean |
|---|
| 145 | */ |
|---|
| 146 | function get($url, $headers=null, $body=null, $args=array()) |
|---|
| 147 | { |
|---|
| 148 | $defaults = array('method' => 'GET'); |
|---|
| 149 | $r = wp_parse_args( $args, $defaults ); |
|---|
| 150 | return $this->request($url, $headers, $body, $r); |
|---|
| 151 | } |
|---|
| 152 | |
|---|
| 153 | /** |
|---|
| 154 | * Uses the HEAD HTTP method. |
|---|
| 155 | * |
|---|
| 156 | * Used for sending data that is expected to be in the body. |
|---|
| 157 | * |
|---|
| 158 | * @access public |
|---|
| 159 | * @since {@internal Version Unknown}} |
|---|
| 160 | * |
|---|
| 161 | * @param string|array $headers Optional. Either the header string or array of Header name and value pairs. |
|---|
| 162 | * @param string $body Optional. The body that should be sent. Expected to be already processed. |
|---|
| 163 | * @return boolean |
|---|
| 164 | */ |
|---|
| 165 | function head($url, $headers=null, $body=null, $args=array()) |
|---|
| 166 | { |
|---|
| 167 | $defaults = array('method' => 'HEAD'); |
|---|
| 168 | $r = wp_parse_args( $args, $defaults ); |
|---|
| 169 | return $this->request($url, $headers, $body, $r); |
|---|
| 170 | } |
|---|
| 171 | |
|---|
| 172 | /** |
|---|
| 173 | * Parses the responses and splits the parts into headers and body. |
|---|
| 174 | * |
|---|
| 175 | * @access public |
|---|
| 176 | * @static |
|---|
| 177 | * @since {@internal Version Unknown}} |
|---|
| 178 | * |
|---|
| 179 | * @param string $strResponse The full response string |
|---|
| 180 | * @return array Array with 'headers' and 'body' keys. |
|---|
| 181 | */ |
|---|
| 182 | function processResponse($strResponse) |
|---|
| 183 | { |
|---|
| 184 | list($theHeaders, $theBody) = explode("\r\n\r\n", $strResponse, 2); |
|---|
| 185 | return array('headers' => $theHeaders, 'body' => $theBody); |
|---|
| 186 | } |
|---|
| 187 | |
|---|
| 188 | /** |
|---|
| 189 | * Whether response code is in the 400 range. |
|---|
| 190 | * |
|---|
| 191 | * @access public |
|---|
| 192 | * @static |
|---|
| 193 | * @since {@internal Version Unknown}} |
|---|
| 194 | * |
|---|
| 195 | * @param array $response Array with code and message keys |
|---|
| 196 | * @return bool True if 40x Response, false if something else. |
|---|
| 197 | */ |
|---|
| 198 | function is400Response($response) |
|---|
| 199 | { |
|---|
| 200 | if( (int) substr($response, 0, 1) == 4 ) |
|---|
| 201 | return true; |
|---|
| 202 | return false; |
|---|
| 203 | } |
|---|
| 204 | |
|---|
| 205 | /** |
|---|
| 206 | * Whether the headers returned a redirect location. |
|---|
| 207 | * |
|---|
| 208 | * @access public |
|---|
| 209 | * @static |
|---|
| 210 | * @since {@internal Version Unknown}} |
|---|
| 211 | * |
|---|
| 212 | * @param array $headers Array with headers |
|---|
| 213 | * @return bool True if Location header is found. |
|---|
| 214 | */ |
|---|
| 215 | function isRedirect($headers) |
|---|
| 216 | { |
|---|
| 217 | if( isset($headers['location']) ) |
|---|
| 218 | return true; |
|---|
| 219 | return false; |
|---|
| 220 | } |
|---|
| 221 | |
|---|
| 222 | /** |
|---|
| 223 | * Transform header string into an array. |
|---|
| 224 | * |
|---|
| 225 | * Will overwrite the last header value, if it is not empty. |
|---|
| 226 | * |
|---|
| 227 | * @access public |
|---|
| 228 | * @static |
|---|
| 229 | * @since {@internal Version Unknown}} |
|---|
| 230 | * |
|---|
| 231 | * @param string|array $headers |
|---|
| 232 | * @return array |
|---|
| 233 | */ |
|---|
| 234 | function processHeaders($headers) |
|---|
| 235 | { |
|---|
| 236 | if( !is_array($headers) ) { |
|---|
| 237 | $headers = explode("\n", str_replace(array("\r"), '', $headers) ); |
|---|
| 238 | } |
|---|
| 239 | |
|---|
| 240 | $response = array('code' => 0, 'message' => ''); |
|---|
| 241 | |
|---|
| 242 | $newheaders = array(); |
|---|
| 243 | foreach($headers as $tempheader) { |
|---|
| 244 | if( empty($tempheader) ) |
|---|
| 245 | continue; |
|---|
| 246 | |
|---|
| 247 | if( false === strpos($tempheader, ':') ) { |
|---|
| 248 | list( , $iResponseCode, $strResponseMsg) = explode(" ", $tempheader, 3); |
|---|
| 249 | $response['code'] = $iResponseCode; |
|---|
| 250 | $response['message'] = $strResponseMsg; |
|---|
| 251 | continue; |
|---|
| 252 | } |
|---|
| 253 | |
|---|
| 254 | list($key, $value) = explode(":", $tempheader, 2); |
|---|
| 255 | |
|---|
| 256 | if( !empty($value) ) |
|---|
| 257 | $newheaders[strtolower($key)] = trim($value); |
|---|
| 258 | } |
|---|
| 259 | |
|---|
| 260 | return array('response' => $response, 'headers' => $newheaders); |
|---|
| 261 | } |
|---|
| 262 | } |
|---|
| 263 | |
|---|
| 264 | /** |
|---|
| 265 | * HTTP request method uses fsockopen function to retrieve the url. |
|---|
| 266 | * |
|---|
| 267 | * Preferred method since it works with all WordPress supported PHP versions. |
|---|
| 268 | * |
|---|
| 269 | * @package WordPress |
|---|
| 270 | * @subpackage HTTP |
|---|
| 271 | * @since {@internal Version Unknown}} |
|---|
| 272 | */ |
|---|
| 273 | class WP_Http_Fsockopen |
|---|
| 274 | { |
|---|
| 275 | /** |
|---|
| 276 | * Retrieve the location and set the class properties after the site has been retrieved. |
|---|
| 277 | * |
|---|
| 278 | * @since {@internal Version Unknown}} |
|---|
| 279 | * @access public |
|---|
| 280 | * @param string $url |
|---|
| 281 | * @param string|array $headers Optional. Either the header string or array of Header name and value pairs. Expects sanitized. |
|---|
| 282 | * @param string $body Optional. The body that should be sent. Expected to be already processed. |
|---|
| 283 | * @param str|array $type Optional. Should be an already processed array with HTTP arguments. |
|---|
| 284 | * @return boolean |
|---|
| 285 | */ |
|---|
| 286 | function request($url, $headers=null, $body=null, $args=array()) |
|---|
| 287 | { |
|---|
| 288 | $defaults = array( |
|---|
| 289 | 'method' => 'GET', 'timeout' => 3, |
|---|
| 290 | 'redirection' => 5, 'httpversion' => '1.0' |
|---|
| 291 | ); |
|---|
| 292 | |
|---|
| 293 | $r = wp_parse_args( $args, $defaults ); |
|---|
| 294 | |
|---|
| 295 | $iError = null; // Store error number |
|---|
| 296 | $strError = null; // Store error string |
|---|
| 297 | |
|---|
| 298 | $arrURL = parse_url($url); |
|---|
| 299 | |
|---|
| 300 | $secure_transport = false; |
|---|
| 301 | |
|---|
| 302 | if( !isset($arrURL['port']) ) { |
|---|
| 303 | if( (($arrURL['scheme'] == 'ssl' || $arrURL['scheme'] == 'https')) && extension_loaded('openssl') ) { |
|---|
| 304 | $arrURL['host'] = 'ssl://'.$arrURL['host']; |
|---|
| 305 | $arrURL['port'] = apply_filters('http_request_default_port', 443); |
|---|
| 306 | $secure_transport = true; |
|---|
| 307 | } |
|---|
| 308 | else |
|---|
| 309 | $arrURL['port'] = apply_filters('http_request_default_port', 80); |
|---|
| 310 | } |
|---|
| 311 | else |
|---|
| 312 | $arrURL['port'] = apply_filters('http_request_port', $arrURL['port']); |
|---|
| 313 | |
|---|
| 314 | if( true === $secure_transport ) |
|---|
| 315 | $error_reporting = error_reporting(0); |
|---|
| 316 | |
|---|
| 317 | $handle = fsockopen($arrURL['host'], $arrURL['port'], $iError, $strError, apply_filters('http_request_timeout', absint($r['timeout']) ) ); |
|---|
| 318 | |
|---|
| 319 | if( false === $handle ) { |
|---|
| 320 | return new WP_Error('http_request_failed', $iError.': '.$strError); |
|---|
| 321 | } |
|---|
| 322 | |
|---|
| 323 | $requestPath = $arrURL['path'] . ( isset($arrURL['query']) ? '?' . $arrURL['query'] : '' ); |
|---|
| 324 | $requestPath = (empty($requestPath)) ? '/' : $requestPath; |
|---|
| 325 | |
|---|
| 326 | $strHeaders = ''; |
|---|
| 327 | $strHeaders .= strtoupper($r['method']).' '.$requestPath.' HTTP/'.$r['httpversion']."\r\n"; |
|---|
| 328 | $strHeaders .= 'Host: '.$arrURL['host']."\r\n"; |
|---|
| 329 | |
|---|
| 330 | if( is_array($header) ) { |
|---|
| 331 | foreach( (array) $this->getHeaders() as $header => $headerValue) |
|---|
| 332 | $strHeaders .= $header.': '.$headerValue."\r\n"; |
|---|
| 333 | } else |
|---|
| 334 | $strHeaders .= $header; |
|---|
| 335 | |
|---|
| 336 | $strHeaders .= "\r\n"; |
|---|
| 337 | |
|---|
| 338 | if( !is_null($body) ) |
|---|
| 339 | $strHeaders .= $body; |
|---|
| 340 | |
|---|
| 341 | fwrite($handle, $strHeaders); |
|---|
| 342 | |
|---|
| 343 | $strResponse = ''; |
|---|
| 344 | while( !feof($handle) ) { |
|---|
| 345 | $strResponse .= fread($handle, 4096); |
|---|
| 346 | } |
|---|
| 347 | fclose($handle); |
|---|
| 348 | |
|---|
| 349 | if( true === $secure_transport ) |
|---|
| 350 | error_reporting($error_reporting); |
|---|
| 351 | |
|---|
| 352 | $process = WP_Http::processResponse($strResponse); |
|---|
| 353 | $arrHeaders = WP_Http::processHeaders($process['headers']); |
|---|
| 354 | |
|---|
| 355 | if( WP_Http::is400Response($arrHeaders['response']) ) |
|---|
| 356 | return new WP_Error('http_request_failed', $arrHeaders['response']['code'] .': '. $arrHeaders['response']['message']); |
|---|
| 357 | |
|---|
| 358 | if( isset($arrHeaders['headers']['location']) ) { |
|---|
| 359 | if( $r['redirection']-- > 0 ) { |
|---|
| 360 | return $this->request($arrHeaders['headers']['location'], $headers, $body, $r); |
|---|
| 361 | } else |
|---|
| 362 | return new WP_Error('http_request_failed', __('Too many redirects.')); |
|---|
| 363 | } |
|---|
| 364 | |
|---|
| 365 | return array('headers' => $arrHeaders['headers'], 'body' => $process['body'], 'response' => $arrHeaders['response']); |
|---|
| 366 | } |
|---|
| 367 | |
|---|
| 368 | /** |
|---|
| 369 | * Whether this class can be used for retrieving an URL. |
|---|
| 370 | * |
|---|
| 371 | * @since {@internal Version Unknown}} |
|---|
| 372 | * @static |
|---|
| 373 | * @return boolean False means this class can not be used, true means it can. |
|---|
| 374 | */ |
|---|
| 375 | function test() |
|---|
| 376 | { |
|---|
| 377 | if( function_exists( 'fsockopen' ) ) |
|---|
| 378 | return true; |
|---|
| 379 | |
|---|
| 380 | return false; |
|---|
| 381 | } |
|---|
| 382 | } |
|---|
| 383 | |
|---|
| 384 | /** |
|---|
| 385 | * HTTP request method uses fopen function to retrieve the url. |
|---|
| 386 | * |
|---|
| 387 | * Requires PHP version greater than 4.3.0 for stream support. Does not allow |
|---|
| 388 | * for $context support, but should still be okay, to write the headers, before |
|---|
| 389 | * getting the response. Also requires that 'allow_url_fopen' to be enabled. |
|---|
| 390 | * |
|---|
| 391 | * Second preferred method for handling the retrieving the url for PHP 4.3+. |
|---|
| 392 | * |
|---|
| 393 | * @package WordPress |
|---|
| 394 | * @subpackage HTTP |
|---|
| 395 | * @since {@internal Version Unknown}} |
|---|
| 396 | */ |
|---|
| 397 | class WP_Http_Fopen |
|---|
| 398 | { |
|---|
| 399 | /** |
|---|
| 400 | * Retrieve the location and set the class properties after the site has been retrieved. |
|---|
| 401 | * |
|---|
| 402 | * @access public |
|---|
| 403 | * @since {@internal Version Unknown}} |
|---|
| 404 | * |
|---|
| 405 | * @param string $url |
|---|
| 406 | * @param string|array $headers Optional. Either the header string or array of Header name and value pairs. Expects sanitized. |
|---|
| 407 | * @param string $body Optional. The body that should be sent. Expected to be already processed. |
|---|
| 408 | * @param str|array $type Optional. Should be an already processed array with HTTP arguments. |
|---|
| 409 | * @return boolean |
|---|
| 410 | */ |
|---|
| 411 | function request($url, $headers=null, $body=null, $args=array()) |
|---|
| 412 | { |
|---|
| 413 | global $http_response_header; |
|---|
| 414 | |
|---|
| 415 | $defaults = array( |
|---|
| 416 | 'method' => 'GET', 'timeout' => 3, |
|---|
| 417 | 'redirection' => 5, 'httpversion' => '1.0' |
|---|
| 418 | ); |
|---|
| 419 | |
|---|
| 420 | $r = wp_parse_args( $args, $defaults ); |
|---|
| 421 | |
|---|
| 422 | $arrURL = parse_url($url); |
|---|
| 423 | |
|---|
| 424 | if( 'http' != $arrURL['scheme'] || 'https' != $arrURL['scheme'] ) |
|---|
| 425 | $url = str_replace($arrURL['scheme'], 'http', $url); |
|---|
| 426 | |
|---|
| 427 | $handle = fopen($url, 'rb'); |
|---|
| 428 | |
|---|
| 429 | if(!$handle) |
|---|
| 430 | return new WP_Error('http_request_failed', sprintf(__("Could not open handle for fopen() to %s"), $url)); |
|---|
| 431 | |
|---|
| 432 | if( function_exists('stream_set_timeout') ) |
|---|
| 433 | stream_set_timeout($handle, apply_filters('http_request_timeout', $r['timeout']) ); |
|---|
| 434 | |
|---|
| 435 | $strResponse = ''; |
|---|
| 436 | while(!feof($handle)) { |
|---|
| 437 | $strResponse .= fread($handle, 4096); |
|---|
| 438 | } |
|---|
| 439 | |
|---|
| 440 | $theHeaders = ''; |
|---|
| 441 | if( function_exists('stream_get_meta_data') ) { |
|---|
| 442 | $meta = stream_get_meta_data($handle); |
|---|
| 443 | $theHeaders = $meta['wrapper_data']; |
|---|
| 444 | } else { |
|---|
| 445 | $theHeaders = $http_response_header; |
|---|
| 446 | } |
|---|
| 447 | fclose($handle); |
|---|
| 448 | |
|---|
| 449 | $processedHeaders = WP_Http::processHeaders($theHeaders); |
|---|
| 450 | return array('headers' => $processedHeaders['headers'], 'body' => $strResponse, 'response' => $processedHeaders['response']); |
|---|
| 451 | } |
|---|
| 452 | |
|---|
| 453 | /** |
|---|
| 454 | * Whether this class can be used for retrieving an URL. |
|---|
| 455 | * |
|---|
| 456 | * @static |
|---|
| 457 | * @return boolean False means this class can not be used, true means it can. |
|---|
| 458 | */ |
|---|
| 459 | function test() |
|---|
| 460 | { |
|---|
| 461 | if( !function_exists('fopen') || (function_exists('ini_get') && true != ini_get('allow_url_fopen')) ) |
|---|
| 462 | return false; |
|---|
| 463 | |
|---|
| 464 | return true; |
|---|
| 465 | } |
|---|
| 466 | } |
|---|
| 467 | |
|---|
| 468 | /** |
|---|
| 469 | * HTTP request method uses Streams to retrieve the url. |
|---|
| 470 | * |
|---|
| 471 | * Requires PHP 5.0+ and uses fopen with stream context. Requires that |
|---|
| 472 | * 'allow_url_fopen' PHP setting to be enabled. |
|---|
| 473 | * |
|---|
| 474 | * Second preferred method for getting the URL, for PHP 5. |
|---|
| 475 | * |
|---|
| 476 | * @package WordPress |
|---|
| 477 | * @subpackage HTTP |
|---|
| 478 | * @since {@internal Version Unknown}} |
|---|
| 479 | */ |
|---|
| 480 | class WP_Http_Streams |
|---|
| 481 | { |
|---|
| 482 | /** |
|---|
| 483 | * Retrieve the location and set the class properties after the site has been retrieved. |
|---|
| 484 | * |
|---|
| 485 | * @access public |
|---|
| 486 | * @since {@internal Version Unknown}} |
|---|
| 487 | * |
|---|
| 488 | * @param string $url |
|---|
| 489 | * @param string|array $headers Optional. Either the header string or array of Header name and value pairs. Expects sanitized. |
|---|
| 490 | * @param string $body Optional. The body that should be sent. Expected to be already processed. |
|---|
| 491 | * @param str|array $type Optional. Should be an already processed array with HTTP arguments. |
|---|
| 492 | * @return boolean |
|---|
| 493 | */ |
|---|
| 494 | function request($url, $headers=null, $body=null, $args=array()) |
|---|
| 495 | { |
|---|
| 496 | $defaults = array( |
|---|
| 497 | 'method' => 'GET', 'timeout' => 3, |
|---|
| 498 | 'redirection' => 5, 'httpversion' => '1.0' |
|---|
| 499 | ); |
|---|
| 500 | |
|---|
| 501 | $r = wp_parse_args( $args, $defaults ); |
|---|
| 502 | |
|---|
| 503 | $arrURL = parse_url($url); |
|---|
| 504 | |
|---|
| 505 | if( 'http' != $arrURL['scheme'] || 'https' != $arrURL['scheme'] ) |
|---|
| 506 | $url = str_replace($arrURL['scheme'], 'http', $url); |
|---|
| 507 | |
|---|
| 508 | $arrContext = array('http' => |
|---|
| 509 | array( |
|---|
| 510 | 'method' => strtoupper($r['method']), |
|---|
| 511 | 'user-agent' => $headers['User-Agent'], |
|---|
| 512 | 'max_redirects' => $r['redirection'], |
|---|
| 513 | 'protocol_version' => (float) $r['httpversion'], |
|---|
| 514 | 'header' => $headers |
|---|
| 515 | ) |
|---|
| 516 | ); |
|---|
| 517 | |
|---|
| 518 | if( !is_null($body) ) |
|---|
| 519 | $arrContext['http']['content'] = $body; |
|---|
| 520 | |
|---|
| 521 | $context = stream_context_create($arrContext); |
|---|
| 522 | |
|---|
| 523 | $handle = fopen($url, 'rb', false, $context); |
|---|
| 524 | |
|---|
| 525 | stream_set_timeout($handle, apply_filters('http_request_stream_timeout', $this->timeout) ); |
|---|
| 526 | |
|---|
| 527 | if(!$handle) |
|---|
| 528 | return new WP_Error('http_request_failed', sprintf(__("Could not open handle for fopen() to %s"), $url)); |
|---|
| 529 | |
|---|
| 530 | $strResponse = stream_get_contents($handle); |
|---|
| 531 | $meta = stream_get_meta_data($handle); |
|---|
| 532 | |
|---|
| 533 | fclose($handle); |
|---|
| 534 | |
|---|
| 535 | $processedHeaders = WP_Http::processHeaders($meta['wrapper_data']); |
|---|
| 536 | return array('headers' => $processedHeaders['headers'], 'body' => $strResponse, 'response' => $processedHeaders['response']); |
|---|
| 537 | } |
|---|
| 538 | |
|---|
| 539 | /** |
|---|
| 540 | * Whether this class can be used for retrieving an URL. |
|---|
| 541 | * |
|---|
| 542 | * @static |
|---|
| 543 | * @access public |
|---|
| 544 | * @since {@internal Version Unknown}} |
|---|
| 545 | * |
|---|
| 546 | * @return boolean False means this class can not be used, true means it can. |
|---|
| 547 | */ |
|---|
| 548 | function test() |
|---|
| 549 | { |
|---|
| 550 | if( !function_exists('fopen') || (function_exists('ini_get') && true != ini_get('allow_url_fopen')) ) |
|---|
| 551 | return false; |
|---|
| 552 | |
|---|
| 553 | if( version_compare(PHP_VERSION, '5.0', '<') ) |
|---|
| 554 | return false; |
|---|
| 555 | |
|---|
| 556 | return true; |
|---|
| 557 | } |
|---|
| 558 | } |
|---|
| 559 | |
|---|
| 560 | /** |
|---|
| 561 | * HTTP request method uses HTTP extension to retrieve the url. |
|---|
| 562 | * |
|---|
| 563 | * Requires the HTTP extension to be installed. |
|---|
| 564 | * |
|---|
| 565 | * Last ditch effort to retrieve the URL before complete failure. |
|---|
| 566 | * |
|---|
| 567 | * @package WordPress |
|---|
| 568 | * @subpackage HTTP |
|---|
| 569 | * @since {@internal Version Unknown}} |
|---|
| 570 | */ |
|---|
| 571 | class WP_Http_ExtHTTP |
|---|
| 572 | { |
|---|
| 573 | /** |
|---|
| 574 | * Retrieve the location and set the class properties after the site has been retrieved. |
|---|
| 575 | * |
|---|
| 576 | * @access public |
|---|
| 577 | * @since {@internal Version Unknown}} |
|---|
| 578 | * |
|---|
| 579 | * @param string $url |
|---|
| 580 | * @param string|array $headers Optional. Either the header string or array of Header name and value pairs. Expects sanitized. |
|---|
| 581 | * @param string $body Optional. The body that should be sent. Expected to be already processed. |
|---|
| 582 | * @param str|array $type Optional. Should be an already processed array with HTTP arguments. |
|---|
| 583 | * @return boolean |
|---|
| 584 | */ |
|---|
| 585 | function request($url, $headers=null, $body=null, $args=array()) |
|---|
| 586 | { |
|---|
| 587 | global $wp_version; |
|---|
| 588 | |
|---|
| 589 | $defaults = array( |
|---|
| 590 | 'method' => 'GET', 'timeout' => 3, |
|---|
| 591 | 'redirection' => 5, 'httpversion' => '1.0', |
|---|
| 592 | 'user_agent' => apply_filters('http_headers_useragent', 'WordPress/'.$wp_version) |
|---|
| 593 | ); |
|---|
| 594 | |
|---|
| 595 | $r = wp_parse_args( $args, $defaults ); |
|---|
| 596 | |
|---|
| 597 | if( isset($headers['User-Agent']) ) |
|---|
| 598 | unset($headers['User-Agent']); |
|---|
| 599 | |
|---|
| 600 | switch($r['method']) |
|---|
| 601 | { |
|---|
| 602 | case 'GET': |
|---|
| 603 | $r['method'] = HTTP_METH_GET; |
|---|
| 604 | break; |
|---|
| 605 | case 'POST': |
|---|
| 606 | $r['method'] = HTTP_METH_POST; |
|---|
| 607 | break; |
|---|
| 608 | case 'HEAD': |
|---|
| 609 | $r['method'] = HTTP_METH_HEAD; |
|---|
| 610 | break; |
|---|
| 611 | default: |
|---|
| 612 | $r['method'] = HTTP_METH_GET; |
|---|
| 613 | } |
|---|
| 614 | |
|---|
| 615 | $arrURL = parse_url($url); |
|---|
| 616 | |
|---|
| 617 | if( 'http' != $arrURL['scheme'] || 'https' != $arrURL['scheme'] ) |
|---|
| 618 | $url = str_replace($arrURL['scheme'], 'http', $url); |
|---|
| 619 | |
|---|
| 620 | $options = array( |
|---|
| 621 | 'timeout' => $this->timeout, |
|---|
| 622 | 'connecttimeout' => apply_filters('http_request_stream_timeout', $this->timeout), |
|---|
| 623 | 'redirect' => apply_filters('http_request_redirect', 3), |
|---|
| 624 | 'useragent' => $r['user_agent'], |
|---|
| 625 | 'headers' => $headers, |
|---|
| 626 | ); |
|---|
| 627 | |
|---|
| 628 | $strResponse = http_request($r['method'], $url, $body, $options, $info); |
|---|
| 629 | |
|---|
| 630 | if( false === $strResponse ) |
|---|
| 631 | return new WP_Error('http_request_failed', $info['response_code'] .': '. $info['error']); |
|---|
| 632 | |
|---|
| 633 | list($theHeaders, $theBody) = explode("\r\n\r\n", $strResponse, 2); |
|---|
| 634 | $theHeaders = WP_Http::processHeaders($theHeaders); |
|---|
| 635 | |
|---|
| 636 | $theResponse = array(); |
|---|
| 637 | $theResponse['response']['code'] = $info['response_code']; |
|---|
| 638 | $theResponse['response']['message'] = get_status_header_desc($info['response_code']); |
|---|
| 639 | |
|---|
| 640 | return array('headers' => $theHeaders['headers'], 'body' => $theBody, 'response' => $theResponse); |
|---|
| 641 | } |
|---|
| 642 | |
|---|
| 643 | /** |
|---|
| 644 | * Whether this class can be used for retrieving an URL. |
|---|
| 645 | * |
|---|
| 646 | * @static |
|---|
| 647 | * @since {@internal Version Unknown}} |
|---|
| 648 | * |
|---|
| 649 | * @return boolean False means this class can not be used, true means it can. |
|---|
| 650 | */ |
|---|
| 651 | function test() |
|---|
| 652 | { |
|---|
| 653 | if( function_exists('http_request') ) |
|---|
| 654 | return true; |
|---|
| 655 | |
|---|
| 656 | return false; |
|---|
| 657 | } |
|---|
| 658 | } |
|---|
| 659 | |
|---|
| 660 | /** |
|---|
| 661 | * Returns the initialized WP_Http Object |
|---|
| 662 | * |
|---|
| 663 | * @since {@internal Version Unknown}} |
|---|
| 664 | * @access private |
|---|
| 665 | * |
|---|
| 666 | * @return WP_Http HTTP Transport object. |
|---|
| 667 | */ |
|---|
| 668 | function &_wp_http_get_object() { |
|---|
| 669 | static $http; |
|---|
| 670 | |
|---|
| 671 | if( is_null($http) ) |
|---|
| 672 | $http = new WP_Http(); |
|---|
| 673 | |
|---|
| 674 | return $http; |
|---|
| 675 | } |
|---|
| 676 | |
|---|
| 677 | /** |
|---|
| 678 | * Retrieve the raw response from the HTTP request. |
|---|
| 679 | * |
|---|
| 680 | * The array structure is a little complex. |
|---|
| 681 | * |
|---|
| 682 | * <code> |
|---|
| 683 | * $res = array( 'headers' => |
|---|
| 684 | * 'response' => array('code', 'message'), |
|---|
| 685 | * 'headers' => array() |
|---|
| 686 | * ); |
|---|
| 687 | * </code> |
|---|
| 688 | * |
|---|
| 689 | * All of the headers in $res['headers']['headers'] are with the name as the key |
|---|
| 690 | * and the value as the value. So to get the User-Agent, you would do the |
|---|
| 691 | * following. |
|---|
| 692 | * |
|---|
| 693 | * <code> |
|---|
| 694 | * $user_agent = $res['headers']['headers']['user-agent']; |
|---|
| 695 | * </code> |
|---|
| 696 | * |
|---|
| 697 | * The body is the raw response content and can be retrieved from $res['body']. |
|---|
| 698 | * |
|---|
| 699 | * This function is called first to make the request and there are other API |
|---|
| 700 | * functions to abstract out the above convoluted setup. |
|---|
| 701 | * |
|---|
| 702 | * @since {@internal Version Unknown}} |
|---|
| 703 | * |
|---|
| 704 | * @param string $url Site URL to retrieve. |
|---|
| 705 | * @param string|array $headers Optional. Either the header string or array of Header name and value pairs. |
|---|
| 706 | * @param string $body Optional. The body that should be sent. Expected to be already processed. |
|---|
| 707 | * @param array $args Optional. Override the defaults. |
|---|
| 708 | * @return string The body of the response |
|---|
| 709 | */ |
|---|
| 710 | function wp_remote_request($url, $headers=null, $body=null, $args=array()) { |
|---|
| 711 | $objFetchSite = _wp_http_get_object(); |
|---|
| 712 | |
|---|
| 713 | return $objFetchSite->request($url, $headers, $body, $args); |
|---|
| 714 | } |
|---|
| 715 | |
|---|
| 716 | /** |
|---|
| 717 | * Retrieve the raw response from the HTTP request using the GET method. |
|---|
| 718 | * |
|---|
| 719 | * @see wp_remote_request() For more information on the response array format. |
|---|
| 720 | * |
|---|
| 721 | * @since {@internal Version Unknown}} |
|---|
| 722 | * |
|---|
| 723 | * @param string $url Site URL to retrieve. |
|---|
| 724 | * @param string|array $headers Optional. Either the header string or array of Header name and value pairs. |
|---|
| 725 | * @param string $body Optional. The body that should be sent. Expected to be already processed. |
|---|
| 726 | * @param array $args Optional. Override the defaults. |
|---|
| 727 | * @return string The body of the response |
|---|
| 728 | */ |
|---|
| 729 | function wp_remote_get($url, $headers=null, $body=null, $args=array()) { |
|---|
| 730 | $objFetchSite = _wp_http_get_object(); |
|---|
| 731 | |
|---|
| 732 | return $objFetchSite->get($url, $headers, $body, $args); |
|---|
| 733 | } |
|---|
| 734 | |
|---|
| 735 | /** |
|---|
| 736 | * Retrieve the raw response from the HTTP request using the POST method. |
|---|
| 737 | * |
|---|
| 738 | * @see wp_remote_request() For more information on the response array format. |
|---|
| 739 | * |
|---|
| 740 | * @since {@internal Version Unknown}} |
|---|
| 741 | * |
|---|
| 742 | * @param string $url Site URL to retrieve. |
|---|
| 743 | * @param string|array $headers Optional. Either the header string or array of Header name and value pairs. |
|---|
| 744 | * @param string $body Optional. The body that should be sent. Expected to be already processed. |
|---|
| 745 | * @param array $args Optional. Override the defaults. |
|---|
| 746 | * @return string The body of the response |
|---|
| 747 | */ |
|---|
| 748 | function wp_remote_post($url, $headers=null, $body=null, $args=array()) { |
|---|
| 749 | $objFetchSite = _wp_http_get_object(); |
|---|
| 750 | |
|---|
| 751 | return $objFetchSite->post($url, $headers, $body, $args); |
|---|
| 752 | } |
|---|
| 753 | |
|---|
| 754 | /** |
|---|
| 755 | * Retrieve the raw response from the HTTP request using the HEAD method. |
|---|
| 756 | * |
|---|
| 757 | * @see wp_remote_request() For more information on the response array format. |
|---|
| 758 | * |
|---|
| 759 | * @since {@internal Version Unknown}} |
|---|
| 760 | * |
|---|
| 761 | * @param string $url Site URL to retrieve. |
|---|
| 762 | * @param string|array $headers Optional. Either the header string or array of Header name and value pairs. |
|---|
| 763 | * @param string $body Optional. The body that should be sent. Expected to be already processed. |
|---|
| 764 | * @param array $args Optional. Override the defaults. |
|---|
| 765 | * @return string The body of the response |
|---|
| 766 | */ |
|---|
| 767 | function wp_remote_head($url, $headers=null, $body=null, $args=array()) { |
|---|
| 768 | $objFetchSite = _wp_http_get_object(); |
|---|
| 769 | |
|---|
| 770 | return $objFetchSite->head($url, $headers, $body, $args); |
|---|
| 771 | } |
|---|
| 772 | |
|---|
| 773 | /** |
|---|
| 774 | * Retrieve only the headers from the raw response. |
|---|
| 775 | * |
|---|
| 776 | * @since {@internal Version Unknown}} |
|---|
| 777 | * |
|---|
| 778 | * @param array $response HTTP response. |
|---|
| 779 | * @return array The headers of the response. Empty array if incorrect parameter given. |
|---|
| 780 | */ |
|---|
| 781 | function wp_remote_retrieve_headers(&$response) { |
|---|
| 782 | if( !isset($response['headers']) || !is_array($response['headers'])) |
|---|
| 783 | return array(); |
|---|
| 784 | |
|---|
| 785 | return $response['headers']; |
|---|
| 786 | } |
|---|
| 787 | |
|---|
| 788 | /** |
|---|
| 789 | * Retrieve a single header by name from the raw response. |
|---|
| 790 | * |
|---|
| 791 | * @since {@internal Version Unknown}} |
|---|
| 792 | * |
|---|
| 793 | * @param array $response |
|---|
| 794 | * @param string $header Header name to retrieve value from. |
|---|
| 795 | * @return array The header value. Empty string on if incorrect parameter given. |
|---|
| 796 | */ |
|---|
| 797 | function wp_remote_retrieve_header(&$response, $header) { |
|---|
| 798 | if( !isset($response['headers']) || !is_array($response['headers'])) |
|---|
| 799 | return ''; |
|---|
| 800 | |
|---|
| 801 | if( array_key_exists($header, $response['headers']) ) |
|---|
| 802 | return $response['headers'][$header]; |
|---|
| 803 | |
|---|
| 804 | return ''; |
|---|
| 805 | } |
|---|
| 806 | |
|---|
| 807 | /** |
|---|
| 808 | * Retrieve only the response code from the raw response. |
|---|
| 809 | * |
|---|
| 810 | * Will return an empty array if incorrect parameter value is given. |
|---|
| 811 | * |
|---|
| 812 | * @since {@internal Version Unknown}} |
|---|
| 813 | * |
|---|
| 814 | * @param array $response HTTP response. |
|---|
| 815 | * @return array The keys 'code' and 'message' give information on the response. |
|---|
| 816 | */ |
|---|
| 817 | function wp_remote_retrieve_response_code(&$response) { |
|---|
| 818 | if( !isset($response['response']) || !is_array($response['response'])) |
|---|
| 819 | return ''; |
|---|
| 820 | |
|---|
| 821 | return $response['response']['code']; |
|---|
| 822 | } |
|---|
| 823 | |
|---|
| 824 | /** |
|---|
| 825 | * Retrieve only the response message from the raw response. |
|---|
| 826 | * |
|---|
| 827 | * Will return an empty array if incorrect parameter value is given. |
|---|
| 828 | * |
|---|
| 829 | * @since {@internal Version Unknown}} |
|---|
| 830 | * |
|---|
| 831 | * @param array $response HTTP response. |
|---|
| 832 | * @return array The keys 'code' and 'message' give information on the response. |
|---|
| 833 | */ |
|---|
| 834 | function wp_remote_retrieve_response_message(&$response) { |
|---|
| 835 | if( !isset($response['response']) || !is_array($response['response'])) |
|---|
| 836 | return ''; |
|---|
| 837 | |
|---|
| 838 | return $response['response']['message']; |
|---|
| 839 | } |
|---|
| 840 | |
|---|
| 841 | /** |
|---|
| 842 | * Retrieve only the body from the raw response. |
|---|
| 843 | * |
|---|
| 844 | * @since {@internal Version Unknown}} |
|---|
| 845 | * |
|---|
| 846 | * @param array $response HTTP response. |
|---|
| 847 | * @return string The body of the response. Empty string if no body or incorrect parameter given. |
|---|
| 848 | */ |
|---|
| 849 | function wp_remote_retrieve_body(&$response) { |
|---|
| 850 | if( !isset($response['body']) ) |
|---|
| 851 | return ''; |
|---|
| 852 | |
|---|
| 853 | return $response['body']; |
|---|
| 854 | } |
|---|
| 855 | |
|---|
| 856 | ?> |
|---|