$s3_ssl = true; } else { $s3_ssl = false; } require_once pb_backupbuddy::plugin_path() . '/destinations/s3/lib/s3.php'; $s3 = new pb_backupbuddy_S3($aws_accesskey, $aws_secretkey, $s3_ssl); // Delete S3 backups if (!empty($_POST['delete_file'])) { pb_backupbuddy::verify_nonce(); $delete_count = 0; if (!empty($_POST['files']) && is_array($_POST['files'])) { // loop through and delete s3 files foreach ($_POST['files'] as $s3file) { $delete_count++; // Delete S3 file $s3->deleteObject($aws_bucket, $s3file); } } if ($delete_count > 0) { pb_backupbuddy::alert(sprintf(_n('Deleted %d file.', 'Deleted %d files.', $delete_count, 'it-l10n-backupbuddy'), $delete_count)); } } // Copy S3 backups to the local backup files if (!empty($_GET['copy_file'])) { pb_backupbuddy::alert(sprintf(_x('The remote file is now being copied to your %1$slocal backups%2$s', '%1$s and %2$s are open and close <a> tags', 'it-l10n-backupbuddy'), '<a href="' . pb_backupbuddy::page_url() . '">', '</a>.')); pb_backupbuddy::status('details', 'Scheduling Cron for creating s3 copy.'); pb_backupbuddy::$classes['core']->schedule_single_event(time(), pb_backupbuddy::cron_tag('process_s3_copy'), array($_GET['copy_file'], $aws_accesskey, $aws_secretkey, $aws_bucket, $aws_directory, $s3_ssl)); spawn_cron(time() + 150); // Adds > 60 seconds to get around once per minute cron running limit. update_option('_transient_doing_cron', 0); // Prevent cron-blocking for next item.
function test_s3($accesskey, $secretkey, $bucket, $directory = '', $ssl) { if (empty($accesskey) || empty($secretkey) || empty($bucket)) { return __('Missing one or more required fields.', 'it-l10n-backupbuddy'); } $bucket_requirements = __("Your bucket name must meet certain criteria. It must fulfill the following: \n\n Characters may be lowercase letters, numbers, periods (.), and dashes (-). \n Must start with a number or letter. \n Must be between 3 and 63 characters long. \n Must not be formatted as an IP address (e.g., 192.168.5.4). \n Should be between 3 and 63 characters long. \n Should not end with a dash. \n Cannot contain two, adjacent periods. \n Cannot contain dashes next to periods.", 'it-l10n-backupbuddy'); if (preg_match("/^[a-z0-9][a-z0-9\\-\\.\\_]*(?<!-)\$/i", $bucket) == 0) { // Starts with a-z or 0-9; middle is a-z, 0-9, -, or .; cannot end in a dash. return __('Your bucket failed one or more things in the check: Starts with a-z or 0-9; middle is a-z, 0-9, -, or .; cannot end in a dash.', 'it-l10n-backupbuddy') . ' ' . $bucket_requirements; } if (strlen($bucket) < 3 || strlen($bucket) > 63) { // Must be between 3 and 63 characters long return __('Your bucket must be between 3 and 63 characters long.', 'it-l10n-backupbuddy') . ' ' . $bucket_requirements; } if (strstr($bucket, '.-') !== false || strstr($bucket, '-.') !== false || strstr($bucket, '..') !== false) { // Bucket names cannot contain dashes next to periods (e.g., "my-.bucket.com" and "my.-bucket" are invalid) return __('Your bucket contains a period next to a dash.', 'it-l10n-backupbuddy') . ' ' . $bucket_requirements; } require_once pb_backupbuddy::plugin_path() . '/lib/s3/s3.php'; $s3 = new pb_backupbuddy_S3($accesskey, $secretkey, $ssl); if ($s3->getBucketLocation($bucket) === false) { // Easy way to see if bucket already exists. $s3->putBucket($bucket, pb_backupbuddy_S3::ACL_PRIVATE); } if (!empty($directory)) { $directory = $directory . '/'; } if ($s3->putObject(__('Upload test for BackupBuddy for Amazon S3', 'it-l10n-backupbuddy'), $bucket, $directory . 'backupbuddy.txt', pb_backupbuddy_S3::ACL_PRIVATE)) { // Success... just delete temp test file later... } else { return __('Unable to upload. Verify your keys, bucket name, and account permissions.', 'it-l10n-backupbuddy'); } if (!pb_backupbuddy_S3::deleteObject($bucket, $directory . 'backupbuddy.txt')) { return __('Partial success. Could not delete temp file.', 'it-l10n-backupbuddy'); } return true; // Success! }
public static function test($settings) { pb_backupbuddy::status('details', 'Beginning Amazon S3 destination test.'); $accesskey = $settings['accesskey']; $secretkey = $settings['secretkey']; $bucket = $settings['bucket']; $directory = $settings['directory']; $storage_class = $settings['storage_class']; $ssl = $settings['ssl']; // Add trailing slash to end of directory if one defined. if ($directory != '') { $directory = rtrim($directory, '/\\') . '/'; } // Verify all required fields passed. if (empty($accesskey) || empty($secretkey) || empty($bucket)) { return __('Missing one or more required fields.', 'it-l10n-backupbuddy'); } // Verify bucket naming requirements. $bucket_requirements = __("Your bucket name must meet certain criteria. It must fulfill the following: \n\n Characters may be lowercase letters, numbers, periods (.), and dashes (-). \n Must start with a number or letter. \n Must be between 3 and 63 characters long. \n Must not be formatted as an IP address (e.g., 192.168.5.4). \n Should be between 3 and 63 characters long. \n Should not end with a dash. \n Cannot contain two, adjacent periods. \n Cannot contain dashes next to periods.", 'it-l10n-backupbuddy'); if (preg_match("/^[a-z0-9][a-z0-9\\-\\.\\_]*(?<!-)\$/i", $bucket) == 0) { // Starts with a-z or 0-9; middle is a-z, 0-9, -, or .; cannot end in a dash. return __('Your bucket failed one or more things in the check: Starts with a-z or 0-9; middle is a-z, 0-9, -, or .; cannot end in a dash.', 'it-l10n-backupbuddy') . ' ' . $bucket_requirements; } if (strlen($bucket) < 3 || strlen($bucket) > 63) { // Must be between 3 and 63 characters long return __('Your bucket must be between 3 and 63 characters long.', 'it-l10n-backupbuddy') . ' ' . $bucket_requirements; } if (strstr($bucket, '.-') !== false || strstr($bucket, '-.') !== false || strstr($bucket, '..') !== false) { // Bucket names cannot contain dashes next to periods (e.g., "my-.bucket.com" and "my.-bucket" are invalid) return __('Your bucket contains a period next to a dash.', 'it-l10n-backupbuddy') . ' ' . $bucket_requirements; } pb_backupbuddy::status('details', 'Loading S3 library.'); require_once dirname(__FILE__) . '/lib/s3.php'; pb_backupbuddy::status('details', 'Creating S3 object.'); $s3 = new pb_backupbuddy_S3($accesskey, $secretkey, $ssl); // Check if target bucket exists. Create it if not. pb_backupbuddy::status('details', 'Check if bucket already exists.'); if ($s3->getBucketLocation($bucket) === false) { // Easy way to see if bucket already exists. pb_backupbuddy::status('details', 'Bucket does not exist; creating it.'); $s3->putBucket($bucket, pb_backupbuddy_S3::ACL_PRIVATE); } // Send temporary test file. //pb_backupbuddy::status( 'details', 'Using S3 storage class `' . $storage_class . '`.' ); pb_backupbuddy::status('details', 'About to put file to S3.'); if ($s3->putObject(__('Upload test for BackupBuddy for Amazon S3', 'it-l10n-backupbuddy'), $bucket, $directory . 'backupbuddy.txt', pb_backupbuddy_S3::ACL_PRIVATE)) { // Success... just delete temp test file later... pb_backupbuddy::status('details', 'Success uploading test file to S3.'); } else { pb_backupbuddy::status('error', 'Failure uploading test file to S3.'); return __('Unable to upload. Verify your keys, bucket name, and account permissions.', 'it-l10n-backupbuddy'); } // Delete temporary test file. if (!pb_backupbuddy_S3::deleteObject($bucket, $directory . 'backupbuddy.txt')) { pb_backupbuddy::status('details', 'Partial S3 test success. Could not delete temp file.'); return __('Partial success. Could not delete temp file.', 'it-l10n-backupbuddy'); } return true; // Success! }