class.jetpack-client-server.php 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. <?php
  2. /**
  3. * Client = Plugin
  4. * Client Server = API Methods the Plugin must respond to
  5. */
  6. class Jetpack_Client_Server {
  7. /**
  8. * Authorizations
  9. */
  10. function client_authorize() {
  11. $data = stripslashes_deep( $_GET );
  12. $data['auth_type'] = 'client';
  13. $role = Jetpack::translate_current_user_to_role();
  14. $redirect = isset( $data['redirect'] ) ? esc_url_raw( (string) $data['redirect'] ) : '';
  15. check_admin_referer( "jetpack-authorize_{$role}_{$redirect}" );
  16. $result = $this->authorize( $data );
  17. if ( is_wp_error( $result ) ) {
  18. Jetpack::state( 'error', $result->get_error_code() );
  19. JetpackTracking::record_user_event( 'jpc_client_authorize_fail', array(
  20. 'error_code' => $result->get_error_code(),
  21. 'error_message' => $result->get_error_message()
  22. ) );
  23. } else {
  24. /**
  25. * Fires after the Jetpack client is authorized to communicate with WordPress.com.
  26. *
  27. * @since 4.2.0
  28. *
  29. * @param int Jetpack Blog ID.
  30. */
  31. do_action( 'jetpack_client_authorized', Jetpack_Options::get_option( 'id' ) );
  32. }
  33. if ( wp_validate_redirect( $redirect ) ) {
  34. // Exit happens below in $this->do_exit()
  35. wp_safe_redirect( $redirect );
  36. } else {
  37. // Exit happens below in $this->do_exit()
  38. wp_safe_redirect( Jetpack::admin_url() );
  39. }
  40. JetpackTracking::record_user_event( 'jpc_client_authorize_success' );
  41. $this->do_exit();
  42. }
  43. function authorize( $data = array() ) {
  44. $redirect = isset( $data['redirect'] ) ? esc_url_raw( (string) $data['redirect'] ) : '';
  45. $jetpack_unique_connection = Jetpack_Options::get_option( 'unique_connection' );
  46. // Checking if site has been active/connected previously before recording unique connection
  47. if ( ! $jetpack_unique_connection ) {
  48. // jetpack_unique_connection option has never been set
  49. $jetpack_unique_connection = array(
  50. 'connected' => 0,
  51. 'disconnected' => 0,
  52. 'version' => '3.6.1',
  53. );
  54. update_option( 'jetpack_unique_connection', $jetpack_unique_connection );
  55. //track unique connection
  56. $jetpack = $this->get_jetpack();
  57. $jetpack->stat( 'connections', 'unique-connection' );
  58. $jetpack->do_stats( 'server_side' );
  59. }
  60. // increment number of times connected
  61. $jetpack_unique_connection['connected'] += 1;
  62. Jetpack_Options::update_option( 'unique_connection', $jetpack_unique_connection );
  63. $role = Jetpack::translate_current_user_to_role();
  64. if ( ! $role ) {
  65. return new Jetpack_Error( 'no_role', 'Invalid request.', 400 );
  66. }
  67. $cap = Jetpack::translate_role_to_cap( $role );
  68. if ( ! $cap ) {
  69. return new Jetpack_Error( 'no_cap', 'Invalid request.', 400 );
  70. }
  71. if ( ! empty( $data['error'] ) ) {
  72. return new Jetpack_Error( $data['error'], 'Error included in the request.', 400 );
  73. }
  74. if ( ! isset( $data['state'] ) ) {
  75. return new Jetpack_Error( 'no_state', 'Request must include state.', 400 );
  76. }
  77. if ( ! ctype_digit( $data['state'] ) ) {
  78. return new Jetpack_Error( $data['error'], 'State must be an integer.', 400 );
  79. }
  80. $current_user_id = get_current_user_id();
  81. if ( $current_user_id != $data['state'] ) {
  82. return new Jetpack_Error( 'wrong_state', 'State does not match current user.', 400 );
  83. }
  84. if ( empty( $data['code'] ) ) {
  85. return new Jetpack_Error( 'no_code', 'Request must include an authorization code.', 400 );
  86. }
  87. $token = $this->get_token( $data );
  88. if ( is_wp_error( $token ) ) {
  89. $code = $token->get_error_code();
  90. if ( empty( $code ) ) {
  91. $code = 'invalid_token';
  92. }
  93. return new Jetpack_Error( $code, $token->get_error_message(), 400 );
  94. }
  95. if ( ! $token ) {
  96. return new Jetpack_Error( 'no_token', 'Error generating token.', 400 );
  97. }
  98. $is_master_user = ! Jetpack::is_active();
  99. Jetpack::update_user_token( $current_user_id, sprintf( '%s.%d', $token, $current_user_id ), $is_master_user );
  100. if ( ! $is_master_user ) {
  101. Jetpack::state( 'message', 'linked' );
  102. // Don't activate anything since we are just connecting a user.
  103. return 'linked';
  104. }
  105. // If this site has been through the Jetpack Onboarding flow, delete the onboarding token
  106. Jetpack::invalidate_onboarding_token();
  107. // If redirect_uri is SSO, ensure SSO module is enabled
  108. parse_str( parse_url( $data['redirect_uri'], PHP_URL_QUERY ), $redirect_options );
  109. /** This filter is documented in class.jetpack-cli.php */
  110. $jetpack_start_enable_sso = apply_filters( 'jetpack_start_enable_sso', true );
  111. $activate_sso = (
  112. isset( $redirect_options['action'] ) &&
  113. 'jetpack-sso' === $redirect_options['action'] &&
  114. $jetpack_start_enable_sso
  115. );
  116. $do_redirect_on_error = ( 'client' === $data['auth_type'] );
  117. Jetpack::handle_post_authorization_actions( $activate_sso, $do_redirect_on_error );
  118. return 'authorized';
  119. }
  120. public static function deactivate_plugin( $probable_file, $probable_title ) {
  121. include_once( ABSPATH . 'wp-admin/includes/plugin.php' );
  122. if ( is_plugin_active( $probable_file ) ) {
  123. deactivate_plugins( $probable_file );
  124. return 1;
  125. } else {
  126. // If the plugin is not in the usual place, try looking through all active plugins.
  127. $active_plugins = Jetpack::get_active_plugins();
  128. foreach ( $active_plugins as $plugin ) {
  129. $data = get_plugin_data( WP_PLUGIN_DIR . '/' . $plugin );
  130. if ( $data['Name'] == $probable_title ) {
  131. deactivate_plugins( $plugin );
  132. return 1;
  133. }
  134. }
  135. }
  136. return 0;
  137. }
  138. /**
  139. * @return object|WP_Error
  140. */
  141. function get_token( $data ) {
  142. $role = Jetpack::translate_current_user_to_role();
  143. if ( ! $role ) {
  144. return new Jetpack_Error( 'role', __( 'An administrator for this blog must set up the Jetpack connection.', 'jetpack' ) );
  145. }
  146. $client_secret = Jetpack_Data::get_access_token();
  147. if ( ! $client_secret ) {
  148. return new Jetpack_Error( 'client_secret', __( 'You need to register your Jetpack before connecting it.', 'jetpack' ) );
  149. }
  150. $redirect = isset( $data['redirect'] ) ? esc_url_raw( (string) $data['redirect'] ) : '';
  151. $redirect_uri = ( 'calypso' === $data['auth_type'] )
  152. ? $data['redirect_uri']
  153. : add_query_arg( array(
  154. 'action' => 'authorize',
  155. '_wpnonce' => wp_create_nonce( "jetpack-authorize_{$role}_{$redirect}" ),
  156. 'redirect' => $redirect ? urlencode( $redirect ) : false,
  157. ), menu_page_url( 'jetpack', false ) );
  158. // inject identity for analytics
  159. $tracks_identity = jetpack_tracks_get_identity( get_current_user_id() );
  160. $body = array(
  161. 'client_id' => Jetpack_Options::get_option( 'id' ),
  162. 'client_secret' => $client_secret->secret,
  163. 'grant_type' => 'authorization_code',
  164. 'code' => $data['code'],
  165. 'redirect_uri' => $redirect_uri,
  166. '_ui' => $tracks_identity['_ui'],
  167. '_ut' => $tracks_identity['_ut'],
  168. );
  169. $args = array(
  170. 'method' => 'POST',
  171. 'body' => $body,
  172. 'headers' => array(
  173. 'Accept' => 'application/json',
  174. ),
  175. );
  176. $response = Jetpack_Client::_wp_remote_request( Jetpack::fix_url_for_bad_hosts( Jetpack::api_url( 'token' ) ), $args );
  177. if ( is_wp_error( $response ) ) {
  178. return new Jetpack_Error( 'token_http_request_failed', $response->get_error_message() );
  179. }
  180. $code = wp_remote_retrieve_response_code( $response );
  181. $entity = wp_remote_retrieve_body( $response );
  182. if ( $entity ) {
  183. $json = json_decode( $entity );
  184. } else {
  185. $json = false;
  186. }
  187. if ( 200 != $code || ! empty( $json->error ) ) {
  188. if ( empty( $json->error ) ) {
  189. return new Jetpack_Error( 'unknown', '', $code );
  190. }
  191. $error_description = isset( $json->error_description ) ? sprintf( __( 'Error Details: %s', 'jetpack' ), (string) $json->error_description ) : '';
  192. return new Jetpack_Error( (string) $json->error, $error_description, $code );
  193. }
  194. if ( empty( $json->access_token ) || ! is_scalar( $json->access_token ) ) {
  195. return new Jetpack_Error( 'access_token', '', $code );
  196. }
  197. if ( empty( $json->token_type ) || 'X_JETPACK' != strtoupper( $json->token_type ) ) {
  198. return new Jetpack_Error( 'token_type', '', $code );
  199. }
  200. if ( empty( $json->scope ) ) {
  201. return new Jetpack_Error( 'scope', 'No Scope', $code );
  202. }
  203. @list( $role, $hmac ) = explode( ':', $json->scope );
  204. if ( empty( $role ) || empty( $hmac ) ) {
  205. return new Jetpack_Error( 'scope', 'Malformed Scope', $code );
  206. }
  207. if ( Jetpack::sign_role( $role ) !== $json->scope ) {
  208. return new Jetpack_Error( 'scope', 'Invalid Scope', $code );
  209. }
  210. if ( ! $cap = Jetpack::translate_role_to_cap( $role ) ) {
  211. return new Jetpack_Error( 'scope', 'No Cap', $code );
  212. }
  213. if ( ! current_user_can( $cap ) ) {
  214. return new Jetpack_Error( 'scope', 'current_user_cannot', $code );
  215. }
  216. /**
  217. * Fires after user has successfully received an auth token.
  218. *
  219. * @since 3.9.0
  220. */
  221. do_action( 'jetpack_user_authorized' );
  222. return (string) $json->access_token;
  223. }
  224. public function get_jetpack() {
  225. return Jetpack::init();
  226. }
  227. public function do_exit() {
  228. exit;
  229. }
  230. }