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