PluginProbe ʕ •ᴥ•ʔ
UpdraftPlus: WP Backup & Migration Plugin / 1.16.23
UpdraftPlus: WP Backup & Migration Plugin v1.16.23
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 1.9.64 1.11.12 1.4.8 1.11.15 1.4.9 1.11.17 1.5.16 1.11.18 1.5.20 1.11.2 1.5.21 1.11.20 1.5.22 1.11.23 1.5.5 1.11.24 1.5.6 1.11.25 1.5.7 1.11.26 1.5.8 1.11.27 1.5.9 1.11.28 1.6.1 1.11.3 1.6.17 1.11.4 1.6.2 1.11.5 1.6.46 1.11.8 1.7.0 1.11.9 1.7.1 1.12.0 1.7.18 1.12.1 1.7.20 1.12.12 1.7.3 1.12.13 1.7.34 1.12.15 1.7.35 1.12.17 1.7.39 1.12.2 1.7.40 1.12.20 1.7.41 1.12.23 1.8.1 1.12.24 1.8.11 1.12.25 1.8.12 1.12.28 1.8.13 1.12.29 1.8.2 1.12.30 1.8.5 1.12.32 1.8.8 1.12.34 1.9.0 1.12.35 1.9.13 1.12.37 1.9.15 1.12.39 1.9.17 1.12.4 1.12.40 1.12.6 1.13.1 1.13.11 1.13.12 1.13.15 1.13.16 1.13.2 1.13.3 1.13.4 1.13.5 1.13.6 1.13.7 1.13.8 1.13.9 1.14.10 1.14.11 1.14.12 1.14.13 1.14.2 1.14.3 1.14.4 1.14.5 1.14.7 1.14.9 1.15.0 1.15.2 1.15.3 1.15.5 1.15.6 1.15.7 1.16.0 1.16.10 1.16.11 1.16.12 1.16.13 1.16.14 1.16.15 1.16.16 1.16.17 1.16.20 1.16.21 1.16.22 1.16.23 1.16.24 1.16.25 1.16.26 1.16.28 1.16.29 1.16.32 1.16.34 1.16.35 1.16.36 1.16.37 1.16.4 1.16.40 1.16.41 1.16.42 1.16.43 1.16.44 1.16.45 1.16.46 1.16.47 1.16.48 1.16.49 1.16.5 1.16.50 1.16.51 1.16.53 1.16.55 1.16.56 1.16.59 1.16.6 1.16.60 1.16.61 1.16.62 1.16.63 1.16.64 1.16.65 1.16.66 1.16.67 1.16.68 1.16.69 1.16.7 1.16.8 1.16.9 1.2.0 1.2.1 1.2.10 1.2.11 1.2.12 1.2.14 1.2.15 1.2.16 1.2.17 1.2.19 1.2.2 1.2.20 1.2.24 1.2.25 1.2.26 1.2.27 1.2.28 1.2.29 1.2.3 1.2.30 1.2.31 1.2.33 1.2.35 1.2.36 1.2.38 1.2.39 1.2.4 1.2.40 1.2.41 1.2.42 1.2.43 1.2.44 1.2.45 1.2.46 1.2.5 1.2.7 1.2.8 1.2.9 1.22.1 1.22.10 1.22.11 1.22.12 1.22.14 1.22.15 1.22.16 1.22.17 1.22.18 1.22.19 1.22.20 1.22.21 1.22.22 1.22.23 1.22.24 1.22.3 1.22.4 1.22.5 1.22.6 1.22.7 1.22.8 1.22.9 1.23.1 1.23.10 1.23.11 1.23.12 1.23.13 1.23.15 1.23.16 1.23.2 1.23.3 1.23.4 1.23.5 1.23.6 1.23.7 1.23.8 1.23.9 1.24.1 1.24.10 1.24.11 1.24.12 1.24.2 trunk 1.24.3 0.7.4 1.24.4 0.7.7 1.24.5 0.8.28 1.24.6 0.8.29 1.24.7 0.8.30 1.24.8 0.8.31 1.24.9 0.8.32 1.25.1 0.8.33 1.25.2 0.8.36 1.25.3 0.8.37 1.25.5 0.8.50 1.25.6 0.8.51 1.25.7 0.9.1 1.25.8 0.9.10 1.25.9 0.9.11 1.26.1 0.9.12 1.26.2 0.9.2 1.3.10 0.9.20 1.3.12 0.9.21 1.3.14 0.9.22 1.3.15 1.0.10 1.3.17 1.0.11 1.3.18 1.0.12 1.3.19 1.0.15 1.3.2 1.0.16 1.3.20 1.0.18 1.3.22 1.0.20 1.3.23 1.0.3 1.3.24 1.0.4 1.3.25 1.0.5 1.3.3 1.0.6 1.3.4 1.0.7 1.3.6 1.0.8 1.3.7 1.0.9 1.3.8 1.1.0 1.3.9 1.1.10 1.4.0 1.1.11 1.4.10 1.1.12 1.4.11 1.1.13 1.4.12 1.1.14 1.4.13 1.1.15 1.4.14 1.1.16 1.4.15 1.1.17 1.4.2 1.1.2 1.4.27 1.1.3 1.4.28 1.1.5 1.4.29 1.1.6 1.4.30 1.1.8 1.4.4 1.1.9 1.4.48 1.10.1 1.4.5 1.10.3 1.4.6 1.11.1 1.4.7
updraftplus / includes / class-remote-send.php
updraftplus / includes Last commit date
Dropbox2 6 years ago Google 6 years ago checkout-embed 6 years ago cloudfiles 12 years ago handlebars 6 years ago images 9 years ago jquery.serializeJSON 7 years ago jstree 6 years ago labelauty 6 years ago tether 6 years ago tether-shepherd 7 years ago updraftclone 6 years ago S3.php 6 years ago S3compat.php 6 years ago cacert.pem 6 years ago class-backup-history.php 6 years ago class-commands.php 6 years ago class-database-utility.php 6 years ago class-filesystem-functions.php 6 years ago class-job-scheduler.php 6 years ago class-manipulation-functions.php 7 years ago class-partialfileservlet.php 9 years ago class-remote-send.php 6 years ago class-semaphore.php 6 years ago class-storage-methods-interface.php 6 years ago class-udrpc.php 6 years ago class-updraft-dashboard-news.php 6 years ago class-updraftcentral-updraftplus-commands.php 8 years ago class-updraftplus-encryption.php 6 years ago class-wpadmin-commands.php 6 years ago class-zip.php 6 years ago ftp.class.php 7 years ago get-cpanel-quota-usage.pl 12 years ago google-extensions.php 9 years ago jquery-ui.custom.css 7 years ago jquery-ui.custom.min.css 7 years ago jquery-ui.custom.min.css.map 7 years ago jquery.blockUI.js 8 years ago jquery.blockUI.min.js 8 years ago updraft-admin-common.js 6 years ago updraft-admin-common.min.js 6 years ago updraft-notices.php 6 years ago updraft-restorer-skin-compatibility.php 6 years ago updraft-restorer-skin.php 6 years ago updraftcentral.php 7 years ago updraftplus-clone.php 6 years ago updraftplus-login.php 7 years ago updraftplus-notices.php 6 years ago updraftplus-tour.php 7 years ago updraftvault.php 6 years ago
class-remote-send.php
656 lines
1 <?php
2
3 if (!defined('UPDRAFTPLUS_DIR')) die('No direct access allowed.');
4
5 abstract class UpdraftPlus_RemoteSend {
6
7 protected $receivers = array();
8
9 protected $php_events = array();
10
11 /**
12 * Class constructor
13 */
14 public function __construct() {
15 add_action('updraft_migrate_newdestination', array($this, 'updraft_migrate_newdestination'));
16 add_action('updraft_remote_ping_test', array($this, 'updraft_remote_ping_test'));
17 add_action('updraft_migrate_key_create', array($this, 'updraft_migrate_key_create'));
18 add_filter('updraft_migrate_key_create_return', array($this, 'updraft_migrate_key_create_return'), 10, 2);
19 add_action('updraft_migrate_key_delete', array($this, 'updraft_migrate_key_delete'));
20 add_filter('updraftplus_initial_jobdata', array($this, 'updraftplus_initial_jobdata'), 10, 3);
21 add_filter('updraft_printjob_beforewarnings', array($this, 'updraft_printjob_beforewarnings'), 10, 2);
22 add_action('plugins_loaded', array($this, 'plugins_loaded'));
23 }
24
25 /**
26 * Runs upon the WP action plugins_loaded; sets up UDRPC listeners for site-to-site migration
27 */
28 public function plugins_loaded() {
29
30 global $updraftplus;
31
32 // Prevent fatal errors if UD was not loaded (e.g. some CLI method)
33 if (!is_a($updraftplus, 'UpdraftPlus')) return;
34
35 // Create a receiver for each key
36 if (!class_exists('UpdraftPlus_Options')) {
37 error_log("UpdraftPlus_Options class not found: is UpdraftPlus properly installed?");
38 return;
39 }
40 $our_keys = UpdraftPlus_Options::get_updraft_option('updraft_migrator_localkeys');
41 if (is_array($our_keys) && !empty($our_keys)) {
42 foreach ($our_keys as $name_hash => $key) {
43 if (!is_array($key)) return;
44 $ud_rpc = $updraftplus->get_udrpc($name_hash.'.migrator.updraftplus.com');
45 if (!empty($key['sender_public'])) {
46 $ud_rpc->set_message_format(2);
47 $ud_rpc->set_key_local($key['key']);
48 $ud_rpc->set_key_remote($key['sender_public']);
49 } else {
50 $ud_rpc->set_message_format(1);
51 $ud_rpc->set_key_local($key['key']);
52 }
53 $this->receivers[$name_hash] = $ud_rpc;
54 // Create listener (which causes WP actions to be fired when messages are received)
55 $ud_rpc->activate_replay_protection();
56 $ud_rpc->create_listener();
57 }
58 add_filter('udrpc_command_send_chunk', array($this, 'udrpc_command_send_chunk'), 10, 3);
59 add_filter('udrpc_command_get_file_status', array($this, 'udrpc_command_get_file_status'), 10, 3);
60 add_filter('udrpc_command_upload_complete', array($this, 'udrpc_command_upload_complete'), 10, 3);
61 add_filter('udrpc_action', array($this, 'udrpc_action'), 10, 4);
62 }
63 }
64
65 /**
66 * This function will return a response to the remote site on any action
67 *
68 * @param string $response - a string response
69 * @param string $command - the incoming command
70 * @param array $data - an array of response data
71 * @param string $name_indicator - a string to identify the request
72 *
73 * @return array - the array response
74 */
75 public function udrpc_action($response, $command, $data, $name_indicator) {// phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter.Found
76
77 if (is_array($data) && isset($data['sender_public'])) {
78 // Do we already know the sender's public key?
79 $our_keys = UpdraftPlus_Options::get_updraft_option('updraft_migrator_localkeys');
80 if (is_array($our_keys) && preg_match('/^([a-f0-9]+)\.migrator.updraftplus.com$/', $name_indicator, $matches) && !empty($our_keys[$matches[1]]) && empty($our_keys[$matches[1]]['sender_public'])) {
81 // N.B. When the sender sends a public key, that indicates that *all* future communications will use it
82 $our_keys[$matches[1]]['sender_public'] = $data['sender_public'];
83 UpdraftPlus_Options::update_updraft_option('updraft_migrator_localkeys', $our_keys);
84 if (!is_array($response['data'])) $response['data'] = array();
85 $response['data']['got_public'] = 1;
86 }
87 }
88
89 return $response;
90 }
91
92 protected function initialise_listener_error_handling($hash) {
93 global $updraftplus;
94 $updraftplus->error_reporting_stop_when_logged = true;
95 set_error_handler(array($updraftplus, 'php_error'), E_ALL & ~E_STRICT);
96 $this->php_events = array();
97 add_filter('updraftplus_logline', array($this, 'updraftplus_logline'), 10, 4);
98 if (!UpdraftPlus_Options::get_updraft_option('updraft_debug_mode')) return;
99 $updraftplus->nonce = $hash;
100 $updraftplus->logfile_open($hash);
101 }
102
103 protected function return_rpc_message($msg) {
104 if (is_array($msg) && isset($msg['response']) && 'error' == $msg['response']) {
105 global $updraftplus;
106 $updraftplus->log('Unexpected response code in remote communications: '.serialize($msg));
107 }
108 if (!empty($this->php_events)) {
109 if (!isset($msg['data'])) $msg['data'] = null;
110 $msg['data'] = array('php_events' => array(), 'previous_data' => $msg['data']);
111 foreach ($this->php_events as $logline) {
112 $msg['data']['php_events'][] = $logline;
113 }
114 }
115 restore_error_handler();
116
117 return $msg;
118 }
119
120 public function updraftplus_logline($line, $nonce, $level, $uniq_id) {// phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter.Found
121 if ('notice' === $level && 'php_event' === $uniq_id) {
122 $this->php_events[] = $line;
123 }
124 return $line;
125 }
126
127 public function udrpc_command_send_chunk($response, $data, $name_indicator) {
128
129 if (!preg_match('/^([a-f0-9]+)\.migrator.updraftplus.com$/', $name_indicator, $matches)) return $response;
130 $name_hash = $matches[1];
131
132 $this->initialise_listener_error_handling($name_hash);
133
134 global $updraftplus;
135
136 // send_message('send_chunk', array('file' => $file, 'data' => $chunk, 'start' => $upload_start))
137
138 if (!is_array($data)) return $this->return_rpc_message(array('response' => 'error', 'data' => 'invalid_input_expected_array'));
139
140 if (!isset($data['file'])) return $this->return_rpc_message(array('response' => 'error', 'data' => 'invalid_input_no_file'));
141
142 if (!isset($data['data'])) return $this->return_rpc_message(array('response' => 'error', 'data' => 'invalid_input_no_data'));
143
144 if (!isset($data['start'])) return $this->return_rpc_message(array('response' => 'error', 'data' => 'invalid_input_no_start'));
145
146 // Make sure the parameters are valid
147 if (!is_numeric($data['start']) || absint($data['start']) != $data['start']) return $this->return_rpc_message(array('response' => 'error', 'data' => 'invalid_start'));
148
149 // Sanity-check the file name
150 $file = $data['file'];
151 if (!preg_match('/(-db\.gz|-db\.gz\.crypt|-db|\.(sql|sql\.gz|sql\.bz2|zip|tar|tar\.bz2|tar\.gz|txt))/i', $file)) return array('response' => 'error', 'data' => 'illegal_file_name1');
152 if (basename($file) != $file) return $this->return_rpc_message(array('response' => 'error', 'data' => 'invalid_input_illegal_character'));
153
154 $start = $data['start'];
155
156 $is_last_chunk = empty($data['last_chunk']) ? 0 : 1;
157 if (!$is_last_chunk) {
158 } else {
159 $orig_file = $file;
160 if (!empty($data['label'])) $label = $data['label'];
161 }
162 $file .= '.tmp';
163
164 // Intentionally over-write the variable, in case memory is short and in case PHP's garbage collector is this clever
165 $data = base64_decode($data['data']);
166
167 $updraft_dir = $updraftplus->backups_dir_location();
168 $fullpath = $updraft_dir.'/'.$file;
169
170 $existing_size = file_exists($fullpath) ? filesize($fullpath) : 0;
171
172 if ($start > $existing_size) {
173 return $this->return_rpc_message(array('response' => 'error', 'data' => "invalid_start_too_big:start=${start},existing_size=${existing_size}"));
174 }
175
176 if (false == ($fhandle = fopen($fullpath, 'ab'))) {
177 return $this->return_rpc_message(array('response' => 'error', 'data' => 'file_open_failure'));
178 }
179
180 // fseek() returns 0 for success, or -1 for failure
181 if ($start != $existing_size && -1 == fseek($fhandle, $start)) return $this->return_rpc_message(array('response' => 'error', 'data' => 'fseek_failure'));
182
183 $write_status = fwrite($fhandle, $data);
184
185 if (false === $write_status || (false == $write_status && !empty($data))) return $this->return_rpc_message(array('response' => 'error', 'data' => 'fwrite_failure'));
186
187 @fclose($fhandle);// phpcs:ignore Generic.PHP.NoSilencedErrors.Discouraged
188
189 $our_keys = UpdraftPlus_Options::get_updraft_option('updraft_migrator_localkeys');
190 if (is_array($our_keys) && isset($our_keys[$name_hash]) && !empty($our_keys[$name_hash]['name'])) $updraftplus->log("Received data chunk on key ".$our_keys[$name_hash]['name']. " ($file, ".$start.", is_last=$is_last_chunk)");
191
192 if ($is_last_chunk) {
193 if (!rename($fullpath, $updraft_dir.'/'.$orig_file)) return $this->return_rpc_message(array('response' => 'error', 'data' => 'rename_failure'));
194 $only_add_this_file = array('file' => $orig_file);
195 if (isset($label)) $only_add_this_file['label'] = $label;
196 UpdraftPlus_Backup_History::rebuild(false, $only_add_this_file);
197 }
198
199 return $this->return_rpc_message(array(
200 'response' => 'file_status',
201 'data' => $this->get_file_status($file)
202 ));
203 }
204
205 protected function get_file_status($file) {
206
207 global $updraftplus;
208 $fullpath = $updraftplus->backups_dir_location().'/'.basename($file);
209
210 if (file_exists($fullpath)) {
211 $size = filesize($fullpath);
212 $status = 1;
213 } elseif (file_exists($fullpath.'.tmp')) {
214 $size = filesize($fullpath.'.tmp');
215 $status = 0;
216 } else {
217 $size = 0;
218 $status = 0;
219 }
220
221 return array(
222 'size' => $size,
223 'status' => $status,
224 );
225 }
226
227 public function udrpc_command_get_file_status($response, $data, $name_indicator) {
228 if (!preg_match('/^([a-f0-9]+)\.migrator.updraftplus.com$/', $name_indicator, $matches)) return $response;
229 $name_hash = $matches[1];
230
231 $this->initialise_listener_error_handling($name_hash);
232
233 if (!is_string($data)) return $this->return_rpc_message(array('response' => 'error', 'data' => 'invalid_input_expected_string'));
234
235 if (basename($data) != $data) return $this->return_rpc_message(array('response' => 'error', 'data' => 'invalid_input_illegal_character'));
236
237 return $this->return_rpc_message(array(
238 'response' => 'file_status',
239 'data' => $this->get_file_status($data)
240 ));
241 }
242
243 /**
244 * This function will return a response to the remote site to acknowledge that we have recieved the upload_complete message and if this is a clone it call the ready_for_restore action
245 *
246 * @param string $response - a string response
247 * @param array $data - an array of data
248 * @param string $name_indicator - a string to identify the request
249 *
250 * @return array - the array response
251 */
252 public function udrpc_command_upload_complete($response, $data, $name_indicator) {// phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter.Found
253 if (!preg_match('/^([a-f0-9]+)\.migrator.updraftplus.com$/', $name_indicator, $matches)) return $response;
254
255 if (defined('UPDRAFTPLUS_THIS_IS_CLONE') && UPDRAFTPLUS_THIS_IS_CLONE) {
256 $job_id = (is_array($data) && !empty($data['job_id'])) ? $data['job_id'] : null;
257 do_action('updraftplus_temporary_clone_ready_for_restore', $job_id);
258 }
259
260 return $this->return_rpc_message(array(
261 'response' => 'file_status',
262 'data' => ''
263 ));
264 }
265
266 public function updraftplus_initial_jobdata($initial_jobdata, $options, $split_every) {
267
268 if (is_array($options) && !empty($options['extradata']) && !empty($options['extradata']['services']) && preg_match('#remotesend/(\d+)#', $options['extradata']['services'], $matches)) {
269
270 // Load the option now - don't wait until send time
271 $site_id = $matches[1];
272 $remotesites = UpdraftPlus_Options::get_updraft_option('updraft_remotesites');
273 if (!is_array($remotesites)) $remotesites = array();
274
275 if (empty($remotesites[$site_id]) || empty($remotesites[$site_id]['url']) || empty($remotesites[$site_id]['key']) || empty($remotesites[$site_id]['name_indicator'])) {
276 throw new Exception("Remote site id ($site_id) not found - send aborted");
277 }
278
279 array_push($initial_jobdata, 'remotesend_info', $remotesites[$site_id]);
280
281 // Reduce to 100MB if it was above. Since the user isn't expected to directly manipulate these zip files, the potentially higher number of zip files doesn't matter.
282 $split_every_key = array_search('split_every', $initial_jobdata) + 1;
283 if ($split_every > 100) $initial_jobdata[$split_every_key] = 100;
284
285 }
286
287 return $initial_jobdata;
288 }
289
290 public function updraft_printjob_beforewarnings($ret, $jobdata) {
291 if (!empty($jobdata['remotesend_info']) && !empty($jobdata['remotesend_info']['url'])) {
292 $ret .= '<p style="padding:0px; margin:2px 0;">'.__('Backup data will be sent to:', 'updraftplus').' '.htmlspecialchars($jobdata['remotesend_info']['url']).'</p>';
293 }
294 return $ret;
295 }
296
297 public function updraft_remote_ping_test($data) {
298
299 if (!isset($data['id']) || !is_numeric($data['id']) || empty($data['url'])) die;
300
301 $remote_indicator = $data['id'];
302
303 $ping_result = $this->do_ping_test($remote_indicator, $data['url']);
304
305 die(json_encode($ping_result));
306
307 }
308
309 /**
310 * Do an RPC ping test
311 *
312 * @param String $remote_indicator
313 * @param String $url
314 *
315 * @return Array - results
316 */
317 public function do_ping_test($remote_indicator, $url) {
318
319 global $updraftplus;
320
321 $remotesites = UpdraftPlus_Options::get_updraft_option('updraft_remotesites');
322 if (!is_array($remotesites)) $remotesites = array();
323
324 if (empty($remotesites[$remote_indicator]) || $url != $remotesites[$remote_indicator]['url'] || empty($remotesites[$remote_indicator]['key']) || empty($remotesites[$remote_indicator]['name_indicator'])) {
325 return array('e' => 1, 'r' => __('Error:', 'updraftplus').' '.__('site not found', 'updraftplus'));
326 }
327
328 try {
329
330 $updraftplus->error_reporting_stop_when_logged = true;
331 set_error_handler(array($updraftplus, 'php_error'), E_ALL & ~E_STRICT);
332 $this->php_events = array();
333 add_filter('updraftplus_logline', array($this, 'updraftplus_logline'), 10, 4);
334
335 $opts = $remotesites[$remote_indicator];
336 $ud_rpc = $updraftplus->get_udrpc($opts['name_indicator']);
337 $send_data = null;
338
339 if (!empty($opts['format_support']) && 2 == $opts['format_support']) {
340 if (empty($opts['remote_got_public'])) {
341 // Can't upgrade to format 2 until we know the other end has our public key
342 $use_format = 1;
343 $send_data = array('sender_public' => $opts['local_public']);
344 } else {
345 $use_format = 2;
346 }
347 } else {
348 $use_format = 1;
349 }
350
351 $ud_rpc->set_message_format($use_format);
352
353 if (2 == $use_format) {
354 $ud_rpc->set_key_remote($opts['key']);
355 $ud_rpc->set_key_local($opts['local_private']);
356 } else {
357 $ud_rpc->set_key_local($opts['key']);
358 }
359
360 $ud_rpc->set_destination_url($url);
361 $ud_rpc->activate_replay_protection();
362
363 do_action('updraftplus_remotesend_udrpc_object_obtained', $ud_rpc, $opts);
364
365 $response = $ud_rpc->send_message('ping', $send_data);
366
367 restore_error_handler();
368
369 if (is_wp_error($response)) {
370
371 $err_msg = __('Error:', 'updraftplus').' '.$response->get_error_message();
372 $err_data = $response->get_error_data();
373 $err_code = $response->get_error_code();
374
375 } elseif (!is_array($response) || empty($response['response']) || 'pong' != $response['response']) {
376
377 $err_msg = __('Error:', 'updraftplus').' '.sprintf(__('You should check that the remote site is online, not firewalled, does not have security modules that may be blocking access, has UpdraftPlus version %s or later active and that the keys have been entered correctly.', 'updraftplus'), '2.10.3');
378 $err_data = $response;
379 $err_code = 'no_pong';
380
381 } elseif (!empty($response['data']['got_public'])) {
382 $remotesites[$remote_indicator]['remote_got_public'] = 1;
383 UpdraftPlus_Options::update_updraft_option('updraft_remotesites', $remotesites);
384 }
385
386 if (isset($err_msg)) {
387
388 $res = array('e' => 1, 'r' => $err_msg);
389
390 if ($this->url_looks_internal($url)) {
391 $res['moreinfo'] = '<p>'.sprintf(__('The site URL you are sending to (%s) looks like a local development website. If you are sending from an external network, it is likely that a firewall will be blocking this.', 'updraftplus'), htmlspecialchars($url)).'</p>';
392 }
393
394 // We got several support requests from people who didn't seem to be aware of other methods
395 $msg_try_other_method = '<p>'.__('If sending directly from site to site does not work for you, then there are three other methods - please try one of these instead.', 'updraftplus').' <a href="https://updraftplus.com/faqs/how-do-i-migrate-to-a-new-site-location/#importing" target="_blank">'.__('For longer help, including screenshots, follow this link.', 'updraftplus').'</a></p>';
396
397 $res['moreinfo'] = isset($res['moreinfo']) ? $res['moreinfo'].$msg_try_other_method : $msg_try_other_method;
398
399 if (isset($err_data)) $res['data'] = $err_data;
400 if (isset($err_code)) $res['code'] = $err_code;
401
402 if (!empty($this->php_events)) $res['php_events'] = $this->php_events;
403
404 return $res;
405 }
406
407 $ret = '<p>'.__('Testing connection...', 'updraftplus').' '.__('OK', 'updraftplus').'</p>';
408
409 global $updraftplus_admin;
410
411 $ret .= '<input type="checkbox" checked="checked" id="remotesend_backupnow_db"> <label for="remotesend_backupnow_db">'.__("Database", 'updraftplus').'</label><br>';
412 $ret .= $updraftplus_admin->files_selector_widgetry('remotesend_', false, false);
413
414 $service = $updraftplus->just_one(UpdraftPlus_Options::get_updraft_option('updraft_service'));
415 if (is_string($service)) $service = array($service);
416
417 if (is_array($service) && !empty($service) && array('none') !== $service) {
418 $first_one = true;
419 foreach ($service as $s) {
420 if (!$s) continue;
421 if (isset($updraftplus->backup_methods[$s])) {
422 if ($first_one) {
423 $first_one = false;
424 $ret .= '<p>';
425 $ret .= '<input type="checkbox" id="remotesend_backupnow_cloud"> <label for="remotesend_backupnow_cloud">'.__("Also send this backup to the active remote storage locations", 'updraftplus');
426 $ret .= ' (';
427 } else {
428 $ret .= ', ';
429 }
430 $ret .= $updraftplus->backup_methods[$s];
431 }
432 }
433 if (!$first_one) $ret .= ')';
434 $ret .= '</label></p>';
435 }
436
437 $ret .= apply_filters('updraft_backupnow_modal_afteroptions', '', 'remotesend_');
438 $ret .= '<button class="button-primary" style="height:30px; font-size:16px; margin-left: 3px; width:85px;" id="updraft_migrate_send_button" onclick="updraft_migrate_go_backup();">'.__('Send', 'updraftplus').'</button>';
439
440 return array('success' => 1, 'r' => $ret);
441 } catch (Exception $e) {
442 return array('e' => 1, 'r' => __('Error:', 'updraftplus').' '.$e->getMessage().' (line: '.$e->getLine().', file: '.$e->getFile().')');
443 }
444 }
445
446 /**
447 * This is used only for an advisory warning - does not have to be able to always detect
448 *
449 * @param string $url
450 */
451 protected function url_looks_internal($url) {
452 $url_host = strtolower(parse_url($url, PHP_URL_HOST));
453 if ('localhost' == $url_host || strpos($url_host, '127.') === 0 || strpos($url_host, '10.') === 0 || '::1' == $url_host || strpos($url_host, 'localhost') !== false || substr($url_host, -4, 4) == '.dev') return true;
454 return false;
455 }
456
457 public function updraft_migrate_key_delete($data) {
458 if (empty($data['keyid'])) die;
459 $our_keys = UpdraftPlus_Options::get_updraft_option('updraft_migrator_localkeys');
460 if (!is_array($our_keys)) $our_keys = array();
461 unset($our_keys[$data['keyid']]);
462 UpdraftPlus_Options::update_updraft_option('updraft_migrator_localkeys', $our_keys);
463 echo json_encode(array('ourkeys' => $this->list_our_keys($our_keys)));
464 die;
465 }
466
467 /**
468 * This function is a wrapper for updraft_migrate_key_create when being called from WP_CLI it allows us to return the created key rather than echo it, by passing return_instead_of_echo as part of $data.
469 *
470 * @param string $string - empty string to filter on
471 * @param array $data - an array of data needed to create the RSA keypair should also include return_instead_of_echo to return the result
472 *
473 * @return string - the RSA remote key
474 */
475 public function updraft_migrate_key_create_return($string, $data) {// phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter.Found
476 return $this->updraft_migrate_key_create($data);
477 }
478
479 /**
480 * Called upon the WP action updraft_s3_newuser. Dies.
481 *
482 * @param array $data - the posted data
483 *
484 * @return void
485 */
486 public function updraft_migrate_key_create($data) {
487
488 if (empty($data['name'])) die;
489 $name = stripslashes($data['name']);
490
491 $size = (empty($data['size']) || !is_numeric($data['size']) || $data['size'] < 512) ? 2048 : (int) $data['size'];
492
493 $name_hash = md5($name); // 32 characters
494 $indicator_name = $name_hash.'.migrator.updraftplus.com';
495
496 $our_keys = UpdraftPlus_Options::get_updraft_option('updraft_migrator_localkeys');
497 if (!is_array($our_keys)) $our_keys = array();
498
499 if (isset($our_keys[$name_hash])) {
500 echo json_encode(array('e' => 1, 'r' => __('Error:', 'updraftplus').' '.__('A key with this name already exists; you must use a unique name.', 'updraftplus')));
501 die;
502 }
503
504 global $updraftplus;
505 $ud_rpc = $updraftplus->get_udrpc($indicator_name);
506
507 if (is_object($ud_rpc) && $ud_rpc->generate_new_keypair($size)) {
508 $local_bundle = $ud_rpc->get_portable_bundle('base64_with_count');
509
510 $our_keys[$name_hash] = array('name' => $name, 'key' => $ud_rpc->get_key_local());
511 UpdraftPlus_Options::update_updraft_option('updraft_migrator_localkeys', $our_keys);
512
513 if (isset($data['return_instead_of_echo']) && $data['return_instead_of_echo']) return $local_bundle;
514
515 echo json_encode(array(
516 'bundle' => $local_bundle,
517 'r' => __('Key created successfully.', 'updraftplus').' '.__('You must copy and paste this key on the sending site now - it cannot be shown again.', 'updraftplus'),
518 'selector' => $this->get_remotesites_selector(array()),
519 'ourkeys' => $this->list_our_keys($our_keys),
520 ));
521 die;
522 }
523
524 if (extension_loaded('mbstring')) {
525 // phpcs:ignore PHPCompatibility.IniDirectives.RemovedIniDirectives.mbstring_func_overloadDeprecated -- Commented out as this flags as not compatible with PHP 5.2
526 if (ini_get('mbstring.func_overload') & 2) {
527 echo json_encode(array('e' => 1, 'r' => __('Error:', 'updraftplus').' '.sprintf(__('The setting %s is turned on in your PHP settings. It is deprecated, causes encryption to malfunction, and should be turned off.', 'updraftplus'), 'mbstring.func_overload')));
528 die;
529 }
530 }
531
532 echo json_encode(array('e' => 1));
533 die;
534 }
535
536 public function updraft_migrate_newdestination($data) {
537
538 global $updraftplus;
539 $ret = array();
540
541 if (empty($data['key'])) {
542 $ret['e'] = sprintf(__("Failure: No %s was given.", 'updraftplus'), __('key', 'updraftplus'));
543 } else {
544
545 // The indicator isn't really needed - we won't be receiving on it
546 $our_indicator = md5(network_site_url()).'.migrator.updraftplus.com';
547 $ud_rpc = $updraftplus->get_udrpc($our_indicator);
548
549 $ud_rpc->set_can_generate(true);
550
551 // A bundle has these keys: key, name_indicator, url
552 $decode_bundle = $ud_rpc->decode_portable_bundle($data['key'], 'base64_with_count');
553
554 if (!is_array($decode_bundle) || !empty($decode_bundle['code'])) {
555 $ret['e'] = __('Error:', 'updraftplus');
556 if (!empty($decode_bundle['code']) && 'invalid_wrong_length' == $decode_bundle['code']) {
557 $ret['e'] .= ' '.__('The entered key was the wrong length - please try again.', 'updraftplus');
558 } elseif (!empty($decode_bundle['code']) && 'invalid_corrupt' == $decode_bundle['code']) {
559 $ret['e'] .= ' '.__('The entered key was corrupt - please try again.', 'updraftplus').' ('.$decode_bundle['data'].')';
560 } elseif (empty($decode_bundle['key']) || empty($decode_bundle['url'])) {
561 $ret['e'] .= ' '.__('The entered key was corrupt - please try again.', 'updraftplus');
562 $ret['data'] = $decode_bundle;
563 }
564 } elseif (empty($decode_bundle['key']) || empty($decode_bundle['url'])) {
565 $ret['e'] = __('Error:', 'updraftplus').' '.__('The entered key was corrupt - please try again.', 'updraftplus');
566 $ret['data'] = $decode_bundle;
567 } else {
568
569 if (trailingslashit(network_site_url()) == $decode_bundle['url']) {
570 $ret['e'] = __('Error:', 'updraftplus').' '.__('The entered key does not belong to a remote site (it belongs to this one).', 'updraftplus');
571 } else {
572
573 // Store the information
574 $remotesites = UpdraftPlus_Options::get_updraft_option('updraft_remotesites');
575 if (!is_array($remotesites)) $remotesites = array();
576 foreach ($remotesites as $k => $rsite) {
577 if (!is_array($rsite)) continue;
578 if ($rsite['url'] == $decode_bundle['url']) unset($remotesites[$k]);
579 }
580
581 if (false == $ud_rpc->generate_new_keypair()) {
582 $ret['e'] = __('Error:', 'updraftplus').' An error occurred when attempting to generate a new key-pair';
583 } else {
584
585 $decode_bundle['local_private'] = $ud_rpc->get_key_local();
586 $decode_bundle['local_public'] = $ud_rpc->get_key_remote();
587
588 $remotesites[] = $decode_bundle;
589 UpdraftPlus_Options::update_updraft_option('updraft_remotesites', $remotesites);
590
591 $ret['selector'] = $this->get_remotesites_selector($remotesites);
592
593 // Return the new HTML widget to the front end
594 $ret['r'] = __('The key was successfully added.', 'updraftplus').' '.__('It is for sending backups to the following site: ', 'updraftplus').htmlspecialchars($decode_bundle['url']);
595 }
596
597 }
598 }
599
600 }
601
602 echo json_encode($ret);
603 die;
604 }
605
606 protected function get_remotesites_selector($remotesites = false) {
607
608 if (false === $remotesites) {
609 $remotesites = UpdraftPlus_Options::get_updraft_option('updraft_remotesites');
610 if (!is_array($remotesites)) $remotesites = array();
611 }
612
613 if (empty($remotesites)) {
614 return '<p id="updraft_migrate_receivingsites_nonemsg"><em>'.__('No receiving sites have yet been added.', 'updraftplus').'</em></p>';
615 } else {
616 $ret = '<p class="updraftplus-remote-sites-selector"><label>'.__('Send to site:', 'updraftplus').'</label> <select id="updraft_remotesites_selector">';
617 foreach ($remotesites as $k => $rsite) {
618 if (!is_array($rsite) || empty($rsite['url'])) continue;
619 $ret .= '<option value="'.esc_attr($k).'">'.htmlspecialchars($rsite['url']).'</option>';
620 }
621 $ret .= '</select>';
622 $ret .= ' <button class="button-primary" style="height:30px; font-size:16px; margin-left: 3px; width:85px;" id="updraft_migrate_send_button" onclick="updraft_migrate_send_backup();">'.__('Send', 'updraftplus').'</button>';
623 $ret .= '</p>';
624 }
625
626 return $ret;
627 }
628
629 protected function list_our_keys($our_keys = false) {
630 if (false === $our_keys) {
631 $our_keys = UpdraftPlus_Options::get_updraft_option('updraft_migrator_localkeys');
632 }
633
634 if (empty($our_keys)) return '<em>'.__('No keys to allow remote sites to send backup data here have yet been created.', 'updraftplus').'</em>';
635
636 $ret = '';
637 $first_one = true;
638
639 foreach ($our_keys as $k => $key) {
640 if (!is_array($key)) continue;
641 if ($first_one) {
642 $first_one = false;
643 $ret .= '<p><strong>'.__('Existing keys', 'updraftplus').'</strong><br>';
644 }
645 $ret .= htmlspecialchars($key['name']);
646 $ret .= ' - <a href="'.UpdraftPlus::get_current_clean_url().'" onclick="updraft_migrate_local_key_delete(\''.esc_attr($k).'\'); return false;" class="updraft_migrate_local_key_delete" data-keyid="'.esc_attr($k).'">'.__('Delete', 'updraftplus').'</a>';
647 $ret .= '<br>';
648 }
649
650 if ($ret) $ret .= '</p>';
651
652 return $ret;
653
654 }
655 }
656