wp/wp-includes/class-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
 * Standardizes the HTTP requests for WordPress. Handles cookies, gzip encoding and decoding, chunk
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
     6
 * decoding, if HTTP 1.1 and various other difficult HTTP protocol implementations.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
     7
 *
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
     8
 * @link https://core.trac.wordpress.org/ticket/4779 HTTP API Proposal
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
     9
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    10
 * @package WordPress
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    11
 * @subpackage HTTP
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    12
 * @since 2.7.0
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
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    16
 * WordPress HTTP Class for managing HTTP Transports and making HTTP requests.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    17
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    18
 * This class is used to consistently make outgoing HTTP requests easy for developers
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    19
 * while still being compatible with the many PHP configurations under which
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    20
 * WordPress runs.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    21
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    22
 * Debugging includes several actions, which pass different variables for debugging the HTTP API.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    23
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    24
 * @package WordPress
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    25
 * @subpackage HTTP
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    26
 * @since 2.7.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    27
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    28
class WP_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
	/**
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    31
	 * Send an HTTP request to a URI.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    32
	 *
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    33
	 * Please note: The only URI that are supported in the HTTP Transport implementation
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    34
	 * are the HTTP and HTTPS protocols.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    35
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    36
	 * @access public
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    37
	 * @since 2.7.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
	 * @param string       $url  The request URL.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    40
	 * @param string|array $args {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    41
	 *     Optional. Array or string of HTTP request arguments.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    42
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    43
	 *     @type string       $method              Request method. Accepts 'GET', 'POST', 'HEAD', or 'PUT'.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    44
	 *                                             Some transports technically allow others, but should not be
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    45
	 *                                             assumed. Default 'GET'.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    46
	 *     @type int          $timeout             How long the connection should stay open in seconds. Default 5.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    47
	 *     @type int          $redirection         Number of allowed redirects. Not supported by all transports
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    48
	 *                                             Default 5.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    49
	 *     @type string       $httpversion         Version of the HTTP protocol to use. Accepts '1.0' and '1.1'.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    50
	 *                                             Default '1.0'.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    51
	 *     @type string       $user-agent          User-agent value sent.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    52
	 *                                             Default WordPress/' . $wp_version . '; ' . get_bloginfo( 'url' ).
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    53
	 *     @type bool         $reject_unsafe_urls  Whether to pass URLs through {@see wp_http_validate_url()}.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    54
	 *                                             Default false.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    55
	 *     @type bool         $blocking            Whether the calling code requires the result of the request.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    56
	 *                                             If set to false, the request will be sent to the remote server,
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    57
	 *                                             and processing returned to the calling code immediately, the caller
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    58
	 *                                             will know if the request succeeded or failed, but will not receive
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    59
	 *                                             any response from the remote server. Default true.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    60
	 *     @type string|array $headers             Array or string of headers to send with the request.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    61
	 *                                             Default empty array.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    62
	 *     @type array        $cookies             List of cookies to send with the request. Default empty array.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    63
	 *     @type string|array $body                Body to send with the request. Default null.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    64
	 *     @type bool         $compress            Whether to compress the $body when sending the request.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    65
	 *                                             Default false.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    66
	 *     @type bool         $decompress          Whether to decompress a compressed response. If set to false and
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    67
	 *                                             compressed content is returned in the response anyway, it will
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    68
	 *                                             need to be separately decompressed. Default true.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    69
	 *     @type bool         $sslverify           Whether to verify SSL for the request. Default true.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    70
	 *     @type string       sslcertificates      Absolute path to an SSL certificate .crt file.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    71
	 *                                             Default ABSPATH . WPINC . '/certificates/ca-bundle.crt'.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    72
	 *     @type bool         $stream              Whether to stream to a file. If set to true and no filename was
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    73
	 *                                             given, it will be droped it in the WP temp dir and its name will
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    74
	 *                                             be set using the basename of the URL. Default false.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    75
	 *     @type string       $filename            Filename of the file to write to when streaming. $stream must be
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    76
	 *                                             set to true. Default null.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    77
	 *     @type int          $limit_response_size Size in bytes to limit the response to. Default null.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    78
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    79
	 * }
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    80
	 * @return array|WP_Error Array containing 'headers', 'body', 'response', 'cookies', 'filename'.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    81
	 *                        A WP_Error instance upon error.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    82
	 */
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    83
	public function request( $url, $args = array() ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    84
		global $wp_version;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    85
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    86
		$defaults = array(
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    87
			'method' => 'GET',
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    88
			/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    89
			 * Filter the timeout value for an HTTP request.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    90
			 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    91
			 * @since 2.7.0
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    92
			 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    93
			 * @param int $timeout_value Time in seconds until a request times out.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    94
			 *                           Default 5.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    95
			 */
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    96
			'timeout' => apply_filters( 'http_request_timeout', 5 ),
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    97
			/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    98
			 * Filter the number of redirects allowed during an HTTP request.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    99
			 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   100
			 * @since 2.7.0
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   101
			 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   102
			 * @param int $redirect_count Number of redirects allowed. Default 5.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   103
			 */
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   104
			'redirection' => apply_filters( 'http_request_redirection_count', 5 ),
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   105
			/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   106
			 * Filter the version of the HTTP protocol used in a request.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   107
			 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   108
			 * @since 2.7.0
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   109
			 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   110
			 * @param string $version Version of HTTP used. Accepts '1.0' and '1.1'.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   111
			 *                        Default '1.0'.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   112
			 */
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   113
			'httpversion' => apply_filters( 'http_request_version', '1.0' ),
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   114
			/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   115
			 * Filter the user agent value sent with an HTTP request.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   116
			 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   117
			 * @since 2.7.0
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   118
			 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   119
			 * @param string $user_agent WordPress user agent string.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   120
			 */
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   121
			'user-agent' => apply_filters( 'http_headers_useragent', 'WordPress/' . $wp_version . '; ' . get_bloginfo( 'url' ) ),
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   122
			/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   123
			 * Filter whether to pass URLs through wp_http_validate_url() in an HTTP request.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   124
			 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   125
			 * @since 3.6.0
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   126
			 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   127
			 * @param bool $pass_url Whether to pass URLs through wp_http_validate_url().
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   128
			 *                       Default false.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   129
			 */
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   130
			'reject_unsafe_urls' => apply_filters( 'http_request_reject_unsafe_urls', false ),
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   131
			'blocking' => true,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   132
			'headers' => array(),
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   133
			'cookies' => array(),
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   134
			'body' => null,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   135
			'compress' => false,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   136
			'decompress' => true,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   137
			'sslverify' => true,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   138
			'sslcertificates' => ABSPATH . WPINC . '/certificates/ca-bundle.crt',
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   139
			'stream' => false,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   140
			'filename' => null,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   141
			'limit_response_size' => null,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   142
		);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   143
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   144
		// Pre-parse for the HEAD checks.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   145
		$args = wp_parse_args( $args );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   146
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   147
		// By default, Head requests do not cause redirections.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   148
		if ( isset($args['method']) && 'HEAD' == $args['method'] )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   149
			$defaults['redirection'] = 0;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   150
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   151
		$r = wp_parse_args( $args, $defaults );
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   152
		/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   153
		 * Filter the arguments used in an HTTP request.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   154
		 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   155
		 * @since 2.7.0
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   156
		 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   157
		 * @param array  $r   An array of HTTP request arguments.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   158
		 * @param string $url The request URL.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   159
		 */
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   160
		$r = apply_filters( 'http_request_args', $r, $url );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   161
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   162
		// The transports decrement this, store a copy of the original value for loop purposes.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   163
		if ( ! isset( $r['_redirection'] ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   164
			$r['_redirection'] = $r['redirection'];
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   165
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   166
		/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   167
		 * Filter whether to preempt an HTTP request's return.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   168
		 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   169
		 * Returning a truthy value to the filter will short-circuit
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   170
		 * the HTTP request and return early with that value.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   171
		 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   172
		 * @since 2.9.0
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   173
		 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   174
		 * @param bool   $preempt Whether to preempt an HTTP request return. Default false.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   175
		 * @param array  $r       HTTP request arguments.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   176
		 * @param string $url     The request URL.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   177
		 */
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   178
		$pre = apply_filters( 'pre_http_request', false, $r, $url );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   179
		if ( false !== $pre )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   180
			return $pre;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   181
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   182
		if ( function_exists( 'wp_kses_bad_protocol' ) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   183
			if ( $r['reject_unsafe_urls'] )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   184
				$url = wp_http_validate_url( $url );
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   185
			if ( $url ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   186
				$url = wp_kses_bad_protocol( $url, array( 'http', 'https', 'ssl' ) );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   187
			}
0
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
		$arrURL = @parse_url( $url );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   191
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   192
		if ( empty( $url ) || empty( $arrURL['scheme'] ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   193
			return new WP_Error('http_request_failed', __('A valid URL was not provided.'));
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   194
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   195
		if ( $this->block_request( $url ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   196
			return new WP_Error( 'http_request_failed', __( 'User has blocked requests through HTTP.' ) );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   197
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   198
		/*
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   199
		 * Determine if this is a https call and pass that on to the transport functions
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   200
		 * so that we can blacklist the transports that do not support ssl verification
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   201
		 */
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   202
		$r['ssl'] = $arrURL['scheme'] == 'https' || $arrURL['scheme'] == 'ssl';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   203
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   204
		// Determine if this request is to OUR install of WordPress.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   205
		$homeURL = parse_url( get_bloginfo( 'url' ) );
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   206
		$r['local'] = 'localhost' == $arrURL['host'] || ( isset( $homeURL['host'] ) && $homeURL['host'] == $arrURL['host'] );
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   207
		unset( $homeURL );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   208
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   209
		/*
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   210
		 * If we are streaming to a file but no filename was given drop it in the WP temp dir
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   211
		 * and pick its name using the basename of the $url.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   212
		 */
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   213
		if ( $r['stream']  && empty( $r['filename'] ) ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   214
			$r['filename'] = wp_unique_filename( get_temp_dir(), basename( $url ) );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   215
		}
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   216
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   217
		/*
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   218
		 * Force some settings if we are streaming to a file and check for existence and perms
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   219
		 * of destination directory.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   220
		 */
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   221
		if ( $r['stream'] ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   222
			$r['blocking'] = true;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   223
			if ( ! wp_is_writable( dirname( $r['filename'] ) ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   224
				return new WP_Error( 'http_request_failed', __( 'Destination directory for file streaming does not exist or is not writable.' ) );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   225
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   226
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   227
		if ( is_null( $r['headers'] ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   228
			$r['headers'] = array();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   229
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   230
		if ( ! is_array( $r['headers'] ) ) {
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   231
			$processedHeaders = self::processHeaders( $r['headers'], $url );
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   232
			$r['headers'] = $processedHeaders['headers'];
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   233
		}
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 ( isset( $r['headers']['User-Agent'] ) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   236
			$r['user-agent'] = $r['headers']['User-Agent'];
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   237
			unset( $r['headers']['User-Agent'] );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   238
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   239
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   240
		if ( isset( $r['headers']['user-agent'] ) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   241
			$r['user-agent'] = $r['headers']['user-agent'];
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   242
			unset( $r['headers']['user-agent'] );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   243
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   244
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   245
		if ( '1.1' == $r['httpversion'] && !isset( $r['headers']['connection'] ) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   246
			$r['headers']['connection'] = 'close';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   247
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   248
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   249
		// Construct Cookie: header if any cookies are set.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   250
		self::buildCookieHeader( $r );
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   251
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   252
		// Avoid issues where mbstring.func_overload is enabled.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   253
		mbstring_binary_safe_encoding();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   254
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   255
		if ( ! isset( $r['headers']['Accept-Encoding'] ) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   256
			if ( $encoding = WP_Http_Encoding::accept_encoding( $url, $r ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   257
				$r['headers']['Accept-Encoding'] = $encoding;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   258
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   259
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   260
		if ( ( ! is_null( $r['body'] ) && '' != $r['body'] ) || 'POST' == $r['method'] || 'PUT' == $r['method'] ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   261
			if ( is_array( $r['body'] ) || is_object( $r['body'] ) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   262
				$r['body'] = http_build_query( $r['body'], null, '&' );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   263
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   264
				if ( ! isset( $r['headers']['Content-Type'] ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   265
					$r['headers']['Content-Type'] = 'application/x-www-form-urlencoded; charset=' . get_option( 'blog_charset' );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   266
			}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   267
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   268
			if ( '' === $r['body'] )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   269
				$r['body'] = null;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   270
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   271
			if ( ! isset( $r['headers']['Content-Length'] ) && ! isset( $r['headers']['content-length'] ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   272
				$r['headers']['Content-Length'] = strlen( $r['body'] );
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
		$response = $this->_dispatch_request( $url, $r );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   276
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   277
		reset_mbstring_encoding();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   278
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   279
		if ( is_wp_error( $response ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   280
			return $response;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   281
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   282
		// Append cookies that were used in this request to the response
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   283
		if ( ! empty( $r['cookies'] ) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   284
			$cookies_set = wp_list_pluck( $response['cookies'], 'name' );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   285
			foreach ( $r['cookies'] as $cookie ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   286
				if ( ! in_array( $cookie->name, $cookies_set ) && $cookie->test( $url ) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   287
					$response['cookies'][] = $cookie;
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
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   292
		return $response;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   293
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   294
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   295
	/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   296
	 * Tests which transports are capable of supporting the request.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   297
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   298
	 * @since 3.2.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   299
	 * @access private
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   300
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   301
	 * @param array $args Request arguments
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   302
	 * @param string $url URL to Request
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   303
	 *
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   304
	 * @return string|false Class name for the first transport that claims to support the request. False if no transport claims to support the request.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   305
	 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   306
	public function _get_first_available_transport( $args, $url = null ) {
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   307
		/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   308
		 * Filter which HTTP transports are available and in what order.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   309
		 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   310
		 * @since 3.7.0
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   311
		 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   312
		 * @param array  $value Array of HTTP transports to check. Default array contains
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   313
		 *                      'curl', and 'streams', in that order.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   314
		 * @param array  $args  HTTP request arguments.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   315
		 * @param string $url   The URL to request.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   316
		 */
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   317
		$request_order = apply_filters( 'http_api_transports', array( 'curl', 'streams' ), $args, $url );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   318
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   319
		// Loop over each transport on each HTTP request looking for one which will serve this request's needs.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   320
		foreach ( $request_order as $transport ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   321
			$class = 'WP_HTTP_' . $transport;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   322
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   323
			// Check to see if this transport is a possibility, calls the transport statically.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   324
			if ( !call_user_func( array( $class, 'test' ), $args, $url ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   325
				continue;
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 $class;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   328
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   329
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   330
		return false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   331
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   332
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   333
	/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   334
	 * Dispatches a HTTP request to a supporting transport.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   335
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   336
	 * Tests each transport in order to find a transport which matches the request arguments.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   337
	 * Also caches the transport instance to be used later.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   338
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   339
	 * The order for requests is cURL, and then PHP Streams.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   340
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   341
	 * @since 3.2.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   342
	 * @access private
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   343
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   344
	 * @param string $url URL to Request
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   345
	 * @param array $args Request arguments
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   346
	 * @return array|WP_Error Array containing 'headers', 'body', 'response', 'cookies', 'filename'. A WP_Error instance upon error
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   347
	 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   348
	private function _dispatch_request( $url, $args ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   349
		static $transports = array();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   350
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   351
		$class = $this->_get_first_available_transport( $args, $url );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   352
		if ( !$class )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   353
			return new WP_Error( 'http_failure', __( 'There are no HTTP transports available which can complete the requested request.' ) );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   354
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   355
		// Transport claims to support request, instantiate it and give it a whirl.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   356
		if ( empty( $transports[$class] ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   357
			$transports[$class] = new $class;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   358
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   359
		$response = $transports[$class]->request( $url, $args );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   360
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   361
		/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   362
		 * Fires after an HTTP API response is received and before the response is returned.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   363
		 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   364
		 * @since 2.8.0
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   365
		 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   366
		 * @param array|WP_Error $response HTTP response or WP_Error object.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   367
		 * @param string         $context  Context under which the hook is fired.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   368
		 * @param string         $class    HTTP transport used.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   369
		 * @param array          $args     HTTP request arguments.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   370
		 * @param string         $url      The request URL.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   371
		 */
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   372
		do_action( 'http_api_debug', $response, 'response', $class, $args, $url );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   373
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   374
		if ( is_wp_error( $response ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   375
			return $response;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   376
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   377
		/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   378
		 * Filter the HTTP API response immediately before the response is returned.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   379
		 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   380
		 * @since 2.9.0
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   381
		 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   382
		 * @param array  $response HTTP response.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   383
		 * @param array  $args     HTTP request arguments.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   384
		 * @param string $url      The request URL.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   385
		 */
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   386
		return apply_filters( 'http_response', $response, $args, $url );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   387
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   388
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   389
	/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   390
	 * Uses the POST HTTP method.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   391
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   392
	 * Used for sending data that is expected to be in the body.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   393
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   394
	 * @access public
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   395
	 * @since 2.7.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   396
	 *
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   397
	 * @param string       $url  The request URL.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   398
	 * @param string|array $args Optional. Override the defaults.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   399
	 * @return array|WP_Error Array containing 'headers', 'body', 'response', 'cookies', 'filename'. A WP_Error instance upon error
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   400
	 */
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   401
	public function post($url, $args = array()) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   402
		$defaults = array('method' => 'POST');
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   403
		$r = wp_parse_args( $args, $defaults );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   404
		return $this->request($url, $r);
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
	 * Uses the GET HTTP method.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   409
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   410
	 * Used for sending data that is expected to be in the body.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   411
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   412
	 * @access public
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   413
	 * @since 2.7.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   414
	 *
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   415
	 * @param string $url The request URL.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   416
	 * @param string|array $args Optional. Override the defaults.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   417
	 * @return array|WP_Error Array containing 'headers', 'body', 'response', 'cookies', 'filename'. A WP_Error instance upon error
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   418
	 */
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   419
	public function get($url, $args = array()) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   420
		$defaults = array('method' => 'GET');
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   421
		$r = wp_parse_args( $args, $defaults );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   422
		return $this->request($url, $r);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   423
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   424
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   425
	/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   426
	 * Uses the HEAD HTTP method.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   427
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   428
	 * Used for sending data that is expected to be in the body.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   429
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   430
	 * @access public
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   431
	 * @since 2.7.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   432
	 *
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   433
	 * @param string $url The request URL.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   434
	 * @param string|array $args Optional. Override the defaults.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   435
	 * @return array|WP_Error Array containing 'headers', 'body', 'response', 'cookies', 'filename'. A WP_Error instance upon error
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   436
	 */
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   437
	public function head($url, $args = array()) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   438
		$defaults = array('method' => 'HEAD');
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   439
		$r = wp_parse_args( $args, $defaults );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   440
		return $this->request($url, $r);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   441
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   442
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   443
	/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   444
	 * Parses the responses and splits the parts into headers and body.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   445
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   446
	 * @access public
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   447
	 * @static
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   448
	 * @since 2.7.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   449
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   450
	 * @param string $strResponse The full response string
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   451
	 * @return array Array with 'headers' and 'body' keys.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   452
	 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   453
	public static function processResponse($strResponse) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   454
		$res = explode("\r\n\r\n", $strResponse, 2);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   455
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   456
		return array('headers' => $res[0], 'body' => isset($res[1]) ? $res[1] : '');
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   457
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   458
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   459
	/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   460
	 * Transform header string into an array.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   461
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   462
	 * If an array is given then it is assumed to be raw header data with numeric keys with the
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   463
	 * headers as the values. No headers must be passed that were already processed.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   464
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   465
	 * @access public
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   466
	 * @static
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   467
	 * @since 2.7.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   468
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   469
	 * @param string|array $headers
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   470
	 * @param string $url The URL that was requested
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   471
	 * @return array Processed string headers. If duplicate headers are encountered,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   472
	 * 					Then a numbered array is returned as the value of that header-key.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   473
	 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   474
	public static function processHeaders( $headers, $url = '' ) {
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   475
		// Split headers, one per array element.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   476
		if ( is_string($headers) ) {
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   477
			// Tolerate line terminator: CRLF = LF (RFC 2616 19.3).
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   478
			$headers = str_replace("\r\n", "\n", $headers);
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   479
			/*
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   480
			 * Unfold folded header fields. LWS = [CRLF] 1*( SP | HT ) <US-ASCII SP, space (32)>,
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   481
			 * <US-ASCII HT, horizontal-tab (9)> (RFC 2616 2.2).
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   482
			 */
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   483
			$headers = preg_replace('/\n[ \t]/', ' ', $headers);
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   484
			// Create the headers array.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   485
			$headers = explode("\n", $headers);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   486
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   487
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   488
		$response = array('code' => 0, 'message' => '');
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   489
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   490
		/*
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   491
		 * If a redirection has taken place, The headers for each page request may have been passed.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   492
		 * In this case, determine the final HTTP header and parse from there.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   493
		 */
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   494
		for ( $i = count($headers)-1; $i >= 0; $i-- ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   495
			if ( !empty($headers[$i]) && false === strpos($headers[$i], ':') ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   496
				$headers = array_splice($headers, $i);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   497
				break;
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
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   501
		$cookies = array();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   502
		$newheaders = array();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   503
		foreach ( (array) $headers as $tempheader ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   504
			if ( empty($tempheader) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   505
				continue;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   506
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   507
			if ( false === strpos($tempheader, ':') ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   508
				$stack = explode(' ', $tempheader, 3);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   509
				$stack[] = '';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   510
				list( , $response['code'], $response['message']) = $stack;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   511
				continue;
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
			list($key, $value) = explode(':', $tempheader, 2);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   515
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   516
			$key = strtolower( $key );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   517
			$value = trim( $value );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   518
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   519
			if ( isset( $newheaders[ $key ] ) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   520
				if ( ! is_array( $newheaders[ $key ] ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   521
					$newheaders[$key] = array( $newheaders[ $key ] );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   522
				$newheaders[ $key ][] = $value;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   523
			} else {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   524
				$newheaders[ $key ] = $value;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   525
			}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   526
			if ( 'set-cookie' == $key )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   527
				$cookies[] = new WP_Http_Cookie( $value, $url );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   528
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   529
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   530
		// Cast the Response Code to an int
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   531
		$response['code'] = intval( $response['code'] );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   532
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   533
		return array('response' => $response, 'headers' => $newheaders, 'cookies' => $cookies);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   534
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   535
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   536
	/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   537
	 * Takes the arguments for a ::request() and checks for the cookie array.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   538
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   539
	 * If it's found, then it upgrades any basic name => value pairs to WP_Http_Cookie instances,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   540
	 * which are each parsed into strings and added to the Cookie: header (within the arguments array).
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   541
	 * Edits the array by reference.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   542
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   543
	 * @access public
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   544
	 * @version 2.8.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   545
	 * @static
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   546
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   547
	 * @param array $r Full array of args passed into ::request()
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   548
	 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   549
	public static function buildCookieHeader( &$r ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   550
		if ( ! empty($r['cookies']) ) {
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   551
			// Upgrade any name => value cookie pairs to WP_HTTP_Cookie instances.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   552
			foreach ( $r['cookies'] as $name => $value ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   553
				if ( ! is_object( $value ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   554
					$r['cookies'][ $name ] = new WP_HTTP_Cookie( array( 'name' => $name, 'value' => $value ) );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   555
			}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   556
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   557
			$cookies_header = '';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   558
			foreach ( (array) $r['cookies'] as $cookie ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   559
				$cookies_header .= $cookie->getHeaderValue() . '; ';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   560
			}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   561
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   562
			$cookies_header = substr( $cookies_header, 0, -2 );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   563
			$r['headers']['cookie'] = $cookies_header;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   564
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   565
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   566
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   567
	/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   568
	 * Decodes chunk transfer-encoding, based off the HTTP 1.1 specification.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   569
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   570
	 * Based off the HTTP http_encoding_dechunk function.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   571
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   572
	 * @link http://tools.ietf.org/html/rfc2616#section-19.4.6 Process for chunked decoding.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   573
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   574
	 * @access public
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   575
	 * @since 2.7.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   576
	 * @static
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   577
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   578
	 * @param string $body Body content
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   579
	 * @return string Chunked decoded body on success or raw body on failure.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   580
	 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   581
	public static function chunkTransferDecode( $body ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   582
		// The body is not chunked encoded or is malformed.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   583
		if ( ! preg_match( '/^([0-9a-f]+)[^\r\n]*\r\n/i', trim( $body ) ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   584
			return $body;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   585
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   586
		$parsed_body = '';
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   587
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   588
		// We'll be altering $body, so need a backup in case of error.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   589
		$body_original = $body;
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   590
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   591
		while ( true ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   592
			$has_chunk = (bool) preg_match( '/^([0-9a-f]+)[^\r\n]*\r\n/i', $body, $match );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   593
			if ( ! $has_chunk || empty( $match[1] ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   594
				return $body_original;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   595
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   596
			$length = hexdec( $match[1] );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   597
			$chunk_length = strlen( $match[0] );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   598
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   599
			// Parse out the chunk of data.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   600
			$parsed_body .= substr( $body, $chunk_length, $length );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   601
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   602
			// Remove the chunk from the raw data.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   603
			$body = substr( $body, $length + $chunk_length );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   604
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   605
			// End of the document.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   606
			if ( '0' === trim( $body ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   607
				return $parsed_body;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   608
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   609
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   610
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   611
	/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   612
	 * Block requests through the proxy.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   613
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   614
	 * Those who are behind a proxy and want to prevent access to certain hosts may do so. This will
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   615
	 * prevent plugins from working and core functionality, if you don't include api.wordpress.org.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   616
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   617
	 * You block external URL requests by defining WP_HTTP_BLOCK_EXTERNAL as true in your wp-config.php
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   618
	 * file and this will only allow localhost and your blog to make requests. The constant
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   619
	 * WP_ACCESSIBLE_HOSTS will allow additional hosts to go through for requests. The format of the
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   620
	 * WP_ACCESSIBLE_HOSTS constant is a comma separated list of hostnames to allow, wildcard domains
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   621
	 * are supported, eg *.wordpress.org will allow for all subdomains of wordpress.org to be contacted.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   622
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   623
	 * @since 2.8.0
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   624
	 * @link https://core.trac.wordpress.org/ticket/8927 Allow preventing external requests.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   625
	 * @link https://core.trac.wordpress.org/ticket/14636 Allow wildcard domains in WP_ACCESSIBLE_HOSTS
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   626
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   627
	 * @param string $uri URI of url.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   628
	 * @return bool True to block, false to allow.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   629
	 */
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   630
	public function block_request($uri) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   631
		// We don't need to block requests, because nothing is blocked.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   632
		if ( ! defined( 'WP_HTTP_BLOCK_EXTERNAL' ) || ! WP_HTTP_BLOCK_EXTERNAL )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   633
			return false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   634
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   635
		$check = parse_url($uri);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   636
		if ( ! $check )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   637
			return true;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   638
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   639
		$home = parse_url( get_option('siteurl') );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   640
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   641
		// Don't block requests back to ourselves by default.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   642
		if ( 'localhost' == $check['host'] || ( isset( $home['host'] ) && $home['host'] == $check['host'] ) ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   643
			/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   644
			 * Filter whether to block local requests through the proxy.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   645
			 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   646
			 * @since 2.8.0
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   647
			 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   648
			 * @param bool $block Whether to block local requests through proxy.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   649
			 *                    Default false.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   650
			 */
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   651
			return apply_filters( 'block_local_requests', false );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   652
		}
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   653
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   654
		if ( !defined('WP_ACCESSIBLE_HOSTS') )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   655
			return true;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   656
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   657
		static $accessible_hosts;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   658
		static $wildcard_regex = false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   659
		if ( null == $accessible_hosts ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   660
			$accessible_hosts = preg_split('|,\s*|', WP_ACCESSIBLE_HOSTS);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   661
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   662
			if ( false !== strpos(WP_ACCESSIBLE_HOSTS, '*') ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   663
				$wildcard_regex = array();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   664
				foreach ( $accessible_hosts as $host )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   665
					$wildcard_regex[] = str_replace( '\*', '.+', preg_quote( $host, '/' ) );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   666
				$wildcard_regex = '/^(' . implode('|', $wildcard_regex) . ')$/i';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   667
			}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   668
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   669
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   670
		if ( !empty($wildcard_regex) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   671
			return !preg_match($wildcard_regex, $check['host']);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   672
		else
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   673
			return !in_array( $check['host'], $accessible_hosts ); //Inverse logic, If it's in the array, then we can't access it.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   674
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   675
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   676
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   677
	/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   678
	 * A wrapper for PHP's parse_url() function that handles edgecases in < PHP 5.4.7
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   679
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   680
	 * PHP 5.4.7 expanded parse_url()'s ability to handle non-absolute url's, including
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   681
	 * schemeless and relative url's with :// in the path, this works around those
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   682
	 * limitations providing a standard output on PHP 5.2~5.4+.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   683
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   684
	 * Error suppression is used as prior to PHP 5.3.3, an E_WARNING would be generated
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   685
	 * when URL parsing failed.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   686
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   687
	 * @since 4.1.0
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   688
	 * @access protected
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   689
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   690
	 * @param string $url The URL to parse.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   691
	 * @return bool|array False on failure; Array of URL components on success;
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   692
	 *                    See parse_url()'s return values.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   693
	 */
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   694
	protected static function parse_url( $url ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   695
		$parts = @parse_url( $url );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   696
		if ( ! $parts ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   697
			// < PHP 5.4.7 compat, trouble with relative paths including a scheme break in the path
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   698
			if ( '/' == $url[0] && false !== strpos( $url, '://' ) ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   699
				// Since we know it's a relative path, prefix with a scheme/host placeholder and try again
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   700
				if ( ! $parts = @parse_url( 'placeholder://placeholder' . $url ) ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   701
					return $parts;
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   702
				}
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   703
				// Remove the placeholder values
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   704
				unset( $parts['scheme'], $parts['host'] );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   705
			} else {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   706
				return $parts;
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   707
			}
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   708
		}
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   709
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   710
		// < PHP 5.4.7 compat, doesn't detect schemeless URL's host field
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   711
		if ( '//' == substr( $url, 0, 2 ) && ! isset( $parts['host'] ) ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   712
			list( $parts['host'], $slashless_path ) = explode( '/', substr( $parts['path'], 2 ), 2 );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   713
			$parts['path'] = "/{$slashless_path}";
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   714
		}
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   715
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   716
		return $parts;
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   717
	}
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   718
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   719
	/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   720
	 * Converts a relative URL to an absolute URL relative to a given URL.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   721
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   722
	 * If an Absolute URL is provided, no processing of that URL is done.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   723
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   724
	 * @since 3.4.0
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   725
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   726
	 * @access public
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   727
	 * @param string $maybe_relative_path The URL which might be relative
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   728
	 * @param string $url                 The URL which $maybe_relative_path is relative to
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   729
	 * @return string An Absolute URL, in a failure condition where the URL cannot be parsed, the relative URL will be returned.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   730
	 */
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   731
	public static function make_absolute_url( $maybe_relative_path, $url ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   732
		if ( empty( $url ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   733
			return $maybe_relative_path;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   734
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   735
		if ( ! $url_parts = WP_HTTP::parse_url( $url ) ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   736
			return $maybe_relative_path;
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   737
		}
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   738
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   739
		if ( ! $relative_url_parts = WP_HTTP::parse_url( $maybe_relative_path ) ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   740
			return $maybe_relative_path;
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   741
		}
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   742
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   743
		// Check for a scheme on the 'relative' url
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   744
		if ( ! empty( $relative_url_parts['scheme'] ) ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   745
			return $maybe_relative_path;
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   746
		}
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   747
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   748
		$absolute_path = $url_parts['scheme'] . '://';
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   749
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   750
		// Schemeless URL's will make it this far, so we check for a host in the relative url and convert it to a protocol-url
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   751
		if ( isset( $relative_url_parts['host'] ) ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   752
			$absolute_path .= $relative_url_parts['host'];
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   753
			if ( isset( $relative_url_parts['port'] ) )
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   754
				$absolute_path .= ':' . $relative_url_parts['port'];
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   755
		} else {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   756
			$absolute_path .= $url_parts['host'];
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   757
			if ( isset( $url_parts['port'] ) )
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   758
				$absolute_path .= ':' . $url_parts['port'];
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   759
		}
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   760
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   761
		// Start off with the Absolute URL path.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   762
		$path = ! empty( $url_parts['path'] ) ? $url_parts['path'] : '/';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   763
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   764
		// If it's a root-relative path, then great.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   765
		if ( ! empty( $relative_url_parts['path'] ) && '/' == $relative_url_parts['path'][0] ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   766
			$path = $relative_url_parts['path'];
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   767
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   768
		// Else it's a relative path.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   769
		} elseif ( ! empty( $relative_url_parts['path'] ) ) {
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   770
			// Strip off any file components from the absolute path.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   771
			$path = substr( $path, 0, strrpos( $path, '/' ) + 1 );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   772
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   773
			// Build the new path.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   774
			$path .= $relative_url_parts['path'];
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   775
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   776
			// Strip all /path/../ out of the path.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   777
			while ( strpos( $path, '../' ) > 1 ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   778
				$path = preg_replace( '![^/]+/\.\./!', '', $path );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   779
			}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   780
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   781
			// Strip any final leading ../ from the path.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   782
			$path = preg_replace( '!^/(\.\./)+!', '', $path );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   783
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   784
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   785
		// Add the Query string.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   786
		if ( ! empty( $relative_url_parts['query'] ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   787
			$path .= '?' . $relative_url_parts['query'];
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   788
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   789
		return $absolute_path . '/' . ltrim( $path, '/' );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   790
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   791
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   792
	/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   793
	 * Handles HTTP Redirects and follows them if appropriate.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   794
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   795
	 * @since 3.7.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   796
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   797
	 * @param string $url The URL which was requested.
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   798
	 * @param array $args The Arguments which were used to make the request.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   799
	 * @param array $response The Response of the HTTP request.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   800
	 * @return false|object False if no redirect is present, a WP_HTTP or WP_Error result otherwise.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   801
	 */
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   802
	public static function handle_redirects( $url, $args, $response ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   803
		// If no redirects are present, or, redirects were not requested, perform no action.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   804
		if ( ! isset( $response['headers']['location'] ) || 0 === $args['_redirection'] )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   805
			return false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   806
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   807
		// Only perform redirections on redirection http codes.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   808
		if ( $response['response']['code'] > 399 || $response['response']['code'] < 300 )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   809
			return false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   810
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   811
		// Don't redirect if we've run out of redirects.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   812
		if ( $args['redirection']-- <= 0 )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   813
			return new WP_Error( 'http_request_failed', __('Too many redirects.') );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   814
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   815
		$redirect_location = $response['headers']['location'];
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   816
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   817
		// If there were multiple Location headers, use the last header specified.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   818
		if ( is_array( $redirect_location ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   819
			$redirect_location = array_pop( $redirect_location );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   820
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   821
		$redirect_location = WP_HTTP::make_absolute_url( $redirect_location, $url );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   822
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   823
		// POST requests should not POST to a redirected location.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   824
		if ( 'POST' == $args['method'] ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   825
			if ( in_array( $response['response']['code'], array( 302, 303 ) ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   826
				$args['method'] = 'GET';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   827
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   828
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   829
		// Include valid cookies in the redirect process.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   830
		if ( ! empty( $response['cookies'] ) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   831
			foreach ( $response['cookies'] as $cookie ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   832
				if ( $cookie->test( $redirect_location ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   833
					$args['cookies'][] = $cookie;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   834
			}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   835
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   836
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   837
		return wp_remote_request( $redirect_location, $args );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   838
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   839
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   840
	/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   841
	 * Determines if a specified string represents an IP address or not.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   842
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   843
	 * This function also detects the type of the IP address, returning either
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   844
	 * '4' or '6' to represent a IPv4 and IPv6 address respectively.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   845
	 * This does not verify if the IP is a valid IP, only that it appears to be
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   846
	 * an IP address.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   847
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   848
	 * @see http://home.deds.nl/~aeron/regex/ for IPv6 regex
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   849
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   850
	 * @since 3.7.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   851
	 * @static
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   852
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   853
	 * @param string $maybe_ip A suspected IP address
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   854
	 * @return integer|bool Upon success, '4' or '6' to represent a IPv4 or IPv6 address, false upon failure
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   855
	 */
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   856
	public static function is_ip_address( $maybe_ip ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   857
		if ( preg_match( '/^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/', $maybe_ip ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   858
			return 4;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   859
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   860
		if ( false !== strpos( $maybe_ip, ':' ) && preg_match( '/^(((?=.*(::))(?!.*\3.+\3))\3?|([\dA-F]{1,4}(\3|:\b|$)|\2))(?4){5}((?4){2}|(((2[0-4]|1\d|[1-9])?\d|25[0-5])\.?\b){4})$/i', trim( $maybe_ip, ' []' ) ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   861
			return 6;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   862
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   863
		return false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   864
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   865
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   866
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   867
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   868
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   869
 * HTTP request method uses PHP Streams to retrieve the url.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   870
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   871
 * @since 2.7.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   872
 * @since 3.7.0 Combined with the fsockopen transport and switched to stream_socket_client().
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   873
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   874
class WP_Http_Streams {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   875
	/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   876
	 * Send a HTTP request to a URI using PHP Streams.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   877
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   878
	 * @see WP_Http::request For default options descriptions.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   879
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   880
	 * @since 2.7.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   881
	 * @since 3.7.0 Combined with the fsockopen transport and switched to stream_socket_client().
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   882
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   883
	 * @access public
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   884
	 * @param string $url The request URL.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   885
	 * @param string|array $args Optional. Override the defaults.
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   886
	 * @return array|WP_Error Array containing 'headers', 'body', 'response', 'cookies', 'filename'. A WP_Error instance upon error
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   887
	 */
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   888
	public function request($url, $args = array()) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   889
		$defaults = array(
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   890
			'method' => 'GET', 'timeout' => 5,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   891
			'redirection' => 5, 'httpversion' => '1.0',
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   892
			'blocking' => true,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   893
			'headers' => array(), 'body' => null, 'cookies' => array()
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   894
		);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   895
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   896
		$r = wp_parse_args( $args, $defaults );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   897
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   898
		if ( isset( $r['headers']['User-Agent'] ) ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   899
			$r['user-agent'] = $r['headers']['User-Agent'];
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   900
			unset( $r['headers']['User-Agent'] );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   901
		} elseif ( isset( $r['headers']['user-agent'] ) ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   902
			$r['user-agent'] = $r['headers']['user-agent'];
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   903
			unset( $r['headers']['user-agent'] );
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   904
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   905
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   906
		// Construct Cookie: header if any cookies are set.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   907
		WP_Http::buildCookieHeader( $r );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   908
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   909
		$arrURL = parse_url($url);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   910
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   911
		$connect_host = $arrURL['host'];
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   912
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   913
		$secure_transport = ( $arrURL['scheme'] == 'ssl' || $arrURL['scheme'] == 'https' );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   914
		if ( ! isset( $arrURL['port'] ) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   915
			if ( $arrURL['scheme'] == 'ssl' || $arrURL['scheme'] == 'https' ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   916
				$arrURL['port'] = 443;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   917
				$secure_transport = true;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   918
			} else {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   919
				$arrURL['port'] = 80;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   920
			}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   921
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   922
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   923
		// Always pass a Path, defaulting to the root in cases such as http://example.com
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   924
		if ( ! isset( $arrURL['path'] ) ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   925
			$arrURL['path'] = '/';
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   926
		}
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   927
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   928
		if ( isset( $r['headers']['Host'] ) || isset( $r['headers']['host'] ) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   929
			if ( isset( $r['headers']['Host'] ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   930
				$arrURL['host'] = $r['headers']['Host'];
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   931
			else
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   932
				$arrURL['host'] = $r['headers']['host'];
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   933
			unset( $r['headers']['Host'], $r['headers']['host'] );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   934
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   935
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   936
		/*
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   937
		 * Certain versions of PHP have issues with 'localhost' and IPv6, It attempts to connect
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   938
		 * to ::1, which fails when the server is not set up for it. For compatibility, always
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   939
		 * connect to the IPv4 address.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   940
		 */
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   941
		if ( 'localhost' == strtolower( $connect_host ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   942
			$connect_host = '127.0.0.1';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   943
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   944
		$connect_host = $secure_transport ? 'ssl://' . $connect_host : 'tcp://' . $connect_host;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   945
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   946
		$is_local = isset( $r['local'] ) && $r['local'];
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   947
		$ssl_verify = isset( $r['sslverify'] ) && $r['sslverify'];
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   948
		if ( $is_local ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   949
			/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   950
			 * Filter whether SSL should be verified for local requests.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   951
			 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   952
			 * @since 2.8.0
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   953
			 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   954
			 * @param bool $ssl_verify Whether to verify the SSL connection. Default true.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   955
			 */
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   956
			$ssl_verify = apply_filters( 'https_local_ssl_verify', $ssl_verify );
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   957
		} elseif ( ! $is_local ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   958
			/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   959
			 * Filter whether SSL should be verified for non-local requests.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   960
			 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   961
			 * @since 2.8.0
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   962
			 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   963
			 * @param bool $ssl_verify Whether to verify the SSL connection. Default true.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   964
			 */
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   965
			$ssl_verify = apply_filters( 'https_ssl_verify', $ssl_verify );
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   966
		}
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   967
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   968
		$proxy = new WP_HTTP_Proxy();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   969
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   970
		$context = stream_context_create( array(
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   971
			'ssl' => array(
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   972
				'verify_peer' => $ssl_verify,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   973
				//'CN_match' => $arrURL['host'], // This is handled by self::verify_ssl_certificate()
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   974
				'capture_peer_cert' => $ssl_verify,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   975
				'SNI_enabled' => true,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   976
				'cafile' => $r['sslcertificates'],
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   977
				'allow_self_signed' => ! $ssl_verify,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   978
			)
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   979
		) );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   980
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   981
		$timeout = (int) floor( $r['timeout'] );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   982
		$utimeout = $timeout == $r['timeout'] ? 0 : 1000000 * $r['timeout'] % 1000000;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   983
		$connect_timeout = max( $timeout, 1 );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   984
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   985
		// Store error number.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   986
		$connection_error = null;
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   987
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   988
		// Store error string.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   989
		$connection_error_str = null;
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   990
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   991
		if ( !WP_DEBUG ) {
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   992
			// In the event that the SSL connection fails, silence the many PHP Warnings.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   993
			if ( $secure_transport )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   994
				$error_reporting = error_reporting(0);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   995
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   996
			if ( $proxy->is_enabled() && $proxy->send_through_proxy( $url ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   997
				$handle = @stream_socket_client( 'tcp://' . $proxy->host() . ':' . $proxy->port(), $connection_error, $connection_error_str, $connect_timeout, STREAM_CLIENT_CONNECT, $context );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   998
			else
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   999
				$handle = @stream_socket_client( $connect_host . ':' . $arrURL['port'], $connection_error, $connection_error_str, $connect_timeout, STREAM_CLIENT_CONNECT, $context );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1000
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1001
			if ( $secure_transport )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1002
				error_reporting( $error_reporting );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1003
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1004
		} else {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1005
			if ( $proxy->is_enabled() && $proxy->send_through_proxy( $url ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1006
				$handle = stream_socket_client( 'tcp://' . $proxy->host() . ':' . $proxy->port(), $connection_error, $connection_error_str, $connect_timeout, STREAM_CLIENT_CONNECT, $context );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1007
			else
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1008
				$handle = stream_socket_client( $connect_host . ':' . $arrURL['port'], $connection_error, $connection_error_str, $connect_timeout, STREAM_CLIENT_CONNECT, $context );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1009
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1010
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1011
		if ( false === $handle ) {
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1012
			// SSL connection failed due to expired/invalid cert, or, OpenSSL configuration is broken.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1013
			if ( $secure_transport && 0 === $connection_error && '' === $connection_error_str )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1014
				return new WP_Error( 'http_request_failed', __( 'The SSL certificate for the host could not be verified.' ) );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1015
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1016
			return new WP_Error('http_request_failed', $connection_error . ': ' . $connection_error_str );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1017
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1018
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1019
		// Verify that the SSL certificate is valid for this request.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1020
		if ( $secure_transport && $ssl_verify && ! $proxy->is_enabled() ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1021
			if ( ! self::verify_ssl_certificate( $handle, $arrURL['host'] ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1022
				return new WP_Error( 'http_request_failed', __( 'The SSL certificate for the host could not be verified.' ) );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1023
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1024
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1025
		stream_set_timeout( $handle, $timeout, $utimeout );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1026
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1027
		if ( $proxy->is_enabled() && $proxy->send_through_proxy( $url ) ) //Some proxies require full URL in this field.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1028
			$requestPath = $url;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1029
		else
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1030
			$requestPath = $arrURL['path'] . ( isset($arrURL['query']) ? '?' . $arrURL['query'] : '' );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1031
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1032
		$strHeaders = strtoupper($r['method']) . ' ' . $requestPath . ' HTTP/' . $r['httpversion'] . "\r\n";
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1033
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1034
		$include_port_in_host_header = (
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1035
			( $proxy->is_enabled() && $proxy->send_through_proxy( $url ) ) ||
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1036
			( 'http'  == $arrURL['scheme'] && 80  != $arrURL['port'] ) ||
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1037
			( 'https' == $arrURL['scheme'] && 443 != $arrURL['port'] )
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1038
		);
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1039
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1040
		if ( $include_port_in_host_header ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1041
			$strHeaders .= 'Host: ' . $arrURL['host'] . ':' . $arrURL['port'] . "\r\n";
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1042
		} else {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1043
			$strHeaders .= 'Host: ' . $arrURL['host'] . "\r\n";
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1044
		}
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1045
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1046
		if ( isset($r['user-agent']) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1047
			$strHeaders .= 'User-agent: ' . $r['user-agent'] . "\r\n";
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1048
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1049
		if ( is_array($r['headers']) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1050
			foreach ( (array) $r['headers'] as $header => $headerValue )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1051
				$strHeaders .= $header . ': ' . $headerValue . "\r\n";
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1052
		} else {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1053
			$strHeaders .= $r['headers'];
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1054
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1055
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1056
		if ( $proxy->use_authentication() )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1057
			$strHeaders .= $proxy->authentication_header() . "\r\n";
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1058
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1059
		$strHeaders .= "\r\n";
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1060
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1061
		if ( ! is_null($r['body']) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1062
			$strHeaders .= $r['body'];
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1063
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1064
		fwrite($handle, $strHeaders);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1065
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1066
		if ( ! $r['blocking'] ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1067
			stream_set_blocking( $handle, 0 );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1068
			fclose( $handle );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1069
			return array( 'headers' => array(), 'body' => '', 'response' => array('code' => false, 'message' => false), 'cookies' => array() );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1070
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1071
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1072
		$strResponse = '';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1073
		$bodyStarted = false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1074
		$keep_reading = true;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1075
		$block_size = 4096;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1076
		if ( isset( $r['limit_response_size'] ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1077
			$block_size = min( $block_size, $r['limit_response_size'] );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1078
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1079
		// If streaming to a file setup the file handle.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1080
		if ( $r['stream'] ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1081
			if ( ! WP_DEBUG )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1082
				$stream_handle = @fopen( $r['filename'], 'w+' );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1083
			else
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1084
				$stream_handle = fopen( $r['filename'], 'w+' );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1085
			if ( ! $stream_handle )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1086
				return new WP_Error( 'http_request_failed', sprintf( __( 'Could not open handle for fopen() to %s' ), $r['filename'] ) );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1087
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1088
			$bytes_written = 0;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1089
			while ( ! feof($handle) && $keep_reading ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1090
				$block = fread( $handle, $block_size );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1091
				if ( ! $bodyStarted ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1092
					$strResponse .= $block;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1093
					if ( strpos( $strResponse, "\r\n\r\n" ) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1094
						$process = WP_Http::processResponse( $strResponse );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1095
						$bodyStarted = true;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1096
						$block = $process['body'];
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1097
						unset( $strResponse );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1098
						$process['body'] = '';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1099
					}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1100
				}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1101
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1102
				$this_block_size = strlen( $block );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1103
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1104
				if ( isset( $r['limit_response_size'] ) && ( $bytes_written + $this_block_size ) > $r['limit_response_size'] ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1105
					$this_block_size = ( $r['limit_response_size'] - $bytes_written );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1106
					$block = substr( $block, 0, $this_block_size );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1107
				}
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1108
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1109
				$bytes_written_to_file = fwrite( $stream_handle, $block );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1110
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1111
				if ( $bytes_written_to_file != $this_block_size ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1112
					fclose( $handle );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1113
					fclose( $stream_handle );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1114
					return new WP_Error( 'http_request_failed', __( 'Failed to write request to temporary file.' ) );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1115
				}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1116
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1117
				$bytes_written += $bytes_written_to_file;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1118
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1119
				$keep_reading = !isset( $r['limit_response_size'] ) || $bytes_written < $r['limit_response_size'];
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1120
			}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1121
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1122
			fclose( $stream_handle );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1123
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1124
		} else {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1125
			$header_length = 0;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1126
			while ( ! feof( $handle ) && $keep_reading ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1127
				$block = fread( $handle, $block_size );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1128
				$strResponse .= $block;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1129
				if ( ! $bodyStarted && strpos( $strResponse, "\r\n\r\n" ) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1130
					$header_length = strpos( $strResponse, "\r\n\r\n" ) + 4;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1131
					$bodyStarted = true;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1132
				}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1133
				$keep_reading = ( ! $bodyStarted || !isset( $r['limit_response_size'] ) || strlen( $strResponse ) < ( $header_length + $r['limit_response_size'] ) );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1134
			}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1135
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1136
			$process = WP_Http::processResponse( $strResponse );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1137
			unset( $strResponse );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1138
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1139
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1140
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1141
		fclose( $handle );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1142
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1143
		$arrHeaders = WP_Http::processHeaders( $process['headers'], $url );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1144
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1145
		$response = array(
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1146
			'headers' => $arrHeaders['headers'],
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1147
			// Not yet processed.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1148
			'body' => null,
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1149
			'response' => $arrHeaders['response'],
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1150
			'cookies' => $arrHeaders['cookies'],
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1151
			'filename' => $r['filename']
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1152
		);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1153
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1154
		// Handle redirects.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1155
		if ( false !== ( $redirect_response = WP_HTTP::handle_redirects( $url, $r, $response ) ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1156
			return $redirect_response;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1157
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1158
		// If the body was chunk encoded, then decode it.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1159
		if ( ! empty( $process['body'] ) && isset( $arrHeaders['headers']['transfer-encoding'] ) && 'chunked' == $arrHeaders['headers']['transfer-encoding'] )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1160
			$process['body'] = WP_Http::chunkTransferDecode($process['body']);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1161
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1162
		if ( true === $r['decompress'] && true === WP_Http_Encoding::should_decode($arrHeaders['headers']) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1163
			$process['body'] = WP_Http_Encoding::decompress( $process['body'] );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1164
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1165
		if ( isset( $r['limit_response_size'] ) && strlen( $process['body'] ) > $r['limit_response_size'] )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1166
			$process['body'] = substr( $process['body'], 0, $r['limit_response_size'] );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1167
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1168
		$response['body'] = $process['body'];
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1169
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1170
		return $response;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1171
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1172
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1173
	/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1174
	 * Verifies the received SSL certificate against it's Common Names and subjectAltName fields
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1175
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1176
	 * PHP's SSL verifications only verify that it's a valid Certificate, it doesn't verify if
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1177
	 * the certificate is valid for the hostname which was requested.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1178
	 * This function verifies the requested hostname against certificate's subjectAltName field,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1179
	 * if that is empty, or contains no DNS entries, a fallback to the Common Name field is used.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1180
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1181
	 * IP Address support is included if the request is being made to an IP address.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1182
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1183
	 * @since 3.7.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1184
	 * @static
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1185
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1186
	 * @param stream $stream The PHP Stream which the SSL request is being made over
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1187
	 * @param string $host The hostname being requested
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1188
	 * @return bool If the cerficiate presented in $stream is valid for $host
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1189
	 */
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1190
	public static function verify_ssl_certificate( $stream, $host ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1191
		$context_options = stream_context_get_options( $stream );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1192
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1193
		if ( empty( $context_options['ssl']['peer_certificate'] ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1194
			return false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1195
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1196
		$cert = openssl_x509_parse( $context_options['ssl']['peer_certificate'] );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1197
		if ( ! $cert )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1198
			return false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1199
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1200
		/*
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1201
		 * If the request is being made to an IP address, we'll validate against IP fields
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1202
		 * in the cert (if they exist)
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1203
		 */
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1204
		$host_type = ( WP_HTTP::is_ip_address( $host ) ? 'ip' : 'dns' );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1205
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1206
		$certificate_hostnames = array();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1207
		if ( ! empty( $cert['extensions']['subjectAltName'] ) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1208
			$match_against = preg_split( '/,\s*/', $cert['extensions']['subjectAltName'] );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1209
			foreach ( $match_against as $match ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1210
				list( $match_type, $match_host ) = explode( ':', $match );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1211
				if ( $host_type == strtolower( trim( $match_type ) ) ) // IP: or DNS:
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1212
					$certificate_hostnames[] = strtolower( trim( $match_host ) );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1213
			}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1214
		} elseif ( !empty( $cert['subject']['CN'] ) ) {
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1215
			// Only use the CN when the certificate includes no subjectAltName extension.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1216
			$certificate_hostnames[] = strtolower( $cert['subject']['CN'] );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1217
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1218
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1219
		// Exact hostname/IP matches.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1220
		if ( in_array( strtolower( $host ), $certificate_hostnames ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1221
			return true;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1222
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1223
		// IP's can't be wildcards, Stop processing.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1224
		if ( 'ip' == $host_type )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1225
			return false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1226
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1227
		// Test to see if the domain is at least 2 deep for wildcard support.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1228
		if ( substr_count( $host, '.' ) < 2 )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1229
			return false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1230
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1231
		// Wildcard subdomains certs (*.example.com) are valid for a.example.com but not a.b.example.com.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1232
		$wildcard_host = preg_replace( '/^[^.]+\./', '*.', $host );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1233
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1234
		return in_array( strtolower( $wildcard_host ), $certificate_hostnames );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1235
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1236
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1237
	/**
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1238
	 * Whether this class can be used for retrieving a URL.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1239
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1240
	 * @static
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1241
	 * @access public
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1242
	 * @since 2.7.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1243
	 * @since 3.7.0 Combined with the fsockopen transport and switched to stream_socket_client().
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1244
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1245
	 * @return boolean False means this class can not be used, true means it can.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1246
	 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1247
	public static function test( $args = array() ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1248
		if ( ! function_exists( 'stream_socket_client' ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1249
			return false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1250
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1251
		$is_ssl = isset( $args['ssl'] ) && $args['ssl'];
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1252
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1253
		if ( $is_ssl ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1254
			if ( ! extension_loaded( 'openssl' ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1255
				return false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1256
			if ( ! function_exists( 'openssl_x509_parse' ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1257
				return false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1258
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1259
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1260
		/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1261
		 * Filter whether streams can be used as a transport for retrieving a URL.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1262
		 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1263
		 * @since 2.7.0
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1264
		 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1265
		 * @param bool  $use_class Whether the class can be used. Default true.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1266
		 * @param array $args      Request arguments.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1267
		 */
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1268
		return apply_filters( 'use_streams_transport', true, $args );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1269
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1270
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1271
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1272
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1273
 * Deprecated HTTP Transport method which used fsockopen.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1274
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1275
 * This class is not used, and is included for backwards compatibility only.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1276
 * All code should make use of WP_HTTP directly through it's API.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1277
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1278
 * @see WP_HTTP::request
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1279
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1280
 * @since 2.7.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1281
 * @deprecated 3.7.0 Please use WP_HTTP::request() directly
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1282
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1283
class WP_HTTP_Fsockopen extends WP_HTTP_Streams {
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1284
	// For backwards compatibility for users who are using the class directly.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1285
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1286
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1287
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1288
 * HTTP request method uses Curl extension to retrieve the url.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1289
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1290
 * Requires the Curl extension to be installed.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1291
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1292
 * @package WordPress
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1293
 * @subpackage HTTP
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1294
 * @since 2.7.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1295
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1296
class WP_Http_Curl {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1297
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1298
	/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1299
	 * Temporary header storage for during requests.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1300
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1301
	 * @since 3.2.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1302
	 * @access private
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1303
	 * @var string
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1304
	 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1305
	private $headers = '';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1306
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1307
	/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1308
	 * Temporary body storage for during requests.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1309
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1310
	 * @since 3.6.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1311
	 * @access private
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1312
	 * @var string
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1313
	 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1314
	private $body = '';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1315
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1316
	/**
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1317
	 * The maximum amount of data to receive from the remote server.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1318
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1319
	 * @since 3.6.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1320
	 * @access private
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1321
	 * @var int
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1322
	 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1323
	private $max_body_length = false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1324
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1325
	/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1326
	 * The file resource used for streaming to file.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1327
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1328
	 * @since 3.6.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1329
	 * @access private
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1330
	 * @var resource
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1331
	 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1332
	private $stream_handle = false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1333
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1334
	/**
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1335
	 * The total bytes written in the current request.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1336
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1337
	 * @since 4.1.0
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1338
	 * @access private
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1339
	 * @var int
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1340
	 */
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1341
	private $bytes_written_total = 0;
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1342
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1343
	/**
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1344
	 * Send a HTTP request to a URI using cURL extension.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1345
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1346
	 * @access public
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1347
	 * @since 2.7.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1348
	 *
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1349
	 * @param string $url The request URL.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1350
	 * @param string|array $args Optional. Override the defaults.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1351
	 * @return array|WP_Error Array containing 'headers', 'body', 'response', 'cookies', 'filename'. A WP_Error instance upon error
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1352
	 */
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1353
	public function request($url, $args = array()) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1354
		$defaults = array(
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1355
			'method' => 'GET', 'timeout' => 5,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1356
			'redirection' => 5, 'httpversion' => '1.0',
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1357
			'blocking' => true,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1358
			'headers' => array(), 'body' => null, 'cookies' => array()
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1359
		);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1360
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1361
		$r = wp_parse_args( $args, $defaults );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1362
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1363
		if ( isset( $r['headers']['User-Agent'] ) ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1364
			$r['user-agent'] = $r['headers']['User-Agent'];
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1365
			unset( $r['headers']['User-Agent'] );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1366
		} elseif ( isset( $r['headers']['user-agent'] ) ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1367
			$r['user-agent'] = $r['headers']['user-agent'];
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1368
			unset( $r['headers']['user-agent'] );
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1369
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1370
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1371
		// Construct Cookie: header if any cookies are set.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1372
		WP_Http::buildCookieHeader( $r );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1373
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1374
		$handle = curl_init();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1375
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1376
		// cURL offers really easy proxy support.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1377
		$proxy = new WP_HTTP_Proxy();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1378
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1379
		if ( $proxy->is_enabled() && $proxy->send_through_proxy( $url ) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1380
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1381
			curl_setopt( $handle, CURLOPT_PROXYTYPE, CURLPROXY_HTTP );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1382
			curl_setopt( $handle, CURLOPT_PROXY, $proxy->host() );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1383
			curl_setopt( $handle, CURLOPT_PROXYPORT, $proxy->port() );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1384
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1385
			if ( $proxy->use_authentication() ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1386
				curl_setopt( $handle, CURLOPT_PROXYAUTH, CURLAUTH_ANY );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1387
				curl_setopt( $handle, CURLOPT_PROXYUSERPWD, $proxy->authentication() );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1388
			}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1389
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1390
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1391
		$is_local = isset($r['local']) && $r['local'];
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1392
		$ssl_verify = isset($r['sslverify']) && $r['sslverify'];
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1393
		if ( $is_local ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1394
			/** This filter is documented in wp-includes/class-http.php */
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1395
			$ssl_verify = apply_filters( 'https_local_ssl_verify', $ssl_verify );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1396
		} elseif ( ! $is_local ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1397
			/** This filter is documented in wp-includes/class-http.php */
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1398
			$ssl_verify = apply_filters( 'https_ssl_verify', $ssl_verify );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1399
		}
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1400
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1401
		/*
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1402
		 * CURLOPT_TIMEOUT and CURLOPT_CONNECTTIMEOUT expect integers. Have to use ceil since.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1403
		 * a value of 0 will allow an unlimited timeout.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1404
		 */
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1405
		$timeout = (int) ceil( $r['timeout'] );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1406
		curl_setopt( $handle, CURLOPT_CONNECTTIMEOUT, $timeout );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1407
		curl_setopt( $handle, CURLOPT_TIMEOUT, $timeout );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1408
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1409
		curl_setopt( $handle, CURLOPT_URL, $url);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1410
		curl_setopt( $handle, CURLOPT_RETURNTRANSFER, true );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1411
		curl_setopt( $handle, CURLOPT_SSL_VERIFYHOST, ( $ssl_verify === true ) ? 2 : false );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1412
		curl_setopt( $handle, CURLOPT_SSL_VERIFYPEER, $ssl_verify );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1413
		curl_setopt( $handle, CURLOPT_CAINFO, $r['sslcertificates'] );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1414
		curl_setopt( $handle, CURLOPT_USERAGENT, $r['user-agent'] );
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1415
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1416
		/*
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1417
		 * The option doesn't work with safe mode or when open_basedir is set, and there's
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1418
		 * a bug #17490 with redirected POST requests, so handle redirections outside Curl.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1419
		 */
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1420
		curl_setopt( $handle, CURLOPT_FOLLOWLOCATION, false );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1421
		if ( defined( 'CURLOPT_PROTOCOLS' ) ) // PHP 5.2.10 / cURL 7.19.4
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1422
			curl_setopt( $handle, CURLOPT_PROTOCOLS, CURLPROTO_HTTP | CURLPROTO_HTTPS );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1423
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1424
		switch ( $r['method'] ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1425
			case 'HEAD':
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1426
				curl_setopt( $handle, CURLOPT_NOBODY, true );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1427
				break;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1428
			case 'POST':
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1429
				curl_setopt( $handle, CURLOPT_POST, true );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1430
				curl_setopt( $handle, CURLOPT_POSTFIELDS, $r['body'] );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1431
				break;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1432
			case 'PUT':
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1433
				curl_setopt( $handle, CURLOPT_CUSTOMREQUEST, 'PUT' );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1434
				curl_setopt( $handle, CURLOPT_POSTFIELDS, $r['body'] );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1435
				break;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1436
			default:
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1437
				curl_setopt( $handle, CURLOPT_CUSTOMREQUEST, $r['method'] );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1438
				if ( ! is_null( $r['body'] ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1439
					curl_setopt( $handle, CURLOPT_POSTFIELDS, $r['body'] );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1440
				break;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1441
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1442
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1443
		if ( true === $r['blocking'] ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1444
			curl_setopt( $handle, CURLOPT_HEADERFUNCTION, array( $this, 'stream_headers' ) );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1445
			curl_setopt( $handle, CURLOPT_WRITEFUNCTION, array( $this, 'stream_body' ) );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1446
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1447
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1448
		curl_setopt( $handle, CURLOPT_HEADER, false );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1449
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1450
		if ( isset( $r['limit_response_size'] ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1451
			$this->max_body_length = intval( $r['limit_response_size'] );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1452
		else
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1453
			$this->max_body_length = false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1454
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1455
		// If streaming to a file open a file handle, and setup our curl streaming handler.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1456
		if ( $r['stream'] ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1457
			if ( ! WP_DEBUG )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1458
				$this->stream_handle = @fopen( $r['filename'], 'w+' );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1459
			else
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1460
				$this->stream_handle = fopen( $r['filename'], 'w+' );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1461
			if ( ! $this->stream_handle )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1462
				return new WP_Error( 'http_request_failed', sprintf( __( 'Could not open handle for fopen() to %s' ), $r['filename'] ) );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1463
		} else {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1464
			$this->stream_handle = false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1465
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1466
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1467
		if ( !empty( $r['headers'] ) ) {
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1468
			// cURL expects full header strings in each element.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1469
			$headers = array();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1470
			foreach ( $r['headers'] as $name => $value ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1471
				$headers[] = "{$name}: $value";
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1472
			}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1473
			curl_setopt( $handle, CURLOPT_HTTPHEADER, $headers );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1474
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1475
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1476
		if ( $r['httpversion'] == '1.0' )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1477
			curl_setopt( $handle, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0 );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1478
		else
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1479
			curl_setopt( $handle, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1 );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1480
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1481
		/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1482
		 * Fires before the cURL request is executed.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1483
		 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1484
		 * Cookies are not currently handled by the HTTP API. This action allows
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1485
		 * plugins to handle cookies themselves.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1486
		 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1487
		 * @since 2.8.0
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1488
		 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1489
		 * @param resource &$handle The cURL handle returned by curl_init().
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1490
		 * @param array    $r       The HTTP request arguments.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1491
		 * @param string   $url     The request URL.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1492
		 */
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1493
		do_action_ref_array( 'http_api_curl', array( &$handle, $r, $url ) );
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1494
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1495
		// We don't need to return the body, so don't. Just execute request and return.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1496
		if ( ! $r['blocking'] ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1497
			curl_exec( $handle );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1498
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1499
			if ( $curl_error = curl_error( $handle ) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1500
				curl_close( $handle );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1501
				return new WP_Error( 'http_request_failed', $curl_error );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1502
			}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1503
			if ( in_array( curl_getinfo( $handle, CURLINFO_HTTP_CODE ), array( 301, 302 ) ) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1504
				curl_close( $handle );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1505
				return new WP_Error( 'http_request_failed', __( 'Too many redirects.' ) );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1506
			}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1507
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1508
			curl_close( $handle );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1509
			return array( 'headers' => array(), 'body' => '', 'response' => array('code' => false, 'message' => false), 'cookies' => array() );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1510
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1511
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1512
		curl_exec( $handle );
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1513
		$theHeaders = WP_Http::processHeaders( $this->headers, $url );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1514
		$theBody = $this->body;
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1515
		$bytes_written_total = $this->bytes_written_total;
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1516
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1517
		$this->headers = '';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1518
		$this->body = '';
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1519
		$this->bytes_written_total = 0;
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1520
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1521
		$curl_error = curl_errno( $handle );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1522
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1523
		// If an error occurred, or, no response.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1524
		if ( $curl_error || ( 0 == strlen( $theBody ) && empty( $theHeaders['headers'] ) ) ) {
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1525
			if ( CURLE_WRITE_ERROR /* 23 */ == $curl_error ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1526
				if ( ! $this->max_body_length || $this->max_body_length != $bytes_written_total ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1527
					if ( $r['stream'] ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1528
						curl_close( $handle );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1529
						fclose( $this->stream_handle );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1530
						return new WP_Error( 'http_request_failed', __( 'Failed to write request to temporary file.' ) );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1531
					} else {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1532
						curl_close( $handle );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1533
						return new WP_Error( 'http_request_failed', curl_error( $handle ) );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1534
					}
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1535
				}
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1536
			} else {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1537
				if ( $curl_error = curl_error( $handle ) ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1538
					curl_close( $handle );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1539
					return new WP_Error( 'http_request_failed', $curl_error );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1540
				}
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1541
			}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1542
			if ( in_array( curl_getinfo( $handle, CURLINFO_HTTP_CODE ), array( 301, 302 ) ) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1543
				curl_close( $handle );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1544
				return new WP_Error( 'http_request_failed', __( 'Too many redirects.' ) );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1545
			}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1546
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1547
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1548
		curl_close( $handle );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1549
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1550
		if ( $r['stream'] )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1551
			fclose( $this->stream_handle );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1552
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1553
		$response = array(
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1554
			'headers' => $theHeaders['headers'],
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1555
			'body' => null,
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1556
			'response' => $theHeaders['response'],
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1557
			'cookies' => $theHeaders['cookies'],
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1558
			'filename' => $r['filename']
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1559
		);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1560
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1561
		// Handle redirects.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1562
		if ( false !== ( $redirect_response = WP_HTTP::handle_redirects( $url, $r, $response ) ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1563
			return $redirect_response;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1564
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1565
		if ( true === $r['decompress'] && true === WP_Http_Encoding::should_decode($theHeaders['headers']) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1566
			$theBody = WP_Http_Encoding::decompress( $theBody );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1567
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1568
		$response['body'] = $theBody;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1569
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1570
		return $response;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1571
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1572
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1573
	/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1574
	 * Grab the headers of the cURL request
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1575
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1576
	 * Each header is sent individually to this callback, so we append to the $header property for temporary storage
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1577
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1578
	 * @since 3.2.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1579
	 * @access private
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1580
	 * @return int
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1581
	 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1582
	private function stream_headers( $handle, $headers ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1583
		$this->headers .= $headers;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1584
		return strlen( $headers );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1585
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1586
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1587
	/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1588
	 * Grab the body of the cURL request
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1589
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1590
	 * The contents of the document are passed in chunks, so we append to the $body property for temporary storage.
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1591
	 * Returning a length shorter than the length of $data passed in will cause cURL to abort the request with CURLE_WRITE_ERROR
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1592
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1593
	 * @since 3.6.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1594
	 * @access private
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1595
	 * @return int
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1596
	 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1597
	private function stream_body( $handle, $data ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1598
		$data_length = strlen( $data );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1599
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1600
		if ( $this->max_body_length && ( $this->bytes_written_total + $data_length ) > $this->max_body_length ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1601
			$data_length = ( $this->max_body_length - $this->bytes_written_total );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1602
			$data = substr( $data, 0, $data_length );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1603
		}
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1604
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1605
		if ( $this->stream_handle ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1606
			$bytes_written = fwrite( $this->stream_handle, $data );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1607
		} else {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1608
			$this->body .= $data;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1609
			$bytes_written = $data_length;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1610
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1611
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1612
		$this->bytes_written_total += $bytes_written;
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1613
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1614
		// Upon event of this function returning less than strlen( $data ) curl will error with CURLE_WRITE_ERROR.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1615
		return $bytes_written;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1616
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1617
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1618
	/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1619
	 * Whether this class can be used for retrieving an URL.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1620
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1621
	 * @static
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1622
	 * @since 2.7.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1623
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1624
	 * @return boolean False means this class can not be used, true means it can.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1625
	 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1626
	public static function test( $args = array() ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1627
		if ( ! function_exists( 'curl_init' ) || ! function_exists( 'curl_exec' ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1628
			return false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1629
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1630
		$is_ssl = isset( $args['ssl'] ) && $args['ssl'];
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1631
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1632
		if ( $is_ssl ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1633
			$curl_version = curl_version();
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1634
			// Check whether this cURL version support SSL requests.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1635
			if ( ! (CURL_VERSION_SSL & $curl_version['features']) )
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1636
				return false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1637
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1638
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1639
		/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1640
		 * Filter whether cURL can be used as a transport for retrieving a URL.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1641
		 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1642
		 * @since 2.7.0
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1643
		 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1644
		 * @param bool  $use_class Whether the class can be used. Default true.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1645
		 * @param array $args      An array of request arguments.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1646
		 */
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1647
		return apply_filters( 'use_curl_transport', true, $args );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1648
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1649
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1650
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1651
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1652
 * Adds Proxy support to the WordPress HTTP API.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1653
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1654
 * There are caveats to proxy support. It requires that defines be made in the wp-config.php file to
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1655
 * enable proxy support. There are also a few filters that plugins can hook into for some of the
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1656
 * constants.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1657
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1658
 * Please note that only BASIC authentication is supported by most transports.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1659
 * cURL MAY support more methods (such as NTLM authentication) depending on your environment.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1660
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1661
 * The constants are as follows:
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1662
 * <ol>
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1663
 * <li>WP_PROXY_HOST - Enable proxy support and host for connecting.</li>
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1664
 * <li>WP_PROXY_PORT - Proxy port for connection. No default, must be defined.</li>
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1665
 * <li>WP_PROXY_USERNAME - Proxy username, if it requires authentication.</li>
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1666
 * <li>WP_PROXY_PASSWORD - Proxy password, if it requires authentication.</li>
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1667
 * <li>WP_PROXY_BYPASS_HOSTS - Will prevent the hosts in this list from going through the proxy.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1668
 * You do not need to have localhost and the blog host in this list, because they will not be passed
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1669
 * through the proxy. The list should be presented in a comma separated list, wildcards using * are supported, eg. *.wordpress.org</li>
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1670
 * </ol>
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1671
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1672
 * An example can be as seen below.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1673
 *
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1674
 *     define('WP_PROXY_HOST', '192.168.84.101');
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1675
 *     define('WP_PROXY_PORT', '8080');
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1676
 *     define('WP_PROXY_BYPASS_HOSTS', 'localhost, www.example.com, *.wordpress.org');
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1677
 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1678
 * @link https://core.trac.wordpress.org/ticket/4011 Proxy support ticket in WordPress.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1679
 * @link https://core.trac.wordpress.org/ticket/14636 Allow wildcard domains in WP_PROXY_BYPASS_HOSTS
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1680
 * @since 2.8.0
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1681
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1682
class WP_HTTP_Proxy {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1683
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1684
	/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1685
	 * Whether proxy connection should be used.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1686
	 *
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1687
	 * @since 2.8.0
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1688
	 *
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1689
	 * @use WP_PROXY_HOST
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1690
	 * @use WP_PROXY_PORT
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1691
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1692
	 * @return bool
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1693
	 */
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1694
	public function is_enabled() {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1695
		return defined('WP_PROXY_HOST') && defined('WP_PROXY_PORT');
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1696
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1697
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1698
	/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1699
	 * Whether authentication should be used.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1700
	 *
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1701
	 * @since 2.8.0
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1702
	 *
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1703
	 * @use WP_PROXY_USERNAME
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1704
	 * @use WP_PROXY_PASSWORD
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1705
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1706
	 * @return bool
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1707
	 */
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1708
	public function use_authentication() {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1709
		return defined('WP_PROXY_USERNAME') && defined('WP_PROXY_PASSWORD');
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1710
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1711
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1712
	/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1713
	 * Retrieve the host for the proxy server.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1714
	 *
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1715
	 * @since 2.8.0
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1716
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1717
	 * @return string
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1718
	 */
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1719
	public function host() {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1720
		if ( defined('WP_PROXY_HOST') )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1721
			return WP_PROXY_HOST;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1722
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1723
		return '';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1724
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1725
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1726
	/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1727
	 * Retrieve the port for the proxy server.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1728
	 *
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1729
	 * @since 2.8.0
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1730
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1731
	 * @return string
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1732
	 */
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1733
	public function port() {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1734
		if ( defined('WP_PROXY_PORT') )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1735
			return WP_PROXY_PORT;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1736
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1737
		return '';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1738
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1739
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1740
	/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1741
	 * Retrieve the username for proxy authentication.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1742
	 *
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1743
	 * @since 2.8.0
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1744
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1745
	 * @return string
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1746
	 */
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1747
	public function username() {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1748
		if ( defined('WP_PROXY_USERNAME') )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1749
			return WP_PROXY_USERNAME;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1750
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1751
		return '';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1752
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1753
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1754
	/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1755
	 * Retrieve the password for proxy authentication.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1756
	 *
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1757
	 * @since 2.8.0
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1758
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1759
	 * @return string
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1760
	 */
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1761
	public function password() {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1762
		if ( defined('WP_PROXY_PASSWORD') )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1763
			return WP_PROXY_PASSWORD;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1764
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1765
		return '';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1766
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1767
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1768
	/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1769
	 * Retrieve authentication string for proxy authentication.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1770
	 *
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1771
	 * @since 2.8.0
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1772
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1773
	 * @return string
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1774
	 */
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1775
	public function authentication() {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1776
		return $this->username() . ':' . $this->password();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1777
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1778
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1779
	/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1780
	 * Retrieve header string for proxy authentication.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1781
	 *
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1782
	 * @since 2.8.0
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1783
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1784
	 * @return string
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1785
	 */
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1786
	public function authentication_header() {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1787
		return 'Proxy-Authorization: Basic ' . base64_encode( $this->authentication() );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1788
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1789
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1790
	/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1791
	 * Whether URL should be sent through the proxy server.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1792
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1793
	 * We want to keep localhost and the blog URL from being sent through the proxy server, because
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1794
	 * some proxies can not handle this. We also have the constant available for defining other
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1795
	 * hosts that won't be sent through the proxy.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1796
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1797
	 * @since 2.8.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1798
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1799
	 * @param string $uri URI to check.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1800
	 * @return bool True, to send through the proxy and false if, the proxy should not be used.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1801
	 */
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1802
	public function send_through_proxy( $uri ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1803
		/*
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1804
		 * parse_url() only handles http, https type URLs, and will emit E_WARNING on failure.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1805
		 * This will be displayed on blogs, which is not reasonable.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1806
		 */
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1807
		$check = @parse_url($uri);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1808
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1809
		// Malformed URL, can not process, but this could mean ssl, so let through anyway.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1810
		if ( $check === false )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1811
			return true;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1812
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1813
		$home = parse_url( get_option('siteurl') );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1814
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1815
		/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1816
		 * Filter whether to preempt sending the request through the proxy server.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1817
		 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1818
		 * Returning false will bypass the proxy; returning true will send
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1819
		 * the request through the proxy. Returning null bypasses the filter.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1820
		 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1821
		 * @since 3.5.0
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1822
		 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1823
		 * @param null   $override Whether to override the request result. Default null.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1824
		 * @param string $uri      URL to check.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1825
		 * @param array  $check    Associative array result of parsing the URI.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1826
		 * @param array  $home     Associative array result of parsing the site URL.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1827
		 */
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1828
		$result = apply_filters( 'pre_http_send_through_proxy', null, $uri, $check, $home );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1829
		if ( ! is_null( $result ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1830
			return $result;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1831
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1832
		if ( 'localhost' == $check['host'] || ( isset( $home['host'] ) && $home['host'] == $check['host'] ) )
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1833
			return false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1834
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1835
		if ( !defined('WP_PROXY_BYPASS_HOSTS') )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1836
			return true;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1837
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1838
		static $bypass_hosts;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1839
		static $wildcard_regex = false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1840
		if ( null == $bypass_hosts ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1841
			$bypass_hosts = preg_split('|,\s*|', WP_PROXY_BYPASS_HOSTS);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1842
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1843
			if ( false !== strpos(WP_PROXY_BYPASS_HOSTS, '*') ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1844
				$wildcard_regex = array();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1845
				foreach ( $bypass_hosts as $host )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1846
					$wildcard_regex[] = str_replace( '\*', '.+', preg_quote( $host, '/' ) );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1847
				$wildcard_regex = '/^(' . implode('|', $wildcard_regex) . ')$/i';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1848
			}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1849
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1850
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1851
		if ( !empty($wildcard_regex) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1852
			return !preg_match($wildcard_regex, $check['host']);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1853
		else
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1854
			return !in_array( $check['host'], $bypass_hosts );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1855
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1856
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1857
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1858
 * Internal representation of a single cookie.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1859
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1860
 * Returned cookies are represented using this class, and when cookies are set, if they are not
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1861
 * already a WP_Http_Cookie() object, then they are turned into one.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1862
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1863
 * @todo The WordPress convention is to use underscores instead of camelCase for function and method
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1864
 * names. Need to switch to use underscores instead for the methods.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1865
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1866
 * @package WordPress
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1867
 * @subpackage HTTP
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1868
 * @since 2.8.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1869
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1870
class WP_Http_Cookie {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1871
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1872
	/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1873
	 * Cookie name.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1874
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1875
	 * @since 2.8.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1876
	 * @var string
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1877
	 */
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1878
	public $name;
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1879
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1880
	/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1881
	 * Cookie value.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1882
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1883
	 * @since 2.8.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1884
	 * @var string
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1885
	 */
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1886
	public $value;
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1887
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1888
	/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1889
	 * When the cookie expires.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1890
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1891
	 * @since 2.8.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1892
	 * @var string
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1893
	 */
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1894
	public $expires;
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1895
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1896
	/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1897
	 * Cookie URL path.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1898
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1899
	 * @since 2.8.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1900
	 * @var string
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1901
	 */
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1902
	public $path;
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1903
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1904
	/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1905
	 * Cookie Domain.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1906
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1907
	 * @since 2.8.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1908
	 * @var string
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1909
	 */
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1910
	public $domain;
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1911
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1912
	/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1913
	 * Sets up this cookie object.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1914
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1915
	 * The parameter $data should be either an associative array containing the indices names below
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1916
	 * or a header string detailing it.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1917
	 *
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1918
	 * @since 2.8.0
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1919
	 * @access public
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1920
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1921
	 * @param string|array $data {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1922
	 *     Raw cookie data as header string or data array.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1923
	 *
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1924
	 *     @type string     $name    Cookie name.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1925
	 *     @type mixed      $value   Value. Should NOT already be urlencoded.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1926
	 *     @type string|int $expires Optional. Unix timestamp or formatted date. Default null.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1927
	 *     @type string     $path    Optional. Path. Default '/'.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1928
	 *     @type string     $domain  Optional. Domain. Default host of parsed $requested_url.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1929
	 *     @type int        $port    Optional. Port. Default null.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1930
	 * }
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1931
	 * @param string       $requested_url The URL which the cookie was set on, used for default $domain
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1932
	 *                                    and $port values.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1933
	 */
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1934
	public function __construct( $data, $requested_url = '' ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1935
		if ( $requested_url )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1936
			$arrURL = @parse_url( $requested_url );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1937
		if ( isset( $arrURL['host'] ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1938
			$this->domain = $arrURL['host'];
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1939
		$this->path = isset( $arrURL['path'] ) ? $arrURL['path'] : '/';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1940
		if (  '/' != substr( $this->path, -1 ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1941
			$this->path = dirname( $this->path ) . '/';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1942
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1943
		if ( is_string( $data ) ) {
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1944
			// Assume it's a header string direct from a previous request.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1945
			$pairs = explode( ';', $data );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1946
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1947
			// Special handling for first pair; name=value. Also be careful of "=" in value.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1948
			$name  = trim( substr( $pairs[0], 0, strpos( $pairs[0], '=' ) ) );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1949
			$value = substr( $pairs[0], strpos( $pairs[0], '=' ) + 1 );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1950
			$this->name  = $name;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1951
			$this->value = urldecode( $value );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1952
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1953
			// Removes name=value from items.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1954
			array_shift( $pairs );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1955
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1956
			// Set everything else as a property.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1957
			foreach ( $pairs as $pair ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1958
				$pair = rtrim($pair);
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1959
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1960
				// Handle the cookie ending in ; which results in a empty final pair.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1961
				if ( empty($pair) )
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1962
					continue;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1963
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1964
				list( $key, $val ) = strpos( $pair, '=' ) ? explode( '=', $pair ) : array( $pair, '' );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1965
				$key = strtolower( trim( $key ) );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1966
				if ( 'expires' == $key )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1967
					$val = strtotime( $val );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1968
				$this->$key = $val;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1969
			}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1970
		} else {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1971
			if ( !isset( $data['name'] ) )
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1972
				return;
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1973
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1974
			// Set properties based directly on parameters.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1975
			foreach ( array( 'name', 'value', 'path', 'domain', 'port' ) as $field ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1976
				if ( isset( $data[ $field ] ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1977
					$this->$field = $data[ $field ];
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1978
			}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1979
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1980
			if ( isset( $data['expires'] ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1981
				$this->expires = is_int( $data['expires'] ) ? $data['expires'] : strtotime( $data['expires'] );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1982
			else
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1983
				$this->expires = null;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1984
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1985
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1986
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1987
	/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1988
	 * Confirms that it's OK to send this cookie to the URL checked against.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1989
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1990
	 * Decision is based on RFC 2109/2965, so look there for details on validity.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1991
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1992
	 * @access public
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1993
	 * @since 2.8.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1994
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1995
	 * @param string $url URL you intend to send this cookie to
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1996
	 * @return boolean true if allowed, false otherwise.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1997
	 */
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1998
	public function test( $url ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1999
		if ( is_null( $this->name ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2000
			return false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2001
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2002
		// Expires - if expired then nothing else matters.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2003
		if ( isset( $this->expires ) && time() > $this->expires )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2004
			return false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2005
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2006
		// Get details on the URL we're thinking about sending to.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2007
		$url = parse_url( $url );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2008
		$url['port'] = isset( $url['port'] ) ? $url['port'] : ( 'https' == $url['scheme'] ? 443 : 80 );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2009
		$url['path'] = isset( $url['path'] ) ? $url['path'] : '/';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2010
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2011
		// Values to use for comparison against the URL.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2012
		$path   = isset( $this->path )   ? $this->path   : '/';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2013
		$port   = isset( $this->port )   ? $this->port   : null;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2014
		$domain = isset( $this->domain ) ? strtolower( $this->domain ) : strtolower( $url['host'] );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2015
		if ( false === stripos( $domain, '.' ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2016
			$domain .= '.local';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2017
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2018
		// Host - very basic check that the request URL ends with the domain restriction (minus leading dot).
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2019
		$domain = substr( $domain, 0, 1 ) == '.' ? substr( $domain, 1 ) : $domain;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2020
		if ( substr( $url['host'], -strlen( $domain ) ) != $domain )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2021
			return false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2022
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2023
		// Port - supports "port-lists" in the format: "80,8000,8080".
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2024
		if ( !empty( $port ) && !in_array( $url['port'], explode( ',', $port) ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2025
			return false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2026
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2027
		// Path - request path must start with path restriction.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2028
		if ( substr( $url['path'], 0, strlen( $path ) ) != $path )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2029
			return false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2030
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2031
		return true;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2032
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2033
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2034
	/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2035
	 * Convert cookie name and value back to header string.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2036
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2037
	 * @access public
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2038
	 * @since 2.8.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2039
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2040
	 * @return string Header encoded cookie name and value.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2041
	 */
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2042
	public function getHeaderValue() {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2043
		if ( ! isset( $this->name ) || ! isset( $this->value ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2044
			return '';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2045
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2046
		/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2047
		 * Filter the header-encoded cookie value.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2048
		 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2049
		 * @since 3.4.0
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2050
		 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2051
		 * @param string $value The cookie value.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2052
		 * @param string $name  The cookie name.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2053
		 */
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2054
		return $this->name . '=' . apply_filters( 'wp_http_cookie_value', $this->value, $this->name );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2055
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2056
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2057
	/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2058
	 * Retrieve cookie header for usage in the rest of the WordPress HTTP API.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2059
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2060
	 * @access public
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2061
	 * @since 2.8.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2062
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2063
	 * @return string
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2064
	 */
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2065
	public function getFullHeader() {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2066
		return 'Cookie: ' . $this->getHeaderValue();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2067
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2068
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2069
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2070
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2071
 * Implementation for deflate and gzip transfer encodings.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2072
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2073
 * Includes RFC 1950, RFC 1951, and RFC 1952.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2074
 *
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2075
 * @since 2.8.0
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2076
 * @package WordPress
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2077
 * @subpackage HTTP
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2078
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2079
class WP_Http_Encoding {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2080
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2081
	/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2082
	 * Compress raw string using the deflate format.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2083
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2084
	 * Supports the RFC 1951 standard.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2085
	 *
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2086
	 * @since 2.8.0
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2087
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2088
	 * @param string $raw String to compress.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2089
	 * @param int $level Optional, default is 9. Compression level, 9 is highest.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2090
	 * @param string $supports Optional, not used. When implemented it will choose the right compression based on what the server supports.
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2091
	 * @return string|false False on failure.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2092
	 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2093
	public static function compress( $raw, $level = 9, $supports = null ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2094
		return gzdeflate( $raw, $level );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2095
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2096
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2097
	/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2098
	 * Decompression of deflated string.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2099
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2100
	 * Will attempt to decompress using the RFC 1950 standard, and if that fails
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2101
	 * then the RFC 1951 standard deflate will be attempted. Finally, the RFC
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2102
	 * 1952 standard gzip decode will be attempted. If all fail, then the
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2103
	 * original compressed string will be returned.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2104
	 *
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2105
	 * @since 2.8.0
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2106
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2107
	 * @param string $compressed String to decompress.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2108
	 * @param int $length The optional length of the compressed data.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2109
	 * @return string|bool False on failure.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2110
	 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2111
	public static function decompress( $compressed, $length = null ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2112
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2113
		if ( empty($compressed) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2114
			return $compressed;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2115
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2116
		if ( false !== ( $decompressed = @gzinflate( $compressed ) ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2117
			return $decompressed;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2118
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2119
		if ( false !== ( $decompressed = self::compatible_gzinflate( $compressed ) ) )
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2120
			return $decompressed;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2121
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2122
		if ( false !== ( $decompressed = @gzuncompress( $compressed ) ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2123
			return $decompressed;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2124
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2125
		if ( function_exists('gzdecode') ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2126
			$decompressed = @gzdecode( $compressed );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2127
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2128
			if ( false !== $decompressed )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2129
				return $decompressed;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2130
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2131
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2132
		return $compressed;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2133
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2134
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2135
	/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2136
	 * Decompression of deflated string while staying compatible with the majority of servers.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2137
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2138
	 * Certain Servers will return deflated data with headers which PHP's gzinflate()
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2139
	 * function cannot handle out of the box. The following function has been created from
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2140
	 * various snippets on the gzinflate() PHP documentation.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2141
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2142
	 * Warning: Magic numbers within. Due to the potential different formats that the compressed
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2143
	 * data may be returned in, some "magic offsets" are needed to ensure proper decompression
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2144
	 * takes place. For a simple progmatic way to determine the magic offset in use, see:
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2145
	 * https://core.trac.wordpress.org/ticket/18273
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2146
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2147
	 * @since 2.8.1
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2148
	 * @link https://core.trac.wordpress.org/ticket/18273
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2149
	 * @link http://au2.php.net/manual/en/function.gzinflate.php#70875
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2150
	 * @link http://au2.php.net/manual/en/function.gzinflate.php#77336
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2151
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2152
	 * @param string $gzData String to decompress.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2153
	 * @return string|bool False on failure.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2154
	 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2155
	public static function compatible_gzinflate($gzData) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2156
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2157
		// Compressed data might contain a full header, if so strip it for gzinflate().
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2158
		if ( substr($gzData, 0, 3) == "\x1f\x8b\x08" ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2159
			$i = 10;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2160
			$flg = ord( substr($gzData, 3, 1) );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2161
			if ( $flg > 0 ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2162
				if ( $flg & 4 ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2163
					list($xlen) = unpack('v', substr($gzData, $i, 2) );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2164
					$i = $i + 2 + $xlen;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2165
				}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2166
				if ( $flg & 8 )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2167
					$i = strpos($gzData, "\0", $i) + 1;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2168
				if ( $flg & 16 )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2169
					$i = strpos($gzData, "\0", $i) + 1;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2170
				if ( $flg & 2 )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2171
					$i = $i + 2;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2172
			}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2173
			$decompressed = @gzinflate( substr($gzData, $i, -8) );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2174
			if ( false !== $decompressed )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2175
				return $decompressed;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2176
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2177
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2178
		// Compressed data from java.util.zip.Deflater amongst others.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2179
		$decompressed = @gzinflate( substr($gzData, 2) );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2180
		if ( false !== $decompressed )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2181
			return $decompressed;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2182
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2183
		return false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2184
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2185
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2186
	/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2187
	 * What encoding types to accept and their priority values.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2188
	 *
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2189
	 * @since 2.8.0
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2190
	 *
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2191
	 * @param string $url
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2192
	 * @param array  $args
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2193
	 * @return string Types of encoding to accept.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2194
	 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2195
	public static function accept_encoding( $url, $args ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2196
		$type = array();
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2197
		$compression_enabled = self::is_available();
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2198
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2199
		if ( ! $args['decompress'] ) // Decompression specifically disabled.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2200
			$compression_enabled = false;
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2201
		elseif ( $args['stream'] ) // Disable when streaming to file.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2202
			$compression_enabled = false;
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2203
		elseif ( isset( $args['limit_response_size'] ) ) // If only partial content is being requested, we won't be able to decompress it.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2204
			$compression_enabled = false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2205
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2206
		if ( $compression_enabled ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2207
			if ( function_exists( 'gzinflate' ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2208
				$type[] = 'deflate;q=1.0';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2209
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2210
			if ( function_exists( 'gzuncompress' ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2211
				$type[] = 'compress;q=0.5';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2212
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2213
			if ( function_exists( 'gzdecode' ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2214
				$type[] = 'gzip;q=0.5';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2215
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2216
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2217
		/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2218
		 * Filter the allowed encoding types.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2219
		 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2220
		 * @since 3.6.0
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2221
		 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2222
		 * @param array  $type Encoding types allowed. Accepts 'gzinflate',
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2223
		 *                     'gzuncompress', 'gzdecode'.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2224
		 * @param string $url  URL of the HTTP request.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2225
		 * @param array  $args HTTP request arguments.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2226
		 */
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2227
		$type = apply_filters( 'wp_http_accept_encoding', $type, $url, $args );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2228
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2229
		return implode(', ', $type);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2230
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2231
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2232
	/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2233
	 * What encoding the content used when it was compressed to send in the headers.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2234
	 *
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2235
	 * @since 2.8.0
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2236
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2237
	 * @return string Content-Encoding string to send in the header.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2238
	 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2239
	public static function content_encoding() {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2240
		return 'deflate';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2241
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2242
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2243
	/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2244
	 * Whether the content be decoded based on the headers.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2245
	 *
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2246
	 * @since 2.8.0
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2247
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2248
	 * @param array|string $headers All of the available headers.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2249
	 * @return bool
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2250
	 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2251
	public static function should_decode($headers) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2252
		if ( is_array( $headers ) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2253
			if ( array_key_exists('content-encoding', $headers) && ! empty( $headers['content-encoding'] ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2254
				return true;
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2255
		} elseif ( is_string( $headers ) ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2256
			return ( stripos($headers, 'content-encoding:') !== false );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2257
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2258
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2259
		return false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2260
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2261
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2262
	/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2263
	 * Whether decompression and compression are supported by the PHP version.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2264
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2265
	 * Each function is tested instead of checking for the zlib extension, to
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2266
	 * ensure that the functions all exist in the PHP version and aren't
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2267
	 * disabled.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2268
	 *
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2269
	 * @since 2.8.0
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2270
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2271
	 * @return bool
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2272
	 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2273
	public static function is_available() {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2274
		return ( function_exists('gzuncompress') || function_exists('gzdeflate') || function_exists('gzinflate') );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2275
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2276
}