Skip to content

Commit de47988

Browse files
Jetpack: Big, structural change. Splitting off all classes into their own include files. This should start helping us to be a bit more modular and let us only include the code and classes that we actually need.
1 parent 2767bbf commit de47988

7 files changed

+4748
-4737
lines changed

class.jetpack-client-server.php

+219
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,219 @@
1+
<?php
2+
3+
/**
4+
* Client = Plugin
5+
* Client Server = API Methods the Plugin must respond to
6+
*
7+
* @todo Roll this into Jetpack? There's only one 'public' method now: ::authorize().
8+
*/
9+
class Jetpack_Client_Server {
10+
function authorize() {
11+
$data = stripslashes_deep( $_GET );
12+
13+
$args = array();
14+
15+
$redirect = isset( $data['redirect'] ) ? esc_url_raw( (string) $data['redirect'] ) : '';
16+
17+
do {
18+
$jetpack = Jetpack::init();
19+
$role = $jetpack->translate_current_user_to_role();
20+
if ( !$role ) {
21+
Jetpack::state( 'error', 'no_role' );
22+
break;
23+
}
24+
25+
$cap = $jetpack->translate_role_to_cap( $role );
26+
if ( !$cap ) {
27+
Jetpack::state( 'error', 'no_cap' );
28+
break;
29+
}
30+
31+
check_admin_referer( "jetpack-authorize_{$role}_{$redirect}" );
32+
33+
if ( !empty( $data['error'] ) ) {
34+
Jetpack::state( 'error', $data['error'] );
35+
break;
36+
}
37+
38+
if ( empty( $data['state'] ) ) {
39+
Jetpack::state( 'error', 'no_state' );
40+
break;
41+
}
42+
43+
if ( !ctype_digit( $data['state'] ) ) {
44+
Jetpack::state( 'error', 'invalid_state' );
45+
break;
46+
}
47+
48+
$current_user_id = get_current_user_id();
49+
if ( $current_user_id != $data['state'] ) {
50+
Jetpack::state( 'error', 'wrong_state' );
51+
break;
52+
}
53+
54+
if ( empty( $data['code'] ) ) {
55+
Jetpack::state( 'error', 'no_code' );
56+
break;
57+
}
58+
59+
$token = $this->get_token( $data );
60+
61+
if ( is_wp_error( $token ) ) {
62+
if ( $error = $token->get_error_code() )
63+
Jetpack::state( 'error', $error );
64+
else
65+
Jetpack::state( 'error', 'invalid_token' );
66+
67+
Jetpack::state( 'error_description', $token->get_error_message() );
68+
69+
break;
70+
}
71+
72+
if ( !$token ) {
73+
Jetpack::state( 'error', 'no_token' );
74+
break;
75+
}
76+
77+
$is_master_user = ! Jetpack::is_active();
78+
79+
Jetpack::update_user_token( $current_user_id, sprintf( '%s.%d', $token, $current_user_id ), $is_master_user );
80+
81+
82+
if ( $is_master_user ) {
83+
Jetpack::state( 'message', 'authorized' );
84+
} else {
85+
Jetpack::state( 'message', 'linked' );
86+
// Don't activate anything since we are just connecting a user.
87+
break;
88+
}
89+
90+
if ( $active_modules = Jetpack::get_option( 'active_modules' ) ) {
91+
Jetpack::delete_option( 'active_modules' );
92+
93+
Jetpack::activate_default_modules( 999, 1, $active_modules );
94+
} else {
95+
Jetpack::activate_default_modules();
96+
}
97+
98+
$jetpack->sync->register( 'noop' ); // Spawn a sync to make sure the Jetpack Servers know what modules are active.
99+
100+
// Start nonce cleaner
101+
wp_clear_scheduled_hook( 'jetpack_clean_nonces' );
102+
wp_schedule_event( time(), 'hourly', 'jetpack_clean_nonces' );
103+
} while ( false );
104+
105+
if ( wp_validate_redirect( $redirect ) ) {
106+
wp_safe_redirect( $redirect );
107+
} else {
108+
wp_safe_redirect( Jetpack::admin_url() );
109+
}
110+
111+
exit;
112+
}
113+
114+
public static function deactivate_plugin( $probable_file, $probable_title ) {
115+
if ( is_plugin_active( $probable_file ) ) {
116+
deactivate_plugins( $probable_file );
117+
return 1;
118+
} else {
119+
// If the plugin is not in the usual place, try looking through all active plugins.
120+
$active_plugins = get_option( 'active_plugins', array() );
121+
foreach ( $active_plugins as $plugin ) {
122+
$data = get_plugin_data( WP_PLUGIN_DIR . '/' . $plugin );
123+
if ( $data['Name'] == $probable_title ) {
124+
deactivate_plugins( $plugin );
125+
return 1;
126+
}
127+
}
128+
}
129+
130+
return 0;
131+
}
132+
133+
/**
134+
* @return object|WP_Error
135+
*/
136+
function get_token( $data ) {
137+
$jetpack = Jetpack::init();
138+
$role = $jetpack->translate_current_user_to_role();
139+
140+
if ( !$role ) {
141+
return new Jetpack_Error( 'role', __( 'An administrator for this blog must set up the Jetpack connection.', 'jetpack' ) );
142+
}
143+
144+
$client_secret = Jetpack_Data::get_access_token();
145+
if ( !$client_secret ) {
146+
return new Jetpack_Error( 'client_secret', __( 'You need to register your Jetpack before connecting it.', 'jetpack' ) );
147+
}
148+
149+
$redirect = isset( $data['redirect'] ) ? esc_url_raw( (string) $data['redirect'] ) : '';
150+
151+
$body = array(
152+
'client_id' => Jetpack::get_option( 'id' ),
153+
'client_secret' => $client_secret->secret,
154+
'grant_type' => 'authorization_code',
155+
'code' => $data['code'],
156+
'redirect_uri' => add_query_arg( array(
157+
'action' => 'authorize',
158+
'_wpnonce' => wp_create_nonce( "jetpack-authorize_{$role}_{$redirect}" ),
159+
'redirect' => $redirect ? urlencode( $redirect ) : false,
160+
), menu_page_url( 'jetpack', false ) ),
161+
);
162+
163+
$args = array(
164+
'method' => 'POST',
165+
'body' => $body,
166+
'headers' => array(
167+
'Accept' => 'application/json',
168+
),
169+
);
170+
$response = Jetpack_Client::_wp_remote_request( Jetpack::fix_url_for_bad_hosts( Jetpack::api_url( 'token' ), $args ), $args );
171+
172+
if ( is_wp_error( $response ) ) {
173+
return new Jetpack_Error( 'token_http_request_failed', $response->get_error_message() );
174+
}
175+
176+
$code = wp_remote_retrieve_response_code( $response );
177+
$entity = wp_remote_retrieve_body( $response );
178+
179+
if ( $entity )
180+
$json = json_decode( $entity );
181+
else
182+
$json = false;
183+
184+
if ( 200 != $code || !empty( $json->error ) ) {
185+
if ( empty( $json->error ) )
186+
return new Jetpack_Error( 'unknown', '', $code );
187+
188+
$error_description = isset( $json->error_description ) ? sprintf( __( 'Error Details: %s', 'jetpack' ), (string) $json->error_description ) : '';
189+
190+
return new Jetpack_Error( (string) $json->error, $error_description, $code );
191+
}
192+
193+
if ( empty( $json->access_token ) || !is_scalar( $json->access_token ) ) {
194+
return new Jetpack_Error( 'access_token', '', $code );
195+
}
196+
197+
if ( empty( $json->token_type ) || 'X_JETPACK' != strtoupper( $json->token_type ) ) {
198+
return new Jetpack_Error( 'token_type', '', $code );
199+
}
200+
201+
if ( empty( $json->scope ) ) {
202+
return new Jetpack_Error( 'scope', 'No Scope', $code );
203+
}
204+
@list( $role, $hmac ) = explode( ':', $json->scope );
205+
if ( empty( $role ) || empty( $hmac ) ) {
206+
return new Jetpack_Error( 'scope', 'Malformed Scope', $code );
207+
}
208+
if ( $jetpack->sign_role( $role ) !== $json->scope ) {
209+
return new Jetpack_Error( 'scope', 'Invalid Scope', $code );
210+
}
211+
212+
if ( !$cap = $jetpack->translate_role_to_cap( $role ) )
213+
return new Jetpack_Error( 'scope', 'No Cap', $code );
214+
if ( !current_user_can( $cap ) )
215+
return new Jetpack_Error( 'scope', 'current_user_cannot', $code );
216+
217+
return (string) $json->access_token;
218+
}
219+
}

0 commit comments

Comments
 (0)