PluginProbe
FluentCart A New Era of eCommerce – Faster, Lighter, and Simpler / 1.6.4
FluentCart A New Era of eCommerce – Faster, Lighter, and Simpler v1.6.4
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 1.2.0 All 47 releases
fluent-cart / app / Modules / StorageDrivers / S3 / S3Settings.php

S3Settings.php in FluentCart A New Era of eCommerce – Faster, Lighter, and Simpler 1.6.4, at app/Modules/StorageDrivers/S3/S3Settings.php

201 lines 6.3 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\Modules\StorageDrivers\S3;
4
5 use FluentCart\App\Helpers\Helper;
6 use FluentCart\Framework\Support\Arr;
7
8 class S3Settings
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.';
11
12 protected $settings;
13
14 protected $driverHandler = 'fluent_cart_storage_settings_s3';
15
16 static ?array $cachedSettings = null;
17
18 public bool $isUsingDefineMode = false;
19
20 public function __construct()
21 {
22 $this->isUsingDefineMode = static::hasDefinedCredentials();
23
24 if (self::$cachedSettings) {
25 $this->settings = self::$cachedSettings;
26 return;
27 }
28
29 $settings = fluent_cart_get_option($this->driverHandler, []);
30 $hasStoredAuthMethod = !empty(Arr::get($settings, 'auth_method'));
31
32 $this->settings = wp_parse_args($settings, static::getDefaults());
33
34 if (!$hasStoredAuthMethod && $this->isUsingDefineMode) {
35 $this->settings['auth_method'] = 'define';
36 }
37
38 $this->settings['defined_in_wp_config'] = $this->isUsingDefineMode;
39 $this->applyDefineCredentialsState();
40
41 self::$cachedSettings = $this->settings;
42 }
43
44 public static function getDefaults()
45 {
46 return [
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' => '',
57 ];
58 }
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
132 public function isActive()
133 {
134 $settings = $this->get();
135
136 $isActive = Arr::get($settings, 'is_active') === 'yes';
137
138 if ($isActive) {
139 if ($this->getAuthMethod() === 'define') {
140 return static::hasDefinedCredentials();
141 }
142
143 $requiredKeys = ['secret_key', 'access_key', 'region'];
144
145 return $this->hasRequiredKeys($settings, $requiredKeys);
146 }
147
148 return false;
149 }
150
151 public function getAuthMethod()
152 {
153 return Arr::get($this->settings, 'auth_method', 'db');
154 }
155
156 private function hasRequiredKeys(array $settings, array $requiredKeys): bool
157 {
158 foreach ($requiredKeys as $key) {
159 if (empty(Arr::get($settings, $key))) {
160 return false;
161 }
162 }
163
164 return true;
165 }
166
167 public static function hasDefinedCredentials(): bool
168 {
169 return !empty(static::getDefinedCredentials());
170 }
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 }
201