PageRenderTime 43ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

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

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