Example #1
0
 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!
 }
Example #2
0
 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!
 }