Bez popisu

class-jetpack-xmlrpc-methods.php 8.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. <?php
  2. /**
  3. * Jetpack XMLRPC Methods.
  4. *
  5. * Registers the Jetpack specific XMLRPC methods
  6. *
  7. * @package jetpack
  8. */
  9. use Automattic\Jetpack\Connection\Manager as Connection_Manager;
  10. use Automattic\Jetpack\Connection\Tokens;
  11. /**
  12. * XMLRPC Methods registration and callbacks
  13. */
  14. class Jetpack_XMLRPC_Methods {
  15. /**
  16. * Initialize the main hooks.
  17. */
  18. public static function init() {
  19. add_filter( 'jetpack_xmlrpc_unauthenticated_methods', array( __CLASS__, 'xmlrpc_methods' ) );
  20. add_filter( 'jetpack_xmlrpc_test_connection_response', array( __CLASS__, 'test_connection' ) );
  21. add_filter( 'jetpack_remote_xmlrpc_provision_response', array( __CLASS__, 'remote_provision_response' ), 10, 2 );
  22. add_action( 'jetpack_xmlrpc_server_event', array( __CLASS__, 'jetpack_xmlrpc_server_event' ), 10, 4 );
  23. add_action( 'jetpack_remote_connect_end', array( __CLASS__, 'remote_connect_end' ) );
  24. add_filter( 'jetpack_xmlrpc_remote_register_redirect_uri', array( __CLASS__, 'remote_register_redirect_uri' ) );
  25. }
  26. /**
  27. * Adds Jetpack specific methods to the methods added by the Connection package.
  28. *
  29. * @param array $methods Methods added by the Connection package.
  30. */
  31. public static function xmlrpc_methods( $methods ) {
  32. $methods['jetpack.featuresAvailable'] = array( __CLASS__, 'features_available' );
  33. $methods['jetpack.featuresEnabled'] = array( __CLASS__, 'features_enabled' );
  34. $methods['jetpack.disconnectBlog'] = array( __CLASS__, 'disconnect_blog' );
  35. $methods['jetpack.jsonAPI'] = array( __CLASS__, 'json_api' );
  36. return $methods;
  37. }
  38. /**
  39. * Returns what features are available. Uses the slug of the module files.
  40. *
  41. * @return array
  42. */
  43. public static function features_available() {
  44. $raw_modules = Jetpack::get_available_modules();
  45. $modules = array();
  46. foreach ( $raw_modules as $module ) {
  47. $modules[] = Jetpack::get_module_slug( $module );
  48. }
  49. return $modules;
  50. }
  51. /**
  52. * Returns what features are enabled. Uses the slug of the modules files.
  53. *
  54. * @return array
  55. */
  56. public static function features_enabled() {
  57. $raw_modules = Jetpack::get_active_modules();
  58. $modules = array();
  59. foreach ( $raw_modules as $module ) {
  60. $modules[] = Jetpack::get_module_slug( $module );
  61. }
  62. return $modules;
  63. }
  64. /**
  65. * Filters the result of test_connection XMLRPC method
  66. *
  67. * @return string The current Jetpack version number
  68. */
  69. public static function test_connection() {
  70. return JETPACK__VERSION;
  71. }
  72. /**
  73. * Disconnect this blog from the connected wordpress.com account
  74. *
  75. * @return boolean
  76. */
  77. public static function disconnect_blog() {
  78. /**
  79. * Fired when we want to log an event to the Jetpack event log.
  80. *
  81. * @since 7.7.0
  82. *
  83. * @param string $code Unique name for the event.
  84. * @param string $data Optional data about the event.
  85. */
  86. do_action( 'jetpack_event_log', 'disconnect' );
  87. Jetpack::disconnect();
  88. return true;
  89. }
  90. /**
  91. * Serve a JSON API request.
  92. *
  93. * @param array $args request arguments.
  94. */
  95. public static function json_api( $args = array() ) {
  96. $json_api_args = $args[0];
  97. $verify_api_user_args = $args[1];
  98. $method = (string) $json_api_args[0];
  99. $url = (string) $json_api_args[1];
  100. $post_body = is_null( $json_api_args[2] ) ? null : (string) $json_api_args[2];
  101. $user_details = (array) $json_api_args[4];
  102. $locale = (string) $json_api_args[5];
  103. if ( ! $verify_api_user_args ) {
  104. $user_id = 0;
  105. } elseif ( 'internal' === $verify_api_user_args[0] ) {
  106. $user_id = (int) $verify_api_user_args[1];
  107. if ( $user_id ) {
  108. $user = get_user_by( 'id', $user_id );
  109. if ( ! $user || is_wp_error( $user ) ) {
  110. return false;
  111. }
  112. }
  113. } else {
  114. $user_id = call_user_func( array( new Jetpack_XMLRPC_Server(), 'test_api_user_code' ), $verify_api_user_args );
  115. if ( ! $user_id ) {
  116. return false;
  117. }
  118. }
  119. if ( 'en' !== $locale ) {
  120. // .org mo files are named slightly different from .com, and all we have is this the locale -- try to guess them.
  121. $new_locale = $locale;
  122. if ( strpos( $locale, '-' ) !== false ) {
  123. $locale_pieces = explode( '-', $locale );
  124. $new_locale = $locale_pieces[0];
  125. $new_locale .= ( ! empty( $locale_pieces[1] ) ) ? '_' . strtoupper( $locale_pieces[1] ) : '';
  126. } else {
  127. // .com might pass 'fr' because thats what our language files are named as, where core seems
  128. // to do fr_FR - so try that if we don't think we can load the file.
  129. if ( ! file_exists( WP_LANG_DIR . '/' . $locale . '.mo' ) ) {
  130. $new_locale = $locale . '_' . strtoupper( $locale );
  131. }
  132. }
  133. if ( file_exists( WP_LANG_DIR . '/' . $new_locale . '.mo' ) ) {
  134. unload_textdomain( 'default' );
  135. load_textdomain( 'default', WP_LANG_DIR . '/' . $new_locale . '.mo' );
  136. }
  137. }
  138. $old_user = wp_get_current_user();
  139. wp_set_current_user( $user_id );
  140. if ( $user_id ) {
  141. $token_key = false;
  142. } else {
  143. $verified = ( new Connection_Manager() )->verify_xml_rpc_signature();
  144. $token_key = $verified['token_key'];
  145. }
  146. $token = ( new Tokens() )->get_access_token( $user_id, $token_key );
  147. if ( ! $token || is_wp_error( $token ) ) {
  148. return false;
  149. }
  150. define( 'REST_API_REQUEST', true );
  151. define( 'WPCOM_JSON_API__BASE', 'public-api.wordpress.com/rest/v1' );
  152. // needed?
  153. require_once ABSPATH . 'wp-admin/includes/admin.php';
  154. require_once JETPACK__PLUGIN_DIR . 'class.json-api.php';
  155. $api = WPCOM_JSON_API::init( $method, $url, $post_body );
  156. $api->token_details['user'] = $user_details;
  157. require_once JETPACK__PLUGIN_DIR . 'class.json-api-endpoints.php';
  158. $display_errors = ini_set( 'display_errors', 0 ); // phpcs:ignore WordPress.PHP.IniSet
  159. ob_start();
  160. $api->serve( false );
  161. $output = ob_get_clean();
  162. ini_set( 'display_errors', $display_errors ); // phpcs:ignore WordPress.PHP.IniSet
  163. $nonce = wp_generate_password( 10, false );
  164. $hmac = hash_hmac( 'md5', $nonce . $output, $token->secret );
  165. wp_set_current_user( isset( $old_user->ID ) ? $old_user->ID : 0 );
  166. return array(
  167. (string) $output,
  168. (string) $nonce,
  169. (string) $hmac,
  170. );
  171. }
  172. /**
  173. * Filters the response of the remote_provision XMLRPC method
  174. *
  175. * @param array $response The response.
  176. * @param array $request An array containing at minimum a nonce key and a local_username key.
  177. *
  178. * @since 9.8.0
  179. * @return array
  180. */
  181. public static function remote_provision_response( $response, $request ) {
  182. if ( ! empty( $request['onboarding'] ) ) {
  183. Jetpack::create_onboarding_token();
  184. $response['onboarding_token'] = Jetpack_Options::get_option( 'onboarding' );
  185. }
  186. return $response;
  187. }
  188. /**
  189. * Runs Jetpack specific action in xmlrpc server events
  190. *
  191. * @param String $action the action name, i.e., 'remote_authorize'.
  192. * @param String $stage the execution stage, can be 'begin', 'success', 'error', etc.
  193. * @param array $parameters extra parameters from the event.
  194. * @param WP_User $user the acting user.
  195. * @return void
  196. */
  197. public static function jetpack_xmlrpc_server_event( $action, $stage, $parameters = array(), $user = null ) { //phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable
  198. if ( 'remote_register' === $action && 'begin' === $stage ) {
  199. Jetpack::maybe_set_version_option();
  200. }
  201. }
  202. /**
  203. * Hooks into the remote_connect XMLRPC endpoint and triggers Jetpack::handle_post_authorization_actions
  204. *
  205. * @since 9.8.0
  206. * @return void
  207. */
  208. public static function remote_connect_end() {
  209. /** This filter is documented in class.jetpack-cli.php */
  210. $enable_sso = apply_filters( 'jetpack_start_enable_sso', true );
  211. Jetpack::handle_post_authorization_actions( $enable_sso, false, false );
  212. }
  213. /**
  214. * Filters the Redirect URI returned by the remote_register XMLRPC method
  215. *
  216. * @since 9.8.0
  217. *
  218. * @param string $redirect_uri The Redirect URI.
  219. * @return string
  220. */
  221. public static function remote_register_redirect_uri( $redirect_uri ) {
  222. $auto_enable_sso = ( ! ( new Connection_Manager() )->has_connected_owner() || Jetpack::is_module_active( 'sso' ) );
  223. /** This filter is documented in class.jetpack-cli.php */
  224. if ( apply_filters( 'jetpack_start_enable_sso', $auto_enable_sso ) ) {
  225. $redirect_uri = add_query_arg(
  226. array(
  227. 'action' => 'jetpack-sso',
  228. 'redirect_to' => rawurlencode( admin_url() ),
  229. ),
  230. wp_login_url() // TODO: come back to Jetpack dashboard?
  231. );
  232. }
  233. return $redirect_uri;
  234. }
  235. }