| 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 |
'jetpack.disconnectBlog' => array( $this, 'disconnect_blog' ), |
| 36 |
'jetpack.unlinkUser' => array( $this, 'unlink_user' ), |
| 37 |
) ); |
| 38 |
|
| 39 |
if ( isset( $core_methods['metaWeblog.editPost'] ) ) { |
| 40 |
$jetpack_methods['metaWeblog.newMediaObject'] = $core_methods['metaWeblog.newMediaObject']; |
| 41 |
$jetpack_methods['jetpack.updateAttachmentParent'] = array( $this, 'update_attachment_parent' ); |
| 42 |
} |
| 43 |
|
| 44 |
$jetpack_methods = apply_filters( 'jetpack_xmlrpc_methods', $jetpack_methods, $core_methods, $user ); |
| 45 |
} |
| 46 |
|
| 47 |
return apply_filters( 'jetpack_xmlrpc_unauthenticated_methods', $jetpack_methods, $core_methods ); |
| 48 |
} |
| 49 |
|
| 50 |
/** |
| 51 |
* Whitelist of the bootstrap XML-RPC methods |
| 52 |
*/ |
| 53 |
function bootstrap_xmlrpc_methods() { |
| 54 |
return array( |
| 55 |
'jetpack.verifyRegistration' => array( $this, 'verify_registration' ), |
| 56 |
); |
| 57 |
} |
| 58 |
|
| 59 |
/** |
| 60 |
* Verifies that Jetpack.WordPress.com received a registration request from this site |
| 61 |
*/ |
| 62 |
function verify_registration( $verify_secret ) { |
| 63 |
return $this->verify_action( array( 'register', $verify_secret ) ); |
| 64 |
} |
| 65 |
|
| 66 |
/** |
| 67 |
* @return WP_Error|string secret_2 on success, WP_Error( error_code => error_code, error_message => error description, error_data => status code ) on failure |
| 68 |
* |
| 69 |
* Possible error_codes: |
| 70 |
* |
| 71 |
* verify_secret_1_missing |
| 72 |
* verify_secret_1_malformed |
| 73 |
* verify_secrets_missing: No longer have verification secrets stored |
| 74 |
* verify_secrets_mismatch: stored secret_1 does not match secret_1 sent by Jetpack.WordPress.com |
| 75 |
*/ |
| 76 |
function verify_action( $params ) { |
| 77 |
$action = $params[0]; |
| 78 |
$verify_secret = $params[1]; |
| 79 |
|
| 80 |
if ( empty( $verify_secret ) ) { |
| 81 |
return $this->error( new Jetpack_Error( 'verify_secret_1_missing', sprintf( 'The required "%s" parameter is missing.', 'secret_1' ), 400 ) ); |
| 82 |
} else if ( !is_string( $verify_secret ) ) { |
| 83 |
return $this->error( new Jetpack_Error( 'verify_secret_1_malformed', sprintf( 'The required "%s" parameter is malformed.', 'secret_1' ), 400 ) ); |
| 84 |
} |
| 85 |
|
| 86 |
$secrets = Jetpack_Options::get_option( $action ); |
| 87 |
if ( !$secrets || is_wp_error( $secrets ) ) { |
| 88 |
Jetpack_Options::delete_option( $action ); |
| 89 |
return $this->error( new Jetpack_Error( 'verify_secrets_missing', 'Verification took too long', 400 ) ); |
| 90 |
} |
| 91 |
|
| 92 |
@list( $secret_1, $secret_2, $secret_eol ) = explode( ':', $secrets ); |
| 93 |
if ( empty( $secret_1 ) || empty( $secret_2 ) || empty( $secret_eol ) || $secret_eol < time() ) { |
| 94 |
Jetpack_Options::delete_option( $action ); |
| 95 |
return $this->error( new Jetpack_Error( 'verify_secrets_missing', 'Verification took too long', 400 ) ); |
| 96 |
} |
| 97 |
|
| 98 |
if ( $verify_secret !== $secret_1 ) { |
| 99 |
Jetpack_Options::delete_option( $action ); |
| 100 |
return $this->error( new Jetpack_Error( 'verify_secrets_mismatch', 'Secret mismatch', 400 ) ); |
| 101 |
} |
| 102 |
|
| 103 |
Jetpack_Options::delete_option( $action ); |
| 104 |
|
| 105 |
return $secret_2; |
| 106 |
} |
| 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 |
|
| 128 |
return $user; |
| 129 |
} |
| 130 |
|
| 131 |
/** |
| 132 |
* Returns the current error as an IXR_Error |
| 133 |
* |
| 134 |
* @return null|IXR_Error |
| 135 |
*/ |
| 136 |
function error( $error = null ) { |
| 137 |
if ( !is_null( $error ) ) { |
| 138 |
$this->error = $error; |
| 139 |
} |
| 140 |
|
| 141 |
if ( is_wp_error( $this->error ) ) { |
| 142 |
$code = $this->error->get_error_data(); |
| 143 |
if ( !$code ) { |
| 144 |
$code = -10520; |
| 145 |
} |
| 146 |
$message = sprintf( 'Jetpack: [%s] %s', $this->error->get_error_code(), $this->error->get_error_message() ); |
| 147 |
return new IXR_Error( $code, $message ); |
| 148 |
} else if ( is_a( $this->error, 'IXR_Error' ) ) { |
| 149 |
return $this->error; |
| 150 |
} |
| 151 |
|
| 152 |
return false; |
| 153 |
} |
| 154 |
|
| 155 |
/* API Methods */ |
| 156 |
|
| 157 |
/** |
| 158 |
* Just authenticates with the given Jetpack credentials. |
| 159 |
* |
| 160 |
* @return bool|IXR_Error |
| 161 |
*/ |
| 162 |
function test_connection() { |
| 163 |
return JETPACK__VERSION; |
| 164 |
} |
| 165 |
|
| 166 |
function test_api_user_code( $args ) { |
| 167 |
$client_id = (int) $args[0]; |
| 168 |
$user_id = (int) $args[1]; |
| 169 |
$nonce = (string) $args[2]; |
| 170 |
$verify = (string) $args[3]; |
| 171 |
|
| 172 |
if ( !$client_id || !$user_id || !strlen( $nonce ) || 32 !== strlen( $verify ) ) { |
| 173 |
return false; |
| 174 |
} |
| 175 |
|
| 176 |
$user = get_user_by( 'id', $user_id ); |
| 177 |
if ( !$user || is_wp_error( $user ) ) { |
| 178 |
return false; |
| 179 |
} |
| 180 |
|
| 181 |
/* debugging |
| 182 |
error_log( "CLIENT: $client_id" ); |
| 183 |
error_log( "USER: $user_id" ); |
| 184 |
error_log( "NONCE: $nonce" ); |
| 185 |
error_log( "VERIFY: $verify" ); |
| 186 |
*/ |
| 187 |
|
| 188 |
$jetpack_token = Jetpack_Data::get_access_token( JETPACK_MASTER_USER ); |
| 189 |
|
| 190 |
$api_user_code = get_user_meta( $user_id, "jetpack_json_api_$client_id", true ); |
| 191 |
if ( !$api_user_code ) { |
| 192 |
return false; |
| 193 |
} |
| 194 |
|
| 195 |
$hmac = hash_hmac( 'md5', json_encode( (object) array( |
| 196 |
'client_id' => (int) $client_id, |
| 197 |
'user_id' => (int) $user_id, |
| 198 |
'nonce' => (string) $nonce, |
| 199 |
'code' => (string) $api_user_code, |
| 200 |
) ), $jetpack_token->secret ); |
| 201 |
|
| 202 |
if ( $hmac !== $verify ) { |
| 203 |
return false; |
| 204 |
} |
| 205 |
|
| 206 |
return $user_id; |
| 207 |
} |
| 208 |
|
| 209 |
/** |
| 210 |
* Disconnect this blog from the connected wordpress.com account |
| 211 |
* @return boolean |
| 212 |
*/ |
| 213 |
function disconnect_blog() { |
| 214 |
Jetpack::log( 'disconnect' ); |
| 215 |
Jetpack::disconnect(); |
| 216 |
|
| 217 |
return true; |
| 218 |
} |
| 219 |
|
| 220 |
/** |
| 221 |
* Unlink a user from WordPress.com |
| 222 |
* |
| 223 |
* This will fail if called by the Master User. |
| 224 |
*/ |
| 225 |
function unlink_user() { |
| 226 |
Jetpack::log( 'unlink' ); |
| 227 |
return Jetpack::unlink_user(); |
| 228 |
} |
| 229 |
|
| 230 |
/** |
| 231 |
* Returns what features are available. Uses the slug of the module files. |
| 232 |
* |
| 233 |
* @return array|IXR_Error |
| 234 |
*/ |
| 235 |
function features_available() { |
| 236 |
$raw_modules = Jetpack::get_available_modules(); |
| 237 |
$modules = array(); |
| 238 |
foreach ( $raw_modules as $module ) { |
| 239 |
$modules[] = Jetpack::get_module_slug( $module ); |
| 240 |
} |
| 241 |
|
| 242 |
return $modules; |
| 243 |
} |
| 244 |
|
| 245 |
/** |
| 246 |
* Returns what features are enabled. Uses the slug of the modules files. |
| 247 |
* |
| 248 |
* @return array|IXR_Error |
| 249 |
*/ |
| 250 |
function features_enabled() { |
| 251 |
$raw_modules = Jetpack::get_active_modules(); |
| 252 |
$modules = array(); |
| 253 |
foreach ( $raw_modules as $module ) { |
| 254 |
$modules[] = Jetpack::get_module_slug( $module ); |
| 255 |
} |
| 256 |
|
| 257 |
return $modules; |
| 258 |
} |
| 259 |
|
| 260 |
function get_post( $id ) { |
| 261 |
if ( !$id = (int) $id ) { |
| 262 |
return false; |
| 263 |
} |
| 264 |
|
| 265 |
$jetpack = Jetpack::init(); |
| 266 |
|
| 267 |
$post = $jetpack->sync->get_post( $id ); |
| 268 |
return $post; |
| 269 |
} |
| 270 |
|
| 271 |
function get_posts( $args ) { |
| 272 |
list( $post_ids ) = $args; |
| 273 |
$post_ids = array_map( 'intval', (array) $post_ids ); |
| 274 |
$jp = Jetpack::init(); |
| 275 |
$sync_data = $jp->sync->get_content( array( 'posts' => $post_ids ) ); |
| 276 |
|
| 277 |
return $sync_data; |
| 278 |
} |
| 279 |
|
| 280 |
function get_comment( $id ) { |
| 281 |
if ( !$id = (int) $id ) { |
| 282 |
return false; |
| 283 |
} |
| 284 |
|
| 285 |
$jetpack = Jetpack::init(); |
| 286 |
|
| 287 |
$comment = $jetpack->sync->get_comment( $id ); |
| 288 |
if ( !is_array( $comment ) ) |
| 289 |
return false; |
| 290 |
|
| 291 |
$post = $jetpack->sync->get_post( $comment['comment_post_ID'] ); |
| 292 |
if ( !$post ) { |
| 293 |
return false; |
| 294 |
} |
| 295 |
|
| 296 |
return $comment; |
| 297 |
} |
| 298 |
|
| 299 |
function get_comments( $args ) { |
| 300 |
list( $comment_ids ) = $args; |
| 301 |
$comment_ids = array_map( 'intval', (array) $comment_ids ); |
| 302 |
$jp = Jetpack::init(); |
| 303 |
$sync_data = $jp->sync->get_content( array( 'comments' => $comment_ids ) ); |
| 304 |
|
| 305 |
return $sync_data; |
| 306 |
} |
| 307 |
|
| 308 |
function update_attachment_parent( $args ) { |
| 309 |
$attachment_id = (int) $args[0]; |
| 310 |
$parent_id = (int) $args[1]; |
| 311 |
|
| 312 |
return wp_update_post( array( |
| 313 |
'ID' => $attachment_id, |
| 314 |
'post_parent' => $parent_id, |
| 315 |
) ); |
| 316 |
} |
| 317 |
|
| 318 |
function json_api( $args = array() ) { |
| 319 |
$json_api_args = $args[0]; |
| 320 |
$verify_api_user_args = $args[1]; |
| 321 |
|
| 322 |
$method = (string) $json_api_args[0]; |
| 323 |
$url = (string) $json_api_args[1]; |
| 324 |
$post_body = is_null( $json_api_args[2] ) ? null : (string) $json_api_args[2]; |
| 325 |
$my_id = (int) $json_api_args[3]; |
| 326 |
$user_details = (array) $json_api_args[4]; |
| 327 |
|
| 328 |
if ( !$verify_api_user_args ) { |
| 329 |
$user_id = 0; |
| 330 |
} elseif ( 'internal' === $verify_api_user_args[0] ) { |
| 331 |
$user_id = (int) $verify_api_user_args[1]; |
| 332 |
if ( $user_id ) { |
| 333 |
$user = get_user_by( 'id', $user_id ); |
| 334 |
if ( !$user || is_wp_error( $user ) ) { |
| 335 |
return false; |
| 336 |
} |
| 337 |
} |
| 338 |
} else { |
| 339 |
$user_id = call_user_func( array( $this, 'test_api_user_code' ), $verify_api_user_args ); |
| 340 |
if ( !$user_id ) { |
| 341 |
return false; |
| 342 |
} |
| 343 |
} |
| 344 |
|
| 345 |
/* debugging |
| 346 |
error_log( "-- begin json api via jetpack debugging -- " ); |
| 347 |
error_log( "METHOD: $method" ); |
| 348 |
error_log( "URL: $url" ); |
| 349 |
error_log( "POST BODY: $post_body" ); |
| 350 |
error_log( "MY JETPACK ID: $my_id" ); |
| 351 |
error_log( "VERIFY_ARGS: " . print_r( $verify_api_user_args, 1 ) ); |
| 352 |
error_log( "VERIFIED USER_ID: " . (int) $user_id ); |
| 353 |
error_log( "-- end json api via jetpack debugging -- " ); |
| 354 |
*/ |
| 355 |
|
| 356 |
$old_user = wp_get_current_user(); |
| 357 |
wp_set_current_user( $user_id ); |
| 358 |
|
| 359 |
$token = Jetpack_Data::get_access_token( get_current_user_id() ); |
| 360 |
if ( !$token || is_wp_error( $token ) ) { |
| 361 |
return false; |
| 362 |
} |
| 363 |
|
| 364 |
define( 'REST_API_REQUEST', true ); |
| 365 |
define( 'WPCOM_JSON_API__BASE', 'public-api.wordpress.com/rest/v1' ); |
| 366 |
|
| 367 |
// needed? |
| 368 |
require_once ABSPATH . 'wp-admin/includes/admin.php'; |
| 369 |
|
| 370 |
require_once dirname( __FILE__ ) . '/class.json-api.php'; |
| 371 |
$api = WPCOM_JSON_API::init( $method, $url, $post_body ); |
| 372 |
$api->token_details['user'] = $user_details; |
| 373 |
require_once dirname( __FILE__ ) . '/class.json-api-endpoints.php'; |
| 374 |
|
| 375 |
$display_errors = ini_set( 'display_errors', 0 ); |
| 376 |
ob_start(); |
| 377 |
$content_type = $api->serve( false ); |
| 378 |
$output = ob_get_clean(); |
| 379 |
ini_set( 'display_errors', $display_errors ); |
| 380 |
|
| 381 |
$nonce = wp_generate_password( 10, false ); |
| 382 |
$hmac = hash_hmac( 'md5', $nonce . $output, $token->secret ); |
| 383 |
|
| 384 |
wp_set_current_user( isset( $old_user->ID ) ? $old_user->ID : 0 ); |
| 385 |
|
| 386 |
return array( |
| 387 |
(string) $output, |
| 388 |
(string) $nonce, |
| 389 |
(string) $hmac, |
| 390 |
); |
| 391 |
} |
| 392 |
} |
| 393 |
|