PluginProbe
Media Cloud Sync / 1.2.11
Media Cloud Sync v1.2.11
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.11, at includes/base/service.php

1,087 lines 49.7 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 /**
803 * Upload media item
804 */
805 public function uploadMedia($attachment_meta, $attachment_id, $source_path) {
806 $wpmcsItem = Item::instance();
807 $upload_dir = wp_get_upload_dir();
808 $type = get_post_mime_type($attachment_id);
809 $is_image = (0 === strpos($type, 'image/'));
810 $existing = $wpmcsItem->get($attachment_id);
811 $existing_extras = $wpmcsItem->get_extras($attachment_id);
812 $has_existing = !Utils::is_empty($existing);
813 $sizes = [];
814 $uploaded = [];
815 $extras = [];
816 $prefix = '';
817 $file_path = '';
818 $file_dir = '';
819 $original_file_path = '';
820 $original_file_source_path = '';
821
822
823 // Add prefix if object versioning is ON
824 if (isset($this->settings['object_versioning']) && $this->settings['object_versioning']) {
825 $prefix = ($existing_extras && isset($existing_extras['prefix']) && !empty($existing_extras['prefix']))
826 ? $existing_extras['prefix']
827 : Utils::generate_object_versioning_prefix();
828 $extras['prefix'] = $prefix;
829 }
830
831 // Get width and height from image meta
832 if (isset($attachment_meta) && !empty($attachment_meta)) {
833 $extras['width'] = (isset($attachment_meta['width']) && !empty($attachment_meta['width'])) ? $attachment_meta['width'] : 0;
834 $extras['height'] = (isset($attachment_meta['height']) && !empty($attachment_meta['height'])) ? $attachment_meta['height'] : 0;
835 }
836
837
838 // Get Original File Name/Path from Image meta
839 $original_file = isset($attachment_meta) && !empty($attachment_meta) &&
840 isset($attachment_meta['original_image']) && !empty($attachment_meta['original_image'])
841 ? $attachment_meta['original_image'] : '';
842
843 $file_path = trailingslashit($upload_dir['basedir']) . $source_path;
844 $file_dir = isset(pathinfo($file_path)['dirname']) ? pathinfo($file_path)['dirname'] : '';
845
846 // Check whether the extension is enabled for uploading
847 if (!Utils::is_extension_available($file_path)) {
848 return $attachment_meta;
849 }
850
851 // Upload the Full Size file
852 if( $has_existing && ( $existing['source_path'] == $source_path ) ) {
853 $uploaded = [
854 'success' => true,
855 'file_url' => $existing['url'],
856 'key' => $existing['key']
857 ];
858 } else if(file_exists($file_path)){
859 $uploaded = $this->service->uploadSingle($file_path, $source_path, $prefix);
860 }
861
862 if(
863 isset($uploaded) && !empty($uploaded) &&
864 isset($uploaded['success']) && $uploaded['success']
865 ) {
866 if(isset($original_file) && !empty($original_file)){
867 // Find original image relative and absolute path
868 $original_file_path = trailingslashit($file_dir) . $original_file;
869 $original_file_source_path = Utils::get_attachment_source_path($original_file_path);
870 $uploaded_original = [];
871
872 // Upload Original File
873 if(
874 !Utils::is_empty($existing_extras) &&
875 isset($existing_extras['original']) && !Utils::is_empty($existing_extras['original']) &&
876 $existing_extras['original']['source_path'] == $original_file_source_path
877 ) {
878 $uploaded_original = [
879 'success' => true,
880 'file_url' => $existing_extras['original']['url'],
881 'key' => $existing_extras['original']['key']
882 ];
883 } else if(file_exists($original_file_path)) {
884 $uploaded_original = $this->service->uploadSingle($original_file_path, $original_file_source_path, $prefix);
885 }
886
887 if(
888 isset($uploaded_original) && !empty($uploaded_original) &&
889 isset($uploaded_original['success']) && $uploaded_original['success']
890 ) {
891 $extras['original'] = array(
892 'source_path' => $original_file_source_path,
893 'url' => $uploaded_original['file_url'],
894 'key' => $uploaded_original['key']
895 );
896 }
897 }
898
899 if (
900 $is_image &&
901 isset($attachment_meta['sizes']) && !empty($attachment_meta['sizes']) &&
902 isset($file_dir) && !empty($file_dir)
903 ) {
904 foreach ($attachment_meta['sizes'] as $size => $sub_image) {
905 $sub_size = [];
906 $sub_file = isset($sub_image['file']) ? $sub_image['file'] : false;
907 $sub_file_path = '';
908 $sub_file_source_path = '';
909 $uploaded_sub_image = [];
910
911 if ($sub_file) {
912 $sub_file_path = $file_dir . '/' . $sub_file;
913 $sub_file_source_path = Utils::get_attachment_source_path($sub_file_path);
914 }
915
916 // Upload Image size
917 if(
918 !Utils::is_empty($existing_extras) &&
919 isset($existing_extras['sizes']) && !Utils::is_empty($existing_extras['sizes']) &&
920 isset($existing_extras['sizes'][$size]) && !Utils::is_empty($existing_extras['sizes'][$size]) &&
921 $existing_extras['sizes'][$size]['source_path'] == $sub_file_source_path
922 ) {
923 $uploaded_sub_image = [
924 'success' => true,
925 'file_url' => $existing_extras['sizes'][$size]['url'],
926 'key' => $existing_extras['sizes'][$size]['key']
927 ];
928 } else if(file_exists($sub_file_path)) {
929 $uploaded_sub_image = $this->service->uploadSingle($sub_file_path, $sub_file_source_path, $prefix);
930 }
931
932 if (
933 isset($uploaded_sub_image) && !empty($uploaded_sub_image) &&
934 isset($uploaded_sub_image['success']) && $uploaded_sub_image['success']
935 ) {
936 $sub_size['source_path'] = $sub_file_source_path;
937 $sub_size['url'] = $uploaded_sub_image['file_url'];
938 $sub_size['key'] = $uploaded_sub_image['key'];
939 $sub_size['width'] = isset($sub_image['width']) ? $sub_image['width']: 0;
940 $sub_size['height'] = isset($sub_image['height']) ? $sub_image['height']: 0;
941 }
942
943 $sizes[$size] = $sub_size;
944 }
945 }
946
947
948 if (isset($sizes) && !empty($sizes)) {
949 $extras['sizes'] = $sizes;
950 }
951
952 return [
953 'file' => [
954 'source_path' => $source_path,
955 'url' => $uploaded['file_url'],
956 'key' => $uploaded['key'],
957 ],
958 'extra' => $extras
959 ];
960
961 }
962
963 return false;
964 }
965
966 /**
967 * Delete media item
968 */
969 public function delete_attachments_by_item($item, $delete_backup = true) {
970 $upload_dir = wp_get_upload_dir();
971
972 if (isset($item['extra']) && !empty($item['extra'])) {
973 $extras = unserialize($item['extra']);
974 if (
975 isset($extras) && !empty($extras) &&
976 isset($extras['sizes']) && !empty($extras['sizes'])
977 ) {
978 foreach ($extras['sizes'] as $sub_image) {
979 if (isset($sub_image['key']) && !empty($sub_image['key'])) {
980 $this->service->deleteSingle($sub_image['key']);
981 }
982 }
983 }
984
985 if (
986 isset($extras) && !empty($extras) &&
987 isset($extras['original']) && !empty($extras['original'])
988 ) {
989 $this->service->deleteSingle($extras['original']['key']);
990 }
991
992 if (
993 isset($extras) && !empty($extras) &&
994 isset($extras['backup']) && !empty($extras['backup']) &&
995 $delete_backup
996 ) {
997 $backup = unserialize($extras['backup']);
998 if (isset($backup) && !empty($backup)) {
999 $this->delete_attachments_by_item($backup, false);
1000 }
1001 }
1002 }
1003 if (isset($item['key']) && !empty($item['key'])) {
1004 $this->service->deleteSingle($item['key']);
1005 }
1006 }
1007
1008
1009 /**
1010 * Get presigned URL
1011 * @since 1.0.0
1012 */
1013 public function get_presigned_url($path) {
1014 $url_result = $this->service->get_presigned_url($path);
1015 if(isset($url_result['success']) && $url_result['success']) {
1016 return isset($url_result['file_url']) ? $url_result['file_url'] : false;
1017 }
1018 return false;
1019 }
1020
1021 /**
1022 * Remove query strings from service.
1023 *
1024 * @param string $content
1025 * @param string $base_url Optional base URL that must exist within URL for Amazon query strings to be removed.
1026 *
1027 * @return string
1028 */
1029 public function remove_query_strings( $content, $base_url = '' ) {
1030 $pattern = '\?[^\s"<\?]*(?:X-Amz-Algorithm|AWSAccessKeyId|Key-Pair-Id|GoogleAccessId)=[^\s"<\?]+';
1031 $group = 0;
1032
1033 if ( ! is_string( $content ) ) {
1034 return $content;
1035 }
1036
1037 if ( ! empty( $base_url ) ) {
1038 $pattern = preg_quote( $base_url, '/' ) . '[^\s"<\?]+(' . $pattern . ')';
1039 $group = 1;
1040 }
1041 if ( ! preg_match_all( '/' . $pattern . '/', $content, $matches ) || ! isset( $matches[ $group ] ) ) {
1042 // No query strings found, return
1043 return $content;
1044 }
1045
1046 $matches = array_unique( $matches[ $group ] );
1047
1048 foreach ( $matches as $match ) {
1049 $content = str_replace( $match, '', $content );
1050 }
1051 return $content;
1052 }
1053
1054
1055 /**
1056 * Move object to server from cloud
1057 */
1058 public function object_to_server($key, $save_path) {
1059 $path_parts = pathinfo($save_path);
1060 if (!file_exists($path_parts['dirname'])) {
1061 mkdir($path_parts['dirname'], 0755, true);
1062 }
1063 return $this->service->object_to_server($key, $save_path);
1064 }
1065
1066 /**
1067 * Get the service domain
1068 *
1069 */
1070 public function get_domain() {
1071 return $this->service->get_domain();
1072 }
1073
1074 /**
1075 * Ensures only one instance of Class is loaded or can be loaded.
1076 *
1077 * @return Service Class instance
1078 * @since 1.0.0
1079 * @static
1080 */
1081 public static function instance(){
1082 if (is_null(self::$instance)) {
1083 self::$instance = new self();
1084 }
1085 return self::$instance;
1086 }
1087 }