author | ymh <ymh.work@gmail.com> |
Fri, 05 Sep 2025 18:40:08 +0200 | |
changeset 21 | 48c4eec2b7e6 |
parent 19 | 3d72ae0968f4 |
permissions | -rw-r--r-- |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
1 |
<?php |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
2 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
3 |
* HTTP API: WP_Http_Streams class |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
4 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
5 |
* @package WordPress |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
6 |
* @subpackage HTTP |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
7 |
* @since 4.4.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
8 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
9 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
10 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
11 |
* Core class used to integrate PHP Streams as an HTTP transport. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
12 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
13 |
* @since 2.7.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
14 |
* @since 3.7.0 Combined with the fsockopen transport and switched to `stream_socket_client()`. |
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
15 |
* @deprecated 6.4.0 Use WP_Http |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
16 |
* @see WP_Http |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
17 |
*/ |
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
18 |
#[AllowDynamicProperties] |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
19 |
class WP_Http_Streams { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
20 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
21 |
* Send a HTTP request to a URI using PHP Streams. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
22 |
* |
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
23 |
* @see WP_Http::request() For default options descriptions. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
24 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
25 |
* @since 2.7.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
26 |
* @since 3.7.0 Combined with the fsockopen transport and switched to stream_socket_client(). |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
27 |
* |
16 | 28 |
* @param string $url The request URL. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
29 |
* @param string|array $args Optional. Override the defaults. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
30 |
* @return array|WP_Error Array containing 'headers', 'body', 'response', 'cookies', 'filename'. A WP_Error instance upon error |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
31 |
*/ |
9 | 32 |
public function request( $url, $args = array() ) { |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
33 |
$defaults = array( |
9 | 34 |
'method' => 'GET', |
35 |
'timeout' => 5, |
|
36 |
'redirection' => 5, |
|
37 |
'httpversion' => '1.0', |
|
38 |
'blocking' => true, |
|
39 |
'headers' => array(), |
|
40 |
'body' => null, |
|
41 |
'cookies' => array(), |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
42 |
'decompress' => false, |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
43 |
'stream' => false, |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
44 |
'filename' => null, |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
45 |
); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
46 |
|
16 | 47 |
$parsed_args = wp_parse_args( $args, $defaults ); |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
48 |
|
16 | 49 |
if ( isset( $parsed_args['headers']['User-Agent'] ) ) { |
50 |
$parsed_args['user-agent'] = $parsed_args['headers']['User-Agent']; |
|
51 |
unset( $parsed_args['headers']['User-Agent'] ); |
|
52 |
} elseif ( isset( $parsed_args['headers']['user-agent'] ) ) { |
|
53 |
$parsed_args['user-agent'] = $parsed_args['headers']['user-agent']; |
|
54 |
unset( $parsed_args['headers']['user-agent'] ); |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
55 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
56 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
57 |
// Construct Cookie: header if any cookies are set. |
16 | 58 |
WP_Http::buildCookieHeader( $parsed_args ); |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
59 |
|
19 | 60 |
$parsed_url = parse_url( $url ); |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
61 |
|
19 | 62 |
$connect_host = $parsed_url['host']; |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
63 |
|
19 | 64 |
$secure_transport = ( 'ssl' === $parsed_url['scheme'] || 'https' === $parsed_url['scheme'] ); |
65 |
if ( ! isset( $parsed_url['port'] ) ) { |
|
66 |
if ( 'ssl' === $parsed_url['scheme'] || 'https' === $parsed_url['scheme'] ) { |
|
67 |
$parsed_url['port'] = 443; |
|
68 |
$secure_transport = true; |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
69 |
} else { |
19 | 70 |
$parsed_url['port'] = 80; |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
71 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
72 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
73 |
|
16 | 74 |
// Always pass a path, defaulting to the root in cases such as http://example.com. |
19 | 75 |
if ( ! isset( $parsed_url['path'] ) ) { |
76 |
$parsed_url['path'] = '/'; |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
77 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
78 |
|
16 | 79 |
if ( isset( $parsed_args['headers']['Host'] ) || isset( $parsed_args['headers']['host'] ) ) { |
80 |
if ( isset( $parsed_args['headers']['Host'] ) ) { |
|
19 | 81 |
$parsed_url['host'] = $parsed_args['headers']['Host']; |
9 | 82 |
} else { |
19 | 83 |
$parsed_url['host'] = $parsed_args['headers']['host']; |
9 | 84 |
} |
16 | 85 |
unset( $parsed_args['headers']['Host'], $parsed_args['headers']['host'] ); |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
86 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
87 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
88 |
/* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
89 |
* Certain versions of PHP have issues with 'localhost' and IPv6, It attempts to connect |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
90 |
* to ::1, which fails when the server is not set up for it. For compatibility, always |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
91 |
* connect to the IPv4 address. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
92 |
*/ |
16 | 93 |
if ( 'localhost' === strtolower( $connect_host ) ) { |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
94 |
$connect_host = '127.0.0.1'; |
9 | 95 |
} |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
96 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
97 |
$connect_host = $secure_transport ? 'ssl://' . $connect_host : 'tcp://' . $connect_host; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
98 |
|
16 | 99 |
$is_local = isset( $parsed_args['local'] ) && $parsed_args['local']; |
100 |
$ssl_verify = isset( $parsed_args['sslverify'] ) && $parsed_args['sslverify']; |
|
19 | 101 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
102 |
if ( $is_local ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
103 |
/** |
18 | 104 |
* Filters whether SSL should be verified for local HTTP API requests. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
105 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
106 |
* @since 2.8.0 |
9 | 107 |
* @since 5.1.0 The `$url` parameter was added. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
108 |
* |
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
109 |
* @param bool|string $ssl_verify Boolean to control whether to verify the SSL connection |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
110 |
* or path to an SSL certificate. |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
111 |
* @param string $url The request URL. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
112 |
*/ |
9 | 113 |
$ssl_verify = apply_filters( 'https_local_ssl_verify', $ssl_verify, $url ); |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
114 |
} elseif ( ! $is_local ) { |
19 | 115 |
/** This filter is documented in wp-includes/class-wp-http.php */ |
9 | 116 |
$ssl_verify = apply_filters( 'https_ssl_verify', $ssl_verify, $url ); |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
117 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
118 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
119 |
$proxy = new WP_HTTP_Proxy(); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
120 |
|
9 | 121 |
$context = stream_context_create( |
122 |
array( |
|
123 |
'ssl' => array( |
|
124 |
'verify_peer' => $ssl_verify, |
|
19 | 125 |
// 'CN_match' => $parsed_url['host'], // This is handled by self::verify_ssl_certificate(). |
9 | 126 |
'capture_peer_cert' => $ssl_verify, |
127 |
'SNI_enabled' => true, |
|
16 | 128 |
'cafile' => $parsed_args['sslcertificates'], |
9 | 129 |
'allow_self_signed' => ! $ssl_verify, |
130 |
), |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
131 |
) |
9 | 132 |
); |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
133 |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
134 |
$timeout = (int) floor( $parsed_args['timeout'] ); |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
135 |
$utimeout = 0; |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
136 |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
137 |
if ( $timeout !== (int) $parsed_args['timeout'] ) { |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
138 |
$utimeout = 1000000 * $parsed_args['timeout'] % 1000000; |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
139 |
} |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
140 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
141 |
$connect_timeout = max( $timeout, 1 ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
142 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
143 |
// Store error number. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
144 |
$connection_error = null; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
145 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
146 |
// Store error string. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
147 |
$connection_error_str = null; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
148 |
|
9 | 149 |
if ( ! WP_DEBUG ) { |
16 | 150 |
// In the event that the SSL connection fails, silence the many PHP warnings. |
9 | 151 |
if ( $secure_transport ) { |
152 |
$error_reporting = error_reporting( 0 ); |
|
153 |
} |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
154 |
|
9 | 155 |
if ( $proxy->is_enabled() && $proxy->send_through_proxy( $url ) ) { |
16 | 156 |
// phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged |
19 | 157 |
$handle = @stream_socket_client( |
158 |
'tcp://' . $proxy->host() . ':' . $proxy->port(), |
|
159 |
$connection_error, |
|
160 |
$connection_error_str, |
|
161 |
$connect_timeout, |
|
162 |
STREAM_CLIENT_CONNECT, |
|
163 |
$context |
|
164 |
); |
|
9 | 165 |
} else { |
16 | 166 |
// phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged |
19 | 167 |
$handle = @stream_socket_client( |
168 |
$connect_host . ':' . $parsed_url['port'], |
|
169 |
$connection_error, |
|
170 |
$connection_error_str, |
|
171 |
$connect_timeout, |
|
172 |
STREAM_CLIENT_CONNECT, |
|
173 |
$context |
|
174 |
); |
|
9 | 175 |
} |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
176 |
|
9 | 177 |
if ( $secure_transport ) { |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
178 |
error_reporting( $error_reporting ); |
9 | 179 |
} |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
180 |
} else { |
9 | 181 |
if ( $proxy->is_enabled() && $proxy->send_through_proxy( $url ) ) { |
19 | 182 |
$handle = stream_socket_client( |
183 |
'tcp://' . $proxy->host() . ':' . $proxy->port(), |
|
184 |
$connection_error, |
|
185 |
$connection_error_str, |
|
186 |
$connect_timeout, |
|
187 |
STREAM_CLIENT_CONNECT, |
|
188 |
$context |
|
189 |
); |
|
9 | 190 |
} else { |
19 | 191 |
$handle = stream_socket_client( |
192 |
$connect_host . ':' . $parsed_url['port'], |
|
193 |
$connection_error, |
|
194 |
$connection_error_str, |
|
195 |
$connect_timeout, |
|
196 |
STREAM_CLIENT_CONNECT, |
|
197 |
$context |
|
198 |
); |
|
9 | 199 |
} |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
200 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
201 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
202 |
if ( false === $handle ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
203 |
// SSL connection failed due to expired/invalid cert, or, OpenSSL configuration is broken. |
9 | 204 |
if ( $secure_transport && 0 === $connection_error && '' === $connection_error_str ) { |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
205 |
return new WP_Error( 'http_request_failed', __( 'The SSL certificate for the host could not be verified.' ) ); |
9 | 206 |
} |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
207 |
|
9 | 208 |
return new WP_Error( 'http_request_failed', $connection_error . ': ' . $connection_error_str ); |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
209 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
210 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
211 |
// Verify that the SSL certificate is valid for this request. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
212 |
if ( $secure_transport && $ssl_verify && ! $proxy->is_enabled() ) { |
19 | 213 |
if ( ! self::verify_ssl_certificate( $handle, $parsed_url['host'] ) ) { |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
214 |
return new WP_Error( 'http_request_failed', __( 'The SSL certificate for the host could not be verified.' ) ); |
9 | 215 |
} |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
216 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
217 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
218 |
stream_set_timeout( $handle, $timeout, $utimeout ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
219 |
|
16 | 220 |
if ( $proxy->is_enabled() && $proxy->send_through_proxy( $url ) ) { // Some proxies require full URL in this field. |
19 | 221 |
$request_path = $url; |
9 | 222 |
} else { |
19 | 223 |
$request_path = $parsed_url['path'] . ( isset( $parsed_url['query'] ) ? '?' . $parsed_url['query'] : '' ); |
9 | 224 |
} |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
225 |
|
19 | 226 |
$headers = strtoupper( $parsed_args['method'] ) . ' ' . $request_path . ' HTTP/' . $parsed_args['httpversion'] . "\r\n"; |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
227 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
228 |
$include_port_in_host_header = ( |
19 | 229 |
( $proxy->is_enabled() && $proxy->send_through_proxy( $url ) ) |
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
230 |
|| ( 'http' === $parsed_url['scheme'] && 80 !== $parsed_url['port'] ) |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
231 |
|| ( 'https' === $parsed_url['scheme'] && 443 !== $parsed_url['port'] ) |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
232 |
); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
233 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
234 |
if ( $include_port_in_host_header ) { |
19 | 235 |
$headers .= 'Host: ' . $parsed_url['host'] . ':' . $parsed_url['port'] . "\r\n"; |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
236 |
} else { |
19 | 237 |
$headers .= 'Host: ' . $parsed_url['host'] . "\r\n"; |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
238 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
239 |
|
16 | 240 |
if ( isset( $parsed_args['user-agent'] ) ) { |
19 | 241 |
$headers .= 'User-agent: ' . $parsed_args['user-agent'] . "\r\n"; |
9 | 242 |
} |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
243 |
|
16 | 244 |
if ( is_array( $parsed_args['headers'] ) ) { |
19 | 245 |
foreach ( (array) $parsed_args['headers'] as $header => $header_value ) { |
246 |
$headers .= $header . ': ' . $header_value . "\r\n"; |
|
9 | 247 |
} |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
248 |
} else { |
19 | 249 |
$headers .= $parsed_args['headers']; |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
250 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
251 |
|
9 | 252 |
if ( $proxy->use_authentication() ) { |
19 | 253 |
$headers .= $proxy->authentication_header() . "\r\n"; |
9 | 254 |
} |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
255 |
|
19 | 256 |
$headers .= "\r\n"; |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
257 |
|
16 | 258 |
if ( ! is_null( $parsed_args['body'] ) ) { |
19 | 259 |
$headers .= $parsed_args['body']; |
9 | 260 |
} |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
261 |
|
19 | 262 |
fwrite( $handle, $headers ); |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
263 |
|
16 | 264 |
if ( ! $parsed_args['blocking'] ) { |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
265 |
stream_set_blocking( $handle, 0 ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
266 |
fclose( $handle ); |
9 | 267 |
return array( |
268 |
'headers' => array(), |
|
269 |
'body' => '', |
|
270 |
'response' => array( |
|
271 |
'code' => false, |
|
272 |
'message' => false, |
|
273 |
), |
|
274 |
'cookies' => array(), |
|
275 |
); |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
276 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
277 |
|
19 | 278 |
$response = ''; |
279 |
$body_started = false; |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
280 |
$keep_reading = true; |
9 | 281 |
$block_size = 4096; |
19 | 282 |
|
16 | 283 |
if ( isset( $parsed_args['limit_response_size'] ) ) { |
284 |
$block_size = min( $block_size, $parsed_args['limit_response_size'] ); |
|
9 | 285 |
} |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
286 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
287 |
// If streaming to a file setup the file handle. |
16 | 288 |
if ( $parsed_args['stream'] ) { |
9 | 289 |
if ( ! WP_DEBUG ) { |
16 | 290 |
$stream_handle = @fopen( $parsed_args['filename'], 'w+' ); |
9 | 291 |
} else { |
16 | 292 |
$stream_handle = fopen( $parsed_args['filename'], 'w+' ); |
9 | 293 |
} |
19 | 294 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
295 |
if ( ! $stream_handle ) { |
9 | 296 |
return new WP_Error( |
297 |
'http_request_failed', |
|
298 |
sprintf( |
|
16 | 299 |
/* translators: 1: fopen(), 2: File name. */ |
9 | 300 |
__( 'Could not open handle for %1$s to %2$s.' ), |
301 |
'fopen()', |
|
16 | 302 |
$parsed_args['filename'] |
9 | 303 |
) |
304 |
); |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
305 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
306 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
307 |
$bytes_written = 0; |
19 | 308 |
|
9 | 309 |
while ( ! feof( $handle ) && $keep_reading ) { |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
310 |
$block = fread( $handle, $block_size ); |
19 | 311 |
if ( ! $body_started ) { |
312 |
$response .= $block; |
|
313 |
if ( strpos( $response, "\r\n\r\n" ) ) { |
|
314 |
$processed_response = WP_Http::processResponse( $response ); |
|
315 |
$body_started = true; |
|
316 |
$block = $processed_response['body']; |
|
317 |
unset( $response ); |
|
318 |
$processed_response['body'] = ''; |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
319 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
320 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
321 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
322 |
$this_block_size = strlen( $block ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
323 |
|
19 | 324 |
if ( isset( $parsed_args['limit_response_size'] ) |
325 |
&& ( $bytes_written + $this_block_size ) > $parsed_args['limit_response_size'] |
|
326 |
) { |
|
16 | 327 |
$this_block_size = ( $parsed_args['limit_response_size'] - $bytes_written ); |
9 | 328 |
$block = substr( $block, 0, $this_block_size ); |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
329 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
330 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
331 |
$bytes_written_to_file = fwrite( $stream_handle, $block ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
332 |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
333 |
if ( $bytes_written_to_file !== $this_block_size ) { |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
334 |
fclose( $handle ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
335 |
fclose( $stream_handle ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
336 |
return new WP_Error( 'http_request_failed', __( 'Failed to write request to temporary file.' ) ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
337 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
338 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
339 |
$bytes_written += $bytes_written_to_file; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
340 |
|
19 | 341 |
$keep_reading = ( |
342 |
! isset( $parsed_args['limit_response_size'] ) |
|
343 |
|| $bytes_written < $parsed_args['limit_response_size'] |
|
344 |
); |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
345 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
346 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
347 |
fclose( $stream_handle ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
348 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
349 |
} else { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
350 |
$header_length = 0; |
19 | 351 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
352 |
while ( ! feof( $handle ) && $keep_reading ) { |
19 | 353 |
$block = fread( $handle, $block_size ); |
354 |
$response .= $block; |
|
355 |
||
356 |
if ( ! $body_started && strpos( $response, "\r\n\r\n" ) ) { |
|
357 |
$header_length = strpos( $response, "\r\n\r\n" ) + 4; |
|
358 |
$body_started = true; |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
359 |
} |
19 | 360 |
|
361 |
$keep_reading = ( |
|
362 |
! $body_started |
|
363 |
|| ! isset( $parsed_args['limit_response_size'] ) |
|
364 |
|| strlen( $response ) < ( $header_length + $parsed_args['limit_response_size'] ) |
|
365 |
); |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
366 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
367 |
|
19 | 368 |
$processed_response = WP_Http::processResponse( $response ); |
369 |
unset( $response ); |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
370 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
371 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
372 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
373 |
fclose( $handle ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
374 |
|
19 | 375 |
$processed_headers = WP_Http::processHeaders( $processed_response['headers'], $url ); |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
376 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
377 |
$response = array( |
19 | 378 |
'headers' => $processed_headers['headers'], |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
379 |
// Not yet processed. |
9 | 380 |
'body' => null, |
19 | 381 |
'response' => $processed_headers['response'], |
382 |
'cookies' => $processed_headers['cookies'], |
|
16 | 383 |
'filename' => $parsed_args['filename'], |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
384 |
); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
385 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
386 |
// Handle redirects. |
16 | 387 |
$redirect_response = WP_Http::handle_redirects( $url, $parsed_args, $response ); |
388 |
if ( false !== $redirect_response ) { |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
389 |
return $redirect_response; |
9 | 390 |
} |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
391 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
392 |
// If the body was chunk encoded, then decode it. |
19 | 393 |
if ( ! empty( $processed_response['body'] ) |
394 |
&& isset( $processed_headers['headers']['transfer-encoding'] ) |
|
395 |
&& 'chunked' === $processed_headers['headers']['transfer-encoding'] |
|
16 | 396 |
) { |
19 | 397 |
$processed_response['body'] = WP_Http::chunkTransferDecode( $processed_response['body'] ); |
9 | 398 |
} |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
399 |
|
19 | 400 |
if ( true === $parsed_args['decompress'] |
401 |
&& true === WP_Http_Encoding::should_decode( $processed_headers['headers'] ) |
|
402 |
) { |
|
403 |
$processed_response['body'] = WP_Http_Encoding::decompress( $processed_response['body'] ); |
|
9 | 404 |
} |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
405 |
|
19 | 406 |
if ( isset( $parsed_args['limit_response_size'] ) |
407 |
&& strlen( $processed_response['body'] ) > $parsed_args['limit_response_size'] |
|
408 |
) { |
|
409 |
$processed_response['body'] = substr( $processed_response['body'], 0, $parsed_args['limit_response_size'] ); |
|
9 | 410 |
} |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
411 |
|
19 | 412 |
$response['body'] = $processed_response['body']; |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
413 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
414 |
return $response; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
415 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
416 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
417 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
418 |
* Verifies the received SSL certificate against its Common Names and subjectAltName fields. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
419 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
420 |
* PHP's SSL verifications only verify that it's a valid Certificate, it doesn't verify if |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
421 |
* the certificate is valid for the hostname which was requested. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
422 |
* This function verifies the requested hostname against certificate's subjectAltName field, |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
423 |
* if that is empty, or contains no DNS entries, a fallback to the Common Name field is used. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
424 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
425 |
* IP Address support is included if the request is being made to an IP address. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
426 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
427 |
* @since 3.7.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
428 |
* |
19 | 429 |
* @param resource $stream The PHP Stream which the SSL request is being made over |
430 |
* @param string $host The hostname being requested |
|
431 |
* @return bool If the certificate presented in $stream is valid for $host |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
432 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
433 |
public static function verify_ssl_certificate( $stream, $host ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
434 |
$context_options = stream_context_get_options( $stream ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
435 |
|
9 | 436 |
if ( empty( $context_options['ssl']['peer_certificate'] ) ) { |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
437 |
return false; |
9 | 438 |
} |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
439 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
440 |
$cert = openssl_x509_parse( $context_options['ssl']['peer_certificate'] ); |
9 | 441 |
if ( ! $cert ) { |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
442 |
return false; |
9 | 443 |
} |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
444 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
445 |
/* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
446 |
* If the request is being made to an IP address, we'll validate against IP fields |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
447 |
* in the cert (if they exist) |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
448 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
449 |
$host_type = ( WP_Http::is_ip_address( $host ) ? 'ip' : 'dns' ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
450 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
451 |
$certificate_hostnames = array(); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
452 |
if ( ! empty( $cert['extensions']['subjectAltName'] ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
453 |
$match_against = preg_split( '/,\s*/', $cert['extensions']['subjectAltName'] ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
454 |
foreach ( $match_against as $match ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
455 |
list( $match_type, $match_host ) = explode( ':', $match ); |
16 | 456 |
if ( strtolower( trim( $match_type ) ) === $host_type ) { // IP: or DNS: |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
457 |
$certificate_hostnames[] = strtolower( trim( $match_host ) ); |
9 | 458 |
} |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
459 |
} |
9 | 460 |
} elseif ( ! empty( $cert['subject']['CN'] ) ) { |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
461 |
// Only use the CN when the certificate includes no subjectAltName extension. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
462 |
$certificate_hostnames[] = strtolower( $cert['subject']['CN'] ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
463 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
464 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
465 |
// Exact hostname/IP matches. |
16 | 466 |
if ( in_array( strtolower( $host ), $certificate_hostnames, true ) ) { |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
467 |
return true; |
9 | 468 |
} |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
469 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
470 |
// IP's can't be wildcards, Stop processing. |
16 | 471 |
if ( 'ip' === $host_type ) { |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
472 |
return false; |
9 | 473 |
} |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
474 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
475 |
// Test to see if the domain is at least 2 deep for wildcard support. |
9 | 476 |
if ( substr_count( $host, '.' ) < 2 ) { |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
477 |
return false; |
9 | 478 |
} |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
479 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
480 |
// Wildcard subdomains certs (*.example.com) are valid for a.example.com but not a.b.example.com. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
481 |
$wildcard_host = preg_replace( '/^[^.]+\./', '*.', $host ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
482 |
|
16 | 483 |
return in_array( strtolower( $wildcard_host ), $certificate_hostnames, true ); |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
484 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
485 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
486 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
487 |
* Determines whether this class can be used for retrieving a URL. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
488 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
489 |
* @since 2.7.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
490 |
* @since 3.7.0 Combined with the fsockopen transport and switched to stream_socket_client(). |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
491 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
492 |
* @param array $args Optional. Array of request arguments. Default empty array. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
493 |
* @return bool False means this class can not be used, true means it can. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
494 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
495 |
public static function test( $args = array() ) { |
9 | 496 |
if ( ! function_exists( 'stream_socket_client' ) ) { |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
497 |
return false; |
9 | 498 |
} |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
499 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
500 |
$is_ssl = isset( $args['ssl'] ) && $args['ssl']; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
501 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
502 |
if ( $is_ssl ) { |
9 | 503 |
if ( ! extension_loaded( 'openssl' ) ) { |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
504 |
return false; |
9 | 505 |
} |
506 |
if ( ! function_exists( 'openssl_x509_parse' ) ) { |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
507 |
return false; |
9 | 508 |
} |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
509 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
510 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
511 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
512 |
* Filters whether streams can be used as a transport for retrieving a URL. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
513 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
514 |
* @since 2.7.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
515 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
516 |
* @param bool $use_class Whether the class can be used. Default true. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
517 |
* @param array $args Request arguments. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
518 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
519 |
return apply_filters( 'use_streams_transport', true, $args ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
520 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
521 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
522 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
523 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
524 |
* Deprecated HTTP Transport method which used fsockopen. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
525 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
526 |
* This class is not used, and is included for backward compatibility only. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
527 |
* All code should make use of WP_Http directly through its API. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
528 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
529 |
* @see WP_HTTP::request |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
530 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
531 |
* @since 2.7.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
532 |
* @deprecated 3.7.0 Please use WP_HTTP::request() directly |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
533 |
*/ |
19 | 534 |
class WP_HTTP_Fsockopen extends WP_Http_Streams { |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
535 |
// For backward compatibility for users who are using the class directly. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
536 |
} |