PluginProbe
FluentCart A New Era of eCommerce – Faster, Lighter, and Simpler / 1.6.6
FluentCart A New Era of eCommerce – Faster, Lighter, and Simpler v1.6.6
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
← All changes | app/Modules/StorageDrivers/S3/S3Settings.php +134 -15 1.3.20 → 1.6.6 View file →
@@ -1,12 +1,14 @@
1 1 <?php
2 2
3 3 namespace FluentCart\App\Modules\StorageDrivers\S3;
4 4
5 +use FluentCart\App\Helpers\Helper;
5 6 use FluentCart\Framework\Support\Arr;
6 7
7 8 class S3Settings
8 9 {
10 + protected const MISSING_DEFINE_CREDENTIALS_MESSAGE = 'S3 was configured previously, but the wp-config.php credentials are missing now. Add them again to continue.';
9 11
10 12 protected $settings;
11 13
12 14 protected $driverHandler = 'fluent_cart_storage_settings_s3';
@@ -16,25 +18,27 @@
16 18 public bool $isUsingDefineMode = false;
17 19
18 20 public function __construct()
19 21 {
22 + $this->isUsingDefineMode = static::hasDefinedCredentials();
23 +
20 24 if (self::$cachedSettings) {
21 25 $this->settings = self::$cachedSettings;
22 26 return;
23 27 }
28 +
24 29 $settings = fluent_cart_get_option($this->driverHandler, []);
30 + $hasStoredAuthMethod = !empty(Arr::get($settings, 'auth_method'));
25 31
26 - $this->isUsingDefineMode = defined('FCT_S3_ACCESS_KEY') && defined('FCT_S3_SECRET_KEY');
27 -
28 32 $this->settings = wp_parse_args($settings, static::getDefaults());
29 33
30 - if ($this->isUsingDefineMode) {
31 - $this->settings['is_using_define_mode'] = true;
32 - $this->settings['access_key'] = FCT_S3_ACCESS_KEY;
33 - $this->settings['secret_key'] = FCT_S3_SECRET_KEY;
34 + if (!$hasStoredAuthMethod && $this->isUsingDefineMode) {
35 + $this->settings['auth_method'] = 'define';
34 36 }
35 37
36 - $settings['region'] = 'us-east-1';
38 + $this->settings['defined_in_wp_config'] = $this->isUsingDefineMode;
39 + $this->applyDefineCredentialsState();
40 +
37 41 self::$cachedSettings = $this->settings;
38 42 }
39 43
40 44 public static function getDefaults()
@@ -39,16 +43,93 @@
39 43
40 44 public static function getDefaults()
41 45 {
42 46 return [
43 - 'is_active' => 'no',
44 - 'secret_key' => '',
45 - 'access_key' => '',
46 - 'bucket' => '',
47 - 'region' => 'us-east-1',
47 + 'is_active' => 'no',
48 + 'auth_method' => 'db',
49 + 'secret_key' => '',
50 + 'access_key' => '',
51 + 'bucket' => '',
52 + 'region' => 'us-east-1',
53 + 'block_public_access' => 'yes',
54 + 'object_ownership' => 'yes',
55 + 'define_credentials_missing' => false,
56 + 'define_credentials_missing_message' => '',
48 57 ];
49 58 }
50 59
60 + public function get($key = '')
61 + {
62 + $settings = $this->settings;
63 + $authMethod = Arr::get($settings, 'auth_method', 'db');
64 +
65 + if ($authMethod === 'define') {
66 + $definedCredentials = static::getDefinedCredentials();
67 +
68 + if ($definedCredentials) {
69 + $settings['access_key'] = Arr::get($definedCredentials, 'access_key', '');
70 + $settings['secret_key'] = Arr::get($definedCredentials, 'secret_key', '');
71 + $settings['provider'] = Arr::get($definedCredentials, 'provider', 'aws');
72 + }
73 + } else {
74 + if (!empty($settings['access_key'])) {
75 + $settings['access_key'] = Helper::decryptKey($settings['access_key']) ?: $settings['access_key'];
76 + }
77 +
78 + if (!empty($settings['secret_key'])) {
79 + $settings['secret_key'] = Helper::decryptKey($settings['secret_key']) ?: $settings['secret_key'];
80 + }
81 + }
82 +
83 + if ($key) {
84 + return Arr::get($settings, $key);
85 + }
86 +
87 + return $settings;
88 + }
89 +
90 + public static function resolveConfiguredBucket(array $settings): string
91 + {
92 + return trim((string) Arr::get($settings, 'bucket', ''));
93 + }
94 +
95 + public static function resolveEffectiveBucket(array $settings): string
96 + {
97 + $bucket = static::resolveConfiguredBucket($settings);
98 + if ($bucket !== '') {
99 + return $bucket;
100 + }
101 +
102 + $legacyBuckets = Arr::get($settings, 'buckets', []);
103 + if (!is_array($legacyBuckets)) {
104 + return '';
105 + }
106 +
107 + foreach ($legacyBuckets as $legacyBucket) {
108 + $legacyBucket = trim((string) $legacyBucket);
109 + if ($legacyBucket !== '') {
110 + return $legacyBucket;
111 + }
112 + }
113 +
114 + return '';
115 + }
116 +
117 + protected function applyDefineCredentialsState(): void
118 + {
119 + $isDefineWithoutCredentials = Arr::get($this->settings, 'auth_method') === 'define' && !$this->isUsingDefineMode;
120 + $hadPreviousDefineContext = !empty(Arr::get($this->settings, 'bucket')) || Arr::get($this->settings, 'is_active') === 'yes';
121 +
122 + $this->settings['define_credentials_missing'] = $isDefineWithoutCredentials && $hadPreviousDefineContext;
123 + $this->settings['define_credentials_missing_message'] = $this->settings['define_credentials_missing']
124 + ? __(self::MISSING_DEFINE_CREDENTIALS_MESSAGE, 'fluent-cart')
125 + : '';
126 +
127 + if ($this->settings['define_credentials_missing']) {
128 + $this->settings['is_active'] = 'no';
129 + }
130 + }
131 +
51 132 public function isActive()
52 133 {
53 134 $settings = $this->get();
54 135
@@ -54,8 +135,12 @@
54 135
55 136 $isActive = Arr::get($settings, 'is_active') === 'yes';
56 137
57 138 if ($isActive) {
139 + if ($this->getAuthMethod() === 'define') {
140 + return static::hasDefinedCredentials();
141 + }
142 +
58 143 $requiredKeys = ['secret_key', 'access_key', 'region'];
59 144
60 145 return $this->hasRequiredKeys($settings, $requiredKeys);
61 146 }
@@ -62,8 +147,13 @@
62 147
63 148 return false;
64 149 }
65 150
151 + public function getAuthMethod()
152 + {
153 + return Arr::get($this->settings, 'auth_method', 'db');
154 + }
155 +
66 156 private function hasRequiredKeys(array $settings, array $requiredKeys): bool
67 157 {
68 158 foreach ($requiredKeys as $key) {
69 159 if (empty(Arr::get($settings, $key))) {
@@ -73,9 +163,38 @@
73 163
74 164 return true;
75 165 }
76 166
77 - public function get($key = '')
167 + public static function hasDefinedCredentials(): bool
78 168 {
79 - return $this->settings;
169 + return !empty(static::getDefinedCredentials());
80 170 }
81 -}
171 +
172 + public static function getDefinedCredentials(): array
173 + {
174 + if (defined('FCT_S3_ACCESS_KEY') && defined('FCT_S3_SECRET_KEY')) {
175 + return [
176 + 'access_key' => FCT_S3_ACCESS_KEY,
177 + 'secret_key' => FCT_S3_SECRET_KEY,
178 + 'provider' => 'aws'
179 + ];
180 + }
181 +
182 + if (defined('FLUENT_CART_AWS_ACCESS_KEY_ID') && defined('FLUENT_CART_AWS_SECRET_ACCESS_KEY')) {
183 + return [
184 + 'access_key' => FLUENT_CART_AWS_ACCESS_KEY_ID,
185 + 'secret_key' => FLUENT_CART_AWS_SECRET_ACCESS_KEY,
186 + 'provider' => 'aws'
187 + ];
188 + }
189 +
190 + if (defined('AWS_ACCESS_KEY_ID') && defined('AWS_SECRET_ACCESS_KEY')) {
191 + return [
192 + 'access_key' => AWS_ACCESS_KEY_ID,
193 + 'secret_key' => AWS_SECRET_ACCESS_KEY,
194 + 'provider' => 'aws'
195 + ];
196 + }
197 +
198 + return [];
199 + }
200 +}