1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* The Jetpack Connection manager class file. |
4
|
|
|
* |
5
|
|
|
* @package automattic/jetpack-connection |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
namespace Automattic\Jetpack\Connection; |
9
|
|
|
|
10
|
|
|
use Automattic\Jetpack\Constants; |
11
|
|
|
use Automattic\Jetpack\Roles; |
12
|
|
|
use Automattic\Jetpack\Status; |
13
|
|
|
use Automattic\Jetpack\Tracking; |
14
|
|
|
use WP_Error; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* The Jetpack Connection Manager class that is used as a single gateway between WordPress.com |
18
|
|
|
* and Jetpack. |
19
|
|
|
*/ |
20
|
|
|
class Manager { |
21
|
|
|
|
22
|
|
|
const SECRETS_MISSING = 'secrets_missing'; |
23
|
|
|
const SECRETS_EXPIRED = 'secrets_expired'; |
24
|
|
|
const SECRETS_OPTION_NAME = 'jetpack_secrets'; |
25
|
|
|
const MAGIC_NORMAL_TOKEN_KEY = ';normal;'; |
26
|
|
|
const JETPACK_MASTER_USER = true; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* The procedure that should be run to generate secrets. |
30
|
|
|
* |
31
|
|
|
* @var Callable |
32
|
|
|
*/ |
33
|
|
|
protected $secret_callable; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* A copy of the raw POST data for signature verification purposes. |
37
|
|
|
* |
38
|
|
|
* @var String |
39
|
|
|
*/ |
40
|
|
|
protected $raw_post_data; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Verification data needs to be stored to properly verify everything. |
44
|
|
|
* |
45
|
|
|
* @var Object |
46
|
|
|
*/ |
47
|
|
|
private $xmlrpc_verification = null; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Plugin management object. |
51
|
|
|
* |
52
|
|
|
* @var Plugin |
53
|
|
|
*/ |
54
|
|
|
private $plugin = null; |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Initialize the object. |
58
|
|
|
* Make sure to call the "Configure" first. |
59
|
|
|
* |
60
|
|
|
* @param string $plugin_slug Slug of the plugin using the connection (optional, but encouraged). |
|
|
|
|
61
|
|
|
* |
62
|
|
|
* @see \Automattic\Jetpack\Config |
63
|
|
|
*/ |
64
|
|
|
public function __construct( $plugin_slug = null ) { |
65
|
|
|
if ( $plugin_slug && is_string( $plugin_slug ) ) { |
|
|
|
|
66
|
|
|
$this->set_plugin_instance( new Plugin( $plugin_slug ) ); |
67
|
|
|
} |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Initializes required listeners. This is done separately from the constructors |
72
|
|
|
* because some objects sometimes need to instantiate separate objects of this class. |
73
|
|
|
* |
74
|
|
|
* @todo Implement a proper nonce verification. |
75
|
|
|
*/ |
76
|
|
|
public static function configure() { |
77
|
|
|
$manager = new self(); |
78
|
|
|
|
79
|
|
|
add_filter( |
80
|
|
|
'jetpack_constant_default_value', |
81
|
|
|
__NAMESPACE__ . '\Utils::jetpack_api_constant_filter', |
82
|
|
|
10, |
83
|
|
|
2 |
84
|
|
|
); |
85
|
|
|
|
86
|
|
|
$manager->setup_xmlrpc_handlers( |
87
|
|
|
$_GET, // phpcs:ignore WordPress.Security.NonceVerification.Recommended |
88
|
|
|
$manager->is_active(), |
89
|
|
|
$manager->verify_xml_rpc_signature() |
|
|
|
|
90
|
|
|
); |
91
|
|
|
|
92
|
|
|
$manager->error_handler = Error_Handler::get_instance(); |
|
|
|
|
93
|
|
|
|
94
|
|
|
if ( $manager->is_active() ) { |
95
|
|
|
add_filter( 'xmlrpc_methods', array( $manager, 'public_xmlrpc_methods' ) ); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
add_action( 'rest_api_init', array( $manager, 'initialize_rest_api_registration_connector' ) ); |
99
|
|
|
|
100
|
|
|
add_action( 'jetpack_clean_nonces', array( $manager, 'clean_nonces' ) ); |
101
|
|
|
if ( ! wp_next_scheduled( 'jetpack_clean_nonces' ) ) { |
102
|
|
|
wp_schedule_event( time(), 'hourly', 'jetpack_clean_nonces' ); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
add_action( 'plugins_loaded', __NAMESPACE__ . '\Plugin_Storage::configure', 100 ); |
106
|
|
|
|
107
|
|
|
add_filter( 'map_meta_cap', array( $manager, 'jetpack_connection_custom_caps' ), 1, 4 ); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* Sets up the XMLRPC request handlers. |
112
|
|
|
* |
113
|
|
|
* @param array $request_params incoming request parameters. |
114
|
|
|
* @param Boolean $is_active whether the connection is currently active. |
115
|
|
|
* @param Boolean $is_signed whether the signature check has been successful. |
116
|
|
|
* @param \Jetpack_XMLRPC_Server $xmlrpc_server (optional) an instance of the server to use instead of instantiating a new one. |
|
|
|
|
117
|
|
|
*/ |
118
|
|
|
public function setup_xmlrpc_handlers( |
119
|
|
|
$request_params, |
120
|
|
|
$is_active, |
121
|
|
|
$is_signed, |
122
|
|
|
\Jetpack_XMLRPC_Server $xmlrpc_server = null |
123
|
|
|
) { |
124
|
|
|
add_filter( 'xmlrpc_blog_options', array( $this, 'xmlrpc_options' ), 1000, 2 ); |
125
|
|
|
|
126
|
|
|
if ( |
127
|
|
|
! isset( $request_params['for'] ) |
128
|
|
|
|| 'jetpack' !== $request_params['for'] |
129
|
|
|
) { |
130
|
|
|
return false; |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
// Alternate XML-RPC, via ?for=jetpack&jetpack=comms. |
134
|
|
|
if ( |
135
|
|
|
isset( $request_params['jetpack'] ) |
136
|
|
|
&& 'comms' === $request_params['jetpack'] |
137
|
|
|
) { |
138
|
|
|
if ( ! Constants::is_defined( 'XMLRPC_REQUEST' ) ) { |
139
|
|
|
// Use the real constant here for WordPress' sake. |
140
|
|
|
define( 'XMLRPC_REQUEST', true ); |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
add_action( 'template_redirect', array( $this, 'alternate_xmlrpc' ) ); |
144
|
|
|
|
145
|
|
|
add_filter( 'xmlrpc_methods', array( $this, 'remove_non_jetpack_xmlrpc_methods' ), 1000 ); |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
if ( ! Constants::get_constant( 'XMLRPC_REQUEST' ) ) { |
149
|
|
|
return false; |
150
|
|
|
} |
151
|
|
|
// Display errors can cause the XML to be not well formed. |
152
|
|
|
@ini_set( 'display_errors', false ); // phpcs:ignore |
|
|
|
|
153
|
|
|
|
154
|
|
|
if ( $xmlrpc_server ) { |
155
|
|
|
$this->xmlrpc_server = $xmlrpc_server; |
|
|
|
|
156
|
|
|
} else { |
157
|
|
|
$this->xmlrpc_server = new \Jetpack_XMLRPC_Server(); |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
$this->require_jetpack_authentication(); |
161
|
|
|
|
162
|
|
|
if ( $is_active ) { |
163
|
|
|
// Hack to preserve $HTTP_RAW_POST_DATA. |
164
|
|
|
add_filter( 'xmlrpc_methods', array( $this, 'xmlrpc_methods' ) ); |
165
|
|
|
|
166
|
|
|
if ( $is_signed ) { |
167
|
|
|
// The actual API methods. |
168
|
|
|
add_filter( 'xmlrpc_methods', array( $this->xmlrpc_server, 'xmlrpc_methods' ) ); |
169
|
|
|
} else { |
170
|
|
|
// The jetpack.authorize method should be available for unauthenticated users on a site with an |
171
|
|
|
// active Jetpack connection, so that additional users can link their account. |
172
|
|
|
add_filter( 'xmlrpc_methods', array( $this->xmlrpc_server, 'authorize_xmlrpc_methods' ) ); |
173
|
|
|
} |
174
|
|
|
} else { |
175
|
|
|
// The bootstrap API methods. |
176
|
|
|
add_filter( 'xmlrpc_methods', array( $this->xmlrpc_server, 'bootstrap_xmlrpc_methods' ) ); |
177
|
|
|
|
178
|
|
|
if ( $is_signed ) { |
179
|
|
|
// The jetpack Provision method is available for blog-token-signed requests. |
180
|
|
|
add_filter( 'xmlrpc_methods', array( $this->xmlrpc_server, 'provision_xmlrpc_methods' ) ); |
181
|
|
|
} else { |
182
|
|
|
new XMLRPC_Connector( $this ); |
183
|
|
|
} |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
// Now that no one can authenticate, and we're whitelisting all XML-RPC methods, force enable_xmlrpc on. |
187
|
|
|
add_filter( 'pre_option_enable_xmlrpc', '__return_true' ); |
188
|
|
|
return true; |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
/** |
192
|
|
|
* Initializes the REST API connector on the init hook. |
193
|
|
|
*/ |
194
|
|
|
public function initialize_rest_api_registration_connector() { |
195
|
|
|
new REST_Connector( $this ); |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
/** |
199
|
|
|
* Since a lot of hosts use a hammer approach to "protecting" WordPress sites, |
200
|
|
|
* and just blanket block all requests to /xmlrpc.php, or apply other overly-sensitive |
201
|
|
|
* security/firewall policies, we provide our own alternate XML RPC API endpoint |
202
|
|
|
* which is accessible via a different URI. Most of the below is copied directly |
203
|
|
|
* from /xmlrpc.php so that we're replicating it as closely as possible. |
204
|
|
|
* |
205
|
|
|
* @todo Tighten $wp_xmlrpc_server_class a bit to make sure it doesn't do bad things. |
206
|
|
|
*/ |
207
|
|
|
public function alternate_xmlrpc() { |
208
|
|
|
// phpcs:disable PHPCompatibility.Variables.RemovedPredefinedGlobalVariables.http_raw_post_dataDeprecatedRemoved |
209
|
|
|
// phpcs:disable WordPress.WP.GlobalVariablesOverride.Prohibited |
210
|
|
|
global $HTTP_RAW_POST_DATA; |
211
|
|
|
|
212
|
|
|
// Some browser-embedded clients send cookies. We don't want them. |
213
|
|
|
$_COOKIE = array(); |
214
|
|
|
|
215
|
|
|
// A fix for mozBlog and other cases where '<?xml' isn't on the very first line. |
216
|
|
|
if ( isset( $HTTP_RAW_POST_DATA ) ) { |
217
|
|
|
$HTTP_RAW_POST_DATA = trim( $HTTP_RAW_POST_DATA ); |
218
|
|
|
} |
219
|
|
|
|
220
|
|
|
// phpcs:enable |
221
|
|
|
|
222
|
|
|
include_once ABSPATH . 'wp-admin/includes/admin.php'; |
223
|
|
|
include_once ABSPATH . WPINC . '/class-IXR.php'; |
224
|
|
|
include_once ABSPATH . WPINC . '/class-wp-xmlrpc-server.php'; |
225
|
|
|
|
226
|
|
|
/** |
227
|
|
|
* Filters the class used for handling XML-RPC requests. |
228
|
|
|
* |
229
|
|
|
* @since 3.1.0 |
230
|
|
|
* |
231
|
|
|
* @param string $class The name of the XML-RPC server class. |
232
|
|
|
*/ |
233
|
|
|
$wp_xmlrpc_server_class = apply_filters( 'wp_xmlrpc_server_class', 'wp_xmlrpc_server' ); |
234
|
|
|
$wp_xmlrpc_server = new $wp_xmlrpc_server_class(); |
235
|
|
|
|
236
|
|
|
// Fire off the request. |
237
|
|
|
nocache_headers(); |
238
|
|
|
$wp_xmlrpc_server->serve_request(); |
239
|
|
|
|
240
|
|
|
exit; |
241
|
|
|
} |
242
|
|
|
|
243
|
|
|
/** |
244
|
|
|
* Removes all XML-RPC methods that are not `jetpack.*`. |
245
|
|
|
* Only used in our alternate XML-RPC endpoint, where we want to |
246
|
|
|
* ensure that Core and other plugins' methods are not exposed. |
247
|
|
|
* |
248
|
|
|
* @param array $methods a list of registered WordPress XMLRPC methods. |
249
|
|
|
* @return array filtered $methods |
250
|
|
|
*/ |
251
|
|
|
public function remove_non_jetpack_xmlrpc_methods( $methods ) { |
252
|
|
|
$jetpack_methods = array(); |
253
|
|
|
|
254
|
|
|
foreach ( $methods as $method => $callback ) { |
255
|
|
|
if ( 0 === strpos( $method, 'jetpack.' ) ) { |
256
|
|
|
$jetpack_methods[ $method ] = $callback; |
257
|
|
|
} |
258
|
|
|
} |
259
|
|
|
|
260
|
|
|
return $jetpack_methods; |
261
|
|
|
} |
262
|
|
|
|
263
|
|
|
/** |
264
|
|
|
* Removes all other authentication methods not to allow other |
265
|
|
|
* methods to validate unauthenticated requests. |
266
|
|
|
*/ |
267
|
|
|
public function require_jetpack_authentication() { |
268
|
|
|
// Don't let anyone authenticate. |
269
|
|
|
$_COOKIE = array(); |
270
|
|
|
remove_all_filters( 'authenticate' ); |
271
|
|
|
remove_all_actions( 'wp_login_failed' ); |
272
|
|
|
|
273
|
|
|
if ( $this->is_active() ) { |
274
|
|
|
// Allow Jetpack authentication. |
275
|
|
|
add_filter( 'authenticate', array( $this, 'authenticate_jetpack' ), 10, 3 ); |
276
|
|
|
} |
277
|
|
|
} |
278
|
|
|
|
279
|
|
|
/** |
280
|
|
|
* Authenticates XML-RPC and other requests from the Jetpack Server |
281
|
|
|
* |
282
|
|
|
* @param WP_User|Mixed $user user object if authenticated. |
283
|
|
|
* @param String $username username. |
284
|
|
|
* @param String $password password string. |
285
|
|
|
* @return WP_User|Mixed authenticated user or error. |
286
|
|
|
*/ |
287
|
|
|
public function authenticate_jetpack( $user, $username, $password ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable |
288
|
|
|
if ( is_a( $user, '\\WP_User' ) ) { |
289
|
|
|
return $user; |
290
|
|
|
} |
291
|
|
|
|
292
|
|
|
$token_details = $this->verify_xml_rpc_signature(); |
293
|
|
|
|
294
|
|
|
if ( ! $token_details ) { |
295
|
|
|
return $user; |
296
|
|
|
} |
297
|
|
|
|
298
|
|
|
if ( 'user' !== $token_details['type'] ) { |
299
|
|
|
return $user; |
300
|
|
|
} |
301
|
|
|
|
302
|
|
|
if ( ! $token_details['user_id'] ) { |
303
|
|
|
return $user; |
304
|
|
|
} |
305
|
|
|
|
306
|
|
|
nocache_headers(); |
307
|
|
|
|
308
|
|
|
return new \WP_User( $token_details['user_id'] ); |
309
|
|
|
} |
310
|
|
|
|
311
|
|
|
/** |
312
|
|
|
* Verifies the signature of the current request. |
313
|
|
|
* |
314
|
|
|
* @return false|array |
315
|
|
|
*/ |
316
|
|
|
public function verify_xml_rpc_signature() { |
317
|
|
|
if ( is_null( $this->xmlrpc_verification ) ) { |
318
|
|
|
$this->xmlrpc_verification = $this->internal_verify_xml_rpc_signature(); |
319
|
|
|
|
320
|
|
|
if ( is_wp_error( $this->xmlrpc_verification ) ) { |
321
|
|
|
/** |
322
|
|
|
* Action for logging XMLRPC signature verification errors. This data is sensitive. |
323
|
|
|
* |
324
|
|
|
* @since 7.5.0 |
325
|
|
|
* |
326
|
|
|
* @param WP_Error $signature_verification_error The verification error |
327
|
|
|
*/ |
328
|
|
|
do_action( 'jetpack_verify_signature_error', $this->xmlrpc_verification ); |
329
|
|
|
|
330
|
|
|
Error_Handler::get_instance()->report_error( $this->xmlrpc_verification ); |
331
|
|
|
|
332
|
|
|
} |
333
|
|
|
} |
334
|
|
|
|
335
|
|
|
return is_wp_error( $this->xmlrpc_verification ) ? false : $this->xmlrpc_verification; |
336
|
|
|
} |
337
|
|
|
|
338
|
|
|
/** |
339
|
|
|
* Verifies the signature of the current request. |
340
|
|
|
* |
341
|
|
|
* This function has side effects and should not be used. Instead, |
342
|
|
|
* use the memoized version `->verify_xml_rpc_signature()`. |
343
|
|
|
* |
344
|
|
|
* @internal |
345
|
|
|
* @todo Refactor to use proper nonce verification. |
346
|
|
|
*/ |
347
|
|
|
private function internal_verify_xml_rpc_signature() { |
348
|
|
|
// phpcs:disable WordPress.Security.NonceVerification.Recommended |
349
|
|
|
// It's not for us. |
350
|
|
|
if ( ! isset( $_GET['token'] ) || empty( $_GET['signature'] ) ) { |
351
|
|
|
return false; |
352
|
|
|
} |
353
|
|
|
|
354
|
|
|
$signature_details = array( |
355
|
|
|
'token' => isset( $_GET['token'] ) ? wp_unslash( $_GET['token'] ) : '', |
356
|
|
|
'timestamp' => isset( $_GET['timestamp'] ) ? wp_unslash( $_GET['timestamp'] ) : '', |
357
|
|
|
'nonce' => isset( $_GET['nonce'] ) ? wp_unslash( $_GET['nonce'] ) : '', |
358
|
|
|
'body_hash' => isset( $_GET['body-hash'] ) ? wp_unslash( $_GET['body-hash'] ) : '', |
359
|
|
|
'method' => wp_unslash( $_SERVER['REQUEST_METHOD'] ), |
360
|
|
|
'url' => wp_unslash( $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'] ), // Temp - will get real signature URL later. |
361
|
|
|
'signature' => isset( $_GET['signature'] ) ? wp_unslash( $_GET['signature'] ) : '', |
362
|
|
|
); |
363
|
|
|
|
364
|
|
|
// phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged |
365
|
|
|
@list( $token_key, $version, $user_id ) = explode( ':', wp_unslash( $_GET['token'] ) ); |
|
|
|
|
366
|
|
|
// phpcs:enable WordPress.Security.NonceVerification.Recommended |
367
|
|
|
|
368
|
|
|
$jetpack_api_version = Constants::get_constant( 'JETPACK__API_VERSION' ); |
369
|
|
|
|
370
|
|
|
if ( |
371
|
|
|
empty( $token_key ) |
372
|
|
|
|| |
373
|
|
|
empty( $version ) || strval( $jetpack_api_version ) !== $version ) { |
374
|
|
|
return new \WP_Error( 'malformed_token', 'Malformed token in request', compact( 'signature_details' ) ); |
|
|
|
|
375
|
|
|
} |
376
|
|
|
|
377
|
|
|
if ( '0' === $user_id ) { |
378
|
|
|
$token_type = 'blog'; |
379
|
|
|
$user_id = 0; |
380
|
|
|
} else { |
381
|
|
|
$token_type = 'user'; |
382
|
|
|
if ( empty( $user_id ) || ! ctype_digit( $user_id ) ) { |
383
|
|
|
return new \WP_Error( |
384
|
|
|
'malformed_user_id', |
|
|
|
|
385
|
|
|
'Malformed user_id in request', |
386
|
|
|
compact( 'signature_details' ) |
387
|
|
|
); |
388
|
|
|
} |
389
|
|
|
$user_id = (int) $user_id; |
390
|
|
|
|
391
|
|
|
$user = new \WP_User( $user_id ); |
392
|
|
|
if ( ! $user || ! $user->exists() ) { |
393
|
|
|
return new \WP_Error( |
394
|
|
|
'unknown_user', |
|
|
|
|
395
|
|
|
sprintf( 'User %d does not exist', $user_id ), |
396
|
|
|
compact( 'signature_details' ) |
397
|
|
|
); |
398
|
|
|
} |
399
|
|
|
} |
400
|
|
|
|
401
|
|
|
$token = $this->get_access_token( $user_id, $token_key, false ); |
402
|
|
|
if ( is_wp_error( $token ) ) { |
403
|
|
|
$token->add_data( compact( 'signature_details' ) ); |
404
|
|
|
return $token; |
405
|
|
|
} elseif ( ! $token ) { |
406
|
|
|
return new \WP_Error( |
407
|
|
|
'unknown_token', |
|
|
|
|
408
|
|
|
sprintf( 'Token %s:%s:%d does not exist', $token_key, $version, $user_id ), |
409
|
|
|
compact( 'signature_details' ) |
410
|
|
|
); |
411
|
|
|
} |
412
|
|
|
|
413
|
|
|
$jetpack_signature = new \Jetpack_Signature( $token->secret, (int) \Jetpack_Options::get_option( 'time_diff' ) ); |
414
|
|
|
// phpcs:disable WordPress.Security.NonceVerification.Missing |
415
|
|
|
if ( isset( $_POST['_jetpack_is_multipart'] ) ) { |
416
|
|
|
$post_data = $_POST; |
417
|
|
|
$file_hashes = array(); |
418
|
|
|
foreach ( $post_data as $post_data_key => $post_data_value ) { |
419
|
|
|
if ( 0 !== strpos( $post_data_key, '_jetpack_file_hmac_' ) ) { |
420
|
|
|
continue; |
421
|
|
|
} |
422
|
|
|
$post_data_key = substr( $post_data_key, strlen( '_jetpack_file_hmac_' ) ); |
423
|
|
|
$file_hashes[ $post_data_key ] = $post_data_value; |
424
|
|
|
} |
425
|
|
|
|
426
|
|
|
foreach ( $file_hashes as $post_data_key => $post_data_value ) { |
427
|
|
|
unset( $post_data[ "_jetpack_file_hmac_{$post_data_key}" ] ); |
428
|
|
|
$post_data[ $post_data_key ] = $post_data_value; |
429
|
|
|
} |
430
|
|
|
|
431
|
|
|
ksort( $post_data ); |
432
|
|
|
|
433
|
|
|
$body = http_build_query( stripslashes_deep( $post_data ) ); |
434
|
|
|
} elseif ( is_null( $this->raw_post_data ) ) { |
435
|
|
|
$body = file_get_contents( 'php://input' ); |
436
|
|
|
} else { |
437
|
|
|
$body = null; |
438
|
|
|
} |
439
|
|
|
// phpcs:enable |
440
|
|
|
|
441
|
|
|
$signature = $jetpack_signature->sign_current_request( |
442
|
|
|
array( 'body' => is_null( $body ) ? $this->raw_post_data : $body ) |
443
|
|
|
); |
444
|
|
|
|
445
|
|
|
$signature_details['url'] = $jetpack_signature->current_request_url; |
446
|
|
|
|
447
|
|
|
if ( ! $signature ) { |
448
|
|
|
return new \WP_Error( |
449
|
|
|
'could_not_sign', |
|
|
|
|
450
|
|
|
'Unknown signature error', |
451
|
|
|
compact( 'signature_details' ) |
452
|
|
|
); |
453
|
|
|
} elseif ( is_wp_error( $signature ) ) { |
454
|
|
|
return $signature; |
455
|
|
|
} |
456
|
|
|
|
457
|
|
|
// phpcs:disable WordPress.Security.NonceVerification.Recommended |
458
|
|
|
$timestamp = (int) $_GET['timestamp']; |
459
|
|
|
$nonce = stripslashes( (string) $_GET['nonce'] ); |
460
|
|
|
// phpcs:enable WordPress.Security.NonceVerification.Recommended |
461
|
|
|
|
462
|
|
|
// Use up the nonce regardless of whether the signature matches. |
463
|
|
|
if ( ! $this->add_nonce( $timestamp, $nonce ) ) { |
464
|
|
|
return new \WP_Error( |
465
|
|
|
'invalid_nonce', |
|
|
|
|
466
|
|
|
'Could not add nonce', |
467
|
|
|
compact( 'signature_details' ) |
468
|
|
|
); |
469
|
|
|
} |
470
|
|
|
|
471
|
|
|
// Be careful about what you do with this debugging data. |
472
|
|
|
// If a malicious requester has access to the expected signature, |
473
|
|
|
// bad things might be possible. |
474
|
|
|
$signature_details['expected'] = $signature; |
475
|
|
|
|
476
|
|
|
// phpcs:ignore WordPress.Security.NonceVerification.Recommended |
477
|
|
|
if ( ! hash_equals( $signature, $_GET['signature'] ) ) { |
478
|
|
|
return new \WP_Error( |
479
|
|
|
'signature_mismatch', |
|
|
|
|
480
|
|
|
'Signature mismatch', |
481
|
|
|
compact( 'signature_details' ) |
482
|
|
|
); |
483
|
|
|
} |
484
|
|
|
|
485
|
|
|
/** |
486
|
|
|
* Action for additional token checking. |
487
|
|
|
* |
488
|
|
|
* @since 7.7.0 |
489
|
|
|
* |
490
|
|
|
* @param array $post_data request data. |
491
|
|
|
* @param array $token_data token data. |
492
|
|
|
*/ |
493
|
|
|
return apply_filters( |
494
|
|
|
'jetpack_signature_check_token', |
495
|
|
|
array( |
496
|
|
|
'type' => $token_type, |
497
|
|
|
'token_key' => $token_key, |
498
|
|
|
'user_id' => $token->external_user_id, |
499
|
|
|
), |
500
|
|
|
$token, |
|
|
|
|
501
|
|
|
$this->raw_post_data |
502
|
|
|
); |
503
|
|
|
} |
504
|
|
|
|
505
|
|
|
/** |
506
|
|
|
* Returns true if the current site is connected to WordPress.com. |
507
|
|
|
* |
508
|
|
|
* @return Boolean is the site connected? |
509
|
|
|
*/ |
510
|
|
|
public function is_active() { |
511
|
|
|
return (bool) $this->get_access_token( self::JETPACK_MASTER_USER ); |
|
|
|
|
512
|
|
|
} |
513
|
|
|
|
514
|
|
|
/** |
515
|
|
|
* Returns true if the site has both a token and a blog id, which indicates a site has been registered. |
516
|
|
|
* |
517
|
|
|
* @access public |
518
|
|
|
* |
519
|
|
|
* @return bool |
520
|
|
|
*/ |
521
|
|
|
public function is_registered() { |
522
|
|
|
$has_blog_id = (bool) \Jetpack_Options::get_option( 'id' ); |
523
|
|
|
$has_blog_token = (bool) $this->get_access_token( false ); |
524
|
|
|
return $has_blog_id && $has_blog_token; |
525
|
|
|
} |
526
|
|
|
|
527
|
|
|
/** |
528
|
|
|
* Checks to see if the connection owner of the site is missing. |
529
|
|
|
* |
530
|
|
|
* @return bool |
531
|
|
|
*/ |
532
|
|
|
public function is_missing_connection_owner() { |
533
|
|
|
$connection_owner = $this->get_connection_owner_id(); |
534
|
|
|
if ( ! get_user_by( 'id', $connection_owner ) ) { |
535
|
|
|
return true; |
536
|
|
|
} |
537
|
|
|
|
538
|
|
|
return false; |
539
|
|
|
} |
540
|
|
|
|
541
|
|
|
/** |
542
|
|
|
* Returns true if the user with the specified identifier is connected to |
543
|
|
|
* WordPress.com. |
544
|
|
|
* |
545
|
|
|
* @param Integer|Boolean $user_id the user identifier. |
546
|
|
|
* @return Boolean is the user connected? |
547
|
|
|
*/ |
548
|
|
|
public function is_user_connected( $user_id = false ) { |
549
|
|
|
$user_id = false === $user_id ? get_current_user_id() : absint( $user_id ); |
550
|
|
|
if ( ! $user_id ) { |
551
|
|
|
return false; |
552
|
|
|
} |
553
|
|
|
|
554
|
|
|
return (bool) $this->get_access_token( $user_id ); |
555
|
|
|
} |
556
|
|
|
|
557
|
|
|
/** |
558
|
|
|
* Returns the local user ID of the connection owner. |
559
|
|
|
* |
560
|
|
|
* @return string|int Returns the ID of the connection owner or False if no connection owner found. |
561
|
|
|
*/ |
562
|
|
View Code Duplication |
public function get_connection_owner_id() { |
563
|
|
|
$user_token = $this->get_access_token( self::JETPACK_MASTER_USER ); |
|
|
|
|
564
|
|
|
$connection_owner = false; |
565
|
|
|
if ( $user_token && is_object( $user_token ) && isset( $user_token->external_user_id ) ) { |
566
|
|
|
$connection_owner = $user_token->external_user_id; |
567
|
|
|
} |
568
|
|
|
|
569
|
|
|
return $connection_owner; |
570
|
|
|
} |
571
|
|
|
|
572
|
|
|
/** |
573
|
|
|
* Returns an array of user_id's that have user tokens for communicating with wpcom. |
574
|
|
|
* Able to select by specific capability. |
575
|
|
|
* |
576
|
|
|
* @param string $capability The capability of the user. |
577
|
|
|
* @return array Array of WP_User objects if found. |
578
|
|
|
*/ |
579
|
|
|
public function get_connected_users( $capability = 'any' ) { |
580
|
|
|
$connected_users = array(); |
581
|
|
|
$connected_user_ids = array_keys( \Jetpack_Options::get_option( 'user_tokens' ) ); |
582
|
|
|
|
583
|
|
|
if ( ! empty( $connected_user_ids ) ) { |
584
|
|
|
foreach ( $connected_user_ids as $id ) { |
585
|
|
|
// Check for capability. |
586
|
|
|
if ( 'any' !== $capability && ! user_can( $id, $capability ) ) { |
587
|
|
|
continue; |
588
|
|
|
} |
589
|
|
|
|
590
|
|
|
$connected_users[] = get_userdata( $id ); |
591
|
|
|
} |
592
|
|
|
} |
593
|
|
|
|
594
|
|
|
return $connected_users; |
595
|
|
|
} |
596
|
|
|
|
597
|
|
|
/** |
598
|
|
|
* Get the wpcom user data of the current|specified connected user. |
599
|
|
|
* |
600
|
|
|
* @todo Refactor to properly load the XMLRPC client independently. |
601
|
|
|
* |
602
|
|
|
* @param Integer $user_id the user identifier. |
|
|
|
|
603
|
|
|
* @return Object the user object. |
604
|
|
|
*/ |
605
|
|
View Code Duplication |
public function get_connected_user_data( $user_id = null ) { |
606
|
|
|
if ( ! $user_id ) { |
|
|
|
|
607
|
|
|
$user_id = get_current_user_id(); |
608
|
|
|
} |
609
|
|
|
|
610
|
|
|
$transient_key = "jetpack_connected_user_data_$user_id"; |
611
|
|
|
$cached_user_data = get_transient( $transient_key ); |
612
|
|
|
|
613
|
|
|
if ( $cached_user_data ) { |
614
|
|
|
return $cached_user_data; |
615
|
|
|
} |
616
|
|
|
|
617
|
|
|
$xml = new \Jetpack_IXR_Client( |
618
|
|
|
array( |
619
|
|
|
'user_id' => $user_id, |
620
|
|
|
) |
621
|
|
|
); |
622
|
|
|
$xml->query( 'wpcom.getUser' ); |
623
|
|
|
if ( ! $xml->isError() ) { |
624
|
|
|
$user_data = $xml->getResponse(); |
625
|
|
|
set_transient( $transient_key, $xml->getResponse(), DAY_IN_SECONDS ); |
626
|
|
|
return $user_data; |
627
|
|
|
} |
628
|
|
|
|
629
|
|
|
return false; |
630
|
|
|
} |
631
|
|
|
|
632
|
|
|
/** |
633
|
|
|
* Returns a user object of the connection owner. |
634
|
|
|
* |
635
|
|
|
* @return object|false False if no connection owner found. |
636
|
|
|
*/ |
637
|
|
View Code Duplication |
public function get_connection_owner() { |
638
|
|
|
$user_token = $this->get_access_token( self::JETPACK_MASTER_USER ); |
|
|
|
|
639
|
|
|
|
640
|
|
|
$connection_owner = false; |
641
|
|
|
if ( $user_token && is_object( $user_token ) && isset( $user_token->external_user_id ) ) { |
642
|
|
|
$connection_owner = get_userdata( $user_token->external_user_id ); |
643
|
|
|
} |
644
|
|
|
|
645
|
|
|
return $connection_owner; |
646
|
|
|
} |
647
|
|
|
|
648
|
|
|
/** |
649
|
|
|
* Returns true if the provided user is the Jetpack connection owner. |
650
|
|
|
* If user ID is not specified, the current user will be used. |
651
|
|
|
* |
652
|
|
|
* @param Integer|Boolean $user_id the user identifier. False for current user. |
653
|
|
|
* @return Boolean True the user the connection owner, false otherwise. |
654
|
|
|
*/ |
655
|
|
View Code Duplication |
public function is_connection_owner( $user_id = false ) { |
656
|
|
|
if ( ! $user_id ) { |
657
|
|
|
$user_id = get_current_user_id(); |
658
|
|
|
} |
659
|
|
|
|
660
|
|
|
$user_token = $this->get_access_token( self::JETPACK_MASTER_USER ); |
|
|
|
|
661
|
|
|
|
662
|
|
|
return $user_token && is_object( $user_token ) && isset( $user_token->external_user_id ) && $user_id === $user_token->external_user_id; |
663
|
|
|
} |
664
|
|
|
|
665
|
|
|
/** |
666
|
|
|
* Connects the user with a specified ID to a WordPress.com user using the |
667
|
|
|
* remote login flow. |
668
|
|
|
* |
669
|
|
|
* @access public |
670
|
|
|
* |
671
|
|
|
* @param Integer $user_id (optional) the user identifier, defaults to current user. |
|
|
|
|
672
|
|
|
* @param String $redirect_url the URL to redirect the user to for processing, defaults to |
|
|
|
|
673
|
|
|
* admin_url(). |
674
|
|
|
* @return WP_Error only in case of a failed user lookup. |
675
|
|
|
*/ |
676
|
|
|
public function connect_user( $user_id = null, $redirect_url = null ) { |
677
|
|
|
$user = null; |
|
|
|
|
678
|
|
|
if ( null === $user_id ) { |
679
|
|
|
$user = wp_get_current_user(); |
680
|
|
|
} else { |
681
|
|
|
$user = get_user_by( 'ID', $user_id ); |
682
|
|
|
} |
683
|
|
|
|
684
|
|
|
if ( empty( $user ) ) { |
685
|
|
|
return new \WP_Error( 'user_not_found', 'Attempting to connect a non-existent user.' ); |
|
|
|
|
686
|
|
|
} |
687
|
|
|
|
688
|
|
|
if ( null === $redirect_url ) { |
689
|
|
|
$redirect_url = admin_url(); |
|
|
|
|
690
|
|
|
} |
691
|
|
|
|
692
|
|
|
// Using wp_redirect intentionally because we're redirecting outside. |
693
|
|
|
wp_redirect( $this->get_authorization_url( $user ) ); // phpcs:ignore WordPress.Security.SafeRedirect |
694
|
|
|
exit(); |
695
|
|
|
} |
696
|
|
|
|
697
|
|
|
/** |
698
|
|
|
* Unlinks the current user from the linked WordPress.com user. |
699
|
|
|
* |
700
|
|
|
* @access public |
701
|
|
|
* @static |
702
|
|
|
* |
703
|
|
|
* @todo Refactor to properly load the XMLRPC client independently. |
704
|
|
|
* |
705
|
|
|
* @param Integer $user_id the user identifier. |
|
|
|
|
706
|
|
|
* @return Boolean Whether the disconnection of the user was successful. |
707
|
|
|
*/ |
708
|
|
|
public static function disconnect_user( $user_id = null ) { |
709
|
|
|
$tokens = \Jetpack_Options::get_option( 'user_tokens' ); |
710
|
|
|
if ( ! $tokens ) { |
711
|
|
|
return false; |
712
|
|
|
} |
713
|
|
|
|
714
|
|
|
$user_id = empty( $user_id ) ? get_current_user_id() : intval( $user_id ); |
715
|
|
|
|
716
|
|
|
if ( \Jetpack_Options::get_option( 'master_user' ) === $user_id ) { |
717
|
|
|
return false; |
718
|
|
|
} |
719
|
|
|
|
720
|
|
|
if ( ! isset( $tokens[ $user_id ] ) ) { |
721
|
|
|
return false; |
722
|
|
|
} |
723
|
|
|
|
724
|
|
|
$xml = new \Jetpack_IXR_Client( compact( 'user_id' ) ); |
725
|
|
|
$xml->query( 'jetpack.unlink_user', $user_id ); |
726
|
|
|
|
727
|
|
|
unset( $tokens[ $user_id ] ); |
728
|
|
|
|
729
|
|
|
\Jetpack_Options::update_option( 'user_tokens', $tokens ); |
730
|
|
|
|
731
|
|
|
// Delete cached connected user data. |
732
|
|
|
$transient_key = "jetpack_connected_user_data_$user_id"; |
733
|
|
|
delete_transient( $transient_key ); |
734
|
|
|
|
735
|
|
|
/** |
736
|
|
|
* Fires after the current user has been unlinked from WordPress.com. |
737
|
|
|
* |
738
|
|
|
* @since 4.1.0 |
739
|
|
|
* |
740
|
|
|
* @param int $user_id The current user's ID. |
741
|
|
|
*/ |
742
|
|
|
do_action( 'jetpack_unlinked_user', $user_id ); |
743
|
|
|
|
744
|
|
|
return true; |
745
|
|
|
} |
746
|
|
|
|
747
|
|
|
/** |
748
|
|
|
* Returns the requested Jetpack API URL. |
749
|
|
|
* |
750
|
|
|
* @param String $relative_url the relative API path. |
751
|
|
|
* @return String API URL. |
752
|
|
|
*/ |
753
|
|
|
public function api_url( $relative_url ) { |
754
|
|
|
$api_base = Constants::get_constant( 'JETPACK__API_BASE' ); |
755
|
|
|
$api_version = '/' . Constants::get_constant( 'JETPACK__API_VERSION' ) . '/'; |
756
|
|
|
|
757
|
|
|
/** |
758
|
|
|
* Filters whether the connection manager should use the iframe authorization |
759
|
|
|
* flow instead of the regular redirect-based flow. |
760
|
|
|
* |
761
|
|
|
* @since 8.3.0 |
762
|
|
|
* |
763
|
|
|
* @param Boolean $is_iframe_flow_used should the iframe flow be used, defaults to false. |
764
|
|
|
*/ |
765
|
|
|
$iframe_flow = apply_filters( 'jetpack_use_iframe_authorization_flow', false ); |
766
|
|
|
|
767
|
|
|
// Do not modify anything that is not related to authorize requests. |
768
|
|
|
if ( 'authorize' === $relative_url && $iframe_flow ) { |
769
|
|
|
$relative_url = 'authorize_iframe'; |
770
|
|
|
} |
771
|
|
|
|
772
|
|
|
/** |
773
|
|
|
* Filters the API URL that Jetpack uses for server communication. |
774
|
|
|
* |
775
|
|
|
* @since 8.0.0 |
776
|
|
|
* |
777
|
|
|
* @param String $url the generated URL. |
778
|
|
|
* @param String $relative_url the relative URL that was passed as an argument. |
779
|
|
|
* @param String $api_base the API base string that is being used. |
780
|
|
|
* @param String $api_version the API version string that is being used. |
781
|
|
|
*/ |
782
|
|
|
return apply_filters( |
783
|
|
|
'jetpack_api_url', |
784
|
|
|
rtrim( $api_base . $relative_url, '/\\' ) . $api_version, |
785
|
|
|
$relative_url, |
|
|
|
|
786
|
|
|
$api_base, |
787
|
|
|
$api_version |
788
|
|
|
); |
789
|
|
|
} |
790
|
|
|
|
791
|
|
|
/** |
792
|
|
|
* Returns the Jetpack XMLRPC WordPress.com API endpoint URL. |
793
|
|
|
* |
794
|
|
|
* @return String XMLRPC API URL. |
795
|
|
|
*/ |
796
|
|
|
public function xmlrpc_api_url() { |
797
|
|
|
$base = preg_replace( |
798
|
|
|
'#(https?://[^?/]+)(/?.*)?$#', |
799
|
|
|
'\\1', |
800
|
|
|
Constants::get_constant( 'JETPACK__API_BASE' ) |
801
|
|
|
); |
802
|
|
|
return untrailingslashit( $base ) . '/xmlrpc.php'; |
803
|
|
|
} |
804
|
|
|
|
805
|
|
|
/** |
806
|
|
|
* Attempts Jetpack registration which sets up the site for connection. Should |
807
|
|
|
* remain public because the call to action comes from the current site, not from |
808
|
|
|
* WordPress.com. |
809
|
|
|
* |
810
|
|
|
* @param String $api_endpoint (optional) an API endpoint to use, defaults to 'register'. |
811
|
|
|
* @return true|WP_Error The error object. |
812
|
|
|
*/ |
813
|
|
|
public function register( $api_endpoint = 'register' ) { |
814
|
|
|
add_action( 'pre_update_jetpack_option_register', array( '\\Jetpack_Options', 'delete_option' ) ); |
815
|
|
|
$secrets = $this->generate_secrets( 'register', get_current_user_id(), 600 ); |
816
|
|
|
|
817
|
|
|
if ( |
818
|
|
|
empty( $secrets['secret_1'] ) || |
819
|
|
|
empty( $secrets['secret_2'] ) || |
820
|
|
|
empty( $secrets['exp'] ) |
821
|
|
|
) { |
822
|
|
|
return new \WP_Error( 'missing_secrets' ); |
|
|
|
|
823
|
|
|
} |
824
|
|
|
|
825
|
|
|
// Better to try (and fail) to set a higher timeout than this system |
826
|
|
|
// supports than to have register fail for more users than it should. |
827
|
|
|
$timeout = $this->set_min_time_limit( 60 ) / 2; |
828
|
|
|
|
829
|
|
|
$gmt_offset = get_option( 'gmt_offset' ); |
830
|
|
|
if ( ! $gmt_offset ) { |
831
|
|
|
$gmt_offset = 0; |
832
|
|
|
} |
833
|
|
|
|
834
|
|
|
$stats_options = get_option( 'stats_options' ); |
835
|
|
|
$stats_id = isset( $stats_options['blog_id'] ) |
836
|
|
|
? $stats_options['blog_id'] |
837
|
|
|
: null; |
838
|
|
|
|
839
|
|
|
/** |
840
|
|
|
* Filters the request body for additional property addition. |
841
|
|
|
* |
842
|
|
|
* @since 7.7.0 |
843
|
|
|
* |
844
|
|
|
* @param array $post_data request data. |
845
|
|
|
* @param Array $token_data token data. |
846
|
|
|
*/ |
847
|
|
|
$body = apply_filters( |
848
|
|
|
'jetpack_register_request_body', |
849
|
|
|
array( |
850
|
|
|
'siteurl' => site_url(), |
851
|
|
|
'home' => home_url(), |
852
|
|
|
'gmt_offset' => $gmt_offset, |
853
|
|
|
'timezone_string' => (string) get_option( 'timezone_string' ), |
854
|
|
|
'site_name' => (string) get_option( 'blogname' ), |
855
|
|
|
'secret_1' => $secrets['secret_1'], |
856
|
|
|
'secret_2' => $secrets['secret_2'], |
857
|
|
|
'site_lang' => get_locale(), |
858
|
|
|
'timeout' => $timeout, |
859
|
|
|
'stats_id' => $stats_id, |
860
|
|
|
'state' => get_current_user_id(), |
861
|
|
|
'site_created' => $this->get_assumed_site_creation_date(), |
862
|
|
|
'jetpack_version' => Constants::get_constant( 'JETPACK__VERSION' ), |
863
|
|
|
'ABSPATH' => Constants::get_constant( 'ABSPATH' ), |
864
|
|
|
) |
865
|
|
|
); |
866
|
|
|
|
867
|
|
|
$args = array( |
868
|
|
|
'method' => 'POST', |
869
|
|
|
'body' => $body, |
870
|
|
|
'headers' => array( |
871
|
|
|
'Accept' => 'application/json', |
872
|
|
|
), |
873
|
|
|
'timeout' => $timeout, |
874
|
|
|
); |
875
|
|
|
|
876
|
|
|
$args['body'] = $this->apply_activation_source_to_args( $args['body'] ); |
877
|
|
|
|
878
|
|
|
// TODO: fix URLs for bad hosts. |
879
|
|
|
$response = Client::_wp_remote_request( |
880
|
|
|
$this->api_url( $api_endpoint ), |
881
|
|
|
$args, |
882
|
|
|
true |
883
|
|
|
); |
884
|
|
|
|
885
|
|
|
// Make sure the response is valid and does not contain any Jetpack errors. |
886
|
|
|
$registration_details = $this->validate_remote_register_response( $response ); |
887
|
|
|
|
888
|
|
|
if ( is_wp_error( $registration_details ) ) { |
889
|
|
|
return $registration_details; |
890
|
|
|
} elseif ( ! $registration_details ) { |
891
|
|
|
return new \WP_Error( |
892
|
|
|
'unknown_error', |
|
|
|
|
893
|
|
|
'Unknown error registering your Jetpack site.', |
894
|
|
|
wp_remote_retrieve_response_code( $response ) |
895
|
|
|
); |
896
|
|
|
} |
897
|
|
|
|
898
|
|
|
if ( empty( $registration_details->jetpack_secret ) || ! is_string( $registration_details->jetpack_secret ) ) { |
899
|
|
|
return new \WP_Error( |
900
|
|
|
'jetpack_secret', |
|
|
|
|
901
|
|
|
'Unable to validate registration of your Jetpack site.', |
902
|
|
|
wp_remote_retrieve_response_code( $response ) |
903
|
|
|
); |
904
|
|
|
} |
905
|
|
|
|
906
|
|
|
if ( isset( $registration_details->jetpack_public ) ) { |
907
|
|
|
$jetpack_public = (int) $registration_details->jetpack_public; |
908
|
|
|
} else { |
909
|
|
|
$jetpack_public = false; |
910
|
|
|
} |
911
|
|
|
|
912
|
|
|
\Jetpack_Options::update_options( |
913
|
|
|
array( |
914
|
|
|
'id' => (int) $registration_details->jetpack_id, |
915
|
|
|
'blog_token' => (string) $registration_details->jetpack_secret, |
916
|
|
|
'public' => $jetpack_public, |
917
|
|
|
) |
918
|
|
|
); |
919
|
|
|
|
920
|
|
|
/** |
921
|
|
|
* Fires when a site is registered on WordPress.com. |
922
|
|
|
* |
923
|
|
|
* @since 3.7.0 |
924
|
|
|
* |
925
|
|
|
* @param int $json->jetpack_id Jetpack Blog ID. |
926
|
|
|
* @param string $json->jetpack_secret Jetpack Blog Token. |
927
|
|
|
* @param int|bool $jetpack_public Is the site public. |
928
|
|
|
*/ |
929
|
|
|
do_action( |
930
|
|
|
'jetpack_site_registered', |
931
|
|
|
$registration_details->jetpack_id, |
932
|
|
|
$registration_details->jetpack_secret, |
933
|
|
|
$jetpack_public |
934
|
|
|
); |
935
|
|
|
|
936
|
|
|
if ( isset( $registration_details->token ) ) { |
937
|
|
|
/** |
938
|
|
|
* Fires when a user token is sent along with the registration data. |
939
|
|
|
* |
940
|
|
|
* @since 7.6.0 |
941
|
|
|
* |
942
|
|
|
* @param object $token the administrator token for the newly registered site. |
943
|
|
|
*/ |
944
|
|
|
do_action( 'jetpack_site_registered_user_token', $registration_details->token ); |
945
|
|
|
} |
946
|
|
|
|
947
|
|
|
return true; |
948
|
|
|
} |
949
|
|
|
|
950
|
|
|
/** |
951
|
|
|
* Takes the response from the Jetpack register new site endpoint and |
952
|
|
|
* verifies it worked properly. |
953
|
|
|
* |
954
|
|
|
* @since 2.6 |
955
|
|
|
* |
956
|
|
|
* @param Mixed $response the response object, or the error object. |
957
|
|
|
* @return string|WP_Error A JSON object on success or WP_Error on failures |
958
|
|
|
**/ |
959
|
|
|
protected function validate_remote_register_response( $response ) { |
960
|
|
|
if ( is_wp_error( $response ) ) { |
961
|
|
|
return new \WP_Error( |
962
|
|
|
'register_http_request_failed', |
|
|
|
|
963
|
|
|
$response->get_error_message() |
964
|
|
|
); |
965
|
|
|
} |
966
|
|
|
|
967
|
|
|
$code = wp_remote_retrieve_response_code( $response ); |
968
|
|
|
$entity = wp_remote_retrieve_body( $response ); |
969
|
|
|
|
970
|
|
|
if ( $entity ) { |
971
|
|
|
$registration_response = json_decode( $entity ); |
972
|
|
|
} else { |
973
|
|
|
$registration_response = false; |
974
|
|
|
} |
975
|
|
|
|
976
|
|
|
$code_type = intval( $code / 100 ); |
977
|
|
|
if ( 5 === $code_type ) { |
978
|
|
|
return new \WP_Error( 'wpcom_5??', $code ); |
|
|
|
|
979
|
|
|
} elseif ( 408 === $code ) { |
980
|
|
|
return new \WP_Error( 'wpcom_408', $code ); |
|
|
|
|
981
|
|
|
} elseif ( ! empty( $registration_response->error ) ) { |
982
|
|
|
if ( |
983
|
|
|
'xml_rpc-32700' === $registration_response->error |
984
|
|
|
&& ! function_exists( 'xml_parser_create' ) |
985
|
|
|
) { |
986
|
|
|
$error_description = __( "PHP's XML extension is not available. Jetpack requires the XML extension to communicate with WordPress.com. Please contact your hosting provider to enable PHP's XML extension.", 'jetpack' ); |
987
|
|
|
} else { |
988
|
|
|
$error_description = isset( $registration_response->error_description ) |
989
|
|
|
? (string) $registration_response->error_description |
990
|
|
|
: ''; |
991
|
|
|
} |
992
|
|
|
|
993
|
|
|
return new \WP_Error( |
994
|
|
|
(string) $registration_response->error, |
|
|
|
|
995
|
|
|
$error_description, |
996
|
|
|
$code |
997
|
|
|
); |
998
|
|
|
} elseif ( 200 !== $code ) { |
999
|
|
|
return new \WP_Error( 'wpcom_bad_response', $code ); |
|
|
|
|
1000
|
|
|
} |
1001
|
|
|
|
1002
|
|
|
// Jetpack ID error block. |
1003
|
|
|
if ( empty( $registration_response->jetpack_id ) ) { |
1004
|
|
|
return new \WP_Error( |
1005
|
|
|
'jetpack_id', |
|
|
|
|
1006
|
|
|
/* translators: %s is an error message string */ |
1007
|
|
|
sprintf( __( 'Error Details: Jetpack ID is empty. Do not publicly post this error message! %s', 'jetpack' ), $entity ), |
1008
|
|
|
$entity |
1009
|
|
|
); |
1010
|
|
|
} elseif ( ! is_scalar( $registration_response->jetpack_id ) ) { |
1011
|
|
|
return new \WP_Error( |
1012
|
|
|
'jetpack_id', |
|
|
|
|
1013
|
|
|
/* translators: %s is an error message string */ |
1014
|
|
|
sprintf( __( 'Error Details: Jetpack ID is not a scalar. Do not publicly post this error message! %s', 'jetpack' ), $entity ), |
1015
|
|
|
$entity |
1016
|
|
|
); |
1017
|
|
View Code Duplication |
} elseif ( preg_match( '/[^0-9]/', $registration_response->jetpack_id ) ) { |
1018
|
|
|
return new \WP_Error( |
1019
|
|
|
'jetpack_id', |
|
|
|
|
1020
|
|
|
/* translators: %s is an error message string */ |
1021
|
|
|
sprintf( __( 'Error Details: Jetpack ID begins with a numeral. Do not publicly post this error message! %s', 'jetpack' ), $entity ), |
1022
|
|
|
$entity |
1023
|
|
|
); |
1024
|
|
|
} |
1025
|
|
|
|
1026
|
|
|
return $registration_response; |
1027
|
|
|
} |
1028
|
|
|
|
1029
|
|
|
/** |
1030
|
|
|
* Adds a used nonce to a list of known nonces. |
1031
|
|
|
* |
1032
|
|
|
* @param int $timestamp the current request timestamp. |
1033
|
|
|
* @param string $nonce the nonce value. |
1034
|
|
|
* @return bool whether the nonce is unique or not. |
1035
|
|
|
*/ |
1036
|
|
|
public function add_nonce( $timestamp, $nonce ) { |
1037
|
|
|
global $wpdb; |
1038
|
|
|
static $nonces_used_this_request = array(); |
1039
|
|
|
|
1040
|
|
|
if ( isset( $nonces_used_this_request[ "$timestamp:$nonce" ] ) ) { |
1041
|
|
|
return $nonces_used_this_request[ "$timestamp:$nonce" ]; |
1042
|
|
|
} |
1043
|
|
|
|
1044
|
|
|
// This should always have gone through Jetpack_Signature::sign_request() first to check $timestamp an $nonce. |
1045
|
|
|
$timestamp = (int) $timestamp; |
1046
|
|
|
$nonce = esc_sql( $nonce ); |
1047
|
|
|
|
1048
|
|
|
// Raw query so we can avoid races: add_option will also update. |
1049
|
|
|
$show_errors = $wpdb->show_errors( false ); |
1050
|
|
|
|
1051
|
|
|
$old_nonce = $wpdb->get_row( |
1052
|
|
|
$wpdb->prepare( "SELECT * FROM `$wpdb->options` WHERE option_name = %s", "jetpack_nonce_{$timestamp}_{$nonce}" ) |
1053
|
|
|
); |
1054
|
|
|
|
1055
|
|
|
if ( is_null( $old_nonce ) ) { |
1056
|
|
|
$return = $wpdb->query( |
1057
|
|
|
$wpdb->prepare( |
1058
|
|
|
"INSERT INTO `$wpdb->options` (`option_name`, `option_value`, `autoload`) VALUES (%s, %s, %s)", |
1059
|
|
|
"jetpack_nonce_{$timestamp}_{$nonce}", |
1060
|
|
|
time(), |
1061
|
|
|
'no' |
1062
|
|
|
) |
1063
|
|
|
); |
1064
|
|
|
} else { |
1065
|
|
|
$return = false; |
1066
|
|
|
} |
1067
|
|
|
|
1068
|
|
|
$wpdb->show_errors( $show_errors ); |
1069
|
|
|
|
1070
|
|
|
$nonces_used_this_request[ "$timestamp:$nonce" ] = $return; |
1071
|
|
|
|
1072
|
|
|
return $return; |
1073
|
|
|
} |
1074
|
|
|
|
1075
|
|
|
/** |
1076
|
|
|
* Cleans nonces that were saved when calling ::add_nonce. |
1077
|
|
|
* |
1078
|
|
|
* @todo Properly prepare the query before executing it. |
1079
|
|
|
* |
1080
|
|
|
* @param bool $all whether to clean even non-expired nonces. |
1081
|
|
|
*/ |
1082
|
|
|
public function clean_nonces( $all = false ) { |
1083
|
|
|
global $wpdb; |
1084
|
|
|
|
1085
|
|
|
$sql = "DELETE FROM `$wpdb->options` WHERE `option_name` LIKE %s"; |
1086
|
|
|
$sql_args = array( $wpdb->esc_like( 'jetpack_nonce_' ) . '%' ); |
1087
|
|
|
|
1088
|
|
|
if ( true !== $all ) { |
1089
|
|
|
$sql .= ' AND CAST( `option_value` AS UNSIGNED ) < %d'; |
1090
|
|
|
$sql_args[] = time() - 3600; |
1091
|
|
|
} |
1092
|
|
|
|
1093
|
|
|
$sql .= ' ORDER BY `option_id` LIMIT 100'; |
1094
|
|
|
|
1095
|
|
|
$sql = $wpdb->prepare( $sql, $sql_args ); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared |
1096
|
|
|
|
1097
|
|
|
for ( $i = 0; $i < 1000; $i++ ) { |
1098
|
|
|
if ( ! $wpdb->query( $sql ) ) { // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared |
1099
|
|
|
break; |
1100
|
|
|
} |
1101
|
|
|
} |
1102
|
|
|
} |
1103
|
|
|
|
1104
|
|
|
/** |
1105
|
|
|
* Sets the Connection custom capabilities. |
1106
|
|
|
* |
1107
|
|
|
* @param string[] $caps Array of the user's capabilities. |
1108
|
|
|
* @param string $cap Capability name. |
1109
|
|
|
* @param int $user_id The user ID. |
1110
|
|
|
* @param array $args Adds the context to the cap. Typically the object ID. |
1111
|
|
|
*/ |
1112
|
|
|
public function jetpack_connection_custom_caps( $caps, $cap, $user_id, $args ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable |
1113
|
|
|
$is_offline_mode = ( new Status() )->is_offline_mode(); |
1114
|
|
|
switch ( $cap ) { |
1115
|
|
|
case 'jetpack_connect': |
1116
|
|
|
case 'jetpack_reconnect': |
1117
|
|
|
if ( $is_offline_mode ) { |
1118
|
|
|
$caps = array( 'do_not_allow' ); |
1119
|
|
|
break; |
1120
|
|
|
} |
1121
|
|
|
// Pass through. If it's not offline mode, these should match disconnect. |
1122
|
|
|
// Let users disconnect if it's offline mode, just in case things glitch. |
1123
|
|
|
case 'jetpack_disconnect': |
1124
|
|
|
/** |
1125
|
|
|
* Filters the jetpack_disconnect capability. |
1126
|
|
|
* |
1127
|
|
|
* @since 8.7.0 |
1128
|
|
|
* |
1129
|
|
|
* @param array An array containing the capability name. |
1130
|
|
|
*/ |
1131
|
|
|
$caps = apply_filters( 'jetpack_disconnect_cap', array( 'manage_options' ) ); |
1132
|
|
|
break; |
1133
|
|
|
case 'jetpack_connect_user': |
1134
|
|
|
if ( $is_offline_mode ) { |
1135
|
|
|
$caps = array( 'do_not_allow' ); |
1136
|
|
|
break; |
1137
|
|
|
} |
1138
|
|
|
$caps = array( 'read' ); |
1139
|
|
|
break; |
1140
|
|
|
} |
1141
|
|
|
return $caps; |
1142
|
|
|
} |
1143
|
|
|
|
1144
|
|
|
/** |
1145
|
|
|
* Builds the timeout limit for queries talking with the wpcom servers. |
1146
|
|
|
* |
1147
|
|
|
* Based on local php max_execution_time in php.ini |
1148
|
|
|
* |
1149
|
|
|
* @since 5.4 |
1150
|
|
|
* @return int |
1151
|
|
|
**/ |
1152
|
|
|
public function get_max_execution_time() { |
1153
|
|
|
$timeout = (int) ini_get( 'max_execution_time' ); |
1154
|
|
|
|
1155
|
|
|
// Ensure exec time set in php.ini. |
1156
|
|
|
if ( ! $timeout ) { |
1157
|
|
|
$timeout = 30; |
1158
|
|
|
} |
1159
|
|
|
return $timeout; |
1160
|
|
|
} |
1161
|
|
|
|
1162
|
|
|
/** |
1163
|
|
|
* Sets a minimum request timeout, and returns the current timeout |
1164
|
|
|
* |
1165
|
|
|
* @since 5.4 |
1166
|
|
|
* @param Integer $min_timeout the minimum timeout value. |
1167
|
|
|
**/ |
1168
|
|
View Code Duplication |
public function set_min_time_limit( $min_timeout ) { |
1169
|
|
|
$timeout = $this->get_max_execution_time(); |
1170
|
|
|
if ( $timeout < $min_timeout ) { |
1171
|
|
|
$timeout = $min_timeout; |
1172
|
|
|
set_time_limit( $timeout ); |
1173
|
|
|
} |
1174
|
|
|
return $timeout; |
1175
|
|
|
} |
1176
|
|
|
|
1177
|
|
|
/** |
1178
|
|
|
* Get our assumed site creation date. |
1179
|
|
|
* Calculated based on the earlier date of either: |
1180
|
|
|
* - Earliest admin user registration date. |
1181
|
|
|
* - Earliest date of post of any post type. |
1182
|
|
|
* |
1183
|
|
|
* @since 7.2.0 |
1184
|
|
|
* |
1185
|
|
|
* @return string Assumed site creation date and time. |
1186
|
|
|
*/ |
1187
|
|
|
public function get_assumed_site_creation_date() { |
1188
|
|
|
$cached_date = get_transient( 'jetpack_assumed_site_creation_date' ); |
1189
|
|
|
if ( ! empty( $cached_date ) ) { |
1190
|
|
|
return $cached_date; |
1191
|
|
|
} |
1192
|
|
|
|
1193
|
|
|
$earliest_registered_users = get_users( |
1194
|
|
|
array( |
1195
|
|
|
'role' => 'administrator', |
1196
|
|
|
'orderby' => 'user_registered', |
1197
|
|
|
'order' => 'ASC', |
1198
|
|
|
'fields' => array( 'user_registered' ), |
1199
|
|
|
'number' => 1, |
1200
|
|
|
) |
1201
|
|
|
); |
1202
|
|
|
$earliest_registration_date = $earliest_registered_users[0]->user_registered; |
1203
|
|
|
|
1204
|
|
|
$earliest_posts = get_posts( |
1205
|
|
|
array( |
1206
|
|
|
'posts_per_page' => 1, |
1207
|
|
|
'post_type' => 'any', |
1208
|
|
|
'post_status' => 'any', |
1209
|
|
|
'orderby' => 'date', |
1210
|
|
|
'order' => 'ASC', |
1211
|
|
|
) |
1212
|
|
|
); |
1213
|
|
|
|
1214
|
|
|
// If there are no posts at all, we'll count only on user registration date. |
1215
|
|
|
if ( $earliest_posts ) { |
1216
|
|
|
$earliest_post_date = $earliest_posts[0]->post_date; |
1217
|
|
|
} else { |
1218
|
|
|
$earliest_post_date = PHP_INT_MAX; |
1219
|
|
|
} |
1220
|
|
|
|
1221
|
|
|
$assumed_date = min( $earliest_registration_date, $earliest_post_date ); |
1222
|
|
|
set_transient( 'jetpack_assumed_site_creation_date', $assumed_date ); |
1223
|
|
|
|
1224
|
|
|
return $assumed_date; |
1225
|
|
|
} |
1226
|
|
|
|
1227
|
|
|
/** |
1228
|
|
|
* Adds the activation source string as a parameter to passed arguments. |
1229
|
|
|
* |
1230
|
|
|
* @todo Refactor to use rawurlencode() instead of urlencode(). |
1231
|
|
|
* |
1232
|
|
|
* @param array $args arguments that need to have the source added. |
1233
|
|
|
* @return array $amended arguments. |
1234
|
|
|
*/ |
1235
|
|
View Code Duplication |
public static function apply_activation_source_to_args( $args ) { |
1236
|
|
|
list( $activation_source_name, $activation_source_keyword ) = get_option( 'jetpack_activation_source' ); |
1237
|
|
|
|
1238
|
|
|
if ( $activation_source_name ) { |
1239
|
|
|
// phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.urlencode_urlencode |
1240
|
|
|
$args['_as'] = urlencode( $activation_source_name ); |
1241
|
|
|
} |
1242
|
|
|
|
1243
|
|
|
if ( $activation_source_keyword ) { |
1244
|
|
|
// phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.urlencode_urlencode |
1245
|
|
|
$args['_ak'] = urlencode( $activation_source_keyword ); |
1246
|
|
|
} |
1247
|
|
|
|
1248
|
|
|
return $args; |
1249
|
|
|
} |
1250
|
|
|
|
1251
|
|
|
/** |
1252
|
|
|
* Returns the callable that would be used to generate secrets. |
1253
|
|
|
* |
1254
|
|
|
* @return Callable a function that returns a secure string to be used as a secret. |
1255
|
|
|
*/ |
1256
|
|
|
protected function get_secret_callable() { |
1257
|
|
|
if ( ! isset( $this->secret_callable ) ) { |
1258
|
|
|
/** |
1259
|
|
|
* Allows modification of the callable that is used to generate connection secrets. |
1260
|
|
|
* |
1261
|
|
|
* @param Callable a function or method that returns a secret string. |
1262
|
|
|
*/ |
1263
|
|
|
$this->secret_callable = apply_filters( 'jetpack_connection_secret_generator', array( $this, 'secret_callable_method' ) ); |
1264
|
|
|
} |
1265
|
|
|
|
1266
|
|
|
return $this->secret_callable; |
1267
|
|
|
} |
1268
|
|
|
|
1269
|
|
|
/** |
1270
|
|
|
* Runs the wp_generate_password function with the required parameters. This is the |
1271
|
|
|
* default implementation of the secret callable, can be overridden using the |
1272
|
|
|
* jetpack_connection_secret_generator filter. |
1273
|
|
|
* |
1274
|
|
|
* @return String $secret value. |
1275
|
|
|
*/ |
1276
|
|
|
private function secret_callable_method() { |
1277
|
|
|
return wp_generate_password( 32, false ); |
1278
|
|
|
} |
1279
|
|
|
|
1280
|
|
|
/** |
1281
|
|
|
* Generates two secret tokens and the end of life timestamp for them. |
1282
|
|
|
* |
1283
|
|
|
* @param String $action The action name. |
1284
|
|
|
* @param Integer $user_id The user identifier. |
|
|
|
|
1285
|
|
|
* @param Integer $exp Expiration time in seconds. |
1286
|
|
|
*/ |
1287
|
|
|
public function generate_secrets( $action, $user_id = false, $exp = 600 ) { |
1288
|
|
|
if ( false === $user_id ) { |
1289
|
|
|
$user_id = get_current_user_id(); |
1290
|
|
|
} |
1291
|
|
|
|
1292
|
|
|
$callable = $this->get_secret_callable(); |
1293
|
|
|
|
1294
|
|
|
$secrets = \Jetpack_Options::get_raw_option( |
1295
|
|
|
self::SECRETS_OPTION_NAME, |
1296
|
|
|
array() |
1297
|
|
|
); |
1298
|
|
|
|
1299
|
|
|
$secret_name = 'jetpack_' . $action . '_' . $user_id; |
1300
|
|
|
|
1301
|
|
|
if ( |
1302
|
|
|
isset( $secrets[ $secret_name ] ) && |
1303
|
|
|
$secrets[ $secret_name ]['exp'] > time() |
1304
|
|
|
) { |
1305
|
|
|
return $secrets[ $secret_name ]; |
1306
|
|
|
} |
1307
|
|
|
|
1308
|
|
|
$secret_value = array( |
1309
|
|
|
'secret_1' => call_user_func( $callable ), |
1310
|
|
|
'secret_2' => call_user_func( $callable ), |
1311
|
|
|
'exp' => time() + $exp, |
1312
|
|
|
); |
1313
|
|
|
|
1314
|
|
|
$secrets[ $secret_name ] = $secret_value; |
1315
|
|
|
|
1316
|
|
|
\Jetpack_Options::update_raw_option( self::SECRETS_OPTION_NAME, $secrets ); |
1317
|
|
|
return $secrets[ $secret_name ]; |
1318
|
|
|
} |
1319
|
|
|
|
1320
|
|
|
/** |
1321
|
|
|
* Returns two secret tokens and the end of life timestamp for them. |
1322
|
|
|
* |
1323
|
|
|
* @param String $action The action name. |
1324
|
|
|
* @param Integer $user_id The user identifier. |
1325
|
|
|
* @return string|array an array of secrets or an error string. |
1326
|
|
|
*/ |
1327
|
|
|
public function get_secrets( $action, $user_id ) { |
1328
|
|
|
$secret_name = 'jetpack_' . $action . '_' . $user_id; |
1329
|
|
|
$secrets = \Jetpack_Options::get_raw_option( |
1330
|
|
|
self::SECRETS_OPTION_NAME, |
1331
|
|
|
array() |
1332
|
|
|
); |
1333
|
|
|
|
1334
|
|
|
if ( ! isset( $secrets[ $secret_name ] ) ) { |
1335
|
|
|
return self::SECRETS_MISSING; |
1336
|
|
|
} |
1337
|
|
|
|
1338
|
|
|
if ( $secrets[ $secret_name ]['exp'] < time() ) { |
1339
|
|
|
$this->delete_secrets( $action, $user_id ); |
1340
|
|
|
return self::SECRETS_EXPIRED; |
1341
|
|
|
} |
1342
|
|
|
|
1343
|
|
|
return $secrets[ $secret_name ]; |
1344
|
|
|
} |
1345
|
|
|
|
1346
|
|
|
/** |
1347
|
|
|
* Deletes secret tokens in case they, for example, have expired. |
1348
|
|
|
* |
1349
|
|
|
* @param String $action The action name. |
1350
|
|
|
* @param Integer $user_id The user identifier. |
1351
|
|
|
*/ |
1352
|
|
|
public function delete_secrets( $action, $user_id ) { |
1353
|
|
|
$secret_name = 'jetpack_' . $action . '_' . $user_id; |
1354
|
|
|
$secrets = \Jetpack_Options::get_raw_option( |
1355
|
|
|
self::SECRETS_OPTION_NAME, |
1356
|
|
|
array() |
1357
|
|
|
); |
1358
|
|
|
if ( isset( $secrets[ $secret_name ] ) ) { |
1359
|
|
|
unset( $secrets[ $secret_name ] ); |
1360
|
|
|
\Jetpack_Options::update_raw_option( self::SECRETS_OPTION_NAME, $secrets ); |
1361
|
|
|
} |
1362
|
|
|
} |
1363
|
|
|
|
1364
|
|
|
/** |
1365
|
|
|
* Deletes all connection tokens and transients from the local Jetpack site. |
1366
|
|
|
* If the plugin object has been provided in the constructor, the function first checks |
1367
|
|
|
* whether it's the only active connection. |
1368
|
|
|
* If there are any other connections, the function will do nothing and return `false` |
1369
|
|
|
* (unless `$ignore_connected_plugins` is set to `true`). |
1370
|
|
|
* |
1371
|
|
|
* @param bool $ignore_connected_plugins Delete the tokens even if there are other connected plugins. |
1372
|
|
|
* |
1373
|
|
|
* @return bool True if disconnected successfully, false otherwise. |
1374
|
|
|
*/ |
1375
|
|
|
public function delete_all_connection_tokens( $ignore_connected_plugins = false ) { |
1376
|
|
View Code Duplication |
if ( ! $ignore_connected_plugins && null !== $this->plugin && ! $this->plugin->is_only() ) { |
1377
|
|
|
return false; |
1378
|
|
|
} |
1379
|
|
|
|
1380
|
|
|
/** |
1381
|
|
|
* Fires upon the disconnect attempt. |
1382
|
|
|
* Return `false` to prevent the disconnect. |
1383
|
|
|
* |
1384
|
|
|
* @since 8.7.0 |
1385
|
|
|
*/ |
1386
|
|
|
if ( ! apply_filters( 'jetpack_connection_delete_all_tokens', true, $this ) ) { |
|
|
|
|
1387
|
|
|
return false; |
1388
|
|
|
} |
1389
|
|
|
|
1390
|
|
|
\Jetpack_Options::delete_option( |
1391
|
|
|
array( |
1392
|
|
|
'blog_token', |
1393
|
|
|
'user_token', |
1394
|
|
|
'user_tokens', |
1395
|
|
|
'master_user', |
1396
|
|
|
'time_diff', |
1397
|
|
|
'fallback_no_verify_ssl_certs', |
1398
|
|
|
) |
1399
|
|
|
); |
1400
|
|
|
|
1401
|
|
|
\Jetpack_Options::delete_raw_option( 'jetpack_secrets' ); |
1402
|
|
|
|
1403
|
|
|
// Delete cached connected user data. |
1404
|
|
|
$transient_key = 'jetpack_connected_user_data_' . get_current_user_id(); |
1405
|
|
|
delete_transient( $transient_key ); |
1406
|
|
|
|
1407
|
|
|
// Delete all XML-RPC errors. |
1408
|
|
|
Error_Handler::get_instance()->delete_all_errors(); |
1409
|
|
|
|
1410
|
|
|
return true; |
1411
|
|
|
} |
1412
|
|
|
|
1413
|
|
|
/** |
1414
|
|
|
* Tells WordPress.com to disconnect the site and clear all tokens from cached site. |
1415
|
|
|
* If the plugin object has been provided in the constructor, the function first check |
1416
|
|
|
* whether it's the only active connection. |
1417
|
|
|
* If there are any other connections, the function will do nothing and return `false` |
1418
|
|
|
* (unless `$ignore_connected_plugins` is set to `true`). |
1419
|
|
|
* |
1420
|
|
|
* @param bool $ignore_connected_plugins Delete the tokens even if there are other connected plugins. |
1421
|
|
|
* |
1422
|
|
|
* @return bool True if disconnected successfully, false otherwise. |
1423
|
|
|
*/ |
1424
|
|
|
public function disconnect_site_wpcom( $ignore_connected_plugins = false ) { |
1425
|
|
View Code Duplication |
if ( ! $ignore_connected_plugins && null !== $this->plugin && ! $this->plugin->is_only() ) { |
1426
|
|
|
return false; |
1427
|
|
|
} |
1428
|
|
|
|
1429
|
|
|
/** |
1430
|
|
|
* Fires upon the disconnect attempt. |
1431
|
|
|
* Return `false` to prevent the disconnect. |
1432
|
|
|
* |
1433
|
|
|
* @since 8.7.0 |
1434
|
|
|
*/ |
1435
|
|
|
if ( ! apply_filters( 'jetpack_connection_disconnect_site_wpcom', true, $this ) ) { |
|
|
|
|
1436
|
|
|
return false; |
1437
|
|
|
} |
1438
|
|
|
|
1439
|
|
|
$xml = new \Jetpack_IXR_Client(); |
1440
|
|
|
$xml->query( 'jetpack.deregister', get_current_user_id() ); |
1441
|
|
|
|
1442
|
|
|
return true; |
1443
|
|
|
} |
1444
|
|
|
|
1445
|
|
|
/** |
1446
|
|
|
* Disconnect the plugin and remove the tokens. |
1447
|
|
|
* This function will automatically perform "soft" or "hard" disconnect depending on whether other plugins are using the connection. |
1448
|
|
|
* This is a proxy method to simplify the Connection package API. |
1449
|
|
|
* |
1450
|
|
|
* @see Manager::disable_plugin() |
1451
|
|
|
* @see Manager::disconnect_site_wpcom() |
1452
|
|
|
* @see Manager::delete_all_connection_tokens() |
1453
|
|
|
* |
1454
|
|
|
* @return bool |
1455
|
|
|
*/ |
1456
|
|
|
public function remove_connection() { |
1457
|
|
|
$this->disable_plugin(); |
1458
|
|
|
$this->disconnect_site_wpcom(); |
1459
|
|
|
$this->delete_all_connection_tokens(); |
1460
|
|
|
|
1461
|
|
|
return true; |
1462
|
|
|
} |
1463
|
|
|
|
1464
|
|
|
/** |
1465
|
|
|
* Completely clearing up the connection, and initiating reconnect. |
1466
|
|
|
* |
1467
|
|
|
* @return true|WP_Error True if reconnected successfully, a `WP_Error` object otherwise. |
1468
|
|
|
*/ |
1469
|
|
|
public function reconnect() { |
1470
|
|
|
$this->disconnect_site_wpcom( true ); |
1471
|
|
|
$this->delete_all_connection_tokens( true ); |
1472
|
|
|
|
1473
|
|
|
return $this->register(); |
1474
|
|
|
} |
1475
|
|
|
|
1476
|
|
|
/** |
1477
|
|
|
* Responds to a WordPress.com call to register the current site. |
1478
|
|
|
* Should be changed to protected. |
1479
|
|
|
* |
1480
|
|
|
* @param array $registration_data Array of [ secret_1, user_id ]. |
1481
|
|
|
*/ |
1482
|
|
|
public function handle_registration( array $registration_data ) { |
1483
|
|
|
list( $registration_secret_1, $registration_user_id ) = $registration_data; |
1484
|
|
|
if ( empty( $registration_user_id ) ) { |
1485
|
|
|
return new \WP_Error( 'registration_state_invalid', __( 'Invalid Registration State', 'jetpack' ), 400 ); |
|
|
|
|
1486
|
|
|
} |
1487
|
|
|
|
1488
|
|
|
return $this->verify_secrets( 'register', $registration_secret_1, (int) $registration_user_id ); |
1489
|
|
|
} |
1490
|
|
|
|
1491
|
|
|
/** |
1492
|
|
|
* Verify a Previously Generated Secret. |
1493
|
|
|
* |
1494
|
|
|
* @param string $action The type of secret to verify. |
1495
|
|
|
* @param string $secret_1 The secret string to compare to what is stored. |
1496
|
|
|
* @param int $user_id The user ID of the owner of the secret. |
1497
|
|
|
* @return \WP_Error|string WP_Error on failure, secret_2 on success. |
1498
|
|
|
*/ |
1499
|
|
|
public function verify_secrets( $action, $secret_1, $user_id ) { |
1500
|
|
|
$allowed_actions = array( 'register', 'authorize', 'publicize' ); |
1501
|
|
|
if ( ! in_array( $action, $allowed_actions, true ) ) { |
1502
|
|
|
return new \WP_Error( 'unknown_verification_action', 'Unknown Verification Action', 400 ); |
|
|
|
|
1503
|
|
|
} |
1504
|
|
|
|
1505
|
|
|
$user = get_user_by( 'id', $user_id ); |
1506
|
|
|
|
1507
|
|
|
/** |
1508
|
|
|
* We've begun verifying the previously generated secret. |
1509
|
|
|
* |
1510
|
|
|
* @since 7.5.0 |
1511
|
|
|
* |
1512
|
|
|
* @param string $action The type of secret to verify. |
1513
|
|
|
* @param \WP_User $user The user object. |
1514
|
|
|
*/ |
1515
|
|
|
do_action( 'jetpack_verify_secrets_begin', $action, $user ); |
1516
|
|
|
|
1517
|
|
|
$return_error = function( \WP_Error $error ) use ( $action, $user ) { |
1518
|
|
|
/** |
1519
|
|
|
* Verifying of the previously generated secret has failed. |
1520
|
|
|
* |
1521
|
|
|
* @since 7.5.0 |
1522
|
|
|
* |
1523
|
|
|
* @param string $action The type of secret to verify. |
1524
|
|
|
* @param \WP_User $user The user object. |
1525
|
|
|
* @param \WP_Error $error The error object. |
1526
|
|
|
*/ |
1527
|
|
|
do_action( 'jetpack_verify_secrets_fail', $action, $user, $error ); |
1528
|
|
|
|
1529
|
|
|
return $error; |
1530
|
|
|
}; |
1531
|
|
|
|
1532
|
|
|
$stored_secrets = $this->get_secrets( $action, $user_id ); |
1533
|
|
|
$this->delete_secrets( $action, $user_id ); |
1534
|
|
|
|
1535
|
|
|
$error = null; |
1536
|
|
|
if ( empty( $secret_1 ) ) { |
1537
|
|
|
$error = $return_error( |
1538
|
|
|
new \WP_Error( |
1539
|
|
|
'verify_secret_1_missing', |
|
|
|
|
1540
|
|
|
/* translators: "%s" is the name of a paramter. It can be either "secret_1" or "state". */ |
1541
|
|
|
sprintf( __( 'The required "%s" parameter is missing.', 'jetpack' ), 'secret_1' ), |
1542
|
|
|
400 |
1543
|
|
|
) |
1544
|
|
|
); |
1545
|
|
|
} elseif ( ! is_string( $secret_1 ) ) { |
1546
|
|
|
$error = $return_error( |
1547
|
|
|
new \WP_Error( |
1548
|
|
|
'verify_secret_1_malformed', |
|
|
|
|
1549
|
|
|
/* translators: "%s" is the name of a paramter. It can be either "secret_1" or "state". */ |
1550
|
|
|
sprintf( __( 'The required "%s" parameter is malformed.', 'jetpack' ), 'secret_1' ), |
1551
|
|
|
400 |
1552
|
|
|
) |
1553
|
|
|
); |
1554
|
|
|
} elseif ( empty( $user_id ) ) { |
1555
|
|
|
// $user_id is passed around during registration as "state". |
1556
|
|
|
$error = $return_error( |
1557
|
|
|
new \WP_Error( |
1558
|
|
|
'state_missing', |
|
|
|
|
1559
|
|
|
/* translators: "%s" is the name of a paramter. It can be either "secret_1" or "state". */ |
1560
|
|
|
sprintf( __( 'The required "%s" parameter is missing.', 'jetpack' ), 'state' ), |
1561
|
|
|
400 |
1562
|
|
|
) |
1563
|
|
|
); |
1564
|
|
|
} elseif ( ! ctype_digit( (string) $user_id ) ) { |
1565
|
|
|
$error = $return_error( |
1566
|
|
|
new \WP_Error( |
1567
|
|
|
'state_malformed', |
|
|
|
|
1568
|
|
|
/* translators: "%s" is the name of a paramter. It can be either "secret_1" or "state". */ |
1569
|
|
|
sprintf( __( 'The required "%s" parameter is malformed.', 'jetpack' ), 'state' ), |
1570
|
|
|
400 |
1571
|
|
|
) |
1572
|
|
|
); |
1573
|
|
|
} elseif ( self::SECRETS_MISSING === $stored_secrets ) { |
1574
|
|
|
$error = $return_error( |
1575
|
|
|
new \WP_Error( |
1576
|
|
|
'verify_secrets_missing', |
|
|
|
|
1577
|
|
|
__( 'Verification secrets not found', 'jetpack' ), |
1578
|
|
|
400 |
1579
|
|
|
) |
1580
|
|
|
); |
1581
|
|
|
} elseif ( self::SECRETS_EXPIRED === $stored_secrets ) { |
1582
|
|
|
$error = $return_error( |
1583
|
|
|
new \WP_Error( |
1584
|
|
|
'verify_secrets_expired', |
|
|
|
|
1585
|
|
|
__( 'Verification took too long', 'jetpack' ), |
1586
|
|
|
400 |
1587
|
|
|
) |
1588
|
|
|
); |
1589
|
|
|
} elseif ( ! $stored_secrets ) { |
1590
|
|
|
$error = $return_error( |
1591
|
|
|
new \WP_Error( |
1592
|
|
|
'verify_secrets_empty', |
|
|
|
|
1593
|
|
|
__( 'Verification secrets are empty', 'jetpack' ), |
1594
|
|
|
400 |
1595
|
|
|
) |
1596
|
|
|
); |
1597
|
|
|
} elseif ( is_wp_error( $stored_secrets ) ) { |
1598
|
|
|
$stored_secrets->add_data( 400 ); |
|
|
|
|
1599
|
|
|
$error = $return_error( $stored_secrets ); |
1600
|
|
|
} elseif ( empty( $stored_secrets['secret_1'] ) || empty( $stored_secrets['secret_2'] ) || empty( $stored_secrets['exp'] ) ) { |
1601
|
|
|
$error = $return_error( |
1602
|
|
|
new \WP_Error( |
1603
|
|
|
'verify_secrets_incomplete', |
|
|
|
|
1604
|
|
|
__( 'Verification secrets are incomplete', 'jetpack' ), |
1605
|
|
|
400 |
1606
|
|
|
) |
1607
|
|
|
); |
1608
|
|
|
} elseif ( ! hash_equals( $secret_1, $stored_secrets['secret_1'] ) ) { |
1609
|
|
|
$error = $return_error( |
1610
|
|
|
new \WP_Error( |
1611
|
|
|
'verify_secrets_mismatch', |
|
|
|
|
1612
|
|
|
__( 'Secret mismatch', 'jetpack' ), |
1613
|
|
|
400 |
1614
|
|
|
) |
1615
|
|
|
); |
1616
|
|
|
} |
1617
|
|
|
|
1618
|
|
|
// Something went wrong during the checks, returning the error. |
1619
|
|
|
if ( ! empty( $error ) ) { |
1620
|
|
|
return $error; |
1621
|
|
|
} |
1622
|
|
|
|
1623
|
|
|
/** |
1624
|
|
|
* We've succeeded at verifying the previously generated secret. |
1625
|
|
|
* |
1626
|
|
|
* @since 7.5.0 |
1627
|
|
|
* |
1628
|
|
|
* @param string $action The type of secret to verify. |
1629
|
|
|
* @param \WP_User $user The user object. |
1630
|
|
|
*/ |
1631
|
|
|
do_action( 'jetpack_verify_secrets_success', $action, $user ); |
1632
|
|
|
|
1633
|
|
|
return $stored_secrets['secret_2']; |
1634
|
|
|
} |
1635
|
|
|
|
1636
|
|
|
/** |
1637
|
|
|
* Responds to a WordPress.com call to authorize the current user. |
1638
|
|
|
* Should be changed to protected. |
1639
|
|
|
*/ |
1640
|
|
|
public function handle_authorization() { |
1641
|
|
|
|
1642
|
|
|
} |
1643
|
|
|
|
1644
|
|
|
/** |
1645
|
|
|
* Obtains the auth token. |
1646
|
|
|
* |
1647
|
|
|
* @param array $data The request data. |
1648
|
|
|
* @return object|\WP_Error Returns the auth token on success. |
1649
|
|
|
* Returns a \WP_Error on failure. |
1650
|
|
|
*/ |
1651
|
|
|
public function get_token( $data ) { |
1652
|
|
|
$roles = new Roles(); |
1653
|
|
|
$role = $roles->translate_current_user_to_role(); |
1654
|
|
|
|
1655
|
|
|
if ( ! $role ) { |
1656
|
|
|
return new \WP_Error( 'role', __( 'An administrator for this blog must set up the Jetpack connection.', 'jetpack' ) ); |
|
|
|
|
1657
|
|
|
} |
1658
|
|
|
|
1659
|
|
|
$client_secret = $this->get_access_token(); |
1660
|
|
|
if ( ! $client_secret ) { |
1661
|
|
|
return new \WP_Error( 'client_secret', __( 'You need to register your Jetpack before connecting it.', 'jetpack' ) ); |
|
|
|
|
1662
|
|
|
} |
1663
|
|
|
|
1664
|
|
|
/** |
1665
|
|
|
* Filter the URL of the first time the user gets redirected back to your site for connection |
1666
|
|
|
* data processing. |
1667
|
|
|
* |
1668
|
|
|
* @since 8.0.0 |
1669
|
|
|
* |
1670
|
|
|
* @param string $redirect_url Defaults to the site admin URL. |
1671
|
|
|
*/ |
1672
|
|
|
$processing_url = apply_filters( 'jetpack_token_processing_url', admin_url( 'admin.php' ) ); |
1673
|
|
|
|
1674
|
|
|
$redirect = isset( $data['redirect'] ) ? esc_url_raw( (string) $data['redirect'] ) : ''; |
1675
|
|
|
|
1676
|
|
|
/** |
1677
|
|
|
* Filter the URL to redirect the user back to when the authentication process |
1678
|
|
|
* is complete. |
1679
|
|
|
* |
1680
|
|
|
* @since 8.0.0 |
1681
|
|
|
* |
1682
|
|
|
* @param string $redirect_url Defaults to the site URL. |
1683
|
|
|
*/ |
1684
|
|
|
$redirect = apply_filters( 'jetpack_token_redirect_url', $redirect ); |
1685
|
|
|
|
1686
|
|
|
$redirect_uri = ( 'calypso' === $data['auth_type'] ) |
1687
|
|
|
? $data['redirect_uri'] |
1688
|
|
|
: add_query_arg( |
1689
|
|
|
array( |
1690
|
|
|
'action' => 'authorize', |
1691
|
|
|
'_wpnonce' => wp_create_nonce( "jetpack-authorize_{$role}_{$redirect}" ), |
1692
|
|
|
'redirect' => $redirect ? rawurlencode( $redirect ) : false, |
1693
|
|
|
), |
1694
|
|
|
esc_url( $processing_url ) |
1695
|
|
|
); |
1696
|
|
|
|
1697
|
|
|
/** |
1698
|
|
|
* Filters the token request data. |
1699
|
|
|
* |
1700
|
|
|
* @since 8.0.0 |
1701
|
|
|
* |
1702
|
|
|
* @param array $request_data request data. |
1703
|
|
|
*/ |
1704
|
|
|
$body = apply_filters( |
1705
|
|
|
'jetpack_token_request_body', |
1706
|
|
|
array( |
1707
|
|
|
'client_id' => \Jetpack_Options::get_option( 'id' ), |
1708
|
|
|
'client_secret' => $client_secret->secret, |
1709
|
|
|
'grant_type' => 'authorization_code', |
1710
|
|
|
'code' => $data['code'], |
1711
|
|
|
'redirect_uri' => $redirect_uri, |
1712
|
|
|
) |
1713
|
|
|
); |
1714
|
|
|
|
1715
|
|
|
$args = array( |
1716
|
|
|
'method' => 'POST', |
1717
|
|
|
'body' => $body, |
1718
|
|
|
'headers' => array( |
1719
|
|
|
'Accept' => 'application/json', |
1720
|
|
|
), |
1721
|
|
|
); |
1722
|
|
|
|
1723
|
|
|
add_filter( 'http_request_timeout', array( $this, 'increase_timeout' ), PHP_INT_MAX - 1 ); |
1724
|
|
|
$response = Client::_wp_remote_request( Utils::fix_url_for_bad_hosts( $this->api_url( 'token' ) ), $args ); |
1725
|
|
|
remove_filter( 'http_request_timeout', array( $this, 'increase_timeout' ), PHP_INT_MAX - 1 ); |
1726
|
|
|
|
1727
|
|
|
if ( is_wp_error( $response ) ) { |
1728
|
|
|
return new \WP_Error( 'token_http_request_failed', $response->get_error_message() ); |
|
|
|
|
1729
|
|
|
} |
1730
|
|
|
|
1731
|
|
|
$code = wp_remote_retrieve_response_code( $response ); |
1732
|
|
|
$entity = wp_remote_retrieve_body( $response ); |
1733
|
|
|
|
1734
|
|
|
if ( $entity ) { |
1735
|
|
|
$json = json_decode( $entity ); |
1736
|
|
|
} else { |
1737
|
|
|
$json = false; |
1738
|
|
|
} |
1739
|
|
|
|
1740
|
|
|
if ( 200 !== $code || ! empty( $json->error ) ) { |
1741
|
|
|
if ( empty( $json->error ) ) { |
1742
|
|
|
return new \WP_Error( 'unknown', '', $code ); |
|
|
|
|
1743
|
|
|
} |
1744
|
|
|
|
1745
|
|
|
/* translators: Error description string. */ |
1746
|
|
|
$error_description = isset( $json->error_description ) ? sprintf( __( 'Error Details: %s', 'jetpack' ), (string) $json->error_description ) : ''; |
1747
|
|
|
|
1748
|
|
|
return new \WP_Error( (string) $json->error, $error_description, $code ); |
|
|
|
|
1749
|
|
|
} |
1750
|
|
|
|
1751
|
|
|
if ( empty( $json->access_token ) || ! is_scalar( $json->access_token ) ) { |
1752
|
|
|
return new \WP_Error( 'access_token', '', $code ); |
|
|
|
|
1753
|
|
|
} |
1754
|
|
|
|
1755
|
|
|
if ( empty( $json->token_type ) || 'X_JETPACK' !== strtoupper( $json->token_type ) ) { |
1756
|
|
|
return new \WP_Error( 'token_type', '', $code ); |
|
|
|
|
1757
|
|
|
} |
1758
|
|
|
|
1759
|
|
|
if ( empty( $json->scope ) ) { |
1760
|
|
|
return new \WP_Error( 'scope', 'No Scope', $code ); |
|
|
|
|
1761
|
|
|
} |
1762
|
|
|
|
1763
|
|
|
// TODO: get rid of the error silencer. |
1764
|
|
|
// phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged |
1765
|
|
|
@list( $role, $hmac ) = explode( ':', $json->scope ); |
|
|
|
|
1766
|
|
|
if ( empty( $role ) || empty( $hmac ) ) { |
1767
|
|
|
return new \WP_Error( 'scope', 'Malformed Scope', $code ); |
|
|
|
|
1768
|
|
|
} |
1769
|
|
|
|
1770
|
|
|
if ( $this->sign_role( $role ) !== $json->scope ) { |
1771
|
|
|
return new \WP_Error( 'scope', 'Invalid Scope', $code ); |
|
|
|
|
1772
|
|
|
} |
1773
|
|
|
|
1774
|
|
|
$cap = $roles->translate_role_to_cap( $role ); |
1775
|
|
|
if ( ! $cap ) { |
1776
|
|
|
return new \WP_Error( 'scope', 'No Cap', $code ); |
|
|
|
|
1777
|
|
|
} |
1778
|
|
|
|
1779
|
|
|
if ( ! current_user_can( $cap ) ) { |
1780
|
|
|
return new \WP_Error( 'scope', 'current_user_cannot', $code ); |
|
|
|
|
1781
|
|
|
} |
1782
|
|
|
|
1783
|
|
|
/** |
1784
|
|
|
* Fires after user has successfully received an auth token. |
1785
|
|
|
* |
1786
|
|
|
* @since 3.9.0 |
1787
|
|
|
*/ |
1788
|
|
|
do_action( 'jetpack_user_authorized' ); |
1789
|
|
|
|
1790
|
|
|
return (string) $json->access_token; |
1791
|
|
|
} |
1792
|
|
|
|
1793
|
|
|
/** |
1794
|
|
|
* Increases the request timeout value to 30 seconds. |
1795
|
|
|
* |
1796
|
|
|
* @return int Returns 30. |
1797
|
|
|
*/ |
1798
|
|
|
public function increase_timeout() { |
1799
|
|
|
return 30; |
1800
|
|
|
} |
1801
|
|
|
|
1802
|
|
|
/** |
1803
|
|
|
* Builds a URL to the Jetpack connection auth page. |
1804
|
|
|
* |
1805
|
|
|
* @param WP_User $user (optional) defaults to the current logged in user. |
|
|
|
|
1806
|
|
|
* @param String $redirect (optional) a redirect URL to use instead of the default. |
|
|
|
|
1807
|
|
|
* @return string Connect URL. |
1808
|
|
|
*/ |
1809
|
|
|
public function get_authorization_url( $user = null, $redirect = null ) { |
1810
|
|
|
|
1811
|
|
|
if ( empty( $user ) ) { |
1812
|
|
|
$user = wp_get_current_user(); |
1813
|
|
|
} |
1814
|
|
|
|
1815
|
|
|
$roles = new Roles(); |
1816
|
|
|
$role = $roles->translate_user_to_role( $user ); |
1817
|
|
|
$signed_role = $this->sign_role( $role ); |
1818
|
|
|
|
1819
|
|
|
/** |
1820
|
|
|
* Filter the URL of the first time the user gets redirected back to your site for connection |
1821
|
|
|
* data processing. |
1822
|
|
|
* |
1823
|
|
|
* @since 8.0.0 |
1824
|
|
|
* |
1825
|
|
|
* @param string $redirect_url Defaults to the site admin URL. |
1826
|
|
|
*/ |
1827
|
|
|
$processing_url = apply_filters( 'jetpack_connect_processing_url', admin_url( 'admin.php' ) ); |
1828
|
|
|
|
1829
|
|
|
/** |
1830
|
|
|
* Filter the URL to redirect the user back to when the authorization process |
1831
|
|
|
* is complete. |
1832
|
|
|
* |
1833
|
|
|
* @since 8.0.0 |
1834
|
|
|
* |
1835
|
|
|
* @param string $redirect_url Defaults to the site URL. |
1836
|
|
|
*/ |
1837
|
|
|
$redirect = apply_filters( 'jetpack_connect_redirect_url', $redirect ); |
1838
|
|
|
|
1839
|
|
|
$secrets = $this->generate_secrets( 'authorize', $user->ID, 2 * HOUR_IN_SECONDS ); |
1840
|
|
|
|
1841
|
|
|
/** |
1842
|
|
|
* Filter the type of authorization. |
1843
|
|
|
* 'calypso' completes authorization on wordpress.com/jetpack/connect |
1844
|
|
|
* while 'jetpack' ( or any other value ) completes the authorization at jetpack.wordpress.com. |
1845
|
|
|
* |
1846
|
|
|
* @since 4.3.3 |
1847
|
|
|
* |
1848
|
|
|
* @param string $auth_type Defaults to 'calypso', can also be 'jetpack'. |
1849
|
|
|
*/ |
1850
|
|
|
$auth_type = apply_filters( 'jetpack_auth_type', 'calypso' ); |
1851
|
|
|
|
1852
|
|
|
/** |
1853
|
|
|
* Filters the user connection request data for additional property addition. |
1854
|
|
|
* |
1855
|
|
|
* @since 8.0.0 |
1856
|
|
|
* |
1857
|
|
|
* @param array $request_data request data. |
1858
|
|
|
*/ |
1859
|
|
|
$body = apply_filters( |
1860
|
|
|
'jetpack_connect_request_body', |
1861
|
|
|
array( |
1862
|
|
|
'response_type' => 'code', |
1863
|
|
|
'client_id' => \Jetpack_Options::get_option( 'id' ), |
1864
|
|
|
'redirect_uri' => add_query_arg( |
1865
|
|
|
array( |
1866
|
|
|
'action' => 'authorize', |
1867
|
|
|
'_wpnonce' => wp_create_nonce( "jetpack-authorize_{$role}_{$redirect}" ), |
1868
|
|
|
'redirect' => rawurlencode( $redirect ), |
1869
|
|
|
), |
1870
|
|
|
esc_url( $processing_url ) |
1871
|
|
|
), |
1872
|
|
|
'state' => $user->ID, |
1873
|
|
|
'scope' => $signed_role, |
1874
|
|
|
'user_email' => $user->user_email, |
1875
|
|
|
'user_login' => $user->user_login, |
1876
|
|
|
'is_active' => $this->is_active(), |
1877
|
|
|
'jp_version' => Constants::get_constant( 'JETPACK__VERSION' ), |
1878
|
|
|
'auth_type' => $auth_type, |
1879
|
|
|
'secret' => $secrets['secret_1'], |
1880
|
|
|
'blogname' => get_option( 'blogname' ), |
1881
|
|
|
'site_url' => site_url(), |
1882
|
|
|
'home_url' => home_url(), |
1883
|
|
|
'site_icon' => get_site_icon_url(), |
1884
|
|
|
'site_lang' => get_locale(), |
1885
|
|
|
'site_created' => $this->get_assumed_site_creation_date(), |
1886
|
|
|
) |
1887
|
|
|
); |
1888
|
|
|
|
1889
|
|
|
$body = $this->apply_activation_source_to_args( urlencode_deep( $body ) ); |
1890
|
|
|
|
1891
|
|
|
$api_url = $this->api_url( 'authorize' ); |
1892
|
|
|
|
1893
|
|
|
return add_query_arg( $body, $api_url ); |
1894
|
|
|
} |
1895
|
|
|
|
1896
|
|
|
/** |
1897
|
|
|
* Authorizes the user by obtaining and storing the user token. |
1898
|
|
|
* |
1899
|
|
|
* @param array $data The request data. |
1900
|
|
|
* @return string|\WP_Error Returns a string on success. |
1901
|
|
|
* Returns a \WP_Error on failure. |
1902
|
|
|
*/ |
1903
|
|
|
public function authorize( $data = array() ) { |
1904
|
|
|
/** |
1905
|
|
|
* Action fired when user authorization starts. |
1906
|
|
|
* |
1907
|
|
|
* @since 8.0.0 |
1908
|
|
|
*/ |
1909
|
|
|
do_action( 'jetpack_authorize_starting' ); |
1910
|
|
|
|
1911
|
|
|
$roles = new Roles(); |
1912
|
|
|
$role = $roles->translate_current_user_to_role(); |
1913
|
|
|
|
1914
|
|
|
if ( ! $role ) { |
1915
|
|
|
return new \WP_Error( 'no_role', 'Invalid request.', 400 ); |
|
|
|
|
1916
|
|
|
} |
1917
|
|
|
|
1918
|
|
|
$cap = $roles->translate_role_to_cap( $role ); |
1919
|
|
|
if ( ! $cap ) { |
1920
|
|
|
return new \WP_Error( 'no_cap', 'Invalid request.', 400 ); |
|
|
|
|
1921
|
|
|
} |
1922
|
|
|
|
1923
|
|
|
if ( ! empty( $data['error'] ) ) { |
1924
|
|
|
return new \WP_Error( $data['error'], 'Error included in the request.', 400 ); |
|
|
|
|
1925
|
|
|
} |
1926
|
|
|
|
1927
|
|
|
if ( ! isset( $data['state'] ) ) { |
1928
|
|
|
return new \WP_Error( 'no_state', 'Request must include state.', 400 ); |
|
|
|
|
1929
|
|
|
} |
1930
|
|
|
|
1931
|
|
|
if ( ! ctype_digit( $data['state'] ) ) { |
1932
|
|
|
return new \WP_Error( $data['error'], 'State must be an integer.', 400 ); |
|
|
|
|
1933
|
|
|
} |
1934
|
|
|
|
1935
|
|
|
$current_user_id = get_current_user_id(); |
1936
|
|
|
if ( $current_user_id !== (int) $data['state'] ) { |
1937
|
|
|
return new \WP_Error( 'wrong_state', 'State does not match current user.', 400 ); |
|
|
|
|
1938
|
|
|
} |
1939
|
|
|
|
1940
|
|
|
if ( empty( $data['code'] ) ) { |
1941
|
|
|
return new \WP_Error( 'no_code', 'Request must include an authorization code.', 400 ); |
|
|
|
|
1942
|
|
|
} |
1943
|
|
|
|
1944
|
|
|
$token = $this->get_token( $data ); |
1945
|
|
|
|
1946
|
|
View Code Duplication |
if ( is_wp_error( $token ) ) { |
1947
|
|
|
$code = $token->get_error_code(); |
|
|
|
|
1948
|
|
|
if ( empty( $code ) ) { |
1949
|
|
|
$code = 'invalid_token'; |
1950
|
|
|
} |
1951
|
|
|
return new \WP_Error( $code, $token->get_error_message(), 400 ); |
|
|
|
|
1952
|
|
|
} |
1953
|
|
|
|
1954
|
|
|
if ( ! $token ) { |
1955
|
|
|
return new \WP_Error( 'no_token', 'Error generating token.', 400 ); |
|
|
|
|
1956
|
|
|
} |
1957
|
|
|
|
1958
|
|
|
$is_master_user = ! $this->is_active(); |
1959
|
|
|
|
1960
|
|
|
Utils::update_user_token( $current_user_id, sprintf( '%s.%d', $token, $current_user_id ), $is_master_user ); |
1961
|
|
|
|
1962
|
|
|
if ( ! $is_master_user ) { |
1963
|
|
|
/** |
1964
|
|
|
* Action fired when a secondary user has been authorized. |
1965
|
|
|
* |
1966
|
|
|
* @since 8.0.0 |
1967
|
|
|
*/ |
1968
|
|
|
do_action( 'jetpack_authorize_ending_linked' ); |
1969
|
|
|
return 'linked'; |
1970
|
|
|
} |
1971
|
|
|
|
1972
|
|
|
/** |
1973
|
|
|
* Action fired when the master user has been authorized. |
1974
|
|
|
* |
1975
|
|
|
* @since 8.0.0 |
1976
|
|
|
* |
1977
|
|
|
* @param array $data The request data. |
1978
|
|
|
*/ |
1979
|
|
|
do_action( 'jetpack_authorize_ending_authorized', $data ); |
1980
|
|
|
|
1981
|
|
|
\Jetpack_Options::delete_raw_option( 'jetpack_last_connect_url_check' ); |
1982
|
|
|
|
1983
|
|
|
// Start nonce cleaner. |
1984
|
|
|
wp_clear_scheduled_hook( 'jetpack_clean_nonces' ); |
1985
|
|
|
wp_schedule_event( time(), 'hourly', 'jetpack_clean_nonces' ); |
1986
|
|
|
|
1987
|
|
|
return 'authorized'; |
1988
|
|
|
} |
1989
|
|
|
|
1990
|
|
|
/** |
1991
|
|
|
* Disconnects from the Jetpack servers. |
1992
|
|
|
* Forgets all connection details and tells the Jetpack servers to do the same. |
1993
|
|
|
*/ |
1994
|
|
|
public function disconnect_site() { |
1995
|
|
|
|
1996
|
|
|
} |
1997
|
|
|
|
1998
|
|
|
/** |
1999
|
|
|
* The Base64 Encoding of the SHA1 Hash of the Input. |
2000
|
|
|
* |
2001
|
|
|
* @param string $text The string to hash. |
2002
|
|
|
* @return string |
2003
|
|
|
*/ |
2004
|
|
|
public function sha1_base64( $text ) { |
2005
|
|
|
return base64_encode( sha1( $text, true ) ); // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.obfuscation_base64_encode |
2006
|
|
|
} |
2007
|
|
|
|
2008
|
|
|
/** |
2009
|
|
|
* This function mirrors Jetpack_Data::is_usable_domain() in the WPCOM codebase. |
2010
|
|
|
* |
2011
|
|
|
* @param string $domain The domain to check. |
2012
|
|
|
* |
2013
|
|
|
* @return bool|WP_Error |
2014
|
|
|
*/ |
2015
|
|
|
public function is_usable_domain( $domain ) { |
2016
|
|
|
|
2017
|
|
|
// If it's empty, just fail out. |
2018
|
|
|
if ( ! $domain ) { |
2019
|
|
|
return new \WP_Error( |
2020
|
|
|
'fail_domain_empty', |
|
|
|
|
2021
|
|
|
/* translators: %1$s is a domain name. */ |
2022
|
|
|
sprintf( __( 'Domain `%1$s` just failed is_usable_domain check as it is empty.', 'jetpack' ), $domain ) |
2023
|
|
|
); |
2024
|
|
|
} |
2025
|
|
|
|
2026
|
|
|
/** |
2027
|
|
|
* Skips the usuable domain check when connecting a site. |
2028
|
|
|
* |
2029
|
|
|
* Allows site administrators with domains that fail gethostname-based checks to pass the request to WP.com |
2030
|
|
|
* |
2031
|
|
|
* @since 4.1.0 |
2032
|
|
|
* |
2033
|
|
|
* @param bool If the check should be skipped. Default false. |
2034
|
|
|
*/ |
2035
|
|
|
if ( apply_filters( 'jetpack_skip_usuable_domain_check', false ) ) { |
2036
|
|
|
return true; |
2037
|
|
|
} |
2038
|
|
|
|
2039
|
|
|
// None of the explicit localhosts. |
2040
|
|
|
$forbidden_domains = array( |
2041
|
|
|
'wordpress.com', |
2042
|
|
|
'localhost', |
2043
|
|
|
'localhost.localdomain', |
2044
|
|
|
'127.0.0.1', |
2045
|
|
|
'local.wordpress.test', // VVV pattern. |
2046
|
|
|
'local.wordpress-trunk.test', // VVV pattern. |
2047
|
|
|
'src.wordpress-develop.test', // VVV pattern. |
2048
|
|
|
'build.wordpress-develop.test', // VVV pattern. |
2049
|
|
|
); |
2050
|
|
View Code Duplication |
if ( in_array( $domain, $forbidden_domains, true ) ) { |
2051
|
|
|
return new \WP_Error( |
2052
|
|
|
'fail_domain_forbidden', |
|
|
|
|
2053
|
|
|
sprintf( |
2054
|
|
|
/* translators: %1$s is a domain name. */ |
2055
|
|
|
__( |
2056
|
|
|
'Domain `%1$s` just failed is_usable_domain check as it is in the forbidden array.', |
2057
|
|
|
'jetpack' |
2058
|
|
|
), |
2059
|
|
|
$domain |
2060
|
|
|
) |
2061
|
|
|
); |
2062
|
|
|
} |
2063
|
|
|
|
2064
|
|
|
// No .test or .local domains. |
2065
|
|
View Code Duplication |
if ( preg_match( '#\.(test|local)$#i', $domain ) ) { |
2066
|
|
|
return new \WP_Error( |
2067
|
|
|
'fail_domain_tld', |
|
|
|
|
2068
|
|
|
sprintf( |
2069
|
|
|
/* translators: %1$s is a domain name. */ |
2070
|
|
|
__( |
2071
|
|
|
'Domain `%1$s` just failed is_usable_domain check as it uses an invalid top level domain.', |
2072
|
|
|
'jetpack' |
2073
|
|
|
), |
2074
|
|
|
$domain |
2075
|
|
|
) |
2076
|
|
|
); |
2077
|
|
|
} |
2078
|
|
|
|
2079
|
|
|
// No WPCOM subdomains. |
2080
|
|
View Code Duplication |
if ( preg_match( '#\.WordPress\.com$#i', $domain ) ) { |
2081
|
|
|
return new \WP_Error( |
2082
|
|
|
'fail_subdomain_wpcom', |
|
|
|
|
2083
|
|
|
sprintf( |
2084
|
|
|
/* translators: %1$s is a domain name. */ |
2085
|
|
|
__( |
2086
|
|
|
'Domain `%1$s` just failed is_usable_domain check as it is a subdomain of WordPress.com.', |
2087
|
|
|
'jetpack' |
2088
|
|
|
), |
2089
|
|
|
$domain |
2090
|
|
|
) |
2091
|
|
|
); |
2092
|
|
|
} |
2093
|
|
|
|
2094
|
|
|
// If PHP was compiled without support for the Filter module (very edge case). |
2095
|
|
|
if ( ! function_exists( 'filter_var' ) ) { |
2096
|
|
|
// Just pass back true for now, and let wpcom sort it out. |
2097
|
|
|
return true; |
2098
|
|
|
} |
2099
|
|
|
|
2100
|
|
|
return true; |
2101
|
|
|
} |
2102
|
|
|
|
2103
|
|
|
/** |
2104
|
|
|
* Gets the requested token. |
2105
|
|
|
* |
2106
|
|
|
* Tokens are one of two types: |
2107
|
|
|
* 1. Blog Tokens: These are the "main" tokens. Each site typically has one Blog Token, |
2108
|
|
|
* though some sites can have multiple "Special" Blog Tokens (see below). These tokens |
2109
|
|
|
* are not associated with a user account. They represent the site's connection with |
2110
|
|
|
* the Jetpack servers. |
2111
|
|
|
* 2. User Tokens: These are "sub-"tokens. Each connected user account has one User Token. |
2112
|
|
|
* |
2113
|
|
|
* All tokens look like "{$token_key}.{$private}". $token_key is a public ID for the |
2114
|
|
|
* token, and $private is a secret that should never be displayed anywhere or sent |
2115
|
|
|
* over the network; it's used only for signing things. |
2116
|
|
|
* |
2117
|
|
|
* Blog Tokens can be "Normal" or "Special". |
2118
|
|
|
* * Normal: The result of a normal connection flow. They look like |
2119
|
|
|
* "{$random_string_1}.{$random_string_2}" |
2120
|
|
|
* That is, $token_key and $private are both random strings. |
2121
|
|
|
* Sites only have one Normal Blog Token. Normal Tokens are found in either |
2122
|
|
|
* Jetpack_Options::get_option( 'blog_token' ) (usual) or the JETPACK_BLOG_TOKEN |
2123
|
|
|
* constant (rare). |
2124
|
|
|
* * Special: A connection token for sites that have gone through an alternative |
2125
|
|
|
* connection flow. They look like: |
2126
|
|
|
* ";{$special_id}{$special_version};{$wpcom_blog_id};.{$random_string}" |
2127
|
|
|
* That is, $private is a random string and $token_key has a special structure with |
2128
|
|
|
* lots of semicolons. |
2129
|
|
|
* Most sites have zero Special Blog Tokens. Special tokens are only found in the |
2130
|
|
|
* JETPACK_BLOG_TOKEN constant. |
2131
|
|
|
* |
2132
|
|
|
* In particular, note that Normal Blog Tokens never start with ";" and that |
2133
|
|
|
* Special Blog Tokens always do. |
2134
|
|
|
* |
2135
|
|
|
* When searching for a matching Blog Tokens, Blog Tokens are examined in the following |
2136
|
|
|
* order: |
2137
|
|
|
* 1. Defined Special Blog Tokens (via the JETPACK_BLOG_TOKEN constant) |
2138
|
|
|
* 2. Stored Normal Tokens (via Jetpack_Options::get_option( 'blog_token' )) |
2139
|
|
|
* 3. Defined Normal Tokens (via the JETPACK_BLOG_TOKEN constant) |
2140
|
|
|
* |
2141
|
|
|
* @param int|false $user_id false: Return the Blog Token. int: Return that user's User Token. |
2142
|
|
|
* @param string|false $token_key If provided, check that the token matches the provided input. |
2143
|
|
|
* @param bool|true $suppress_errors If true, return a falsy value when the token isn't found; When false, return a descriptive WP_Error when the token isn't found. |
2144
|
|
|
* |
2145
|
|
|
* @return object|false |
2146
|
|
|
*/ |
2147
|
|
|
public function get_access_token( $user_id = false, $token_key = false, $suppress_errors = true ) { |
2148
|
|
|
$possible_special_tokens = array(); |
2149
|
|
|
$possible_normal_tokens = array(); |
2150
|
|
|
$user_tokens = \Jetpack_Options::get_option( 'user_tokens' ); |
2151
|
|
|
|
2152
|
|
|
if ( $user_id ) { |
|
|
|
|
2153
|
|
|
if ( ! $user_tokens ) { |
2154
|
|
|
return $suppress_errors ? false : new \WP_Error( 'no_user_tokens', __( 'No user tokens found', 'jetpack' ) ); |
|
|
|
|
2155
|
|
|
} |
2156
|
|
|
if ( self::JETPACK_MASTER_USER === $user_id ) { |
2157
|
|
|
$user_id = \Jetpack_Options::get_option( 'master_user' ); |
2158
|
|
|
if ( ! $user_id ) { |
2159
|
|
|
return $suppress_errors ? false : new \WP_Error( 'empty_master_user_option', __( 'No primary user defined', 'jetpack' ) ); |
|
|
|
|
2160
|
|
|
} |
2161
|
|
|
} |
2162
|
|
|
if ( ! isset( $user_tokens[ $user_id ] ) || ! $user_tokens[ $user_id ] ) { |
2163
|
|
|
// translators: %s is the user ID. |
2164
|
|
|
return $suppress_errors ? false : new \WP_Error( 'no_token_for_user', sprintf( __( 'No token for user %d', 'jetpack' ), $user_id ) ); |
|
|
|
|
2165
|
|
|
} |
2166
|
|
|
$user_token_chunks = explode( '.', $user_tokens[ $user_id ] ); |
2167
|
|
View Code Duplication |
if ( empty( $user_token_chunks[1] ) || empty( $user_token_chunks[2] ) ) { |
2168
|
|
|
// translators: %s is the user ID. |
2169
|
|
|
return $suppress_errors ? false : new \WP_Error( 'token_malformed', sprintf( __( 'Token for user %d is malformed', 'jetpack' ), $user_id ) ); |
|
|
|
|
2170
|
|
|
} |
2171
|
|
|
if ( $user_token_chunks[2] !== (string) $user_id ) { |
2172
|
|
|
// translators: %1$d is the ID of the requested user. %2$d is the user ID found in the token. |
2173
|
|
|
return $suppress_errors ? false : new \WP_Error( 'user_id_mismatch', sprintf( __( 'Requesting user_id %1$d does not match token user_id %2$d', 'jetpack' ), $user_id, $user_token_chunks[2] ) ); |
|
|
|
|
2174
|
|
|
} |
2175
|
|
|
$possible_normal_tokens[] = "{$user_token_chunks[0]}.{$user_token_chunks[1]}"; |
2176
|
|
|
} else { |
2177
|
|
|
$stored_blog_token = \Jetpack_Options::get_option( 'blog_token' ); |
2178
|
|
|
if ( $stored_blog_token ) { |
2179
|
|
|
$possible_normal_tokens[] = $stored_blog_token; |
2180
|
|
|
} |
2181
|
|
|
|
2182
|
|
|
$defined_tokens_string = Constants::get_constant( 'JETPACK_BLOG_TOKEN' ); |
2183
|
|
|
|
2184
|
|
|
if ( $defined_tokens_string ) { |
2185
|
|
|
$defined_tokens = explode( ',', $defined_tokens_string ); |
2186
|
|
|
foreach ( $defined_tokens as $defined_token ) { |
2187
|
|
|
if ( ';' === $defined_token[0] ) { |
2188
|
|
|
$possible_special_tokens[] = $defined_token; |
2189
|
|
|
} else { |
2190
|
|
|
$possible_normal_tokens[] = $defined_token; |
2191
|
|
|
} |
2192
|
|
|
} |
2193
|
|
|
} |
2194
|
|
|
} |
2195
|
|
|
|
2196
|
|
|
if ( self::MAGIC_NORMAL_TOKEN_KEY === $token_key ) { |
2197
|
|
|
$possible_tokens = $possible_normal_tokens; |
2198
|
|
|
} else { |
2199
|
|
|
$possible_tokens = array_merge( $possible_special_tokens, $possible_normal_tokens ); |
2200
|
|
|
} |
2201
|
|
|
|
2202
|
|
|
if ( ! $possible_tokens ) { |
|
|
|
|
2203
|
|
|
// If no user tokens were found, it would have failed earlier, so this is about blog token. |
2204
|
|
|
return $suppress_errors ? false : new \WP_Error( 'no_possible_tokens', __( 'No blog token found', 'jetpack' ) ); |
|
|
|
|
2205
|
|
|
} |
2206
|
|
|
|
2207
|
|
|
$valid_token = false; |
2208
|
|
|
|
2209
|
|
|
if ( false === $token_key ) { |
2210
|
|
|
// Use first token. |
2211
|
|
|
$valid_token = $possible_tokens[0]; |
2212
|
|
|
} elseif ( self::MAGIC_NORMAL_TOKEN_KEY === $token_key ) { |
2213
|
|
|
// Use first normal token. |
2214
|
|
|
$valid_token = $possible_tokens[0]; // $possible_tokens only contains normal tokens because of earlier check. |
2215
|
|
|
} else { |
2216
|
|
|
// Use the token matching $token_key or false if none. |
2217
|
|
|
// Ensure we check the full key. |
2218
|
|
|
$token_check = rtrim( $token_key, '.' ) . '.'; |
2219
|
|
|
|
2220
|
|
|
foreach ( $possible_tokens as $possible_token ) { |
2221
|
|
|
if ( hash_equals( substr( $possible_token, 0, strlen( $token_check ) ), $token_check ) ) { |
2222
|
|
|
$valid_token = $possible_token; |
2223
|
|
|
break; |
2224
|
|
|
} |
2225
|
|
|
} |
2226
|
|
|
} |
2227
|
|
|
|
2228
|
|
|
if ( ! $valid_token ) { |
2229
|
|
|
if ( $user_id ) { |
2230
|
|
|
// translators: %d is the user ID. |
2231
|
|
|
return $suppress_errors ? false : new \WP_Error( 'no_valid_token', sprintf( __( 'Invalid token for user %d', 'jetpack' ), $user_id ) ); |
|
|
|
|
2232
|
|
|
} else { |
2233
|
|
|
return $suppress_errors ? false : new \WP_Error( 'no_valid_token', __( 'Invalid blog token', 'jetpack' ) ); |
|
|
|
|
2234
|
|
|
} |
2235
|
|
|
} |
2236
|
|
|
|
2237
|
|
|
return (object) array( |
2238
|
|
|
'secret' => $valid_token, |
2239
|
|
|
'external_user_id' => (int) $user_id, |
2240
|
|
|
); |
2241
|
|
|
} |
2242
|
|
|
|
2243
|
|
|
/** |
2244
|
|
|
* In some setups, $HTTP_RAW_POST_DATA can be emptied during some IXR_Server paths |
2245
|
|
|
* since it is passed by reference to various methods. |
2246
|
|
|
* Capture it here so we can verify the signature later. |
2247
|
|
|
* |
2248
|
|
|
* @param array $methods an array of available XMLRPC methods. |
2249
|
|
|
* @return array the same array, since this method doesn't add or remove anything. |
2250
|
|
|
*/ |
2251
|
|
|
public function xmlrpc_methods( $methods ) { |
2252
|
|
|
$this->raw_post_data = $GLOBALS['HTTP_RAW_POST_DATA']; |
2253
|
|
|
return $methods; |
2254
|
|
|
} |
2255
|
|
|
|
2256
|
|
|
/** |
2257
|
|
|
* Resets the raw post data parameter for testing purposes. |
2258
|
|
|
*/ |
2259
|
|
|
public function reset_raw_post_data() { |
2260
|
|
|
$this->raw_post_data = null; |
2261
|
|
|
} |
2262
|
|
|
|
2263
|
|
|
/** |
2264
|
|
|
* Registering an additional method. |
2265
|
|
|
* |
2266
|
|
|
* @param array $methods an array of available XMLRPC methods. |
2267
|
|
|
* @return array the amended array in case the method is added. |
2268
|
|
|
*/ |
2269
|
|
|
public function public_xmlrpc_methods( $methods ) { |
2270
|
|
|
if ( array_key_exists( 'wp.getOptions', $methods ) ) { |
2271
|
|
|
$methods['wp.getOptions'] = array( $this, 'jetpack_get_options' ); |
2272
|
|
|
} |
2273
|
|
|
return $methods; |
2274
|
|
|
} |
2275
|
|
|
|
2276
|
|
|
/** |
2277
|
|
|
* Handles a getOptions XMLRPC method call. |
2278
|
|
|
* |
2279
|
|
|
* @param array $args method call arguments. |
2280
|
|
|
* @return an amended XMLRPC server options array. |
2281
|
|
|
*/ |
2282
|
|
|
public function jetpack_get_options( $args ) { |
2283
|
|
|
global $wp_xmlrpc_server; |
2284
|
|
|
|
2285
|
|
|
$wp_xmlrpc_server->escape( $args ); |
2286
|
|
|
|
2287
|
|
|
$username = $args[1]; |
2288
|
|
|
$password = $args[2]; |
2289
|
|
|
|
2290
|
|
|
$user = $wp_xmlrpc_server->login( $username, $password ); |
2291
|
|
|
if ( ! $user ) { |
2292
|
|
|
return $wp_xmlrpc_server->error; |
2293
|
|
|
} |
2294
|
|
|
|
2295
|
|
|
$options = array(); |
2296
|
|
|
$user_data = $this->get_connected_user_data(); |
2297
|
|
|
if ( is_array( $user_data ) ) { |
2298
|
|
|
$options['jetpack_user_id'] = array( |
2299
|
|
|
'desc' => __( 'The WP.com user ID of the connected user', 'jetpack' ), |
2300
|
|
|
'readonly' => true, |
2301
|
|
|
'value' => $user_data['ID'], |
2302
|
|
|
); |
2303
|
|
|
$options['jetpack_user_login'] = array( |
2304
|
|
|
'desc' => __( 'The WP.com username of the connected user', 'jetpack' ), |
2305
|
|
|
'readonly' => true, |
2306
|
|
|
'value' => $user_data['login'], |
2307
|
|
|
); |
2308
|
|
|
$options['jetpack_user_email'] = array( |
2309
|
|
|
'desc' => __( 'The WP.com user email of the connected user', 'jetpack' ), |
2310
|
|
|
'readonly' => true, |
2311
|
|
|
'value' => $user_data['email'], |
2312
|
|
|
); |
2313
|
|
|
$options['jetpack_user_site_count'] = array( |
2314
|
|
|
'desc' => __( 'The number of sites of the connected WP.com user', 'jetpack' ), |
2315
|
|
|
'readonly' => true, |
2316
|
|
|
'value' => $user_data['site_count'], |
2317
|
|
|
); |
2318
|
|
|
} |
2319
|
|
|
$wp_xmlrpc_server->blog_options = array_merge( $wp_xmlrpc_server->blog_options, $options ); |
2320
|
|
|
$args = stripslashes_deep( $args ); |
2321
|
|
|
return $wp_xmlrpc_server->wp_getOptions( $args ); |
2322
|
|
|
} |
2323
|
|
|
|
2324
|
|
|
/** |
2325
|
|
|
* Adds Jetpack-specific options to the output of the XMLRPC options method. |
2326
|
|
|
* |
2327
|
|
|
* @param array $options standard Core options. |
2328
|
|
|
* @return array amended options. |
2329
|
|
|
*/ |
2330
|
|
|
public function xmlrpc_options( $options ) { |
2331
|
|
|
$jetpack_client_id = false; |
2332
|
|
|
if ( $this->is_active() ) { |
2333
|
|
|
$jetpack_client_id = \Jetpack_Options::get_option( 'id' ); |
2334
|
|
|
} |
2335
|
|
|
$options['jetpack_version'] = array( |
2336
|
|
|
'desc' => __( 'Jetpack Plugin Version', 'jetpack' ), |
2337
|
|
|
'readonly' => true, |
2338
|
|
|
'value' => Constants::get_constant( 'JETPACK__VERSION' ), |
2339
|
|
|
); |
2340
|
|
|
|
2341
|
|
|
$options['jetpack_client_id'] = array( |
2342
|
|
|
'desc' => __( 'The Client ID/WP.com Blog ID of this site', 'jetpack' ), |
2343
|
|
|
'readonly' => true, |
2344
|
|
|
'value' => $jetpack_client_id, |
2345
|
|
|
); |
2346
|
|
|
return $options; |
2347
|
|
|
} |
2348
|
|
|
|
2349
|
|
|
/** |
2350
|
|
|
* Resets the saved authentication state in between testing requests. |
2351
|
|
|
*/ |
2352
|
|
|
public function reset_saved_auth_state() { |
2353
|
|
|
$this->xmlrpc_verification = null; |
2354
|
|
|
} |
2355
|
|
|
|
2356
|
|
|
/** |
2357
|
|
|
* Sign a user role with the master access token. |
2358
|
|
|
* If not specified, will default to the current user. |
2359
|
|
|
* |
2360
|
|
|
* @access public |
2361
|
|
|
* |
2362
|
|
|
* @param string $role User role. |
2363
|
|
|
* @param int $user_id ID of the user. |
|
|
|
|
2364
|
|
|
* @return string Signed user role. |
2365
|
|
|
*/ |
2366
|
|
|
public function sign_role( $role, $user_id = null ) { |
2367
|
|
|
if ( empty( $user_id ) ) { |
2368
|
|
|
$user_id = (int) get_current_user_id(); |
2369
|
|
|
} |
2370
|
|
|
|
2371
|
|
|
if ( ! $user_id ) { |
2372
|
|
|
return false; |
2373
|
|
|
} |
2374
|
|
|
|
2375
|
|
|
$token = $this->get_access_token(); |
2376
|
|
|
if ( ! $token || is_wp_error( $token ) ) { |
2377
|
|
|
return false; |
2378
|
|
|
} |
2379
|
|
|
|
2380
|
|
|
return $role . ':' . hash_hmac( 'md5', "{$role}|{$user_id}", $token->secret ); |
2381
|
|
|
} |
2382
|
|
|
|
2383
|
|
|
/** |
2384
|
|
|
* Set the plugin instance. |
2385
|
|
|
* |
2386
|
|
|
* @param Plugin $plugin_instance The plugin instance. |
2387
|
|
|
* |
2388
|
|
|
* @return $this |
2389
|
|
|
*/ |
2390
|
|
|
public function set_plugin_instance( Plugin $plugin_instance ) { |
2391
|
|
|
$this->plugin = $plugin_instance; |
2392
|
|
|
|
2393
|
|
|
return $this; |
2394
|
|
|
} |
2395
|
|
|
|
2396
|
|
|
/** |
2397
|
|
|
* Retrieve the plugin management object. |
2398
|
|
|
* |
2399
|
|
|
* @return Plugin |
2400
|
|
|
*/ |
2401
|
|
|
public function get_plugin() { |
2402
|
|
|
return $this->plugin; |
2403
|
|
|
} |
2404
|
|
|
|
2405
|
|
|
/** |
2406
|
|
|
* Get all connected plugins information, excluding those disconnected by user. |
2407
|
|
|
* WARNING: the method cannot be called until Plugin_Storage::configure is called, which happens on plugins_loaded |
2408
|
|
|
* Even if you don't use Jetpack Config, it may be introduced later by other plugins, |
2409
|
|
|
* so please make sure not to run the method too early in the code. |
2410
|
|
|
* |
2411
|
|
|
* @return array|WP_Error |
2412
|
|
|
*/ |
2413
|
|
|
public function get_connected_plugins() { |
2414
|
|
|
$maybe_plugins = Plugin_Storage::get_all( true ); |
2415
|
|
|
|
2416
|
|
|
if ( $maybe_plugins instanceof WP_Error ) { |
2417
|
|
|
return $maybe_plugins; |
2418
|
|
|
} |
2419
|
|
|
|
2420
|
|
|
return $maybe_plugins; |
2421
|
|
|
} |
2422
|
|
|
|
2423
|
|
|
/** |
2424
|
|
|
* Force plugin disconnect. After its called, the plugin will not be allowed to use the connection. |
2425
|
|
|
* Note: this method does not remove any access tokens. |
2426
|
|
|
* |
2427
|
|
|
* @return bool |
2428
|
|
|
*/ |
2429
|
|
|
public function disable_plugin() { |
2430
|
|
|
if ( ! $this->plugin ) { |
2431
|
|
|
return false; |
2432
|
|
|
} |
2433
|
|
|
|
2434
|
|
|
return $this->plugin->disable(); |
2435
|
|
|
} |
2436
|
|
|
|
2437
|
|
|
/** |
2438
|
|
|
* Force plugin reconnect after user-initiated disconnect. |
2439
|
|
|
* After its called, the plugin will be allowed to use the connection again. |
2440
|
|
|
* Note: this method does not initialize access tokens. |
2441
|
|
|
* |
2442
|
|
|
* @return bool |
2443
|
|
|
*/ |
2444
|
|
|
public function enable_plugin() { |
2445
|
|
|
if ( ! $this->plugin ) { |
2446
|
|
|
return false; |
2447
|
|
|
} |
2448
|
|
|
|
2449
|
|
|
return $this->plugin->enable(); |
2450
|
|
|
} |
2451
|
|
|
|
2452
|
|
|
/** |
2453
|
|
|
* Whether the plugin is allowed to use the connection, or it's been disconnected by user. |
2454
|
|
|
* If no plugin slug was passed into the constructor, always returns true. |
2455
|
|
|
* |
2456
|
|
|
* @return bool |
2457
|
|
|
*/ |
2458
|
|
|
public function is_plugin_enabled() { |
2459
|
|
|
if ( ! $this->plugin ) { |
2460
|
|
|
return true; |
2461
|
|
|
} |
2462
|
|
|
|
2463
|
|
|
return $this->plugin->is_enabled(); |
2464
|
|
|
} |
2465
|
|
|
|
2466
|
|
|
} |
2467
|
|
|
|
This check looks for
@param
annotations where the type inferred by our type inference engine differs from the declared type.It makes a suggestion as to what type it considers more descriptive.
Most often this is a case of a parameter that can be null in addition to its declared types.