]> scripts.mit.edu Git - autoinstalls/wordpress.git/blob - wp-includes/http.php
WordPress 4.1.4
[autoinstalls/wordpress.git] / wp-includes / http.php
1 <?php
2 /**
3  * Simple and uniform HTTP request API.
4  *
5  * Will eventually replace and standardize the WordPress HTTP requests made.
6  *
7  * @link https://core.trac.wordpress.org/ticket/4779 HTTP API Proposal
8  *
9  * @package WordPress
10  * @subpackage HTTP
11  * @since 2.7.0
12  */
13
14 /**
15  * Returns the initialized WP_Http Object
16  *
17  * @since 2.7.0
18  * @access private
19  *
20  * @return WP_Http HTTP Transport object.
21  */
22 function _wp_http_get_object() {
23         static $http;
24
25         if ( is_null($http) )
26                 $http = new WP_Http();
27
28         return $http;
29 }
30
31 /**
32  * Retrieve the raw response from a safe HTTP request.
33  *
34  * This function is ideal when the HTTP request is being made to an arbitrary
35  * URL. The URL is validated to avoid redirection and request forgery attacks.
36  *
37  * @since 3.6.0
38  *
39  * @see wp_remote_request() For more information on the response array format.
40  * @see WP_Http::request() For default arguments information.
41  *
42  * @param string $url  Site URL to retrieve.
43  * @param array  $args Optional. Request arguments. Default empty array.
44  * @return WP_Error|array The response or WP_Error on failure.
45  */
46 function wp_safe_remote_request( $url, $args = array() ) {
47         $args['reject_unsafe_urls'] = true;
48         $http = _wp_http_get_object();
49         return $http->request( $url, $args );
50 }
51
52 /**
53  * Retrieve the raw response from a safe HTTP request using the GET method.
54  *
55  * This function is ideal when the HTTP request is being made to an arbitrary
56  * URL. The URL is validated to avoid redirection and request forgery attacks.
57  *
58  * @since 3.6.0
59  *
60  * @see wp_remote_request() For more information on the response array format.
61  * @see WP_Http::request() For default arguments information.
62  *
63  * @param string $url  Site URL to retrieve.
64  * @param array  $args Optional. Request arguments. Default empty array.
65  * @return WP_Error|array The response or WP_Error on failure.
66  */
67 function wp_safe_remote_get( $url, $args = array() ) {
68         $args['reject_unsafe_urls'] = true;
69         $http = _wp_http_get_object();
70         return $http->get( $url, $args );
71 }
72
73 /**
74  * Retrieve the raw response from a safe HTTP request using the POST method.
75  *
76  * This function is ideal when the HTTP request is being made to an arbitrary
77  * URL. The URL is validated to avoid redirection and request forgery attacks.
78  *
79  * @since 3.6.0
80  *
81  * @see wp_remote_request() For more information on the response array format.
82  * @see WP_Http::request() For default arguments information.
83  *
84  * @param string $url  Site URL to retrieve.
85  * @param array  $args Optional. Request arguments. Default empty array.
86  * @return WP_Error|array The response or WP_Error on failure.
87  */
88 function wp_safe_remote_post( $url, $args = array() ) {
89         $args['reject_unsafe_urls'] = true;
90         $http = _wp_http_get_object();
91         return $http->post( $url, $args );
92 }
93
94 /**
95  * Retrieve the raw response from a safe HTTP request using the HEAD method.
96  *
97  * This function is ideal when the HTTP request is being made to an arbitrary
98  * URL. The URL is validated to avoid redirection and request forgery attacks.
99  *
100  * @since 3.6.0
101  *
102  * @see wp_remote_request() For more information on the response array format.
103  * @see WP_Http::request() For default arguments information.
104  *
105  * @param string $url Site URL to retrieve.
106  * @param array $args Optional. Request arguments. Default empty array.
107  * @return WP_Error|array The response or WP_Error on failure.
108  */
109 function wp_safe_remote_head( $url, $args = array() ) {
110         $args['reject_unsafe_urls'] = true;
111         $http = _wp_http_get_object();
112         return $http->head( $url, $args );
113 }
114
115 /**
116  * Retrieve the raw response from the HTTP request.
117  *
118  * The array structure is a little complex:
119  *
120  *     $res = array(
121  *         'headers'  => array(),
122  *         'response' => array(
123  *             'code'    => int,
124  *             'message' => string
125  *         )
126  *     );
127  *
128  * All of the headers in $res['headers'] are with the name as the key and the
129  * value as the value. So to get the User-Agent, you would do the following.
130  *
131  *     $user_agent = $res['headers']['user-agent'];
132  *
133  * The body is the raw response content and can be retrieved from $res['body'].
134  *
135  * This function is called first to make the request and there are other API
136  * functions to abstract out the above convoluted setup.
137  *
138  * Request method defaults for helper functions:
139  *  - Default 'GET'  for wp_remote_get()
140  *  - Default 'POST' for wp_remote_post()
141  *  - Default 'HEAD' for wp_remote_head()
142  *
143  * @since 2.7.0
144  *
145  * @see WP_Http::request() For additional information on default arguments.
146  *
147  * @param string $url  Site URL to retrieve.
148  * @param array  $args Optional. Request arguments. Default empty array.
149  * @return WP_Error|array The response or WP_Error on failure.
150  */
151 function wp_remote_request($url, $args = array()) {
152         $objFetchSite = _wp_http_get_object();
153         return $objFetchSite->request($url, $args);
154 }
155
156 /**
157  * Retrieve the raw response from the HTTP request using the GET method.
158  *
159  * @since 2.7.0
160  *
161  * @see wp_remote_request() For more information on the response array format.
162  * @see WP_Http::request() For default arguments information.
163  *
164  * @param string $url  Site URL to retrieve.
165  * @param array  $args Optional. Request arguments. Default empty array.
166  * @return WP_Error|array The response or WP_Error on failure.
167  */
168 function wp_remote_get($url, $args = array()) {
169         $objFetchSite = _wp_http_get_object();
170         return $objFetchSite->get($url, $args);
171 }
172
173 /**
174  * Retrieve the raw response from the HTTP request using the POST method.
175  *
176  * @since 2.7.0
177  *
178  * @see wp_remote_request() For more information on the response array format.
179  * @see WP_Http::request() For default arguments information.
180  *
181  * @param string $url  Site URL to retrieve.
182  * @param array  $args Optional. Request arguments. Default empty array.
183  * @return WP_Error|array The response or WP_Error on failure.
184  */
185 function wp_remote_post($url, $args = array()) {
186         $objFetchSite = _wp_http_get_object();
187         return $objFetchSite->post($url, $args);
188 }
189
190 /**
191  * Retrieve the raw response from the HTTP request using the HEAD method.
192  *
193  * @since 2.7.0
194  *
195  * @see wp_remote_request() For more information on the response array format.
196  * @see WP_Http::request() For default arguments information.
197  *
198  * @param string $url  Site URL to retrieve.
199  * @param array  $args Optional. Request arguments. Default empty array.
200  * @return WP_Error|array The response or WP_Error on failure.
201  */
202 function wp_remote_head($url, $args = array()) {
203         $objFetchSite = _wp_http_get_object();
204         return $objFetchSite->head($url, $args);
205 }
206
207 /**
208  * Retrieve only the headers from the raw response.
209  *
210  * @since 2.7.0
211  *
212  * @param array $response HTTP response.
213  * @return array The headers of the response. Empty array if incorrect parameter given.
214  */
215 function wp_remote_retrieve_headers( $response ) {
216         if ( is_wp_error($response) || ! isset($response['headers']) || ! is_array($response['headers']))
217                 return array();
218
219         return $response['headers'];
220 }
221
222 /**
223  * Retrieve a single header by name from the raw response.
224  *
225  * @since 2.7.0
226  *
227  * @param array $response
228  * @param string $header Header name to retrieve value from.
229  * @return string The header value. Empty string on if incorrect parameter given, or if the header doesn't exist.
230  */
231 function wp_remote_retrieve_header( $response, $header ) {
232         if ( is_wp_error($response) || ! isset($response['headers']) || ! is_array($response['headers']))
233                 return '';
234
235         if ( array_key_exists($header, $response['headers']) )
236                 return $response['headers'][$header];
237
238         return '';
239 }
240
241 /**
242  * Retrieve only the response code from the raw response.
243  *
244  * Will return an empty array if incorrect parameter value is given.
245  *
246  * @since 2.7.0
247  *
248  * @param array $response HTTP response.
249  * @return int|string The response code as an integer. Empty string on incorrect parameter given.
250  */
251 function wp_remote_retrieve_response_code( $response ) {
252         if ( is_wp_error($response) || ! isset($response['response']) || ! is_array($response['response']))
253                 return '';
254
255         return $response['response']['code'];
256 }
257
258 /**
259  * Retrieve only the response message from the raw response.
260  *
261  * Will return an empty array if incorrect parameter value is given.
262  *
263  * @since 2.7.0
264  *
265  * @param array $response HTTP response.
266  * @return string The response message. Empty string on incorrect parameter given.
267  */
268 function wp_remote_retrieve_response_message( $response ) {
269         if ( is_wp_error($response) || ! isset($response['response']) || ! is_array($response['response']))
270                 return '';
271
272         return $response['response']['message'];
273 }
274
275 /**
276  * Retrieve only the body from the raw response.
277  *
278  * @since 2.7.0
279  *
280  * @param array $response HTTP response.
281  * @return string The body of the response. Empty string if no body or incorrect parameter given.
282  */
283 function wp_remote_retrieve_body( $response ) {
284         if ( is_wp_error($response) || ! isset($response['body']) )
285                 return '';
286
287         return $response['body'];
288 }
289
290 /**
291  * Determines if there is an HTTP Transport that can process this request.
292  *
293  * @since 3.2.0
294  *
295  * @param array  $capabilities Array of capabilities to test or a wp_remote_request() $args array.
296  * @param string $url Optional. If given, will check if the URL requires SSL and adds that requirement to the capabilities array.
297  *
298  * @return bool
299  */
300 function wp_http_supports( $capabilities = array(), $url = null ) {
301         $objFetchSite = _wp_http_get_object();
302
303         $capabilities = wp_parse_args( $capabilities );
304
305         $count = count( $capabilities );
306
307         // If we have a numeric $capabilities array, spoof a wp_remote_request() associative $args array
308         if ( $count && count( array_filter( array_keys( $capabilities ), 'is_numeric' ) ) == $count ) {
309                 $capabilities = array_combine( array_values( $capabilities ), array_fill( 0, $count, true ) );
310         }
311
312         if ( $url && !isset( $capabilities['ssl'] ) ) {
313                 $scheme = parse_url( $url, PHP_URL_SCHEME );
314                 if ( 'https' == $scheme || 'ssl' == $scheme ) {
315                         $capabilities['ssl'] = true;
316                 }
317         }
318
319         return (bool) $objFetchSite->_get_first_available_transport( $capabilities );
320 }
321
322 /**
323  * Get the HTTP Origin of the current request.
324  *
325  * @since 3.4.0
326  *
327  * @return string URL of the origin. Empty string if no origin.
328  */
329 function get_http_origin() {
330         $origin = '';
331         if ( ! empty ( $_SERVER[ 'HTTP_ORIGIN' ] ) )
332                 $origin = $_SERVER[ 'HTTP_ORIGIN' ];
333
334         /**
335          * Change the origin of an HTTP request.
336          *
337          * @since 3.4.0
338          *
339          * @param string $origin The original origin for the request.
340          */
341         return apply_filters( 'http_origin', $origin );
342 }
343
344 /**
345  * Retrieve list of allowed HTTP origins.
346  *
347  * @since 3.4.0
348  *
349  * @return array Array of origin URLs.
350  */
351 function get_allowed_http_origins() {
352         $admin_origin = parse_url( admin_url() );
353         $home_origin = parse_url( home_url() );
354
355         // @todo preserve port?
356         $allowed_origins = array_unique( array(
357                 'http://' . $admin_origin[ 'host' ],
358                 'https://' . $admin_origin[ 'host' ],
359                 'http://' . $home_origin[ 'host' ],
360                 'https://' . $home_origin[ 'host' ],
361         ) );
362
363         /**
364          * Change the origin types allowed for HTTP requests.
365          *
366          * @since 3.4.0
367          *
368          * @param array $allowed_origins {
369          *     Default allowed HTTP origins.
370          *     @type string Non-secure URL for admin origin.
371          *     @type string Secure URL for admin origin.
372          *     @type string Non-secure URL for home origin.
373          *     @type string Secure URL for home origin.
374          * }
375          */
376         return apply_filters( 'allowed_http_origins' , $allowed_origins );
377 }
378
379 /**
380  * Determines if the HTTP origin is an authorized one.
381  *
382  * @since 3.4.0
383  *
384  * @param null|string $origin Origin URL. If not provided, the value of get_http_origin() is used.
385  * @return bool|null True if the origin is allowed. False otherwise.
386  */
387 function is_allowed_http_origin( $origin = null ) {
388         $origin_arg = $origin;
389
390         if ( null === $origin )
391                 $origin = get_http_origin();
392
393         if ( $origin && ! in_array( $origin, get_allowed_http_origins() ) )
394                 $origin = '';
395
396         /**
397          * Change the allowed HTTP origin result.
398          *
399          * @since 3.4.0
400          *
401          * @param string $origin Result of check for allowed origin.
402          * @param string $origin_arg original origin string passed into is_allowed_http_origin function.
403          */
404         return apply_filters( 'allowed_http_origin', $origin, $origin_arg );
405 }
406
407 /**
408  * Send Access-Control-Allow-Origin and related headers if the current request
409  * is from an allowed origin.
410  *
411  * If the request is an OPTIONS request, the script exits with either access
412  * control headers sent, or a 403 response if the origin is not allowed. For
413  * other request methods, you will receive a return value.
414  *
415  * @since 3.4.0
416  *
417  * @return bool|string Returns the origin URL if headers are sent. Returns false
418  * if headers are not sent.
419  */
420 function send_origin_headers() {
421         $origin = get_http_origin();
422
423         if ( is_allowed_http_origin( $origin ) ) {
424                 @header( 'Access-Control-Allow-Origin: ' .  $origin );
425                 @header( 'Access-Control-Allow-Credentials: true' );
426                 if ( 'OPTIONS' === $_SERVER['REQUEST_METHOD'] )
427                         exit;
428                 return $origin;
429         }
430
431         if ( 'OPTIONS' === $_SERVER['REQUEST_METHOD'] ) {
432                 status_header( 403 );
433                 exit;
434         }
435
436         return false;
437 }
438
439 /**
440  * Validate a URL for safe use in the HTTP API.
441  *
442  * @since 3.5.2
443  *
444  * @param string $url
445  * @return false|string URL or false on failure.
446  */
447 function wp_http_validate_url( $url ) {
448         $original_url = $url;
449         $url = wp_kses_bad_protocol( $url, array( 'http', 'https' ) );
450         if ( ! $url || strtolower( $url ) !== strtolower( $original_url ) )
451                 return false;
452
453         $parsed_url = @parse_url( $url );
454         if ( ! $parsed_url || empty( $parsed_url['host'] ) )
455                 return false;
456
457         if ( isset( $parsed_url['user'] ) || isset( $parsed_url['pass'] ) )
458                 return false;
459
460         if ( false !== strpbrk( $parsed_url['host'], ':#?[]' ) )
461                 return false;
462
463         $parsed_home = @parse_url( get_option( 'home' ) );
464
465         $same_host = strtolower( $parsed_home['host'] ) === strtolower( $parsed_url['host'] );
466
467         if ( ! $same_host ) {
468                 $host = trim( $parsed_url['host'], '.' );
469                 if ( preg_match( '#^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$#', $host ) ) {
470                         $ip = $host;
471                 } else {
472                         $ip = gethostbyname( $host );
473                         if ( $ip === $host ) // Error condition for gethostbyname()
474                                 $ip = false;
475                 }
476                 if ( $ip ) {
477                         $parts = array_map( 'intval', explode( '.', $ip ) );
478                         if ( 127 === $parts[0] || 10 === $parts[0]
479                                 || ( 172 === $parts[0] && 16 <= $parts[1] && 31 >= $parts[1] )
480                                 || ( 192 === $parts[0] && 168 === $parts[1] )
481                         ) {
482                                 // If host appears local, reject unless specifically allowed.
483                                 /**
484                                  * Check if HTTP request is external or not.
485                                  *
486                                  * Allows to change and allow external requests for the HTTP request.
487                                  *
488                                  * @since 3.6.0
489                                  *
490                                  * @param bool false Whether HTTP request is external or not.
491                                  * @param string $host IP of the requested host.
492                                  * @param string $url URL of the requested host.
493                                  */
494                                 if ( ! apply_filters( 'http_request_host_is_external', false, $host, $url ) )
495                                         return false;
496                         }
497                 }
498         }
499
500         if ( empty( $parsed_url['port'] ) )
501                 return $url;
502
503         $port = $parsed_url['port'];
504         if ( 80 === $port || 443 === $port || 8080 === $port )
505                 return $url;
506
507         if ( $parsed_home && $same_host && isset( $parsed_home['port'] ) && $parsed_home['port'] === $port )
508                 return $url;
509
510         return false;
511 }
512
513 /**
514  * Whitelists allowed redirect hosts for safe HTTP requests as well.
515  *
516  * Attached to the http_request_host_is_external filter.
517  *
518  * @since 3.6.0
519  *
520  * @param bool $is_external
521  * @param string $host
522  * @return bool
523  */
524 function allowed_http_request_hosts( $is_external, $host ) {
525         if ( ! $is_external && wp_validate_redirect( 'http://' . $host ) )
526                 $is_external = true;
527         return $is_external;
528 }
529
530 /**
531  * Whitelists any domain in a multisite installation for safe HTTP requests.
532  *
533  * Attached to the http_request_host_is_external filter.
534  *
535  * @since 3.6.0
536  *
537  * @param bool $is_external
538  * @param string $host
539  * @return bool
540  */
541 function ms_allowed_http_request_hosts( $is_external, $host ) {
542         global $wpdb;
543         static $queried = array();
544         if ( $is_external )
545                 return $is_external;
546         if ( $host === get_current_site()->domain )
547                 return true;
548         if ( isset( $queried[ $host ] ) )
549                 return $queried[ $host ];
550         $queried[ $host ] = (bool) $wpdb->get_var( $wpdb->prepare( "SELECT domain FROM $wpdb->blogs WHERE domain = %s LIMIT 1", $host ) );
551         return $queried[ $host ];
552 }