Edit File by line
/home/barbar84/www/wp-conte.../plugins/updraftp.../methods
File: remotesend.php
<?php
[0] Fix | Delete
[1] Fix | Delete
if (!defined('UPDRAFTPLUS_DIR')) die('No direct access allowed');
[2] Fix | Delete
[3] Fix | Delete
if (!class_exists('UpdraftPlus_RemoteStorage_Addons_Base_v2')) require_once(UPDRAFTPLUS_DIR.'/methods/addon-base-v2.php');
[4] Fix | Delete
[5] Fix | Delete
class UpdraftPlus_BackupModule_remotesend extends UpdraftPlus_RemoteStorage_Addons_Base_v2 {
[6] Fix | Delete
[7] Fix | Delete
private $default_chunk_size;
[8] Fix | Delete
[9] Fix | Delete
private $remotesend_use_chunk_size;
[10] Fix | Delete
[11] Fix | Delete
private $remotesend_chunked_wp_error;
[12] Fix | Delete
[13] Fix | Delete
private $try_format_upgrade = false;
[14] Fix | Delete
[15] Fix | Delete
/**
[16] Fix | Delete
* Class constructor
[17] Fix | Delete
*/
[18] Fix | Delete
public function __construct() {
[19] Fix | Delete
// 3rd parameter: chunking? 4th: Test button?
[20] Fix | Delete
parent::__construct('remotesend', 'Remote send', false, false);
[21] Fix | Delete
}
[22] Fix | Delete
[23] Fix | Delete
/**
[24] Fix | Delete
* Supplies the list of keys for options to be saved in the backup job.
[25] Fix | Delete
*
[26] Fix | Delete
* @return Array
[27] Fix | Delete
*/
[28] Fix | Delete
public function get_credentials() {
[29] Fix | Delete
return array('updraft_ssl_disableverify', 'updraft_ssl_nossl', 'updraft_ssl_useservercerts');
[30] Fix | Delete
}
[31] Fix | Delete
[32] Fix | Delete
/**
[33] Fix | Delete
* Upload a single file
[34] Fix | Delete
*
[35] Fix | Delete
* @param String $file - the basename of the file to upload
[36] Fix | Delete
* @param String $from - the full path of the file
[37] Fix | Delete
*
[38] Fix | Delete
* @return Boolean - success status. Failures can also be thrown as exceptions.
[39] Fix | Delete
*/
[40] Fix | Delete
public function do_upload($file, $from) {
[41] Fix | Delete
[42] Fix | Delete
global $updraftplus;
[43] Fix | Delete
$opts = $this->options;
[44] Fix | Delete
[45] Fix | Delete
try {
[46] Fix | Delete
$storage = $this->bootstrap();
[47] Fix | Delete
if (is_wp_error($storage)) throw new Exception($storage->get_error_message());
[48] Fix | Delete
if (!is_object($storage)) throw new Exception("RPC service error");
[49] Fix | Delete
} catch (Exception $e) {
[50] Fix | Delete
$message = $e->getMessage().' ('.get_class($e).') (line: '.$e->getLine().', file: '.$e->getFile().')';
[51] Fix | Delete
$this->log("RPC service error: ".$message);
[52] Fix | Delete
$this->log($message, 'error');
[53] Fix | Delete
return false;
[54] Fix | Delete
}
[55] Fix | Delete
[56] Fix | Delete
$filesize = filesize($from);
[57] Fix | Delete
$this->remotesend_file_size = $filesize;
[58] Fix | Delete
[59] Fix | Delete
// See what the sending side currently has. This also serves as a ping. For that reason, we don't try/catch - we let them be caught at the next level up.
[60] Fix | Delete
[61] Fix | Delete
$get_remote_size = $this->send_message('get_file_status', $file, 30);
[62] Fix | Delete
[63] Fix | Delete
if (is_wp_error($get_remote_size)) {
[64] Fix | Delete
throw new Exception($get_remote_size->get_error_message().' (get_file_status: '.$get_remote_size->get_error_code().')');
[65] Fix | Delete
}
[66] Fix | Delete
[67] Fix | Delete
if (!is_array($get_remote_size) || empty($get_remote_size['response'])) throw new Exception(__('Unexpected response:', 'updraftplus').' '.serialize($get_remote_size));
[68] Fix | Delete
[69] Fix | Delete
if ('error' == $get_remote_size['response']) {
[70] Fix | Delete
$msg = $get_remote_size['data'];
[71] Fix | Delete
// Could interpret the codes to get more interesting messages directly to the user
[72] Fix | Delete
throw new Exception(__('Error:', 'updraftplus').' '.$msg);
[73] Fix | Delete
}
[74] Fix | Delete
[75] Fix | Delete
if (empty($get_remote_size['data']) || !isset($get_remote_size['data']['size']) || 'file_status' != $get_remote_size['response']) throw new Exception(__('Unexpected response:', 'updraftplus').' '.serialize($get_remote_size));
[76] Fix | Delete
[77] Fix | Delete
// Possible statuses: 0=temporary file (or not present), 1=file
[78] Fix | Delete
if (empty($get_remote_size['data'])) {
[79] Fix | Delete
$remote_size = 0;
[80] Fix | Delete
$remote_status = 0;
[81] Fix | Delete
} else {
[82] Fix | Delete
$remote_size = (int) $get_remote_size['data']['size'];
[83] Fix | Delete
$remote_status = $get_remote_size['data']['status'];
[84] Fix | Delete
}
[85] Fix | Delete
[86] Fix | Delete
$this->log("$file: existing size: ".$remote_size);
[87] Fix | Delete
[88] Fix | Delete
// Perhaps it already exists? (if we didn't get the final confirmation)
[89] Fix | Delete
if ($remote_size >= $filesize && $remote_status) {
[90] Fix | Delete
$this->log("$file: already uploaded");
[91] Fix | Delete
return true;
[92] Fix | Delete
}
[93] Fix | Delete
[94] Fix | Delete
// Length = 44 (max = 45)
[95] Fix | Delete
$this->remote_sent_defchunk_transient = 'ud_rsenddck_'.md5($opts['name_indicator']);
[96] Fix | Delete
[97] Fix | Delete
if (empty($this->default_chunk_size)) {
[98] Fix | Delete
[99] Fix | Delete
// Default is 2MB. After being b64-encoded twice, this is ~ 3.7MB = 113 seconds on 32KB/s uplink
[100] Fix | Delete
$default_chunk_size = $updraftplus->jobdata_get('clone_job') ? 4194304 : 2097152;
[101] Fix | Delete
[102] Fix | Delete
if (defined('UPDRAFTPLUS_REMOTESEND_DEFAULT_CHUNK_BYTES') && UPDRAFTPLUS_REMOTESEND_DEFAULT_CHUNK_BYTES >= 16384) $default_chunk_size = UPDRAFTPLUS_REMOTESEND_DEFAULT_CHUNK_BYTES;
[103] Fix | Delete
[104] Fix | Delete
$this->default_chunk_size = $default_chunk_size;
[105] Fix | Delete
[106] Fix | Delete
}
[107] Fix | Delete
[108] Fix | Delete
$default_chunk_size = $this->default_chunk_size;
[109] Fix | Delete
[110] Fix | Delete
if (false !== ($saved_default_chunk_size = get_transient($this->remote_sent_defchunk_transient)) && $saved_default_chunk_size > 16384) {
[111] Fix | Delete
// Don't go lower than 256KB for the *default*. (The job size can go lower).
[112] Fix | Delete
$default_chunk_size = max($saved_default_chunk_size, 262144);
[113] Fix | Delete
}
[114] Fix | Delete
[115] Fix | Delete
$this->remotesend_use_chunk_size = $updraftplus->jobdata_get('remotesend_chunksize', $default_chunk_size);
[116] Fix | Delete
[117] Fix | Delete
if (0 == $remote_size && $this->remotesend_use_chunk_size == $this->default_chunk_size && $updraftplus->current_resumption - max($updraftplus->jobdata_get('uploaded_lastreset'), 1) > 1) {
[118] Fix | Delete
$new_chunk_size = floor($this->remotesend_use_chunk_size / 2);
[119] Fix | Delete
$this->log("No uploading activity has been detected for a while; reducing chunk size in case a timeout was occurring. New chunk size: ".$new_chunk_size);
[120] Fix | Delete
$this->remotesend_set_new_chunk_size($new_chunk_size);
[121] Fix | Delete
}
[122] Fix | Delete
[123] Fix | Delete
try {
[124] Fix | Delete
if (false != ($handle = fopen($from, 'rb'))) {
[125] Fix | Delete
[126] Fix | Delete
$this->remotesend_uploaded_size = $remote_size;
[127] Fix | Delete
$ret = $updraftplus->chunked_upload($this, $file, $this->method."://".trailingslashit($opts['url']).$file, $this->description, $this->remotesend_use_chunk_size, $remote_size, true);
[128] Fix | Delete
[129] Fix | Delete
fclose($handle);
[130] Fix | Delete
[131] Fix | Delete
return $ret;
[132] Fix | Delete
} else {
[133] Fix | Delete
throw new Exception("Failed to open file for reading: $from");
[134] Fix | Delete
}
[135] Fix | Delete
} catch (Exception $e) {
[136] Fix | Delete
$this->log("upload: error (".get_class($e)."): ($file) (".$e->getMessage().") (line: ".$e->getLine().', file: '.$e->getFile().')');
[137] Fix | Delete
if (!empty($this->remotesend_chunked_wp_error) && is_wp_error($this->remotesend_chunked_wp_error)) {
[138] Fix | Delete
$this->log("Exception data: ".base64_encode(serialize($this->remotesend_chunked_wp_error->get_error_data())));
[139] Fix | Delete
}
[140] Fix | Delete
return false;
[141] Fix | Delete
}
[142] Fix | Delete
[143] Fix | Delete
return true;
[144] Fix | Delete
}
[145] Fix | Delete
[146] Fix | Delete
/**
[147] Fix | Delete
* Chunked upload
[148] Fix | Delete
*
[149] Fix | Delete
* @param String $file Specific file to be used in chunked upload
[150] Fix | Delete
* @param Resource $fp File handle
[151] Fix | Delete
* @param Integer $chunk_index The index of the chunked data
[152] Fix | Delete
* @param Integer $upload_size Size of the upload
[153] Fix | Delete
* @param Integer $upload_start String the upload starts on
[154] Fix | Delete
* @param Integer $upload_end String the upload ends on
[155] Fix | Delete
*
[156] Fix | Delete
* @return Boolean|Integer Result (N.B> (int)1 means the same as true, but additionally indicates "don't log it")
[157] Fix | Delete
*/
[158] Fix | Delete
public function chunked_upload($file, $fp, $chunk_index, $upload_size, $upload_start, $upload_end) {
[159] Fix | Delete
[160] Fix | Delete
// Condition used to be "$upload_start < $this->remotesend_uploaded_size" - but this assumed that the other side never failed after writing only some bytes to disk
[161] Fix | Delete
// $upload_end is the byte offset of the final byte. Therefore, add 1 onto it when comparing with a size.
[162] Fix | Delete
if ($upload_end + 1 <= $this->remotesend_uploaded_size) return 1;
[163] Fix | Delete
[164] Fix | Delete
global $updraftplus;
[165] Fix | Delete
[166] Fix | Delete
$chunk = fread($fp, $upload_size);
[167] Fix | Delete
[168] Fix | Delete
if (false === $chunk) {
[169] Fix | Delete
$this->log("upload: $file: fread failure ($upload_start)");
[170] Fix | Delete
return false;
[171] Fix | Delete
}
[172] Fix | Delete
[173] Fix | Delete
$try_again = false;
[174] Fix | Delete
[175] Fix | Delete
$data = array('file' => $file, 'data' => base64_encode($chunk), 'start' => $upload_start);
[176] Fix | Delete
[177] Fix | Delete
if ($upload_end+1 >= $this->remotesend_file_size) {
[178] Fix | Delete
$data['last_chunk'] = true;
[179] Fix | Delete
if ('' != ($label = $updraftplus->jobdata_get('label'))) $data['label'] = $label;
[180] Fix | Delete
}
[181] Fix | Delete
[182] Fix | Delete
// ~ 3.7MB of data typically - timeout allows for 15.9KB/s
[183] Fix | Delete
try {
[184] Fix | Delete
$put_chunk = $this->send_message('send_chunk', $data, 240);
[185] Fix | Delete
} catch (Exception $e) {
[186] Fix | Delete
$try_again = true;
[187] Fix | Delete
}
[188] Fix | Delete
[189] Fix | Delete
if ($try_again || is_wp_error($put_chunk)) {
[190] Fix | Delete
// 413 = Request entity too large
[191] Fix | Delete
// Don't go lower than 64KB chunks (i.e. 128KB/2)
[192] Fix | Delete
// Note that mod_security can be configured to 'helpfully' decides to replace HTTP error codes + messages with a simple serving up of the site home page, which means that we need to also guess about other reasons this condition may have occurred other than detecting via the direct 413 code. Of course, our search for wp-includes|wp-content|WordPress|/themes/ would be thwarted by someone who tries to hide their WP. The /themes/ is pretty hard to hide, as the theme directory is always <wp-content-dir>/themes - even if you moved your wp-content. The point though is just a 'best effort' - this doesn't have to be infallible.
[193] Fix | Delete
if (is_wp_error($put_chunk)) {
[194] Fix | Delete
[195] Fix | Delete
$error_data = $put_chunk->get_error_data();
[196] Fix | Delete
[197] Fix | Delete
$is_413 = ('unexpected_http_code' == $put_chunk->get_error_code() && (
[198] Fix | Delete
413 == $error_data
[199] Fix | Delete
|| (is_array($error_data) && !empty($error_data['response']['code']) && 413 == $error_data['response']['code'])
[200] Fix | Delete
)
[201] Fix | Delete
);
[202] Fix | Delete
[203] Fix | Delete
$is_timeout = ('http_request_failed' == $put_chunk->get_error_code() && false !== strpos($put_chunk->get_error_message(), 'timed out'));
[204] Fix | Delete
[205] Fix | Delete
if ($this->remotesend_use_chunk_size >= 131072 && ($is_413 || $is_timeout || ('response_not_understood' == $put_chunk->get_error_code() && (false !== strpos($error_data, 'wp-includes') || false !== strpos($error_data, 'wp-content') || false !== strpos($error_data, 'WordPress') || false !== strpos($put_chunk->get_error_data(), '/themes/'))))) {
[206] Fix | Delete
if (1 == $chunk_index) {
[207] Fix | Delete
$new_chunk_size = floor($this->remotesend_use_chunk_size / 2);
[208] Fix | Delete
$this->remotesend_set_new_chunk_size($new_chunk_size);
[209] Fix | Delete
$log_msg = "Returned WP_Error: code=".$put_chunk->get_error_code();
[210] Fix | Delete
if ('unexpected_http_code' == $put_chunk->get_error_code()) $log_msg .= ' ('.$error_data.')';
[211] Fix | Delete
$log_msg .= " - reducing chunk size to: ".$new_chunk_size;
[212] Fix | Delete
$this->log($log_msg);
[213] Fix | Delete
return new WP_Error('reduce_chunk_size', 'HTTP 413 or possibly equivalent condition on first chunk - should reduce chunk size', $new_chunk_size);
[214] Fix | Delete
} elseif ($this->remotesend_use_chunk_size >= 131072 && $is_413) {
[215] Fix | Delete
// In this limited case, where we got a 413 but the chunk is not number 1, our algorithm/architecture doesn't allow us to just resume immediately with a new chunk size. However, we can just have UD reduce the chunk size on its next resumption.
[216] Fix | Delete
$new_chunk_size = floor($this->remotesend_use_chunk_size / 2);
[217] Fix | Delete
$this->remotesend_set_new_chunk_size($new_chunk_size);
[218] Fix | Delete
$log_msg = "Returned WP_Error: code=".$put_chunk->get_error_code().", message=".$put_chunk->get_error_message();
[219] Fix | Delete
$log_msg .= " - reducing chunk size to: ".$new_chunk_size." and then scheduling resumption/aborting";
[220] Fix | Delete
$this->log($log_msg);
[221] Fix | Delete
UpdraftPlus_Job_Scheduler::reschedule(50);
[222] Fix | Delete
UpdraftPlus_Job_Scheduler::record_still_alive();
[223] Fix | Delete
die;
[224] Fix | Delete
[225] Fix | Delete
}
[226] Fix | Delete
}
[227] Fix | Delete
}
[228] Fix | Delete
$put_chunk = $this->send_message('send_chunk', $data, 240);
[229] Fix | Delete
}
[230] Fix | Delete
[231] Fix | Delete
if (is_wp_error($put_chunk)) {
[232] Fix | Delete
// The exception handler is within this class. So we can store the data.
[233] Fix | Delete
$this->remotesend_chunked_wp_error = $put_chunk;
[234] Fix | Delete
throw new Exception($put_chunk->get_error_message().' ('.$put_chunk->get_error_code().')');
[235] Fix | Delete
}
[236] Fix | Delete
[237] Fix | Delete
if (!is_array($put_chunk) || empty($put_chunk['response'])) throw new Exception(__('Unexpected response:', 'updraftplus').' '.serialize($put_chunk));
[238] Fix | Delete
[239] Fix | Delete
if ('error' == $put_chunk['response']) {
[240] Fix | Delete
$msg = $put_chunk['data'];
[241] Fix | Delete
// Could interpret the codes to get more interesting messages directly to the user
[242] Fix | Delete
// The textual prefixes here were added after 1.12.5 - hence optional when parsing
[243] Fix | Delete
if (preg_match('/^invalid_start_too_big:(start=)?(\d+),(existing_size=)?(\d+)/', $msg, $matches)) {
[244] Fix | Delete
$existing_size = $matches[2];
[245] Fix | Delete
if ($existing_size < $this->remotesend_uploaded_size) {
[246] Fix | Delete
// The file on the remote system seems to have shrunk. Could be some load-balancing system with a distributed filesystem that is only eventually consistent.
[247] Fix | Delete
return new WP_Error('try_again', 'File on remote system is smaller than expected - perhaps an eventually-consistent filesystem (wait and retry)');
[248] Fix | Delete
}
[249] Fix | Delete
}
[250] Fix | Delete
throw new Exception(__('Error:', 'updraftplus').' '.$msg);
[251] Fix | Delete
}
[252] Fix | Delete
[253] Fix | Delete
if ('file_status' != $put_chunk['response']) throw new Exception(__('Unexpected response:', 'updraftplus').' '.serialize($put_chunk));
[254] Fix | Delete
[255] Fix | Delete
// Possible statuses: 0=temporary file (or not present), 1=file
[256] Fix | Delete
if (empty($put_chunk['data']) || !is_array($put_chunk['data'])) {
[257] Fix | Delete
$this->log("Unexpected response when putting chunk $chunk_index: ".serialize($put_chunk));
[258] Fix | Delete
return false;
[259] Fix | Delete
} else {
[260] Fix | Delete
$remote_size = (int) $put_chunk['data']['size'];
[261] Fix | Delete
$this->remotesend_uploaded_size = $remote_size;
[262] Fix | Delete
}
[263] Fix | Delete
[264] Fix | Delete
return true;
[265] Fix | Delete
[266] Fix | Delete
}
[267] Fix | Delete
[268] Fix | Delete
/**
[269] Fix | Delete
* This function will send a message to the remote site to inform it that the backup has finished sending, on success will update the jobdata key upload_completed and return true else false
[270] Fix | Delete
*
[271] Fix | Delete
* @return Boolean - returns true on success or false on error, all errors are logged to the backup log
[272] Fix | Delete
*/
[273] Fix | Delete
public function upload_completed() {
[274] Fix | Delete
global $updraftplus;
[275] Fix | Delete
[276] Fix | Delete
$service = $updraftplus->jobdata_get('service');
[277] Fix | Delete
$remote_sent = (!empty($service) && ((is_array($service) && in_array('remotesend', $service)) || 'remotesend' === $service));
[278] Fix | Delete
[279] Fix | Delete
if (!$remote_sent) return;
[280] Fix | Delete
[281] Fix | Delete
// ensure options have been loaded
[282] Fix | Delete
$this->options = $this->get_options();
[283] Fix | Delete
[284] Fix | Delete
try {
[285] Fix | Delete
$storage = $this->bootstrap();
[286] Fix | Delete
if (is_wp_error($storage)) throw new Exception($storage->get_error_message());
[287] Fix | Delete
if (!is_object($storage)) throw new Exception("RPC service error");
[288] Fix | Delete
} catch (Exception $e) {
[289] Fix | Delete
$message = $e->getMessage().' ('.get_class($e).') (line: '.$e->getLine().', file: '.$e->getFile().')';
[290] Fix | Delete
$this->log("RPC service error: ".$message);
[291] Fix | Delete
$this->log($message, 'error');
[292] Fix | Delete
return false;
[293] Fix | Delete
}
[294] Fix | Delete
[295] Fix | Delete
if (is_wp_error($storage)) return $updraftplus->log_wp_error($storage, false, true);
[296] Fix | Delete
[297] Fix | Delete
for ($i = 0; $i < 3; $i++) {
[298] Fix | Delete
[299] Fix | Delete
$success = false;
[300] Fix | Delete
[301] Fix | Delete
$response = $this->send_message('upload_complete', array('job_id' => $updraftplus->nonce), 30);
[302] Fix | Delete
[303] Fix | Delete
if (is_wp_error($response)) {
[304] Fix | Delete
$message = $response->get_error_message().' (upload_complete: '.$response->get_error_code().')';
[305] Fix | Delete
$this->log("RPC service error: ".$message);
[306] Fix | Delete
$this->log($message, 'error');
[307] Fix | Delete
} elseif (!is_array($response) || empty($response['response'])) {
[308] Fix | Delete
$this->log("RPC service error: ".serialize($response));
[309] Fix | Delete
$this->log(serialize($response), 'error');
[310] Fix | Delete
} elseif ('error' == $response['response']) {
[311] Fix | Delete
// Could interpret the codes to get more interesting messages directly to the user
[312] Fix | Delete
$msg = $response['data'];
[313] Fix | Delete
$this->log("RPC service error: ".$msg);
[314] Fix | Delete
$this->log($msg, 'error');
[315] Fix | Delete
} elseif ('file_status' == $response['response']) {
[316] Fix | Delete
$success = true;
[317] Fix | Delete
break;
[318] Fix | Delete
}
[319] Fix | Delete
[320] Fix | Delete
sleep(5);
[321] Fix | Delete
}
[322] Fix | Delete
[323] Fix | Delete
return $success;
[324] Fix | Delete
}
[325] Fix | Delete
[326] Fix | Delete
/**
[327] Fix | Delete
* Change the chunk size
[328] Fix | Delete
*
[329] Fix | Delete
* @param Integer $new_chunk_size - in bytes
[330] Fix | Delete
*/
[331] Fix | Delete
private function remotesend_set_new_chunk_size($new_chunk_size) {
[332] Fix | Delete
global $updraftplus;
[333] Fix | Delete
$this->remotesend_use_chunk_size = $new_chunk_size;
[334] Fix | Delete
$updraftplus->jobdata_set('remotesend_chunksize', $new_chunk_size);
[335] Fix | Delete
// Save, so that we don't have to cycle through the illegitimate/larger chunk sizes each time. Set the transient expiry to 120 days, in case they change hosting/configuration - so that we're not stuck on the lower size forever.
[336] Fix | Delete
set_transient($this->remote_sent_defchunk_transient, $new_chunk_size, 86400*120);
[337] Fix | Delete
}
[338] Fix | Delete
[339] Fix | Delete
/**
[340] Fix | Delete
* Send a message to the remote site
[341] Fix | Delete
*
[342] Fix | Delete
* @param String $message - the message identifier
[343] Fix | Delete
* @param Array|Null $data - the data to send with the message
[344] Fix | Delete
* @param Integer $timeout - timeout in waiting for a response
[345] Fix | Delete
*
[346] Fix | Delete
* @return Array|WP_Error - results, or an error
[347] Fix | Delete
*/
[348] Fix | Delete
private function send_message($message, $data = null, $timeout = 30) {
[349] Fix | Delete
$storage = $this->get_storage();
[350] Fix | Delete
[351] Fix | Delete
if (is_array($this->try_format_upgrade) && is_array($data)) {
[352] Fix | Delete
$data['sender_public'] = $this->try_format_upgrade['local_public'];
[353] Fix | Delete
}
[354] Fix | Delete
[355] Fix | Delete
$response = $storage->send_message($message, $data, $timeout);
[356] Fix | Delete
[357] Fix | Delete
if (is_array($response) && !empty($response['data']) && is_array($response['data'])) {
[358] Fix | Delete
[359] Fix | Delete
if (!empty($response['data']['php_events']) && !empty($response['data']['previous_data'])) {
[360] Fix | Delete
foreach ($response['data']['php_events'] as $logline) {
[361] Fix | Delete
$this->log("From remote side: ".$logline);
[362] Fix | Delete
}
[363] Fix | Delete
$response['data'] = $response['data']['previous_data'];
[364] Fix | Delete
}
[365] Fix | Delete
[366] Fix | Delete
if (is_array($response) && is_array($response['data']) && !empty($response['data']['got_public'])) {
[367] Fix | Delete
$name_indicator = $this->try_format_upgrade['name_indicator'];
[368] Fix | Delete
[369] Fix | Delete
$remotesites = UpdraftPlus_Options::get_updraft_option('updraft_remotesites');
[370] Fix | Delete
[371] Fix | Delete
foreach ($remotesites as $key => $site) {
[372] Fix | Delete
if (!is_array($site) || empty($site['name_indicator']) || $site['name_indicator'] != $name_indicator) continue;
[373] Fix | Delete
// This means 'format 2'
[374] Fix | Delete
$this->try_format_upgrade = true;
[375] Fix | Delete
$remotesites[$key]['remote_got_public'] = 1;
[376] Fix | Delete
// If this DB save fails, they'll have to recreate the key
[377] Fix | Delete
UpdraftPlus_Options::update_updraft_option('updraft_remotesites', $remotesites);
[378] Fix | Delete
// Now we need to get a fresh storage object, because the remote end will no longer accept messages with format=1
[379] Fix | Delete
$this->set_storage(null);
[380] Fix | Delete
$this->do_bootstrap(null);
[381] Fix | Delete
break;
[382] Fix | Delete
}
[383] Fix | Delete
}
[384] Fix | Delete
}
[385] Fix | Delete
[386] Fix | Delete
return $response;
[387] Fix | Delete
}
[388] Fix | Delete
[389] Fix | Delete
public function do_bootstrap($opts, $connect = true) {// phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable -- $connect unused
[390] Fix | Delete
[391] Fix | Delete
global $updraftplus;
[392] Fix | Delete
[393] Fix | Delete
if (!class_exists('UpdraftPlus_Remote_Communications')) include_once(apply_filters('updraftplus_class_udrpc_path', UPDRAFTPLUS_DIR.'/includes/class-udrpc.php', $updraftplus->version));
[394] Fix | Delete
[395] Fix | Delete
$opts = $this->get_opts();
[396] Fix | Delete
[397] Fix | Delete
try {
[398] Fix | Delete
$ud_rpc = new UpdraftPlus_Remote_Communications($opts['name_indicator']);
[399] Fix | Delete
if (!empty($opts['format_support']) && 2 == $opts['format_support'] && !empty($opts['local_private']) && !empty($opts['local_public']) && !empty($opts['remote_got_public'])) {
[400] Fix | Delete
$ud_rpc->set_message_format(2);
[401] Fix | Delete
$ud_rpc->set_key_remote($opts['key']);
[402] Fix | Delete
$ud_rpc->set_key_local($opts['local_private']);
[403] Fix | Delete
} else {
[404] Fix | Delete
// Enforce the legacy communications protocol (which is only suitable for when only one side only sends, and the other only receives - which is what we happen to do)
[405] Fix | Delete
$ud_rpc->set_message_format(1);
[406] Fix | Delete
$ud_rpc->set_key_local($opts['key']);
[407] Fix | Delete
if (!empty($opts['format_support']) && 2 == $opts['format_support'] && !empty($opts['local_public']) && !empty($opts['local_private'])) {
[408] Fix | Delete
$this->try_format_upgrade = array('name_indicator' => $opts['name_indicator'], 'local_public' => $opts['local_public']);
[409] Fix | Delete
}
[410] Fix | Delete
}
[411] Fix | Delete
$ud_rpc->set_destination_url($opts['url']);
[412] Fix | Delete
$ud_rpc->activate_replay_protection();
[413] Fix | Delete
} catch (Exception $e) {
[414] Fix | Delete
return new WP_Error('rpc_failure', "Commmunications failure: ".$e->getMessage().' (line: '.$e->getLine().', file: '.$e->getFile().')');
[415] Fix | Delete
}
[416] Fix | Delete
[417] Fix | Delete
do_action('updraftplus_remotesend_udrpc_object_obtained', $ud_rpc, $opts);
[418] Fix | Delete
[419] Fix | Delete
$this->set_storage($ud_rpc);
[420] Fix | Delete
[421] Fix | Delete
return $ud_rpc;
[422] Fix | Delete
}
[423] Fix | Delete
[424] Fix | Delete
public function options_exist($opts) {
[425] Fix | Delete
if (is_array($opts) && !empty($opts['url']) && !empty($opts['name_indicator']) && !empty($opts['key'])) return true;
[426] Fix | Delete
return false;
[427] Fix | Delete
}
[428] Fix | Delete
[429] Fix | Delete
public function get_opts() {
[430] Fix | Delete
global $updraftplus;
[431] Fix | Delete
$opts = $updraftplus->jobdata_get('remotesend_info');
[432] Fix | Delete
$opts = $this->clone_remotesend_options($opts);
[433] Fix | Delete
if (true === $this->try_format_upgrade && is_array($opts)) $opts['remote_got_public'] = 1;
[434] Fix | Delete
return is_array($opts) ? $opts : array();
[435] Fix | Delete
}
[436] Fix | Delete
[437] Fix | Delete
/**
[438] Fix | Delete
* This function will check the options we have for the remote send and if it's a clone job and there are missing settings it will call the mothership to get this information.
[439] Fix | Delete
*
[440] Fix | Delete
* @param Array $opts - an array of remote send options
[441] Fix | Delete
*
[442] Fix | Delete
* @return Array - an array of options
[443] Fix | Delete
*/
[444] Fix | Delete
public function clone_remotesend_options($opts) {
[445] Fix | Delete
[446] Fix | Delete
// Don't call self::log() - this then requests options (to get the label), causing an infinite loop.
[447] Fix | Delete
[448] Fix | Delete
global $updraftplus, $updraftplus_admin;
[449] Fix | Delete
if (empty($updraftplus_admin)) include_once(UPDRAFTPLUS_DIR.'/admin.php');
[450] Fix | Delete
[451] Fix | Delete
$clone_job = $updraftplus->jobdata_get('clone_job');
[452] Fix | Delete
[453] Fix | Delete
// check this is a clone job before we proceed
[454] Fix | Delete
if (empty($clone_job)) return $opts;
[455] Fix | Delete
[456] Fix | Delete
// check that we don't already have the needed information
[457] Fix | Delete
if (is_array($opts) && !empty($opts['url']) && !empty($opts['name_indicator']) && !empty($opts['key'])) return $opts;
[458] Fix | Delete
[459] Fix | Delete
$updraftplus->jobdata_set('jobstatus', 'clonepolling');
[460] Fix | Delete
$clone_id = $updraftplus->jobdata_get('clone_id');
[461] Fix | Delete
$clone_url = $updraftplus->jobdata_get('clone_url');
[462] Fix | Delete
$clone_key = $updraftplus->jobdata_get('clone_key');
[463] Fix | Delete
$secret_token = $updraftplus->jobdata_get('secret_token');
[464] Fix | Delete
[465] Fix | Delete
if (empty($clone_id) && empty($secret_token)) return $opts;
[466] Fix | Delete
[467] Fix | Delete
$params = array('clone_id' => $clone_id, 'secret_token' => $secret_token);
[468] Fix | Delete
$response = $updraftplus->get_updraftplus_clone()->clone_info_poll($params);
[469] Fix | Delete
[470] Fix | Delete
if (!isset($response['status']) || 'success' != $response['status']) {
[471] Fix | Delete
$updraftplus->log("UpdraftClone migration information poll failed with code: " . $response['code']);
[472] Fix | Delete
return $opts;
[473] Fix | Delete
}
[474] Fix | Delete
[475] Fix | Delete
if (!isset($response['data']) || !isset($response['data']['url']) || !isset($response['data']['key'])) {
[476] Fix | Delete
$updraftplus->log("UpdraftClone migration information poll unexpected return information with code:" . $response['code']);
[477] Fix | Delete
return $opts;
[478] Fix | Delete
}
[479] Fix | Delete
[480] Fix | Delete
$clone_url = $response['data']['url'];
[481] Fix | Delete
$clone_key = json_decode($response['data']['key'], true);
[482] Fix | Delete
[483] Fix | Delete
if (empty($clone_url) || empty($clone_key)) {
[484] Fix | Delete
$updraftplus->log("UpdraftClone migration information not found (probably still provisioning): will poll again in 60");
[485] Fix | Delete
UpdraftPlus_Job_Scheduler::reschedule(60);
[486] Fix | Delete
UpdraftPlus_Job_Scheduler::record_still_alive();
[487] Fix | Delete
die;
[488] Fix | Delete
}
[489] Fix | Delete
[490] Fix | Delete
// Store the information
[491] Fix | Delete
$remotesites = UpdraftPlus_Options::get_updraft_option('updraft_remotesites');
[492] Fix | Delete
if (!is_array($remotesites)) $remotesites = array();
[493] Fix | Delete
[494] Fix | Delete
foreach ($remotesites as $k => $rsite) {
[495] Fix | Delete
if (!is_array($rsite)) continue;
[496] Fix | Delete
if ($rsite['url'] == $clone_key['url']) unset($remotesites[$k]);
[497] Fix | Delete
}
[498] Fix | Delete
[499] Fix | Delete
12
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function