PluginProbe
FluentCart A New Era of eCommerce – Faster, Lighter, and Simpler / 1.3.20
FluentCart A New Era of eCommerce – Faster, Lighter, and Simpler v1.3.20
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 trunk All 48 releases
fluent-cart / app / Services / FileSystem / Drivers / S3 / S3BucketList.php

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

147 lines 4.2 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 FluentCart\Framework\Support\Arr;
6
7 class S3BucketList
8 {
9 private string $accessKey;
10 private string $secretKey;
11 private string $region;
12 private string $hashAlgorithm = 'sha256';
13 private string $httpMethod;
14 private string $signature;
15 private $timeStamp;
16 private $date;
17 private string $requestUrl;
18
19
20 public static function get(string $secret, string $accessKey, string $region)
21 {
22 return (new static($secret, $accessKey, $region))->getList();
23 }
24
25 public function __construct(string $secret, string $accessKey, string $region)
26 {
27 $this->secretKey = $secret;
28 $this->accessKey = $accessKey;
29 $this->region = $region;
30
31 $this->httpMethod = "GET";
32 $this->timeStamp = gmdate('Ymd\THis\Z');
33 $this->date = substr($this->timeStamp, 0, 8);
34 $this->requestUrl = "https://s3.amazonaws.com";
35 $this->generateSignature();
36 }
37
38 public function getList()
39 {
40 add_filter('http_request_timeout', function () {
41 return 30; // Set the timeout to 30 seconds (or adjust as needed)
42 });
43
44 $response = wp_remote_request($this->requestUrl, [
45 'method' => $this->httpMethod,
46 'headers' => $this->getHeaders()
47 ]);
48
49
50 $responseCode = wp_remote_retrieve_response_code($response);
51
52 if ($responseCode == '200') {
53 return $this->parseResponseXml($response);
54 } else {
55 return new \WP_Error(
56 $responseCode,
57 __('Invalid Credential', 'fluent-cart')
58 );
59 }
60 }
61
62 private function parseResponseXml($response): array
63 {
64 $xml = simplexml_load_string(wp_remote_retrieve_body($response));
65 $array = json_decode(json_encode($xml), TRUE);
66
67 $responseBucket = Arr::get($array, 'Buckets.Bucket', []);
68
69 if(Arr::has($responseBucket,'Name')){
70 return [
71 Arr::get($responseBucket, 'Name')
72 ];
73 }
74
75 $buckets = [];
76 foreach ($responseBucket as $bucket) {
77 $buckets[] = Arr::get($bucket, 'Name');
78 }
79 return $buckets;
80 }
81
82 public function getSignature(): string
83 {
84 return $this->signature;
85 }
86
87 public function generateSignature()
88 {
89 $this->signature = $this->generateSignatureKey();
90 }
91
92 private function createScope(): string
93 {
94 return "{$this->date}/{$this->region}/s3/aws4_request";
95 }
96
97 private function getContentHash(): string
98 {
99 return hash($this->hashAlgorithm, "");
100 }
101
102 private function createCanonicalUrl(): string
103 {
104 return "$this->httpMethod\n" .
105 "/\n\n" .
106 "host:{$this->getHost()}\n" .
107 "x-amz-content-sha256:{$this->getContentHash()}\n" .
108 "x-amz-date:{$this->timeStamp}\n\n" .
109 "host;x-amz-content-sha256;x-amz-date\n" .
110 "{$this->getContentHash()}";
111
112 }
113
114 private function getHost(): string
115 {
116 return "s3.amazonaws.com";
117 }
118
119 private function createStringToSign(): string
120 {
121 $hash = hash($this->hashAlgorithm, $this->createCanonicalUrl());
122 return "AWS4-HMAC-SHA256\n{$this->timeStamp}\n{$this->createScope()}\n{$hash}";
123 }
124
125 private function getSigningKey()
126 {
127 $dateKey = hash_hmac($this->hashAlgorithm, $this->date, "AWS4{$this->secretKey}", true);
128 $regionKey = hash_hmac($this->hashAlgorithm, $this->region, $dateKey, true);
129 $serviceKey = hash_hmac($this->hashAlgorithm, 's3', $regionKey, true);
130 return hash_hmac($this->hashAlgorithm, 'aws4_request', $serviceKey, true);
131 }
132
133 private function generateSignatureKey()
134 {
135 return hash_hmac($this->hashAlgorithm, $this->createStringToSign(), $this->getSigningKey());
136 }
137
138 public function getHeaders(): array
139 {
140 return [
141 "x-amz-content-sha256" => $this->getContentHash(),
142 'x-amz-date' => $this->timeStamp,
143 '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->getSignature()}"
144 ];
145 }
146 }
147