|
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\Connection\Manager_Interface; |
|
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 = 'secrets'; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* The object for managing options. |
|
24
|
|
|
* |
|
25
|
|
|
* @var \Automattic\Jetpack\Options\Manager |
|
26
|
|
|
*/ |
|
27
|
|
|
protected $option_manager; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* The procedure that should be run to generate secrets. |
|
31
|
|
|
* |
|
32
|
|
|
* @var Callable |
|
33
|
|
|
*/ |
|
34
|
|
|
protected $secret_callable; |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* Initializes all needed hooks and request handlers. Handles API calls, upload |
|
38
|
|
|
* requests, authentication requests. Also XMLRPC options requests. |
|
39
|
|
|
* Fallback XMLRPC is also a bridge, but probably can be a class that inherits |
|
40
|
|
|
* this one. Among other things it should strip existing methods. |
|
41
|
|
|
* |
|
42
|
|
|
* @param Array $methods an array of API method names for the Connection to accept and |
|
43
|
|
|
* pass on to existing callables. It's possible to specify whether |
|
44
|
|
|
* each method should be available for unauthenticated calls or not. |
|
45
|
|
|
* @see Jetpack::__construct |
|
46
|
|
|
*/ |
|
47
|
|
|
public function initialize( $methods ) { |
|
48
|
|
|
$methods; |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* Returns true if the current site is connected to WordPress.com. |
|
53
|
|
|
* |
|
54
|
|
|
* @return Boolean is the site connected? |
|
55
|
|
|
*/ |
|
56
|
|
|
public function is_active() { |
|
57
|
|
|
return false; |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* Returns true if the user with the specified identifier is connected to |
|
62
|
|
|
* WordPress.com. |
|
63
|
|
|
* |
|
64
|
|
|
* @param Integer $user_id the user identifier. |
|
65
|
|
|
* @return Boolean is the user connected? |
|
66
|
|
|
*/ |
|
67
|
|
|
public function is_user_connected( $user_id ) { |
|
68
|
|
|
return $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
|
|
|
public function get_connected_user_data( $user_id ) { |
|
78
|
|
|
return $user_id; |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* Is the user the connection owner. |
|
83
|
|
|
* |
|
84
|
|
|
* @param Integer $user_id the user identifier. |
|
85
|
|
|
* @return Boolean is the user the connection owner? |
|
86
|
|
|
*/ |
|
87
|
|
|
public function is_connection_owner( $user_id ) { |
|
88
|
|
|
return $user_id; |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* Unlinks the current user from the linked WordPress.com user |
|
93
|
|
|
* |
|
94
|
|
|
* @param Integer $user_id the user identifier. |
|
95
|
|
|
*/ |
|
96
|
|
|
public static function disconnect_user( $user_id ) { |
|
97
|
|
|
return $user_id; |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
/** |
|
101
|
|
|
* Initializes a transport server, whatever it may be, saves into the object property. |
|
102
|
|
|
* Should be changed to be protected. |
|
103
|
|
|
*/ |
|
104
|
|
|
public function initialize_server() { |
|
105
|
|
|
|
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
/** |
|
109
|
|
|
* Checks if the current request is properly authenticated, bails if not. |
|
110
|
|
|
* Should be changed to be protected. |
|
111
|
|
|
*/ |
|
112
|
|
|
public function require_authentication() { |
|
113
|
|
|
|
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
/** |
|
117
|
|
|
* Verifies the correctness of the request signature. |
|
118
|
|
|
* Should be changed to be protected. |
|
119
|
|
|
*/ |
|
120
|
|
|
public function verify_signature() { |
|
121
|
|
|
|
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
/** |
|
125
|
|
|
* Attempts Jetpack registration which sets up the site for connection. Should |
|
126
|
|
|
* remain public because the call to action comes from the current site, not from |
|
127
|
|
|
* WordPress.com. |
|
128
|
|
|
* |
|
129
|
|
|
* @return Integer zero on success, or a bitmask on failure. |
|
130
|
|
|
*/ |
|
131
|
|
|
public function register() { |
|
132
|
|
|
return 0; |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
/** |
|
136
|
|
|
* Returns the callable that would be used to generate secrets. |
|
137
|
|
|
* |
|
138
|
|
|
* @return Callable a function that returns a secure string to be used as a secret. |
|
139
|
|
|
*/ |
|
140
|
|
|
protected function get_secret_callable() { |
|
141
|
|
|
if ( ! isset( $this->secret_callable ) ) { |
|
142
|
|
|
/** |
|
143
|
|
|
* Allows modification of the callable that is used to generate connection secrets. |
|
144
|
|
|
* |
|
145
|
|
|
* @param Callable a function or method that returns a secret string. |
|
146
|
|
|
*/ |
|
147
|
|
|
$this->secret_callable = apply_filters( 'jetpack_connection_secret_generator', 'wp_generate_password' ); |
|
148
|
|
|
} |
|
149
|
|
|
|
|
150
|
|
|
return $this->secret_callable; |
|
151
|
|
|
} |
|
152
|
|
|
|
|
153
|
|
|
/** |
|
154
|
|
|
* Returns the object that is to be used for all option manipulation. |
|
155
|
|
|
* |
|
156
|
|
|
* @return Object $manager an option manager object. |
|
157
|
|
|
*/ |
|
158
|
|
|
protected function get_option_manager() { |
|
159
|
|
|
if ( ! isset( $this->option_manager ) ) { |
|
160
|
|
|
/** |
|
161
|
|
|
* Allows modification of the object that is used to manipulate stored data. |
|
162
|
|
|
* |
|
163
|
|
|
* @param Jetpack_Options an option manager object. |
|
164
|
|
|
*/ |
|
165
|
|
|
$this->option_manager = apply_filters( 'jetpack_connection_option_manager', false ); |
|
166
|
|
|
} |
|
167
|
|
|
|
|
168
|
|
|
return $this->option_manager; |
|
169
|
|
|
} |
|
170
|
|
|
|
|
171
|
|
|
/** |
|
172
|
|
|
* Generates two secret tokens and the end of life timestamp for them. |
|
173
|
|
|
* |
|
174
|
|
|
* @param String $action The action name. |
|
175
|
|
|
* @param Integer $user_id The user identifier. |
|
176
|
|
|
* @param Integer $exp Expiration time in seconds. |
|
177
|
|
|
*/ |
|
178
|
|
|
public function generate_secrets( $action, $user_id, $exp ) { |
|
179
|
|
|
$callable = $this->get_secret_callable(); |
|
180
|
|
|
|
|
181
|
|
|
$secrets = $this->get_option_manager()->get_raw_option( 'jetpack_secrets', array() ); |
|
182
|
|
|
|
|
183
|
|
|
$secret_name = 'jetpack_' . $action . '_' . $user_id; |
|
184
|
|
|
|
|
185
|
|
|
if ( |
|
186
|
|
|
isset( $secrets[ $secret_name ] ) && |
|
187
|
|
|
$secrets[ $secret_name ]['exp'] > time() |
|
188
|
|
|
) { |
|
189
|
|
|
return $secrets[ $secret_name ]; |
|
190
|
|
|
} |
|
191
|
|
|
|
|
192
|
|
|
$secret_value = array( |
|
193
|
|
|
'secret_1' => call_user_func( $callable ), |
|
194
|
|
|
'secret_2' => call_user_func( $callable ), |
|
195
|
|
|
'exp' => time() + $exp, |
|
196
|
|
|
); |
|
197
|
|
|
|
|
198
|
|
|
$secrets[ $secret_name ] = $secret_value; |
|
199
|
|
|
|
|
200
|
|
|
$this->get_option_manager()->update_option( self::SECRETS_OPTION_NAME, $secrets ); |
|
201
|
|
|
return $secrets[ $secret_name ]; |
|
202
|
|
|
} |
|
203
|
|
|
|
|
204
|
|
|
/** |
|
205
|
|
|
* Returns two secret tokens and the end of life timestamp for them. |
|
206
|
|
|
* |
|
207
|
|
|
* @param String $action The action name. |
|
208
|
|
|
* @param Integer $user_id The user identifier. |
|
209
|
|
|
* @return string|array an array of secrets or an error string. |
|
210
|
|
|
*/ |
|
211
|
|
|
public function get_secrets( $action, $user_id ) { |
|
212
|
|
|
$secret_name = 'jetpack_' . $action . '_' . $user_id; |
|
213
|
|
|
$secrets = $this->get_option_manager()->get_option( self::SECRETS_OPTION_NAME, array() ); |
|
214
|
|
|
|
|
215
|
|
|
if ( ! isset( $secrets[ $secret_name ] ) ) { |
|
216
|
|
|
return self::SECRETS_MISSING; |
|
217
|
|
|
} |
|
218
|
|
|
|
|
219
|
|
|
if ( $secrets[ $secret_name ]['exp'] < time() ) { |
|
220
|
|
|
$this->delete_secrets( $action, $user_id ); |
|
221
|
|
|
return self::SECRETS_EXPIRED; |
|
222
|
|
|
} |
|
223
|
|
|
|
|
224
|
|
|
return $secrets[ $secret_name ]; |
|
225
|
|
|
} |
|
226
|
|
|
|
|
227
|
|
|
/** |
|
228
|
|
|
* Deletes secret tokens in case they, for example, have expired. |
|
229
|
|
|
* |
|
230
|
|
|
* @param String $action The action name. |
|
231
|
|
|
* @param Integer $user_id The user identifier. |
|
232
|
|
|
*/ |
|
233
|
|
|
public function delete_secrets( $action, $user_id ) { |
|
234
|
|
|
$secret_name = 'jetpack_' . $action . '_' . $user_id; |
|
235
|
|
|
$secrets = $this->get_option_manager()->get_option( self::SECRETS_OPTION_NAME, array() ); |
|
236
|
|
|
if ( isset( $secrets[ $secret_name ] ) ) { |
|
237
|
|
|
unset( $secrets[ $secret_name ] ); |
|
238
|
|
|
$this->get_option_manager()->update_option( self::SECRETS_OPTION_NAME, $secrets ); |
|
239
|
|
|
} |
|
240
|
|
|
} |
|
241
|
|
|
|
|
242
|
|
|
/** |
|
243
|
|
|
* Responds to a WordPress.com call to register the current site. |
|
244
|
|
|
* Should be changed to protected. |
|
245
|
|
|
*/ |
|
246
|
|
|
public function handle_registration() { |
|
247
|
|
|
|
|
248
|
|
|
} |
|
249
|
|
|
|
|
250
|
|
|
/** |
|
251
|
|
|
* Responds to a WordPress.com call to authorize the current user. |
|
252
|
|
|
* Should be changed to protected. |
|
253
|
|
|
*/ |
|
254
|
|
|
public function handle_authorization() { |
|
255
|
|
|
|
|
256
|
|
|
} |
|
257
|
|
|
|
|
258
|
|
|
/** |
|
259
|
|
|
* Builds a URL to the Jetpack connection auth page. |
|
260
|
|
|
* This needs rethinking. |
|
261
|
|
|
* |
|
262
|
|
|
* @param bool $raw If true, URL will not be escaped. |
|
263
|
|
|
* @param bool|string $redirect If true, will redirect back to Jetpack wp-admin landing page after connection. |
|
264
|
|
|
* If string, will be a custom redirect. |
|
265
|
|
|
* @param bool|string $from If not false, adds 'from=$from' param to the connect URL. |
|
266
|
|
|
* @param bool $register If true, will generate a register URL regardless of the existing token, since 4.9.0. |
|
267
|
|
|
* |
|
268
|
|
|
* @return string Connect URL |
|
269
|
|
|
*/ |
|
270
|
|
|
public function build_connect_url( $raw, $redirect, $from, $register ) { |
|
271
|
|
|
return array( $raw, $redirect, $from, $register ); |
|
272
|
|
|
} |
|
273
|
|
|
|
|
274
|
|
|
/** |
|
275
|
|
|
* Disconnects from the Jetpack servers. |
|
276
|
|
|
* Forgets all connection details and tells the Jetpack servers to do the same. |
|
277
|
|
|
*/ |
|
278
|
|
|
public function disconnect_site() { |
|
279
|
|
|
|
|
280
|
|
|
} |
|
281
|
|
|
} |
|
282
|
|
|
|