PageRenderTime 49ms CodeModel.GetById 11ms RepoModel.GetById 1ms app.codeStats 0ms

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

https://gitlab.com/Gashler/sg
PHP | 414 lines | 264 code | 68 blank | 82 comment | 48 complexity | c9de2a560f2f0733b1b1e55020c1c975 MD5 | raw file
  1. <?php
  2. /**
  3. * Just a sack of functions. Not actually an IXR_Server
  4. */
  5. class Jetpack_XMLRPC_Server {
  6. /**
  7. * The current error object
  8. */
  9. var $error = null;
  10. /**
  11. * Whitelist of the XML-RPC methods available to the Jetpack Server. If the
  12. * user is not authenticated (->login()) then the methods are never added,
  13. * so they will get a "does not exist" error.
  14. */
  15. function xmlrpc_methods( $core_methods ) {
  16. $jetpack_methods = array(
  17. 'jetpack.jsonAPI' => array( $this, 'json_api' ),
  18. 'jetpack.verifyAction' => array( $this, 'verify_action' ),
  19. );
  20. $user = $this->login();
  21. if ( $user ) {
  22. $jetpack_methods = array_merge( $jetpack_methods, array(
  23. 'jetpack.testConnection' => array( $this, 'test_connection' ),
  24. 'jetpack.testAPIUserCode' => array( $this, 'test_api_user_code' ),
  25. 'jetpack.featuresAvailable' => array( $this, 'features_available' ),
  26. 'jetpack.featuresEnabled' => array( $this, 'features_enabled' ),
  27. 'jetpack.getPost' => array( $this, 'get_post' ),
  28. 'jetpack.getPosts' => array( $this, 'get_posts' ),
  29. 'jetpack.getComment' => array( $this, 'get_comment' ),
  30. 'jetpack.getComments' => array( $this, 'get_comments' ),
  31. 'jetpack.disconnectBlog' => array( $this, 'disconnect_blog' ),
  32. 'jetpack.unlinkUser' => array( $this, 'unlink_user' ),
  33. ) );
  34. if ( isset( $core_methods['metaWeblog.editPost'] ) ) {
  35. $jetpack_methods['metaWeblog.newMediaObject'] = $core_methods['metaWeblog.newMediaObject'];
  36. $jetpack_methods['jetpack.updateAttachmentParent'] = array( $this, 'update_attachment_parent' );
  37. }
  38. $jetpack_methods = apply_filters( 'jetpack_xmlrpc_methods', $jetpack_methods, $core_methods, $user );
  39. }
  40. return apply_filters( 'jetpack_xmlrpc_unauthenticated_methods', $jetpack_methods, $core_methods );
  41. }
  42. /**
  43. * Whitelist of the bootstrap XML-RPC methods
  44. */
  45. function bootstrap_xmlrpc_methods() {
  46. return array(
  47. 'jetpack.verifyRegistration' => array( $this, 'verify_registration' ),
  48. );
  49. }
  50. /**
  51. * Verifies that Jetpack.WordPress.com received a registration request from this site
  52. */
  53. function verify_registration( $verify_secret ) {
  54. return $this->verify_action( array( 'register', $verify_secret ) );
  55. }
  56. /**
  57. * @return WP_Error|string secret_2 on success, WP_Error( error_code => error_code, error_message => error description, error_data => status code ) on failure
  58. *
  59. * Possible error_codes:
  60. *
  61. * verify_secret_1_missing
  62. * verify_secret_1_malformed
  63. * verify_secrets_missing: No longer have verification secrets stored
  64. * verify_secrets_mismatch: stored secret_1 does not match secret_1 sent by Jetpack.WordPress.com
  65. */
  66. function verify_action( $params ) {
  67. $action = $params[0];
  68. $verify_secret = $params[1];
  69. if ( empty( $verify_secret ) ) {
  70. return $this->error( new Jetpack_Error( 'verify_secret_1_missing', sprintf( 'The required "%s" parameter is missing.', 'secret_1' ), 400 ) );
  71. } else if ( !is_string( $verify_secret ) ) {
  72. return $this->error( new Jetpack_Error( 'verify_secret_1_malformed', sprintf( 'The required "%s" parameter is malformed.', 'secret_1' ), 400 ) );
  73. }
  74. $secrets = Jetpack_Options::get_option( $action );
  75. if ( !$secrets || is_wp_error( $secrets ) ) {
  76. Jetpack_Options::delete_option( $action );
  77. return $this->error( new Jetpack_Error( 'verify_secrets_missing', 'Verification took too long', 400 ) );
  78. }
  79. @list( $secret_1, $secret_2, $secret_eol ) = explode( ':', $secrets );
  80. if ( empty( $secret_1 ) || empty( $secret_2 ) || empty( $secret_eol ) || $secret_eol < time() ) {
  81. Jetpack_Options::delete_option( $action );
  82. return $this->error( new Jetpack_Error( 'verify_secrets_missing', 'Verification took too long', 400 ) );
  83. }
  84. if ( $verify_secret !== $secret_1 ) {
  85. Jetpack_Options::delete_option( $action );
  86. return $this->error( new Jetpack_Error( 'verify_secrets_mismatch', 'Secret mismatch', 400 ) );
  87. }
  88. Jetpack_Options::delete_option( $action );
  89. return $secret_2;
  90. }
  91. /**
  92. * Wrapper for wp_authenticate( $username, $password );
  93. *
  94. * @return WP_User|IXR_Error
  95. */
  96. function login() {
  97. Jetpack::init()->require_jetpack_authentication();
  98. $user = wp_authenticate( 'username', 'password' );
  99. if ( is_wp_error( $user ) ) {
  100. if ( 'authentication_failed' == $user->get_error_code() ) { // Generic error could mean most anything.
  101. $this->error = new Jetpack_Error( 'invalid_request', 'Invalid Request', 403 );
  102. } else {
  103. $this->error = $user;
  104. }
  105. return false;
  106. } else if ( !$user ) { // Shouldn't happen.
  107. $this->error = new Jetpack_Error( 'invalid_request', 'Invalid Request', 403 );
  108. return false;
  109. }
  110. return $user;
  111. }
  112. /**
  113. * Returns the current error as an IXR_Error
  114. *
  115. * @return null|IXR_Error
  116. */
  117. function error( $error = null ) {
  118. if ( !is_null( $error ) ) {
  119. $this->error = $error;
  120. }
  121. if ( is_wp_error( $this->error ) ) {
  122. $code = $this->error->get_error_data();
  123. if ( !$code ) {
  124. $code = -10520;
  125. }
  126. $message = sprintf( 'Jetpack: [%s] %s', $this->error->get_error_code(), $this->error->get_error_message() );
  127. return new IXR_Error( $code, $message );
  128. } else if ( is_a( $this->error, 'IXR_Error' ) ) {
  129. return $this->error;
  130. }
  131. return false;
  132. }
  133. /* API Methods */
  134. /**
  135. * Just authenticates with the given Jetpack credentials.
  136. *
  137. * @return bool|IXR_Error
  138. */
  139. function test_connection() {
  140. return JETPACK__VERSION;
  141. }
  142. function test_api_user_code( $args ) {
  143. $client_id = (int) $args[0];
  144. $user_id = (int) $args[1];
  145. $nonce = (string) $args[2];
  146. $verify = (string) $args[3];
  147. if ( !$client_id || !$user_id || !strlen( $nonce ) || 32 !== strlen( $verify ) ) {
  148. return false;
  149. }
  150. $user = get_user_by( 'id', $user_id );
  151. if ( !$user || is_wp_error( $user ) ) {
  152. return false;
  153. }
  154. /* debugging
  155. error_log( "CLIENT: $client_id" );
  156. error_log( "USER: $user_id" );
  157. error_log( "NONCE: $nonce" );
  158. error_log( "VERIFY: $verify" );
  159. */
  160. $jetpack_token = Jetpack_Data::get_access_token( JETPACK_MASTER_USER );
  161. $api_user_code = get_user_meta( $user_id, "jetpack_json_api_$client_id", true );
  162. if ( !$api_user_code ) {
  163. return false;
  164. }
  165. $hmac = hash_hmac( 'md5', json_encode( (object) array(
  166. 'client_id' => (int) $client_id,
  167. 'user_id' => (int) $user_id,
  168. 'nonce' => (string) $nonce,
  169. 'code' => (string) $api_user_code,
  170. ) ), $jetpack_token->secret );
  171. if ( $hmac !== $verify ) {
  172. return false;
  173. }
  174. return $user_id;
  175. }
  176. /**
  177. * Disconnect this blog from the connected wordpress.com account
  178. * @return boolean
  179. */
  180. function disconnect_blog() {
  181. Jetpack::log( 'disconnect' );
  182. Jetpack::disconnect();
  183. return true;
  184. }
  185. /**
  186. * Unlink a user from WordPress.com
  187. *
  188. * This will fail if called by the Master User.
  189. */
  190. function unlink_user() {
  191. Jetpack::log( 'unlink' );
  192. return Jetpack::unlink_user();
  193. }
  194. /**
  195. * Returns what features are available. Uses the slug of the module files.
  196. *
  197. * @return array|IXR_Error
  198. */
  199. function features_available() {
  200. $raw_modules = Jetpack::get_available_modules();
  201. $modules = array();
  202. foreach ( $raw_modules as $module ) {
  203. $modules[] = Jetpack::get_module_slug( $module );
  204. }
  205. return $modules;
  206. }
  207. /**
  208. * Returns what features are enabled. Uses the slug of the modules files.
  209. *
  210. * @return array|IXR_Error
  211. */
  212. function features_enabled() {
  213. $raw_modules = Jetpack::get_active_modules();
  214. $modules = array();
  215. foreach ( $raw_modules as $module ) {
  216. $modules[] = Jetpack::get_module_slug( $module );
  217. }
  218. return $modules;
  219. }
  220. function get_post( $id ) {
  221. if ( !$id = (int) $id ) {
  222. return false;
  223. }
  224. $jetpack = Jetpack::init();
  225. $post = $jetpack->sync->get_post( $id );
  226. return $post;
  227. }
  228. function get_posts( $args ) {
  229. list( $post_ids ) = $args;
  230. $post_ids = array_map( 'intval', (array) $post_ids );
  231. $jp = Jetpack::init();
  232. $sync_data = $jp->sync->get_content( array( 'posts' => $post_ids ) );
  233. return $sync_data;
  234. }
  235. function get_comment( $id ) {
  236. if ( !$id = (int) $id ) {
  237. return false;
  238. }
  239. $jetpack = Jetpack::init();
  240. $comment = $jetpack->sync->get_comment( $id );
  241. if ( !is_array( $comment ) )
  242. return false;
  243. $post = $jetpack->sync->get_post( $comment['comment_post_ID'] );
  244. if ( !$post ) {
  245. return false;
  246. }
  247. return $comment;
  248. }
  249. function get_comments( $args ) {
  250. list( $comment_ids ) = $args;
  251. $comment_ids = array_map( 'intval', (array) $comment_ids );
  252. $jp = Jetpack::init();
  253. $sync_data = $jp->sync->get_content( array( 'comments' => $comment_ids ) );
  254. return $sync_data;
  255. }
  256. function update_attachment_parent( $args ) {
  257. $attachment_id = (int) $args[0];
  258. $parent_id = (int) $args[1];
  259. return wp_update_post( array(
  260. 'ID' => $attachment_id,
  261. 'post_parent' => $parent_id,
  262. ) );
  263. }
  264. function json_api( $args = array() ) {
  265. $json_api_args = $args[0];
  266. $verify_api_user_args = $args[1];
  267. $method = (string) $json_api_args[0];
  268. $url = (string) $json_api_args[1];
  269. $post_body = is_null( $json_api_args[2] ) ? null : (string) $json_api_args[2];
  270. $my_id = (int) $json_api_args[3];
  271. $user_details = (array) $json_api_args[4];
  272. $locale = (string) $json_api_args[5];
  273. if ( !$verify_api_user_args ) {
  274. $user_id = 0;
  275. } elseif ( 'internal' === $verify_api_user_args[0] ) {
  276. $user_id = (int) $verify_api_user_args[1];
  277. if ( $user_id ) {
  278. $user = get_user_by( 'id', $user_id );
  279. if ( !$user || is_wp_error( $user ) ) {
  280. return false;
  281. }
  282. }
  283. } else {
  284. $user_id = call_user_func( array( $this, 'test_api_user_code' ), $verify_api_user_args );
  285. if ( !$user_id ) {
  286. return false;
  287. }
  288. }
  289. /* debugging
  290. error_log( "-- begin json api via jetpack debugging -- " );
  291. error_log( "METHOD: $method" );
  292. error_log( "URL: $url" );
  293. error_log( "POST BODY: $post_body" );
  294. error_log( "MY JETPACK ID: $my_id" );
  295. error_log( "VERIFY_ARGS: " . print_r( $verify_api_user_args, 1 ) );
  296. error_log( "VERIFIED USER_ID: " . (int) $user_id );
  297. error_log( "-- end json api via jetpack debugging -- " );
  298. */
  299. if ( 'en' !== $locale ) {
  300. // .org mo files are named slightly different from .com, and all we have is this the locale -- try to guess them.
  301. $new_locale = $locale;
  302. if ( strpos( $locale, '-' ) !== false ) {
  303. $pieces = explode( '-', $locale );
  304. $new_locale = $locale_pieces[0];
  305. $new_locale .= ( ! empty( $locale_pieces[1] ) ) ? '_' . strtoupper( $locale_pieces[1] ) : '';
  306. } else {
  307. // .com might pass 'fr' because thats what our language files are named as, where core seems
  308. // to do fr_FR - so try that if we don't think we can load the file.
  309. if ( ! file_exists( WP_LANG_DIR . '/' . $locale . '.mo' ) ) {
  310. $new_locale = $locale . '_' . strtoupper( $locale );
  311. }
  312. }
  313. if ( file_exists( WP_LANG_DIR . '/' . $new_locale . '.mo' ) ) {
  314. unload_textdomain( 'default' );
  315. load_textdomain( 'default', WP_LANG_DIR . '/' . $new_locale . '.mo' );
  316. }
  317. }
  318. $old_user = wp_get_current_user();
  319. wp_set_current_user( $user_id );
  320. $token = Jetpack_Data::get_access_token( get_current_user_id() );
  321. if ( !$token || is_wp_error( $token ) ) {
  322. return false;
  323. }
  324. define( 'REST_API_REQUEST', true );
  325. define( 'WPCOM_JSON_API__BASE', 'public-api.wordpress.com/rest/v1' );
  326. // needed?
  327. require_once ABSPATH . 'wp-admin/includes/admin.php';
  328. require_once JETPACK__PLUGIN_DIR . 'class.json-api.php';
  329. $api = WPCOM_JSON_API::init( $method, $url, $post_body );
  330. $api->token_details['user'] = $user_details;
  331. require_once JETPACK__PLUGIN_DIR . 'class.json-api-endpoints.php';
  332. $display_errors = ini_set( 'display_errors', 0 );
  333. ob_start();
  334. $content_type = $api->serve( false );
  335. $output = ob_get_clean();
  336. ini_set( 'display_errors', $display_errors );
  337. $nonce = wp_generate_password( 10, false );
  338. $hmac = hash_hmac( 'md5', $nonce . $output, $token->secret );
  339. wp_set_current_user( isset( $old_user->ID ) ? $old_user->ID : 0 );
  340. return array(
  341. (string) $output,
  342. (string) $nonce,
  343. (string) $hmac,
  344. );
  345. }
  346. }