PluginProbe
Jetpack – WP Security, Backup, Speed, & Growth / 2.1.7
Jetpack – WP Security, Backup, Speed, & Growth v2.1.7
16.2-beta 12.0.3 12.1.3 12.2.3 12.3.2 12.4.2 12.5.2 12.6.4 12.7.3 12.8.3 12.9.5 13.0.2 13.1.5 13.2.4 13.3.3 13.4.5 13.5.2 13.6.2 13.7.2 13.8.3 13.9.2 14.0.1 14.1.1 14.2.2 14.3.1 All 501 releases
jetpack / class.jetpack-xmlrpc-server.php

class.jetpack-xmlrpc-server.php in Jetpack – WP Security, Backup, Speed, & Growth 2.1.7, at class.jetpack-xmlrpc-server.php

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