Completed
Push — add/80-changelog ( e8df2c...cdbe9d )
by Jeremy
08:23
created

class.jetpack-client-server.php (3 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
use Automattic\Jetpack\Connection\Client;
4
use Automattic\Jetpack\Connection\Manager as Connection_Manager;
5
use Automattic\Jetpack\Connection\Utils as Connection_Utils;
6
use Automattic\Jetpack\Roles;
7
use Automattic\Jetpack\Tracking;
8
9
/**
10
 * Client = Plugin
11
 * Client Server = API Methods the Plugin must respond to
12
 */
13
class Jetpack_Client_Server {
14
15
	/**
16
	 * Authorizations
17
	 */
18
	function client_authorize() {
19
		$data              = stripslashes_deep( $_GET );
20
		$data['auth_type'] = 'client';
21
		$roles             = new Roles();
22
		$role              = $roles->translate_current_user_to_role();
23
		$redirect          = isset( $data['redirect'] ) ? esc_url_raw( (string) $data['redirect'] ) : '';
24
25
		check_admin_referer( "jetpack-authorize_{$role}_{$redirect}" );
26
27
		$tracking = new Tracking();
28
29
		$manager = new Connection_Manager();
30
		$result  = $manager->authorize( $data );
31
32
		if ( is_wp_error( $result ) ) {
33
			Jetpack::state( 'error', $result->get_error_code() );
0 ignored issues
show
The method get_error_code() does not seem to exist on object<WP_Error>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
34
35
			$tracking->record_user_event(
36
				'jpc_client_authorize_fail',
37
				array(
38
					'error_code'    => $result->get_error_code(),
0 ignored issues
show
The method get_error_code() does not seem to exist on object<WP_Error>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
39
					'error_message' => $result->get_error_message(),
0 ignored issues
show
The method get_error_message() does not seem to exist on object<WP_Error>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
40
				)
41
			);
42
		} else {
43
			/**
44
			 * Fires after the Jetpack client is authorized to communicate with WordPress.com.
45
			 *
46
			 * @since 4.2.0
47
			 *
48
			 * @param int Jetpack Blog ID.
49
			 */
50
			do_action( 'jetpack_client_authorized', Jetpack_Options::get_option( 'id' ) );
51
		}
52
53
		if ( wp_validate_redirect( $redirect ) ) {
54
			// Exit happens below in $this->do_exit()
55
			wp_safe_redirect( $redirect );
56
		} else {
57
			// Exit happens below in $this->do_exit()
58
			wp_safe_redirect( Jetpack::admin_url() );
59
		}
60
61
		$tracking->record_user_event( 'jpc_client_authorize_success' );
62
63
		$this->do_exit();
64
	}
65
66
	/*
67
	 * @deprecated 8.0 Use Automattic\Jetpack\Connection\Manager::authorize() instead.
68
	 */
69
	function authorize( $data = array() ) {
70
		_deprecated_function( __METHOD__, 'jetpack-8.0', 'Automattic\\Jetpack\\Connection\\Manager::authorize' );
71
		$manager = new Connection_Manager();
72
		return $manager->authorize( $data );
73
	}
74
75
	public static function deactivate_plugin( $probable_file, $probable_title ) {
76
		include_once ABSPATH . 'wp-admin/includes/plugin.php';
77
		if ( is_plugin_active( $probable_file ) ) {
78
			deactivate_plugins( $probable_file );
79
			return 1;
80
		} else {
81
			// If the plugin is not in the usual place, try looking through all active plugins.
82
			$active_plugins = Jetpack::get_active_plugins();
83
			foreach ( $active_plugins as $plugin ) {
84
				$data = get_plugin_data( WP_PLUGIN_DIR . '/' . $plugin );
85
				if ( $data['Name'] == $probable_title ) {
86
					deactivate_plugins( $plugin );
87
					return 1;
88
				}
89
			}
90
		}
91
92
		return 0;
93
	}
94
95
	/**
96
	 * @return object|WP_Error
97
	 */
98
	function get_token( $data ) {
99
		$roles = new Roles();
100
		$role  = $roles->translate_current_user_to_role();
101
102
		if ( ! $role ) {
103
			return new Jetpack_Error( 'role', __( 'An administrator for this blog must set up the Jetpack connection.', 'jetpack' ) );
104
		}
105
106
		$client_secret = Jetpack_Data::get_access_token();
107
		if ( ! $client_secret ) {
108
			return new Jetpack_Error( 'client_secret', __( 'You need to register your Jetpack before connecting it.', 'jetpack' ) );
109
		}
110
111
		$redirect     = isset( $data['redirect'] ) ? esc_url_raw( (string) $data['redirect'] ) : '';
112
		$redirect_uri = ( 'calypso' === $data['auth_type'] )
113
			? $data['redirect_uri']
114
			: add_query_arg(
115
				array(
116
					'action'   => 'authorize',
117
					'_wpnonce' => wp_create_nonce( "jetpack-authorize_{$role}_{$redirect}" ),
118
					'redirect' => $redirect ? urlencode( $redirect ) : false,
119
				),
120
				menu_page_url( 'jetpack', false )
121
			);
122
123
		// inject identity for analytics
124
		$tracks          = new Automattic\Jetpack\Tracking();
125
		$tracks_identity = $tracks->tracks_get_identity( get_current_user_id() );
126
127
		$body = array(
128
			'client_id'     => Jetpack_Options::get_option( 'id' ),
129
			'client_secret' => $client_secret->secret,
130
			'grant_type'    => 'authorization_code',
131
			'code'          => $data['code'],
132
			'redirect_uri'  => $redirect_uri,
133
			'_ui'           => $tracks_identity['_ui'],
134
			'_ut'           => $tracks_identity['_ut'],
135
		);
136
137
		$args     = array(
138
			'method'  => 'POST',
139
			'body'    => $body,
140
			'headers' => array(
141
				'Accept' => 'application/json',
142
			),
143
		);
144
		$response = Client::_wp_remote_request( Connection_Utils::fix_url_for_bad_hosts( Jetpack::connection()->api_url( 'token' ) ), $args );
145
146
		if ( is_wp_error( $response ) ) {
147
			return new Jetpack_Error( 'token_http_request_failed', $response->get_error_message() );
148
		}
149
150
		$code   = wp_remote_retrieve_response_code( $response );
151
		$entity = wp_remote_retrieve_body( $response );
152
153
		if ( $entity ) {
154
			$json = json_decode( $entity );
155
		} else {
156
			$json = false;
157
		}
158
159
		if ( 200 != $code || ! empty( $json->error ) ) {
160
			if ( empty( $json->error ) ) {
161
				return new Jetpack_Error( 'unknown', '', $code );
162
			}
163
164
			$error_description = isset( $json->error_description ) ? sprintf( __( 'Error Details: %s', 'jetpack' ), (string) $json->error_description ) : '';
165
166
			return new Jetpack_Error( (string) $json->error, $error_description, $code );
167
		}
168
169
		if ( empty( $json->access_token ) || ! is_scalar( $json->access_token ) ) {
170
			return new Jetpack_Error( 'access_token', '', $code );
171
		}
172
173
		if ( empty( $json->token_type ) || 'X_JETPACK' != strtoupper( $json->token_type ) ) {
174
			return new Jetpack_Error( 'token_type', '', $code );
175
		}
176
177
		if ( empty( $json->scope ) ) {
178
			return new Jetpack_Error( 'scope', 'No Scope', $code );
179
		}
180
181
		@list( $role, $hmac ) = explode( ':', $json->scope );
182
		if ( empty( $role ) || empty( $hmac ) ) {
183
			return new Jetpack_Error( 'scope', 'Malformed Scope', $code );
184
		}
185
186
		if ( Jetpack::connection()->sign_role( $role ) !== $json->scope ) {
187
			return new Jetpack_Error( 'scope', 'Invalid Scope', $code );
188
		}
189
190
		$cap = $roles->translate_role_to_cap( $role );
191
		if ( ! $cap ) {
192
			return new Jetpack_Error( 'scope', 'No Cap', $code );
193
		}
194
195
		if ( ! current_user_can( $cap ) ) {
196
			return new Jetpack_Error( 'scope', 'current_user_cannot', $code );
197
		}
198
199
		/**
200
		 * Fires after user has successfully received an auth token.
201
		 *
202
		 * @since 3.9.0
203
		 */
204
		do_action( 'jetpack_user_authorized' );
205
206
		return (string) $json->access_token;
207
	}
208
209
	public function get_jetpack() {
210
		return Jetpack::init();
211
	}
212
213
	public function do_exit() {
214
		exit;
215
	}
216
}
217