| 1 |
<?php |
| 2 |
namespace Dudlewebs\WPMCS; |
| 3 |
|
| 4 |
defined('ABSPATH') || exit; |
| 5 |
|
| 6 |
// Libraries |
| 7 |
use Dudlewebs\WPMCS\s3\Aws\S3\S3Client; |
| 8 |
use Dudlewebs\WPMCS\s3\Aws\Exception\AwsException; |
| 9 |
use Dudlewebs\WPMCS\s3\Aws\S3\Exception\S3Exception; |
| 10 |
use Dudlewebs\WPMCS\s3\Aws\S3\MultipartUploader; |
| 11 |
use Dudlewebs\WPMCS\s3\Aws\Exception\MultipartUploadException; |
| 12 |
use Exception; |
| 13 |
|
| 14 |
class S3 { |
| 15 |
private $assets_url; |
| 16 |
private $version; |
| 17 |
private $token; |
| 18 |
|
| 19 |
protected $config; |
| 20 |
protected $bucketConfig; |
| 21 |
protected $settings; |
| 22 |
protected $credentials; |
| 23 |
protected $bucket_name; |
| 24 |
|
| 25 |
public $service = 's3'; |
| 26 |
public $s3Client = false; |
| 27 |
|
| 28 |
/** |
| 29 |
* Admin constructor. |
| 30 |
* @since 1.0.0 |
| 31 |
*/ |
| 32 |
public function __construct() { |
| 33 |
$this->assets_url = WPMCS_ASSETS_URL; |
| 34 |
$this->version = WPMCS_VERSION; |
| 35 |
$this->token = WPMCS_TOKEN; |
| 36 |
|
| 37 |
// Initialize setup |
| 38 |
$this->init(); |
| 39 |
} |
| 40 |
|
| 41 |
/** |
| 42 |
* Initialise Client |
| 43 |
*/ |
| 44 |
public function init() { |
| 45 |
$this->settings = Utils::get_settings(); |
| 46 |
$this->credentials = Utils::get_credentials(); |
| 47 |
$this->config = isset($this->credentials['config']) && !empty($this->credentials['config']) |
| 48 |
? $this->credentials['config'] |
| 49 |
: []; |
| 50 |
$this->bucketConfig = isset($this->credentials['bucketConfig']) && !empty($this->credentials['bucketConfig']) |
| 51 |
? $this->credentials['bucketConfig'] |
| 52 |
: []; |
| 53 |
$this->bucket_name = isset($this->bucketConfig['bucket_name']) && !empty($this->bucketConfig['bucket_name']) |
| 54 |
? $this->bucketConfig['bucket_name'] |
| 55 |
: ''; |
| 56 |
|
| 57 |
if ( |
| 58 |
isset($this->config['region']) && !empty($this->config['region']) && |
| 59 |
isset($this->config['access_key']) && !empty($this->config['access_key']) && |
| 60 |
isset($this->config['secret_key']) && !empty($this->config['secret_key']) |
| 61 |
) { |
| 62 |
$this->s3Client = new S3Client([ |
| 63 |
'version' => '2006-03-01', |
| 64 |
'region' => $this->config['region'], |
| 65 |
'use_accelerate_endpoint' => isset($this->bucketConfig['transfer_acceleration']) |
| 66 |
? $this->bucketConfig['transfer_acceleration'] : false, |
| 67 |
'use_aws_shared_config_files' => false, |
| 68 |
'credentials' => [ |
| 69 |
'key' => $this->config['access_key'], |
| 70 |
'secret' => $this->config['secret_key'], |
| 71 |
], |
| 72 |
]); |
| 73 |
} |
| 74 |
|
| 75 |
} |
| 76 |
|
| 77 |
|
| 78 |
/** |
| 79 |
* Verify Credentials |
| 80 |
* @since 1.0.0 |
| 81 |
* @return boolean |
| 82 |
*/ |
| 83 |
public function verifyCredentials($access_key, $secret_key, $region){ |
| 84 |
if ( |
| 85 |
isset($region) && !empty($region) && |
| 86 |
isset($access_key) && !empty($access_key) && |
| 87 |
isset($secret_key) && !empty($secret_key) |
| 88 |
) { |
| 89 |
try { |
| 90 |
$s3Client = new S3Client([ |
| 91 |
'version' => '2006-03-01', |
| 92 |
'region' => $region, |
| 93 |
'use_aws_shared_config_files' => false, |
| 94 |
'credentials' => [ |
| 95 |
'key' => $access_key, |
| 96 |
'secret' => $secret_key, |
| 97 |
], |
| 98 |
]); |
| 99 |
|
| 100 |
$result = [ |
| 101 |
'success' => false, |
| 102 |
'code' => 200, |
| 103 |
'message' => esc_html__('Please check the authorization details', 'media-cloud-sync'), |
| 104 |
]; |
| 105 |
|
| 106 |
try { |
| 107 |
$s3Client->listObjectsV2([ |
| 108 |
'Bucket' => $this->token . '_dummy-bucket-for-auth-check' |
| 109 |
]); |
| 110 |
|
| 111 |
// If we reach here, the credentials are valid |
| 112 |
$result = [ |
| 113 |
'success' => true, |
| 114 |
'code' => 200, |
| 115 |
'message' => esc_html__('Credentials are valid', 'media-cloud-sync'), |
| 116 |
]; |
| 117 |
} catch (AwsException $e) { |
| 118 |
$code = $e->getAwsErrorCode(); |
| 119 |
|
| 120 |
$validErrors = [ |
| 121 |
'AccessDenied', |
| 122 |
'NoSuchBucket', |
| 123 |
'AllAccessDisabled', |
| 124 |
'AuthorizationHeaderMalformed', |
| 125 |
'PermanentRedirect', |
| 126 |
]; |
| 127 |
|
| 128 |
if (in_array($code, $validErrors)) { |
| 129 |
// If we reach here, the credentials are valid |
| 130 |
$result = [ |
| 131 |
'success' => true, |
| 132 |
'code' => 200, |
| 133 |
'message' => esc_html__('Credentials are valid', 'media-cloud-sync'), |
| 134 |
]; |
| 135 |
} |
| 136 |
} |
| 137 |
|
| 138 |
if($result['success'] == false) { |
| 139 |
return $result; |
| 140 |
} |
| 141 |
|
| 142 |
try { |
| 143 |
$buckets = $s3Client->listBuckets(); |
| 144 |
$newBucketFormat = []; |
| 145 |
if(isset($buckets['Buckets']) && !empty($buckets['Buckets'])){ |
| 146 |
foreach($buckets['Buckets'] as $bucket) { |
| 147 |
if(isset($bucket['Name'])) { |
| 148 |
$newBucketFormat[] = ['Name' => $bucket['Name'], 'CreationDate' => $bucket['CreationDate']]; |
| 149 |
} |
| 150 |
} |
| 151 |
} |
| 152 |
$result['buckets_data']['buckets'] = $newBucketFormat; |
| 153 |
$result['buckets_data']['message'] = esc_html__('Buckets listed successfully', 'media-cloud-sync'); |
| 154 |
$result['buckets_data']['status'] = true; |
| 155 |
} catch (S3Exception $e) { |
| 156 |
$result ['buckets_data']['buckets'] = []; |
| 157 |
$result ['buckets_data']['message'] = esc_html__('Unable to list buckets, Please check the bucket listing permission', 'media-cloud-sync'); |
| 158 |
$result ['buckets_data']['status'] = false; |
| 159 |
} catch (Exception $e) { |
| 160 |
$result ['buckets_data']['buckets'] = []; |
| 161 |
$result ['buckets_data']['message'] = esc_html__('Unable to list buckets, Please check the bucket listing permission', 'media-cloud-sync'); |
| 162 |
$result ['buckets_data']['status'] = false; |
| 163 |
} |
| 164 |
return $result; |
| 165 |
} |
| 166 |
catch (S3Exception $ex) { |
| 167 |
return array('message' => $ex->getAwsErrorMessage() ?? esc_html__('Please check the authorization details', 'media-cloud-sync'), 'code' => 200, 'success' => false); |
| 168 |
} catch (Exception $ex) { |
| 169 |
return array('message' => $ex->getMessage() ?? esc_html__('Please check the authorization details', 'media-cloud-sync'), 'code' => 200, 'success' => false); |
| 170 |
} |
| 171 |
} |
| 172 |
return array('message' => esc_html__('Insufficient Data. Please try again', 'media-cloud-sync'), 'code' => 200, 'success' => false); |
| 173 |
} |
| 174 |
|
| 175 |
/** |
| 176 |
* Verify Bucket Exist |
| 177 |
* @since 1.0.0 |
| 178 |
* @return boolean |
| 179 |
*/ |
| 180 |
public function verifyBucketExist($access_key, $secret_key, $region, $bucket_name, $transfer_acceleration=false){ |
| 181 |
if ( |
| 182 |
isset($region) && !empty($region) && |
| 183 |
isset($access_key) && !empty($access_key) && |
| 184 |
isset($secret_key) && !empty($secret_key) && |
| 185 |
isset($bucket_name) && !empty($bucket_name) |
| 186 |
) { |
| 187 |
try { |
| 188 |
$s3Client = new S3Client([ |
| 189 |
'version' => '2006-03-01', |
| 190 |
'region' => $region, |
| 191 |
'use_accelerate_endpoint' => $transfer_acceleration, |
| 192 |
'use_aws_shared_config_files' => false, |
| 193 |
'credentials' => [ |
| 194 |
'key' => $access_key, |
| 195 |
'secret' => $secret_key, |
| 196 |
], |
| 197 |
]); |
| 198 |
|
| 199 |
//get S3 object |
| 200 |
$bucket_found = false; |
| 201 |
try { |
| 202 |
$s3Client->getObject([ |
| 203 |
'Bucket' => $bucket_name, |
| 204 |
'Key' => $this->token . '_dummy-object-for-bucket-exist-check' |
| 205 |
]); |
| 206 |
$bucket_found = true; |
| 207 |
} catch (AwsException $e) { |
| 208 |
$code = $e->getAwsErrorCode(); |
| 209 |
if ($code === 'NoSuchKey') { |
| 210 |
$bucket_found = true; |
| 211 |
} |
| 212 |
} |
| 213 |
|
| 214 |
if($bucket_found) { |
| 215 |
return array('message' => esc_html__('Bucket exist', 'media-cloud-sync'), 'code' => 200, 'success' => true); |
| 216 |
} else { |
| 217 |
return array('message' => esc_html__("Bucket choosen does not exist / does not have read permission", 'media-cloud-sync'), 'code' => 200, 'success' => false); |
| 218 |
} |
| 219 |
} |
| 220 |
catch (S3Exception $ex) { |
| 221 |
return array('message' => $ex->getAwsErrorMessage() ?? esc_html__('Please check the authorization details', 'media-cloud-sync'), 'code' => 200, 'success' => false); |
| 222 |
} catch (Exception $ex) { |
| 223 |
return array('message' => $ex->getMessage() ?? esc_html__('Please check the authorization details', 'media-cloud-sync'), 'code' => 200, 'success' => false); |
| 224 |
} |
| 225 |
} |
| 226 |
return array('message' => esc_html__('Insufficient Data. Please try again', 'media-cloud-sync'), 'code' => 200, 'success' => false); |
| 227 |
} |
| 228 |
|
| 229 |
/** |
| 230 |
* Create Bucket |
| 231 |
* @since 1.0.0 |
| 232 |
* @return boolean |
| 233 |
*/ |
| 234 |
public function createBucket($access_key, $secret_key, $region, $bucket_name, $transfer_acceleration = false){ |
| 235 |
if (empty($region) || empty($access_key) || empty($secret_key) || empty($bucket_name)) { |
| 236 |
return ['message' => esc_html__('Insufficient Data. Please try again', 'media-cloud-sync'), 'code' => 200, 'success' => false]; |
| 237 |
} |
| 238 |
|
| 239 |
try { |
| 240 |
$s3ClientConfig = [ |
| 241 |
'version' => '2006-03-01', |
| 242 |
'region' => $region, |
| 243 |
'use_aws_shared_config_files' => false, |
| 244 |
'credentials' => [ |
| 245 |
'key' => $access_key, |
| 246 |
'secret' => $secret_key, |
| 247 |
], |
| 248 |
]; |
| 249 |
|
| 250 |
$s3Client = new S3Client($s3ClientConfig); |
| 251 |
|
| 252 |
// Create Bucket |
| 253 |
if ($region === 'us-east-1') { |
| 254 |
$s3Client->createBucket([ |
| 255 |
'Bucket' => $bucket_name, |
| 256 |
]); |
| 257 |
} else { |
| 258 |
$s3Client->createBucket([ |
| 259 |
'Bucket' => $bucket_name, |
| 260 |
'CreateBucketConfiguration' => [ |
| 261 |
'LocationConstraint' => $region, |
| 262 |
], |
| 263 |
]); |
| 264 |
} |
| 265 |
|
| 266 |
try { |
| 267 |
$this->blockPublicAccess($bucket_name, false, $s3Client); |
| 268 |
$this->putBucketPolicy($bucket_name, $s3Client); |
| 269 |
$this->changeBucketOwnership($bucket_name, $s3Client); |
| 270 |
try { |
| 271 |
|
| 272 |
$this->changeTransferAccilaration($bucket_name, $s3Client, $transfer_acceleration); |
| 273 |
|
| 274 |
return [ |
| 275 |
'message' => esc_html__('Bucket created successfully. Choose bucket from list to select the bucket.', 'media-cloud-sync'), |
| 276 |
'data' => [ |
| 277 |
'Name' => $bucket_name, |
| 278 |
'CreationDate' => date('Y-m-d\TH:i:s\Z'), |
| 279 |
], |
| 280 |
'code' => 200, |
| 281 |
'success' => true, |
| 282 |
]; |
| 283 |
} catch (AwsException $ex) { |
| 284 |
return ['message' => esc_html__('Bucket created and made public. But the following error happened while setting the transfer accilaration,', 'media-cloud-sync') . ' ' . $ex->getAwsErrorMessage(), 'code' => 200, 'success' => false]; |
| 285 |
} |
| 286 |
} catch (AwsException $ex) { |
| 287 |
return ['message' => esc_html__('Bucket created. But the following error happened while setting the public access,', 'media-cloud-sync') . ' ' . $ex->getAwsErrorMessage(), 'code' => 200, 'success' => false]; |
| 288 |
} |
| 289 |
} catch (AwsException $ex) { |
| 290 |
return ['message' => $ex->getAwsErrorMessage(), 'code' => 200, 'success' => false]; |
| 291 |
} catch (S3Exception $ex) { |
| 292 |
return ['message' => $ex->getAwsErrorMessage() ?? esc_html__('Please check the authorization details', 'media-cloud-sync'), 'code' => 200, 'success' => false]; |
| 293 |
} catch (Exception $ex) { |
| 294 |
return ['message' => $ex->getMessage() ?? esc_html__('Please check the authorization details', 'media-cloud-sync'), 'code' => 200, 'success' => false]; |
| 295 |
} |
| 296 |
} |
| 297 |
|
| 298 |
/** |
| 299 |
* Check Bucket Write Permission |
| 300 |
* @since 1.0.0 |
| 301 |
*/ |
| 302 |
public function verifyObjectWritePermission($access_key, $secret_key, $region, $bucket_name, $transfer_acceleration = false){ |
| 303 |
if (empty($region) || empty($access_key) || empty($secret_key) || empty($bucket_name)) { |
| 304 |
return ['message' => esc_html__('Insufficient Data. Please try again', 'media-cloud-sync'), 'code' => 200, 'success' => false]; |
| 305 |
} |
| 306 |
|
| 307 |
try { |
| 308 |
$s3ClientConfig = [ |
| 309 |
'version' => '2006-03-01', |
| 310 |
'region' => $region, |
| 311 |
'use_accelerate_endpoint' => $transfer_acceleration, |
| 312 |
'use_aws_shared_config_files' => false, |
| 313 |
'credentials' => [ |
| 314 |
'key' => $access_key, |
| 315 |
'secret' => $secret_key, |
| 316 |
], |
| 317 |
]; |
| 318 |
|
| 319 |
$s3Client = new S3Client($s3ClientConfig); |
| 320 |
|
| 321 |
$object_key = Utils::generate_object_key($this->token . '_dummy-object-for-bucket-permission-check', ''); |
| 322 |
|
| 323 |
|
| 324 |
// Create a dummy object to check write permission |
| 325 |
$s3Client->putObject([ |
| 326 |
'Bucket' => $bucket_name, |
| 327 |
'Key' => $object_key, |
| 328 |
'Body' => 'This is a test object to check write permission.', |
| 329 |
]); |
| 330 |
// Check if the object was created successfully |
| 331 |
if ($s3Client->doesObjectExist($bucket_name, $object_key)) { |
| 332 |
return ['message' => esc_html__('Bucket write permission verified successfully', 'media-cloud-sync'), 'code' => 200, 'success' => true]; |
| 333 |
} else { |
| 334 |
return ['message' => esc_html__('Bucket write permission not verified', 'media-cloud-sync'), 'code' => 200, 'success' => false]; |
| 335 |
} |
| 336 |
|
| 337 |
} catch (AwsException $ex) { |
| 338 |
return ['message' => $ex->getAwsErrorMessage(), 'code' => 200, 'success' => false]; |
| 339 |
} catch (S3Exception $ex) { |
| 340 |
return ['message' => $ex->getAwsErrorMessage() ?? esc_html__('Please check the authorization details', 'media-cloud-sync'), 'code' => 200, 'success' => false]; |
| 341 |
} catch (Exception $ex) { |
| 342 |
return ['message' => $ex->getMessage() ?? esc_html__('Please check the authorization details', 'media-cloud-sync'), 'code' => 200, 'success' => false]; |
| 343 |
} |
| 344 |
} |
| 345 |
|
| 346 |
|
| 347 |
|
| 348 |
/** |
| 349 |
* Check Bucket Delete Permission |
| 350 |
* @since 1.0.0 |
| 351 |
*/ |
| 352 |
public function verifyObjectDeletePermission($access_key, $secret_key, $region, $bucket_name, $transfer_acceleration = false){ |
| 353 |
if (empty($region) || empty($access_key) || empty($secret_key) || empty($bucket_name)) { |
| 354 |
return ['message' => esc_html__('Insufficient Data. Please try again', 'media-cloud-sync'), 'code' => 200, 'success' => false]; |
| 355 |
} |
| 356 |
|
| 357 |
try { |
| 358 |
$s3ClientConfig = [ |
| 359 |
'version' => '2006-03-01', |
| 360 |
'region' => $region, |
| 361 |
'use_accelerate_endpoint' => $transfer_acceleration, |
| 362 |
'use_aws_shared_config_files' => false, |
| 363 |
'credentials' => [ |
| 364 |
'key' => $access_key, |
| 365 |
'secret' => $secret_key, |
| 366 |
], |
| 367 |
]; |
| 368 |
|
| 369 |
$s3Client = new S3Client($s3ClientConfig); |
| 370 |
|
| 371 |
$object_key = Utils::generate_object_key($this->token . '_dummy-object-for-bucket-permission-check', ''); |
| 372 |
|
| 373 |
// Create a dummy object to check dlete permission |
| 374 |
$s3Client->deleteObject([ |
| 375 |
'Bucket' => $bucket_name, |
| 376 |
'Key' => $object_key, |
| 377 |
]); |
| 378 |
|
| 379 |
// Check if the object was created successfully |
| 380 |
if (!$s3Client->doesObjectExist($bucket_name, $object_key)) { |
| 381 |
return ['message' => esc_html__('Bucket delete permission verified successfully', 'media-cloud-sync'), 'code' => 200, 'success' => true]; |
| 382 |
} else { |
| 383 |
return ['message' => esc_html__('Bucket delete permission not verified', 'media-cloud-sync'), 'code' => 200, 'success' => false]; |
| 384 |
} |
| 385 |
|
| 386 |
} catch (AwsException $ex) { |
| 387 |
return ['message' => $ex->getAwsErrorMessage(), 'code' => 200, 'success' => false]; |
| 388 |
} catch (S3Exception $ex) { |
| 389 |
return ['message' => $ex->getAwsErrorMessage() ?? esc_html__('Please check the authorization details', 'media-cloud-sync'), 'code' => 200, 'success' => false]; |
| 390 |
} catch (Exception $ex) { |
| 391 |
return ['message' => $ex->getMessage() ?? esc_html__('Please check the authorization details', 'media-cloud-sync'), 'code' => 200, 'success' => false]; |
| 392 |
} |
| 393 |
} |
| 394 |
|
| 395 |
|
| 396 |
/** |
| 397 |
* get Bucket Security Settings |
| 398 |
*/ |
| 399 |
public function getBucketSecuritySettings($access_key, $secret_key, $region, $bucket_name, $transfer_acceleration = false){ |
| 400 |
if (empty($region) || empty($access_key) || empty($secret_key) || empty($bucket_name)) { |
| 401 |
return ['message' => esc_html__('Insufficient Data. Please try again', 'media-cloud-sync'), 'code' => 200, 'success' => false]; |
| 402 |
} |
| 403 |
|
| 404 |
try { |
| 405 |
$s3ClientConfig = [ |
| 406 |
'version' => '2006-03-01', |
| 407 |
'region' => $region, |
| 408 |
'use_accelerate_endpoint' => $transfer_acceleration, |
| 409 |
'use_aws_shared_config_files' => false, |
| 410 |
'credentials' => [ |
| 411 |
'key' => $access_key, |
| 412 |
'secret' => $secret_key, |
| 413 |
], |
| 414 |
]; |
| 415 |
|
| 416 |
$s3Client = new S3Client($s3ClientConfig); |
| 417 |
|
| 418 |
$publicAccessBlock = $s3Client->getPublicAccessBlock([ |
| 419 |
'Bucket' => $bucket_name, |
| 420 |
]); |
| 421 |
|
| 422 |
$publicAccessBlockConfig = $publicAccessBlock['PublicAccessBlockConfiguration']; |
| 423 |
|
| 424 |
$security = []; |
| 425 |
if ( |
| 426 |
$publicAccessBlockConfig['BlockPublicAcls'] && |
| 427 |
$publicAccessBlockConfig['IgnorePublicAcls'] && |
| 428 |
$publicAccessBlockConfig['BlockPublicPolicy'] && |
| 429 |
$publicAccessBlockConfig['RestrictPublicBuckets'] |
| 430 |
) { |
| 431 |
$security['block_public_access'] = true; |
| 432 |
} else { |
| 433 |
$security['block_public_access'] = false; |
| 434 |
} |
| 435 |
|
| 436 |
|
| 437 |
$ownershipControls = $s3Client->getBucketOwnershipControls([ |
| 438 |
'Bucket' => $bucket_name, |
| 439 |
]); |
| 440 |
|
| 441 |
$ownershipRule = $ownershipControls['OwnershipControls']['Rules'][0]['ObjectOwnership']; |
| 442 |
|
| 443 |
if ($ownershipRule === 'BucketOwnerEnforced') { |
| 444 |
$security['object_ownership_enforced'] = true; |
| 445 |
} else { |
| 446 |
$security['object_ownership_enforced'] = false; |
| 447 |
} |
| 448 |
|
| 449 |
return ['message' => '', 'code' => 200, 'success' => true, 'security' => $security]; |
| 450 |
} |
| 451 |
catch (AwsException $ex) { |
| 452 |
return ['message' => $ex->getAwsErrorMessage(), 'code' => 200, 'success' => false]; |
| 453 |
} catch (S3Exception $ex) { |
| 454 |
return ['message' => $ex->getAwsErrorMessage() ?? esc_html__('Please check the authorization details', 'media-cloud-sync'), 'code' => 200, 'success' => false]; |
| 455 |
} catch (Exception $ex) { |
| 456 |
return ['message' => $ex->getMessage() ?? esc_html__('Please check the authorization details', 'media-cloud-sync'), 'code' => 200, 'success' => false]; |
| 457 |
} |
| 458 |
} |
| 459 |
|
| 460 |
|
| 461 |
|
| 462 |
/** |
| 463 |
* Change Bucket Public Access |
| 464 |
*/ |
| 465 |
|
| 466 |
public function changePublicAccess($value, $access_key, $secret_key, $region, $bucket_name, $transfer_acceleration = false){ |
| 467 |
if (empty($region) || empty($access_key) || empty($secret_key) || empty($bucket_name)) { |
| 468 |
return ['message' => esc_html__('Insufficient Data. Please try again', 'media-cloud-sync'), 'code' => 200, 'success' => false]; |
| 469 |
} |
| 470 |
|
| 471 |
try { |
| 472 |
$s3ClientConfig = [ |
| 473 |
'version' => '2006-03-01', |
| 474 |
'region' => $region, |
| 475 |
'use_accelerate_endpoint' => $transfer_acceleration, |
| 476 |
'use_aws_shared_config_files' => false, |
| 477 |
'credentials' => [ |
| 478 |
'key' => $access_key, |
| 479 |
'secret' => $secret_key, |
| 480 |
], |
| 481 |
]; |
| 482 |
|
| 483 |
$s3Client = new S3Client($s3ClientConfig); |
| 484 |
|
| 485 |
return ['message' => '', 'code' => 200, 'success' => true, 'result' => $this->blockPublicAccess($bucket_name, $value, $s3Client)]; |
| 486 |
} |
| 487 |
catch (AwsException $ex) { |
| 488 |
return ['message' => $ex->getAwsErrorMessage(), 'code' => 200, 'success' => false]; |
| 489 |
} catch (S3Exception $ex) { |
| 490 |
return ['message' => $ex->getAwsErrorMessage() ?? esc_html__('Please check the authorization details', 'media-cloud-sync'), 'code' => 200, 'success' => false]; |
| 491 |
} catch (Exception $ex) { |
| 492 |
return ['message' => $ex->getMessage() ?? esc_html__('Please check the authorization details', 'media-cloud-sync'), 'code' => 200, 'success' => false]; |
| 493 |
} |
| 494 |
} |
| 495 |
|
| 496 |
|
| 497 |
|
| 498 |
/** |
| 499 |
* Change Bucket Ownership |
| 500 |
*/ |
| 501 |
|
| 502 |
public function changeObjectOwnership($value, $access_key, $secret_key, $region, $bucket_name, $transfer_acceleration = false){ |
| 503 |
if (empty($region) || empty($access_key) || empty($secret_key) || empty($bucket_name)) { |
| 504 |
return ['message' => esc_html__('Insufficient Data. Please try again', 'media-cloud-sync'), 'code' => 200, 'success' => false]; |
| 505 |
} |
| 506 |
|
| 507 |
try { |
| 508 |
$s3ClientConfig = [ |
| 509 |
'version' => '2006-03-01', |
| 510 |
'region' => $region, |
| 511 |
'use_accelerate_endpoint' => $transfer_acceleration, |
| 512 |
'use_aws_shared_config_files' => false, |
| 513 |
'credentials' => [ |
| 514 |
'key' => $access_key, |
| 515 |
'secret' => $secret_key, |
| 516 |
], |
| 517 |
]; |
| 518 |
|
| 519 |
$s3Client = new S3Client($s3ClientConfig); |
| 520 |
|
| 521 |
$ownership = $value ? 'BucketOwnerEnforced' : 'BucketOwnerPreferred'; |
| 522 |
|
| 523 |
return ['message' => '', 'code' => 200, 'success' => true, 'result' => $this->changeBucketOwnership( $bucket_name, $s3Client, $ownership )]; |
| 524 |
} |
| 525 |
catch (AwsException $ex) { |
| 526 |
return ['message' => $ex->getAwsErrorMessage(), 'code' => 200, 'success' => false]; |
| 527 |
} catch (S3Exception $ex) { |
| 528 |
return ['message' => $ex->getAwsErrorMessage() ?? esc_html__('Please check the authorization details', 'media-cloud-sync'), 'code' => 200, 'success' => false]; |
| 529 |
} catch (Exception $ex) { |
| 530 |
return ['message' => $ex->getMessage() ?? esc_html__('Please check the authorization details', 'media-cloud-sync'), 'code' => 200, 'success' => false]; |
| 531 |
} |
| 532 |
} |
| 533 |
|
| 534 |
|
| 535 |
|
| 536 |
|
| 537 |
/** |
| 538 |
* Block Public Access |
| 539 |
* @since 1.0.0 |
| 540 |
*/ |
| 541 |
private function blockPublicAccess($bucket, $block = true, $s3Client = false) { |
| 542 |
if($s3Client == false) { |
| 543 |
$s3Client = $this->s3Client; |
| 544 |
} |
| 545 |
|
| 546 |
if(empty($bucket)) return false; |
| 547 |
|
| 548 |
try { |
| 549 |
$s3Client->putPublicAccessBlock([ |
| 550 |
'Bucket' => $bucket, |
| 551 |
'PublicAccessBlockConfiguration' => [ |
| 552 |
'BlockPublicPolicy' => $block, |
| 553 |
'BlockPublicAcls' => $block, |
| 554 |
'IgnorePublicAcls' => $block, |
| 555 |
'RestrictPublicBuckets' => $block, |
| 556 |
] |
| 557 |
]); |
| 558 |
|
| 559 |
return true; |
| 560 |
} catch (AwsException $ex) { |
| 561 |
return false; |
| 562 |
} |
| 563 |
} |
| 564 |
|
| 565 |
/** |
| 566 |
* Add Bucket Policy |
| 567 |
*/ |
| 568 |
private function putBucketPolicy($bucket, $s3Client = false) { |
| 569 |
if($s3Client == false) { |
| 570 |
$s3Client = $this->s3Client; |
| 571 |
} |
| 572 |
|
| 573 |
if(empty($bucket)) return false; |
| 574 |
|
| 575 |
$policy = json_encode([ |
| 576 |
"Version" => "2012-10-17", |
| 577 |
"Statement" => [ |
| 578 |
[ |
| 579 |
"Effect" => "Allow", |
| 580 |
"Principal" => "*", |
| 581 |
"Action" => [ |
| 582 |
"s3:DeleteObjectTagging", |
| 583 |
"s3:ListBucketMultipartUploads", |
| 584 |
"s3:DeleteObjectVersion", |
| 585 |
"s3:ListBucket", |
| 586 |
"s3:DeleteObjectVersionTagging", |
| 587 |
"s3:GetBucketAcl", |
| 588 |
"s3:ListMultipartUploadParts", |
| 589 |
"s3:PutObject", |
| 590 |
"s3:GetObjectAcl", |
| 591 |
"s3:GetObject", |
| 592 |
"s3:AbortMultipartUpload", |
| 593 |
"s3:DeleteObject", |
| 594 |
"s3:GetBucketLocation", |
| 595 |
"s3:PutObjectAcl", |
| 596 |
"s3:putBucketOwnershipControls", |
| 597 |
"s3:putBucketPolicy" |
| 598 |
], |
| 599 |
"Resource" => [ |
| 600 |
"arn:aws:s3:::$bucket/*", |
| 601 |
"arn:aws:s3:::$bucket" |
| 602 |
] |
| 603 |
] |
| 604 |
] |
| 605 |
]); |
| 606 |
|
| 607 |
// Add bucket policy |
| 608 |
$s3Client->putBucketPolicy(['Bucket' => $bucket, 'Policy' => $policy]); |
| 609 |
|
| 610 |
return true; |
| 611 |
} |
| 612 |
|
| 613 |
/** |
| 614 |
* Add Bucket Ownership |
| 615 |
*/ |
| 616 |
private function changeBucketOwnership($bucket, $s3Client = false, $ownership = 'BucketOwnerPreferred') { |
| 617 |
if($s3Client == false) { |
| 618 |
$s3Client = $this->s3Client; |
| 619 |
} |
| 620 |
|
| 621 |
if(empty($bucket)) return false; |
| 622 |
|
| 623 |
// Change object ownership ACL enabled |
| 624 |
$s3Client->putBucketOwnershipControls([ |
| 625 |
'Bucket' => $bucket, |
| 626 |
'OwnershipControls' => [ |
| 627 |
'Rules' => [['ObjectOwnership' => $ownership]], |
| 628 |
], |
| 629 |
]); |
| 630 |
|
| 631 |
return true; |
| 632 |
} |
| 633 |
|
| 634 |
/** |
| 635 |
* Change transfer accilaration |
| 636 |
*/ |
| 637 |
private function changeTransferAccilaration($bucket, $s3Client = false, $enable=false, $force = false) { |
| 638 |
if($s3Client == false) { |
| 639 |
$s3Client = $this->s3Client; |
| 640 |
} |
| 641 |
|
| 642 |
if(empty($bucket)) return false; |
| 643 |
if(!$force && !$enable) return true; |
| 644 |
|
| 645 |
$s3Client->putBucketAccelerateConfiguration([ |
| 646 |
'Bucket' => $bucket, |
| 647 |
'AccelerateConfiguration' => [ |
| 648 |
'Status' => $enable ? 'Enabled' : 'Suspended' |
| 649 |
] |
| 650 |
]); |
| 651 |
|
| 652 |
return true; |
| 653 |
} |
| 654 |
|
| 655 |
/** |
| 656 |
* isConfigured Function To Identify the congfigurations are correct |
| 657 |
* @since 1.0.0 |
| 658 |
*/ |
| 659 |
public function isConfigured(){ |
| 660 |
if ($this->s3Client) { |
| 661 |
try { |
| 662 |
$buckets = $s3Client->listBuckets(); |
| 663 |
if(!empty($buckets)){ |
| 664 |
foreach($buckets as $bucket) { |
| 665 |
if ($bucket['Name']==$this->bucket_name) { |
| 666 |
return true; |
| 667 |
} |
| 668 |
} |
| 669 |
} |
| 670 |
return false; |
| 671 |
} catch (AwsException $ex) { |
| 672 |
return false; |
| 673 |
} |
| 674 |
} |
| 675 |
return false; |
| 676 |
} |
| 677 |
|
| 678 |
/** |
| 679 |
* Make Object Private |
| 680 |
* @since 1.0.0 |
| 681 |
* |
| 682 |
*/ |
| 683 |
public function toPrivate($key) { |
| 684 |
if(!$key) return false; |
| 685 |
try { |
| 686 |
$this->s3Client->putObjectAcl([ |
| 687 |
'Bucket' => $this->bucket_name, |
| 688 |
'Key' => $key, |
| 689 |
'ACL' => 'private' |
| 690 |
]); |
| 691 |
return true; |
| 692 |
} catch (AwsException $ex) { |
| 693 |
return false; |
| 694 |
} |
| 695 |
return false; |
| 696 |
} |
| 697 |
|
| 698 |
|
| 699 |
|
| 700 |
/** |
| 701 |
* Make Object Public |
| 702 |
* @since 1.0.0 |
| 703 |
* |
| 704 |
*/ |
| 705 |
public function toPublic($key) { |
| 706 |
if(!$key) return false; |
| 707 |
try { |
| 708 |
$this->s3Client->putObjectAcl([ |
| 709 |
'Bucket' => $this->bucket_name, |
| 710 |
'Key' => $key, |
| 711 |
'ACL' => 'public-read' |
| 712 |
]); |
| 713 |
return true; |
| 714 |
} catch (AwsException $ex) { |
| 715 |
return false; |
| 716 |
} |
| 717 |
return false; |
| 718 |
} |
| 719 |
|
| 720 |
|
| 721 |
|
| 722 |
/** |
| 723 |
* Check the object exist |
| 724 |
* @since 1.1.8 |
| 725 |
*/ |
| 726 |
public function exists($key) { |
| 727 |
if(!$key) return false; |
| 728 |
|
| 729 |
if($this->s3Client->doesObjectExist($this->bucket_name, $key)) { |
| 730 |
return true; |
| 731 |
} |
| 732 |
|
| 733 |
return false; |
| 734 |
} |
| 735 |
|
| 736 |
/** |
| 737 |
* Upload Single |
| 738 |
* @since 1.0.0 |
| 739 |
* @return boolean |
| 740 |
*/ |
| 741 |
public function uploadSingle($media_absolute_path, $media_path, $prefix='') { |
| 742 |
$result = array(); |
| 743 |
if ( |
| 744 |
isset($media_absolute_path) && !empty($media_absolute_path) && |
| 745 |
isset($media_path) && !empty($media_path) |
| 746 |
) { |
| 747 |
$file_name = wp_basename( $media_path ); |
| 748 |
if ($file_name) { |
| 749 |
$upload_path = Utils::generate_object_key($media_path, $prefix); |
| 750 |
|
| 751 |
// Decide Multipart upload or normal put object |
| 752 |
if (filesize($media_absolute_path) <= Schema::getConstant('S3_MULTIPART_MIN_FILE_SIZE')) { |
| 753 |
// Upload a publicly accessible file. The file size and type are determined by the SDK. |
| 754 |
try { |
| 755 |
$upload = $this->s3Client->putObject([ |
| 756 |
'Bucket' => $this->bucket_name, |
| 757 |
'Key' => $upload_path, |
| 758 |
'Body' => fopen($media_absolute_path, 'r'), |
| 759 |
'ACL' => 'public-read', // make file 'public' |
| 760 |
]); |
| 761 |
|
| 762 |
$result = array( |
| 763 |
'success' => true, |
| 764 |
'code' => 200, |
| 765 |
'file_url' => $this->generate_file_url($upload_path), |
| 766 |
'key' => $upload_path, |
| 767 |
'message' => esc_html__('File Uploaded Successfully', 'media-cloud-sync') |
| 768 |
); |
| 769 |
} catch (AwsException $e) { |
| 770 |
$result = array( |
| 771 |
'success' => false, |
| 772 |
'code' => 200, |
| 773 |
'message' => $e->getMessage() |
| 774 |
); |
| 775 |
} |
| 776 |
} else { |
| 777 |
$multiUploader = new MultipartUploader($this->s3Client, $media_absolute_path, [ |
| 778 |
'bucket' => $this->bucket_name, |
| 779 |
'key' => $upload_path, |
| 780 |
'acl' => 'public-read', // make file 'public' |
| 781 |
]); |
| 782 |
|
| 783 |
try { |
| 784 |
do { |
| 785 |
try { |
| 786 |
$uploaded = $multiUploader->upload(); |
| 787 |
} catch (MultipartUploadException $e) { |
| 788 |
$multiUploader = new MultipartUploader($this->s3Client, $media_absolute_path, [ |
| 789 |
'state' => $e->getState(), |
| 790 |
]); |
| 791 |
} |
| 792 |
} while (!isset($uploaded)); |
| 793 |
|
| 794 |
if (isset($uploaded['ObjectURL']) && !empty($uploaded['ObjectURL'])) { |
| 795 |
$result = array( |
| 796 |
'success' => true, |
| 797 |
'code' => 200, |
| 798 |
'file_url' => $this->generate_file_url($upload_path), |
| 799 |
'key' => $upload_path, |
| 800 |
'message' => esc_html__('File Uploaded Successfully', 'media-cloud-sync') |
| 801 |
); |
| 802 |
} else { |
| 803 |
$result = array( |
| 804 |
'success' => false, |
| 805 |
'code' => 200, |
| 806 |
'message' => esc_html__('Something happened while uploading to server', 'media-cloud-sync') |
| 807 |
); |
| 808 |
} |
| 809 |
} catch (MultipartUploadException $e) { |
| 810 |
$result = array( |
| 811 |
'success' => false, |
| 812 |
'code' => 200, |
| 813 |
'message' => $e->getMessage() |
| 814 |
); |
| 815 |
} |
| 816 |
} |
| 817 |
} else { |
| 818 |
$result = array( |
| 819 |
'success' => false, |
| 820 |
'code' => 200, |
| 821 |
'message' => esc_html__('Check the file you are trying to upload. Please try again', 'media-cloud-sync') |
| 822 |
); |
| 823 |
} |
| 824 |
} else { |
| 825 |
$result = array( |
| 826 |
'success' => false, |
| 827 |
'code' => 200, |
| 828 |
'message' => esc_html__('Insufficient Data. Please try again', 'media-cloud-sync') |
| 829 |
); |
| 830 |
} |
| 831 |
return $result; |
| 832 |
} |
| 833 |
|
| 834 |
/** |
| 835 |
* Save object to server |
| 836 |
* @since 1.0.0 |
| 837 |
*/ |
| 838 |
public function object_to_server($key, $save_path) { |
| 839 |
try { |
| 840 |
$getObject = $this->s3Client->GetObject([ |
| 841 |
'Bucket' => $this->bucket_name, |
| 842 |
'Key' => $key, |
| 843 |
'SaveAs' => $save_path |
| 844 |
]); |
| 845 |
if (file_exists($save_path)) { |
| 846 |
return true; |
| 847 |
} |
| 848 |
} catch (AwsException $e) { |
| 849 |
return false; |
| 850 |
} |
| 851 |
return false; |
| 852 |
} |
| 853 |
|
| 854 |
|
| 855 |
/** |
| 856 |
* Delete Single |
| 857 |
* @since 1.0.0 |
| 858 |
* @return boolean |
| 859 |
*/ |
| 860 |
public function deleteSingle($key) { |
| 861 |
$result = array(); |
| 862 |
if (isset($key) && !empty($key)) { |
| 863 |
try { |
| 864 |
$this->s3Client->deleteObject([ |
| 865 |
'Bucket' => $this->bucket_name, |
| 866 |
'Key' => $key |
| 867 |
]); |
| 868 |
|
| 869 |
if (!$this->s3Client->doesObjectExist($this->bucket_name, $key)) { |
| 870 |
$result = array( |
| 871 |
'success' => true, |
| 872 |
'code' => 200, |
| 873 |
'message' => esc_html__('Deleted Successfully', 'media-cloud-sync') |
| 874 |
); |
| 875 |
} else { |
| 876 |
$result = array( |
| 877 |
'success' => false, |
| 878 |
'code' => 200, |
| 879 |
'message' => esc_html__('File not deleted', 'media-cloud-sync') |
| 880 |
); |
| 881 |
} |
| 882 |
} catch (AwsException $e) { |
| 883 |
$result = array( |
| 884 |
'success' => false, |
| 885 |
'code' => 200, |
| 886 |
'message' => $e->getMessage() |
| 887 |
); |
| 888 |
} |
| 889 |
} else { |
| 890 |
$result = array( |
| 891 |
'success' => false, |
| 892 |
'code' => 200, |
| 893 |
'message' => esc_html__('Insufficient Data. Please try again', 'media-cloud-sync') |
| 894 |
); |
| 895 |
} |
| 896 |
return $result; |
| 897 |
} |
| 898 |
|
| 899 |
/** |
| 900 |
* get presigned URL |
| 901 |
* @since 1.0.0 |
| 902 |
* @return boolean |
| 903 |
*/ |
| 904 |
public function get_presigned_url($key) { |
| 905 |
$result = array(); |
| 906 |
if (isset($key) && !empty($key)) { |
| 907 |
try { |
| 908 |
$cmd = $this->s3Client->getCommand('GetObject', [ |
| 909 |
'Bucket' => $this->bucket_name, |
| 910 |
'Key' => $key |
| 911 |
]); |
| 912 |
|
| 913 |
$expires = isset($this->settings['presigned_expire']) ? $this->settings['presigned_expire'] : 20; |
| 914 |
|
| 915 |
$request = $this->s3Client->createPresignedRequest($cmd, sprintf('+%s minutes', $expires)); |
| 916 |
|
| 917 |
if ($presignedUrl = (string)$request->getUri()) { |
| 918 |
$result = array( |
| 919 |
'success' => true, |
| 920 |
'code' => 200, |
| 921 |
'file_url' => $presignedUrl, |
| 922 |
'message' => esc_html__('Got Presigned URL Successfully', 'media-cloud-sync') |
| 923 |
); |
| 924 |
} else { |
| 925 |
$result = array( |
| 926 |
'success' => false, |
| 927 |
'code' => 200, |
| 928 |
'message' => esc_html__('Error getting presigned URL', 'media-cloud-sync') |
| 929 |
); |
| 930 |
} |
| 931 |
} catch (AwsException $e) { |
| 932 |
$result = array( |
| 933 |
'success' => false, |
| 934 |
'code' => 200, |
| 935 |
'message' => $e->getMessage() |
| 936 |
); |
| 937 |
} |
| 938 |
} else { |
| 939 |
$result = array( |
| 940 |
'success' => false, |
| 941 |
'code' => 200, |
| 942 |
'message' => esc_html__('Insufficient Data. Please try again', 'media-cloud-sync') |
| 943 |
); |
| 944 |
} |
| 945 |
return $result; |
| 946 |
} |
| 947 |
|
| 948 |
/** |
| 949 |
* Generate file URL |
| 950 |
*/ |
| 951 |
private function generate_file_url($key){ |
| 952 |
$domain = $this->get_domain(); |
| 953 |
|
| 954 |
return apply_filters('wpmcs_generate_s3_file_url', |
| 955 |
$domain . '/' . $key, |
| 956 |
$domain, $key |
| 957 |
); |
| 958 |
} |
| 959 |
|
| 960 |
/** |
| 961 |
* Get domain URL |
| 962 |
*/ |
| 963 |
public function get_domain() { |
| 964 |
$region = isset($this->config['region']) ? $this->config['region'] : ''; |
| 965 |
return "https://{$this->bucket_name}.s3.{$region}.amazonaws.com"; |
| 966 |
} |
| 967 |
|
| 968 |
} |