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