/class.jetpack-client-server.php

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