|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* The Jetpack Connection manager class file. |
|
4
|
|
|
* |
|
5
|
|
|
* @package jetpack-connection |
|
6
|
|
|
*/ |
|
7
|
|
|
|
|
8
|
|
|
namespace Automattic\Jetpack\Connection; |
|
9
|
|
|
|
|
10
|
|
|
use Automattic\Jetpack\Constants; |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* The Jetpack Connection Manager class that is used as a single gateway between WordPress.com |
|
14
|
|
|
* and Jetpack. |
|
15
|
|
|
*/ |
|
16
|
|
|
class Manager implements Manager_Interface { |
|
17
|
|
|
|
|
18
|
|
|
const SECRETS_MISSING = 'secrets_missing'; |
|
19
|
|
|
const SECRETS_EXPIRED = 'secrets_expired'; |
|
20
|
|
|
const SECRETS_OPTION_NAME = 'jetpack_secrets'; |
|
21
|
|
|
const MAGIC_NORMAL_TOKEN_KEY = ';normal;'; |
|
22
|
|
|
const JETPACK_MASTER_USER = true; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* The procedure that should be run to generate secrets. |
|
26
|
|
|
* |
|
27
|
|
|
* @var Callable |
|
28
|
|
|
*/ |
|
29
|
|
|
protected $secret_callable; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* Initializes all needed hooks and request handlers. Handles API calls, upload |
|
33
|
|
|
* requests, authentication requests. Also XMLRPC options requests. |
|
34
|
|
|
* Fallback XMLRPC is also a bridge, but probably can be a class that inherits |
|
35
|
|
|
* this one. Among other things it should strip existing methods. |
|
36
|
|
|
* |
|
37
|
|
|
* @param Array $methods an array of API method names for the Connection to accept and |
|
38
|
|
|
* pass on to existing callables. It's possible to specify whether |
|
39
|
|
|
* each method should be available for unauthenticated calls or not. |
|
40
|
|
|
* @see Jetpack::__construct |
|
41
|
|
|
*/ |
|
42
|
|
|
public function initialize( $methods ) { |
|
43
|
|
|
$methods; |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* Returns true if the current site is connected to WordPress.com. |
|
48
|
|
|
* |
|
49
|
|
|
* @return Boolean is the site connected? |
|
50
|
|
|
*/ |
|
51
|
|
|
public function is_active() { |
|
52
|
|
|
return (bool) $this->get_access_token( self::JETPACK_MASTER_USER ); |
|
|
|
|
|
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* Returns true if the user with the specified identifier is connected to |
|
57
|
|
|
* WordPress.com. |
|
58
|
|
|
* |
|
59
|
|
|
* @param Integer|Boolean $user_id the user identifier. |
|
60
|
|
|
* @return Boolean is the user connected? |
|
61
|
|
|
*/ |
|
62
|
|
View Code Duplication |
public function is_user_connected( $user_id = false ) { |
|
63
|
|
|
$user_id = false === $user_id ? get_current_user_id() : absint( $user_id ); |
|
64
|
|
|
if ( ! $user_id ) { |
|
65
|
|
|
return false; |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
return (bool) $this->get_access_token( $user_id ); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* Get the wpcom user data of the current|specified connected user. |
|
73
|
|
|
* |
|
74
|
|
|
* @param Integer $user_id the user identifier. |
|
|
|
|
|
|
75
|
|
|
* @return Object the user object. |
|
76
|
|
|
*/ |
|
77
|
|
View Code Duplication |
public function get_connected_user_data( $user_id = null ) { |
|
78
|
|
|
if ( ! $user_id ) { |
|
|
|
|
|
|
79
|
|
|
$user_id = get_current_user_id(); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
$transient_key = "jetpack_connected_user_data_$user_id"; |
|
83
|
|
|
|
|
84
|
|
|
if ( $cached_user_data = get_transient( $transient_key ) ) { |
|
85
|
|
|
return $cached_user_data; |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
\Jetpack::load_xml_rpc_client(); |
|
89
|
|
|
$xml = new \Jetpack_IXR_Client( |
|
90
|
|
|
array( |
|
91
|
|
|
'user_id' => $user_id, |
|
92
|
|
|
) |
|
93
|
|
|
); |
|
94
|
|
|
$xml->query( 'wpcom.getUser' ); |
|
95
|
|
|
if ( ! $xml->isError() ) { |
|
96
|
|
|
$user_data = $xml->getResponse(); |
|
97
|
|
|
set_transient( $transient_key, $xml->getResponse(), DAY_IN_SECONDS ); |
|
98
|
|
|
return $user_data; |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
return false; |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
/** |
|
105
|
|
|
* Is the user the connection owner. |
|
106
|
|
|
* |
|
107
|
|
|
* @param Integer $user_id the user identifier. |
|
108
|
|
|
* @return Boolean is the user the connection owner? |
|
109
|
|
|
*/ |
|
110
|
|
|
public function is_connection_owner( $user_id ) { |
|
111
|
|
|
return $user_id; |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
/** |
|
115
|
|
|
* Unlinks the current user from the linked WordPress.com user |
|
116
|
|
|
* |
|
117
|
|
|
* @param Integer $user_id the user identifier. |
|
118
|
|
|
*/ |
|
119
|
|
|
public static function disconnect_user( $user_id ) { |
|
120
|
|
|
return $user_id; |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
/** |
|
124
|
|
|
* Initializes a transport server, whatever it may be, saves into the object property. |
|
125
|
|
|
* Should be changed to be protected. |
|
126
|
|
|
*/ |
|
127
|
|
|
public function initialize_server() { |
|
128
|
|
|
|
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
|
|
/** |
|
132
|
|
|
* Checks if the current request is properly authenticated, bails if not. |
|
133
|
|
|
* Should be changed to be protected. |
|
134
|
|
|
*/ |
|
135
|
|
|
public function require_authentication() { |
|
136
|
|
|
|
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
|
|
/** |
|
140
|
|
|
* Verifies the correctness of the request signature. |
|
141
|
|
|
* Should be changed to be protected. |
|
142
|
|
|
*/ |
|
143
|
|
|
public function verify_signature() { |
|
144
|
|
|
|
|
145
|
|
|
} |
|
146
|
|
|
|
|
147
|
|
|
/** |
|
148
|
|
|
* Attempts Jetpack registration which sets up the site for connection. Should |
|
149
|
|
|
* remain public because the call to action comes from the current site, not from |
|
150
|
|
|
* WordPress.com. |
|
151
|
|
|
* |
|
152
|
|
|
* @return Integer zero on success, or a bitmask on failure. |
|
153
|
|
|
*/ |
|
154
|
|
|
public function register() { |
|
155
|
|
|
return 0; |
|
156
|
|
|
} |
|
157
|
|
|
|
|
158
|
|
|
/** |
|
159
|
|
|
* Returns the callable that would be used to generate secrets. |
|
160
|
|
|
* |
|
161
|
|
|
* @return Callable a function that returns a secure string to be used as a secret. |
|
162
|
|
|
*/ |
|
163
|
|
|
protected function get_secret_callable() { |
|
164
|
|
|
if ( ! isset( $this->secret_callable ) ) { |
|
165
|
|
|
/** |
|
166
|
|
|
* Allows modification of the callable that is used to generate connection secrets. |
|
167
|
|
|
* |
|
168
|
|
|
* @param Callable a function or method that returns a secret string. |
|
169
|
|
|
*/ |
|
170
|
|
|
$this->secret_callable = apply_filters( 'jetpack_connection_secret_generator', 'wp_generate_password' ); |
|
171
|
|
|
} |
|
172
|
|
|
|
|
173
|
|
|
return $this->secret_callable; |
|
174
|
|
|
} |
|
175
|
|
|
|
|
176
|
|
|
/** |
|
177
|
|
|
* Generates two secret tokens and the end of life timestamp for them. |
|
178
|
|
|
* |
|
179
|
|
|
* @param String $action The action name. |
|
180
|
|
|
* @param Integer $user_id The user identifier. |
|
181
|
|
|
* @param Integer $exp Expiration time in seconds. |
|
182
|
|
|
*/ |
|
183
|
|
|
public function generate_secrets( $action, $user_id, $exp ) { |
|
184
|
|
|
$callable = $this->get_secret_callable(); |
|
185
|
|
|
|
|
186
|
|
|
$secrets = \Jetpack_Options::get_raw_option( |
|
187
|
|
|
self::SECRETS_OPTION_NAME, |
|
188
|
|
|
array() |
|
189
|
|
|
); |
|
190
|
|
|
|
|
191
|
|
|
$secret_name = 'jetpack_' . $action . '_' . $user_id; |
|
192
|
|
|
|
|
193
|
|
|
if ( |
|
194
|
|
|
isset( $secrets[ $secret_name ] ) && |
|
195
|
|
|
$secrets[ $secret_name ]['exp'] > time() |
|
196
|
|
|
) { |
|
197
|
|
|
return $secrets[ $secret_name ]; |
|
198
|
|
|
} |
|
199
|
|
|
|
|
200
|
|
|
$secret_value = array( |
|
201
|
|
|
'secret_1' => call_user_func( $callable ), |
|
202
|
|
|
'secret_2' => call_user_func( $callable ), |
|
203
|
|
|
'exp' => time() + $exp, |
|
204
|
|
|
); |
|
205
|
|
|
|
|
206
|
|
|
$secrets[ $secret_name ] = $secret_value; |
|
207
|
|
|
|
|
208
|
|
|
\Jetpack_Options::update_raw_option( self::SECRETS_OPTION_NAME, $secrets ); |
|
209
|
|
|
return $secrets[ $secret_name ]; |
|
210
|
|
|
} |
|
211
|
|
|
|
|
212
|
|
|
/** |
|
213
|
|
|
* Returns two secret tokens and the end of life timestamp for them. |
|
214
|
|
|
* |
|
215
|
|
|
* @param String $action The action name. |
|
216
|
|
|
* @param Integer $user_id The user identifier. |
|
217
|
|
|
* @return string|array an array of secrets or an error string. |
|
218
|
|
|
*/ |
|
219
|
|
|
public function get_secrets( $action, $user_id ) { |
|
220
|
|
|
$secret_name = 'jetpack_' . $action . '_' . $user_id; |
|
221
|
|
|
$secrets = \Jetpack_Options::get_raw_option( |
|
222
|
|
|
self::SECRETS_OPTION_NAME, |
|
223
|
|
|
array() |
|
224
|
|
|
); |
|
225
|
|
|
|
|
226
|
|
|
if ( ! isset( $secrets[ $secret_name ] ) ) { |
|
227
|
|
|
return self::SECRETS_MISSING; |
|
228
|
|
|
} |
|
229
|
|
|
|
|
230
|
|
|
if ( $secrets[ $secret_name ]['exp'] < time() ) { |
|
231
|
|
|
$this->delete_secrets( $action, $user_id ); |
|
232
|
|
|
return self::SECRETS_EXPIRED; |
|
233
|
|
|
} |
|
234
|
|
|
|
|
235
|
|
|
return $secrets[ $secret_name ]; |
|
236
|
|
|
} |
|
237
|
|
|
|
|
238
|
|
|
/** |
|
239
|
|
|
* Deletes secret tokens in case they, for example, have expired. |
|
240
|
|
|
* |
|
241
|
|
|
* @param String $action The action name. |
|
242
|
|
|
* @param Integer $user_id The user identifier. |
|
243
|
|
|
*/ |
|
244
|
|
|
public function delete_secrets( $action, $user_id ) { |
|
245
|
|
|
$secret_name = 'jetpack_' . $action . '_' . $user_id; |
|
246
|
|
|
$secrets = \Jetpack_Options::get_raw_option( |
|
247
|
|
|
self::SECRETS_OPTION_NAME, |
|
248
|
|
|
array() |
|
249
|
|
|
); |
|
250
|
|
|
if ( isset( $secrets[ $secret_name ] ) ) { |
|
251
|
|
|
unset( $secrets[ $secret_name ] ); |
|
252
|
|
|
\Jetpack_Options::update_raw_option( self::SECRETS_OPTION_NAME, $secrets ); |
|
253
|
|
|
} |
|
254
|
|
|
} |
|
255
|
|
|
|
|
256
|
|
|
/** |
|
257
|
|
|
* Responds to a WordPress.com call to register the current site. |
|
258
|
|
|
* Should be changed to protected. |
|
259
|
|
|
* |
|
260
|
|
|
* @param array $registration_data Array of [ secret_1, user_id ]. |
|
261
|
|
|
*/ |
|
262
|
|
|
public function handle_registration( array $registration_data ) { |
|
263
|
|
|
list( $registration_secret_1, $registration_user_id ) = $registration_data; |
|
264
|
|
|
if ( empty( $registration_user_id ) ) { |
|
265
|
|
|
return new \WP_Error( 'registration_state_invalid', __( 'Invalid Registration State', 'jetpack' ), 400 ); |
|
|
|
|
|
|
266
|
|
|
} |
|
267
|
|
|
|
|
268
|
|
|
return $this->verify_secrets( 'register', $registration_secret_1, (int) $registration_user_id ); |
|
269
|
|
|
} |
|
270
|
|
|
|
|
271
|
|
|
/** |
|
272
|
|
|
* Verify a Previously Generated Secret. |
|
273
|
|
|
* |
|
274
|
|
|
* @param string $action The type of secret to verify. |
|
275
|
|
|
* @param string $secret_1 The secret string to compare to what is stored. |
|
276
|
|
|
* @param int $user_id The user ID of the owner of the secret. |
|
277
|
|
|
*/ |
|
278
|
|
|
protected function verify_secrets( $action, $secret_1, $user_id ) { |
|
279
|
|
|
$allowed_actions = array( 'register', 'authorize', 'publicize' ); |
|
280
|
|
|
if ( ! in_array( $action, $allowed_actions, true ) ) { |
|
281
|
|
|
return new \WP_Error( 'unknown_verification_action', 'Unknown Verification Action', 400 ); |
|
|
|
|
|
|
282
|
|
|
} |
|
283
|
|
|
|
|
284
|
|
|
$user = get_user_by( 'id', $user_id ); |
|
285
|
|
|
|
|
286
|
|
|
/** |
|
287
|
|
|
* We've begun verifying the previously generated secret. |
|
288
|
|
|
* |
|
289
|
|
|
* @since 7.5.0 |
|
290
|
|
|
* |
|
291
|
|
|
* @param string $action The type of secret to verify. |
|
292
|
|
|
* @param \WP_User $user The user object. |
|
293
|
|
|
*/ |
|
294
|
|
|
do_action( 'jetpack_verify_secrets_begin', $action, $user ); |
|
295
|
|
|
|
|
296
|
|
|
$return_error = function( \WP_Error $error ) use ( $action, $user ) { |
|
297
|
|
|
/** |
|
298
|
|
|
* Verifying of the previously generated secret has failed. |
|
299
|
|
|
* |
|
300
|
|
|
* @since 7.5.0 |
|
301
|
|
|
* |
|
302
|
|
|
* @param string $action The type of secret to verify. |
|
303
|
|
|
* @param \WP_User $user The user object. |
|
304
|
|
|
* @param \WP_Error $error The error object. |
|
305
|
|
|
*/ |
|
306
|
|
|
do_action( 'jetpack_verify_secrets_fail', $action, $user, $error ); |
|
307
|
|
|
|
|
308
|
|
|
return $error; |
|
309
|
|
|
}; |
|
310
|
|
|
|
|
311
|
|
|
$stored_secrets = $this->get_secrets( $action, $user_id ); |
|
312
|
|
|
$this->delete_secrets( $action, $user_id ); |
|
313
|
|
|
|
|
314
|
|
|
if ( empty( $secret_1 ) ) { |
|
315
|
|
|
return $return_error( |
|
316
|
|
|
new \WP_Error( |
|
317
|
|
|
'verify_secret_1_missing', |
|
|
|
|
|
|
318
|
|
|
/* translators: "%s" is the name of a paramter. It can be either "secret_1" or "state". */ |
|
319
|
|
|
sprintf( __( 'The required "%s" parameter is missing.', 'jetpack' ), 'secret_1' ), |
|
320
|
|
|
400 |
|
321
|
|
|
) |
|
322
|
|
|
); |
|
323
|
|
|
} elseif ( ! is_string( $secret_1 ) ) { |
|
324
|
|
|
return $return_error( |
|
325
|
|
|
new \WP_Error( |
|
326
|
|
|
'verify_secret_1_malformed', |
|
|
|
|
|
|
327
|
|
|
/* translators: "%s" is the name of a paramter. It can be either "secret_1" or "state". */ |
|
328
|
|
|
sprintf( __( 'The required "%s" parameter is malformed.', 'jetpack' ), 'secret_1' ), |
|
329
|
|
|
400 |
|
330
|
|
|
) |
|
331
|
|
|
); |
|
332
|
|
|
} elseif ( empty( $user_id ) ) { |
|
333
|
|
|
// $user_id is passed around during registration as "state". |
|
334
|
|
|
return $return_error( |
|
335
|
|
|
new \WP_Error( |
|
336
|
|
|
'state_missing', |
|
|
|
|
|
|
337
|
|
|
/* translators: "%s" is the name of a paramter. It can be either "secret_1" or "state". */ |
|
338
|
|
|
sprintf( __( 'The required "%s" parameter is missing.', 'jetpack' ), 'state' ), |
|
339
|
|
|
400 |
|
340
|
|
|
) |
|
341
|
|
|
); |
|
342
|
|
|
} elseif ( ! ctype_digit( (string) $user_id ) ) { |
|
343
|
|
|
return $return_error( |
|
344
|
|
|
new \WP_Error( |
|
345
|
|
|
'verify_secret_1_malformed', |
|
|
|
|
|
|
346
|
|
|
/* translators: "%s" is the name of a paramter. It can be either "secret_1" or "state". */ |
|
347
|
|
|
sprintf( __( 'The required "%s" parameter is malformed.', 'jetpack' ), 'state' ), |
|
348
|
|
|
400 |
|
349
|
|
|
) |
|
350
|
|
|
); |
|
351
|
|
|
} |
|
352
|
|
|
|
|
353
|
|
|
if ( ! $stored_secrets ) { |
|
354
|
|
|
return $return_error( |
|
355
|
|
|
new \WP_Error( |
|
356
|
|
|
'verify_secrets_missing', |
|
|
|
|
|
|
357
|
|
|
__( 'Verification secrets not found', 'jetpack' ), |
|
358
|
|
|
400 |
|
359
|
|
|
) |
|
360
|
|
|
); |
|
361
|
|
|
} elseif ( is_wp_error( $stored_secrets ) ) { |
|
362
|
|
|
$stored_secrets->add_data( 400 ); |
|
|
|
|
|
|
363
|
|
|
return $return_error( $stored_secrets ); |
|
364
|
|
|
} elseif ( empty( $stored_secrets['secret_1'] ) || empty( $stored_secrets['secret_2'] ) || empty( $stored_secrets['exp'] ) ) { |
|
365
|
|
|
return $return_error( |
|
366
|
|
|
new \WP_Error( |
|
367
|
|
|
'verify_secrets_incomplete', |
|
|
|
|
|
|
368
|
|
|
__( 'Verification secrets are incomplete', 'jetpack' ), |
|
369
|
|
|
400 |
|
370
|
|
|
) |
|
371
|
|
|
); |
|
372
|
|
|
} elseif ( ! hash_equals( $secret_1, $stored_secrets['secret_1'] ) ) { |
|
373
|
|
|
return $return_error( |
|
374
|
|
|
new \WP_Error( |
|
375
|
|
|
'verify_secrets_mismatch', |
|
|
|
|
|
|
376
|
|
|
__( 'Secret mismatch', 'jetpack' ), |
|
377
|
|
|
400 |
|
378
|
|
|
) |
|
379
|
|
|
); |
|
380
|
|
|
} |
|
381
|
|
|
|
|
382
|
|
|
/** |
|
383
|
|
|
* We've succeeded at verifying the previously generated secret. |
|
384
|
|
|
* |
|
385
|
|
|
* @since 7.5.0 |
|
386
|
|
|
* |
|
387
|
|
|
* @param string $action The type of secret to verify. |
|
388
|
|
|
* @param \WP_User $user The user object. |
|
389
|
|
|
*/ |
|
390
|
|
|
do_action( 'jetpack_verify_secrets_success', $action, $user ); |
|
391
|
|
|
|
|
392
|
|
|
return $stored_secrets['secret_2']; |
|
393
|
|
|
} |
|
394
|
|
|
|
|
395
|
|
|
/** |
|
396
|
|
|
* Responds to a WordPress.com call to authorize the current user. |
|
397
|
|
|
* Should be changed to protected. |
|
398
|
|
|
*/ |
|
399
|
|
|
public function handle_authorization() { |
|
400
|
|
|
|
|
401
|
|
|
} |
|
402
|
|
|
|
|
403
|
|
|
/** |
|
404
|
|
|
* Builds a URL to the Jetpack connection auth page. |
|
405
|
|
|
* This needs rethinking. |
|
406
|
|
|
* |
|
407
|
|
|
* @param bool $raw If true, URL will not be escaped. |
|
408
|
|
|
* @param bool|string $redirect If true, will redirect back to Jetpack wp-admin landing page after connection. |
|
409
|
|
|
* If string, will be a custom redirect. |
|
410
|
|
|
* @param bool|string $from If not false, adds 'from=$from' param to the connect URL. |
|
411
|
|
|
* @param bool $register If true, will generate a register URL regardless of the existing token, since 4.9.0. |
|
412
|
|
|
* |
|
413
|
|
|
* @return string Connect URL |
|
414
|
|
|
*/ |
|
415
|
|
|
public function build_connect_url( $raw, $redirect, $from, $register ) { |
|
416
|
|
|
return array( $raw, $redirect, $from, $register ); |
|
417
|
|
|
} |
|
418
|
|
|
|
|
419
|
|
|
/** |
|
420
|
|
|
* Disconnects from the Jetpack servers. |
|
421
|
|
|
* Forgets all connection details and tells the Jetpack servers to do the same. |
|
422
|
|
|
*/ |
|
423
|
|
|
public function disconnect_site() { |
|
424
|
|
|
|
|
425
|
|
|
} |
|
426
|
|
|
|
|
427
|
|
|
/** |
|
428
|
|
|
* The Base64 Encoding of the SHA1 Hash of the Input. |
|
429
|
|
|
* |
|
430
|
|
|
* @param string $text The string to hash. |
|
431
|
|
|
* @return string |
|
432
|
|
|
*/ |
|
433
|
|
|
public function sha1_base64( $text ) { |
|
434
|
|
|
return base64_encode( sha1( $text, true ) ); // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.obfuscation_base64_encode |
|
435
|
|
|
} |
|
436
|
|
|
|
|
437
|
|
|
/** |
|
438
|
|
|
* This function mirrors Jetpack_Data::is_usable_domain() in the WPCOM codebase. |
|
439
|
|
|
* |
|
440
|
|
|
* @param string $domain The domain to check. |
|
441
|
|
|
* |
|
442
|
|
|
* @return bool|WP_Error |
|
443
|
|
|
*/ |
|
444
|
|
|
public function is_usable_domain( $domain ) { |
|
445
|
|
|
|
|
446
|
|
|
// If it's empty, just fail out. |
|
447
|
|
|
if ( ! $domain ) { |
|
448
|
|
|
return new \WP_Error( |
|
449
|
|
|
'fail_domain_empty', |
|
|
|
|
|
|
450
|
|
|
/* translators: %1$s is a domain name. */ |
|
451
|
|
|
sprintf( __( 'Domain `%1$s` just failed is_usable_domain check as it is empty.', 'jetpack' ), $domain ) |
|
452
|
|
|
); |
|
453
|
|
|
} |
|
454
|
|
|
|
|
455
|
|
|
/** |
|
456
|
|
|
* Skips the usuable domain check when connecting a site. |
|
457
|
|
|
* |
|
458
|
|
|
* Allows site administrators with domains that fail gethostname-based checks to pass the request to WP.com |
|
459
|
|
|
* |
|
460
|
|
|
* @since 4.1.0 |
|
461
|
|
|
* |
|
462
|
|
|
* @param bool If the check should be skipped. Default false. |
|
463
|
|
|
*/ |
|
464
|
|
|
if ( apply_filters( 'jetpack_skip_usuable_domain_check', false ) ) { |
|
465
|
|
|
return true; |
|
466
|
|
|
} |
|
467
|
|
|
|
|
468
|
|
|
// None of the explicit localhosts. |
|
469
|
|
|
$forbidden_domains = array( |
|
470
|
|
|
'wordpress.com', |
|
471
|
|
|
'localhost', |
|
472
|
|
|
'localhost.localdomain', |
|
473
|
|
|
'127.0.0.1', |
|
474
|
|
|
'local.wordpress.test', // VVV pattern. |
|
475
|
|
|
'local.wordpress-trunk.test', // VVV pattern. |
|
476
|
|
|
'src.wordpress-develop.test', // VVV pattern. |
|
477
|
|
|
'build.wordpress-develop.test', // VVV pattern. |
|
478
|
|
|
); |
|
479
|
|
View Code Duplication |
if ( in_array( $domain, $forbidden_domains, true ) ) { |
|
480
|
|
|
return new \WP_Error( |
|
481
|
|
|
'fail_domain_forbidden', |
|
|
|
|
|
|
482
|
|
|
sprintf( |
|
483
|
|
|
/* translators: %1$s is a domain name. */ |
|
484
|
|
|
__( |
|
485
|
|
|
'Domain `%1$s` just failed is_usable_domain check as it is in the forbidden array.', |
|
486
|
|
|
'jetpack' |
|
487
|
|
|
), |
|
488
|
|
|
$domain |
|
489
|
|
|
) |
|
490
|
|
|
); |
|
491
|
|
|
} |
|
492
|
|
|
|
|
493
|
|
|
// No .test or .local domains. |
|
494
|
|
View Code Duplication |
if ( preg_match( '#\.(test|local)$#i', $domain ) ) { |
|
495
|
|
|
return new \WP_Error( |
|
496
|
|
|
'fail_domain_tld', |
|
|
|
|
|
|
497
|
|
|
sprintf( |
|
498
|
|
|
/* translators: %1$s is a domain name. */ |
|
499
|
|
|
__( |
|
500
|
|
|
'Domain `%1$s` just failed is_usable_domain check as it uses an invalid top level domain.', |
|
501
|
|
|
'jetpack' |
|
502
|
|
|
), |
|
503
|
|
|
$domain |
|
504
|
|
|
) |
|
505
|
|
|
); |
|
506
|
|
|
} |
|
507
|
|
|
|
|
508
|
|
|
// No WPCOM subdomains. |
|
509
|
|
View Code Duplication |
if ( preg_match( '#\.WordPress\.com$#i', $domain ) ) { |
|
510
|
|
|
return new \WP_Error( |
|
511
|
|
|
'fail_subdomain_wpcom', |
|
|
|
|
|
|
512
|
|
|
sprintf( |
|
513
|
|
|
/* translators: %1$s is a domain name. */ |
|
514
|
|
|
__( |
|
515
|
|
|
'Domain `%1$s` just failed is_usable_domain check as it is a subdomain of WordPress.com.', |
|
516
|
|
|
'jetpack' |
|
517
|
|
|
), |
|
518
|
|
|
$domain |
|
519
|
|
|
) |
|
520
|
|
|
); |
|
521
|
|
|
} |
|
522
|
|
|
|
|
523
|
|
|
// If PHP was compiled without support for the Filter module (very edge case). |
|
524
|
|
|
if ( ! function_exists( 'filter_var' ) ) { |
|
525
|
|
|
// Just pass back true for now, and let wpcom sort it out. |
|
526
|
|
|
return true; |
|
527
|
|
|
} |
|
528
|
|
|
|
|
529
|
|
|
return true; |
|
530
|
|
|
} |
|
531
|
|
|
|
|
532
|
|
|
/** |
|
533
|
|
|
* Gets the requested token. |
|
534
|
|
|
* |
|
535
|
|
|
* Tokens are one of two types: |
|
536
|
|
|
* 1. Blog Tokens: These are the "main" tokens. Each site typically has one Blog Token, |
|
537
|
|
|
* though some sites can have multiple "Special" Blog Tokens (see below). These tokens |
|
538
|
|
|
* are not associated with a user account. They represent the site's connection with |
|
539
|
|
|
* the Jetpack servers. |
|
540
|
|
|
* 2. User Tokens: These are "sub-"tokens. Each connected user account has one User Token. |
|
541
|
|
|
* |
|
542
|
|
|
* All tokens look like "{$token_key}.{$private}". $token_key is a public ID for the |
|
543
|
|
|
* token, and $private is a secret that should never be displayed anywhere or sent |
|
544
|
|
|
* over the network; it's used only for signing things. |
|
545
|
|
|
* |
|
546
|
|
|
* Blog Tokens can be "Normal" or "Special". |
|
547
|
|
|
* * Normal: The result of a normal connection flow. They look like |
|
548
|
|
|
* "{$random_string_1}.{$random_string_2}" |
|
549
|
|
|
* That is, $token_key and $private are both random strings. |
|
550
|
|
|
* Sites only have one Normal Blog Token. Normal Tokens are found in either |
|
551
|
|
|
* Jetpack_Options::get_option( 'blog_token' ) (usual) or the JETPACK_BLOG_TOKEN |
|
552
|
|
|
* constant (rare). |
|
553
|
|
|
* * Special: A connection token for sites that have gone through an alternative |
|
554
|
|
|
* connection flow. They look like: |
|
555
|
|
|
* ";{$special_id}{$special_version};{$wpcom_blog_id};.{$random_string}" |
|
556
|
|
|
* That is, $private is a random string and $token_key has a special structure with |
|
557
|
|
|
* lots of semicolons. |
|
558
|
|
|
* Most sites have zero Special Blog Tokens. Special tokens are only found in the |
|
559
|
|
|
* JETPACK_BLOG_TOKEN constant. |
|
560
|
|
|
* |
|
561
|
|
|
* In particular, note that Normal Blog Tokens never start with ";" and that |
|
562
|
|
|
* Special Blog Tokens always do. |
|
563
|
|
|
* |
|
564
|
|
|
* When searching for a matching Blog Tokens, Blog Tokens are examined in the following |
|
565
|
|
|
* order: |
|
566
|
|
|
* 1. Defined Special Blog Tokens (via the JETPACK_BLOG_TOKEN constant) |
|
567
|
|
|
* 2. Stored Normal Tokens (via Jetpack_Options::get_option( 'blog_token' )) |
|
568
|
|
|
* 3. Defined Normal Tokens (via the JETPACK_BLOG_TOKEN constant) |
|
569
|
|
|
* |
|
570
|
|
|
* @param int|false $user_id false: Return the Blog Token. int: Return that user's User Token. |
|
571
|
|
|
* @param string|false $token_key If provided, check that the token matches the provided input. |
|
572
|
|
|
* @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. |
|
573
|
|
|
* |
|
574
|
|
|
* @return object|false |
|
575
|
|
|
*/ |
|
576
|
|
|
public function get_access_token( $user_id = false, $token_key = false, $suppress_errors = true ) { |
|
577
|
|
|
$possible_special_tokens = array(); |
|
578
|
|
|
$possible_normal_tokens = array(); |
|
579
|
|
|
$user_tokens = \Jetpack_Options::get_option( 'user_tokens' ); |
|
580
|
|
|
|
|
581
|
|
|
if ( $user_id ) { |
|
|
|
|
|
|
582
|
|
|
if ( ! $user_tokens ) { |
|
583
|
|
|
return $suppress_errors ? false : new \WP_Error( 'no_user_tokens' ); |
|
|
|
|
|
|
584
|
|
|
} |
|
585
|
|
|
if ( self::JETPACK_MASTER_USER === $user_id ) { |
|
586
|
|
|
$user_id = \Jetpack_Options::get_option( 'master_user' ); |
|
587
|
|
|
if ( ! $user_id ) { |
|
588
|
|
|
return $suppress_errors ? false : new \WP_Error( 'empty_master_user_option' ); |
|
|
|
|
|
|
589
|
|
|
} |
|
590
|
|
|
} |
|
591
|
|
|
if ( ! isset( $user_tokens[ $user_id ] ) || ! $user_tokens[ $user_id ] ) { |
|
592
|
|
|
return $suppress_errors ? false : new \WP_Error( 'no_token_for_user' ); |
|
|
|
|
|
|
593
|
|
|
} |
|
594
|
|
|
$user_token_chunks = explode( '.', $user_tokens[ $user_id ] ); |
|
595
|
|
|
if ( empty( $user_token_chunks[1] ) || empty( $user_token_chunks[2] ) ) { |
|
596
|
|
|
return $suppress_errors ? false : new \WP_Error( 'token_missing_two_periods' ); |
|
|
|
|
|
|
597
|
|
|
} |
|
598
|
|
|
if ( $user_token_chunks[2] !== (string) $user_id ) { |
|
599
|
|
|
return $suppress_errors ? false : new \WP_Error( 'user_id_mismatch' ); |
|
|
|
|
|
|
600
|
|
|
} |
|
601
|
|
|
$possible_normal_tokens[] = "{$user_token_chunks[0]}.{$user_token_chunks[1]}"; |
|
602
|
|
|
} else { |
|
603
|
|
|
$stored_blog_token = \Jetpack_Options::get_option( 'blog_token' ); |
|
604
|
|
|
if ( $stored_blog_token ) { |
|
605
|
|
|
$possible_normal_tokens[] = $stored_blog_token; |
|
606
|
|
|
} |
|
607
|
|
|
|
|
608
|
|
|
$defined_tokens = Constants::is_defined( 'JETPACK_BLOG_TOKEN' ) |
|
609
|
|
|
? explode( ',', Constants::get_constant( 'JETPACK_BLOG_TOKEN' ) ) |
|
610
|
|
|
: array(); |
|
611
|
|
|
|
|
612
|
|
|
foreach ( $defined_tokens as $defined_token ) { |
|
613
|
|
|
if ( ';' === $defined_token[0] ) { |
|
614
|
|
|
$possible_special_tokens[] = $defined_token; |
|
615
|
|
|
} else { |
|
616
|
|
|
$possible_normal_tokens[] = $defined_token; |
|
617
|
|
|
} |
|
618
|
|
|
} |
|
619
|
|
|
} |
|
620
|
|
|
|
|
621
|
|
|
if ( self::MAGIC_NORMAL_TOKEN_KEY === $token_key ) { |
|
622
|
|
|
$possible_tokens = $possible_normal_tokens; |
|
623
|
|
|
} else { |
|
624
|
|
|
$possible_tokens = array_merge( $possible_special_tokens, $possible_normal_tokens ); |
|
625
|
|
|
} |
|
626
|
|
|
|
|
627
|
|
|
if ( ! $possible_tokens ) { |
|
|
|
|
|
|
628
|
|
|
return $suppress_errors ? false : new \WP_Error( 'no_possible_tokens' ); |
|
|
|
|
|
|
629
|
|
|
} |
|
630
|
|
|
|
|
631
|
|
|
$valid_token = false; |
|
632
|
|
|
|
|
633
|
|
|
if ( false === $token_key ) { |
|
634
|
|
|
// Use first token. |
|
635
|
|
|
$valid_token = $possible_tokens[0]; |
|
636
|
|
|
} elseif ( self::MAGIC_NORMAL_TOKEN_KEY === $token_key ) { |
|
637
|
|
|
// Use first normal token. |
|
638
|
|
|
$valid_token = $possible_tokens[0]; // $possible_tokens only contains normal tokens because of earlier check. |
|
639
|
|
|
} else { |
|
640
|
|
|
// Use the token matching $token_key or false if none. |
|
641
|
|
|
// Ensure we check the full key. |
|
642
|
|
|
$token_check = rtrim( $token_key, '.' ) . '.'; |
|
643
|
|
|
|
|
644
|
|
|
foreach ( $possible_tokens as $possible_token ) { |
|
645
|
|
|
if ( hash_equals( substr( $possible_token, 0, strlen( $token_check ) ), $token_check ) ) { |
|
646
|
|
|
$valid_token = $possible_token; |
|
647
|
|
|
break; |
|
648
|
|
|
} |
|
649
|
|
|
} |
|
650
|
|
|
} |
|
651
|
|
|
|
|
652
|
|
|
if ( ! $valid_token ) { |
|
653
|
|
|
return $suppress_errors ? false : new \WP_Error( 'no_valid_token' ); |
|
|
|
|
|
|
654
|
|
|
} |
|
655
|
|
|
|
|
656
|
|
|
return (object) array( |
|
657
|
|
|
'secret' => $valid_token, |
|
658
|
|
|
'external_user_id' => (int) $user_id, |
|
659
|
|
|
); |
|
660
|
|
|
} |
|
661
|
|
|
} |
|
662
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: