| 1 |
<?php |
| 2 |
|
| 3 |
// https://www.dropbox.com/developers/apply?cont=/developers/apps |
| 4 |
|
| 5 |
class UpdraftPlus_BackupModule_dropbox { |
| 6 |
|
| 7 |
private $current_file_hash; |
| 8 |
private $current_file_size; |
| 9 |
|
| 10 |
function chunked_callback($offset, $uploadid) { |
| 11 |
global $updraftplus; |
| 12 |
|
| 13 |
// Update upload ID |
| 14 |
set_transient('updraf_dbid_'.$this->current_file_hash, $uploadid, 3600*3); |
| 15 |
set_transient('updraf_dbof_'.$this->current_file_hash, $offset, 3600*3); |
| 16 |
|
| 17 |
if ($this->current_file_size > 0) { |
| 18 |
$percent = round(100*($offset/$this->current_file_size),1); |
| 19 |
$updraftplus->log("Dropbox: Chunked Upload: ${percent}% ($uploadid, $offset)"); |
| 20 |
} else { |
| 21 |
$updraftplus->log("Dropbox: Chunked Upload: $offset bytes uploaded"); |
| 22 |
} |
| 23 |
|
| 24 |
} |
| 25 |
|
| 26 |
function backup($backup_array) { |
| 27 |
|
| 28 |
global $updraftplus; |
| 29 |
$updraftplus->log("Dropbox: begin cloud upload"); |
| 30 |
|
| 31 |
if (UpdraftPlus_Options::get_updraft_option('updraft_dropboxtk_request_token', 'xyz') == 'xyz') { |
| 32 |
$updraftplus->log('You do not appear to be authenticated with Dropbox'); |
| 33 |
$updraftplus->error('You do not appear to be authenticated with Dropbox'); |
| 34 |
return false; |
| 35 |
} |
| 36 |
|
| 37 |
try { |
| 38 |
$dropbox = $this->bootstrap(); |
| 39 |
$dropbox->setChunkSize(524288); // 512Kb |
| 40 |
} catch (Exception $e) { |
| 41 |
$updraftplus->log('Dropbox error: '.$e->getMessage().' (line: '.$e->getLine().', file: '.$e->getFile().')'); |
| 42 |
$updraftplus->error('Dropbox error: '.$e->getMessage().' (see log file for more)'); |
| 43 |
return false; |
| 44 |
} |
| 45 |
|
| 46 |
$updraft_dir = $updraftplus->backups_dir_location(); |
| 47 |
$dropbox_folder = trailingslashit(UpdraftPlus_Options::get_updraft_option('updraft_dropbox_folder')); |
| 48 |
|
| 49 |
foreach($backup_array as $file) { |
| 50 |
$updraftplus->log("Dropbox: Attempt to upload: $file"); |
| 51 |
|
| 52 |
$file_success = 1; |
| 53 |
|
| 54 |
$hash = md5($file); |
| 55 |
$this->current_file_hash=$hash; |
| 56 |
|
| 57 |
$filesize = filesize($updraft_dir.'/'.$file); |
| 58 |
$this->current_file_size = $filesize; |
| 59 |
// Into Kb |
| 60 |
$filesize = $filesize/1024; |
| 61 |
$microtime = microtime(true); |
| 62 |
|
| 63 |
if ($upload_id = get_transient('updraf_dbid_'.$hash)) { |
| 64 |
# Resume |
| 65 |
$offset = get_transient('updraf_dbof_'.$hash); |
| 66 |
$updraftplus->log("This is a resumption: $offset bytes had already been uploaded"); |
| 67 |
} else { |
| 68 |
$offset = 0; |
| 69 |
$upload_id = null; |
| 70 |
} |
| 71 |
|
| 72 |
// I did erroneously have $dropbox_folder as the third parameter in chunkedUpload... this causes a sub-directory to be created |
| 73 |
// Old-style, single file put: $put = $dropbox->putFile($updraft_dir.'/'.$file, $dropbox_folder.$file); |
| 74 |
|
| 75 |
$ourself = $this; |
| 76 |
|
| 77 |
try { |
| 78 |
$dropbox->chunkedUpload($updraft_dir.'/'.$file, $file, '', true, $offset, $upload_id, array($ourself, 'chunked_callback')); |
| 79 |
} catch (Exception $e) { |
| 80 |
$updraftplus->log("Exception: ".$e->getMessage()); |
| 81 |
if (preg_match("/Submitted input out of alignment: got \[(\d+)\] expected \[(\d+)\]/i", $e->getMessage(), $matches)) { |
| 82 |
// Try the indicated offset |
| 83 |
$we_tried = $matches[1]; |
| 84 |
$dropbox_wanted = $matches[2]; |
| 85 |
$updraftplus->log("Dropbox alignment error: tried=$we_tried, wanted=$dropbox_wanted; will attempt recovery"); |
| 86 |
try { |
| 87 |
$dropbox->chunkedUpload($updraft_dir.'/'.$file, $file, '', true, $dropbox_wanted, $upload_id, array($ourself, 'chunked_callback')); |
| 88 |
} catch (Exception $e) { |
| 89 |
$updraftplus->log('Dropbox error: '.$e->getMessage().' (line: '.$e->getLine().', file: '.$e->getFile().')'); |
| 90 |
$updraftplus->error("Dropbox error: failed to upload file $file (see full log for more)"); |
| 91 |
$file_success = 0; |
| 92 |
} |
| 93 |
} else { |
| 94 |
$updraftplus->log('Dropbox error: '.$e->getMessage()); |
| 95 |
$updraftplus->error("Dropbox error: failed to upload file $file (see full log for more)"); |
| 96 |
$file_success = 0; |
| 97 |
} |
| 98 |
} |
| 99 |
if ($file_success) { |
| 100 |
$updraftplus->uploaded_file($file); |
| 101 |
$microtime_elapsed = microtime(true)-$microtime; |
| 102 |
$speedps = $filesize/$microtime_elapsed; |
| 103 |
$speed = sprintf("%.2d",$filesize)." Kb in ".sprintf("%.2d",$microtime_elapsed)."s (".sprintf("%.2d", $speedps)." Kb/s)"; |
| 104 |
$updraftplus->log("Dropbox: File upload success (".$file."): $speed"); |
| 105 |
delete_transient('updraft_duido_'.$hash); |
| 106 |
delete_transient('updraft_duidi_'.$hash); |
| 107 |
} |
| 108 |
|
| 109 |
} |
| 110 |
|
| 111 |
$updraftplus->prune_retained_backups('dropbox', $this, null); |
| 112 |
|
| 113 |
} |
| 114 |
|
| 115 |
function defaults() { |
| 116 |
return array('Z3Q3ZmkwbnplNHA0Zzlx', 'bTY0bm9iNmY4eWhjODRt'); |
| 117 |
} |
| 118 |
|
| 119 |
function delete($file) { |
| 120 |
|
| 121 |
global $updraftplus; |
| 122 |
$updraftplus->log("Dropbox: request deletion: $file"); |
| 123 |
|
| 124 |
if (UpdraftPlus_Options::get_updraft_option('updraft_dropboxtk_request_token', 'xyz') == 'xyz') { |
| 125 |
$updraftplus->log('You do not appear to be authenticated with Dropbox'); |
| 126 |
//$updraftplus->error('You do not appear to be authenticated with Dropbox'); |
| 127 |
return false; |
| 128 |
} |
| 129 |
|
| 130 |
try { |
| 131 |
$dropbox = $this->bootstrap(); |
| 132 |
} catch (Exception $e) { |
| 133 |
$updraftplus->log('Dropbox error: '.$e->getMessage().' (line: '.$e->getLine().', file: '.$e->getFile().')'); |
| 134 |
//$updraftplus->error('Dropbox error: failed to access Dropbox (see log file for more)'); |
| 135 |
return false; |
| 136 |
} |
| 137 |
|
| 138 |
$dropbox_folder = trailingslashit(UpdraftPlus_Options::get_updraft_option('updraft_dropbox_folder')); |
| 139 |
|
| 140 |
$file_success = 1; |
| 141 |
try { |
| 142 |
// Apparently $dropbox_folder is not needed |
| 143 |
$dropbox->delete($file); |
| 144 |
} catch (Exception $e) { |
| 145 |
$updraftplus->log('Dropbox error: '.$e->getMessage().' (line: '.$e->getLine().', file: '.$e->getFile().')'); |
| 146 |
// TODO |
| 147 |
// Add this back October 2013 when removing the block below |
| 148 |
//$updraftplus->error("Dropbox error: failed to delete file ($file): see log file for more info"); |
| 149 |
$file_success = 0; |
| 150 |
} |
| 151 |
if ($file_success) { |
| 152 |
$updraftplus->log('Dropbox: delete succeeded'); |
| 153 |
} else { |
| 154 |
$file_success = 1; |
| 155 |
// We created the file in the wrong place for a while. This code is needed until October 2013, when it can be removed. |
| 156 |
try { |
| 157 |
$dropbox->delete($dropbox_folder.'/'.$file); |
| 158 |
} catch (Exception $e) { |
| 159 |
$updraftplus->log('Dropbox error: '.$e->getMessage().' (line: '.$e->getLine().', file: '.$e->getFile().')'); |
| 160 |
$file_success = 0; |
| 161 |
} |
| 162 |
if ($file_success) $updraftplus->log('Dropbox: delete succeeded (alternative path)'); |
| 163 |
} |
| 164 |
|
| 165 |
} |
| 166 |
|
| 167 |
function download($file) { |
| 168 |
|
| 169 |
global $updraftplus; |
| 170 |
|
| 171 |
if (UpdraftPlus_Options::get_updraft_option('updraft_dropboxtk_request_token', 'xyz') == 'xyz') { |
| 172 |
$updraftplus->error('You do not appear to be authenticated with Dropbox'); |
| 173 |
return false; |
| 174 |
} |
| 175 |
|
| 176 |
try { |
| 177 |
$dropbox = $this->bootstrap(); |
| 178 |
} catch (Exception $e) { |
| 179 |
$updraftplus->log('Dropbox error: '.$e->getMessage().' (line: '.$e->getLine().', file: '.$e->getFile().')'); |
| 180 |
return false; |
| 181 |
} |
| 182 |
|
| 183 |
$updraft_dir = $updraftplus->backups_dir_location(); |
| 184 |
$microtime = microtime(true); |
| 185 |
|
| 186 |
$try_the_other_one = false; |
| 187 |
try { |
| 188 |
$get = $dropbox->getFile($file, $updraft_dir.'/'.$file); |
| 189 |
} catch (Exception $e) { |
| 190 |
// TODO: Remove this October 2013 (we stored in the wrong place for a while...) |
| 191 |
$try_the_other_one = true; |
| 192 |
$possible_error = $e->getMessage(); |
| 193 |
} |
| 194 |
|
| 195 |
// TODO: Remove this October 2013 (we stored in the wrong place for a while...) |
| 196 |
if ($try_the_other_one) { |
| 197 |
$dropbox_folder = trailingslashit(UpdraftPlus_Options::get_updraft_option('updraft_dropbox_folder')); |
| 198 |
$updraftplus->error('Dropbox error: '.$e); |
| 199 |
try { |
| 200 |
$get = $dropbox->getFile($file, $updraft_dir.'/'.$file); |
| 201 |
} catch (Exception $e) { |
| 202 |
$updraftplus->error($possible_error); |
| 203 |
$updraftplus->error($e->getMessage()); |
| 204 |
} |
| 205 |
} |
| 206 |
|
| 207 |
} |
| 208 |
|
| 209 |
function config_print() { |
| 210 |
|
| 211 |
?> |
| 212 |
<tr class="updraftplusmethod dropbox"> |
| 213 |
<td></td> |
| 214 |
<td> |
| 215 |
<img alt="Dropbox logo" src="<?php echo UPDRAFTPLUS_URL.'/images/dropbox-logo.png' ?>"> |
| 216 |
<p><em>Dropbox is a great choice, because UpdraftPlus supports chunked uploads - no matter how big your blog is, UpdraftPlus can upload it a little at a time, and not get thwarted by timeouts.</em></p> |
| 217 |
</td> |
| 218 |
</tr> |
| 219 |
|
| 220 |
<tr class="updraftplusmethod dropbox"> |
| 221 |
<th>Authenticate with Dropbox:</th> |
| 222 |
<td><p><?php if (UpdraftPlus_Options::get_updraft_option('updraft_dropboxtk_request_token','xyz') != 'xyz') echo "<strong>(You appear to be already authenticated).</strong>"; ?> <a href="?page=updraftplus&action=updraftmethod-dropbox-auth&updraftplus_dropboxauth=doit"><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 Dropbox.</a> |
| 223 |
</p> |
| 224 |
</td> |
| 225 |
</tr> |
| 226 |
|
| 227 |
<tr class="updraftplusmethod dropbox"> |
| 228 |
<th></th> |
| 229 |
<td> |
| 230 |
<?php |
| 231 |
// Check requirements. |
| 232 |
if (!function_exists('mcrypt_encrypt')) { |
| 233 |
?><p><strong>Warning:</strong> Your web server's PHP installation does not included a required module (MCrypt). Please contact your web hosting provider's support. UpdraftPlus's Dropbox module <strong>requires</strong> MCrypt. Please do not file any support requests; there is no alternative.</p><?php |
| 234 |
} |
| 235 |
if (!function_exists("curl_init")) { |
| 236 |
?><p><strong>Warning:</strong> Your web server's PHP installation does not included a required module (Curl). Please contact your web hosting provider's support. UpdraftPlus's Dropbox module <strong>requires</strong> Curl. Your only options to get this working are 1) Install/enable curl or 2) Hire us or someone else to code additional support options into UpdraftPlus. 3) Wait, possibly forever, for someone else to do this.</p><?php |
| 237 |
} else { |
| 238 |
$curl_version = curl_version(); |
| 239 |
$curl_ssl_supported= ($curl_version['features'] & CURL_VERSION_SSL); |
| 240 |
if (!$curl_ssl_supported) { |
| 241 |
?><p><strong>Warning:</strong> Your web server's PHP/Curl installation does not support https access. We cannot access Dropbox without this support. Please contact your web hosting provider's support. UpdraftPlus's Dropbox module <strong>requires</strong> Curl+https. Your only options to get this working are 1) Install/enable curl with https or 2) Hire us or someone else to code additional support options into UpdraftPlus. 3) Wait, possibly forever, for someone else to do this.</p><?php |
| 242 |
} |
| 243 |
} |
| 244 |
?> |
| 245 |
</td> |
| 246 |
</tr> |
| 247 |
|
| 248 |
<?php |
| 249 |
// This setting should never have been used - it is legacy/deprecated |
| 250 |
?> |
| 251 |
<input type="hidden" name="updraft_dropbox_folder" value=""> |
| 252 |
|
| 253 |
<?php |
| 254 |
// Legacy: only show this next setting to old users who had a setting stored |
| 255 |
if (UpdraftPlus_Options::get_updraft_option('updraft_dropbox_appkey')) { |
| 256 |
?> |
| 257 |
|
| 258 |
<tr class="updraftplusmethod dropbox"> |
| 259 |
<th>Your Dropbox App Key:</th> |
| 260 |
<td><input type="text" autocomplete="off" style="width:332px" id="updraft_dropbox_appkey" name="updraft_dropbox_appkey" value="<?php echo htmlspecialchars(UpdraftPlus_Options::get_updraft_option('updraft_dropbox_appkey')) ?>" /></td> |
| 261 |
</tr> |
| 262 |
<tr class="updraftplusmethod dropbox"> |
| 263 |
<th>Your Dropbox App Secret:</th> |
| 264 |
<td><input type="text" style="width:332px" id="updraft_dropbox_secret" name="updraft_dropbox_secret" value="<?php echo htmlspecialchars(UpdraftPlus_Options::get_updraft_option('updraft_dropbox_secret')); ?>" /></td> |
| 265 |
</tr> |
| 266 |
|
| 267 |
<?php } ?> |
| 268 |
|
| 269 |
<!-- <tr class="updraftplusmethod dropbox"> |
| 270 |
<th></th> |
| 271 |
<td><p><button id="updraft-dropbox-test" type="button" class="button-primary" style="font-size:18px !important">Test Dropbox Settings</button></p></td> |
| 272 |
</tr>--> |
| 273 |
<?php |
| 274 |
} |
| 275 |
/* |
| 276 |
function config_print_javascript_onready() { |
| 277 |
?> |
| 278 |
jQuery('#updraft-dropbox-test').click(function(){ |
| 279 |
var data = { |
| 280 |
action: 'updraft_credentials_test', |
| 281 |
method: 'dropbox', |
| 282 |
nonce: '<?php echo wp_create_nonce('updraftplus-credentialtest-nonce'); ?>', |
| 283 |
appkey: jQuery('#updraft_dropbox_appkey').val(), |
| 284 |
secret: jQuery('#updraft_dropbox_secret').val(), |
| 285 |
folder: jQuery('#updraft_dropbox_folder').val() |
| 286 |
}; |
| 287 |
jQuery.post(ajaxurl, data, function(response) { |
| 288 |
alert('Settings test result: ' + response); |
| 289 |
}); |
| 290 |
}); |
| 291 |
<?php |
| 292 |
}*/ |
| 293 |
|
| 294 |
function action_auth() { |
| 295 |
if ( isset( $_GET['oauth_token'] ) ) { |
| 296 |
self::auth_token(); |
| 297 |
} elseif (isset($_GET['updraftplus_dropboxauth'])) { |
| 298 |
self::auth_request(); |
| 299 |
} |
| 300 |
} |
| 301 |
|
| 302 |
function show_authed_admin_warning() { |
| 303 |
global $updraftplus; |
| 304 |
|
| 305 |
$dropbox = self::bootstrap(); |
| 306 |
$accountInfo = $dropbox->accountInfo(); |
| 307 |
|
| 308 |
$message = "<strong>Success</strong>: you have authenticated your Dropbox account"; |
| 309 |
|
| 310 |
if ($accountInfo['code'] != "200") { |
| 311 |
$message .= " (though part of the returned information was not as expected - your mileage may vary)". $accountInfo['code']; |
| 312 |
} else { |
| 313 |
$body = $accountInfo['body']; |
| 314 |
$message .= ". Your Dropbox account name: ".htmlspecialchars($body->display_name); |
| 315 |
} |
| 316 |
$updraftplus->show_admin_warning($message); |
| 317 |
|
| 318 |
} |
| 319 |
|
| 320 |
function auth_token() { |
| 321 |
$previous_token = UpdraftPlus_Options::get_updraft_option("updraft_dropboxtk_request_token","xyz"); |
| 322 |
self::bootstrap(); |
| 323 |
$new_token = UpdraftPlus_Options::get_updraft_option("updraft_dropboxtk_request_token","xyz"); |
| 324 |
if ($new_token && $new_token != "xyz") { |
| 325 |
add_action('admin_notices', array('UpdraftPlus_BackupModule_dropbox', 'show_authed_admin_warning') ); |
| 326 |
} |
| 327 |
} |
| 328 |
|
| 329 |
// Acquire single-use authorization code |
| 330 |
function auth_request() { |
| 331 |
|
| 332 |
self::bootstrap(); |
| 333 |
|
| 334 |
} |
| 335 |
|
| 336 |
// This basically reproduces the relevant bits of bootstrap.php from the SDK |
| 337 |
function bootstrap() { |
| 338 |
|
| 339 |
require_once(UPDRAFTPLUS_DIR.'/includes/Dropbox/API.php' ); |
| 340 |
require_once(UPDRAFTPLUS_DIR.'/includes/Dropbox/Exception.php'); |
| 341 |
require_once(UPDRAFTPLUS_DIR.'/includes/Dropbox/API.php'); |
| 342 |
require_once(UPDRAFTPLUS_DIR.'/includes/Dropbox/OAuth/Consumer/ConsumerAbstract.php'); |
| 343 |
require_once(UPDRAFTPLUS_DIR.'/includes/Dropbox/OAuth/Storage/StorageInterface.php'); |
| 344 |
require_once(UPDRAFTPLUS_DIR.'/includes/Dropbox/OAuth/Storage/Encrypter.php'); |
| 345 |
require_once(UPDRAFTPLUS_DIR.'/includes/Dropbox/OAuth/Storage/WordPress.php'); |
| 346 |
require_once(UPDRAFTPLUS_DIR.'/includes/Dropbox/OAuth/Consumer/Curl.php'); |
| 347 |
// require_once(UPDRAFTPLUS_DIR.'/includes/Dropbox/OAuth/Consumer/WordPress.php'); |
| 348 |
|
| 349 |
$key = UpdraftPlus_Options::get_updraft_option('updraft_dropbox_secret'); |
| 350 |
$sec = UpdraftPlus_Options::get_updraft_option('updraft_dropbox_appkey'); |
| 351 |
|
| 352 |
// Set the callback URL |
| 353 |
$callback = admin_url('options-general.php?page=updraftplus&action=updraftmethod-dropbox-auth'); |
| 354 |
|
| 355 |
// Instantiate the Encrypter and storage objects |
| 356 |
$encrypter = new Dropbox_Encrypter('ThisOneDoesNotMatterBeyondLength'); |
| 357 |
|
| 358 |
// Instantiate the storage |
| 359 |
$storage = new Dropbox_WordPress($encrypter, "updraft_dropboxtk_"); |
| 360 |
|
| 361 |
// WordPress consumer does not yet work |
| 362 |
// $OAuth = new Dropbox_ConsumerWordPress($sec, $key, $storage, $callback); |
| 363 |
|
| 364 |
// Get the DropBox API access details |
| 365 |
list($d2, $d1) = self::defaults(); |
| 366 |
if (empty($sec)) { $sec = base64_decode($d1); }; if (empty($key)) { $key = base64_decode($d2); } |
| 367 |
$OAuth = new Dropbox_Curl($sec, $key, $storage, $callback); |
| 368 |
return new Dropbox_API($OAuth); |
| 369 |
} |
| 370 |
|
| 371 |
} |
| 372 |
|