Completed
Push — feature/videopress-uploader ( 31b66d...022324 )
by
unknown
09:37
created

Jetpack_Client   B

Complexity

Total Complexity 50

Size/Duplication

Total Lines 327
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 1
Bugs 1 Features 1
Metric Value
c 1
b 1
f 1
dl 0
loc 327
rs 8.6206
wmc 50
lcom 1
cbo 5

5 Methods

Rating   Name   Duplication   Size   Complexity  
F remote_request() 0 123 16
B _stringify_data() 0 23 6
C _wp_remote_request() 0 73 14
D set_time_diff() 0 27 10
B wpcom_json_api_request_as_blog() 0 34 4

How to fix   Complexity   

Complex Class

Complex classes like Jetpack_Client often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use Jetpack_Client, and based on these observations, apply Extract Interface, too.

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
			'headers' => array(),
21
		);
22
23
		$args = wp_parse_args( $args, $defaults );
24
25
		$args['blog_id'] = (int) $args['blog_id'];
26
27
		if ( 'header' != $args['auth_location'] ) {
28
			$args['auth_location'] = 'query_string';
29
		}
30
31
		$token = Jetpack_Data::get_access_token( $args['user_id'] );
0 ignored issues
show
Documentation introduced by
$args['user_id'] is of type integer|string, but the function expects a boolean.

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:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
32
		if ( !$token ) {
33
			return new Jetpack_Error( 'missing_token' );
34
		}
35
36
		$method = strtoupper( $args['method'] );
37
38
		$timeout = intval( $args['timeout'] );
39
40
		$redirection = $args['redirection'];
41
42
		$request = compact( 'method', 'body', 'timeout', 'redirection' );
43
44
		@list( $token_key, $secret ) = explode( '.', $token->secret );
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
45
		if ( empty( $token ) || empty( $secret ) ) {
46
			return new Jetpack_Error( 'malformed_token' );
47
		}
48
49
		$token_key = sprintf( '%s:%d:%d', $token_key, JETPACK__API_VERSION, $token->external_user_id );
50
51
		require_once JETPACK__PLUGIN_DIR . 'class.jetpack-signature.php';
52
53
		$time_diff = (int) Jetpack_Options::get_option( 'time_diff' );
54
		$jetpack_signature = new Jetpack_Signature( $token->secret, $time_diff );
55
56
		$timestamp = time() + $time_diff;
57
58
		if( function_exists( 'wp_generate_password' ) ) {
59
			$nonce = wp_generate_password( 10, false );
60
		} else {
61
			$nonce = substr( sha1( rand( 0, 1000000 ) ), 0, 10);
62
		}
63
64
		// Kind of annoying.  Maybe refactor Jetpack_Signature to handle body-hashing
65
		if ( is_null( $body ) ) {
66
			$body_hash = '';
67
		} else {
68
69
			// Allow arrays to be used in passing data.
70
			$body_to_hash = $body;
71
			if ( is_array( $body ) ) {
72
				$body_to_hash = json_encode( self::_stringify_data( $body ) );
73
			}
74
75
			if ( !is_string( $body_to_hash ) ) {
76
				return new Jetpack_Error( 'invalid_body', 'Body is malformed.' );
77
			}
78
79
			$body_hash = jetpack_sha1_base64( $body_to_hash );
80
		}
81
82
		$auth = array(
83
			'token' => $token_key,
84
			'timestamp' => $timestamp,
85
			'nonce' => $nonce,
86
			'body-hash' => $body_hash,
87
		);
88
89
		if ( false !== strpos( $args['url'], 'xmlrpc.php' ) ) {
90
			$url_args = array(
91
				'for'           => 'jetpack',
92
				'wpcom_blog_id' => Jetpack_Options::get_option( 'id' ),
93
			);
94
		} else {
95
			$url_args = array();
96
		}
97
98
		if ( 'header' != $args['auth_location'] ) {
99
			$url_args += $auth;
100
		}
101
102
		$url = add_query_arg( urlencode_deep( $url_args ), $args['url'] );
103
		$url = Jetpack::fix_url_for_bad_hosts( $url );
104
105
		$signature = $jetpack_signature->sign_request( $token_key, $timestamp, $nonce, $body_hash, $method, $url, $body, false );
106
107
		if ( !$signature || is_wp_error( $signature ) ) {
108
			return $signature;
109
		}
110
111
		// Send an Authorization header so various caches/proxies do the right thing
112
		$auth['signature'] = $signature;
113
		$auth['version'] = JETPACK__VERSION;
114
		$header_pieces = array();
115
		foreach ( $auth as $key => $value ) {
116
			$header_pieces[] = sprintf( '%s="%s"', $key, $value );
117
		}
118
		$request['headers'] = array_merge( $args['headers'], array(
119
			'Authorization' => "X_JETPACK " . join( ' ', $header_pieces ),
120
		) );
121
122
		// Make sure we keep the host when we do JETPACK__WPCOM_JSON_API_HOST requests.
123
		$host = parse_url( $url, PHP_URL_HOST );
124
		if ( $host === JETPACK__WPCOM_JSON_API_HOST ) {
125
			$request['headers']['Host'] = 'public-api.wordpress.com';
126
		}
127
128
		if ( 'header' != $args['auth_location'] ) {
129
			$url = add_query_arg( 'signature', urlencode( $signature ), $url );
130
		}
131
132
		return Jetpack_Client::_wp_remote_request( $url, $request );
133
	}
134
135
	/**
136
	 * Takes an array or similar structure and recursively turns all values into strings. This is used to
137
	 * make sure that body hashes are made ith the string version, which is what will be seen after a
138
	 * server pulls up the data in the $_POST array.
139
	 *
140
	 * @param array|mixed $data
141
	 *
142
	 * @return array|string
143
	 */
144
	public static function _stringify_data( $data ) {
145
146
		// Booleans are special, lets just makes them and explicit 1/0 instead of the 0 being an empty string.
147
		if ( is_bool( $data ) ) {
148
			return $data ? "1" : "0";
149
		}
150
151
		// Cast objects into arrays.
152
		if ( is_object( $data ) ) {
153
			$data = (array) $data;
154
		}
155
156
		// Non arrays at this point should be just converted to strings.
157
		if ( ! is_array( $data ) ) {
158
			return (string)$data;
159
		}
160
161
		foreach ( $data as $key => &$value ) {
162
			$value = self::_stringify_data( $value );
163
		}
164
165
		return $data;
166
	}
167
168
	/**
169
	 * Wrapper for wp_remote_request().  Turns off SSL verification for certain SSL errors.
170
	 * This is lame, but many, many, many hosts have misconfigured SSL.
171
	 *
172
	 * When Jetpack is registered, the jetpack_fallback_no_verify_ssl_certs option is set to the current time if:
173
	 * 1. a certificate error is found AND
174
	 * 2. not verifying the certificate works around the problem.
175
	 *
176
	 * The option is checked on each request.
177
	 *
178
	 * @internal
179
	 * @see Jetpack::fix_url_for_bad_hosts()
180
	 *
181
	 * @return array|WP_Error WP HTTP response on success
182
	 */
183
	public static function _wp_remote_request( $url, $args, $set_fallback = false ) {
184
		/**
185
		 * SSL verification (`sslverify`) for the JetpackClient remote request
186
		 * defaults to off, use this filter to force it on.
187
		 *
188
		 * Return `true` to ENABLE SSL verification, return `false`
189
		 * to DISABLE SSL verification.
190
		 *
191
		 * @since 3.6.0
192
		 *
193
		 * @param bool Whether to force `sslverify` or not.
194
		 */
195
		if ( apply_filters( 'jetpack_client_verify_ssl_certs', false ) ) {
196
			return wp_remote_request( $url, $args );
197
		}
198
199
		$fallback = Jetpack_Options::get_option( 'fallback_no_verify_ssl_certs' );
200
		if ( false === $fallback ) {
201
			Jetpack_Options::update_option( 'fallback_no_verify_ssl_certs', 0 );
202
		}
203
204
		if ( (int) $fallback ) {
205
			// We're flagged to fallback
206
			$args['sslverify'] = false;
207
		}
208
209
		$response = wp_remote_request( $url, $args );
210
211
		if (
212
			!$set_fallback                                     // We're not allowed to set the flag on this request, so whatever happens happens
213
		||
214
			isset( $args['sslverify'] ) && !$args['sslverify'] // No verification - no point in doing it again
215
		||
216
			!is_wp_error( $response )                          // Let it ride
217
		) {
218
			Jetpack_Client::set_time_diff( $response, $set_fallback );
219
			return $response;
220
		}
221
222
		// At this point, we're not flagged to fallback and we are allowed to set the flag on this request.
223
224
		$message = $response->get_error_message();
225
226
		// Is it an SSL Certificate verification error?
227
		if (
228
			false === strpos( $message, '14090086' ) // OpenSSL SSL3 certificate error
229
		&&
230
			false === strpos( $message, '1407E086' ) // OpenSSL SSL2 certificate error
231
		&&
232
			false === strpos( $message, 'error setting certificate verify locations' ) // cURL CA bundle not found
233
		&&
234
			false === strpos( $message, 'Peer certificate cannot be authenticated with' ) // cURL CURLE_SSL_CACERT: CA bundle found, but not helpful
235
			                                                                              // different versions of curl have different error messages
236
			                                                                              // this string should catch them all
237
		&&
238
			false === strpos( $message, 'Problem with the SSL CA cert' ) // cURL CURLE_SSL_CACERT_BADFILE: probably access rights
239
		) {
240
			// No, it is not.
241
			return $response;
242
		}
243
244
		// Redo the request without SSL certificate verification.
245
		$args['sslverify'] = false;
246
		$response = wp_remote_request( $url, $args );
247
248
		if ( !is_wp_error( $response ) ) {
249
			// The request went through this time, flag for future fallbacks
250
			Jetpack_Options::update_option( 'fallback_no_verify_ssl_certs', time() );
251
			Jetpack_Client::set_time_diff( $response, $set_fallback );
252
		}
253
254
		return $response;
255
	}
256
257
	public static function set_time_diff( &$response, $force_set = false ) {
258
		$code = wp_remote_retrieve_response_code( $response );
259
260
		// Only trust the Date header on some responses
261
		if ( 200 != $code && 304 != $code && 400 != $code && 401 != $code ) {
262
			return;
263
		}
264
265
		if ( !$date = wp_remote_retrieve_header( $response, 'date' ) ) {
266
			return;
267
		}
268
269
		if ( 0 >= $time = (int) strtotime( $date ) ) {
270
			return;
271
		}
272
273
		$time_diff = $time - time();
274
275
		if ( $force_set ) { // during register
276
			Jetpack_Options::update_option( 'time_diff', $time_diff );
277
		} else { // otherwise
278
			$old_diff = Jetpack_Options::get_option( 'time_diff' );
279
			if ( false === $old_diff || abs( $time_diff - (int) $old_diff ) > 10 ) {
280
				Jetpack_Options::update_option( 'time_diff', $time_diff );
281
			}
282
		}
283
	}
284
285
	/**
286
	 * Query the WordPress.com REST API using the blog token
287
	 *
288
	 * @param string  $path
289
	 * @param string  $version
290
	 * @param array   $args
291
	 * @param string  $body
0 ignored issues
show
Documentation introduced by
Should the type for parameter $body not be string|null?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
292
	 * @return array|WP_Error $response Data.
293
	 */
294
	static function wpcom_json_api_request_as_blog( $path, $version = self::WPCOM_JSON_API_VERSION, $args = array(), $body = null ) {
295
		$filtered_args = array_intersect_key( $args, array(
296
			'method'      => 'string',
297
			'timeout'     => 'int',
298
			'redirection' => 'int',
299
		) );
300
301
		/**
302
		 * Determines whether Jetpack can send outbound https requests to the WPCOM api.
303
		 *
304
		 * @since 3.6.0
305
		 *
306
		 * @param bool $proto Defaults to true.
307
		 */
308
		$proto = apply_filters( 'jetpack_can_make_outbound_https', true ) ? 'https' : 'http';
309
310
		// unprecedingslashit
311
		$_path = preg_replace( '/^\//', '', $path );
312
313
		// Use GET by default whereas `remote_request` uses POST
314
		if ( isset( $filtered_args['method'] ) && strtoupper( $filtered_args['method'] === 'POST' ) ) {
315
			$request_method = 'POST';
316
		} else {
317
			$request_method = 'GET';
318
		}
319
320
		$validated_args = array_merge( $filtered_args, array(
321
			'url'     => sprintf( '%s://%s/rest/v%s/%s', $proto, JETPACK__WPCOM_JSON_API_HOST, $version, $_path ),
322
			'blog_id' => (int) Jetpack_Options::get_option( 'id' ),
323
			'method'  => $request_method,
324
		) );
325
326
		return Jetpack_Client::remote_request( $validated_args, $body );
327
	}
328
329
}
330