PageRenderTime 74ms CodeModel.GetById 17ms RepoModel.GetById 1ms app.codeStats 0ms

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

https://gitlab.com/endomorphosis/falkenstein
PHP | 367 lines | 235 code | 62 blank | 70 comment | 43 complexity | bbe21c50158201b9b5b3809ad327cc3d 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. ) );
  32. if ( isset( $core_methods['metaWeblog.editPost'] ) ) {
  33. $jetpack_methods['metaWeblog.newMediaObject'] = $core_methods['metaWeblog.newMediaObject'];
  34. $jetpack_methods['jetpack.updateAttachmentParent'] = array( $this, 'update_attachment_parent' );
  35. }
  36. }
  37. return apply_filters( 'jetpack_xmlrpc_methods', $jetpack_methods, $core_methods, $user );
  38. }
  39. /**
  40. * Whitelist of the bootstrap XML-RPC methods
  41. */
  42. function bootstrap_xmlrpc_methods() {
  43. return array(
  44. 'jetpack.verifyRegistration' => array( $this, 'verify_registration' ),
  45. );
  46. }
  47. /**
  48. * Verifies that Jetpack.WordPress.com received a registration request from this site
  49. */
  50. function verify_registration( $verify_secret ) {
  51. return $this->verify_action( array( 'register', $verify_secret ) );
  52. }
  53. /**
  54. * @return WP_Error|string secret_2 on success, WP_Error( error_code => error_code, error_message => error description, error_data => status code ) on failure
  55. *
  56. * Possible error_codes:
  57. *
  58. * verify_secret_1_missing
  59. * verify_secret_1_malformed
  60. * verify_secrets_missing: No longer have verification secrets stored
  61. * verify_secrets_mismatch: stored secret_1 does not match secret_1 sent by Jetpack.WordPress.com
  62. */
  63. function verify_action( $params ) {
  64. $action = $params[0];
  65. $verify_secret = $params[1];
  66. if ( empty( $verify_secret ) ) {
  67. return $this->error( new Jetpack_Error( 'verify_secret_1_missing', sprintf( 'The required "%s" parameter is missing.', 'secret_1' ), 400 ) );
  68. } else if ( !is_string( $verify_secret ) ) {
  69. return $this->error( new Jetpack_Error( 'verify_secret_1_malformed', sprintf( 'The required "%s" parameter is malformed.', 'secret_1' ), 400 ) );
  70. }
  71. $secrets = Jetpack_Options::get_option( $action );
  72. if ( !$secrets || is_wp_error( $secrets ) ) {
  73. Jetpack_Options::delete_option( $action );
  74. return $this->error( new Jetpack_Error( 'verify_secrets_missing', 'Verification took too long', 400 ) );
  75. }
  76. @list( $secret_1, $secret_2, $secret_eol ) = explode( ':', $secrets );
  77. if ( empty( $secret_1 ) || empty( $secret_2 ) || empty( $secret_eol ) || $secret_eol < time() ) {
  78. Jetpack_Options::delete_option( $action );
  79. return $this->error( new Jetpack_Error( 'verify_secrets_missing', 'Verification took too long', 400 ) );
  80. }
  81. if ( $verify_secret !== $secret_1 ) {
  82. Jetpack_Options::delete_option( $action );
  83. return $this->error( new Jetpack_Error( 'verify_secrets_mismatch', 'Secret mismatch', 400 ) );
  84. }
  85. Jetpack_Options::delete_option( $action );
  86. return $secret_2;
  87. }
  88. /**
  89. * Wrapper for wp_authenticate( $username, $password );
  90. *
  91. * @return WP_User|IXR_Error
  92. */
  93. function login() {
  94. Jetpack::init()->require_jetpack_authentication();
  95. $user = wp_authenticate( 'username', 'password' );
  96. if ( is_wp_error( $user ) ) {
  97. if ( 'authentication_failed' == $user->get_error_code() ) { // Generic error could mean most anything.
  98. $this->error = new Jetpack_Error( 'invalid_request', 'Invalid Request', 403 );
  99. } else {
  100. $this->error = $user;
  101. }
  102. return false;
  103. } else if ( !$user ) { // Shouldn't happen.
  104. $this->error = new Jetpack_Error( 'invalid_request', 'Invalid Request', 403 );
  105. return false;
  106. }
  107. return $user;
  108. }
  109. /**
  110. * Returns the current error as an IXR_Error
  111. *
  112. * @return null|IXR_Error
  113. */
  114. function error( $error = null ) {
  115. if ( !is_null( $error ) ) {
  116. $this->error = $error;
  117. }
  118. if ( is_wp_error( $this->error ) ) {
  119. $code = $this->error->get_error_data();
  120. if ( !$code ) {
  121. $code = -10520;
  122. }
  123. $message = sprintf( 'Jetpack: [%s] %s', $this->error->get_error_code(), $this->error->get_error_message() );
  124. return new IXR_Error( $code, $message );
  125. } else if ( is_a( $this->error, 'IXR_Error' ) ) {
  126. return $this->error;
  127. }
  128. return false;
  129. }
  130. /* API Methods */
  131. /**
  132. * Just authenticates with the given Jetpack credentials.
  133. *
  134. * @return bool|IXR_Error
  135. */
  136. function test_connection() {
  137. return JETPACK__VERSION;
  138. }
  139. function test_api_user_code( $args ) {
  140. $client_id = (int) $args[0];
  141. $user_id = (int) $args[1];
  142. $nonce = (string) $args[2];
  143. $verify = (string) $args[3];
  144. if ( !$client_id || !$user_id || !strlen( $nonce ) || 32 !== strlen( $verify ) ) {
  145. return false;
  146. }
  147. $user = get_user_by( 'id', $user_id );
  148. if ( !$user || is_wp_error( $user ) ) {
  149. return false;
  150. }
  151. /* debugging
  152. error_log( "CLIENT: $client_id" );
  153. error_log( "USER: $user_id" );
  154. error_log( "NONCE: $nonce" );
  155. error_log( "VERIFY: $verify" );
  156. */
  157. $jetpack_token = Jetpack_Data::get_access_token( JETPACK_MASTER_USER );
  158. $api_user_code = get_user_meta( $user_id, "jetpack_json_api_$client_id", true );
  159. if ( !$api_user_code ) {
  160. return false;
  161. }
  162. $hmac = hash_hmac( 'md5', json_encode( (object) array(
  163. 'client_id' => (int) $client_id,
  164. 'user_id' => (int) $user_id,
  165. 'nonce' => (string) $nonce,
  166. 'code' => (string) $api_user_code,
  167. ) ), $jetpack_token->secret );
  168. if ( $hmac !== $verify ) {
  169. return false;
  170. }
  171. return $user_id;
  172. }
  173. /**
  174. * Returns what features are available. Uses the slug of the module files.
  175. *
  176. * @return array|IXR_Error
  177. */
  178. function features_available() {
  179. $raw_modules = Jetpack::get_available_modules();
  180. $modules = array();
  181. foreach ( $raw_modules as $module ) {
  182. $modules[] = Jetpack::get_module_slug( $module );
  183. }
  184. return $modules;
  185. }
  186. /**
  187. * Returns what features are enabled. Uses the slug of the modules files.
  188. *
  189. * @return array|IXR_Error
  190. */
  191. function features_enabled() {
  192. $raw_modules = Jetpack::get_active_modules();
  193. $modules = array();
  194. foreach ( $raw_modules as $module ) {
  195. $modules[] = Jetpack::get_module_slug( $module );
  196. }
  197. return $modules;
  198. }
  199. function get_post( $id ) {
  200. if ( !$id = (int) $id ) {
  201. return false;
  202. }
  203. $jetpack = Jetpack::init();
  204. $post = $jetpack->sync->get_post( $id );
  205. return $post;
  206. }
  207. function get_posts( $args ) {
  208. list( $post_ids ) = $args;
  209. $post_ids = array_map( 'intval', (array) $post_ids );
  210. $jp = Jetpack::init();
  211. $sync_data = $jp->sync->get_content( array( 'posts' => $post_ids ) );
  212. return $sync_data;
  213. }
  214. function get_comment( $id ) {
  215. if ( !$id = (int) $id ) {
  216. return false;
  217. }
  218. $jetpack = Jetpack::init();
  219. $comment = $jetpack->sync->get_comment( $id );
  220. if ( !is_array( $comment ) )
  221. return false;
  222. $post = $jetpack->sync->get_post( $comment['comment_post_ID'] );
  223. if ( !$post ) {
  224. return false;
  225. }
  226. return $comment;
  227. }
  228. function get_comments( $args ) {
  229. list( $comment_ids ) = $args;
  230. $comment_ids = array_map( 'intval', (array) $comment_ids );
  231. $jp = Jetpack::init();
  232. $sync_data = $jp->sync->get_content( array( 'comments' => $comment_ids ) );
  233. return $sync_data;
  234. }
  235. function update_attachment_parent( $args ) {
  236. $attachment_id = (int) $args[0];
  237. $parent_id = (int) $args[1];
  238. return wp_update_post( array(
  239. 'ID' => $attachment_id,
  240. 'post_parent' => $parent_id,
  241. ) );
  242. }
  243. function json_api( $args = array() ) {
  244. $json_api_args = $args[0];
  245. $verify_api_user_args = $args[1];
  246. $method = (string) $json_api_args[0];
  247. $url = (string) $json_api_args[1];
  248. $post_body = is_null( $json_api_args[2] ) ? null : (string) $json_api_args[2];
  249. $my_id = (int) $json_api_args[3];
  250. $user_details = (array) $json_api_args[4];
  251. if ( !$verify_api_user_args ) {
  252. $user_id = 0;
  253. } elseif ( 'internal' === $verify_api_user_args[0] ) {
  254. $user_id = (int) $verify_api_user_args[1];
  255. if ( $user_id ) {
  256. $user = get_user_by( 'id', $user_id );
  257. if ( !$user || is_wp_error( $user ) ) {
  258. return false;
  259. }
  260. }
  261. } else {
  262. $user_id = call_user_func( array( $this, 'test_api_user_code' ), $verify_api_user_args );
  263. if ( !$user_id ) {
  264. return false;
  265. }
  266. }
  267. /* debugging
  268. error_log( "-- begin json api via jetpack debugging -- " );
  269. error_log( "METHOD: $method" );
  270. error_log( "URL: $url" );
  271. error_log( "POST BODY: $post_body" );
  272. error_log( "MY JETPACK ID: $my_id" );
  273. error_log( "VERIFY_ARGS: " . print_r( $verify_api_user_args, 1 ) );
  274. error_log( "VERIFIED USER_ID: " . (int) $user_id );
  275. error_log( "-- end json api via jetpack debugging -- " );
  276. */
  277. $old_user = wp_get_current_user();
  278. wp_set_current_user( $user_id );
  279. $token = Jetpack_Data::get_access_token( get_current_user_id() );
  280. if ( !$token || is_wp_error( $token ) ) {
  281. return false;
  282. }
  283. define( 'REST_API_REQUEST', true );
  284. define( 'WPCOM_JSON_API__BASE', 'public-api.wordpress.com/rest/v1' );
  285. // needed?
  286. require_once ABSPATH . 'wp-admin/includes/admin.php';
  287. require_once dirname( __FILE__ ) . '/class.json-api.php';
  288. $api = WPCOM_JSON_API::init( $method, $url, $post_body );
  289. $api->token_details['user'] = $user_details;
  290. require_once dirname( __FILE__ ) . '/class.json-api-endpoints.php';
  291. $display_errors = ini_set( 'display_errors', 0 );
  292. ob_start();
  293. $content_type = $api->serve( false );
  294. $output = ob_get_clean();
  295. ini_set( 'display_errors', $display_errors );
  296. $nonce = wp_generate_password( 10, false );
  297. $hmac = hash_hmac( 'md5', $nonce . $output, $token->secret );
  298. wp_set_current_user( isset( $old_user->ID ) ? $old_user->ID : 0 );
  299. return array(
  300. (string) $output,
  301. (string) $nonce,
  302. (string) $hmac,
  303. );
  304. }
  305. }