1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
class Jetpack_Client { |
4
|
|
|
const WPCOM_JSON_API_VERSION = '1.1'; |
5
|
|
|
|
6
|
|
|
/** |
7
|
|
|
* Makes an authorized remote request using Jetpack_Signature |
8
|
|
|
* |
9
|
|
|
* @return array|WP_Error WP HTTP response on success |
10
|
|
|
*/ |
11
|
|
|
public static function remote_request( $args, $body = null ) { |
12
|
|
|
$defaults = array( |
13
|
|
|
'url' => '', |
14
|
|
|
'user_id' => 0, |
15
|
|
|
'blog_id' => 0, |
16
|
|
|
'auth_location' => JETPACK_CLIENT__AUTH_LOCATION, |
17
|
|
|
'method' => 'POST', |
18
|
|
|
'timeout' => 10, |
19
|
|
|
'redirection' => 0, |
20
|
|
|
); |
21
|
|
|
|
22
|
|
|
$args = wp_parse_args( $args, $defaults ); |
23
|
|
|
|
24
|
|
|
$args['blog_id'] = (int) $args['blog_id']; |
25
|
|
|
|
26
|
|
|
if ( 'header' != $args['auth_location'] ) { |
27
|
|
|
$args['auth_location'] = 'query_string'; |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
$token = Jetpack_Data::get_access_token( $args['user_id'] ); |
|
|
|
|
31
|
|
|
if ( !$token ) { |
32
|
|
|
return new Jetpack_Error( 'missing_token' ); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
$method = strtoupper( $args['method'] ); |
36
|
|
|
|
37
|
|
|
$timeout = intval( $args['timeout'] ); |
38
|
|
|
|
39
|
|
|
$redirection = $args['redirection']; |
40
|
|
|
|
41
|
|
|
$request = compact( 'method', 'body', 'timeout', 'redirection' ); |
42
|
|
|
|
43
|
|
|
@list( $token_key, $secret ) = explode( '.', $token->secret ); |
|
|
|
|
44
|
|
|
if ( empty( $token ) || empty( $secret ) ) { |
45
|
|
|
return new Jetpack_Error( 'malformed_token' ); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
$token_key = sprintf( '%s:%d:%d', $token_key, JETPACK__API_VERSION, $token->external_user_id ); |
49
|
|
|
|
50
|
|
|
require_once JETPACK__PLUGIN_DIR . 'class.jetpack-signature.php'; |
51
|
|
|
|
52
|
|
|
$time_diff = (int) Jetpack_Options::get_option( 'time_diff' ); |
53
|
|
|
$jetpack_signature = new Jetpack_Signature( $token->secret, $time_diff ); |
54
|
|
|
|
55
|
|
|
$timestamp = time() + $time_diff; |
56
|
|
|
|
57
|
|
|
if( function_exists( 'wp_generate_password' ) ) { |
58
|
|
|
$nonce = wp_generate_password( 10, false ); |
59
|
|
|
} else { |
60
|
|
|
$nonce = substr( sha1( rand( 0, 1000000 ) ), 0, 10); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
// Kind of annoying. Maybe refactor Jetpack_Signature to handle body-hashing |
64
|
|
View Code Duplication |
if ( is_null( $body ) ) { |
65
|
|
|
$body_hash = ''; |
66
|
|
|
} else { |
67
|
|
|
if ( !is_string( $body ) ) { |
68
|
|
|
return new Jetpack_Error( 'invalid_body', 'Body is malformed.' ); |
69
|
|
|
} |
70
|
|
|
$body_hash = jetpack_sha1_base64( $body ); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
$auth = array( |
74
|
|
|
'token' => $token_key, |
75
|
|
|
'timestamp' => $timestamp, |
76
|
|
|
'nonce' => $nonce, |
77
|
|
|
'body-hash' => $body_hash, |
78
|
|
|
); |
79
|
|
|
|
80
|
|
|
if ( false !== strpos( $args['url'], 'xmlrpc.php' ) ) { |
81
|
|
|
$url_args = array( |
82
|
|
|
'for' => 'jetpack', |
83
|
|
|
'wpcom_blog_id' => Jetpack_Options::get_option( 'id' ), |
84
|
|
|
); |
85
|
|
|
} else { |
86
|
|
|
$url_args = array(); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
if ( 'header' != $args['auth_location'] ) { |
90
|
|
|
$url_args += $auth; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
$url = add_query_arg( urlencode_deep( $url_args ), $args['url'] ); |
94
|
|
|
$url = Jetpack::fix_url_for_bad_hosts( $url ); |
95
|
|
|
|
96
|
|
|
$signature = $jetpack_signature->sign_request( $token_key, $timestamp, $nonce, $body_hash, $method, $url, $body, false ); |
97
|
|
|
|
98
|
|
|
if ( !$signature || is_wp_error( $signature ) ) { |
99
|
|
|
return $signature; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
// Send an Authorization header so various caches/proxies do the right thing |
103
|
|
|
$auth['signature'] = $signature; |
104
|
|
|
$auth['version'] = JETPACK__VERSION; |
105
|
|
|
$header_pieces = array(); |
106
|
|
|
foreach ( $auth as $key => $value ) { |
107
|
|
|
$header_pieces[] = sprintf( '%s="%s"', $key, $value ); |
108
|
|
|
} |
109
|
|
|
$request['headers'] = array( |
110
|
|
|
'Authorization' => "X_JETPACK " . join( ' ', $header_pieces ), |
111
|
|
|
); |
112
|
|
|
|
113
|
|
|
// Make sure we keep the host when we do JETPACK__WPCOM_JSON_API_HOST requests. |
114
|
|
|
$host = parse_url( $url, PHP_URL_HOST ); |
115
|
|
|
if ( $host === JETPACK__WPCOM_JSON_API_HOST ) { |
116
|
|
|
$request['headers']['Host'] = 'public-api.wordpress.com'; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
if ( 'header' != $args['auth_location'] ) { |
120
|
|
|
$url = add_query_arg( 'signature', urlencode( $signature ), $url ); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
return Jetpack_Client::_wp_remote_request( $url, $request ); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* Wrapper for wp_remote_request(). Turns off SSL verification for certain SSL errors. |
128
|
|
|
* This is lame, but many, many, many hosts have misconfigured SSL. |
129
|
|
|
* |
130
|
|
|
* When Jetpack is registered, the jetpack_fallback_no_verify_ssl_certs option is set to the current time if: |
131
|
|
|
* 1. a certificate error is found AND |
132
|
|
|
* 2. not verifying the certificate works around the problem. |
133
|
|
|
* |
134
|
|
|
* The option is checked on each request. |
135
|
|
|
* |
136
|
|
|
* @internal |
137
|
|
|
* @see Jetpack::fix_url_for_bad_hosts() |
138
|
|
|
* |
139
|
|
|
* @return array|WP_Error WP HTTP response on success |
140
|
|
|
*/ |
141
|
|
|
public static function _wp_remote_request( $url, $args, $set_fallback = false ) { |
142
|
|
|
/** |
143
|
|
|
* SSL verification (`sslverify`) for the JetpackClient remote request |
144
|
|
|
* defaults to off, use this filter to force it on. |
145
|
|
|
* |
146
|
|
|
* Return `true` to ENABLE SSL verification, return `false` |
147
|
|
|
* to DISABLE SSL verification. |
148
|
|
|
* |
149
|
|
|
* @since 3.6.0 |
150
|
|
|
* |
151
|
|
|
* @param bool Whether to force `sslverify` or not. |
152
|
|
|
*/ |
153
|
|
|
if ( apply_filters( 'jetpack_client_verify_ssl_certs', false ) ) { |
154
|
|
|
return wp_remote_request( $url, $args ); |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
$fallback = Jetpack_Options::get_option( 'fallback_no_verify_ssl_certs' ); |
158
|
|
|
if ( false === $fallback ) { |
159
|
|
|
Jetpack_Options::update_option( 'fallback_no_verify_ssl_certs', 0 ); |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
if ( (int) $fallback ) { |
163
|
|
|
// We're flagged to fallback |
164
|
|
|
$args['sslverify'] = false; |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
$response = wp_remote_request( $url, $args ); |
168
|
|
|
|
169
|
|
|
if ( |
170
|
|
|
!$set_fallback // We're not allowed to set the flag on this request, so whatever happens happens |
171
|
|
|
|| |
172
|
|
|
isset( $args['sslverify'] ) && !$args['sslverify'] // No verification - no point in doing it again |
173
|
|
|
|| |
174
|
|
|
!is_wp_error( $response ) // Let it ride |
175
|
|
|
) { |
176
|
|
|
Jetpack_Client::set_time_diff( $response, $set_fallback ); |
177
|
|
|
return $response; |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
// At this point, we're not flagged to fallback and we are allowed to set the flag on this request. |
181
|
|
|
|
182
|
|
|
$message = $response->get_error_message(); |
183
|
|
|
|
184
|
|
|
// Is it an SSL Certificate verification error? |
185
|
|
|
if ( |
186
|
|
|
false === strpos( $message, '14090086' ) // OpenSSL SSL3 certificate error |
187
|
|
|
&& |
188
|
|
|
false === strpos( $message, '1407E086' ) // OpenSSL SSL2 certificate error |
189
|
|
|
&& |
190
|
|
|
false === strpos( $message, 'error setting certificate verify locations' ) // cURL CA bundle not found |
191
|
|
|
&& |
192
|
|
|
false === strpos( $message, 'Peer certificate cannot be authenticated with' ) // cURL CURLE_SSL_CACERT: CA bundle found, but not helpful |
193
|
|
|
// different versions of curl have different error messages |
194
|
|
|
// this string should catch them all |
195
|
|
|
&& |
196
|
|
|
false === strpos( $message, 'Problem with the SSL CA cert' ) // cURL CURLE_SSL_CACERT_BADFILE: probably access rights |
197
|
|
|
) { |
198
|
|
|
// No, it is not. |
199
|
|
|
return $response; |
200
|
|
|
} |
201
|
|
|
|
202
|
|
|
// Redo the request without SSL certificate verification. |
203
|
|
|
$args['sslverify'] = false; |
204
|
|
|
$response = wp_remote_request( $url, $args ); |
205
|
|
|
|
206
|
|
|
if ( !is_wp_error( $response ) ) { |
207
|
|
|
// The request went through this time, flag for future fallbacks |
208
|
|
|
Jetpack_Options::update_option( 'fallback_no_verify_ssl_certs', time() ); |
209
|
|
|
Jetpack_Client::set_time_diff( $response, $set_fallback ); |
210
|
|
|
} |
211
|
|
|
|
212
|
|
|
return $response; |
213
|
|
|
} |
214
|
|
|
|
215
|
|
|
public static function set_time_diff( &$response, $force_set = false ) { |
216
|
|
|
$code = wp_remote_retrieve_response_code( $response ); |
217
|
|
|
|
218
|
|
|
// Only trust the Date header on some responses |
219
|
|
|
if ( 200 != $code && 304 != $code && 400 != $code && 401 != $code ) { |
220
|
|
|
return; |
221
|
|
|
} |
222
|
|
|
|
223
|
|
|
if ( !$date = wp_remote_retrieve_header( $response, 'date' ) ) { |
224
|
|
|
return; |
225
|
|
|
} |
226
|
|
|
|
227
|
|
|
if ( 0 >= $time = (int) strtotime( $date ) ) { |
228
|
|
|
return; |
229
|
|
|
} |
230
|
|
|
|
231
|
|
|
$time_diff = $time - time(); |
232
|
|
|
|
233
|
|
|
if ( $force_set ) { // during register |
234
|
|
|
Jetpack_Options::update_option( 'time_diff', $time_diff ); |
235
|
|
|
} else { // otherwise |
236
|
|
|
$old_diff = Jetpack_Options::get_option( 'time_diff' ); |
237
|
|
|
if ( false === $old_diff || abs( $time_diff - (int) $old_diff ) > 10 ) { |
238
|
|
|
Jetpack_Options::update_option( 'time_diff', $time_diff ); |
239
|
|
|
} |
240
|
|
|
} |
241
|
|
|
} |
242
|
|
|
|
243
|
|
|
/** |
244
|
|
|
* Query the WordPress.com REST API using the blog token |
245
|
|
|
* |
246
|
|
|
* @param string $path |
247
|
|
|
* @param string $version |
248
|
|
|
* @param array $args |
249
|
|
|
* @param string $body |
|
|
|
|
250
|
|
|
* @return array|WP_Error $response Data. |
251
|
|
|
*/ |
252
|
|
|
static function wpcom_json_api_request_as_blog( $path, $version = self::WPCOM_JSON_API_VERSION, $args = array(), $body = null ) { |
253
|
|
|
$filtered_args = array_intersect_key( $args, array( |
254
|
|
|
'method' => 'string', |
255
|
|
|
'timeout' => 'int', |
256
|
|
|
'redirection' => 'int', |
257
|
|
|
) ); |
258
|
|
|
|
259
|
|
|
/** |
260
|
|
|
* Determines whether Jetpack can send outbound https requests to the WPCOM api. |
261
|
|
|
* |
262
|
|
|
* @since 3.6.0 |
263
|
|
|
* |
264
|
|
|
* @param bool $proto Defaults to true. |
265
|
|
|
*/ |
266
|
|
|
$proto = apply_filters( 'jetpack_can_make_outbound_https', true ) ? 'https' : 'http'; |
267
|
|
|
|
268
|
|
|
// unprecedingslashit |
269
|
|
|
$_path = preg_replace( '/^\//', '', $path ); |
270
|
|
|
|
271
|
|
|
// Use GET by default whereas `remote_request` uses POST |
272
|
|
|
if ( isset( $filtered_args['method'] ) && strtoupper( $filtered_args['method'] === 'POST' ) ) { |
273
|
|
|
$request_method = 'POST'; |
274
|
|
|
} else { |
275
|
|
|
$request_method = 'GET'; |
276
|
|
|
} |
277
|
|
|
|
278
|
|
|
$validated_args = array_merge( $filtered_args, array( |
279
|
|
|
'url' => sprintf( '%s://%s/rest/v%s/%s', $proto, JETPACK__WPCOM_JSON_API_HOST, $version, $_path ), |
280
|
|
|
'blog_id' => (int) Jetpack_Options::get_option( 'id' ), |
281
|
|
|
'method' => $request_method, |
282
|
|
|
) ); |
283
|
|
|
|
284
|
|
|
return Jetpack_Client::remote_request( $validated_args, $body ); |
285
|
|
|
} |
286
|
|
|
|
287
|
|
|
} |
288
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: