public/wordpress/wp-content/plugins/backupbuddy/destinations/s3/init.php
/* _prepareBucketAndRegion()
	 *
	 * Validates bucket existance, creating if needed.  Sets region for non-US usage.
	 *
	 * @param	object		&$s3			S3 object currently in use. Pased by reference so region can be set.
	 * @param	array 		$settings		Destination settings array.
	 * @param	bool		$createBucket	Whether or not to create bucket if it does not currently exist.
	 * @return	bool						true on all okay, false otherwise.
	 *
	 */
_prepareBucketAndRegion()
Validates bucket existance, creating if needed.  Sets region for non-US usage.

@param       object          &$s3                    S3 object currently in use. Pased by reference so region can be set.
@param       array           $settings               Destination settings array.
@param       bool            $createBucket   Whether or not to create bucket if it does not currently exist.
@return      bool                                            true on all okay, false otherwise.
_prepareBucketAndRegion () Validates bucket existance creating if needed . Sets region for non - US usage . @param object &$s3 S3 object currently in use . Pased by reference so region can be set . @param array $settings Destination settings array . @param bool $createBucket Whether or not to create bucket if it does not currently exist . @return bool true on all okay false otherwise .
private static function _prepareBucketAndRegion( &$s3, $settings, $createBucket = true ) {
		$maybe_create_bucket = false;
		pb_backupbuddy::status( 'details', 'Getting region for bucket: `' . $settings['bucket'] . "`." );
		try {
			$response = self::get_bucket_region( $s3, $settings['bucket'] );
		} catch( Exception $e ) {
			$message = 'Exception retrieving information for bucket `' . $settings['bucket'] . '`. Details: `' . $e->getMessage() . '`.';
			pb_backupbuddy::status( 'error', $message );
			echo $message;
			return false;
		}
		if( !$response->isOK() ) {
			$this_error = 'Bucket region could not be determined; bucket may not exist yet. Message details: `' . (string)$response->body->Message . '`.';
			pb_backupbuddy::status( 'details' , $this_error );
			$region = '';
			$maybe_create_bucket = true;
		} else {
			pb_backupbuddy::status( 'details', 'Bucket exists in region: ' .  (($response->body ==="") ? 'us-east-1' : $response->body ) );
			$region = $response->body; 
		}
		if ( '' == $region ) { 
			$s3->set_region( $settings['region'] );
		} else {
			$s3->set_region( 's3-' . $region . '.amazonaws.com' );
		}
		if ( ( true === $maybe_create_bucket ) && ( true === $createBucket ) ) {
			pb_backupbuddy::status( 'details', 'Attempting to create bucket `' . $settings['bucket'] . '` at region endpoint `' . $settings['region'] . '` (detected region: `' . $region . '`).' );
			try {
				$response = $s3->create_bucket(
					$settings['bucket'],
					$settings['region'],
					AmazonS3::ACL_PRIVATE
				);
			} catch( Exception $e ) {
				$message = 'Exception while trying to create bucket `' . $settings['bucket'] . '` at region endpoint `' . $settings['region'] . '` (detected region: `' . $region . '`). Details: `' . $e->getMessage() . '`.';
				pb_backupbuddy::status( 'error', $message );
				echo $message;
				return false;
			}
			if ( ! $response->isOK() ) { 
				$message = 'Failure creating bucket `' . $settings['bucket'] . '` at region endpoint `' . $settings['region'] . '` (detected region: `' . $region . '`). Message details: `' . (string)$response->body->Message . '`. ';
				if ( '' == $region ) {
					$message .= ' Note: Since the region could not be detected, if you are using IAM security, verify this resource ALLOWs the action "s3:GetBucketLocation". ';
				}
				pb_backupbuddy::status( 'error', $message );
				echo $message;
				return false;
			} else { 
				if ( is_object( $response->body ) ) {
					$messageDetails = (string)$response->body->Message;
				} else {
					$messageDetails = '';
				}
				pb_backupbuddy::status( 'details', 'Success creating bucket `' . $settings['bucket'] . '` at region endpoint `' . $settings['region'] . '`. Message details: `' . $messageDetails . '`.' );
				unset( $messageDetails );
			}
		} 
		return true;
	}
private static function _prepareBucketAndRegion ( & $ s3 , $ settings , $ createBucket = true ) { $ maybe_create_bucket = false ; pb_backupbuddy :: status ( 'details' , 'Getting region for bucket: `' . $ settings [ 'bucket' ] . " `. " ) ; try { $ response = self :: get_bucket_region ( $ s3 , $ settings [ 'bucket' ] ) ; } catch ( Exception $ e ) { $ message = 'Exception retrieving information for bucket `' . $ settings [ 'bucket' ] . '`. Details: `' . $ e -> getMessage ( ) . '`.' ; pb_backupbuddy :: status ( 'error' , $ message ) ; echo $ message ; return false ; } if ( ! $ response -> isOK ( ) ) { $ this_error = 'Bucket region could not be determined; bucket may not exist yet. Message details: `' . ( string ) $ response -> body -> Message . '`.' ; pb_backupbuddy :: status ( 'details' , $ this_error ) ; $ region = '' ; $ maybe_create_bucket = true ; } else { pb_backupbuddy :: status ( 'details' , 'Bucket exists in region: ' . ( ( $ response -> body === " " ) ? 'us-east-1' : $ response -> body ) ) ; $ region = $ response -> body ; } if ( '' == $ region ) { $ s3 -> set_region ( $ settings [ 'region' ] ) ; } else { $ s3 -> set_region ( 's3-' . $ region . '.amazonaws.com' ) ; } if ( ( true === $ maybe_create_bucket ) && ( true === $ createBucket ) ) { pb_backupbuddy :: status ( 'details' , 'Attempting to create bucket `' . $ settings [ 'bucket' ] . '` at region endpoint `' . $ settings [ 'region' ] . '` (detected region: `' . $ region . '`).' ) ; try { $ response = $ s3 -> create_bucket ( $ settings [ 'bucket' ] , $ settings [ 'region' ] , AmazonS3 :: ACL_PRIVATE ) ; } catch ( Exception $ e ) { $ message = 'Exception while trying to create bucket `' . $ settings [ 'bucket' ] . '` at region endpoint `' . $ settings [ 'region' ] . '` (detected region: `' . $ region . '`). Details: `' . $ e -> getMessage ( ) . '`.' ; pb_backupbuddy :: status ( 'error' , $ message ) ; echo $ message ; return false ; } if ( ! $ response -> isOK ( ) ) { $ message = 'Failure creating bucket `' . $ settings [ 'bucket' ] . '` at region endpoint `' . $ settings [ 'region' ] . '` (detected region: `' . $ region . '`). Message details: `' . ( string ) $ response -> body -> Message . '`. ' ; if ( '' == $ region ) { $ message .= ' Note: Since the region could not be detected, if you are using IAM security, verify this resource ALLOWs the action "s3:GetBucketLocation". ' ; } pb_backupbuddy :: status ( 'error' , $ message ) ; echo $ message ; return false ; } else { if ( is_object ( $ response -> body ) ) { $ messageDetails = ( string ) $ response -> body -> Message ; } else { $ messageDetails = '' ; } pb_backupbuddy :: status ( 'details' , 'Success creating bucket `' . $ settings [ 'bucket' ] . '` at region endpoint `' . $ settings [ 'region' ] . '`. Message details: `' . $ messageDetails . '`.' ) ; unset ( $ messageDetails ) ; } } return true ; }
_prepareBucketAndRegion()
 Validates bucket existance, creating if needed.
_prepareBucketAndRegion () Validates bucket existance creating if needed .
// Get bucket region to determine if a bucket already exists. // Assume we will not have to try and create a bucket // Assume we have to create the bucket // Must leave as is for actual operational usage // Set region context for later operations - note that if we are going to try and create // a bucket the region will have been set to empty so we'll get the bucket created in the // user-specified region. // Bucket has no current region (ie it does not exist). Set user-specified region for new buckets. // Create bucket if it does not exist AND parameter pased to this function to create the bucket set to true. // Region/endpoint used based on user-defined setting. // Bucket creation FAILED. // Send SUCCESS. // end if create bucket.