Edit File by line
/home/barbar84/www/wp-conte.../plugins/updraftp.../methods
File: dropbox.php
<?php
[0] Fix | Delete
/**
[1] Fix | Delete
* https://www.dropbox.com/developers/apply?cont=/developers/apps
[2] Fix | Delete
*/
[3] Fix | Delete
[4] Fix | Delete
if (!defined('UPDRAFTPLUS_DIR')) die('No direct access allowed.');
[5] Fix | Delete
[6] Fix | Delete
// Converted to multi-options (Feb 2017-) and previous options conversion removed: Yes
[7] Fix | Delete
[8] Fix | Delete
if (!class_exists('UpdraftPlus_BackupModule')) require_once(UPDRAFTPLUS_DIR.'/methods/backup-module.php');
[9] Fix | Delete
[10] Fix | Delete
// Fix a potential problem for users who had the short-lived 1.12.35-1.12.38 free versions (see: https://wordpress.org/support/topic/1-12-37-dropbox-auth-broken/page/2/#post-8981457)
[11] Fix | Delete
// Can be removed after a few months
[12] Fix | Delete
$potential_options = UpdraftPlus_Options::get_updraft_option('updraft_dropbox');
[13] Fix | Delete
if (is_array($potential_options) && isset($potential_options['version']) && isset($potential_options['settings']) && array() === $potential_options['settings']) {
[14] Fix | Delete
// Wipe it, which will force its re-creation in proper format
[15] Fix | Delete
UpdraftPlus_Options::delete_updraft_option('updraft_dropbox');
[16] Fix | Delete
}
[17] Fix | Delete
[18] Fix | Delete
class UpdraftPlus_BackupModule_dropbox extends UpdraftPlus_BackupModule {
[19] Fix | Delete
[20] Fix | Delete
private $current_file_hash;
[21] Fix | Delete
[22] Fix | Delete
private $current_file_size;
[23] Fix | Delete
[24] Fix | Delete
private $uploaded_offset;
[25] Fix | Delete
[26] Fix | Delete
private $upload_tick;
[27] Fix | Delete
[28] Fix | Delete
/**
[29] Fix | Delete
* This callback is called as upload progress is made
[30] Fix | Delete
*
[31] Fix | Delete
* @param Integer $offset - the byte offset
[32] Fix | Delete
* @param String $uploadid - identifier for the upload in progress
[33] Fix | Delete
* @param Boolean|String $fullpath - optional full path to the file being uploaded
[34] Fix | Delete
*/
[35] Fix | Delete
public function chunked_callback($offset, $uploadid, $fullpath = false) {
[36] Fix | Delete
[37] Fix | Delete
global $updraftplus;
[38] Fix | Delete
[39] Fix | Delete
$storage = $this->get_storage();
[40] Fix | Delete
[41] Fix | Delete
// Update upload ID
[42] Fix | Delete
$this->jobdata_set('upload_id_'.$this->current_file_hash, $uploadid);
[43] Fix | Delete
$this->jobdata_set('upload_offset_'.$this->current_file_hash, $offset);
[44] Fix | Delete
[45] Fix | Delete
$time_now = microtime(true);
[46] Fix | Delete
[47] Fix | Delete
$time_since_last_tick = $time_now - $this->upload_tick;
[48] Fix | Delete
$data_since_last_tick = $offset - $this->uploaded_offset;
[49] Fix | Delete
[50] Fix | Delete
$this->upload_tick = $time_now;
[51] Fix | Delete
$this->uploaded_offset = $offset;
[52] Fix | Delete
[53] Fix | Delete
// Here we use job-wide data, because we don't expect wildly different performance for different Dropbox accounts
[54] Fix | Delete
$chunk_size = $updraftplus->jobdata_get('dropbox_chunk_size', 1048576);
[55] Fix | Delete
// Don't go beyond 10MB, or change the chunk size after the last segment
[56] Fix | Delete
if ($chunk_size < 10485760 && $this->current_file_size > 0 && $offset < $this->current_file_size) {
[57] Fix | Delete
$job_run_time = $time_now - $updraftplus->job_time_ms;
[58] Fix | Delete
if ($time_since_last_tick < 10) {
[59] Fix | Delete
$upload_rate = $data_since_last_tick / max($time_since_last_tick, 1);
[60] Fix | Delete
$upload_secs = min(floor($job_run_time), 10);
[61] Fix | Delete
if ($job_run_time < 15) $upload_secs = max(6, $job_run_time*0.6);
[62] Fix | Delete
$new_chunk = max(min($upload_secs * $upload_rate * 0.9, 10485760), 1048576);
[63] Fix | Delete
$new_chunk = $new_chunk - ($new_chunk % 524288);
[64] Fix | Delete
$chunk_size = (int) $new_chunk;
[65] Fix | Delete
$storage->setChunkSize($chunk_size);
[66] Fix | Delete
$updraftplus->jobdata_set('dropbox_chunk_size', $chunk_size);
[67] Fix | Delete
}
[68] Fix | Delete
}
[69] Fix | Delete
[70] Fix | Delete
if ($this->current_file_size > 0) {
[71] Fix | Delete
$percent = round(100*($offset/$this->current_file_size), 1);
[72] Fix | Delete
$updraftplus->record_uploaded_chunk($percent, "$uploadid, $offset, ".round($chunk_size/1024, 1)." KB", $fullpath);
[73] Fix | Delete
} else {
[74] Fix | Delete
$this->log("Chunked Upload: $offset bytes uploaded");
[75] Fix | Delete
// This act is done by record_uploaded_chunk, and helps prevent overlapping runs
[76] Fix | Delete
if ($fullpath) touch($fullpath);
[77] Fix | Delete
}
[78] Fix | Delete
}
[79] Fix | Delete
[80] Fix | Delete
/**
[81] Fix | Delete
* Supported features
[82] Fix | Delete
*
[83] Fix | Delete
* @return Array
[84] Fix | Delete
*/
[85] Fix | Delete
public function get_supported_features() {
[86] Fix | Delete
// This options format is handled via only accessing options via $this->get_options()
[87] Fix | Delete
return array('multi_options', 'config_templates', 'multi_storage', 'conditional_logic', 'manual_authentication');
[88] Fix | Delete
}
[89] Fix | Delete
[90] Fix | Delete
/**
[91] Fix | Delete
* Default options
[92] Fix | Delete
*
[93] Fix | Delete
* @return Array
[94] Fix | Delete
*/
[95] Fix | Delete
public function get_default_options() {
[96] Fix | Delete
return array(
[97] Fix | Delete
'appkey' => '',
[98] Fix | Delete
'secret' => '',
[99] Fix | Delete
'folder' => '',
[100] Fix | Delete
'tk_access_token' => '',
[101] Fix | Delete
);
[102] Fix | Delete
}
[103] Fix | Delete
[104] Fix | Delete
/**
[105] Fix | Delete
* Check whether options have been set up by the user, or not
[106] Fix | Delete
*
[107] Fix | Delete
* @param Array $opts - the potential options
[108] Fix | Delete
*
[109] Fix | Delete
* @return Boolean
[110] Fix | Delete
*/
[111] Fix | Delete
public function options_exist($opts) {
[112] Fix | Delete
if (is_array($opts) && !empty($opts['tk_access_token'])) return true;
[113] Fix | Delete
return false;
[114] Fix | Delete
}
[115] Fix | Delete
[116] Fix | Delete
/**
[117] Fix | Delete
* Acts as a WordPress options filter
[118] Fix | Delete
*
[119] Fix | Delete
* @param Array $dropbox - An array of Dropbox options
[120] Fix | Delete
* @return Array - the returned array can either be the set of updated Dropbox settings or a WordPress error array
[121] Fix | Delete
*/
[122] Fix | Delete
public function options_filter($dropbox) {
[123] Fix | Delete
[124] Fix | Delete
// Get the current options (and possibly update them to the new format)
[125] Fix | Delete
$opts = UpdraftPlus_Storage_Methods_Interface::update_remote_storage_options_format('dropbox');
[126] Fix | Delete
[127] Fix | Delete
if (is_wp_error($opts)) {
[128] Fix | Delete
if ('recursion' !== $opts->get_error_code()) {
[129] Fix | Delete
$msg = "(".$opts->get_error_code()."): ".$opts->get_error_message();
[130] Fix | Delete
$this->log($msg);
[131] Fix | Delete
error_log("UpdraftPlus: $msg");
[132] Fix | Delete
}
[133] Fix | Delete
// The saved options had a problem; so, return the new ones
[134] Fix | Delete
return $dropbox;
[135] Fix | Delete
}
[136] Fix | Delete
[137] Fix | Delete
// If the input is not as expected, then return the current options
[138] Fix | Delete
if (!is_array($dropbox)) return $opts;
[139] Fix | Delete
[140] Fix | Delete
// Remove instances that no longer exist
[141] Fix | Delete
foreach ($opts['settings'] as $instance_id => $storage_options) {
[142] Fix | Delete
if (!isset($dropbox['settings'][$instance_id])) unset($opts['settings'][$instance_id]);
[143] Fix | Delete
}
[144] Fix | Delete
[145] Fix | Delete
// Dropbox has a special case where the settings could be empty so we should check for this before
[146] Fix | Delete
if (!empty($dropbox['settings'])) {
[147] Fix | Delete
[148] Fix | Delete
foreach ($dropbox['settings'] as $instance_id => $storage_options) {
[149] Fix | Delete
if (!empty($opts['settings'][$instance_id]['tk_access_token'])) {
[150] Fix | Delete
[151] Fix | Delete
$current_app_key = empty($opts['settings'][$instance_id]['appkey']) ? false : $opts['settings'][$instance_id]['appkey'];
[152] Fix | Delete
$new_app_key = empty($storage_options['appkey']) ? false : $storage_options['appkey'];
[153] Fix | Delete
[154] Fix | Delete
// If a different app key is being used, then wipe the stored token as it cannot belong to the new app
[155] Fix | Delete
if ($current_app_key !== $new_app_key) {
[156] Fix | Delete
unset($opts['settings'][$instance_id]['tk_access_token']);
[157] Fix | Delete
unset($opts['settings'][$instance_id]['ownername']);
[158] Fix | Delete
unset($opts['settings'][$instance_id]['CSRF']);
[159] Fix | Delete
}
[160] Fix | Delete
[161] Fix | Delete
}
[162] Fix | Delete
[163] Fix | Delete
// Now loop over the new options, and replace old options with them
[164] Fix | Delete
foreach ($storage_options as $key => $value) {
[165] Fix | Delete
if (null === $value) {
[166] Fix | Delete
unset($opts['settings'][$instance_id][$key]);
[167] Fix | Delete
} else {
[168] Fix | Delete
if (!isset($opts['settings'][$instance_id])) $opts['settings'][$instance_id] = array();
[169] Fix | Delete
$opts['settings'][$instance_id][$key] = $value;
[170] Fix | Delete
}
[171] Fix | Delete
}
[172] Fix | Delete
[173] Fix | Delete
if (!empty($opts['settings'][$instance_id]['folder']) && preg_match('#^https?://(www.)dropbox\.com/home/Apps/UpdraftPlus(.Com)?([^/]*)/(.*)$#i', $opts['settings'][$instance_id]['folder'], $matches)) $opts['settings'][$instance_id]['folder'] = $matches[3];
[174] Fix | Delete
[175] Fix | Delete
// check if we have the dummy nosave option and remove it so that it doesn't get saved
[176] Fix | Delete
if (isset($opts['settings'][$instance_id]['dummy-nosave'])) unset($opts['settings'][$instance_id]['dummy-nosave']);
[177] Fix | Delete
}
[178] Fix | Delete
[179] Fix | Delete
}
[180] Fix | Delete
[181] Fix | Delete
return $opts;
[182] Fix | Delete
}
[183] Fix | Delete
[184] Fix | Delete
public function backup($backup_array) {
[185] Fix | Delete
[186] Fix | Delete
global $updraftplus;
[187] Fix | Delete
[188] Fix | Delete
$opts = $this->get_options();
[189] Fix | Delete
[190] Fix | Delete
if (empty($opts['tk_access_token'])) {
[191] Fix | Delete
$this->log('You do not appear to be authenticated with Dropbox (1)');
[192] Fix | Delete
$this->log(__('You do not appear to be authenticated with Dropbox', 'updraftplus'), 'error');
[193] Fix | Delete
return false;
[194] Fix | Delete
}
[195] Fix | Delete
[196] Fix | Delete
// 28 September 2017: APIv1 is gone. We'll keep the variable to make life easier if there's ever an APIv3.
[197] Fix | Delete
$use_api_ver = 2;
[198] Fix | Delete
[199] Fix | Delete
if (empty($opts['tk_request_token'])) {
[200] Fix | Delete
$this->log("begin cloud upload (using API version $use_api_ver with OAuth v2 token)");
[201] Fix | Delete
} else {
[202] Fix | Delete
$this->log("begin cloud upload (using API version $use_api_ver with OAuth v1 token)");
[203] Fix | Delete
}
[204] Fix | Delete
[205] Fix | Delete
$chunk_size = $updraftplus->jobdata_get('dropbox_chunk_size', 1048576);
[206] Fix | Delete
[207] Fix | Delete
try {
[208] Fix | Delete
$dropbox = $this->bootstrap();
[209] Fix | Delete
if (false === $dropbox) throw new Exception(__('You do not appear to be authenticated with Dropbox', 'updraftplus'));
[210] Fix | Delete
$this->log("access gained; setting chunk size to: ".round($chunk_size/1024, 1)." KB");
[211] Fix | Delete
$dropbox->setChunkSize($chunk_size);
[212] Fix | Delete
} catch (Exception $e) {
[213] Fix | Delete
$this->log('error when trying to gain access: '.$e->getMessage().' (line: '.$e->getLine().', file: '.$e->getFile().')');
[214] Fix | Delete
$this->log(sprintf(__('error: %s (see log file for more)', 'updraftplus'), $e->getMessage()), 'error');
[215] Fix | Delete
return false;
[216] Fix | Delete
}
[217] Fix | Delete
[218] Fix | Delete
$updraft_dir = $updraftplus->backups_dir_location();
[219] Fix | Delete
[220] Fix | Delete
foreach ($backup_array as $file) {
[221] Fix | Delete
[222] Fix | Delete
$available_quota = -1;
[223] Fix | Delete
[224] Fix | Delete
// If we experience any failures collecting account info, then carry on anyway
[225] Fix | Delete
try {
[226] Fix | Delete
[227] Fix | Delete
/*
[228] Fix | Delete
Quota information is no longer provided with account information a new call to quotaInfo must be made to get this information.
[229] Fix | Delete
*/
[230] Fix | Delete
$quota_info = $dropbox->quotaInfo();
[231] Fix | Delete
[232] Fix | Delete
// Access token expired try to refresh and then call quota info again
[233] Fix | Delete
if ("401" == $quota_info['code']) {
[234] Fix | Delete
$this->log('HTTP code 401 (unauthorized) code returned from Dropbox; attempting to refresh access token');
[235] Fix | Delete
$dropbox->refreshAccessToken();
[236] Fix | Delete
$quota_info = $dropbox->quotaInfo();
[237] Fix | Delete
}
[238] Fix | Delete
[239] Fix | Delete
if ("200" != $quota_info['code']) {
[240] Fix | Delete
$message = "account/info did not return HTTP 200; returned: ". $quota_info['code'];
[241] Fix | Delete
} elseif (!isset($quota_info['body'])) {
[242] Fix | Delete
$message = "account/info did not return the expected data";
[243] Fix | Delete
} else {
[244] Fix | Delete
$body = $quota_info['body'];
[245] Fix | Delete
if (isset($body->quota_info)) {
[246] Fix | Delete
$quota_info = $body->quota_info;
[247] Fix | Delete
$total_quota = $quota_info->quota;
[248] Fix | Delete
$normal_quota = $quota_info->normal;
[249] Fix | Delete
$shared_quota = $quota_info->shared;
[250] Fix | Delete
$available_quota = $total_quota - ($normal_quota + $shared_quota);
[251] Fix | Delete
$message = "quota usage: normal=".round($normal_quota/1048576, 1)." MB, shared=".round($shared_quota/1048576, 1)." MB, total=".round($total_quota/1048576, 1)." MB, available=".round($available_quota/1048576, 1)." MB";
[252] Fix | Delete
} else {
[253] Fix | Delete
$total_quota = max($body->allocation->allocated, 1);
[254] Fix | Delete
$used = $body->used;
[255] Fix | Delete
/* check here to see if the account is a team account and if so use the other used value
[256] Fix | Delete
This will give us their total usage including their individual account and team account */
[257] Fix | Delete
if (isset($body->allocation->used)) $used = $body->allocation->used;
[258] Fix | Delete
$available_quota = $total_quota - $used;
[259] Fix | Delete
$message = "quota usage: used=".round($used/1048576, 1)." MB, total=".round($total_quota/1048576, 1)." MB, available=".round($available_quota/1048576, 1)." MB";
[260] Fix | Delete
}
[261] Fix | Delete
}
[262] Fix | Delete
$this->log($message);
[263] Fix | Delete
} catch (Exception $e) {
[264] Fix | Delete
$this->log("exception (".get_class($e).") occurred whilst getting account info: ".$e->getMessage());
[265] Fix | Delete
// $this->log(sprintf(__("%s error: %s", 'updraftplus'), 'Dropbox', $e->getMessage()).' ('.$e->getCode().')', 'warning', md5($e->getMessage()));
[266] Fix | Delete
}
[267] Fix | Delete
[268] Fix | Delete
$file_success = 1;
[269] Fix | Delete
[270] Fix | Delete
$hash = md5($file);
[271] Fix | Delete
$this->current_file_hash = $hash;
[272] Fix | Delete
[273] Fix | Delete
$filesize = filesize($updraft_dir.'/'.$file);
[274] Fix | Delete
$this->current_file_size = $filesize;
[275] Fix | Delete
[276] Fix | Delete
// Into KB
[277] Fix | Delete
$filesize = $filesize/1024;
[278] Fix | Delete
$microtime = microtime(true);
[279] Fix | Delete
[280] Fix | Delete
if ('None' != ($upload_id = $this->jobdata_get('upload_id_'.$hash, 'None', 'updraf_dbid_'.$hash))) {
[281] Fix | Delete
// Resume
[282] Fix | Delete
$offset = $this->jobdata_get('upload_offset_'.$hash, 0, 'updraf_dbof_'.$hash);
[283] Fix | Delete
if ($offset) $this->log("This is a resumption: $offset bytes had already been uploaded");
[284] Fix | Delete
} else {
[285] Fix | Delete
$offset = 0;
[286] Fix | Delete
$upload_id = 'None';
[287] Fix | Delete
}
[288] Fix | Delete
[289] Fix | Delete
// We don't actually abort now - there's no harm in letting it try and then fail
[290] Fix | Delete
if (-1 != $available_quota && $available_quota < ($filesize-$offset)) {
[291] Fix | Delete
$this->log("File upload expected to fail: file data remaining to upload ($file) size is ".($filesize-$offset)." b (overall file size; .".($filesize*1024)." b), whereas available quota is only $available_quota b");
[292] Fix | Delete
// $this->log(sprintf(__("Account full: your %s account has only %d bytes left, but the file to be uploaded has %d bytes remaining (total size: %d bytes)",'updraftplus'),'Dropbox', $available_quota, $filesize-$offset, $filesize), 'warning');
[293] Fix | Delete
}
[294] Fix | Delete
[295] Fix | Delete
$ufile = apply_filters('updraftplus_dropbox_modpath', $file, $this);
[296] Fix | Delete
[297] Fix | Delete
$this->log("Attempt to upload: $file to: $ufile");
[298] Fix | Delete
[299] Fix | Delete
$this->upload_tick = microtime(true);
[300] Fix | Delete
$this->uploaded_offset = $offset;
[301] Fix | Delete
[302] Fix | Delete
try {
[303] Fix | Delete
$response = $dropbox->chunkedUpload($updraft_dir.'/'.$file, '', $ufile, true, $offset, $upload_id, array($this, 'chunked_callback'));
[304] Fix | Delete
if (empty($response['code']) || "200" != $response['code']) {
[305] Fix | Delete
$this->log('Unexpected HTTP code returned from Dropbox: '.$response['code']." (".serialize($response).")");
[306] Fix | Delete
if ($response['code'] >= 400) {
[307] Fix | Delete
if (401 == $response['code']) {
[308] Fix | Delete
$this->log('HTTP code 401 returned from Dropbox, refreshing access token');
[309] Fix | Delete
$dropbox->refreshAccessToken();
[310] Fix | Delete
}
[311] Fix | Delete
$this->log(sprintf(__('error: failed to upload file to %s (see log file for more)', 'updraftplus'), $file), 'error');
[312] Fix | Delete
$file_success = 0;
[313] Fix | Delete
} else {
[314] Fix | Delete
$this->log(__('did not return the expected response - check your log file for more details', 'updraftplus'), 'warning');
[315] Fix | Delete
}
[316] Fix | Delete
}
[317] Fix | Delete
} catch (Exception $e) {
[318] Fix | Delete
$this->log("chunked upload exception (".get_class($e)."): ".$e->getMessage().' (line: '.$e->getLine().', file: '.$e->getFile().')');
[319] Fix | Delete
if (preg_match("/Submitted input out of alignment: got \[(\d+)\] expected \[(\d+)\]/i", $e->getMessage(), $matches)) {
[320] Fix | Delete
// Try the indicated offset
[321] Fix | Delete
$we_tried = $matches[1];
[322] Fix | Delete
$dropbox_wanted = (int) $matches[2];
[323] Fix | Delete
$this->log("not yet aligned: tried=$we_tried, wanted=$dropbox_wanted; will attempt recovery");
[324] Fix | Delete
$this->uploaded_offset = $dropbox_wanted;
[325] Fix | Delete
$upload_id = $this->jobdata_get('upload_id_'.$hash, 'None', 'updraf_dbid_'.$hash);
[326] Fix | Delete
try {
[327] Fix | Delete
$dropbox->chunkedUpload($updraft_dir.'/'.$file, '', $ufile, true, $dropbox_wanted, $upload_id, array($this, 'chunked_callback'));
[328] Fix | Delete
} catch (Exception $e) {
[329] Fix | Delete
$msg = $e->getMessage();
[330] Fix | Delete
if (preg_match('/Upload with upload_id .* already completed/', $msg)) {
[331] Fix | Delete
$this->log('returned an error, but apparently indicating previous success: '.$msg);
[332] Fix | Delete
} else {
[333] Fix | Delete
$this->log($msg.' (line: '.$e->getLine().', file: '.$e->getFile().')');
[334] Fix | Delete
$this->log(sprintf(__('failed to upload file to %s (see log file for more)', 'updraftplus'), $ufile), 'error');
[335] Fix | Delete
$file_success = 0;
[336] Fix | Delete
if (strpos($msg, 'select/poll returned error') !== false && $this->upload_tick > 0 && time() - $this->upload_tick > 800) {
[337] Fix | Delete
UpdraftPlus_Job_Scheduler::reschedule(60);
[338] Fix | Delete
$this->log("Select/poll returned after a long time: scheduling a resumption and terminating for now");
[339] Fix | Delete
UpdraftPlus_Job_Scheduler::record_still_alive();
[340] Fix | Delete
die;
[341] Fix | Delete
}
[342] Fix | Delete
}
[343] Fix | Delete
}
[344] Fix | Delete
} else {
[345] Fix | Delete
$msg = $e->getMessage();
[346] Fix | Delete
if (preg_match('/Upload with upload_id .* already completed/', $msg)) {
[347] Fix | Delete
$this->log('returned an error, but apparently indicating previous success: '.$msg);
[348] Fix | Delete
} else {
[349] Fix | Delete
$this->log(sprintf(__('failed to upload file to %s (see log file for more)', 'updraftplus'), $ufile), 'error');
[350] Fix | Delete
$file_success = 0;
[351] Fix | Delete
if (strpos($msg, 'select/poll returned error') !== false && $this->upload_tick > 0 && time() - $this->upload_tick > 800) {
[352] Fix | Delete
UpdraftPlus_Job_Scheduler::reschedule(60);
[353] Fix | Delete
$this->log("Select/poll returned after a long time: scheduling a resumption and terminating for now");
[354] Fix | Delete
UpdraftPlus_Job_Scheduler::record_still_alive();
[355] Fix | Delete
die;
[356] Fix | Delete
}
[357] Fix | Delete
}
[358] Fix | Delete
}
[359] Fix | Delete
}
[360] Fix | Delete
if ($file_success) {
[361] Fix | Delete
$updraftplus->uploaded_file($file);
[362] Fix | Delete
$microtime_elapsed = microtime(true)-$microtime;
[363] Fix | Delete
$speedps = ($microtime_elapsed > 0) ? $filesize/$microtime_elapsed : 0;
[364] Fix | Delete
$speed = sprintf("%.2d", $filesize)." KB in ".sprintf("%.2d", $microtime_elapsed)."s (".sprintf("%.2d", $speedps)." KB/s)";
[365] Fix | Delete
$this->log("File upload success (".$file."): $speed");
[366] Fix | Delete
$this->jobdata_delete('upload_id_'.$hash, 'updraf_dbid_'.$hash);
[367] Fix | Delete
$this->jobdata_delete('upload_offset_'.$hash, 'updraf_dbof_'.$hash);
[368] Fix | Delete
}
[369] Fix | Delete
[370] Fix | Delete
}
[371] Fix | Delete
[372] Fix | Delete
return null;
[373] Fix | Delete
[374] Fix | Delete
}
[375] Fix | Delete
[376] Fix | Delete
/**
[377] Fix | Delete
* This method gets a list of files from the remote stoage that match the string passed in and returns an array of backups
[378] Fix | Delete
*
[379] Fix | Delete
* @param String $match a substring to require (tested via strpos() !== false)
[380] Fix | Delete
* @return Array
[381] Fix | Delete
*/
[382] Fix | Delete
public function listfiles($match = 'backup_') {
[383] Fix | Delete
[384] Fix | Delete
$opts = $this->get_options();
[385] Fix | Delete
[386] Fix | Delete
if (empty($opts['tk_access_token'])) return new WP_Error('no_settings', __('No settings were found', 'updraftplus').' (dropbox)');
[387] Fix | Delete
[388] Fix | Delete
try {
[389] Fix | Delete
$dropbox = $this->bootstrap();
[390] Fix | Delete
} catch (Exception $e) {
[391] Fix | Delete
$this->log('access error: '.$e->getMessage().' (line: '.$e->getLine().', file: '.$e->getFile().')');
[392] Fix | Delete
return new WP_Error('access_error', $e->getMessage());
[393] Fix | Delete
}
[394] Fix | Delete
[395] Fix | Delete
$searchpath = '/'.untrailingslashit(apply_filters('updraftplus_dropbox_modpath', '', $this));
[396] Fix | Delete
[397] Fix | Delete
try {
[398] Fix | Delete
/* Some users could have a large amount of backups, the max search is 1000 entries we should continue to search until there are no more entries to bring back. */
[399] Fix | Delete
$cursor = '';
[400] Fix | Delete
$matches = array();
[401] Fix | Delete
[402] Fix | Delete
while (true) {
[403] Fix | Delete
$search = $dropbox->search($match, $searchpath, 1000, $cursor);
[404] Fix | Delete
if (empty($search['code']) || 200 != $search['code']) return new WP_Error('response_error', sprintf(__('%s returned an unexpected HTTP response: %s', 'updraftplus'), 'Dropbox', $search['code']), $search['body']);
[405] Fix | Delete
[406] Fix | Delete
if (empty($search['body'])) return array();
[407] Fix | Delete
[408] Fix | Delete
if (isset($search['body']->matches) && is_array($search['body']->matches)) {
[409] Fix | Delete
$matches = array_merge($matches, $search['body']->matches);
[410] Fix | Delete
} elseif (is_array($search['body'])) {
[411] Fix | Delete
$matches = $search['body'];
[412] Fix | Delete
} else {
[413] Fix | Delete
break;
[414] Fix | Delete
}
[415] Fix | Delete
[416] Fix | Delete
if (isset($search['body']->has_more) && true == $search['body']->has_more && isset($search['body']->cursor)) {
[417] Fix | Delete
$cursor = $search['body']->cursor;
[418] Fix | Delete
} else {
[419] Fix | Delete
break;
[420] Fix | Delete
}
[421] Fix | Delete
}
[422] Fix | Delete
[423] Fix | Delete
} catch (Exception $e) {
[424] Fix | Delete
$this->log($e->getMessage().' (line: '.$e->getLine().', file: '.$e->getFile().')');
[425] Fix | Delete
// The most likely cause of a search_error is specifying a non-existent path, which should just result in an empty result set.
[426] Fix | Delete
// return new WP_Error('search_error', $e->getMessage());
[427] Fix | Delete
return array();
[428] Fix | Delete
}
[429] Fix | Delete
[430] Fix | Delete
$results = array();
[431] Fix | Delete
[432] Fix | Delete
foreach ($matches as $item) {
[433] Fix | Delete
$item = $item->metadata;
[434] Fix | Delete
if (!is_object($item)) continue;
[435] Fix | Delete
if (isset($item->metadata)) $item = $item->metadata; // 2/files/search_v2 has a slightly different output structure compared to 2/files/search model
[436] Fix | Delete
[437] Fix | Delete
if ((!isset($item->size) || $item->size > 0) && 'folder' != $item->{'.tag'} && !empty($item->path_display) && 0 === strpos($item->path_display, $searchpath)) {
[438] Fix | Delete
[439] Fix | Delete
$path = substr($item->path_display, strlen($searchpath));
[440] Fix | Delete
if ('/' == substr($path, 0, 1)) $path = substr($path, 1);
[441] Fix | Delete
[442] Fix | Delete
// Ones in subfolders are not wanted
[443] Fix | Delete
if (false !== strpos($path, '/')) continue;
[444] Fix | Delete
[445] Fix | Delete
$result = array('name' => $path);
[446] Fix | Delete
if (!empty($item->size)) $result['size'] = $item->size;
[447] Fix | Delete
[448] Fix | Delete
$results[] = $result;
[449] Fix | Delete
}
[450] Fix | Delete
}
[451] Fix | Delete
[452] Fix | Delete
return $results;
[453] Fix | Delete
}
[454] Fix | Delete
[455] Fix | Delete
/**
[456] Fix | Delete
* Identification of Dropbox app
[457] Fix | Delete
*
[458] Fix | Delete
* @return Array
[459] Fix | Delete
*/
[460] Fix | Delete
private function defaults() {
[461] Fix | Delete
return apply_filters('updraftplus_dropbox_defaults', array('Z3Q3ZmkwbnplNHA0Zzlx', 'bTY0bm9iNmY4eWhjODRt'));
[462] Fix | Delete
}
[463] Fix | Delete
[464] Fix | Delete
/**
[465] Fix | Delete
* Delete files from the service using the Dropbox API
[466] Fix | Delete
*
[467] Fix | Delete
* @param Array $files - array of filenames to delete
[468] Fix | Delete
* @param Array $data - unused here
[469] Fix | Delete
* @param Array $sizeinfo - unused here
[470] Fix | Delete
* @return Boolean|String - either a boolean true or an error code string
[471] Fix | Delete
*/
[472] Fix | Delete
public function delete($files, $data = null, $sizeinfo = array()) {// phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable -- $data and $sizeinfo unused
[473] Fix | Delete
[474] Fix | Delete
if (is_string($files)) $files = array($files);
[475] Fix | Delete
[476] Fix | Delete
$opts = $this->get_options();
[477] Fix | Delete
[478] Fix | Delete
if (empty($opts['tk_access_token'])) {
[479] Fix | Delete
$this->log('You do not appear to be authenticated with Dropbox (3)');
[480] Fix | Delete
$this->log(sprintf(__('You do not appear to be authenticated with %s (whilst deleting)', 'updraftplus'), 'Dropbox'), 'warning');
[481] Fix | Delete
return 'authentication_fail';
[482] Fix | Delete
}
[483] Fix | Delete
[484] Fix | Delete
try {
[485] Fix | Delete
$dropbox = $this->bootstrap();
[486] Fix | Delete
} catch (Exception $e) {
[487] Fix | Delete
$this->log($e->getMessage().' (line: '.$e->getLine().', file: '.$e->getFile().')');
[488] Fix | Delete
$this->log(sprintf(__('Failed to access %s when deleting (see log file for more)', 'updraftplus'), 'Dropbox'), 'warning');
[489] Fix | Delete
return 'service_unavailable';
[490] Fix | Delete
}
[491] Fix | Delete
if (false === $dropbox) return false;
[492] Fix | Delete
[493] Fix | Delete
$any_failures = false;
[494] Fix | Delete
[495] Fix | Delete
foreach ($files as $file) {
[496] Fix | Delete
$ufile = apply_filters('updraftplus_dropbox_modpath', $file, $this);
[497] Fix | Delete
$this->log("request deletion: $ufile");
[498] Fix | Delete
[499] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function