PluginProbe
Media Cloud Sync / 1.2.13
Media Cloud Sync v1.2.13
1.4.0 1.3.12 1.3.11 1.3.10 trunk 1.0.0 1.0.1 1.0.2 1.0.3 1.1.0 1.1.1 1.2.0 1.2.10 1.2.11 1.2.12 1.2.13 1.2.2 1.2.3 1.2.4 1.2.5 1.2.6 1.2.7 1.2.8 1.2.9 1.3.0 All 34 releases
media-cloud-sync / includes / base / service.php

service.php in Media Cloud Sync 1.2.13, at includes/base/service.php

864 lines 40.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 namespace Dudlewebs\WPMCS;
3
4 defined('ABSPATH') || exit;
5
6 class Service {
7 private static $instance = null;
8 private $assets_url;
9 private $version;
10 private $token;
11 private $service = false;
12
13 protected $settings;
14
15
16 /**
17 * Admin constructor.
18 * @since 1.0.0
19 */
20 public function __construct() {
21 $this->assets_url = WPMCS_ASSETS_URL;
22 $this->version = WPMCS_VERSION;
23 $this->token = WPMCS_TOKEN;
24
25 $this->settings = Utils::get_settings();
26
27 $current_service = Utils::get_service();
28 switch ($current_service) {
29 case 's3':
30 $this->service = new S3;
31 break;
32 case 'gcloud':
33 $this->service = new GCloud;
34 break;
35 case 'docean':
36 $this->service = new DOcean;
37 break;
38 case 's3compatible':
39 $this->service = new S3Compatible;
40 break;
41 default:$this->service = false;
42 }
43 }
44
45 /**
46 * Verify Service Credentials
47 * @since 1.0.0
48 */
49 public function verifyCredentials($data) {
50 $result = [
51 'success' => false,
52 'message' => esc_html__('Something went wrong', 'media-cloud-sync')
53 ];
54
55 $service = isset($data['service'])? $data['service'] : false;
56
57 if($service == false) {
58 $result = [
59 'success' => false,
60 'message' => esc_html__('No service selected', 'media-cloud-sync')
61 ];
62 return $result;
63 }
64
65 switch($service){
66 case 's3':
67 $config = isset($data['config']) ? $data['config'] : [];
68 $access_key = isset($config['access_key']) ? $config['access_key'] : false;
69 $secret_key = isset($config['secret_key']) ? $config['secret_key'] : false;
70 $region = isset($config['region']) ? $config['region'] : false;
71
72 if($access_key==false || $secret_key==false || $region==false) {
73 $result = [
74 'success' => false,
75 'message' => esc_html__('Insufficient Data. Please try again', 'media-cloud-sync')
76 ];
77 return $result;
78 }
79
80 $currentService = new S3;
81 return $currentService->verifyCredentials($access_key, $secret_key, $region);
82 break;
83 case 'gcloud':
84 $config = isset($data['config']) ? $data['config'] : [];
85 $config_json = isset($config['config_json']) ? $config['config_json'] : '';
86
87 if(!(isset($config_json) && !empty($config_json))) {
88 $result = [
89 'success' => false,
90 'message' => esc_html__('Insufficient Data. Please try again', 'media-cloud-sync')
91 ];
92 return $result;
93 }
94
95 if(!Utils::is_json($config_json)) {
96 $result = [
97 'success' => false,
98 'message' => esc_html__('Invalid JSON. Please try again', 'media-cloud-sync')
99 ];
100 return $result;
101 }
102
103 $currentService = new GCloud;
104 return $currentService->verifyCredentials($config_json);
105 break;
106 case 'docean':
107 $config = isset($data['config']) ? $data['config'] : [];
108 $access_key = isset($config['access_key']) ? $config['access_key'] : false;
109 $secret_key = isset($config['secret_key']) ? $config['secret_key'] : false;
110 $region = isset($config['region']) ? $config['region'] : false;
111
112 if($access_key==false || $secret_key==false || $region==false) {
113 $result = [
114 'success' => false,
115 'message' => esc_html__('Insufficient Data. Please try again', 'media-cloud-sync')
116 ];
117 return $result;
118 }
119
120 $currentService = new DOcean;
121 return $currentService->verifyCredentials($access_key, $secret_key, $region);
122 break;
123 case 's3compatible':
124 $config = isset($data['config']) ? $data['config'] : [];
125 $access_key = isset($config['access_key']) ? $config['access_key'] : false;
126 $secret_key = isset($config['secret_key']) ? $config['secret_key'] : false;
127 $region = isset($config['region']) ? $config['region'] : false;
128 $endpoint = isset($config['endpoint']) ? $config['endpoint'] : false;
129
130 if($access_key==false || $secret_key==false || $endpoint==false) {
131 $result = [
132 'success' => false,
133 'message' => esc_html__('Insufficient Data. Please try again', 'media-cloud-sync')
134 ];
135 return $result;
136 }
137
138 $currentService = new S3Compatible;
139 return $currentService->verifyCredentials($endpoint, $access_key, $secret_key, $region);
140 break;
141 default:
142 $result = [
143 'success' => false,
144 'message' => esc_html__('Invalid service', 'media-cloud-sync')
145 ];
146 }
147
148 return $result;
149 }
150
151 /**
152 * Verify Bucket Exist
153 * @since 1.0.0
154 */
155 public function verifyBucketExist($data) {
156 $result = [
157 'success' => false,
158 'message' => esc_html__('Something went wrong', 'media-cloud-sync')
159 ];
160
161 $service = isset($data['service'])? $data['service'] : false;
162
163 if($service == false) {
164 $result = [
165 'success' => false,
166 'message' => esc_html__('No service selected', 'media-cloud-sync')
167 ];
168 return $result;
169 }
170
171 switch($service){
172 case 's3':
173 $result = [
174 'success' => false,
175 'message' => esc_html__('Insufficient Data. Please try again', 'media-cloud-sync')
176 ];
177 $config = isset($data['config']) ? $data['config'] : [];
178 $bucketData = isset($data['bucketData']) ? $data['bucketData'] : [];
179 $bucketConfig = isset($bucketData['config']) ? $bucketData['config'] : [];
180 $bucket = isset($bucketConfig['bucket_name']) ? $bucketConfig['bucket_name'] : false;
181 $transfer_acceleration = isset($bucketConfig['transfer_acceleration']) ? $bucketConfig['transfer_acceleration'] : false;
182 $access_key = isset($config['access_key']) ? $config['access_key'] : false;
183 $secret_key = isset($config['secret_key']) ? $config['secret_key'] : false;
184 $region = isset($config['region']) ? $config['region'] : false;
185
186 if( $access_key==false || $secret_key==false || $region==false || $bucket==false ) return $result;
187
188 $currentService = new S3;
189
190
191 return $currentService->verifyBucketExist($access_key, $secret_key, $region, $bucket, $transfer_acceleration);
192
193 case 'gcloud':
194 $result = [
195 'success' => false,
196 'message' => esc_html__('Insufficient Data. Please try again', 'media-cloud-sync')
197 ];
198 $config = isset($data['config']) ? $data['config'] : [];
199 $config_json = isset($config['config_json']) ? $config['config_json'] : '';
200
201 $bucketData = isset($data['bucketData']) ? $data['bucketData'] : [];
202 $bucketConfig = isset($bucketData['config']) ? $bucketData['config'] : [];
203 $bucket = isset($bucketConfig['bucket_name']) ? $bucketConfig['bucket_name'] : false;
204
205 if( !(isset($config_json) && !empty($config_json) && $bucket!=false ) ) return $result;
206
207 if(!Utils::is_json($config_json)) {
208 $result = [
209 'success' => false,
210 'message' => esc_html__('Invalid config file', 'media-cloud-sync')
211 ];
212 return $result;
213 };
214
215 $currentService = new GCloud;
216 return $currentService->verifyBucketExist($config_json, $bucket);
217 break;
218 case 'docean':
219 $result = [
220 'success' => false,
221 'message' => esc_html__('Insufficient Data. Please try again', 'media-cloud-sync')
222 ];
223 $config = isset($data['config']) ? $data['config'] : [];
224 $bucketData = isset($data['bucketData']) ? $data['bucketData'] : [];
225 $bucketConfig = isset($bucketData['config']) ? $bucketData['config'] : [];
226 $bucket = isset($bucketConfig['bucket_name']) ? $bucketConfig['bucket_name'] : false;
227 $access_key = isset($config['access_key']) ? $config['access_key'] : false;
228 $secret_key = isset($config['secret_key']) ? $config['secret_key'] : false;
229 $region = isset($config['region']) ? $config['region'] : false;
230
231 if( $access_key==false || $secret_key==false || $region==false || $bucket==false ) return $result;
232
233 $currentService = new DOcean;
234
235
236 return $currentService->verifyBucketExist($access_key, $secret_key, $region, $bucket);
237 break;
238 case 's3compatible':
239 $result = [
240 'success' => false,
241 'message' => esc_html__('Insufficient Data. Please try again', 'media-cloud-sync')
242 ];
243 $config = isset($data['config']) ? $data['config'] : [];
244 $bucketData = isset($data['bucketData']) ? $data['bucketData'] : [];
245 $bucketConfig = isset($bucketData['config']) ? $bucketData['config'] : [];
246 $bucket = isset($bucketConfig['bucket_name']) ? $bucketConfig['bucket_name'] : false;
247 $access_key = isset($config['access_key']) ? $config['access_key'] : false;
248 $secret_key = isset($config['secret_key']) ? $config['secret_key'] : false;
249 $region = isset($config['region']) ? $config['region'] : false;
250 $endpoint = isset($config['endpoint']) ? $config['endpoint'] : false;
251
252 if( $access_key==false || $secret_key==false || $bucket==false || $endpoint==false ) return $result;
253
254 $currentService = new S3Compatible;
255
256 return $currentService->verifyBucketExist($endpoint, $access_key, $secret_key, $bucket, $region);
257 break;
258 default:
259 $result = [
260 'success' => false,
261 'message' => esc_html__('Invalid service', 'media-cloud-sync')
262 ];
263 }
264
265 return $result;
266 }
267
268 /**
269 * Verify Bucket Credentials
270 * @since 1.0.0
271 */
272 public function createBucket($data) {
273 $result = [
274 'success' => false,
275 'message' => esc_html__('Something went wrong', 'media-cloud-sync')
276 ];
277
278 $service = isset($data['service'])? $data['service'] : false;
279
280 if($service == false) {
281 $result = [
282 'success' => false,
283 'message' => esc_html__('No service selected', 'media-cloud-sync')
284 ];
285 return $result;
286 }
287
288 switch($service){
289 case 's3':
290 $result = [
291 'success' => false,
292 'message' => esc_html__('Insufficient Data. Please try again', 'media-cloud-sync')
293 ];
294 $config = isset($data['config']) ? $data['config'] : [];
295 $bucketData = isset($data['bucketData']) ? $data['bucketData'] : [];
296 $bucketAddNewConfig = isset($bucketData['addNewConfig']) ? $bucketData['addNewConfig'] : [];
297 $bucket = isset($bucketAddNewConfig['bucket_name']) ? $bucketAddNewConfig['bucket_name'] : false;
298 $transfer_acceleration = isset($bucketAddNewConfig['transfer_acceleration']) ? $bucketAddNewConfig['transfer_acceleration'] : false;
299 $access_key = isset($config['access_key']) ? $config['access_key'] : false;
300 $secret_key = isset($config['secret_key']) ? $config['secret_key'] : false;
301 $region = isset($config['region']) ? $config['region'] : false;
302
303 if( $access_key==false || $secret_key==false || $region==false || $bucket==false ) return $result;
304
305 $currentService = new S3;
306 return $currentService->createBucket($access_key, $secret_key, $region, $bucket, $transfer_acceleration);
307 case 'gcloud':
308 $result = [
309 'success' => false,
310 'message' => esc_html__('Insufficient Data. Please try again', 'media-cloud-sync')
311 ];
312 $config = isset($data['config']) ? $data['config'] : [];
313 $config_json = isset($config['config_json']) ? $config['config_json'] : '';
314
315 $bucketConfig = isset($data['bucketData']) ? $data['bucketData'] : [];
316 $bucketAddNewConfig = isset($bucketConfig['addNewConfig']) ? $bucketConfig['addNewConfig'] : [];
317 $bucket = isset($bucketAddNewConfig['bucket_name']) ? $bucketAddNewConfig['bucket_name'] : false;
318 $region = isset($bucketAddNewConfig['region']) ? $bucketAddNewConfig['region'] : false;
319
320 if( !(isset($config_json) && !empty($config_json) &&$region!=false && $bucket!=false ) ) return $result;
321
322 if(!Utils::is_json($config_json)) {
323 $result = [
324 'success' => false,
325 'message' => esc_html__('Invalid JSON', 'media-cloud-sync')
326 ];
327 return $result;
328 };
329
330 $currentService = new GCloud;
331 return $currentService->createBucket($config_json, $region, $bucket);
332 break;
333 case 'docean':
334 $result = [
335 'success' => false,
336 'message' => esc_html__('Insufficient Data. Please try again', 'media-cloud-sync')
337 ];
338
339 $config = isset($data['config']) ? $data['config'] : [];
340 $access_key = isset($config['access_key']) ? $config['access_key'] : false;
341 $secret_key = isset($config['secret_key']) ? $config['secret_key'] : false;
342 $region = isset($config['region']) ? $config['region'] : false;
343 $bucketData = isset($data['bucketData']) ? $data['bucketData'] : [];
344 $bucketConfig = isset($bucketData['addNewConfig']) ? $bucketData['addNewConfig'] : [];
345 $bucket = isset($bucketConfig['bucket_name']) ? $bucketConfig['bucket_name'] : false;
346
347 if( $access_key==false || $secret_key==false || $region==false || $bucket==false ) return $result;
348
349 $currentService = new DOcean;
350 return $currentService->createBucket($access_key, $secret_key, $region, $bucket);
351 break;
352 case 's3compatible':
353 $result = [
354 'success' => false,
355 'message' => esc_html__('Insufficient Data. Please try again', 'media-cloud-sync')
356 ];
357
358 $config = isset($data['config']) ? $data['config'] : [];
359 $access_key = isset($config['access_key']) ? $config['access_key'] : false;
360 $secret_key = isset($config['secret_key']) ? $config['secret_key'] : false;
361 $region = isset($config['region']) ? $config['region'] : false;
362 $endpoint = isset($config['endpoint']) ? $config['endpoint'] : false;
363
364 $bucketData = isset($data['bucketData']) ? $data['bucketData'] : [];
365 $bucketConfig = isset($bucketData['addNewConfig']) ? $bucketData['addNewConfig'] : [];
366 $bucket = isset($bucketConfig['bucket_name']) ? $bucketConfig['bucket_name'] : false;
367
368 if( $access_key==false || $secret_key==false || $bucket==false || $endpoint==false ) return $result;
369
370 $currentService = new S3Compatible;
371 return $currentService->createBucket($endpoint, $access_key, $secret_key, $bucket, $region);
372 break;
373 default:
374 $result = [
375 'success' => false,
376 'message' => esc_html__('Invalid service', 'media-cloud-sync')
377 ];
378 }
379
380 return $result;
381 }
382
383 /**
384 * Verify Object write permission
385 * @since 1.0.0
386 */
387 public function verifyObjectWritePermission($data) {
388 $result = [
389 'success' => false,
390 'message' => esc_html__('Something went wrong', 'media-cloud-sync')
391 ];
392
393 $service = isset($data['service'])? $data['service'] : false;
394
395 if($service == false) {
396 $result = [
397 'success' => false,
398 'message' => esc_html__('No service selected', 'media-cloud-sync')
399 ];
400 return $result;
401 }
402
403 switch($service){
404 case 's3':
405 $result = [
406 'success' => false,
407 'message' => esc_html__('Insufficient Data. Please try again', 'media-cloud-sync')
408 ];
409 $config = isset($data['config']) ? $data['config'] : [];
410 $bucketData = isset($data['bucketData']) ? $data['bucketData'] : [];
411 if(isset($bucketData['addNew']) && $bucketData['addNew']) {
412 $bucketConfig = isset($bucketData['addNewConfig']) ? $bucketData['addNewConfig'] : [];
413 } else {
414 $bucketConfig = isset($bucketData['config']) ? $bucketData['config'] : [];
415 }
416 $bucket = isset($bucketConfig['bucket_name']) ? $bucketConfig['bucket_name'] : false;
417 $transfer_acceleration = isset($bucketConfig['transfer_acceleration']) ? $bucketConfig['transfer_acceleration'] : false;
418 $access_key = isset($config['access_key']) ? $config['access_key'] : false;
419 $secret_key = isset($config['secret_key']) ? $config['secret_key'] : false;
420 $region = isset($config['region']) ? $config['region'] : false;
421
422 if( $access_key==false || $secret_key==false || $region==false || $bucket==false ) return $result;
423
424 $currentService = new S3;
425 return $currentService->verifyObjectWritePermission($access_key, $secret_key, $region, $bucket, $transfer_acceleration);
426
427 case 'gcloud':
428 $result = [
429 'success' => false,
430 'message' => esc_html__('Insufficient Data. Please try again', 'media-cloud-sync')
431 ];
432 $config = isset($data['config']) ? $data['config'] : [];
433 $config_json = isset($config['config_json']) ? $config['config_json'] : '';
434
435 $bucketData = isset($data['bucketData']) ? $data['bucketData'] : [];
436 if(isset($bucketData['addNew']) && $bucketData['addNew']) {
437 $bucketConfig = isset($bucketData['addNewConfig']) ? $bucketData['addNewConfig'] : [];
438 } else {
439 $bucketConfig = isset($bucketData['config']) ? $bucketData['config'] : [];
440 }
441 $bucket = isset($bucketConfig['bucket_name']) ? $bucketConfig['bucket_name'] : false;
442
443 if( !(isset($config_json) && !empty($config_json) && $bucket!=false ) ) return $result;
444
445 if(!Utils::is_json($config_json)) {
446 $result = [
447 'success' => false,
448 'message' => esc_html__('Invalid config json', 'media-cloud-sync')
449 ];
450 return $result;
451 }
452
453 $currentService = new GCloud;
454 return $currentService->verifyObjectWritePermission($config_json, $bucket);
455 break;
456 case 'docean':
457 $result = [
458 'success' => false,
459 'message' => esc_html__('Insufficient Data. Please try again', 'media-cloud-sync')
460 ];
461 $config = isset($data['config']) ? $data['config'] : [];
462 $bucketData = isset($data['bucketData']) ? $data['bucketData'] : [];
463 if(isset($bucketData['addNew']) && $bucketData['addNew']) {
464 $bucketConfig = isset($bucketData['addNewConfig']) ? $bucketData['addNewConfig'] : [];
465 } else {
466 $bucketConfig = isset($bucketData['config']) ? $bucketData['config'] : [];
467 }
468 $bucket = isset($bucketConfig['bucket_name']) ? $bucketConfig['bucket_name'] : false;
469 $transfer_acceleration = isset($bucketConfig['transfer_acceleration']) ? $bucketConfig['transfer_acceleration'] : false;
470 $access_key = isset($config['access_key']) ? $config['access_key'] : false;
471 $secret_key = isset($config['secret_key']) ? $config['secret_key'] : false;
472 $region = isset($config['region']) ? $config['region'] : false;
473
474 if( $access_key==false || $secret_key==false || $region==false || $bucket==false ) return $result;
475
476 $currentService = new DOcean;
477 return $currentService->verifyObjectWritePermission($access_key, $secret_key, $region, $bucket);
478 break;
479 case 's3compatible':
480 $result = [
481 'success' => false,
482 'message' => esc_html__('Insufficient Data. Please try again', 'media-cloud-sync')
483 ];
484 $config = isset($data['config']) ? $data['config'] : [];
485 $bucketData = isset($data['bucketData']) ? $data['bucketData'] : [];
486 if(isset($bucketData['addNew']) && $bucketData['addNew']) {
487 $bucketConfig = isset($bucketData['addNewConfig']) ? $bucketData['addNewConfig'] : [];
488 } else {
489 $bucketConfig = isset($bucketData['config']) ? $bucketData['config'] : [];
490 }
491 $bucket = isset($bucketConfig['bucket_name']) ? $bucketConfig['bucket_name'] : false;
492 $transfer_acceleration = isset($bucketConfig['transfer_acceleration']) ? $bucketConfig['transfer_acceleration'] : false;
493 $access_key = isset($config['access_key']) ? $config['access_key'] : false;
494 $secret_key = isset($config['secret_key']) ? $config['secret_key'] : false;
495 $region = isset($config['region']) ? $config['region'] : false;
496 $endpoint = isset($config['endpoint']) ? $config['endpoint'] : false;
497
498 if( $access_key==false || $secret_key==false || $endpoint==false || $bucket==false ) return $result;
499 $currentService = new S3Compatible;
500 return $currentService->verifyObjectWritePermission($endpoint, $access_key, $secret_key, $bucket, $region);
501 break;
502 default:
503 break;
504 }
505
506 return false;
507 }
508
509
510 /**
511 * Verify Object delete permission
512 * @since 1.0.0
513 */
514 public function verifyObjectDeletePermission($data) {
515 $result = [
516 'success' => false,
517 'message' => esc_html__('Something went wrong', 'media-cloud-sync')
518 ];
519
520 $service = isset($data['service'])? $data['service'] : false;
521
522 if($service == false) {
523 $result = [
524 'success' => false,
525 'message' => esc_html__('No service selected', 'media-cloud-sync')
526 ];
527 return $result;
528 }
529
530 switch($service){
531 case 's3':
532 $result = [
533 'success' => false,
534 'message' => esc_html__('Insufficient Data. Please try again', 'media-cloud-sync')
535 ];
536 $config = isset($data['config']) ? $data['config'] : [];
537 $bucketData = isset($data['bucketData']) ? $data['bucketData'] : [];
538 if(isset($bucketData['addNew']) && $bucketData['addNew']) {
539 $bucketConfig = isset($bucketData['addNewConfig']) ? $bucketData['addNewConfig'] : [];
540 } else {
541 $bucketConfig = isset($bucketData['config']) ? $bucketData['config'] : [];
542 }
543 $bucket = isset($bucketConfig['bucket_name']) ? $bucketConfig['bucket_name'] : false;
544 $transfer_acceleration = isset($bucketConfig['transfer_acceleration']) ? $bucketConfig['transfer_acceleration'] : false;
545 $access_key = isset($config['access_key']) ? $config['access_key'] : false;
546 $secret_key = isset($config['secret_key']) ? $config['secret_key'] : false;
547 $region = isset($config['region']) ? $config['region'] : false;
548
549 if( $access_key==false || $secret_key==false || $region==false || $bucket==false ) return $result;
550
551 $currentService = new S3;
552 return $currentService->verifyObjectDeletePermission($access_key, $secret_key, $region, $bucket, $transfer_acceleration);
553
554 case 'gcloud':
555 $result = [
556 'success' => false,
557 'message' => esc_html__('Insufficient Data. Please try again', 'media-cloud-sync')
558 ];
559 $config = isset($data['config']) ? $data['config'] : [];
560 $config_json = isset($config['config_json']) ? $config['config_json'] : '';
561 $bucketData = isset($data['bucketData']) ? $data['bucketData'] : [];
562 if(isset($bucketData['addNew']) && $bucketData['addNew']) {
563 $bucketConfig = isset($bucketData['addNewConfig']) ? $bucketData['addNewConfig'] : [];
564 } else {
565 $bucketConfig = isset($bucketData['config']) ? $bucketData['config'] : [];
566 }
567 $bucket = isset($bucketConfig['bucket_name']) ? $bucketConfig['bucket_name'] : false;
568
569 if( !(isset($config_json) && !empty($config_json) && $bucket!=false) ) return $result;
570
571 if(!Utils::is_json($config_json)) {
572 $result = [
573 'success' => false,
574 'message' => esc_html__('Invalid config json', 'media-cloud-sync')
575 ];
576 return $result;
577 }
578
579 $currentService = new GCloud;
580 return $currentService->verifyObjectDeletePermission($config_json, $bucket);
581 break;
582 case 'docean':
583 $result = [
584 'success' => false,
585 'message' => esc_html__('Insufficient Data. Please try again', 'media-cloud-sync')
586 ];
587 $config = isset($data['config']) ? $data['config'] : [];
588 $bucketData = isset($data['bucketData']) ? $data['bucketData'] : [];
589 if(isset($bucketData['addNew']) && $bucketData['addNew']) {
590 $bucketConfig = isset($bucketData['addNewConfig']) ? $bucketData['addNewConfig'] : [];
591 } else {
592 $bucketConfig = isset($bucketData['config']) ? $bucketData['config'] : [];
593 }
594 $bucket = isset($bucketConfig['bucket_name']) ? $bucketConfig['bucket_name'] : false;
595 $access_key = isset($config['access_key']) ? $config['access_key'] : false;
596 $secret_key = isset($config['secret_key']) ? $config['secret_key'] : false;
597 $region = isset($config['region']) ? $config['region'] : false;
598
599 if( $access_key==false || $secret_key==false || $region==false || $bucket==false ) return $result;
600
601 $currentService = new DOcean;
602 return $currentService->verifyObjectDeletePermission($access_key, $secret_key, $region, $bucket);
603 break;
604 case 's3compatible':
605 $result = [
606 'success' => false,
607 'message' => esc_html__('Insufficient Data. Please try again', 'media-cloud-sync')
608 ];
609 $config = isset($data['config']) ? $data['config'] : [];
610 $bucketData = isset($data['bucketData']) ? $data['bucketData'] : [];
611 if(isset($bucketData['addNew']) && $bucketData['addNew']) {
612 $bucketConfig = isset($bucketData['addNewConfig']) ? $bucketData['addNewConfig'] : [];
613 } else {
614 $bucketConfig = isset($bucketData['config']) ? $bucketData['config'] : [];
615 }
616 $bucket = isset($bucketConfig['bucket_name']) ? $bucketConfig['bucket_name'] : false;
617 $access_key = isset($config['access_key']) ? $config['access_key'] : false;
618 $secret_key = isset($config['secret_key']) ? $config['secret_key'] : false;
619 $region = isset($config['region']) ? $config['region'] : false;
620 $endpoint = isset($config['endpoint']) ? $config['endpoint'] : false;
621
622 if( $access_key==false || $secret_key==false || $endpoint==false || $bucket==false ) return $result;
623
624 $currentService = new S3Compatible;
625 return $currentService->verifyObjectDeletePermission($endpoint, $access_key, $secret_key, $bucket, $region);
626 break;
627 default:
628 break;
629 }
630
631 return false;
632 }
633
634
635 /**
636 * Verify Object read permission
637 * @since 1.0.0
638 */
639 public function verifyObjectReadPermission($data) {
640 return $this->service->verifyObjectReadPermission($data);
641 }
642
643 /**
644 * Get Bucket Security Settings
645 */
646 public function getBucketSecuritySettings($data) {
647 $result = [
648 'success' => false,
649 'message' => esc_html__('Something went wrong', 'media-cloud-sync')
650 ];
651
652 $service = isset($data['service'])? $data['service'] : false;
653
654 if($service == false) {
655 $result = [
656 'success' => false,
657 'message' => esc_html__('No service selected', 'media-cloud-sync')
658 ];
659 return $result;
660 }
661
662 switch($service){
663 case 's3':
664 $result = [
665 'success' => false,
666 'message' => esc_html__('Insufficient Data. Please try again', 'media-cloud-sync')
667 ];
668 $config = isset($data['config']) ? $data['config'] : [];
669 $bucketData = isset($data['bucketData']) ? $data['bucketData'] : [];
670 if(isset($bucketData['addNew']) && $bucketData['addNew']) {
671 $bucketConfig = isset($bucketData['addNewConfig']) ? $bucketData['addNewConfig'] : [];
672 } else {
673 $bucketConfig = isset($bucketData['config']) ? $bucketData['config'] : [];
674 }
675 $bucket = isset($bucketConfig['bucket_name']) ? $bucketConfig['bucket_name'] : false;
676 $transfer_acceleration = isset($bucketConfig['transfer_acceleration']) ? $bucketConfig['transfer_acceleration'] : false;
677 $access_key = isset($config['access_key']) ? $config['access_key'] : false;
678 $secret_key = isset($config['secret_key']) ? $config['secret_key'] : false;
679 $region = isset($config['region']) ? $config['region'] : false;
680
681 if( $access_key==false || $secret_key==false || $region==false || $bucket==false ) return $result;
682
683 $currentService = new S3;
684 return $currentService->getBucketSecuritySettings($access_key, $secret_key, $region, $bucket, $transfer_acceleration);
685 break;
686 default:
687 break;
688 }
689
690 return $result;
691 }
692
693
694 /**
695 * Change Bucket Public Access
696 * @since 1.0.0
697 * @param array $data
698 */
699 public function changePublicAccess($data) {
700 $result = [
701 'success' => false,
702 'message' => esc_html__('Something went wrong', 'media-cloud-sync')
703 ];
704
705 $service = isset($data['service'])? $data['service'] : false;
706
707 if($service == false) {
708 $result = [
709 'success' => false,
710 'message' => esc_html__('No service selected', 'media-cloud-sync')
711 ];
712 return $result;
713 }
714
715 switch($service){
716 case 's3':
717 $result = [
718 'success' => false,
719 'message' => esc_html__('Insufficient Data. Please try again', 'media-cloud-sync')
720 ];
721 $config = isset($data['config']) ? $data['config'] : [];
722 $bucketData = isset($data['bucketData']) ? $data['bucketData'] : [];
723 if(isset($bucketData['addNew']) && $bucketData['addNew']) {
724 $bucketConfig = isset($bucketData['addNewConfig']) ? $bucketData['addNewConfig'] : [];
725 } else {
726 $bucketConfig = isset($bucketData['config']) ? $bucketData['config'] : [];
727 }
728 $bucket = isset($bucketConfig['bucket_name']) ? $bucketConfig['bucket_name'] : false;
729 $transfer_acceleration = isset($bucketConfig['transfer_acceleration']) ? $bucketConfig['transfer_acceleration'] : false;
730 $access_key = isset($config['access_key']) ? $config['access_key'] : false;
731 $secret_key = isset($config['secret_key']) ? $config['secret_key'] : false;
732 $region = isset($config['region']) ? $config['region'] : false;
733 $value = isset($data['value']) ? $data['value'] : false;
734
735 if( $access_key==false || $secret_key==false || $region==false || $bucket==false ) return $result;
736
737 $currentService = new S3;
738 return $currentService->changePublicAccess($value, $access_key, $secret_key, $region, $bucket, $transfer_acceleration);
739 break;
740
741 default:
742 break;
743 }
744
745 return $result;
746 }
747
748 /**
749 * Change bucket ownership
750 */
751 public function changeObjectOwnership($data) {
752 $result = [
753 'success' => false,
754 'message' => esc_html__('Something went wrong', 'media-cloud-sync')
755 ];
756
757 $service = isset($data['service'])? $data['service'] : false;
758
759 if($service == false) {
760 $result = [
761 'success' => false,
762 'message' => esc_html__('No service selected', 'media-cloud-sync')
763 ];
764 return $result;
765 }
766
767 switch($service){
768 case 's3':
769 $result = [
770 'success' => false,
771 'message' => esc_html__('Insufficient Data. Please try again', 'media-cloud-sync')
772 ];
773 $config = isset($data['config']) ? $data['config'] : [];
774 $bucketData = isset($data['bucketData']) ? $data['bucketData'] : [];
775 if(isset($bucketData['addNew']) && $bucketData['addNew']) {
776 $bucketConfig = isset($bucketData['addNewConfig']) ? $bucketData['addNewConfig'] : [];
777 } else {
778 $bucketConfig = isset($bucketData['config']) ? $bucketData['config'] : [];
779 }
780 $bucket = isset($bucketConfig['bucket_name']) ? $bucketConfig['bucket_name'] : false;
781 $transfer_acceleration = isset($bucketConfig['transfer_acceleration']) ? $bucketConfig['transfer_acceleration'] : false;
782 $access_key = isset($config['access_key']) ? $config['access_key'] : false;
783 $secret_key = isset($config['secret_key']) ? $config['secret_key'] : false;
784 $region = isset($config['region']) ? $config['region'] : false;
785 $value = isset($data['value']) ? $data['value'] : false;
786
787 if( $access_key==false || $secret_key==false || $region==false || $bucket==false ) return $result;
788
789 $currentService = new S3;
790 return $currentService->changeObjectOwnership($value, $access_key, $secret_key, $region, $bucket, $transfer_acceleration);
791 break;
792
793 default:
794 break;
795 }
796
797 return $result;
798 }
799
800
801 /**
802 * Get presigned URL
803 * @since 1.0.0
804 */
805 public function get_presigned_url($path) {
806 $url_result = $this->service->get_presigned_url($path);
807 if(isset($url_result['success']) && $url_result['success']) {
808 return isset($url_result['file_url']) ? $url_result['file_url'] : false;
809 }
810 return false;
811 }
812
813
814 /**
815 * Upload a single file to the cloud storage.
816 *
817 * @param string $file_path The absolute path to the file on the local server.
818 * @param string $source_path The path where the file will be stored in the cloud.
819 * @param string $prefix An optional prefix to add to the cloud storage path.
820 * @return array The result of the upload operation, including success status and any relevant messages.
821 */
822 public function uploadSingle($file_path, $source_path, $prefix = '') {
823 return $this->service->uploadSingle($file_path, $source_path, $prefix);
824 }
825
826
827 public function deleteSingle($key) {
828 return $this->service->deleteSingle($key);
829 }
830
831
832 /**
833 * Move object to server from cloud
834 */
835 public function object_to_server($key, $save_path) {
836 $path_parts = pathinfo($save_path);
837 if (!file_exists($path_parts['dirname'])) {
838 mkdir($path_parts['dirname'], 0755, true);
839 }
840 return $this->service->object_to_server($key, $save_path);
841 }
842
843 /**
844 * Get the service domain
845 *
846 */
847 public function get_domain() {
848 return $this->service->get_domain();
849 }
850
851 /**
852 * Ensures only one instance of Class is loaded or can be loaded.
853 *
854 * @return Service Class instance
855 * @since 1.0.0
856 * @static
857 */
858 public static function instance(){
859 if (is_null(self::$instance)) {
860 self::$instance = new self();
861 }
862 return self::$instance;
863 }
864 }