PluginProbe
FluentCart A New Era of eCommerce – Faster, Lighter, and Simpler / 1.3.21
FluentCart A New Era of eCommerce – Faster, Lighter, and Simpler v1.3.21
1.6.6 1.6.5 1.6.4 1.6.3 1.6.2 1.6.1 1.6.0 1.5.4 1.5.5 1.5.3 1.5.2 1.5.1 1.5.0 1.4.2 1.4.1 1.4.0 1.3.28 1.3.27 1.3.26 1.3.25 1.3.23 1.3.22 1.3.21 1.3.20 1.3.19 All 49 releases
fluent-cart / app / Services / FileSystem / Drivers / S3 / S3FileDeleter.php

S3FileDeleter.php in FluentCart A New Era of eCommerce – Faster, Lighter, and Simpler 1.3.21, at app/Services/FileSystem/Drivers/S3/S3FileDeleter.php

129 lines 4.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace FluentCart\App\Services\FileSystem\Drivers\S3;
4
5 use Exception;
6 use FluentCart\App\Modules\StorageDrivers\S3\S3;
7
8 class S3FileDeleter
9 {
10 private string $accessKey;
11 private string $secretKey;
12 private string $bucket;
13 private string $region;
14 private string $hashAlgorithm = 'sha256';
15 private string $httpMethod;
16 private string $s3FilePath;
17 private string $signature;
18 private string $requestUrl;
19 private $timeStamp;
20 private $date;
21
22 public function __construct(string $secret, string $accessKey, string $bucket, string $region, string $s3FilePath)
23 {
24 $this->secretKey = $secret;
25 $this->accessKey = $accessKey;
26 $this->bucket = $bucket;
27 $this->region = S3::getBucketRegion($bucket);
28 $this->s3FilePath = $s3FilePath;
29 $this->httpMethod = "DELETE";
30 $this->timeStamp = gmdate('Ymd\THis\Z');
31 $this->date = substr($this->timeStamp, 0, 8);
32 $hasDot = strpos($this->bucket, '.') !== false;
33 if ($hasDot) {
34 $this->requestUrl = "https://s3.{$this->region}.amazonaws.com/{$this->bucket}/{$this->s3FilePath}";
35 } else {
36 $this->requestUrl = "https://{$this->bucket}.s3.{$this->region}.amazonaws.com/{$this->s3FilePath}";
37 }
38 $this->signature = $this->generateSignature();
39 }
40
41 public static function delete(string $secret, string $accessKey, string $bucket, string $region, string $s3FilePath)
42 {
43 return (new static($secret, $accessKey, $bucket, $region, $s3FilePath))->deleteFile();
44 }
45
46 public function deleteFile()
47 {
48 add_filter('http_request_timeout', function () {
49 return 30;
50 });
51
52 $response = wp_remote_request($this->requestUrl, [
53 'method' => $this->httpMethod,
54 'headers' => $this->getHeaders()
55 ]);
56
57 $responseCode = wp_remote_retrieve_response_code($response);
58
59 if ($responseCode == 204) {
60 return [
61 'message' => __('File Deleted Successfully', 'fluent-cart'),
62 'driver' => 's3',
63 'path' => $this->s3FilePath
64 ];
65 } else {
66 return new \WP_Error($responseCode, __('Failed To Delete File', 'fluent-cart'));
67 }
68 }
69
70 private function generateSignature()
71 {
72 return hash_hmac(
73 $this->hashAlgorithm,
74 $this->createStringToSign(),
75 $this->getSigningKey()
76 );
77 }
78
79 private function createStringToSign(): string
80 {
81 $hash = hash($this->hashAlgorithm, $this->createCanonicalUrl());
82 return "AWS4-HMAC-SHA256\n{$this->timeStamp}\n{$this->getScope()}\n{$hash}";
83 }
84
85 private function createCanonicalUrl(): string
86 {
87 $s3FilePath = '/' . ltrim($this->s3FilePath, '/');
88 $hasDot = strpos($this->bucket, '.') !== false;
89 $canonicalUri = $hasDot ? '/' . $this->bucket . $s3FilePath : $s3FilePath;
90
91 return "{$this->httpMethod}\n{$canonicalUri}\n\nhost:{$this->getHost()}\nx-amz-content-sha256:{$this->getContentHash()}\nx-amz-date:{$this->timeStamp}\n\nhost;x-amz-content-sha256;x-amz-date\n{$this->getContentHash()}";
92 }
93
94 private function getHost(): string
95 {
96 if (strpos($this->bucket, '.') !== false) {
97 return "s3.{$this->region}.amazonaws.com";
98 }
99
100 return "{$this->bucket}.s3.{$this->region}.amazonaws.com";
101 }
102
103 private function getContentHash(): string
104 {
105 return hash($this->hashAlgorithm, "");
106 }
107
108 private function getScope(): string
109 {
110 return "{$this->date}/{$this->region}/s3/aws4_request";
111 }
112
113 private function getSigningKey()
114 {
115 $dateKey = hash_hmac($this->hashAlgorithm, $this->date, "AWS4{$this->secretKey}", true);
116 $regionKey = hash_hmac($this->hashAlgorithm, $this->region, $dateKey, true);
117 $serviceKey = hash_hmac($this->hashAlgorithm, 's3', $regionKey, true);
118 return hash_hmac($this->hashAlgorithm, 'aws4_request', $serviceKey, true);
119 }
120
121 private function getHeaders(): array
122 {
123 return [
124 'x-amz-content-sha256' => $this->getContentHash(),
125 'x-amz-date' => $this->timeStamp,
126 'Authorization' => "AWS4-HMAC-SHA256 Credential={$this->accessKey}/{$this->date}/{$this->region}/s3/aws4_request, SignedHeaders=host;x-amz-content-sha256;x-amz-date, Signature={$this->signature}"
127 ];
128 }
129 }