PluginProbe
WP-Optimize – Cache, Compress images, Minify & Clean database to boost page speed & performance / 4.7.0
WP-Optimize – Cache, Compress images, Minify & Clean database to boost page speed & performance v4.7.0
4.7.0 4.6.1 4.6.0 4.5.5 4.5.4 4.5.3 4.5.2 3.2.20 3.2.21 3.2.22 3.2.3 3.2.5 3.2.6 3.2.7 3.2.9 3.3.0 3.3.1 3.3.2 3.4.0 3.4.1 3.4.2 3.5.0 3.6.0 3.7.0 3.7.1 All 111 releases
wp-optimize / vendor / team-updraft / lib-central / central / bootstrap.php

bootstrap.php in WP-Optimize – Cache, Compress images, Minify & Clean database to boost page speed & performance 4.7.0, at vendor/team-updraft/lib-central/central/bootstrap.php

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