PageRenderTime 25ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-content/plugins/jetpack/class.jetpack-client-server.php

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