| 1 |
<?php |
| 2 |
|
| 3 |
// https://www.dropbox.com/developers/apply?cont=/developers/apps |
| 4 |
|
| 5 |
if (!defined('UPDRAFTPLUS_DIR')) die('No direct access allowed.'); |
| 6 |
|
| 7 |
# Converted to job_options: yes |
| 8 |
# Converted to array options: yes |
| 9 |
|
| 10 |
# Migrate options to new-style storage - May 2014 |
| 11 |
# appkey, secret, folder, updraft_dropboxtk_request_token, updraft_dropboxtk_access_token |
| 12 |
if (!is_array(UpdraftPlus_Options::get_updraft_option('updraft_dropbox'))) { |
| 13 |
$opts = array( |
| 14 |
'appkey' => UpdraftPlus_Options::get_updraft_option('updraft_dropbox_appkey'), |
| 15 |
'secret' => UpdraftPlus_Options::get_updraft_option('updraft_dropbox_secret'), |
| 16 |
'folder' => UpdraftPlus_Options::get_updraft_option('updraft_dropbox_folder'), |
| 17 |
'tk_request_token' => UpdraftPlus_Options::get_updraft_option('updraft_dropboxtk_request_token'), |
| 18 |
'tk_access_token' => UpdraftPlus_Options::get_updraft_option('updraft_dropboxtk_access_token'), |
| 19 |
); |
| 20 |
UpdraftPlus_Options::update_updraft_option('updraft_dropbox', $opts); |
| 21 |
UpdraftPlus_Options::delete_updraft_option('updraft_dropbox_appkey'); |
| 22 |
UpdraftPlus_Options::delete_updraft_option('updraft_dropbox_secret'); |
| 23 |
UpdraftPlus_Options::delete_updraft_option('updraft_dropbox_folder'); |
| 24 |
UpdraftPlus_Options::delete_updraft_option('updraft_dropboxtk_request_token'); |
| 25 |
UpdraftPlus_Options::delete_updraft_option('updraft_dropboxtk_access_token'); |
| 26 |
} |
| 27 |
|
| 28 |
class UpdraftPlus_BackupModule_dropbox { |
| 29 |
|
| 30 |
private $current_file_hash; |
| 31 |
private $current_file_size; |
| 32 |
private $dropbox_object; |
| 33 |
|
| 34 |
public function chunked_callback($offset, $uploadid, $fullpath = false) { |
| 35 |
global $updraftplus; |
| 36 |
|
| 37 |
// Update upload ID |
| 38 |
$updraftplus->jobdata_set('updraf_dbid_'.$this->current_file_hash, $uploadid); |
| 39 |
$updraftplus->jobdata_set('updraf_dbof_'.$this->current_file_hash, $offset); |
| 40 |
|
| 41 |
$this->upload_tick = time(); |
| 42 |
|
| 43 |
if ($this->current_file_size > 0) { |
| 44 |
$percent = round(100*($offset/$this->current_file_size),1); |
| 45 |
$updraftplus->record_uploaded_chunk($percent, "$uploadid, $offset", $fullpath); |
| 46 |
} else { |
| 47 |
$updraftplus->log("Dropbox: Chunked Upload: $offset bytes uploaded"); |
| 48 |
// This act is done by record_uploaded_chunk, and helps prevent overlapping runs |
| 49 |
touch($fullpath); |
| 50 |
} |
| 51 |
} |
| 52 |
|
| 53 |
public function get_credentials() { |
| 54 |
return array('updraft_dropbox'); |
| 55 |
} |
| 56 |
|
| 57 |
public function get_opts() { |
| 58 |
global $updraftplus; |
| 59 |
$opts = $updraftplus->get_job_option('updraft_dropbox'); |
| 60 |
if (!is_array($opts)) $opts = array(); |
| 61 |
if (!isset($opts['folder'])) $opts['folder'] = ''; |
| 62 |
return $opts; |
| 63 |
} |
| 64 |
|
| 65 |
public function backup($backup_array) { |
| 66 |
|
| 67 |
global $updraftplus, $updraftplus_backup; |
| 68 |
$updraftplus->log("Dropbox: begin cloud upload"); |
| 69 |
|
| 70 |
if (!function_exists('mcrypt_encrypt')) { |
| 71 |
$updraftplus->log('The required mcrypt PHP module is not installed'); |
| 72 |
$updraftplus->log(sprintf(__('The required %s PHP module is not installed - ask your web hosting company to enable it', 'updraftplus'), 'mcrypt'), 'error'); |
| 73 |
return false; |
| 74 |
} |
| 75 |
|
| 76 |
$opts = $this->get_opts(); |
| 77 |
|
| 78 |
if (empty($opts['tk_request_token'])) { |
| 79 |
$updraftplus->log('You do not appear to be authenticated with Dropbox'); |
| 80 |
$updraftplus->log(__('You do not appear to be authenticated with Dropbox','updraftplus'), 'error'); |
| 81 |
return false; |
| 82 |
} |
| 83 |
|
| 84 |
try { |
| 85 |
$dropbox = $this->bootstrap(); |
| 86 |
if (false === $dropbox) throw new Exception(__('You do not appear to be authenticated with Dropbox', 'updraftplus')); |
| 87 |
$updraftplus->log("Dropbox: access gained"); |
| 88 |
$dropbox->setChunkSize(1048576); |
| 89 |
} catch (Exception $e) { |
| 90 |
$updraftplus->log('Dropbox error when trying to gain access: '.$e->getMessage().' (line: '.$e->getLine().', file: '.$e->getFile().')'); |
| 91 |
$updraftplus->log(sprintf(__('Dropbox error: %s (see log file for more)','updraftplus'), $e->getMessage()), 'error'); |
| 92 |
return false; |
| 93 |
} |
| 94 |
|
| 95 |
$updraft_dir = $updraftplus->backups_dir_location(); |
| 96 |
$dropbox_folder = trailingslashit($opts['folder']); |
| 97 |
|
| 98 |
foreach ($backup_array as $file) { |
| 99 |
|
| 100 |
$available_quota = -1; |
| 101 |
|
| 102 |
// If we experience any failures collecting account info, then carry on anyway |
| 103 |
try { |
| 104 |
|
| 105 |
$accountInfo = $dropbox->accountInfo(); |
| 106 |
|
| 107 |
if ($accountInfo['code'] != "200") { |
| 108 |
$message = "Dropbox account/info did not return HTTP 200; returned: ". $accountInfo['code']; |
| 109 |
} elseif (!isset($accountInfo['body'])) { |
| 110 |
$message = "Dropbox account/info did not return the expected data"; |
| 111 |
} else { |
| 112 |
$body = $accountInfo['body']; |
| 113 |
if (!isset($body->quota_info)) { |
| 114 |
$message = "Dropbox account/info did not return the expected data"; |
| 115 |
} else { |
| 116 |
$quota_info = $body->quota_info; |
| 117 |
$total_quota = $quota_info->quota; |
| 118 |
$normal_quota = $quota_info->normal; |
| 119 |
$shared_quota = $quota_info->shared; |
| 120 |
$available_quota = $total_quota - ($normal_quota + $shared_quota); |
| 121 |
$message = "Dropbox 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"; |
| 122 |
} |
| 123 |
} |
| 124 |
$updraftplus->log($message); |
| 125 |
} catch (Exception $e) { |
| 126 |
$updraftplus->log("Dropbox error: exception (".get_class($e).") occurred whilst getting account info: ".$e->getMessage()); |
| 127 |
$updraftplus->log(sprintf(__("%s error: %s", 'updraftplus'), 'Dropbox', $e->getMessage()).' ('.$e->getCode().')', 'warning', md5($e->getMessage())); |
| 128 |
} |
| 129 |
|
| 130 |
$file_success = 1; |
| 131 |
|
| 132 |
$hash = md5($file); |
| 133 |
$this->current_file_hash = $hash; |
| 134 |
|
| 135 |
$filesize = filesize($updraft_dir.'/'.$file); |
| 136 |
$this->current_file_size = $filesize; |
| 137 |
|
| 138 |
// Into Kb |
| 139 |
$filesize = $filesize/1024; |
| 140 |
$microtime = microtime(true); |
| 141 |
|
| 142 |
if ($upload_id = $updraftplus->jobdata_get('updraf_dbid_'.$hash)) { |
| 143 |
# Resume |
| 144 |
$offset = $updraftplus->jobdata_get('updraf_dbof_'.$hash); |
| 145 |
$updraftplus->log("This is a resumption: $offset bytes had already been uploaded"); |
| 146 |
} else { |
| 147 |
$offset = 0; |
| 148 |
$upload_id = null; |
| 149 |
} |
| 150 |
|
| 151 |
// We don't actually abort now - there's no harm in letting it try and then fail |
| 152 |
if ($available_quota != -1 && $available_quota < ($filesize-$offset)) { |
| 153 |
$updraftplus->log("File upload expected to fail: file data remaining to upload ($file) size is ".($filesize-$offset)." b (overall file size; $filesize b), whereas available quota is only $available_quota b"); |
| 154 |
$updraftplus->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), 'error'); |
| 155 |
} |
| 156 |
|
| 157 |
// Old-style, single file put: $put = $dropbox->putFile($updraft_dir.'/'.$file, $dropbox_folder.$file); |
| 158 |
|
| 159 |
$ufile = apply_filters('updraftplus_dropbox_modpath', $file); |
| 160 |
|
| 161 |
$updraftplus->log("Dropbox: Attempt to upload: $file to: $ufile"); |
| 162 |
|
| 163 |
$this->upload_tick = time(); |
| 164 |
|
| 165 |
try { |
| 166 |
$response = $dropbox->chunkedUpload($updraft_dir.'/'.$file, '', $ufile, true, $offset, $upload_id, array($this, 'chunked_callback')); |
| 167 |
if (empty($response['code']) || "200" != $response['code']) { |
| 168 |
$updraftplus->log('Unexpected HTTP code returned from Dropbox: '.$response['code']." (".serialize($response).")"); |
| 169 |
if ($response['code'] >= 400) { |
| 170 |
$updraftplus->log('Dropbox '.sprintf(__('error: failed to upload file to %s (see log file for more)','updraftplus'), $file), 'error'); |
| 171 |
} else { |
| 172 |
$updraftplus->log(sprintf(__('%s did not return the expected response - check your log file for more details', 'updraftplus'), 'Dropbox'), 'warning'); |
| 173 |
} |
| 174 |
} |
| 175 |
} catch (Exception $e) { |
| 176 |
$updraftplus->log("Dropbox chunked upload exception (".get_class($e)."): ".$e->getMessage().' (line: '.$e->getLine().', file: '.$e->getFile().')'); |
| 177 |
if (preg_match("/Submitted input out of alignment: got \[(\d+)\] expected \[(\d+)\]/i", $e->getMessage(), $matches)) { |
| 178 |
// Try the indicated offset |
| 179 |
$we_tried = $matches[1]; |
| 180 |
$dropbox_wanted = $matches[2]; |
| 181 |
$updraftplus->log("Dropbox not yet aligned: tried=$we_tried, wanted=$dropbox_wanted; will attempt recovery"); |
| 182 |
try { |
| 183 |
$dropbox->chunkedUpload($updraft_dir.'/'.$file, '', $ufile, true, $dropbox_wanted, $upload_id, array($this, 'chunked_callback')); |
| 184 |
} catch (Exception $e) { |
| 185 |
$msg = $e->getMessage(); |
| 186 |
$updraftplus->log('Dropbox error: '.$msg.' (line: '.$e->getLine().', file: '.$e->getFile().')'); |
| 187 |
$updraftplus->log('Dropbox '.sprintf(__('error: failed to upload file to %s (see log file for more)','updraftplus'), $ufile), 'error'); |
| 188 |
$file_success = 0; |
| 189 |
if (strpos($msg, 'select/poll returned error') !== false && $this->upload_tick > 0 && time() - $this->upload_tick > 800) { |
| 190 |
$updraftplus->reschedule(60); |
| 191 |
$updraftplus->log("Select/poll returned after a long time: scheduling a resumption and terminating for now"); |
| 192 |
$updraftplus->record_still_alive(); |
| 193 |
die; |
| 194 |
} |
| 195 |
} |
| 196 |
} else { |
| 197 |
$msg = $e->getMessage(); |
| 198 |
$updraftplus->log('Dropbox error: '.$msg); |
| 199 |
$updraftplus->log('Dropbox '.sprintf(__('error: failed to upload file to %s (see log file for more)','updraftplus'), $ufile), 'error'); |
| 200 |
$file_success = 0; |
| 201 |
if (strpos($msg, 'select/poll returned error') !== false && $this->upload_tick > 0 && time() - $this->upload_tick > 800) { |
| 202 |
$updraftplus->reschedule(60); |
| 203 |
$updraftplus->log("Select/poll returned after a long time: scheduling a resumption and terminating for now"); |
| 204 |
$updraftplus->record_still_alive(); |
| 205 |
die; |
| 206 |
} |
| 207 |
} |
| 208 |
} |
| 209 |
if ($file_success) { |
| 210 |
$updraftplus->uploaded_file($file); |
| 211 |
$microtime_elapsed = microtime(true)-$microtime; |
| 212 |
$speedps = $filesize/$microtime_elapsed; |
| 213 |
$speed = sprintf("%.2d",$filesize)." Kb in ".sprintf("%.2d",$microtime_elapsed)."s (".sprintf("%.2d", $speedps)." Kb/s)"; |
| 214 |
$updraftplus->log("Dropbox: File upload success (".$file."): $speed"); |
| 215 |
$updraftplus->jobdata_delete('updraft_duido_'.$hash); |
| 216 |
$updraftplus->jobdata_delete('updraft_duidi_'.$hash); |
| 217 |
} |
| 218 |
|
| 219 |
} |
| 220 |
|
| 221 |
return null; |
| 222 |
|
| 223 |
} |
| 224 |
|
| 225 |
# $match: a substring to require (tested via strpos() !== false) |
| 226 |
public function listfiles($match = 'backup_') { |
| 227 |
|
| 228 |
$opts = $this->get_opts(); |
| 229 |
|
| 230 |
if (empty($opts['tk_access_token'])) return new WP_Error('no_settings', __('No settings were found', 'updraftplus')); |
| 231 |
|
| 232 |
global $updraftplus; |
| 233 |
try { |
| 234 |
$dropbox = $this->bootstrap(); |
| 235 |
} catch (Exception $e) { |
| 236 |
$updraftplus->log('Dropbox access error: '.$e->getMessage().' (line: '.$e->getLine().', file: '.$e->getFile().')'); |
| 237 |
return new WP_Error('access_error', $e->getMessage()); |
| 238 |
} |
| 239 |
|
| 240 |
$searchpath = '/'.untrailingslashit(apply_filters('updraftplus_dropbox_modpath', '')); |
| 241 |
|
| 242 |
try { |
| 243 |
$search = $dropbox->search($match, $searchpath); |
| 244 |
} catch (Exception $e) { |
| 245 |
$updraftplus->log('Dropbox error: '.$e->getMessage().' (line: '.$e->getLine().', file: '.$e->getFile().')'); |
| 246 |
return new WP_Error('search_error', $e->getMessage()); |
| 247 |
} |
| 248 |
|
| 249 |
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']); |
| 250 |
|
| 251 |
if (empty($search['body']) || !is_array($search['body'])) return array(); |
| 252 |
|
| 253 |
$results = array(); |
| 254 |
|
| 255 |
foreach ($search['body'] as $item) { |
| 256 |
if (!is_object($item)) continue; |
| 257 |
|
| 258 |
if ((!isset($item->bytes) || $item->bytes > 0) && empty($item->is_dir) && !empty($item->path) && 0 === strpos($item->path, $searchpath)) { |
| 259 |
|
| 260 |
$path = substr($item->path, strlen($searchpath)); |
| 261 |
if ('/' == substr($path, 0, 1)) $path=substr($path, 1); |
| 262 |
|
| 263 |
# Ones in subfolders are not wanted |
| 264 |
if (false !== strpos($path, '/')) continue; |
| 265 |
|
| 266 |
$result = array('name' => $path); |
| 267 |
if (!empty($item->bytes)) $result['size'] = $item->bytes; |
| 268 |
|
| 269 |
$results[] = $result; |
| 270 |
|
| 271 |
} |
| 272 |
|
| 273 |
} |
| 274 |
|
| 275 |
return $results; |
| 276 |
} |
| 277 |
|
| 278 |
public function defaults() { |
| 279 |
return apply_filters('updraftplus_dropbox_defaults', array('Z3Q3ZmkwbnplNHA0Zzlx', 'bTY0bm9iNmY4eWhjODRt')); |
| 280 |
} |
| 281 |
|
| 282 |
public function delete($files) { |
| 283 |
|
| 284 |
global $updraftplus; |
| 285 |
if (is_string($files)) $files=array($files); |
| 286 |
|
| 287 |
$opts = $this->get_opts(); |
| 288 |
|
| 289 |
if (empty($opts['tk_request_token'])) { |
| 290 |
$updraftplus->log('You do not appear to be authenticated with Dropbox'); |
| 291 |
$updraftplus->log(sprintf(__('You do not appear to be authenticated with %s (whilst deleting)', 'updraftplus'), 'Dropbox'), 'warning'); |
| 292 |
return false; |
| 293 |
} |
| 294 |
|
| 295 |
try { |
| 296 |
$dropbox = $this->bootstrap(); |
| 297 |
} catch (Exception $e) { |
| 298 |
$updraftplus->log('Dropbox error: '.$e->getMessage().' (line: '.$e->getLine().', file: '.$e->getFile().')'); |
| 299 |
$updraftplus->log(sprintf(__('Failed to access %s when deleting (see log file for more)', 'updraftplus'), 'Dropbox'), 'warning'); |
| 300 |
return false; |
| 301 |
} |
| 302 |
if (false === $dropbox) return false; |
| 303 |
|
| 304 |
foreach ($files as $file) { |
| 305 |
$ufile = apply_filters('updraftplus_dropbox_modpath', $file); |
| 306 |
$updraftplus->log("Dropbox: request deletion: $ufile"); |
| 307 |
|
| 308 |
try { |
| 309 |
$dropbox->delete($ufile); |
| 310 |
$file_success = 1; |
| 311 |
} catch (Exception $e) { |
| 312 |
$updraftplus->log('Dropbox error: '.$e->getMessage().' (line: '.$e->getLine().', file: '.$e->getFile().')'); |
| 313 |
} |
| 314 |
|
| 315 |
if (isset($file_success)) { |
| 316 |
$updraftplus->log('Dropbox: delete succeeded'); |
| 317 |
} else { |
| 318 |
return false; |
| 319 |
} |
| 320 |
} |
| 321 |
|
| 322 |
} |
| 323 |
|
| 324 |
public function download($file) { |
| 325 |
|
| 326 |
global $updraftplus; |
| 327 |
|
| 328 |
$opts = $this->get_opts(); |
| 329 |
|
| 330 |
if (empty($opts['tk_request_token'])) { |
| 331 |
$updraftplus->log('You do not appear to be authenticated with Dropbox'); |
| 332 |
$updraftplus->log(sprintf(__('You do not appear to be authenticated with %s','updraftplus'), 'Dropbox'), 'error'); |
| 333 |
return false; |
| 334 |
} |
| 335 |
|
| 336 |
try { |
| 337 |
$dropbox = $this->bootstrap(); |
| 338 |
} catch (Exception $e) { |
| 339 |
$updraftplus->log('Dropbox error: '.$e->getMessage().' (line: '.$e->getLine().', file: '.$e->getFile().')'); |
| 340 |
$updraftplus->log('Dropbox error: '.$e->getMessage().' (line: '.$e->getLine().', file: '.$e->getFile().')', 'error'); |
| 341 |
return false; |
| 342 |
} |
| 343 |
if (false === $dropbox) return false; |
| 344 |
|
| 345 |
$updraft_dir = $updraftplus->backups_dir_location(); |
| 346 |
$microtime = microtime(true); |
| 347 |
|
| 348 |
$try_the_other_one = false; |
| 349 |
|
| 350 |
$ufile = apply_filters('updraftplus_dropbox_modpath', $file); |
| 351 |
|
| 352 |
try { |
| 353 |
$get = $dropbox->getFile($ufile, $updraft_dir.'/'.$file, null, true); |
| 354 |
} catch (Exception $e) { |
| 355 |
// TODO: Remove this October 2013 (we stored in the wrong place for a while...) |
| 356 |
$try_the_other_one = true; |
| 357 |
$possible_error = $e->getMessage(); |
| 358 |
$updraftplus->log('Dropbox error: '.$e); |
| 359 |
$get = false; |
| 360 |
} |
| 361 |
|
| 362 |
// TODO: Remove this October 2013 (we stored files in the wrong place for a while...) |
| 363 |
if ($try_the_other_one) { |
| 364 |
$dropbox_folder = trailingslashit($opts['folder']); |
| 365 |
try { |
| 366 |
$get = $dropbox->getFile($dropbox_folder.'/'.$file, $updraft_dir.'/'.$file, null, true); |
| 367 |
if (isset($get['response']['body'])) { |
| 368 |
$updraftplus->log("Dropbox: downloaded ".round(strlen($get['response']['body'])/1024,1).' Kb'); |
| 369 |
} |
| 370 |
} catch (Exception $e) { |
| 371 |
$updraftplus->log($possible_error, 'error'); |
| 372 |
$updraftplus->log($e->getMessage(), 'error'); |
| 373 |
$get = false; |
| 374 |
} |
| 375 |
} |
| 376 |
|
| 377 |
return $get; |
| 378 |
|
| 379 |
} |
| 380 |
|
| 381 |
public function config_print() { |
| 382 |
$opts = $this->get_opts(); |
| 383 |
?> |
| 384 |
<tr class="updraftplusmethod dropbox"> |
| 385 |
<td></td> |
| 386 |
<td> |
| 387 |
<img alt="<?php _e(sprintf(__('%s logo', 'updraftplus'), 'Dropbox')); ?>" src="<?php echo UPDRAFTPLUS_URL.'/images/dropbox-logo.png' ?>"> |
| 388 |
<p><em><?php printf(__('%s is a great choice, because UpdraftPlus supports chunked uploads - no matter how big your site is, UpdraftPlus can upload it a little at a time, and not get thwarted by timeouts.','updraftplus'),'Dropbox');?></em></p> |
| 389 |
</td> |
| 390 |
</tr> |
| 391 |
|
| 392 |
<tr class="updraftplusmethod dropbox"> |
| 393 |
<th></th> |
| 394 |
<td> |
| 395 |
<?php |
| 396 |
// Check requirements. |
| 397 |
global $updraftplus_admin; |
| 398 |
if (!function_exists('mcrypt_encrypt')) { |
| 399 |
$updraftplus_admin->show_double_warning('<strong>'.__('Warning','updraftplus').':</strong> '. sprintf(__('Your web server\'s PHP installation does not included a required module (%s). Please contact your web hosting provider\'s support and ask for them to enable it.', 'updraftplus'), 'mcrypt')); |
| 400 |
/* |
| 401 |
.' '.sprintf(__("UpdraftPlus's %s module <strong>requires</strong> %s. Please do not file any support requests; there is no alternative.",'updraftplus'),'Dropbox', 'mcrypt'), 'dropbox') |
| 402 |
*/ |
| 403 |
} |
| 404 |
$updraftplus_admin->curl_check('Dropbox', false, 'dropbox'); |
| 405 |
?> |
| 406 |
</td> |
| 407 |
</tr> |
| 408 |
|
| 409 |
<?php |
| 410 |
|
| 411 |
$defmsg = '<tr class="updraftplusmethod dropbox"><td></td><td><strong>'.__('Need to use sub-folders?','updraftplus').'</strong> '.__('Backups are saved in','updraftplus').' apps/UpdraftPlus. '.__('If you back up several sites into the same Dropbox and want to organise with sub-folders, then ','updraftplus').'<a href="http://updraftplus.com/shop/">'.__("there's an add-on for that.",'updraftplus').'</a></td></tr>'; |
| 412 |
|
| 413 |
echo apply_filters('updraftplus_dropbox_extra_config', $defmsg); ?> |
| 414 |
|
| 415 |
<tr class="updraftplusmethod dropbox"> |
| 416 |
<th><?php echo sprintf(__('Authenticate with %s', 'updraftplus'), __('Dropbox', 'updraftplus'));?>:</th> |
| 417 |
<td><p><?php $rt = (empty($opts['tk_request_token'])) ? '' : $opts['tk_request_token']; if (!empty($rt)) echo "<strong>".__('(You appear to be already authenticated).','updraftplus')."</strong>"; ?> <a href="?page=updraftplus&action=updraftmethod-dropbox-auth&updraftplus_dropboxauth=doit"><?php echo sprintf(__('<strong>After</strong> you have saved your settings (by clicking \'Save Changes\' below), then come back here once and click this link to complete authentication with %s.','updraftplus'), __('Dropbox', 'updraftplus'));?></a> |
| 418 |
</p> |
| 419 |
<?php |
| 420 |
if (!empty($rt)) { |
| 421 |
$ownername = empty($opts['ownername']) ? '' : $opts['ownername']; |
| 422 |
if (!empty($ownername)) { |
| 423 |
echo '<br>'.sprintf(__("Account holder's name: %s.", 'updraftplus'), htmlspecialchars($opts['ownername'])).' '; |
| 424 |
} |
| 425 |
} |
| 426 |
?> |
| 427 |
</td> |
| 428 |
</tr> |
| 429 |
|
| 430 |
<?php |
| 431 |
// Legacy: only show this next setting to old users who had a setting stored |
| 432 |
if (!empty($opts['appkey'])) { |
| 433 |
?> |
| 434 |
|
| 435 |
<tr class="updraftplusmethod dropbox"> |
| 436 |
<th>Your Dropbox App Key:</th> |
| 437 |
<td><input type="text" autocomplete="off" style="width:332px" id="updraft_dropbox_appkey" name="updraft_dropbox[appkey]" value="<?php echo htmlspecialchars($opts['appkey']) ?>" /></td> |
| 438 |
</tr> |
| 439 |
<tr class="updraftplusmethod dropbox"> |
| 440 |
<th>Your Dropbox App Secret:</th> |
| 441 |
<td><input type="text" style="width:332px" id="updraft_dropbox_secret" name="updraft_dropbox[secret]" value="<?php echo htmlspecialchars($opts['secret']); ?>" /></td> |
| 442 |
</tr> |
| 443 |
|
| 444 |
<?php } ?> |
| 445 |
|
| 446 |
<?php |
| 447 |
} |
| 448 |
|
| 449 |
public function action_auth() { |
| 450 |
if ( isset( $_GET['oauth_token'] ) ) { |
| 451 |
$this->auth_token(); |
| 452 |
} elseif (isset($_GET['updraftplus_dropboxauth'])) { |
| 453 |
// Clear out the existing credentials |
| 454 |
if ('doit' == $_GET['updraftplus_dropboxauth']) { |
| 455 |
$opts = $this->get_opts(); |
| 456 |
$opts['tk_request_token'] = ''; |
| 457 |
$opts['tk_access_token'] = ''; |
| 458 |
$opts['ownername'] = ''; |
| 459 |
UpdraftPlus_Options::update_updraft_option('updraft_dropbox', $opts); |
| 460 |
} |
| 461 |
try { |
| 462 |
$this->auth_request(); |
| 463 |
} catch (Exception $e) { |
| 464 |
global $updraftplus; |
| 465 |
$updraftplus->log(sprintf(__("%s error: %s", 'updraftplus'), sprintf(__("%s authentication", 'updraftplus'), 'Dropbox'), $e->getMessage()), 'error'); |
| 466 |
} |
| 467 |
} |
| 468 |
} |
| 469 |
|
| 470 |
public function show_authed_admin_warning() { |
| 471 |
global $updraftplus_admin, $updraftplus; |
| 472 |
|
| 473 |
$dropbox = $this->bootstrap(); |
| 474 |
if (false === $dropbox) return false; |
| 475 |
|
| 476 |
try { |
| 477 |
$accountInfo = $dropbox->accountInfo(); |
| 478 |
} catch (Exception $e) { |
| 479 |
$accountinfo_err = sprintf(__("%s error: %s", 'updraftplus'), 'Dropbox', $e->getMessage()).' ('.$e->getCode().')'; |
| 480 |
} |
| 481 |
|
| 482 |
$message = "<strong>".__('Success:','updraftplus').'</strong> '.sprintf(__('you have authenticated your %s account','updraftplus'),'Dropbox'); |
| 483 |
# We log, because otherwise people get confused by the most recent log message of 'Parameter not found: oauth_token' and raise support requests |
| 484 |
$updraftplus->log(__('Success:','updraftplus').' '.sprintf(__('you have authenticated your %s account','updraftplus'),'Dropbox')); |
| 485 |
|
| 486 |
if (empty($accountInfo['code']) || "200" != $accountInfo['code']) { |
| 487 |
$message .= " (".__('though part of the returned information was not as expected - your mileage may vary','updraftplus').")". $accountInfo['code']; |
| 488 |
if (!empty($accountinfo_err)) $message .= "<br>".htmlspecialchars($accountinfo_err); |
| 489 |
} else { |
| 490 |
$body = $accountInfo['body']; |
| 491 |
$message .= ". <br>".sprintf(__('Your %s account name: %s','updraftplus'),'Dropbox', htmlspecialchars($body->display_name)); |
| 492 |
$opts = $this->get_opts(); |
| 493 |
$opts['ownername'] = ''; |
| 494 |
if (!empty($body->display_name)) $opts['ownername'] = $body->display_name; |
| 495 |
UpdraftPlus_Options::update_updraft_option('updraft_dropbox', $opts); |
| 496 |
|
| 497 |
try { |
| 498 |
$quota_info = $body->quota_info; |
| 499 |
$total_quota = max($quota_info->quota, 1); |
| 500 |
$normal_quota = $quota_info->normal; |
| 501 |
$shared_quota = $quota_info->shared; |
| 502 |
$available_quota =$total_quota - ($normal_quota + $shared_quota); |
| 503 |
$used_perc = round(($normal_quota + $shared_quota)*100/$total_quota, 1); |
| 504 |
$message .= ' <br>'.sprintf(__('Your %s quota usage: %s %% used, %s available','updraftplus'), 'Dropbox', $used_perc, round($available_quota/1048576, 1).' Mb'); |
| 505 |
} catch (Exception $e) { |
| 506 |
} |
| 507 |
|
| 508 |
} |
| 509 |
$updraftplus_admin->show_admin_warning($message); |
| 510 |
|
| 511 |
} |
| 512 |
|
| 513 |
public function auth_token() { |
| 514 |
// $opts = $this->get_opts(); |
| 515 |
// $previous_token = empty($opts['tk_request_token']) ? '' : $opts['tk_request_token']; |
| 516 |
$this->bootstrap(); |
| 517 |
$opts = $this->get_opts(); |
| 518 |
$new_token = empty($opts['tk_request_token']) ? '' : $opts['tk_request_token']; |
| 519 |
if ($new_token) { |
| 520 |
add_action('all_admin_notices', array($this, 'show_authed_admin_warning') ); |
| 521 |
} |
| 522 |
} |
| 523 |
|
| 524 |
// Acquire single-use authorization code |
| 525 |
public function auth_request() { |
| 526 |
$this->bootstrap(); |
| 527 |
} |
| 528 |
|
| 529 |
// This basically reproduces the relevant bits of bootstrap.php from the SDK |
| 530 |
public function bootstrap() { |
| 531 |
|
| 532 |
if (!empty($this->dropbox_object) && !is_wp_error($this->dropbox_object)) return $this->dropbox_object; |
| 533 |
|
| 534 |
require_once(UPDRAFTPLUS_DIR.'/includes/Dropbox/API.php'); |
| 535 |
require_once(UPDRAFTPLUS_DIR.'/includes/Dropbox/Exception.php'); |
| 536 |
require_once(UPDRAFTPLUS_DIR.'/includes/Dropbox/OAuth/Consumer/ConsumerAbstract.php'); |
| 537 |
require_once(UPDRAFTPLUS_DIR.'/includes/Dropbox/OAuth/Storage/StorageInterface.php'); |
| 538 |
require_once(UPDRAFTPLUS_DIR.'/includes/Dropbox/OAuth/Storage/Encrypter.php'); |
| 539 |
require_once(UPDRAFTPLUS_DIR.'/includes/Dropbox/OAuth/Storage/WordPress.php'); |
| 540 |
require_once(UPDRAFTPLUS_DIR.'/includes/Dropbox/OAuth/Consumer/Curl.php'); |
| 541 |
// require_once(UPDRAFTPLUS_DIR.'/includes/Dropbox/OAuth/Consumer/WordPress.php'); |
| 542 |
|
| 543 |
$opts = $this->get_opts(); |
| 544 |
|
| 545 |
$key = (empty($opts['secret'])) ? '' : $opts['secret']; |
| 546 |
$sec = (empty($opts['appkey'])) ? '' : $opts['appkey']; |
| 547 |
|
| 548 |
// Set the callback URL |
| 549 |
$callback = UpdraftPlus_Options::admin_page_url().'?page=updraftplus&action=updraftmethod-dropbox-auth'; |
| 550 |
|
| 551 |
// Instantiate the Encrypter and storage objects |
| 552 |
$encrypter = new Dropbox_Encrypter('ThisOneDoesNotMatterBeyondLength'); |
| 553 |
|
| 554 |
// Instantiate the storage |
| 555 |
$storage = new Dropbox_WordPress($encrypter, "tk_", 'updraft_dropbox'); |
| 556 |
|
| 557 |
// WordPress consumer does not yet work |
| 558 |
// $OAuth = new Dropbox_ConsumerWordPress($sec, $key, $storage, $callback); |
| 559 |
|
| 560 |
// Get the DropBox API access details |
| 561 |
list($d2, $d1) = $this->defaults(); |
| 562 |
if (empty($sec)) { $sec = base64_decode($d1); }; if (empty($key)) { $key = base64_decode($d2); } |
| 563 |
$root = 'sandbox'; |
| 564 |
if ('dropbox:' == substr($sec, 0, 8)) { |
| 565 |
$sec = substr($sec, 8); |
| 566 |
$root = 'dropbox'; |
| 567 |
} |
| 568 |
|
| 569 |
try { |
| 570 |
$OAuth = new Dropbox_Curl($sec, $key, $storage, $callback); |
| 571 |
} catch (Exception $e) { |
| 572 |
global $updraftplus; |
| 573 |
$updraftplus->log("Dropbox Curl error: ".$e->getMessage()); |
| 574 |
$updraftplus->log(sprintf(__("%s error: %s", 'updraftplus'), "Dropbox/Curl", $e->getMessage()), 'error'); |
| 575 |
return false; |
| 576 |
} |
| 577 |
|
| 578 |
$this->dropbox_object = new UpdraftPlus_Dropbox_API($OAuth, $root); |
| 579 |
|
| 580 |
return $this->dropbox_object; |
| 581 |
} |
| 582 |
|
| 583 |
} |
| 584 |
|