PluginProbe
UpdraftPlus: WP Backup & Migration Plugin / 1.22.19
UpdraftPlus: WP Backup & Migration Plugin v1.22.19
1.26.7 1.26.6 1.26.5 1.26.4 1.26.3 1.9.19 1.9.25 1.9.26 1.9.30 1.9.31 1.9.32 1.9.4 1.9.40 1.9.41 1.9.42 1.9.43 1.9.44 1.9.45 1.9.46 1.9.5 1.9.50 1.9.51 1.9.60 1.9.62 1.9.63 All 371 releases
updraftplus / central / bootstrap.php

bootstrap.php in UpdraftPlus: WP Backup & Migration Plugin 1.22.19, at central/bootstrap.php

796 lines 32.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 if (!defined('ABSPATH')) die('No direct access.');
4
5 global $updraftcentral_host_plugin;
6 if (!$updraftcentral_host_plugin->is_host_dir_set()) die('No access.');
7
8 // This file is included during plugins_loaded
9
10 // Load the listener class that we rely on to pick up messages
11 if (!class_exists('UpdraftCentral_Listener')) require_once('listener.php');
12
13 // We exit if class already exists. More common if two or more plugins integrated
14 // the same `UpdraftCentral` client folder.
15 if (!class_exists('UpdraftCentral_Main')) :
16
17 class UpdraftCentral_Main {
18
19 /**
20 * Class constructor
21 */
22 public function __construct() {
23
24 add_action('admin_enqueue_scripts', array($this, 'enqueue_central_scripts'));
25
26 add_action('udrpc_log', array($this, 'udrpc_log'), 10, 3);
27
28 add_action('wp_ajax_updraftcentral_receivepublickey', array($this, 'wp_ajax_updraftcentral_receivepublickey'));
29 add_action('wp_ajax_nopriv_updraftcentral_receivepublickey', array($this, 'wp_ajax_updraftcentral_receivepublickey'));
30
31 // The host plugin's command class is registered in its "plugins_loaded" method (e.g. UpdraftPlus::plugins_loaded()).
32 //
33 // N.B. The new filter "updraftcentral_remotecontrol_command_classes" was introduced on Jan. 2021 and will soon replace the
34 // old filter "updraftplus_remotecontrol_command_classes" (below). This was done in order to synchronize all available filters
35 // and actions related to UpdraftCentral so that we can easily port the UpdraftCentral client code into our other plugins.
36 //
37 // If you happened to use the old filter from any of your projects then you might as well update it with the new filter as the
38 // old filter has already been marked as deprecated, though currently supported as can be seen below but will soon be remove
39 // from this code block.
40 $command_classes = apply_filters('updraftcentral_remotecontrol_command_classes', array(
41 'core' => 'UpdraftCentral_Core_Commands',
42 'updates' => 'UpdraftCentral_Updates_Commands',
43 'users' => 'UpdraftCentral_Users_Commands',
44 'comments' => 'UpdraftCentral_Comments_Commands',
45 'analytics' => 'UpdraftCentral_Analytics_Commands',
46 'plugin' => 'UpdraftCentral_Plugin_Commands',
47 'theme' => 'UpdraftCentral_Theme_Commands',
48 'posts' => 'UpdraftCentral_Posts_Commands',
49 'media' => 'UpdraftCentral_Media_Commands',
50 'pages' => 'UpdraftCentral_Pages_Commands'
51 ));
52
53 // N.B. This "updraftplus_remotecontrol_command_classes" filter has been marked as deprecated and will be remove after May 2021.
54 // Please see above code comment for further explanation and its alternative.
55 $command_classes = apply_filters('updraftplus_remotecontrol_command_classes', $command_classes);
56
57 // If nothing was sent, then there is no incoming message, so no need to set up a listener (or CORS request, etc.). This avoids a DB SELECT query on the option below in the case where it didn't get autoloaded, which is the case when there are no keys.
58 if (!empty($_SERVER['REQUEST_METHOD']) && ('GET' == $_SERVER['REQUEST_METHOD'] || 'POST' == $_SERVER['REQUEST_METHOD']) && (empty($_REQUEST['action']) || 'updraft_central' !== $_REQUEST['action']) && empty($_REQUEST['udcentral_action']) && empty($_REQUEST['udrpc_message'])) return;
59
60 // Remote control keys
61 // These are different from the remote send keys, which are set up in the Migrator add-on
62 $our_keys = $this->get_central_localkeys();
63
64 if (is_array($our_keys) && !empty($our_keys)) {
65 new UpdraftCentral_Listener($our_keys, $command_classes);
66 }
67
68 }
69
70 /**
71 * Enqueues the needed styles and scripts for UpdraftCentral
72 *
73 * @return void
74 */
75 public function enqueue_central_scripts() {
76 global $updraftcentral_host_plugin;
77 $version = $updraftcentral_host_plugin->get_version();
78
79 $enqueue_version = (defined('WP_DEBUG') && WP_DEBUG) ? $version.'.'.time() : $version;
80 $min_or_not = (defined('SCRIPT_DEBUG') && SCRIPT_DEBUG) ? '' : '.min';
81
82 // Fallback to unminified version if the minified version is not found.
83 if (!empty($min_or_not) && !file_exists(UPDRAFTCENTRAL_CLIENT_DIR.'/js/central'.$min_or_not.'.js')) {
84 $min_or_not = '';
85 }
86
87 wp_enqueue_script('updraft-central', UPDRAFTCENTRAL_CLIENT_URL.'/js/central'.$min_or_not.'.js', array(), $enqueue_version);
88 wp_enqueue_style('updraft-central', UPDRAFTCENTRAL_CLIENT_URL.'/css/central'.$min_or_not.'.css', array(), $enqueue_version);
89
90 $localize = array_merge(
91 array(
92 'central_url' => UPDRAFTCENTRAL_CLIENT_URL,
93 'plugin_name' => $updraftcentral_host_plugin->get_plugin_name(),
94 'updraftcentral_credentialtest_nonce' => wp_create_nonce('updraftcentral-credentialtest-nonce'),
95 ),
96 $updraftcentral_host_plugin->translations
97 );
98
99 wp_localize_script('updraft-central', 'uclion', apply_filters('updraftcentral_uclion', $localize));
100 }
101
102 /**
103 * Retrieves current clean url for anchor link where href attribute value is not url (for ex. #div) or empty. Output is not escaped (caller should escape).
104 *
105 * @return String - current clean url
106 */
107 public function get_current_clean_url() {
108
109 // Within an UpdraftCentral context, there should be no prefix on the anchor link
110 if (defined('UPDRAFTCENTRAL_COMMAND') && UPDRAFTCENTRAL_COMMAND || defined('WP_CLI') && WP_CLI) return '';
111
112 if (defined('DOING_AJAX') && DOING_AJAX && !empty($_SERVER['HTTP_REFERER'])) {
113 $current_url = $_SERVER['HTTP_REFERER'];
114 } else {
115 $url_prefix = is_ssl() ? 'https' : 'http';
116 $host = empty($_SERVER['HTTP_HOST']) ? parse_url(network_site_url(), PHP_URL_HOST) : $_SERVER['HTTP_HOST'];
117 $current_url = $url_prefix."://".$host.$_SERVER['REQUEST_URI'];
118 }
119 $remove_query_args = array('state', 'action', 'oauth_verifier', 'nonce', 'updraftplus_instance', 'access_token', 'user_id', 'updraftplus_googledriveauth');
120
121 $query_string = remove_query_arg($remove_query_args, $current_url);
122 return function_exists('wp_unslash') ? wp_unslash($query_string) : stripslashes_deep($query_string);
123 }
124
125 /**
126 * Get the WordPress version
127 *
128 * @return String - the version
129 */
130 public function get_wordpress_version() {
131 static $got_wp_version = false;
132 if (!$got_wp_version) {
133 @include(ABSPATH.WPINC.'/version.php');// phpcs:ignore Generic.PHP.NoSilencedErrors.Discouraged
134 $got_wp_version = $wp_version;// phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UndefinedVariable
135 }
136 return $got_wp_version;
137 }
138
139 /**
140 * Retrieves the UpdraftCentral generated keys
141 *
142 * @param Mixed $default default value to return when option is not found
143 *
144 * @return Mixed
145 */
146 public function get_central_localkeys($default = null) {
147 $option = 'updraft_central_localkeys';
148
149 $ret = get_option($option, $default);
150 return apply_filters('updraftcentral_get_option', $ret, $option, $default);
151 }
152
153 /**
154 * Updates the UpdraftCentral's keys
155 *
156 * @param string $value Specify option value
157 * @param bool $use_cache Whether or not to use the WP options cache
158 * @param string $autoload Whether to autoload (only takes effect on a change of value)
159 *
160 * @return bool
161 */
162 public function update_central_localkeys($value, $use_cache = true, $autoload = 'yes') {
163 $option = 'updraft_central_localkeys';
164
165 return update_option($option, apply_filters('updraftcentral_update_option', $value, $option, $use_cache), $autoload);
166 }
167
168 /**
169 * Receive a new public key in $_GET, and echo a response. Will die() if called.
170 */
171 public function wp_ajax_updraftcentral_receivepublickey() {
172 global $updraftcentral_host_plugin;
173
174 // The actual nonce check is done in the method below
175 if (empty($_GET['_wpnonce']) || empty($_GET['public_key']) || !isset($_GET['updraft_key_index'])) die;
176
177 $result = $this->receive_public_key();
178 if (!is_array($result) || empty($result['responsetype'])) die;
179
180 echo '<html><head><title>UpdraftCentral</title></head><body><h1>'.$updraftcentral_host_plugin->retrieve_show_message('updraftcentral_connection').'</h1><h2>'.htmlspecialchars(network_site_url()).'</h2><p>';
181
182 if ('ok' == $result['responsetype']) {
183 $updraftcentral_host_plugin->retrieve_show_message('updraftcentral_connection_successful', true);
184 } else {
185 echo '<strong>'.$updraftcentral_host_plugin->retrieve_show_message('updraftcentral_connection_failed').'</strong><br>';
186 switch ($result['code']) {
187 case 'unknown_key':
188 $updraftcentral_host_plugin->retrieve_show_message('unknown_key', true);
189 break;
190 case 'not_logged_in':
191 echo $updraftcentral_host_plugin->retrieve_show_message('not_logged_in').' '.$updraftcentral_host_plugin->retrieve_show_message('must_visit_url');
192 break;
193 case 'nonce_failure':
194 $updraftcentral_host_plugin->retrieve_show_message('security_check', true);
195 $updraftcentral_host_plugin->retrieve_show_message('must_visit_link', true);
196 break;
197 case 'already_have':
198 $updraftcentral_host_plugin->retrieve_show_message('connection_already_made', true);
199 break;
200 default:
201 echo htmlspecialchars(print_r($result, true));
202 break;
203 }
204 }
205
206 echo '</p><p><a href="'.esc_url($this->get_current_clean_url()).'" onclick="window.close();">'.$updraftcentral_host_plugin->retrieve_show_message('close').'</a></p>';
207 die;
208 }
209
210 /**
211 * Checks _wpnonce, and if successful, saves the public key found in $_GET
212 *
213 * @return Array - with keys responsetype (can be 'error' or 'ok') and code, indicating whether the parse was successful
214 */
215 private function receive_public_key() {
216
217 if (!is_user_logged_in()) {
218 return array('responsetype' => 'error', 'code' => 'not_logged_in');
219 }
220
221 if (!wp_verify_nonce($_GET['_wpnonce'], 'updraftcentral_receivepublickey')) return array('responsetype' => 'error', 'code' => 'nonce_failure');
222
223 $updraft_key_index = $_GET['updraft_key_index'];
224 $our_keys = $this->get_central_localkeys();
225
226 if (!is_array($our_keys)) $our_keys = array();
227
228 if (!isset($our_keys[$updraft_key_index])) {
229 return array('responsetype' => 'error', 'code' => 'unknown_key');
230 }
231
232 if (!empty($our_keys[$updraft_key_index]['publickey_remote'])) {
233 return array('responsetype' => 'error', 'code' => 'already_have');
234 }
235
236 $our_keys[$updraft_key_index]['publickey_remote'] = base64_decode(stripslashes($_GET['public_key']));
237 $this->update_central_localkeys($our_keys, true, 'no');
238
239 return array('responsetype' => 'ok', 'code' => 'ok');
240 }
241
242 /**
243 * Action parameters, from udrpc: $message, $level, $this->key_name_indicator, $this->debug, $this
244 *
245 * @param string $message The log message
246 * @param string $level Log level
247 * @param string $key_name_indicator This indicates the key name
248 */
249 public function udrpc_log($message, $level, $key_name_indicator) {
250 $udrpc_log = get_site_option('updraftcentral_client_log');
251 if (!is_array($udrpc_log)) $udrpc_log = array();
252
253 $new_item = array(
254 'time' => time(),
255 'level' => $level,
256 'message' => $message,
257 'key_name_indicator' => $key_name_indicator
258 );
259
260 if (!empty($_SERVER['REMOTE_ADDR'])) {
261 $new_item['remote_ip'] = $_SERVER['REMOTE_ADDR'];
262 }
263 if (!empty($_SERVER['HTTP_USER_AGENT'])) {
264 $new_item['http_user_agent'] = $_SERVER['HTTP_USER_AGENT'];
265 }
266 if (!empty($_SERVER['HTTP_X_SECONDARY_USER_AGENT'])) {
267 $new_item['http_secondary_user_agent'] = $_SERVER['HTTP_X_SECONDARY_USER_AGENT'];
268 }
269
270 $udrpc_log[] = $new_item;
271
272 if (count($udrpc_log) > 50) array_shift($udrpc_log);
273
274 update_site_option('updraftcentral_client_log', $udrpc_log);
275 }
276
277 /**
278 * Delete UpdraftCentral Key
279 *
280 * @param array $key_id key_id of UpdraftCentral
281 * @return array which contains deleted flag and key table. If error, Returns array which contains fatal_error flag and fatal_error_message
282 */
283 public function delete_key($key_id) {
284 $our_keys = $this->get_central_localkeys();
285 if (is_array($key_id) && isset($key_id['key_id'])) {
286 $key_id = $key_id['key_id'];
287 }
288
289 if (!is_array($our_keys)) $our_keys = array();
290 if (isset($our_keys[$key_id])) {
291 unset($our_keys[$key_id]);
292 $this->update_central_localkeys($our_keys);
293 }
294 return array('deleted' => 1, 'keys_table' => $this->get_keys_table());
295 }
296
297 /**
298 * Get UpdraftCentral Log
299 *
300 * @return array which contains log_contents. If error, Returns array which contains fatal_error flag and fatal_error_message
301 */
302 public function get_log() {
303 global $updraftcentral_host_plugin;
304
305 $udrpc_log = get_site_option('updraftcentral_client_log');
306 if (!is_array($udrpc_log)) $udrpc_log = array();
307
308 $log_contents = '';
309
310 // Events are appended to the array in the order they happen. So, reversing the order gets them into most-recent-first order.
311 rsort($udrpc_log);
312
313 if (empty($udrpc_log)) {
314 $log_contents = '<em>'.$updraftcentral_host_plugin->retrieve_show_message('nothing_yet_logged').'</em>';
315 }
316
317 foreach ($udrpc_log as $m) {
318
319 // Skip invalid data
320 if (!isset($m['time'])) continue;
321
322 $time = gmdate('Y-m-d H:i:s O', $m['time']);
323 // $level is not used yet. We could put the message in different colours for different levels, if/when it becomes used.
324
325 $key_name_indicator = empty($m['key_name_indicator']) ? '' : $m['key_name_indicator'];
326
327 $log_contents .= '<span title="'.esc_attr(print_r($m, true)).'">'."$time ";
328
329 if (!empty($m['remote_ip'])) $log_contents .= '['.htmlspecialchars($m['remote_ip']).'] ';
330
331 $log_contents .= "[".htmlspecialchars($key_name_indicator)."] ".htmlspecialchars($m['message'])."</span>\n";
332 }
333
334 return array('log_contents' => $log_contents);
335
336 }
337
338 public function create_key($params) {
339 global $updraftcentral_host_plugin;
340
341 // Use the site URL - this means that if the site URL changes, communication ends; which is the case anyway
342 $user = wp_get_current_user();
343
344 $where_send = empty($params['where_send']) ? '' : (string) $params['where_send'];
345
346 if ('__updraftpluscom' != $where_send) {
347 $purl = parse_url($where_send);
348 if (empty($purl) || !array($purl) || empty($purl['scheme']) || empty($purl['host'])) return array('error' => $updraftcentral_host_plugin->retrieve_show_message('invalid_url'));
349 }
350
351 // ENT_HTML5 exists only on PHP 5.4+
352 // @codingStandardsIgnoreLine
353 $flags = defined('ENT_HTML5') ? ENT_QUOTES | ENT_HTML5 : ENT_QUOTES;
354
355 $extra_info = array(
356 'user_id' => $user->ID,
357 'user_login' => $user->user_login,
358 'ms_id' => get_current_blog_id(),
359 'site_title' => html_entity_decode(get_bloginfo('name'), $flags),
360 );
361
362 if ($where_send) {
363 $extra_info['mothership'] = $where_send;
364 if (!empty($params['mothership_firewalled'])) {
365 $extra_info['mothership_firewalled'] = true;
366 }
367 }
368
369 if (!empty($params['key_description'])) {
370 $extra_info['name'] = (string) $params['key_description'];
371 }
372
373 $key_size = (empty($params['key_size']) || !is_numeric($params['key_size']) || $params['key_size'] < 512) ? 2048 : (int) $params['key_size'];
374
375 $extra_info['key_size'] = $key_size;
376
377 $created = $this->create_remote_control_key(false, $extra_info, $where_send);
378
379 if (is_array($created)) {
380 $created['keys_table'] = $this->get_keys_table();
381
382 $created['keys_guide'] = '<h2 class="updraftcentral_wizard_success">'. $updraftcentral_host_plugin->retrieve_show_message('updraftcentral_key_created') .'</h2>';
383
384 if ('__updraftpluscom' != $where_send) {
385 $created['keys_guide'] .= '<div class="updraftcentral_wizard_success"><p>'.sprintf($updraftcentral_host_plugin->retrieve_show_message('need_to_copy_key'), '<a href="'.$where_send.'" target="_blank">UpdraftCentral dashboard</a>').'</p><p>'.$updraftcentral_host_plugin->retrieve_show_message('press_add_site_button').'</p><p>'.sprintf($updraftcentral_host_plugin->retrieve_show_message('detailed_instructions'), '<a target="_blank" href="https://updraftplus.com/updraftcentral-how-to-add-a-site/">UpdraftPlus.com</a>').'</p></div>';
386 } else {
387 $created['keys_guide'] .= '<div class="updraftcentral_wizard_success"><p>'. sprintf($updraftcentral_host_plugin->retrieve_show_message('control_this_site'), '<a target="_blank" href="https://updraftplus.com/my-account/updraftcentral-remote-control/">UpdraftPlus.com</a>').'</p></div>';
388 }
389 }
390
391 return $created;
392 }
393
394 /**
395 * Given an index, return the indicator name
396 *
397 * @param String $index
398 *
399 * @return String
400 */
401 private function indicator_name_from_index($index) {
402 return $index.'.central.updraftplus.com';
403 }
404
405 /**
406 * Gets an RPC object, and sets some defaults on it that we always want
407 *
408 * @param string $indicator_name indicator name
409 * @return array
410 */
411 public function get_udrpc($indicator_name = 'migrator.updraftplus.com') {
412 global $updraftcentral_host_plugin;
413
414 if (!class_exists('UpdraftPlus_Remote_Communications')) include_once($updraftcentral_host_plugin->get_host_dir().'/vendor/team-updraft/common-libs/src/updraft-rpc/class-udrpc.php');
415 $ud_rpc = new UpdraftPlus_Remote_Communications($indicator_name);
416 $ud_rpc->set_can_generate(true);
417
418 return $ud_rpc;
419 }
420
421 private function create_remote_control_key($index = false, $extra_info = array(), $post_it = false) {
422 global $updraftcentral_host_plugin;
423
424 $our_keys = $this->get_central_localkeys();
425 if (!is_array($our_keys)) $our_keys = array();
426
427 if (false === $index) {
428 if (empty($our_keys)) {
429 $index = 0;
430 } else {
431 $index = max(array_keys($our_keys))+1;
432 }
433 }
434
435 $name_hash = $index;
436
437 if (isset($our_keys[$name_hash])) {
438 unset($our_keys[$name_hash]);
439 }
440
441 $indicator_name = $this->indicator_name_from_index($name_hash);
442 $ud_rpc = $this->get_udrpc($indicator_name);
443
444 if ('__updraftpluscom' == $post_it) {
445 $post_it = defined('UPDRAFTPLUS_OVERRIDE_UDCOM_DESTINATION') ? UPDRAFTPLUS_OVERRIDE_UDCOM_DESTINATION : 'https://updraftplus.com/?updraftcentral_action=receive_key';
446 $post_it_description = 'UpdraftPlus.Com';
447 } else {
448 $post_it_description = $post_it;
449 }
450
451 // Normally, key generation takes seconds, even on a slow machine. However, some Windows machines appear to have a setup in which it takes a minute or more. And then, if you're on a double-localhost setup on slow hardware - even worse. It doesn't hurt to just raise the maximum execution time.
452
453 if (function_exists('set_time_limit')) @set_time_limit(UPDRAFTCENTRAL_SET_TIME_LIMIT);// phpcs:ignore Generic.PHP.NoSilencedErrors.Discouraged
454
455 $key_size = (empty($extra_info['key_size']) || !is_numeric($extra_info['key_size']) || $extra_info['key_size'] < 512) ? 2048 : (int) $extra_info['key_size'];
456
457 if (is_object($ud_rpc) && $ud_rpc->generate_new_keypair($key_size)) {
458
459 if ($post_it && empty($extra_info['mothership_firewalled'])) {
460
461 $p_url = parse_url($post_it);
462 if (is_array($p_url) && !empty($p_url['user'])) {
463 $http_username = $p_url['user'];
464 $http_password = empty($p_url['pass']) ? '' : $p_url['pass'];
465 $post_it = $p_url['scheme'].'://'.$p_url['host'];
466 if (!empty($p_url['port'])) $post_it .= ':'.$p_url['port'];
467 $post_it .= $p_url['path'];
468 if (!empty($p_url['query'])) $post_it .= '?'.$p_url['query'];
469 }
470
471 $post_options = array(
472 'timeout' => 90,
473 'body' => array(
474 'updraftcentral_action' => 'receive_key',
475 'key' => $ud_rpc->get_key_remote()
476 )
477 );
478
479 if (!empty($http_username)) {
480 $post_options['headers'] = array(
481 'Authorization' => 'Basic '.base64_encode($http_username.':'.$http_password)
482 );
483 }
484
485 // This option allows the key to be sent to the other side via a known-secure channel (e.g. http over SSL), rather than potentially allowing it to travel over an unencrypted channel (e.g. http back to the user's browser). As such, if specified, it is compulsory for it to work.
486
487 $updraftcentral_host_plugin->register_wp_http_option_hooks();
488
489 $sent_key = wp_remote_post(
490 $post_it,
491 $post_options
492 );
493
494 $updraftcentral_host_plugin->register_wp_http_option_hooks(false);
495
496 if (is_wp_error($sent_key) || empty($sent_key)) {
497 $err_msg = sprintf($updraftcentral_host_plugin->retrieve_show_message('attempt_to_register_failed'), (string) $post_it_description);
498 if (is_wp_error($sent_key)) $err_msg .= ' '.$sent_key->get_error_message().' ('.$sent_key->get_error_code().')';
499 return array(
500 'r' => $err_msg
501 );
502 }
503
504 $response = json_decode(wp_remote_retrieve_body($sent_key), true);
505
506 if (!is_array($response) || !isset($response['key_id']) || !isset($response['key_public'])) {
507 return array(
508 'r' => sprintf($updraftcentral_host_plugin->retrieve_show_message('attempt_to_register_failed'), (string) $post_it_description),
509 'raw' => wp_remote_retrieve_body($sent_key)
510 );
511 }
512
513 $key_hash = hash('sha256', $ud_rpc->get_key_remote());
514
515 $local_bundle = $ud_rpc->get_portable_bundle('base64_with_count', $extra_info, array('key' => array('key_hash' => $key_hash, 'key_id' => $response['key_id'])));
516
517 } elseif ($post_it) {
518 // Don't send; instead, include in the bundle info that the mothership is firewalled; this will then tell the mothership to try the reverse connection instead
519
520 if (is_array($extra_info)) {
521 $extra_info['mothership_firewalled_callback_url'] = wp_nonce_url(admin_url('admin-ajax.php'), 'updraftcentral_receivepublickey');
522 $extra_info['updraft_key_index'] = $index;
523 }
524
525
526 $local_bundle = $ud_rpc->get_portable_bundle('base64_with_count', $extra_info, array('key' => $ud_rpc->get_key_remote()));
527 }
528
529
530 if (isset($extra_info['name'])) {
531 $name = (string) $extra_info['name'];
532 unset($extra_info['name']);
533 } else {
534 $name = 'UpdraftCentral Remote Control';
535 }
536
537 $our_keys[$name_hash] = array(
538 'name' => $name,
539 'key' => $ud_rpc->get_key_local(),
540 'extra_info' => $extra_info,
541 'created' => time(),
542 );
543 // Store the other side's public key
544 if (!empty($response) && is_array($response) && !empty($response['key_public'])) {
545 $our_keys[$name_hash]['publickey_remote'] = $response['key_public'];
546 }
547 $this->update_central_localkeys($our_keys, true, 'no');
548
549 return array(
550 'bundle' => $local_bundle,
551 'r' => $updraftcentral_host_plugin->retrieve_show_message('key_created_successfully').' '.$updraftcentral_host_plugin->retrieve_show_message('copy_paste_key'),
552 );
553 }
554
555 return false;
556
557 }
558
559 /**
560 * Get the HTML for the keys table
561 *
562 * @return String
563 */
564 public function get_keys_table() {
565 global $updraftcentral_host_plugin;
566
567 $ret = '';
568
569 $our_keys = $this->get_central_localkeys();
570 if (!is_array($our_keys)) $our_keys = array();
571
572 if (empty($our_keys)) {
573 $ret .= '<tr><td colspan="2"><em>'.$updraftcentral_host_plugin->retrieve_show_message('no_updraftcentral_dashboards').'</em></td></tr>';
574 }
575
576 foreach ($our_keys as $i => $key) {
577
578 if (empty($key['extra_info'])) continue;
579
580 $user_id = $key['extra_info']['user_id'];
581
582 if (!empty($key['extra_info']['mothership'])) {
583
584 $mothership_url = $key['extra_info']['mothership'];
585
586 if ('__updraftpluscom' == $mothership_url) {
587 $reconstructed_url = 'https://updraftplus.com';
588 } else {
589 $purl = parse_url($mothership_url);
590 $path = empty($purl['path']) ? '' : $purl['path'];
591
592 $reconstructed_url = $purl['scheme'].'://'.$purl['host'].(!empty($purl['port']) ? ':'.$purl['port'] : '').$path;
593 }
594
595 } else {
596 $reconstructed_url = $updraftcentral_host_plugin->retrieve_show_message('unknown');
597 }
598
599 $name = $key['name'];
600
601 $user = get_user_by('id', $user_id);
602
603 $user_display = is_a($user, 'WP_User') ? $user->user_login.' ('.$user->user_email.')' : $updraftcentral_host_plugin->retrieve_show_message('unknown');
604
605 $ret .= '<tr class="updraft_debugrow"><td style="vertical-align:top;">'.htmlspecialchars($name).' ('.htmlspecialchars($i).')</td><td>'.$updraftcentral_host_plugin->retrieve_show_message('access_as_user')." ".htmlspecialchars($user_display)."<br>".$updraftcentral_host_plugin->retrieve_show_message('public_key_sent').' '.htmlspecialchars($reconstructed_url).'<br>';
606
607 if (!empty($key['created'])) {
608 $ret .= $updraftcentral_host_plugin->retrieve_show_message('created').' '.date_i18n(get_option('date_format').' '.get_option('time_format'), $key['created']).'.';
609 if (!empty($key['extra_info']['key_size'])) {
610 $ret .= ' '.sprintf($updraftcentral_host_plugin->retrieve_show_message('key_size'), $key['extra_info']['key_size']).'.';
611 }
612 $ret .= '<br>';
613 }
614
615 $ret .= '<a href="'.esc_url($this->get_current_clean_url()).'" data-key_id="'.esc_attr($i).'" class="updraftcentral_key_delete">'.$updraftcentral_host_plugin->retrieve_show_message('delete').'</a></td></tr>';
616 }
617
618
619 ob_start();
620 ?>
621 <div id="updraftcentral_keys_content" style="margin: 10px 0;">
622 <?php if (!empty($our_keys)) { ?>
623 <a href="<?php echo esc_url($this->get_current_clean_url()); ?>" class="updraftcentral_keys_show hidden-in-updraftcentral"><?php printf($updraftcentral_host_plugin->retrieve_show_message('manage_keys'), count($our_keys)); ?></a>
624 <?php } ?>
625 <table id="updraftcentral_keys_table">
626 <thead>
627 <tr>
628 <th style="text-align:left;"><?php $updraftcentral_host_plugin->retrieve_show_message('key_description', true); ?></th>
629 <th style="text-align:left;"><?php $updraftcentral_host_plugin->retrieve_show_message('details', true); ?></th>
630 </tr>
631 </thead>
632 <tbody>
633 <?php
634
635 echo $ret;
636
637 ?>
638 </tbody>
639 </table>
640 </div>
641 <?php
642 return ob_get_clean();
643 }
644
645 /**
646 * Return HTML markup for the 'create key' section
647 *
648 * @return String - the HTML
649 */
650 private function create_key_markup() {
651 global $updraftcentral_host_plugin;
652
653 ob_start();
654 ?>
655 <div class="create_key_container">
656 <h4 class="updraftcentral_wizard_stage1"> <?php $updraftcentral_host_plugin->retrieve_show_message('connect_to_updraftcentral_dashboard', true); ?></h4>
657 <table style="width: 100%; table-layout:fixed;">
658 <thead></thead>
659 <tbody>
660 <tr class="updraftcentral_wizard_stage1">
661 <td>
662 <div class="updraftcentral_wizard_mothership updraftcentral_wizard_option">
663 <label class="button-primary" tabindex="0">
664 <input checked="checked" type="radio" name="updraftcentral_mothership" id="updraftcentral_mothership_updraftpluscom" style="display: none;">
665 UpdraftPlus.Com
666 </label><br>
667 <div><?php printf($updraftcentral_host_plugin->retrieve_show_message('in_example'), '<a target="_blank" href="https://updraftplus.com/my-account/">'.$updraftcentral_host_plugin->retrieve_show_message('an_account').'</a>'); ?></div>
668
669 </div>
670 <div class="updraftcentral_wizard_self_hosted_stage1 updraftcentral_wizard_option">
671 <label class="button-primary" tabindex="0">
672 <input type="radio" name="updraftcentral_mothership" id="updraftcentral_mothership_other" style="display: none;">
673 <?php $updraftcentral_host_plugin->retrieve_show_message('self_hosted_dashboard', true);?>
674 </label><br>
675 <div><?php printf($updraftcentral_host_plugin->retrieve_show_message('website_installed'), '<a target="_blank" href="https://wordpress.org/plugins/updraftcentral/">UpdraftCentral</a>'); ?></div>
676 </div>
677 <div class="updraftcentral_wizard_self_hosted_stage2" style="float:left; clear:left;display:none;">
678 <p style="font-size: 13px;"><?php echo $updraftcentral_host_plugin->retrieve_show_message('enter_url');?></p>
679 <p style="font-size: 13px;" id="updraftcentral_wizard_stage1_error"></p>
680 <input disabled="disabled" id="updraftcentral_keycreate_mothership" type="text" size="40" placeholder="<?php $updraftcentral_host_plugin->retrieve_show_message('updraftcentral_dashboard_url', true); ?>" value="">
681 <button type="button" class="button button-primary" id="updraftcentral_stage2_go"><?php $updraftcentral_host_plugin->retrieve_show_message('next', true); ?></button>
682 </div>
683 </td>
684 </tr>
685
686 <tr class="updraft_debugrow updraftcentral_wizard_stage2" style="display: none;">
687 <h4 class="updraftcentral_wizard_stage2" style="display: none;"><?php $updraftcentral_host_plugin->retrieve_show_message('updraftcentral_connection_details', true); ?></h4>
688 <td class="updraftcentral_keycreate_description">
689 <?php $updraftcentral_host_plugin->retrieve_show_message('description', true); ?>:
690 <input id="updraftcentral_keycreate_description" type="text" size="20" placeholder="<?php $updraftcentral_host_plugin->retrieve_show_message('enter_description', true); ?>" value="" >
691 </td>
692 </tr>
693
694 <tr class="updraft_debugrow updraftcentral_wizard_stage2" style="display: none;">
695 <td>
696 <?php $updraftcentral_host_plugin->retrieve_show_message('encryption_key_size', true); ?>
697 <select style="" id="updraftcentral_keycreate_keysize">
698 <option value="512"><?php echo sprintf($updraftcentral_host_plugin->retrieve_show_message('bits').' - '.$updraftcentral_host_plugin->retrieve_show_message('easy_to_break'), '512'); ?></option>
699 <option value="1024"><?php echo sprintf($updraftcentral_host_plugin->retrieve_show_message('bits').' - '.$updraftcentral_host_plugin->retrieve_show_message('faster'), '1024'); ?></option>
700 <option value="2048" selected="selected"><?php echo sprintf($updraftcentral_host_plugin->retrieve_show_message('bytes').' - '.$updraftcentral_host_plugin->retrieve_show_message('recommended'), '2048'); ?></option>
701 <option value="4096"><?php echo sprintf($updraftcentral_host_plugin->retrieve_show_message('bits').' - '.$updraftcentral_host_plugin->retrieve_show_message('slower'), '4096'); ?></option>
702 </select>
703 <br>
704 <div id="updraftcentral_keycreate_mothership_firewalled_container">
705 <label>
706 <input id="updraftcentral_keycreate_mothership_firewalled" type="checkbox">
707 <?php $updraftcentral_host_plugin->retrieve_show_message('use_alternative_method', true); ?>
708 <a href="<?php echo esc_url($this->get_current_clean_url()); ?>" id="updraftcentral_keycreate_altmethod_moreinfo_get"><?php $updraftcentral_host_plugin->retrieve_show_message('more_information', true); ?></a>
709 <p id="updraftcentral_keycreate_altmethod_moreinfo" style="display:none; border: 1px dotted; padding: 3px; margin: 2px 10px 2px 24px;">
710 <em><?php $updraftcentral_host_plugin->retrieve_show_message('this_is_useful', true);?></em>
711 </p>
712 </label>
713 </div>
714 </td>
715 </tr>
716
717 <tr class="updraft_debugrow updraftcentral_wizard_stage2" style="display: none;">
718 <td>
719 <button style="margin-top: 5px;" type="button" class="button button-primary" id="updraftcentral_keycreate_go"><?php $updraftcentral_host_plugin->retrieve_show_message('create', true); ?></button>
720 </td>
721 </tr>
722 <tr class="updraft_debugrow updraftcentral_wizard_stage2" style="display: none;">
723 <td>
724 <a id="updraftcentral_stage1_go"><?php $updraftcentral_host_plugin->retrieve_show_message('back', true); ?></a>
725 </td>
726 </tr>
727 </tbody>
728 </table>
729 </div>
730 <?php
731 return ob_get_clean();
732 }
733
734 /**
735 * Get log event viewer mark-up
736 *
737 * @return String - the HTML
738 */
739 private function get_log_markup() {
740 global $updraftcentral_host_plugin;
741
742 ob_start();
743 ?>
744 <div id="updraftcentral_view_log_container" style="margin: 10px 0;">
745 <a href="<?php echo esc_url($this->get_current_clean_url()); ?>" id="updraftcentral_view_log"><?php $updraftcentral_host_plugin->retrieve_show_message('view_log_events', true); ?>...</a><br>
746 <pre id="updraftcentral_view_log_contents" style="min-height: 110px; padding: 0 4px;">
747 </pre>
748 </div>
749 <?php
750 return ob_get_clean();
751 }
752
753 /**
754 * Echo the debug-tools dashboard HTML. Called by the WP action updraftplus_debugtools_dashboard.
755 */
756 public function debugtools_dashboard() {
757 global $updraftcentral_host_plugin;
758
759 $screen = get_current_screen();
760 $hosts = apply_filters('updraftcentral_host_plugins', array());
761 $includes = $updraftcentral_host_plugin->retrieve_show_message('including_description');
762
763 $including_desc = '';
764 foreach ($hosts as $plugin) {
765 if (false !== stripos($screen->id, $plugin)) {
766 $key = str_replace('-', '_', strtolower($plugin)).'_desc';
767 if (isset($includes[$key])) {
768 $including_desc = $includes[$key];
769 break;
770 }
771 }
772 }
773
774 $updraftcentral_description = preg_replace('/\s+/', ' ', sprintf($updraftcentral_host_plugin->retrieve_show_message('updraftcentral_description'), $including_desc));
775 ?>
776 <div class="advanced_tools updraft_central">
777 <h3><?php $updraftcentral_host_plugin->retrieve_show_message('updraftcentral_remote_control', true); ?></h3>
778 <p>
779 <?php echo $updraftcentral_description.' <a target="_blank" href="https://updraftcentral.com">'.$updraftcentral_host_plugin->retrieve_show_message('read_more').'</a>'; ?>
780 </p>
781 <div style="min-height: 310px;" id="updraftcentral_keys">
782 <?php echo $this->create_key_markup(); ?>
783 <?php echo $this->get_keys_table(); ?>
784 <button style="display: none;" type="button" class="button button-primary" id="updraftcentral_wizard_go"><?php $updraftcentral_host_plugin->retrieve_show_message('create_another_key', true); ?></button>
785 <?php echo $this->get_log_markup(); ?>
786 </div>
787 </div>
788 <?php
789 }
790 }
791
792 endif;
793
794 global $updraftcentral_main;
795 $updraftcentral_main = new UpdraftCentral_Main();
796