function process_remote_copy($destination_type, $file, $settings) { pb_backupbuddy::status('details', 'Copying remote `' . $destination_type . '` file `' . $file . '` down to local.'); pb_backupbuddy::set_greedy_script_limits(); if (!class_exists('backupbuddy_core')) { require_once pb_backupbuddy::plugin_path() . '/classes/core.php'; } // Determine destination filename. $destination_file = backupbuddy_core::getBackupDirectory() . basename($file); if (file_exists($destination_file)) { $destination_file = str_replace('backup-', 'backup_copy_' . pb_backupbuddy::random_string(5) . '-', $destination_file); } pb_backupbuddy::status('details', 'Filename of resulting local copy: `' . $destination_file . '`.'); if ($destination_type == 'stash2') { require_once ABSPATH . 'wp-admin/includes/file.php'; pb_backupbuddy::status('details', 'About to begin downloading from URL.'); $download = download_url($url); pb_backupbuddy::status('details', 'Download process complete.'); if (is_wp_error($download)) { $error = 'Error #832989323: Unable to download file `' . $file . '` from URL: `' . $url . '`. Details: `' . $download->get_error_message() . '`.'; pb_backupbuddy::status('error', $error); pb_backupbuddy::alert($error); return false; } else { if (false === copy($download, $destination_file)) { $error = 'Error #3329383: Unable to copy file from `' . $download . '` to `' . $destination_file . '`.'; pb_backupbuddy::status('error', $error); pb_backupbuddy::alert($error); @unlink($download); return false; } else { pb_backupbuddy::status('details', 'File saved to `' . $destination_file . '`.'); @unlink($download); return true; } } } // end stash2. if ($destination_type == 'stash') { $itxapi_username = $settings['itxapi_username']; $itxapi_password = $settings['itxapi_password']; // Load required files. pb_backupbuddy::status('details', 'Load Stash files.'); require_once pb_backupbuddy::plugin_path() . '/destinations/stash/init.php'; require_once dirname(dirname(__FILE__)) . '/destinations/_s3lib/aws-sdk/sdk.class.php'; require_once pb_backupbuddy::plugin_path() . '/destinations/stash/lib/class.itx_helper.php'; // Talk with the Stash API to get access to do things. pb_backupbuddy::status('details', 'Authenticating Stash for remote copy to local.'); $stash = new ITXAPI_Helper(pb_backupbuddy_destination_stash::ITXAPI_KEY, pb_backupbuddy_destination_stash::ITXAPI_URL, $itxapi_username, $itxapi_password); $manage_url = $stash->get_manage_url(); $request = new RequestCore($manage_url); $response = $request->send_request(true); // Validate response. if (!$response->isOK()) { $error = 'Request for management credentials failed.'; pb_backupbuddy::status('error', $error); pb_backupbuddy::alert($error); return false; } if (!($manage_data = json_decode($response->body, true))) { $error = 'Did not get valid JSON response.'; pb_backupbuddy::status('error', $error); pb_backupbuddy::alert($error); return false; } if (isset($manage_data['error'])) { $error = 'Error: ' . implode(' - ', $manage_data['error']); pb_backupbuddy::status('error', $error); pb_backupbuddy::alert($error); return false; } // Connect to S3. pb_backupbuddy::status('details', 'Instantiating S3 object.'); $s3 = new AmazonS3($manage_data['credentials']); pb_backupbuddy::status('details', 'About to get Stash object `' . $file . '`...'); try { $response = $s3->get_object($manage_data['bucket'], $manage_data['subkey'] . pb_backupbuddy_destination_stash::get_remote_path() . $file, array('fileDownload' => $destination_file)); } catch (Exception $e) { pb_backupbuddy::status('error', 'Error #5443984: ' . $e->getMessage()); error_log('err:' . $e->getMessage()); return false; } if ($response->isOK()) { pb_backupbuddy::status('details', 'Stash copy to local success.'); return true; } else { pb_backupbuddy::status('error', 'Error #894597845. Stash copy to local FAILURE. Details: `' . print_r($response, true) . '`.'); return false; } } elseif ($destination_type == 'gdrive') { die('Not implemented here.'); require_once pb_backupbuddy::plugin_path() . '/destinations/gdrive/init.php'; $settings = array_merge(pb_backupbuddy_destination_gdrive::$default_settings, $settings); if (true === pb_backupbuddy_destination_gdrive::getFile($settings, $file, $destination_file)) { // success pb_backupbuddy::status('details', 'Google Drive copy to local success.'); return true; } else { // fail pb_backupbuddy::status('details', 'Error #2332903. Google Drive copy to local FAILURE.'); return false; } } elseif ($destination_type == 's3') { require_once pb_backupbuddy::plugin_path() . '/destinations/s3/init.php'; if (true === pb_backupbuddy_destination_s3::download_file($settings, $file, $destination_file)) { // success pb_backupbuddy::status('details', 'S3 copy to local success.'); return true; } else { // fail pb_backupbuddy::status('details', 'Error #85448774. S3 copy to local FAILURE.'); return false; } } else { pb_backupbuddy::status('error', 'Error #859485. Unknown destination type for remote copy `' . $destination_type . '`.'); return false; } }