PluginProbe
Media Cloud Sync / 1.2.3
Media Cloud Sync v1.2.3
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.3, at includes/base/service.php

988 lines 46.2 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 $bucketData = isset($data['bucketData']) ? $data['bucketData'] : [];
530 if(isset($bucketData['addNew']) && $bucketData['addNew']) {
531 $bucketConfig = isset($bucketData['addNewConfig']) ? $bucketData['addNewConfig'] : [];
532 } else {
533 $bucketConfig = isset($bucketData['config']) ? $bucketData['config'] : [];
534 }
535 $bucket = isset($bucketConfig['bucket_name']) ? $bucketConfig['bucket_name'] : false;
536 $access_key = isset($config['access_key']) ? $config['access_key'] : false;
537 $secret_key = isset($config['secret_key']) ? $config['secret_key'] : false;
538 $region = isset($config['region']) ? $config['region'] : false;
539
540 if( $access_key==false || $secret_key==false || $region==false || $bucket==false ) return $result;
541
542 $currentService = new GCloud;
543 return $currentService->verifyObjectDeletePermission($access_key, $secret_key, $region, $bucket);
544 break;
545 case 'docean':
546 $result = [
547 'success' => false,
548 'message' => esc_html__('Insufficient Data. Please try again', 'media-cloud-sync')
549 ];
550 $config = isset($data['config']) ? $data['config'] : [];
551 $bucketData = isset($data['bucketData']) ? $data['bucketData'] : [];
552 if(isset($bucketData['addNew']) && $bucketData['addNew']) {
553 $bucketConfig = isset($bucketData['addNewConfig']) ? $bucketData['addNewConfig'] : [];
554 } else {
555 $bucketConfig = isset($bucketData['config']) ? $bucketData['config'] : [];
556 }
557 $bucket = isset($bucketConfig['bucket_name']) ? $bucketConfig['bucket_name'] : false;
558 $access_key = isset($config['access_key']) ? $config['access_key'] : false;
559 $secret_key = isset($config['secret_key']) ? $config['secret_key'] : false;
560 $region = isset($config['region']) ? $config['region'] : false;
561
562 if( $access_key==false || $secret_key==false || $region==false || $bucket==false ) return $result;
563
564 $currentService = new Docean;
565 return $currentService->verifyObjectDeletePermission($access_key, $secret_key, $region, $bucket);
566 break;
567 case 's3compatible':
568 $result = [
569 'success' => false,
570 'message' => esc_html__('Insufficient Data. Please try again', 'media-cloud-sync')
571 ];
572 $config = isset($data['config']) ? $data['config'] : [];
573 $bucketData = isset($data['bucketData']) ? $data['bucketData'] : [];
574 if(isset($bucketData['addNew']) && $bucketData['addNew']) {
575 $bucketConfig = isset($bucketData['addNewConfig']) ? $bucketData['addNewConfig'] : [];
576 } else {
577 $bucketConfig = isset($bucketData['config']) ? $bucketData['config'] : [];
578 }
579 $bucket = isset($bucketConfig['bucket_name']) ? $bucketConfig['bucket_name'] : false;
580 $access_key = isset($config['access_key']) ? $config['access_key'] : false;
581 $secret_key = isset($config['secret_key']) ? $config['secret_key'] : false;
582 $region = isset($config['region']) ? $config['region'] : false;
583 $endpoint = isset($config['endpoint']) ? $config['endpoint'] : false;
584
585 if( $access_key==false || $secret_key==false || $endpoint==false || $bucket==false ) return $result;
586
587 $currentService = new S3Compatible;
588 return $currentService->verifyObjectDeletePermission($endpoint, $access_key, $secret_key, $region, $bucket);
589 break;
590 default:
591 break;
592 }
593
594 return false;
595 }
596
597
598 /**
599 * Get Bucket Security Settings
600 */
601 public function getBucketSecuritySettings($data) {
602 $result = [
603 'success' => false,
604 'message' => esc_html__('Something went wrong', 'media-cloud-sync')
605 ];
606
607 $service = isset($data['service'])? $data['service'] : false;
608
609 if($service == false) {
610 $result = [
611 'success' => false,
612 'message' => esc_html__('No service selected', 'media-cloud-sync')
613 ];
614 return $result;
615 }
616
617 switch($service){
618 case 's3':
619 $result = [
620 'success' => false,
621 'message' => esc_html__('Insufficient Data. Please try again', 'media-cloud-sync')
622 ];
623 $config = isset($data['config']) ? $data['config'] : [];
624 $bucketData = isset($data['bucketData']) ? $data['bucketData'] : [];
625 if(isset($bucketData['addNew']) && $bucketData['addNew']) {
626 $bucketConfig = isset($bucketData['addNewConfig']) ? $bucketData['addNewConfig'] : [];
627 } else {
628 $bucketConfig = isset($bucketData['config']) ? $bucketData['config'] : [];
629 }
630 $bucket = isset($bucketConfig['bucket_name']) ? $bucketConfig['bucket_name'] : false;
631 $transfer_acceleration = isset($bucketConfig['transfer_acceleration']) ? $bucketConfig['transfer_acceleration'] : false;
632 $access_key = isset($config['access_key']) ? $config['access_key'] : false;
633 $secret_key = isset($config['secret_key']) ? $config['secret_key'] : false;
634 $region = isset($config['region']) ? $config['region'] : false;
635
636 if( $access_key==false || $secret_key==false || $region==false || $bucket==false ) return $result;
637
638 $currentService = new S3;
639 return $currentService->getBucketSecuritySettings($access_key, $secret_key, $region, $bucket, $transfer_acceleration);
640 break;
641 default:
642 break;
643 }
644
645 return $result;
646 }
647
648
649 /**
650 * Change bucket ownership
651 */
652 public function changeObjectOwnership($data) {
653 $result = [
654 'success' => false,
655 'message' => esc_html__('Something went wrong', 'media-cloud-sync')
656 ];
657
658 $service = isset($data['service'])? $data['service'] : false;
659
660 if($service == false) {
661 $result = [
662 'success' => false,
663 'message' => esc_html__('No service selected', 'media-cloud-sync')
664 ];
665 return $result;
666 }
667
668 switch($service){
669 case 's3':
670 $result = [
671 'success' => false,
672 'message' => esc_html__('Insufficient Data. Please try again', 'media-cloud-sync')
673 ];
674 $config = isset($data['config']) ? $data['config'] : [];
675 $bucketData = isset($data['bucketData']) ? $data['bucketData'] : [];
676 if(isset($bucketData['addNew']) && $bucketData['addNew']) {
677 $bucketConfig = isset($bucketData['addNewConfig']) ? $bucketData['addNewConfig'] : [];
678 } else {
679 $bucketConfig = isset($bucketData['config']) ? $bucketData['config'] : [];
680 }
681 $bucket = isset($bucketConfig['bucket_name']) ? $bucketConfig['bucket_name'] : false;
682 $transfer_acceleration = isset($bucketConfig['transfer_acceleration']) ? $bucketConfig['transfer_acceleration'] : false;
683 $access_key = isset($config['access_key']) ? $config['access_key'] : false;
684 $secret_key = isset($config['secret_key']) ? $config['secret_key'] : false;
685 $region = isset($config['region']) ? $config['region'] : false;
686 $value = isset($data['value']) ? $data['value'] : false;
687
688 if( $access_key==false || $secret_key==false || $region==false || $bucket==false ) return $result;
689
690 $currentService = new S3;
691 return $currentService->changeObjectOwnership($value, $access_key, $secret_key, $region, $bucket, $transfer_acceleration);
692 break;
693
694 default:
695 break;
696 }
697
698 return $result;
699 }
700
701
702
703 /**
704 * Upload media item
705 */
706 public function uploadMedia($attachment_meta, $attachment_id, $source_path) {
707 $wpmcsItem = Item::instance();
708 $upload_dir = wp_get_upload_dir();
709 $type = get_post_mime_type($attachment_id);
710 $is_image = (0 === strpos($type, 'image/'));
711 $existing = $wpmcsItem->get($attachment_id);
712 $existing_extras = $wpmcsItem->get_extras($attachment_id);
713 $has_existing = !Utils::is_empty($existing);
714 $sizes = [];
715 $uploaded = [];
716 $extras = [];
717 $prefix = '';
718 $file_path = '';
719 $file_dir = '';
720 $original_file_path = '';
721 $original_file_source_path = '';
722
723
724 // Add prefix if object versioning is ON
725 if (isset($this->settings['object_versioning']) && $this->settings['object_versioning']) {
726 $prefix = ($existing_extras && isset($existing_extras['prefix']) && !empty($existing_extras['prefix']))
727 ? $existing_extras['prefix']
728 : Utils::generate_object_versioning_prefix();
729 $extras['prefix'] = $prefix;
730 }
731
732 // Get width and height from image meta
733 if (isset($attachment_meta) && !empty($attachment_meta)) {
734 $extras['width'] = (isset($attachment_meta['width']) && !empty($attachment_meta['width'])) ? $attachment_meta['width'] : 0;
735 $extras['height'] = (isset($attachment_meta['height']) && !empty($attachment_meta['height'])) ? $attachment_meta['height'] : 0;
736 }
737
738
739 // Get Original File Name/Path from Image meta
740 $original_file = isset($attachment_meta) && !empty($attachment_meta) &&
741 isset($attachment_meta['original_image']) && !empty($attachment_meta['original_image'])
742 ? $attachment_meta['original_image'] : '';
743
744 $file_path = trailingslashit($upload_dir['basedir']) . $source_path;
745 $file_dir = isset(pathinfo($file_path)['dirname']) ? pathinfo($file_path)['dirname'] : '';
746
747 // Check whether the extension is enabled for uploading
748 if (!Utils::is_extension_available($file_path)) {
749 return $attachment_meta;
750 }
751
752 // Upload the Full Size file
753 if( $has_existing && ( $existing['source_path'] == $source_path ) ) {
754 $uploaded = [
755 'success' => true,
756 'file_url' => $existing['url'],
757 'key' => $existing['key']
758 ];
759 } else if(file_exists($file_path)){
760 $uploaded = $this->service->uploadSingle($file_path, $source_path, $prefix);
761 }
762
763 if(
764 isset($uploaded) && !empty($uploaded) &&
765 isset($uploaded['success']) && $uploaded['success']
766 ) {
767 if(isset($original_file) && !empty($original_file)){
768 // Find original image relative and absolute path
769 $original_file_path = trailingslashit($file_dir) . $original_file;
770 $original_file_source_path = Utils::get_attachment_source_path($original_file_path);
771 $uploaded_original = [];
772
773 // Upload Original File
774 if(
775 !Utils::is_empty($existing_extras) &&
776 isset($existing_extras['original']) && !Utils::is_empty($existing_extras['original']) &&
777 $existing_extras['original']['source_path'] == $original_file_source_path
778 ) {
779 $uploaded_original = [
780 'success' => true,
781 'file_url' => $existing_extras['original']['url'],
782 'key' => $existing_extras['original']['key']
783 ];
784 } else if(file_exists($original_file_path)) {
785 $uploaded_original = $this->service->uploadSingle($original_file_path, $original_file_source_path, $prefix);
786 }
787
788 if(
789 isset($uploaded_original) && !empty($uploaded_original) &&
790 isset($uploaded_original['success']) && $uploaded_original['success']
791 ) {
792 $extras['original'] = array(
793 'source_path' => $original_file_source_path,
794 'url' => $uploaded_original['file_url'],
795 'key' => $uploaded_original['key']
796 );
797 }
798 }
799
800 if (
801 $is_image &&
802 isset($attachment_meta['sizes']) && !empty($attachment_meta['sizes']) &&
803 isset($file_dir) && !empty($file_dir)
804 ) {
805 foreach ($attachment_meta['sizes'] as $size => $sub_image) {
806 $sub_size = [];
807 $sub_file = isset($sub_image['file']) ? $sub_image['file'] : false;
808 $sub_file_path = '';
809 $sub_file_source_path = '';
810 $uploaded_sub_image = [];
811
812 if ($sub_file) {
813 $sub_file_path = $file_dir . '/' . $sub_file;
814 $sub_file_source_path = Utils::get_attachment_source_path($sub_file_path);
815 }
816
817 // Upload Image size
818 if(
819 !Utils::is_empty($existing_extras) &&
820 isset($existing_extras['sizes']) && !Utils::is_empty($existing_extras['sizes']) &&
821 isset($existing_extras['sizes'][$size]) && !Utils::is_empty($existing_extras['sizes'][$size]) &&
822 $existing_extras['sizes'][$size]['source_path'] == $sub_file_source_path
823 ) {
824 $uploaded_sub_image = [
825 'success' => true,
826 'file_url' => $existing_extras['sizes'][$size]['url'],
827 'key' => $existing_extras['sizes'][$size]['key']
828 ];
829 } else if(file_exists($sub_file_path)) {
830 $uploaded_sub_image = $this->service->uploadSingle($sub_file_path, $sub_file_source_path, $prefix);
831 }
832
833 if (
834 isset($uploaded_sub_image) && !empty($uploaded_sub_image) &&
835 isset($uploaded_sub_image['success']) && $uploaded_sub_image['success']
836 ) {
837 $sub_size['source_path'] = $sub_file_source_path;
838 $sub_size['url'] = $uploaded_sub_image['file_url'];
839 $sub_size['key'] = $uploaded_sub_image['key'];
840 $sub_size['width'] = isset($sub_image['width']) ? $sub_image['width']: 0;
841 $sub_size['height'] = isset($sub_image['height']) ? $sub_image['height']: 0;
842 }
843
844 $sizes[$size] = $sub_size;
845 }
846 }
847
848
849 if (isset($sizes) && !empty($sizes)) {
850 $extras['sizes'] = $sizes;
851 }
852
853 return [
854 'file' => [
855 'source_path' => $source_path,
856 'url' => $uploaded['file_url'],
857 'key' => $uploaded['key'],
858 ],
859 'extra' => $extras
860 ];
861
862 }
863
864 return false;
865 }
866
867 /**
868 * Delete media item
869 */
870 public function delete_attachments_by_item($item, $delete_backup = true) {
871 $upload_dir = wp_get_upload_dir();
872
873 if (isset($item['extra']) && !empty($item['extra'])) {
874 $extras = unserialize($item['extra']);
875 if (
876 isset($extras) && !empty($extras) &&
877 isset($extras['sizes']) && !empty($extras['sizes'])
878 ) {
879 foreach ($extras['sizes'] as $sub_image) {
880 if (isset($sub_image['key']) && !empty($sub_image['key'])) {
881 $this->service->deleteSingle($sub_image['key']);
882 }
883 }
884 }
885
886 if (
887 isset($extras) && !empty($extras) &&
888 isset($extras['original']) && !empty($extras['original'])
889 ) {
890 $this->service->deleteSingle($extras['original']['key']);
891 }
892
893 if (
894 isset($extras) && !empty($extras) &&
895 isset($extras['backup']) && !empty($extras['backup']) &&
896 $delete_backup
897 ) {
898 $backup = unserialize($extras['backup']);
899 if (isset($backup) && !empty($backup)) {
900 $this->delete_attachments_by_item($backup, false);
901 }
902 }
903 }
904 if (isset($item['key']) && !empty($item['key'])) {
905 $this->service->deleteSingle($item['key']);
906 }
907 }
908
909
910 /**
911 * Get presigned URL
912 * @since 1.0.0
913 */
914 public function get_presigned_url($path) {
915 $url_result = $this->service->get_presigned_url($path);
916 if(isset($url_result['success']) && $url_result['success']) {
917 return isset($url_result['file_url']) ? $url_result['file_url'] : false;
918 }
919 return false;
920 }
921
922 /**
923 * Remove query strings from service.
924 *
925 * @param string $content
926 * @param string $base_url Optional base URL that must exist within URL for Amazon query strings to be removed.
927 *
928 * @return string
929 */
930 public function remove_query_strings( $content, $base_url = '' ) {
931 $pattern = '\?[^\s"<\?]*(?:X-Amz-Algorithm|AWSAccessKeyId|Key-Pair-Id|GoogleAccessId)=[^\s"<\?]+';
932 $group = 0;
933
934 if ( ! is_string( $content ) ) {
935 return $content;
936 }
937
938 if ( ! empty( $base_url ) ) {
939 $pattern = preg_quote( $base_url, '/' ) . '[^\s"<\?]+(' . $pattern . ')';
940 $group = 1;
941 }
942 if ( ! preg_match_all( '/' . $pattern . '/', $content, $matches ) || ! isset( $matches[ $group ] ) ) {
943 // No query strings found, return
944 return $content;
945 }
946
947 $matches = array_unique( $matches[ $group ] );
948
949 foreach ( $matches as $match ) {
950 $content = str_replace( $match, '', $content );
951 }
952 return $content;
953 }
954
955
956 /**
957 * Move object to server from cloud
958 */
959 public function object_to_server($key, $save_path) {
960 $path_parts = pathinfo($save_path);
961 if (!file_exists($path_parts['dirname'])) {
962 mkdir($path_parts['dirname'], 0755, true);
963 }
964 return $this->service->object_to_server($key, $save_path);
965 }
966
967 /**
968 * Get the service domain
969 *
970 */
971 public function get_domain() {
972 return $this->service->get_domain();
973 }
974
975 /**
976 * Ensures only one instance of Class is loaded or can be loaded.
977 *
978 * @return Service Class instance
979 * @since 1.0.0
980 * @static
981 */
982 public static function instance(){
983 if (is_null(self::$instance)) {
984 self::$instance = new self();
985 }
986 return self::$instance;
987 }
988 }