PluginProbe
Media Cloud Sync / 1.2.5
Media Cloud Sync v1.2.5
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.5, at includes/base/service.php

1,048 lines 48.6 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_file = isset($config['config_json']) ? $config['config_json'] : [ 'name' => '', 'path' => '' ];
86
87 if(!(isset($config_file['path']) && !empty($config_file['path']) && file_exists($config_file['path']))) {
88 $result = [
89 'success' => false,
90 'message' => esc_html__('Insufficient Data. Please try again', 'media-cloud-sync')
91 ];
92 return $result;
93 }
94
95 $currentService = new GCloud;
96 return $currentService->verifyCredentials($config_file);
97 break;
98 case 'docean':
99 $config = isset($data['config']) ? $data['config'] : [];
100 $access_key = isset($config['access_key']) ? $config['access_key'] : false;
101 $secret_key = isset($config['secret_key']) ? $config['secret_key'] : false;
102 $region = isset($config['region']) ? $config['region'] : false;
103
104 if($access_key==false || $secret_key==false || $region==false) {
105 $result = [
106 'success' => false,
107 'message' => esc_html__('Insufficient Data. Please try again', 'media-cloud-sync')
108 ];
109 return $result;
110 }
111
112 $currentService = new DOcean;
113 return $currentService->verifyCredentials($access_key, $secret_key, $region);
114 break;
115 case 's3compatible':
116 $config = isset($data['config']) ? $data['config'] : [];
117 $access_key = isset($config['access_key']) ? $config['access_key'] : false;
118 $secret_key = isset($config['secret_key']) ? $config['secret_key'] : false;
119 $region = isset($config['region']) ? $config['region'] : false;
120 $endpoint = isset($config['endpoint']) ? $config['endpoint'] : false;
121
122 if($access_key==false || $secret_key==false || $endpoint==false) {
123 $result = [
124 'success' => false,
125 'message' => esc_html__('Insufficient Data. Please try again', 'media-cloud-sync')
126 ];
127 return $result;
128 }
129
130 $currentService = new S3Compatible;
131 return $currentService->verifyCredentials($endpoint, $access_key, $secret_key, $region);
132 break;
133 default:
134 $result = [
135 'success' => false,
136 'message' => esc_html__('Invalid service', 'media-cloud-sync')
137 ];
138 }
139
140 return $result;
141 }
142
143 /**
144 * Verify Bucket Exist
145 * @since 1.0.0
146 */
147 public function verifyBucketExist($data) {
148 $result = [
149 'success' => false,
150 'message' => esc_html__('Something went wrong', 'media-cloud-sync')
151 ];
152
153 $service = isset($data['service'])? $data['service'] : false;
154
155 if($service == false) {
156 $result = [
157 'success' => false,
158 'message' => esc_html__('No service selected', 'media-cloud-sync')
159 ];
160 return $result;
161 }
162
163 switch($service){
164 case 's3':
165 $result = [
166 'success' => false,
167 'message' => esc_html__('Insufficient Data. Please try again', 'media-cloud-sync')
168 ];
169 $config = isset($data['config']) ? $data['config'] : [];
170 $bucketData = isset($data['bucketData']) ? $data['bucketData'] : [];
171 $bucketConfig = isset($bucketData['config']) ? $bucketData['config'] : [];
172 $bucket = isset($bucketConfig['bucket_name']) ? $bucketConfig['bucket_name'] : false;
173 $transfer_acceleration = isset($bucketConfig['transfer_acceleration']) ? $bucketConfig['transfer_acceleration'] : false;
174 $access_key = isset($config['access_key']) ? $config['access_key'] : false;
175 $secret_key = isset($config['secret_key']) ? $config['secret_key'] : false;
176 $region = isset($config['region']) ? $config['region'] : false;
177
178 if( $access_key==false || $secret_key==false || $region==false || $bucket==false ) return $result;
179
180 $currentService = new S3;
181
182
183 return $currentService->verifyBucketExist($access_key, $secret_key, $region, $bucket, $transfer_acceleration);
184
185 case 'gcloud':
186 $result = [
187 'success' => false,
188 'message' => esc_html__('Insufficient Data. Please try again', 'media-cloud-sync')
189 ];
190 $config = isset($data['config']) ? $data['config'] : [];
191 $config_file = isset($config['config_json']) ? $config['config_json'] : [ 'name' => '', 'path' => '' ];
192
193 $bucketData = isset($data['bucketData']) ? $data['bucketData'] : [];
194 $bucketConfig = isset($bucketData['config']) ? $bucketData['config'] : [];
195 $bucket = isset($bucketConfig['bucket_name']) ? $bucketConfig['bucket_name'] : false;
196
197 if( !(isset($config_file['path']) && !empty($config_file['path']) && file_exists($config_file['path']) && $bucket!=false ) ) return $result;
198
199 $currentService = new GCloud;
200 return $currentService->verifyBucketExist($config_file, $bucket);
201 break;
202 case 'docean':
203 $result = [
204 'success' => false,
205 'message' => esc_html__('Insufficient Data. Please try again', 'media-cloud-sync')
206 ];
207 $config = isset($data['config']) ? $data['config'] : [];
208 $bucketData = isset($data['bucketData']) ? $data['bucketData'] : [];
209 $bucketConfig = isset($bucketData['config']) ? $bucketData['config'] : [];
210 $bucket = isset($bucketConfig['bucket_name']) ? $bucketConfig['bucket_name'] : false;
211 $access_key = isset($config['access_key']) ? $config['access_key'] : false;
212 $secret_key = isset($config['secret_key']) ? $config['secret_key'] : false;
213 $region = isset($config['region']) ? $config['region'] : false;
214
215 if( $access_key==false || $secret_key==false || $region==false || $bucket==false ) return $result;
216
217 $currentService = new DOcean;
218
219
220 return $currentService->verifyBucketExist($access_key, $secret_key, $region, $bucket);
221 break;
222 case 's3compatible':
223 $result = [
224 'success' => false,
225 'message' => esc_html__('Insufficient Data. Please try again', 'media-cloud-sync')
226 ];
227 $config = isset($data['config']) ? $data['config'] : [];
228 $bucketData = isset($data['bucketData']) ? $data['bucketData'] : [];
229 $bucketConfig = isset($bucketData['config']) ? $bucketData['config'] : [];
230 $bucket = isset($bucketConfig['bucket_name']) ? $bucketConfig['bucket_name'] : false;
231 $access_key = isset($config['access_key']) ? $config['access_key'] : false;
232 $secret_key = isset($config['secret_key']) ? $config['secret_key'] : false;
233 $region = isset($config['region']) ? $config['region'] : false;
234 $endpoint = isset($config['endpoint']) ? $config['endpoint'] : false;
235
236 if( $access_key==false || $secret_key==false || $bucket==false || $endpoint==false ) return $result;
237
238 $currentService = new S3Compatible;
239
240 return $currentService->verifyBucketExist($endpoint, $access_key, $secret_key, $region, $bucket);
241 break;
242 default:
243 $result = [
244 'success' => false,
245 'message' => esc_html__('Invalid service', 'media-cloud-sync')
246 ];
247 }
248
249 return $result;
250 }
251
252 /**
253 * Verify Bucket Credentials
254 * @since 1.0.0
255 */
256 public function createBucket($data) {
257 $result = [
258 'success' => false,
259 'message' => esc_html__('Something went wrong', 'media-cloud-sync')
260 ];
261
262 $service = isset($data['service'])? $data['service'] : false;
263
264 if($service == false) {
265 $result = [
266 'success' => false,
267 'message' => esc_html__('No service selected', 'media-cloud-sync')
268 ];
269 return $result;
270 }
271
272 switch($service){
273 case 's3':
274 $result = [
275 'success' => false,
276 'message' => esc_html__('Insufficient Data. Please try again', 'media-cloud-sync')
277 ];
278 $config = isset($data['config']) ? $data['config'] : [];
279 $bucketData = isset($data['bucketData']) ? $data['bucketData'] : [];
280 $bucketAddNewConfig = isset($bucketData['addNewConfig']) ? $bucketData['addNewConfig'] : [];
281 $bucket = isset($bucketAddNewConfig['bucket_name']) ? $bucketAddNewConfig['bucket_name'] : false;
282 $transfer_acceleration = isset($bucketAddNewConfig['transfer_acceleration']) ? $bucketAddNewConfig['transfer_acceleration'] : false;
283 $access_key = isset($config['access_key']) ? $config['access_key'] : false;
284 $secret_key = isset($config['secret_key']) ? $config['secret_key'] : false;
285 $region = isset($config['region']) ? $config['region'] : false;
286
287 if( $access_key==false || $secret_key==false || $region==false || $bucket==false ) return $result;
288
289 $currentService = new S3;
290 return $currentService->createBucket($access_key, $secret_key, $region, $bucket, $transfer_acceleration);
291 case 'gcloud':
292 $result = [
293 'success' => false,
294 'message' => esc_html__('Insufficient Data. Please try again', 'media-cloud-sync')
295 ];
296 $config = isset($data['config']) ? $data['config'] : [];
297 $config_file = isset($config['config_json']) ? $config['config_json'] : [ 'name' => '', 'path' => '' ];
298
299 $bucketConfig = isset($data['bucketData']) ? $data['bucketData'] : [];
300 $bucketAddNewConfig = isset($bucketConfig['addNewConfig']) ? $bucketConfig['addNewConfig'] : [];
301 $bucket = isset($bucketAddNewConfig['bucket_name']) ? $bucketAddNewConfig['bucket_name'] : false;
302 $region = isset($bucketAddNewConfig['region']) ? $bucketAddNewConfig['region'] : false;
303
304 if( !(isset($config_file['path']) && !empty($config_file['path']) && file_exists($config_file['path']) && $region!=false && $bucket!=false ) ) return $result;
305
306
307 $currentService = new GCloud;
308 return $currentService->createBucket($config_file, $region, $bucket);
309 break;
310 case 'docean':
311 $result = [
312 'success' => false,
313 'message' => esc_html__('Insufficient Data. Please try again', 'media-cloud-sync')
314 ];
315
316 $config = isset($data['config']) ? $data['config'] : [];
317 $access_key = isset($config['access_key']) ? $config['access_key'] : false;
318 $secret_key = isset($config['secret_key']) ? $config['secret_key'] : false;
319 $region = isset($config['region']) ? $config['region'] : false;
320 $bucketData = isset($data['bucketData']) ? $data['bucketData'] : [];
321 $bucketConfig = isset($bucketData['addNewConfig']) ? $bucketData['addNewConfig'] : [];
322 $bucket = isset($bucketConfig['bucket_name']) ? $bucketConfig['bucket_name'] : false;
323
324 if( $access_key==false || $secret_key==false || $region==false || $bucket==false ) return $result;
325
326 $currentService = new DOcean;
327 return $currentService->createBucket($access_key, $secret_key, $region, $bucket);
328 break;
329 case 's3compatible':
330 $result = [
331 'success' => false,
332 'message' => esc_html__('Insufficient Data. Please try again', 'media-cloud-sync')
333 ];
334
335 $config = isset($data['config']) ? $data['config'] : [];
336 $access_key = isset($config['access_key']) ? $config['access_key'] : false;
337 $secret_key = isset($config['secret_key']) ? $config['secret_key'] : false;
338 $region = isset($config['region']) ? $config['region'] : false;
339 $endpoint = isset($config['endpoint']) ? $config['endpoint'] : false;
340
341 $bucketData = isset($data['bucketData']) ? $data['bucketData'] : [];
342 $bucketConfig = isset($bucketData['addNewConfig']) ? $bucketData['addNewConfig'] : [];
343 $bucket = isset($bucketConfig['bucket_name']) ? $bucketConfig['bucket_name'] : false;
344
345 if( $access_key==false || $secret_key==false || $bucket==false || $endpoint==false ) return $result;
346
347 $currentService = new S3Compatible;
348 return $currentService->createBucket($endpoint, $access_key, $secret_key, $region, $bucket, $endpoint);
349 break;
350 default:
351 $result = [
352 'success' => false,
353 'message' => esc_html__('Invalid service', 'media-cloud-sync')
354 ];
355 }
356
357 return $result;
358 }
359
360 /**
361 * Verify Object write permission
362 * @since 1.0.0
363 */
364 public function verifyObjectWritePermission($data) {
365 $result = [
366 'success' => false,
367 'message' => esc_html__('Something went wrong', 'media-cloud-sync')
368 ];
369
370 $service = isset($data['service'])? $data['service'] : false;
371
372 if($service == false) {
373 $result = [
374 'success' => false,
375 'message' => esc_html__('No service selected', 'media-cloud-sync')
376 ];
377 return $result;
378 }
379
380 switch($service){
381 case 's3':
382 $result = [
383 'success' => false,
384 'message' => esc_html__('Insufficient Data. Please try again', 'media-cloud-sync')
385 ];
386 $config = isset($data['config']) ? $data['config'] : [];
387 $bucketData = isset($data['bucketData']) ? $data['bucketData'] : [];
388 if(isset($bucketData['addNew']) && $bucketData['addNew']) {
389 $bucketConfig = isset($bucketData['addNewConfig']) ? $bucketData['addNewConfig'] : [];
390 } else {
391 $bucketConfig = isset($bucketData['config']) ? $bucketData['config'] : [];
392 }
393 $bucket = isset($bucketConfig['bucket_name']) ? $bucketConfig['bucket_name'] : false;
394 $transfer_acceleration = isset($bucketConfig['transfer_acceleration']) ? $bucketConfig['transfer_acceleration'] : false;
395 $access_key = isset($config['access_key']) ? $config['access_key'] : false;
396 $secret_key = isset($config['secret_key']) ? $config['secret_key'] : false;
397 $region = isset($config['region']) ? $config['region'] : false;
398
399 if( $access_key==false || $secret_key==false || $region==false || $bucket==false ) return $result;
400
401 $currentService = new S3;
402 return $currentService->verifyObjectWritePermission($access_key, $secret_key, $region, $bucket, $transfer_acceleration);
403
404 case 'gcloud':
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 $config_file = isset($config['config_json']) ? $config['config_json'] : [ 'name' => '', 'path' => '' ];
411
412 $bucketData = isset($data['bucketData']) ? $data['bucketData'] : [];
413 if(isset($bucketData['addNew']) && $bucketData['addNew']) {
414 $bucketConfig = isset($bucketData['addNewConfig']) ? $bucketData['addNewConfig'] : [];
415 } else {
416 $bucketConfig = isset($bucketData['config']) ? $bucketData['config'] : [];
417 }
418 $bucket = isset($bucketConfig['bucket_name']) ? $bucketConfig['bucket_name'] : false;
419
420 if( !(isset($config_file['path']) && !empty($config_file['path']) && file_exists($config_file['path']) && $bucket!=false ) ) return $result;
421
422 $currentService = new GCloud;
423 return $currentService->verifyObjectWritePermission($config_file, $bucket);
424 break;
425 case 'docean':
426 $result = [
427 'success' => false,
428 'message' => esc_html__('Insufficient Data. Please try again', 'media-cloud-sync')
429 ];
430 $config = isset($data['config']) ? $data['config'] : [];
431 $bucketData = isset($data['bucketData']) ? $data['bucketData'] : [];
432 if(isset($bucketData['addNew']) && $bucketData['addNew']) {
433 $bucketConfig = isset($bucketData['addNewConfig']) ? $bucketData['addNewConfig'] : [];
434 } else {
435 $bucketConfig = isset($bucketData['config']) ? $bucketData['config'] : [];
436 }
437 $bucket = isset($bucketConfig['bucket_name']) ? $bucketConfig['bucket_name'] : false;
438 $transfer_acceleration = isset($bucketConfig['transfer_acceleration']) ? $bucketConfig['transfer_acceleration'] : false;
439 $access_key = isset($config['access_key']) ? $config['access_key'] : false;
440 $secret_key = isset($config['secret_key']) ? $config['secret_key'] : false;
441 $region = isset($config['region']) ? $config['region'] : false;
442
443 if( $access_key==false || $secret_key==false || $region==false || $bucket==false ) return $result;
444
445 $currentService = new DOcean;
446 return $currentService->verifyObjectWritePermission($access_key, $secret_key, $region, $bucket);
447 break;
448 case 's3compatible':
449 $result = [
450 'success' => false,
451 'message' => esc_html__('Insufficient Data. Please try again', 'media-cloud-sync')
452 ];
453 $config = isset($data['config']) ? $data['config'] : [];
454 $bucketData = isset($data['bucketData']) ? $data['bucketData'] : [];
455 if(isset($bucketData['addNew']) && $bucketData['addNew']) {
456 $bucketConfig = isset($bucketData['addNewConfig']) ? $bucketData['addNewConfig'] : [];
457 } else {
458 $bucketConfig = isset($bucketData['config']) ? $bucketData['config'] : [];
459 }
460 $bucket = isset($bucketConfig['bucket_name']) ? $bucketConfig['bucket_name'] : false;
461 $transfer_acceleration = isset($bucketConfig['transfer_acceleration']) ? $bucketConfig['transfer_acceleration'] : false;
462 $access_key = isset($config['access_key']) ? $config['access_key'] : false;
463 $secret_key = isset($config['secret_key']) ? $config['secret_key'] : false;
464 $region = isset($config['region']) ? $config['region'] : false;
465 $endpoint = isset($config['endpoint']) ? $config['endpoint'] : false;
466
467 if( $access_key==false || $secret_key==false || $endpoint==false || $bucket==false ) return $result;
468 $currentService = new S3Compatible;
469 return $currentService->verifyObjectWritePermission($endpoint, $access_key, $secret_key, $region, $bucket);
470 break;
471 default:
472 break;
473 }
474
475 return false;
476 }
477
478
479 /**
480 * Verify Object delete permission
481 * @since 1.0.0
482 */
483 public function verifyObjectDeletePermission($data) {
484 $result = [
485 'success' => false,
486 'message' => esc_html__('Something went wrong', 'media-cloud-sync')
487 ];
488
489 $service = isset($data['service'])? $data['service'] : false;
490
491 if($service == false) {
492 $result = [
493 'success' => false,
494 'message' => esc_html__('No service selected', 'media-cloud-sync')
495 ];
496 return $result;
497 }
498
499 switch($service){
500 case 's3':
501 $result = [
502 'success' => false,
503 'message' => esc_html__('Insufficient Data. Please try again', 'media-cloud-sync')
504 ];
505 $config = isset($data['config']) ? $data['config'] : [];
506 $bucketData = isset($data['bucketData']) ? $data['bucketData'] : [];
507 if(isset($bucketData['addNew']) && $bucketData['addNew']) {
508 $bucketConfig = isset($bucketData['addNewConfig']) ? $bucketData['addNewConfig'] : [];
509 } else {
510 $bucketConfig = isset($bucketData['config']) ? $bucketData['config'] : [];
511 }
512 $bucket = isset($bucketConfig['bucket_name']) ? $bucketConfig['bucket_name'] : false;
513 $transfer_acceleration = isset($bucketConfig['transfer_acceleration']) ? $bucketConfig['transfer_acceleration'] : false;
514 $access_key = isset($config['access_key']) ? $config['access_key'] : false;
515 $secret_key = isset($config['secret_key']) ? $config['secret_key'] : false;
516 $region = isset($config['region']) ? $config['region'] : false;
517
518 if( $access_key==false || $secret_key==false || $region==false || $bucket==false ) return $result;
519
520 $currentService = new S3;
521 return $currentService->verifyObjectDeletePermission($access_key, $secret_key, $region, $bucket, $transfer_acceleration);
522
523 case 'gcloud':
524 $result = [
525 'success' => false,
526 'message' => esc_html__('Insufficient Data. Please try again', 'media-cloud-sync')
527 ];
528 $config = isset($data['config']) ? $data['config'] : [];
529 $config_file = isset($config['config_json']) ? $config['config_json'] : [ 'name' => '', 'path' => '' ];
530 $bucketData = isset($data['bucketData']) ? $data['bucketData'] : [];
531 if(isset($bucketData['addNew']) && $bucketData['addNew']) {
532 $bucketConfig = isset($bucketData['addNewConfig']) ? $bucketData['addNewConfig'] : [];
533 } else {
534 $bucketConfig = isset($bucketData['config']) ? $bucketData['config'] : [];
535 }
536 $bucket = isset($bucketConfig['bucket_name']) ? $bucketConfig['bucket_name'] : false;
537
538 if( !(isset($config_file['path']) && !empty($config_file['path']) && file_exists($config_file['path']) && $bucket!=false) ) return $result;
539
540 $currentService = new GCloud;
541 return $currentService->verifyObjectDeletePermission($config_file, $bucket);
542 break;
543 case 'docean':
544 $result = [
545 'success' => false,
546 'message' => esc_html__('Insufficient Data. Please try again', 'media-cloud-sync')
547 ];
548 $config = isset($data['config']) ? $data['config'] : [];
549 $bucketData = isset($data['bucketData']) ? $data['bucketData'] : [];
550 if(isset($bucketData['addNew']) && $bucketData['addNew']) {
551 $bucketConfig = isset($bucketData['addNewConfig']) ? $bucketData['addNewConfig'] : [];
552 } else {
553 $bucketConfig = isset($bucketData['config']) ? $bucketData['config'] : [];
554 }
555 $bucket = isset($bucketConfig['bucket_name']) ? $bucketConfig['bucket_name'] : false;
556 $access_key = isset($config['access_key']) ? $config['access_key'] : false;
557 $secret_key = isset($config['secret_key']) ? $config['secret_key'] : false;
558 $region = isset($config['region']) ? $config['region'] : false;
559
560 if( $access_key==false || $secret_key==false || $region==false || $bucket==false ) return $result;
561
562 $currentService = new Docean;
563 return $currentService->verifyObjectDeletePermission($access_key, $secret_key, $region, $bucket);
564 break;
565 case 's3compatible':
566 $result = [
567 'success' => false,
568 'message' => esc_html__('Insufficient Data. Please try again', 'media-cloud-sync')
569 ];
570 $config = isset($data['config']) ? $data['config'] : [];
571 $bucketData = isset($data['bucketData']) ? $data['bucketData'] : [];
572 if(isset($bucketData['addNew']) && $bucketData['addNew']) {
573 $bucketConfig = isset($bucketData['addNewConfig']) ? $bucketData['addNewConfig'] : [];
574 } else {
575 $bucketConfig = isset($bucketData['config']) ? $bucketData['config'] : [];
576 }
577 $bucket = isset($bucketConfig['bucket_name']) ? $bucketConfig['bucket_name'] : false;
578 $access_key = isset($config['access_key']) ? $config['access_key'] : false;
579 $secret_key = isset($config['secret_key']) ? $config['secret_key'] : false;
580 $region = isset($config['region']) ? $config['region'] : false;
581 $endpoint = isset($config['endpoint']) ? $config['endpoint'] : false;
582
583 if( $access_key==false || $secret_key==false || $endpoint==false || $bucket==false ) return $result;
584
585 $currentService = new S3Compatible;
586 return $currentService->verifyObjectDeletePermission($endpoint, $access_key, $secret_key, $region, $bucket);
587 break;
588 default:
589 break;
590 }
591
592 return false;
593 }
594
595
596 /**
597 * Verify Object read permission
598 * @since 1.0.0
599 */
600 public function verifyObjectReadPermission($data) {
601 return $this->service->verifyObjectReadPermission($data);
602 }
603
604 /**
605 * Get Bucket Security Settings
606 */
607 public function getBucketSecuritySettings($data) {
608 $result = [
609 'success' => false,
610 'message' => esc_html__('Something went wrong', 'media-cloud-sync')
611 ];
612
613 $service = isset($data['service'])? $data['service'] : false;
614
615 if($service == false) {
616 $result = [
617 'success' => false,
618 'message' => esc_html__('No service selected', 'media-cloud-sync')
619 ];
620 return $result;
621 }
622
623 switch($service){
624 case 's3':
625 $result = [
626 'success' => false,
627 'message' => esc_html__('Insufficient Data. Please try again', 'media-cloud-sync')
628 ];
629 $config = isset($data['config']) ? $data['config'] : [];
630 $bucketData = isset($data['bucketData']) ? $data['bucketData'] : [];
631 if(isset($bucketData['addNew']) && $bucketData['addNew']) {
632 $bucketConfig = isset($bucketData['addNewConfig']) ? $bucketData['addNewConfig'] : [];
633 } else {
634 $bucketConfig = isset($bucketData['config']) ? $bucketData['config'] : [];
635 }
636 $bucket = isset($bucketConfig['bucket_name']) ? $bucketConfig['bucket_name'] : false;
637 $transfer_acceleration = isset($bucketConfig['transfer_acceleration']) ? $bucketConfig['transfer_acceleration'] : false;
638 $access_key = isset($config['access_key']) ? $config['access_key'] : false;
639 $secret_key = isset($config['secret_key']) ? $config['secret_key'] : false;
640 $region = isset($config['region']) ? $config['region'] : false;
641
642 if( $access_key==false || $secret_key==false || $region==false || $bucket==false ) return $result;
643
644 $currentService = new S3;
645 return $currentService->getBucketSecuritySettings($access_key, $secret_key, $region, $bucket, $transfer_acceleration);
646 break;
647 default:
648 break;
649 }
650
651 return $result;
652 }
653
654
655 /**
656 * Change Bucket Public Access
657 * @since 1.0.0
658 * @param array $data
659 */
660 public function changePublicAccess($data) {
661 $result = [
662 'success' => false,
663 'message' => esc_html__('Something went wrong', 'media-cloud-sync')
664 ];
665
666 $service = isset($data['service'])? $data['service'] : false;
667
668 if($service == false) {
669 $result = [
670 'success' => false,
671 'message' => esc_html__('No service selected', 'media-cloud-sync')
672 ];
673 return $result;
674 }
675
676 switch($service){
677 case 's3':
678 $result = [
679 'success' => false,
680 'message' => esc_html__('Insufficient Data. Please try again', 'media-cloud-sync')
681 ];
682 $config = isset($data['config']) ? $data['config'] : [];
683 $bucketData = isset($data['bucketData']) ? $data['bucketData'] : [];
684 if(isset($bucketData['addNew']) && $bucketData['addNew']) {
685 $bucketConfig = isset($bucketData['addNewConfig']) ? $bucketData['addNewConfig'] : [];
686 } else {
687 $bucketConfig = isset($bucketData['config']) ? $bucketData['config'] : [];
688 }
689 $bucket = isset($bucketConfig['bucket_name']) ? $bucketConfig['bucket_name'] : false;
690 $transfer_acceleration = isset($bucketConfig['transfer_acceleration']) ? $bucketConfig['transfer_acceleration'] : false;
691 $access_key = isset($config['access_key']) ? $config['access_key'] : false;
692 $secret_key = isset($config['secret_key']) ? $config['secret_key'] : false;
693 $region = isset($config['region']) ? $config['region'] : false;
694 $value = isset($data['value']) ? $data['value'] : false;
695
696 if( $access_key==false || $secret_key==false || $region==false || $bucket==false ) return $result;
697
698 $currentService = new S3;
699 return $currentService->changePublicAccess($value, $access_key, $secret_key, $region, $bucket, $transfer_acceleration);
700 break;
701
702 default:
703 break;
704 }
705
706 return $result;
707 }
708
709 /**
710 * Change bucket ownership
711 */
712 public function changeObjectOwnership($data) {
713 $result = [
714 'success' => false,
715 'message' => esc_html__('Something went wrong', 'media-cloud-sync')
716 ];
717
718 $service = isset($data['service'])? $data['service'] : false;
719
720 if($service == false) {
721 $result = [
722 'success' => false,
723 'message' => esc_html__('No service selected', 'media-cloud-sync')
724 ];
725 return $result;
726 }
727
728 switch($service){
729 case 's3':
730 $result = [
731 'success' => false,
732 'message' => esc_html__('Insufficient Data. Please try again', 'media-cloud-sync')
733 ];
734 $config = isset($data['config']) ? $data['config'] : [];
735 $bucketData = isset($data['bucketData']) ? $data['bucketData'] : [];
736 if(isset($bucketData['addNew']) && $bucketData['addNew']) {
737 $bucketConfig = isset($bucketData['addNewConfig']) ? $bucketData['addNewConfig'] : [];
738 } else {
739 $bucketConfig = isset($bucketData['config']) ? $bucketData['config'] : [];
740 }
741 $bucket = isset($bucketConfig['bucket_name']) ? $bucketConfig['bucket_name'] : false;
742 $transfer_acceleration = isset($bucketConfig['transfer_acceleration']) ? $bucketConfig['transfer_acceleration'] : false;
743 $access_key = isset($config['access_key']) ? $config['access_key'] : false;
744 $secret_key = isset($config['secret_key']) ? $config['secret_key'] : false;
745 $region = isset($config['region']) ? $config['region'] : false;
746 $value = isset($data['value']) ? $data['value'] : false;
747
748 if( $access_key==false || $secret_key==false || $region==false || $bucket==false ) return $result;
749
750 $currentService = new S3;
751 return $currentService->changeObjectOwnership($value, $access_key, $secret_key, $region, $bucket, $transfer_acceleration);
752 break;
753
754 default:
755 break;
756 }
757
758 return $result;
759 }
760
761
762
763 /**
764 * Upload media item
765 */
766 public function uploadMedia($attachment_meta, $attachment_id, $source_path) {
767 $wpmcsItem = Item::instance();
768 $upload_dir = wp_get_upload_dir();
769 $type = get_post_mime_type($attachment_id);
770 $is_image = (0 === strpos($type, 'image/'));
771 $existing = $wpmcsItem->get($attachment_id);
772 $existing_extras = $wpmcsItem->get_extras($attachment_id);
773 $has_existing = !Utils::is_empty($existing);
774 $sizes = [];
775 $uploaded = [];
776 $extras = [];
777 $prefix = '';
778 $file_path = '';
779 $file_dir = '';
780 $original_file_path = '';
781 $original_file_source_path = '';
782
783
784 // Add prefix if object versioning is ON
785 if (isset($this->settings['object_versioning']) && $this->settings['object_versioning']) {
786 $prefix = ($existing_extras && isset($existing_extras['prefix']) && !empty($existing_extras['prefix']))
787 ? $existing_extras['prefix']
788 : Utils::generate_object_versioning_prefix();
789 $extras['prefix'] = $prefix;
790 }
791
792 // Get width and height from image meta
793 if (isset($attachment_meta) && !empty($attachment_meta)) {
794 $extras['width'] = (isset($attachment_meta['width']) && !empty($attachment_meta['width'])) ? $attachment_meta['width'] : 0;
795 $extras['height'] = (isset($attachment_meta['height']) && !empty($attachment_meta['height'])) ? $attachment_meta['height'] : 0;
796 }
797
798
799 // Get Original File Name/Path from Image meta
800 $original_file = isset($attachment_meta) && !empty($attachment_meta) &&
801 isset($attachment_meta['original_image']) && !empty($attachment_meta['original_image'])
802 ? $attachment_meta['original_image'] : '';
803
804 $file_path = trailingslashit($upload_dir['basedir']) . $source_path;
805 $file_dir = isset(pathinfo($file_path)['dirname']) ? pathinfo($file_path)['dirname'] : '';
806
807 // Check whether the extension is enabled for uploading
808 if (!Utils::is_extension_available($file_path)) {
809 return $attachment_meta;
810 }
811
812 // Upload the Full Size file
813 if( $has_existing && ( $existing['source_path'] == $source_path ) ) {
814 $uploaded = [
815 'success' => true,
816 'file_url' => $existing['url'],
817 'key' => $existing['key']
818 ];
819 } else if(file_exists($file_path)){
820 $uploaded = $this->service->uploadSingle($file_path, $source_path, $prefix);
821 }
822
823 if(
824 isset($uploaded) && !empty($uploaded) &&
825 isset($uploaded['success']) && $uploaded['success']
826 ) {
827 if(isset($original_file) && !empty($original_file)){
828 // Find original image relative and absolute path
829 $original_file_path = trailingslashit($file_dir) . $original_file;
830 $original_file_source_path = Utils::get_attachment_source_path($original_file_path);
831 $uploaded_original = [];
832
833 // Upload Original File
834 if(
835 !Utils::is_empty($existing_extras) &&
836 isset($existing_extras['original']) && !Utils::is_empty($existing_extras['original']) &&
837 $existing_extras['original']['source_path'] == $original_file_source_path
838 ) {
839 $uploaded_original = [
840 'success' => true,
841 'file_url' => $existing_extras['original']['url'],
842 'key' => $existing_extras['original']['key']
843 ];
844 } else if(file_exists($original_file_path)) {
845 $uploaded_original = $this->service->uploadSingle($original_file_path, $original_file_source_path, $prefix);
846 }
847
848 if(
849 isset($uploaded_original) && !empty($uploaded_original) &&
850 isset($uploaded_original['success']) && $uploaded_original['success']
851 ) {
852 $extras['original'] = array(
853 'source_path' => $original_file_source_path,
854 'url' => $uploaded_original['file_url'],
855 'key' => $uploaded_original['key']
856 );
857 }
858 }
859
860 if (
861 $is_image &&
862 isset($attachment_meta['sizes']) && !empty($attachment_meta['sizes']) &&
863 isset($file_dir) && !empty($file_dir)
864 ) {
865 foreach ($attachment_meta['sizes'] as $size => $sub_image) {
866 $sub_size = [];
867 $sub_file = isset($sub_image['file']) ? $sub_image['file'] : false;
868 $sub_file_path = '';
869 $sub_file_source_path = '';
870 $uploaded_sub_image = [];
871
872 if ($sub_file) {
873 $sub_file_path = $file_dir . '/' . $sub_file;
874 $sub_file_source_path = Utils::get_attachment_source_path($sub_file_path);
875 }
876
877 // Upload Image size
878 if(
879 !Utils::is_empty($existing_extras) &&
880 isset($existing_extras['sizes']) && !Utils::is_empty($existing_extras['sizes']) &&
881 isset($existing_extras['sizes'][$size]) && !Utils::is_empty($existing_extras['sizes'][$size]) &&
882 $existing_extras['sizes'][$size]['source_path'] == $sub_file_source_path
883 ) {
884 $uploaded_sub_image = [
885 'success' => true,
886 'file_url' => $existing_extras['sizes'][$size]['url'],
887 'key' => $existing_extras['sizes'][$size]['key']
888 ];
889 } else if(file_exists($sub_file_path)) {
890 $uploaded_sub_image = $this->service->uploadSingle($sub_file_path, $sub_file_source_path, $prefix);
891 }
892
893 if (
894 isset($uploaded_sub_image) && !empty($uploaded_sub_image) &&
895 isset($uploaded_sub_image['success']) && $uploaded_sub_image['success']
896 ) {
897 $sub_size['source_path'] = $sub_file_source_path;
898 $sub_size['url'] = $uploaded_sub_image['file_url'];
899 $sub_size['key'] = $uploaded_sub_image['key'];
900 $sub_size['width'] = isset($sub_image['width']) ? $sub_image['width']: 0;
901 $sub_size['height'] = isset($sub_image['height']) ? $sub_image['height']: 0;
902 }
903
904 $sizes[$size] = $sub_size;
905 }
906 }
907
908
909 if (isset($sizes) && !empty($sizes)) {
910 $extras['sizes'] = $sizes;
911 }
912
913 return [
914 'file' => [
915 'source_path' => $source_path,
916 'url' => $uploaded['file_url'],
917 'key' => $uploaded['key'],
918 ],
919 'extra' => $extras
920 ];
921
922 }
923
924 return false;
925 }
926
927 /**
928 * Delete media item
929 */
930 public function delete_attachments_by_item($item, $delete_backup = true) {
931 $upload_dir = wp_get_upload_dir();
932
933 if (isset($item['extra']) && !empty($item['extra'])) {
934 $extras = unserialize($item['extra']);
935 if (
936 isset($extras) && !empty($extras) &&
937 isset($extras['sizes']) && !empty($extras['sizes'])
938 ) {
939 foreach ($extras['sizes'] as $sub_image) {
940 if (isset($sub_image['key']) && !empty($sub_image['key'])) {
941 $this->service->deleteSingle($sub_image['key']);
942 }
943 }
944 }
945
946 if (
947 isset($extras) && !empty($extras) &&
948 isset($extras['original']) && !empty($extras['original'])
949 ) {
950 $this->service->deleteSingle($extras['original']['key']);
951 }
952
953 if (
954 isset($extras) && !empty($extras) &&
955 isset($extras['backup']) && !empty($extras['backup']) &&
956 $delete_backup
957 ) {
958 $backup = unserialize($extras['backup']);
959 if (isset($backup) && !empty($backup)) {
960 $this->delete_attachments_by_item($backup, false);
961 }
962 }
963 }
964 if (isset($item['key']) && !empty($item['key'])) {
965 $this->service->deleteSingle($item['key']);
966 }
967 }
968
969
970 /**
971 * Get presigned URL
972 * @since 1.0.0
973 */
974 public function get_presigned_url($path) {
975 $url_result = $this->service->get_presigned_url($path);
976 if(isset($url_result['success']) && $url_result['success']) {
977 return isset($url_result['file_url']) ? $url_result['file_url'] : false;
978 }
979 return false;
980 }
981
982 /**
983 * Remove query strings from service.
984 *
985 * @param string $content
986 * @param string $base_url Optional base URL that must exist within URL for Amazon query strings to be removed.
987 *
988 * @return string
989 */
990 public function remove_query_strings( $content, $base_url = '' ) {
991 $pattern = '\?[^\s"<\?]*(?:X-Amz-Algorithm|AWSAccessKeyId|Key-Pair-Id|GoogleAccessId)=[^\s"<\?]+';
992 $group = 0;
993
994 if ( ! is_string( $content ) ) {
995 return $content;
996 }
997
998 if ( ! empty( $base_url ) ) {
999 $pattern = preg_quote( $base_url, '/' ) . '[^\s"<\?]+(' . $pattern . ')';
1000 $group = 1;
1001 }
1002 if ( ! preg_match_all( '/' . $pattern . '/', $content, $matches ) || ! isset( $matches[ $group ] ) ) {
1003 // No query strings found, return
1004 return $content;
1005 }
1006
1007 $matches = array_unique( $matches[ $group ] );
1008
1009 foreach ( $matches as $match ) {
1010 $content = str_replace( $match, '', $content );
1011 }
1012 return $content;
1013 }
1014
1015
1016 /**
1017 * Move object to server from cloud
1018 */
1019 public function object_to_server($key, $save_path) {
1020 $path_parts = pathinfo($save_path);
1021 if (!file_exists($path_parts['dirname'])) {
1022 mkdir($path_parts['dirname'], 0755, true);
1023 }
1024 return $this->service->object_to_server($key, $save_path);
1025 }
1026
1027 /**
1028 * Get the service domain
1029 *
1030 */
1031 public function get_domain() {
1032 return $this->service->get_domain();
1033 }
1034
1035 /**
1036 * Ensures only one instance of Class is loaded or can be loaded.
1037 *
1038 * @return Service Class instance
1039 * @since 1.0.0
1040 * @static
1041 */
1042 public static function instance(){
1043 if (is_null(self::$instance)) {
1044 self::$instance = new self();
1045 }
1046 return self::$instance;
1047 }
1048 }