Automattic /
jetpack
These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
| 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 | public $error = null; |
||
| 11 | |||
| 12 | /** |
||
| 13 | * The current user |
||
| 14 | */ |
||
| 15 | public $user = null; |
||
| 16 | |||
| 17 | /** |
||
| 18 | * Whitelist of the XML-RPC methods available to the Jetpack Server. If the |
||
| 19 | * user is not authenticated (->login()) then the methods are never added, |
||
| 20 | * so they will get a "does not exist" error. |
||
| 21 | */ |
||
| 22 | function xmlrpc_methods( $core_methods ) { |
||
| 23 | $jetpack_methods = array( |
||
| 24 | 'jetpack.jsonAPI' => array( $this, 'json_api' ), |
||
| 25 | 'jetpack.verifyAction' => array( $this, 'verify_action' ), |
||
| 26 | 'jetpack.remoteRegister' => array( $this, 'remote_register' ), |
||
| 27 | 'jetpack.remoteProvision' => array( $this, 'remote_provision' ), |
||
| 28 | ); |
||
| 29 | |||
| 30 | $this->user = $this->login(); |
||
| 31 | |||
| 32 | if ( $this->user ) { |
||
| 33 | $jetpack_methods = array_merge( $jetpack_methods, array( |
||
| 34 | 'jetpack.testConnection' => array( $this, 'test_connection' ), |
||
| 35 | 'jetpack.testAPIUserCode' => array( $this, 'test_api_user_code' ), |
||
| 36 | 'jetpack.featuresAvailable' => array( $this, 'features_available' ), |
||
| 37 | 'jetpack.featuresEnabled' => array( $this, 'features_enabled' ), |
||
| 38 | 'jetpack.disconnectBlog' => array( $this, 'disconnect_blog' ), |
||
| 39 | 'jetpack.unlinkUser' => array( $this, 'unlink_user' ), |
||
| 40 | 'jetpack.syncObject' => array( $this, 'sync_object' ), |
||
| 41 | 'jetpack.idcUrlValidation' => array( $this, 'validate_urls_for_idc_mitigation' ), |
||
| 42 | ) ); |
||
| 43 | |||
| 44 | if ( isset( $core_methods['metaWeblog.editPost'] ) ) { |
||
| 45 | $jetpack_methods['metaWeblog.newMediaObject'] = $core_methods['metaWeblog.newMediaObject']; |
||
| 46 | $jetpack_methods['jetpack.updateAttachmentParent'] = array( $this, 'update_attachment_parent' ); |
||
| 47 | } |
||
| 48 | |||
| 49 | /** |
||
| 50 | * Filters the XML-RPC methods available to Jetpack for authenticated users. |
||
| 51 | * |
||
| 52 | * @since 1.1.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 | * @param WP_User $user Information about a given WordPress user. |
||
| 57 | */ |
||
| 58 | $jetpack_methods = apply_filters( 'jetpack_xmlrpc_methods', $jetpack_methods, $core_methods, $this->user ); |
||
| 59 | } |
||
| 60 | |||
| 61 | /** |
||
| 62 | * Filters the XML-RPC methods available to Jetpack for unauthenticated users. |
||
| 63 | * |
||
| 64 | * @since 3.0.0 |
||
| 65 | * |
||
| 66 | * @param array $jetpack_methods XML-RPC methods available to the Jetpack Server. |
||
| 67 | * @param array $core_methods Available core XML-RPC methods. |
||
| 68 | */ |
||
| 69 | return apply_filters( 'jetpack_xmlrpc_unauthenticated_methods', $jetpack_methods, $core_methods ); |
||
| 70 | } |
||
| 71 | |||
| 72 | /** |
||
| 73 | * Whitelist of the bootstrap XML-RPC methods |
||
| 74 | */ |
||
| 75 | function bootstrap_xmlrpc_methods() { |
||
| 76 | return array( |
||
| 77 | 'jetpack.verifyRegistration' => array( $this, 'verify_registration' ), |
||
| 78 | 'jetpack.remoteAuthorize' => array( $this, 'remote_authorize' ), |
||
| 79 | 'jetpack.remoteRegister' => array( $this, 'remote_register' ), |
||
| 80 | ); |
||
| 81 | } |
||
| 82 | |||
| 83 | function authorize_xmlrpc_methods() { |
||
| 84 | return array( |
||
| 85 | 'jetpack.remoteAuthorize' => array( $this, 'remote_authorize' ), |
||
| 86 | ); |
||
| 87 | } |
||
| 88 | |||
| 89 | function provision_xmlrpc_methods() { |
||
| 90 | return array( |
||
| 91 | 'jetpack.remoteRegister' => array( $this, 'remote_register' ), |
||
| 92 | 'jetpack.remoteProvision' => array( $this, 'remote_provision' ), |
||
| 93 | ); |
||
| 94 | } |
||
| 95 | |||
| 96 | function remote_authorize( $request ) { |
||
| 97 | $user = get_user_by( 'id', $request['state'] ); |
||
| 98 | JetpackTracking::record_user_event( 'jpc_remote_authorize_begin', array(), $user ); |
||
| 99 | |||
| 100 | foreach( array( 'secret', 'state', 'redirect_uri', 'code' ) as $required ) { |
||
| 101 | if ( ! isset( $request[ $required ] ) || empty( $request[ $required ] ) ) { |
||
| 102 | return $this->error( new Jetpack_Error( 'missing_parameter', 'One or more parameters is missing from the request.', 400 ), 'jpc_remote_authorize_fail' ); |
||
| 103 | } |
||
| 104 | } |
||
| 105 | |||
| 106 | if ( ! $user ) { |
||
| 107 | return $this->error( new Jetpack_Error( 'user_unknown', 'User not found.', 404 ), 'jpc_remote_authorize_fail' ); |
||
| 108 | } |
||
| 109 | |||
| 110 | if ( Jetpack::is_active() && Jetpack::is_user_connected( $request['state'] ) ) { |
||
| 111 | return $this->error( new Jetpack_Error( 'already_connected', 'User already connected.', 400 ), 'jpc_remote_authorize_fail' ); |
||
| 112 | } |
||
| 113 | |||
| 114 | $verified = $this->verify_action( array( 'authorize', $request['secret'], $request['state'] ) ); |
||
| 115 | |||
| 116 | if ( is_a( $verified, 'IXR_Error' ) ) { |
||
| 117 | return $this->error( $verified, 'jpc_remote_authorize_fail' ); |
||
| 118 | } |
||
| 119 | |||
| 120 | wp_set_current_user( $request['state'] ); |
||
| 121 | |||
| 122 | $client_server = new Jetpack_Client_Server; |
||
| 123 | $result = $client_server->authorize( $request ); |
||
| 124 | |||
| 125 | if ( is_wp_error( $result ) ) { |
||
| 126 | return $this->error( $result, 'jpc_remote_authorize_fail' ); |
||
| 127 | } |
||
| 128 | |||
| 129 | JetpackTracking::record_user_event( 'jpc_remote_authorize_success' ); |
||
| 130 | |||
| 131 | return array( |
||
| 132 | 'result' => $result, |
||
| 133 | ); |
||
| 134 | } |
||
| 135 | |||
| 136 | /** |
||
| 137 | * This XML-RPC method is called from the /jpphp/provision endpoint on WPCOM in order to |
||
| 138 | * register this site so that a plan can be provisioned. |
||
| 139 | * |
||
| 140 | * @param array $request An array containing at minimum nonce and local_user keys. |
||
| 141 | * |
||
| 142 | * @return WP_Error|array |
||
| 143 | */ |
||
| 144 | public function remote_register( $request ) { |
||
| 145 | JetpackTracking::record_user_event( 'jpc_remote_register_begin', array() ); |
||
| 146 | |||
| 147 | $user = $this->fetch_and_verify_local_user( $request ); |
||
| 148 | |||
| 149 | if ( ! $user ) { |
||
| 150 | return $this->error( new WP_Error( 'input_error', __( 'Valid user is required', 'jetpack' ), 400 ), 'jpc_remote_register_fail' ); |
||
| 151 | } |
||
| 152 | |||
| 153 | if ( is_wp_error( $user ) || is_a( $user, 'IXR_Error' ) ) { |
||
| 154 | return $this->error( $user, 'jpc_remote_register_fail' ); |
||
| 155 | } |
||
| 156 | |||
| 157 | if ( empty( $request['nonce'] ) ) { |
||
| 158 | return $this->error( |
||
| 159 | new Jetpack_Error( |
||
| 160 | 'nonce_missing', |
||
| 161 | __( 'The required "nonce" parameter is missing.', 'jetpack' ), |
||
| 162 | 400 |
||
| 163 | ), |
||
| 164 | 'jpc_remote_register_fail' |
||
| 165 | ); |
||
| 166 | } |
||
| 167 | |||
| 168 | $nonce = sanitize_text_field( $request['nonce'] ); |
||
| 169 | unset( $request['nonce'] ); |
||
| 170 | |||
| 171 | $api_url = Jetpack::fix_url_for_bad_hosts( Jetpack::api_url( 'partner_provision_nonce_check' ) ); |
||
| 172 | $response = Jetpack_Client::_wp_remote_request( |
||
| 173 | esc_url_raw( add_query_arg( 'nonce', $nonce, $api_url ) ), |
||
| 174 | array( 'method' => 'GET' ), |
||
| 175 | true |
||
| 176 | ); |
||
| 177 | |||
| 178 | if ( |
||
| 179 | 200 !== wp_remote_retrieve_response_code( $response ) || |
||
| 180 | 'OK' !== trim( wp_remote_retrieve_body( $response ) ) |
||
| 181 | ) { |
||
| 182 | return $this->error( |
||
| 183 | new Jetpack_Error( |
||
| 184 | 'invalid_nonce', |
||
| 185 | __( 'There was an issue validating this request.', 'jetpack' ), |
||
| 186 | 400 |
||
| 187 | ), |
||
| 188 | 'jpc_remote_register_fail' |
||
| 189 | ); |
||
| 190 | } |
||
| 191 | |||
| 192 | if ( ! Jetpack_Options::get_option( 'id' ) || ! Jetpack_Options::get_option( 'blog_token' ) || ! empty( $request['force'] ) ) { |
||
| 193 | wp_set_current_user( $user->ID ); |
||
| 194 | |||
| 195 | // This code mostly copied from Jetpack::admin_page_load. |
||
| 196 | Jetpack::maybe_set_version_option(); |
||
| 197 | $registered = Jetpack::try_registration(); |
||
| 198 | if ( is_wp_error( $registered ) ) { |
||
| 199 | return $this->error( $registered, 'jpc_remote_register_fail' ); |
||
| 200 | } elseif ( ! $registered ) { |
||
| 201 | return $this->error( |
||
| 202 | new Jetpack_Error( |
||
| 203 | 'registration_error', |
||
| 204 | __( 'There was an unspecified error registering the site', 'jetpack' ), |
||
| 205 | 400 |
||
| 206 | ), |
||
| 207 | 'jpc_remote_register_fail' |
||
| 208 | ); |
||
| 209 | } |
||
| 210 | } |
||
| 211 | |||
| 212 | JetpackTracking::record_user_event( 'jpc_remote_register_success' ); |
||
| 213 | |||
| 214 | return array( |
||
| 215 | 'client_id' => Jetpack_Options::get_option( 'id' ) |
||
| 216 | ); |
||
| 217 | } |
||
| 218 | |||
| 219 | /** |
||
| 220 | * This XML-RPC method is called from the /jpphp/provision endpoint on WPCOM in order to |
||
| 221 | * register this site so that a plan can be provisioned. |
||
| 222 | * |
||
| 223 | * @param array $request An array containing at minimum a nonce key and a local_username key. |
||
| 224 | * |
||
| 225 | * @return WP_Error|array |
||
| 226 | */ |
||
| 227 | public function remote_provision( $request ) { |
||
| 228 | $user = $this->fetch_and_verify_local_user( $request ); |
||
| 229 | |||
| 230 | if ( ! $user ) { |
||
| 231 | return $this->error( new WP_Error( 'input_error', __( 'Valid user is required', 'jetpack' ), 400 ), 'jpc_remote_register_fail' ); |
||
| 232 | } |
||
| 233 | |||
| 234 | if ( is_wp_error( $user ) || is_a( $user, 'IXR_Error' ) ) { |
||
| 235 | return $this->error( $user, 'jpc_remote_register_fail' ); |
||
| 236 | } |
||
| 237 | |||
| 238 | $site_icon = ( function_exists( 'has_site_icon' ) && has_site_icon() ) |
||
| 239 | ? get_site_icon_url() |
||
| 240 | : false; |
||
| 241 | |||
| 242 | $auto_enable_sso = ( ! Jetpack::is_active() || Jetpack::is_module_active( 'sso' ) ); |
||
| 243 | |||
| 244 | /** This filter is documented in class.jetpack-cli.php */ |
||
| 245 | View Code Duplication | if ( apply_filters( 'jetpack_start_enable_sso', $auto_enable_sso ) ) { |
|
| 246 | $redirect_uri = add_query_arg( |
||
| 247 | array( |
||
| 248 | 'action' => 'jetpack-sso', |
||
| 249 | 'redirect_to' => rawurlencode( admin_url() ), |
||
| 250 | ), |
||
| 251 | wp_login_url() // TODO: come back to Jetpack dashboard? |
||
|
0 ignored issues
–
show
Coding Style
Best Practice
introduced
by
Loading history...
|
|||
| 252 | ); |
||
| 253 | } else { |
||
| 254 | $redirect_uri = admin_url(); |
||
| 255 | } |
||
| 256 | |||
| 257 | // Generate secrets. |
||
| 258 | $role = Jetpack::translate_user_to_role( $user ); |
||
| 259 | $secrets = Jetpack::init()->generate_secrets( 'authorize', $user->ID ); |
||
| 260 | |||
| 261 | $response = array( |
||
| 262 | 'jp_version' => JETPACK__VERSION, |
||
| 263 | 'redirect_uri' => $redirect_uri, |
||
| 264 | 'user_id' => $user->ID, |
||
| 265 | 'user_email' => $user->user_email, |
||
| 266 | 'user_login' => $user->user_login, |
||
| 267 | 'scope' => Jetpack::sign_role( $role, $user->ID ), |
||
| 268 | 'secret' => $secrets['secret_1'], |
||
| 269 | 'is_active' => Jetpack::is_active(), |
||
| 270 | ); |
||
| 271 | |||
| 272 | if ( $site_icon ) { |
||
| 273 | $response['site_icon'] = $site_icon; |
||
| 274 | } |
||
| 275 | |||
| 276 | if ( ! empty( $request['onboarding'] ) ) { |
||
| 277 | Jetpack::create_onboarding_token(); |
||
| 278 | $response['onboarding_token'] = Jetpack_Options::get_option( 'onboarding' ); |
||
| 279 | } |
||
| 280 | |||
| 281 | return $response; |
||
| 282 | } |
||
| 283 | |||
| 284 | private function fetch_and_verify_local_user( $request ) { |
||
| 285 | if ( empty( $request['local_user'] ) ) { |
||
| 286 | return $this->error( |
||
| 287 | new Jetpack_Error( |
||
| 288 | 'local_user_missing', |
||
| 289 | __( 'The required "local_user" parameter is missing.', 'jetpack' ), |
||
| 290 | 400 |
||
| 291 | ), |
||
| 292 | 'jpc_remote_provision_fail' |
||
| 293 | ); |
||
| 294 | } |
||
| 295 | |||
| 296 | // local user is used to look up by login, email or ID |
||
| 297 | $local_user_info = $request['local_user']; |
||
| 298 | |||
| 299 | $user = get_user_by( 'login', $local_user_info ); |
||
| 300 | |||
| 301 | if ( ! $user ) { |
||
| 302 | $user = get_user_by( 'email', $local_user_info ); |
||
| 303 | } |
||
| 304 | |||
| 305 | if ( ! $user ) { |
||
| 306 | $user = get_user_by( 'ID', $local_user_info ); |
||
| 307 | } |
||
| 308 | |||
| 309 | return $user; |
||
| 310 | } |
||
| 311 | |||
| 312 | private function tracks_record_error( $name, $error, $user = null ) { |
||
| 313 | if ( is_wp_error( $error ) ) { |
||
| 314 | JetpackTracking::record_user_event( $name, array( |
||
| 315 | 'error_code' => $error->get_error_code(), |
||
| 316 | 'error_message' => $error->get_error_message() |
||
| 317 | ), $user ); |
||
| 318 | } elseif( is_a( $error, 'IXR_Error' ) ) { |
||
| 319 | JetpackTracking::record_user_event( $name, array( |
||
| 320 | 'error_code' => $error->code, |
||
| 321 | 'error_message' => $error->message |
||
| 322 | ), $user ); |
||
| 323 | } |
||
| 324 | |||
| 325 | return $error; |
||
| 326 | } |
||
| 327 | |||
| 328 | /** |
||
| 329 | * Verifies that Jetpack.WordPress.com received a registration request from this site |
||
| 330 | */ |
||
| 331 | function verify_registration( $data ) { |
||
| 332 | // failure modes will be recorded in tracks in the verify_action method |
||
| 333 | return $this->verify_action( array( 'register', $data[0], $data[1] ) ); |
||
| 334 | } |
||
| 335 | |||
| 336 | /** |
||
| 337 | * @return WP_Error|string secret_2 on success, WP_Error( error_code => error_code, error_message => error description, error_data => status code ) on failure |
||
| 338 | * |
||
| 339 | * Possible error_codes: |
||
| 340 | * |
||
| 341 | * verify_secret_1_missing |
||
| 342 | * verify_secret_1_malformed |
||
| 343 | * verify_secrets_missing: verification secrets are not found in database |
||
| 344 | * verify_secrets_incomplete: verification secrets are only partially found in database |
||
| 345 | * verify_secrets_expired: verification secrets have expired |
||
| 346 | * verify_secrets_mismatch: stored secret_1 does not match secret_1 sent by Jetpack.WordPress.com |
||
| 347 | * state_missing: required parameter of state not found |
||
| 348 | * state_malformed: state is not a digit |
||
| 349 | * invalid_state: state in request does not match the stored state |
||
| 350 | * |
||
| 351 | * The 'authorize' and 'register' actions have additional error codes |
||
| 352 | * |
||
| 353 | * state_missing: a state ( user id ) was not supplied |
||
| 354 | * state_malformed: state is not the correct data type |
||
| 355 | * invalid_state: supplied state does not match the stored state |
||
| 356 | */ |
||
| 357 | function verify_action( $params ) { |
||
| 358 | $action = $params[0]; |
||
| 359 | $verify_secret = $params[1]; |
||
| 360 | $state = isset( $params[2] ) ? $params[2] : ''; |
||
| 361 | $user = get_user_by( 'id', $state ); |
||
| 362 | JetpackTracking::record_user_event( 'jpc_verify_' . $action . '_begin', array(), $user ); |
||
| 363 | $tracks_failure_event_name = 'jpc_verify_' . $action . '_fail'; |
||
| 364 | |||
| 365 | if ( empty( $verify_secret ) ) { |
||
| 366 | return $this->error( new Jetpack_Error( 'verify_secret_1_missing', sprintf( 'The required "%s" parameter is missing.', 'secret_1' ), 400 ), $tracks_failure_event_name, $user ); |
||
| 367 | } else if ( ! is_string( $verify_secret ) ) { |
||
| 368 | return $this->error( new Jetpack_Error( 'verify_secret_1_malformed', sprintf( 'The required "%s" parameter is malformed.', 'secret_1' ), 400 ), $tracks_failure_event_name, $user ); |
||
| 369 | } else if ( empty( $state ) ) { |
||
| 370 | return $this->error( new Jetpack_Error( 'state_missing', sprintf( 'The required "%s" parameter is missing.', 'state' ), 400 ), $tracks_failure_event_name, $user ); |
||
| 371 | } else if ( ! ctype_digit( $state ) ) { |
||
| 372 | return $this->error( new Jetpack_Error( 'state_malformed', sprintf( 'The required "%s" parameter is malformed.', 'state' ), 400 ), $tracks_failure_event_name, $user ); |
||
| 373 | } |
||
| 374 | |||
| 375 | $secrets = Jetpack::get_secrets( $action, $state ); |
||
| 376 | |||
| 377 | if ( ! $secrets ) { |
||
| 378 | Jetpack::delete_secrets( $action, $state ); |
||
| 379 | return $this->error( new Jetpack_Error( 'verify_secrets_missing', 'Verification secrets not found', 400 ), $tracks_failure_event_name, $user ); |
||
| 380 | } |
||
| 381 | |||
| 382 | if ( is_wp_error( $secrets ) ) { |
||
| 383 | Jetpack::delete_secrets( $action, $state ); |
||
| 384 | return $this->error( new Jetpack_Error( $secrets->get_error_code(), $secrets->get_error_message(), 400 ), $tracks_failure_event_name, $user ); |
||
| 385 | } |
||
| 386 | |||
| 387 | if ( empty( $secrets['secret_1'] ) || empty( $secrets['secret_2'] ) || empty( $secrets['exp'] ) ) { |
||
| 388 | Jetpack::delete_secrets( $action, $state ); |
||
| 389 | return $this->error( new Jetpack_Error( 'verify_secrets_incomplete', 'Verification secrets are incomplete', 400 ), $tracks_failure_event_name, $user ); |
||
| 390 | } |
||
| 391 | |||
| 392 | if ( ! hash_equals( $verify_secret, $secrets['secret_1'] ) ) { |
||
| 393 | Jetpack::delete_secrets( $action, $state ); |
||
| 394 | return $this->error( new Jetpack_Error( 'verify_secrets_mismatch', 'Secret mismatch', 400 ), $tracks_failure_event_name, $user ); |
||
| 395 | } |
||
| 396 | |||
| 397 | Jetpack::delete_secrets( $action, $state ); |
||
| 398 | |||
| 399 | JetpackTracking::record_user_event( 'jpc_verify_' . $action . '_success', array(), $user ); |
||
| 400 | |||
| 401 | return $secrets['secret_2']; |
||
| 402 | } |
||
| 403 | |||
| 404 | /** |
||
| 405 | * Wrapper for wp_authenticate( $username, $password ); |
||
| 406 | * |
||
| 407 | * @return WP_User|bool |
||
| 408 | */ |
||
| 409 | function login() { |
||
| 410 | Jetpack::init()->require_jetpack_authentication(); |
||
| 411 | $user = wp_authenticate( 'username', 'password' ); |
||
| 412 | if ( is_wp_error( $user ) ) { |
||
| 413 | if ( 'authentication_failed' == $user->get_error_code() ) { // Generic error could mean most anything. |
||
| 414 | $this->error = new Jetpack_Error( 'invalid_request', 'Invalid Request', 403 ); |
||
| 415 | } else { |
||
| 416 | $this->error = $user; |
||
| 417 | } |
||
| 418 | return false; |
||
| 419 | } else if ( !$user ) { // Shouldn't happen. |
||
| 420 | $this->error = new Jetpack_Error( 'invalid_request', 'Invalid Request', 403 ); |
||
| 421 | return false; |
||
| 422 | } |
||
| 423 | |||
| 424 | return $user; |
||
| 425 | } |
||
| 426 | |||
| 427 | /** |
||
| 428 | * Returns the current error as an IXR_Error |
||
| 429 | * |
||
| 430 | * @return bool|IXR_Error |
||
| 431 | */ |
||
| 432 | function error( $error = null, $tracks_event_name = null, $user = null ) { |
||
| 433 | // record using Tracks |
||
| 434 | if ( null !== $tracks_event_name ) { |
||
| 435 | $this->tracks_record_error( $tracks_event_name, $error, $user ); |
||
| 436 | } |
||
| 437 | |||
| 438 | if ( !is_null( $error ) ) { |
||
| 439 | $this->error = $error; |
||
| 440 | } |
||
| 441 | |||
| 442 | if ( is_wp_error( $this->error ) ) { |
||
| 443 | $code = $this->error->get_error_data(); |
||
| 444 | if ( !$code ) { |
||
| 445 | $code = -10520; |
||
| 446 | } |
||
| 447 | $message = sprintf( 'Jetpack: [%s] %s', $this->error->get_error_code(), $this->error->get_error_message() ); |
||
| 448 | return new IXR_Error( $code, $message ); |
||
| 449 | } else if ( is_a( $this->error, 'IXR_Error' ) ) { |
||
| 450 | return $this->error; |
||
| 451 | } |
||
| 452 | |||
| 453 | return false; |
||
| 454 | } |
||
| 455 | |||
| 456 | /* API Methods */ |
||
| 457 | |||
| 458 | /** |
||
| 459 | * Just authenticates with the given Jetpack credentials. |
||
| 460 | * |
||
| 461 | * @return string The current Jetpack version number |
||
| 462 | */ |
||
| 463 | function test_connection() { |
||
| 464 | return JETPACK__VERSION; |
||
| 465 | } |
||
| 466 | |||
| 467 | function test_api_user_code( $args ) { |
||
| 468 | $client_id = (int) $args[0]; |
||
| 469 | $user_id = (int) $args[1]; |
||
| 470 | $nonce = (string) $args[2]; |
||
| 471 | $verify = (string) $args[3]; |
||
| 472 | |||
| 473 | if ( !$client_id || !$user_id || !strlen( $nonce ) || 32 !== strlen( $verify ) ) { |
||
| 474 | return false; |
||
| 475 | } |
||
| 476 | |||
| 477 | $user = get_user_by( 'id', $user_id ); |
||
| 478 | if ( !$user || is_wp_error( $user ) ) { |
||
| 479 | return false; |
||
| 480 | } |
||
| 481 | |||
| 482 | /* debugging |
||
| 483 | error_log( "CLIENT: $client_id" ); |
||
| 484 | error_log( "USER: $user_id" ); |
||
| 485 | error_log( "NONCE: $nonce" ); |
||
| 486 | error_log( "VERIFY: $verify" ); |
||
| 487 | */ |
||
| 488 | |||
| 489 | $jetpack_token = Jetpack_Data::get_access_token( $user_id ); |
||
| 490 | |||
| 491 | $api_user_code = get_user_meta( $user_id, "jetpack_json_api_$client_id", true ); |
||
| 492 | if ( !$api_user_code ) { |
||
| 493 | return false; |
||
| 494 | } |
||
| 495 | |||
| 496 | $hmac = hash_hmac( 'md5', json_encode( (object) array( |
||
| 497 | 'client_id' => (int) $client_id, |
||
| 498 | 'user_id' => (int) $user_id, |
||
| 499 | 'nonce' => (string) $nonce, |
||
| 500 | 'code' => (string) $api_user_code, |
||
| 501 | ) ), $jetpack_token->secret ); |
||
| 502 | |||
| 503 | if ( ! hash_equals( $hmac, $verify ) ) { |
||
| 504 | return false; |
||
| 505 | } |
||
| 506 | |||
| 507 | return $user_id; |
||
| 508 | } |
||
| 509 | |||
| 510 | /** |
||
| 511 | * Disconnect this blog from the connected wordpress.com account |
||
| 512 | * @return boolean |
||
| 513 | */ |
||
| 514 | function disconnect_blog() { |
||
| 515 | |||
| 516 | // For tracking |
||
| 517 | if ( ! empty( $this->user->ID ) ) { |
||
| 518 | wp_set_current_user( $this->user->ID ); |
||
| 519 | } |
||
| 520 | |||
| 521 | Jetpack::log( 'disconnect' ); |
||
| 522 | Jetpack::disconnect(); |
||
| 523 | |||
| 524 | return true; |
||
| 525 | } |
||
| 526 | |||
| 527 | /** |
||
| 528 | * Unlink a user from WordPress.com |
||
| 529 | * |
||
| 530 | * This will fail if called by the Master User. |
||
| 531 | */ |
||
| 532 | function unlink_user() { |
||
| 533 | Jetpack::log( 'unlink' ); |
||
| 534 | return Jetpack::unlink_user(); |
||
| 535 | } |
||
| 536 | |||
| 537 | /** |
||
| 538 | * Returns any object that is able to be synced |
||
| 539 | */ |
||
| 540 | function sync_object( $args ) { |
||
| 541 | // e.g. posts, post, 5 |
||
| 542 | list( $module_name, $object_type, $id ) = $args; |
||
| 543 | require_once dirname( __FILE__ ) . '/sync/class.jetpack-sync-modules.php'; |
||
| 544 | require_once dirname( __FILE__ ) . '/sync/class.jetpack-sync-sender.php'; |
||
| 545 | |||
| 546 | $sync_module = Jetpack_Sync_Modules::get_module( $module_name ); |
||
| 547 | $codec = Jetpack_Sync_Sender::get_instance()->get_codec(); |
||
| 548 | |||
| 549 | return $codec->encode( $sync_module->get_object_by_id( $object_type, $id ) ); |
||
| 550 | } |
||
| 551 | |||
| 552 | /** |
||
| 553 | * Returns the home URL and site URL for the current site which can be used on the WPCOM side for |
||
| 554 | * IDC mitigation to decide whether sync should be allowed if the home and siteurl values differ between WPCOM |
||
| 555 | * and the remote Jetpack site. |
||
| 556 | * |
||
| 557 | * @return array |
||
| 558 | */ |
||
| 559 | function validate_urls_for_idc_mitigation() { |
||
| 560 | require_once JETPACK__PLUGIN_DIR . 'sync/class.jetpack-sync-functions.php'; |
||
| 561 | return array( |
||
| 562 | 'home' => Jetpack_Sync_Functions::home_url(), |
||
| 563 | 'siteurl' => Jetpack_Sync_Functions::site_url(), |
||
| 564 | ); |
||
| 565 | } |
||
| 566 | |||
| 567 | /** |
||
| 568 | * Returns what features are available. Uses the slug of the module files. |
||
| 569 | * |
||
| 570 | * @return array |
||
| 571 | */ |
||
| 572 | View Code Duplication | function features_available() { |
|
| 573 | $raw_modules = Jetpack::get_available_modules(); |
||
| 574 | $modules = array(); |
||
| 575 | foreach ( $raw_modules as $module ) { |
||
| 576 | $modules[] = Jetpack::get_module_slug( $module ); |
||
| 577 | } |
||
| 578 | |||
| 579 | return $modules; |
||
| 580 | } |
||
| 581 | |||
| 582 | /** |
||
| 583 | * Returns what features are enabled. Uses the slug of the modules files. |
||
| 584 | * |
||
| 585 | * @return array |
||
| 586 | */ |
||
| 587 | View Code Duplication | function features_enabled() { |
|
| 588 | $raw_modules = Jetpack::get_active_modules(); |
||
| 589 | $modules = array(); |
||
| 590 | foreach ( $raw_modules as $module ) { |
||
| 591 | $modules[] = Jetpack::get_module_slug( $module ); |
||
| 592 | } |
||
| 593 | |||
| 594 | return $modules; |
||
| 595 | } |
||
| 596 | |||
| 597 | function update_attachment_parent( $args ) { |
||
| 598 | $attachment_id = (int) $args[0]; |
||
| 599 | $parent_id = (int) $args[1]; |
||
| 600 | |||
| 601 | return wp_update_post( array( |
||
| 602 | 'ID' => $attachment_id, |
||
| 603 | 'post_parent' => $parent_id, |
||
| 604 | ) ); |
||
| 605 | } |
||
| 606 | |||
| 607 | function json_api( $args = array() ) { |
||
| 608 | $json_api_args = $args[0]; |
||
| 609 | $verify_api_user_args = $args[1]; |
||
| 610 | |||
| 611 | $method = (string) $json_api_args[0]; |
||
| 612 | $url = (string) $json_api_args[1]; |
||
| 613 | $post_body = is_null( $json_api_args[2] ) ? null : (string) $json_api_args[2]; |
||
| 614 | $user_details = (array) $json_api_args[4]; |
||
| 615 | $locale = (string) $json_api_args[5]; |
||
| 616 | |||
| 617 | if ( !$verify_api_user_args ) { |
||
| 618 | $user_id = 0; |
||
| 619 | } elseif ( 'internal' === $verify_api_user_args[0] ) { |
||
| 620 | $user_id = (int) $verify_api_user_args[1]; |
||
| 621 | if ( $user_id ) { |
||
| 622 | $user = get_user_by( 'id', $user_id ); |
||
| 623 | if ( !$user || is_wp_error( $user ) ) { |
||
| 624 | return false; |
||
| 625 | } |
||
| 626 | } |
||
| 627 | } else { |
||
| 628 | $user_id = call_user_func( array( $this, 'test_api_user_code' ), $verify_api_user_args ); |
||
| 629 | if ( !$user_id ) { |
||
| 630 | return false; |
||
| 631 | } |
||
| 632 | } |
||
| 633 | |||
| 634 | /* debugging |
||
| 635 | error_log( "-- begin json api via jetpack debugging -- " ); |
||
| 636 | error_log( "METHOD: $method" ); |
||
| 637 | error_log( "URL: $url" ); |
||
| 638 | error_log( "POST BODY: $post_body" ); |
||
| 639 | error_log( "VERIFY_ARGS: " . print_r( $verify_api_user_args, 1 ) ); |
||
| 640 | error_log( "VERIFIED USER_ID: " . (int) $user_id ); |
||
| 641 | error_log( "-- end json api via jetpack debugging -- " ); |
||
| 642 | */ |
||
| 643 | |||
| 644 | if ( 'en' !== $locale ) { |
||
| 645 | // .org mo files are named slightly different from .com, and all we have is this the locale -- try to guess them. |
||
| 646 | $new_locale = $locale; |
||
| 647 | if ( strpos( $locale, '-' ) !== false ) { |
||
| 648 | $locale_pieces = explode( '-', $locale ); |
||
| 649 | $new_locale = $locale_pieces[0]; |
||
| 650 | $new_locale .= ( ! empty( $locale_pieces[1] ) ) ? '_' . strtoupper( $locale_pieces[1] ) : ''; |
||
| 651 | } else { |
||
| 652 | // .com might pass 'fr' because thats what our language files are named as, where core seems |
||
| 653 | // to do fr_FR - so try that if we don't think we can load the file. |
||
| 654 | if ( ! file_exists( WP_LANG_DIR . '/' . $locale . '.mo' ) ) { |
||
| 655 | $new_locale = $locale . '_' . strtoupper( $locale ); |
||
| 656 | } |
||
| 657 | } |
||
| 658 | |||
| 659 | if ( file_exists( WP_LANG_DIR . '/' . $new_locale . '.mo' ) ) { |
||
| 660 | unload_textdomain( 'default' ); |
||
| 661 | load_textdomain( 'default', WP_LANG_DIR . '/' . $new_locale . '.mo' ); |
||
| 662 | } |
||
| 663 | } |
||
| 664 | |||
| 665 | $old_user = wp_get_current_user(); |
||
| 666 | wp_set_current_user( $user_id ); |
||
| 667 | |||
| 668 | $token = Jetpack_Data::get_access_token( get_current_user_id() ); |
||
| 669 | if ( !$token || is_wp_error( $token ) ) { |
||
| 670 | return false; |
||
| 671 | } |
||
| 672 | |||
| 673 | define( 'REST_API_REQUEST', true ); |
||
| 674 | define( 'WPCOM_JSON_API__BASE', 'public-api.wordpress.com/rest/v1' ); |
||
| 675 | |||
| 676 | // needed? |
||
| 677 | require_once ABSPATH . 'wp-admin/includes/admin.php'; |
||
| 678 | |||
| 679 | require_once JETPACK__PLUGIN_DIR . 'class.json-api.php'; |
||
| 680 | $api = WPCOM_JSON_API::init( $method, $url, $post_body ); |
||
| 681 | $api->token_details['user'] = $user_details; |
||
| 682 | require_once JETPACK__PLUGIN_DIR . 'class.json-api-endpoints.php'; |
||
| 683 | |||
| 684 | $display_errors = ini_set( 'display_errors', 0 ); |
||
| 685 | ob_start(); |
||
| 686 | $content_type = $api->serve( false ); |
||
| 687 | $output = ob_get_clean(); |
||
| 688 | ini_set( 'display_errors', $display_errors ); |
||
| 689 | |||
| 690 | $nonce = wp_generate_password( 10, false ); |
||
| 691 | $hmac = hash_hmac( 'md5', $nonce . $output, $token->secret ); |
||
| 692 | |||
| 693 | wp_set_current_user( isset( $old_user->ID ) ? $old_user->ID : 0 ); |
||
| 694 | |||
| 695 | return array( |
||
| 696 | (string) $output, |
||
| 697 | (string) $nonce, |
||
| 698 | (string) $hmac, |
||
| 699 | ); |
||
| 700 | } |
||
| 701 | } |
||
| 702 |