PageRenderTime 31ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 1ms

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

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