PluginProbe
Media Cloud Sync / 1.2.0
Media Cloud Sync v1.2.0
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.0, at includes/base/service.php

991 lines 46.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 namespace Dudlewebs\WPMCS;
3
4 defined('ABSPATH') || exit;
5
6 class Service {
7 private static $instance = null;
8 private $assets_url;
9 private $version;
10 private $token;
11 private $service = false;
12
13 protected $settings;
14
15
16 /**
17 * Admin constructor.
18 * @since 1.0.0
19 */
20 public function __construct() {
21 $this->assets_url = WPMCS_ASSETS_URL;
22 $this->version = WPMCS_VERSION;
23 $this->token = WPMCS_TOKEN;
24
25 $this->settings = Utils::get_settings();
26
27 $current_service = Utils::get_service();
28 switch ($current_service) {
29 case 's3':
30 $this->service = new S3;
31 break;
32 case 'gcloud':
33 $this->service = new GCloud;
34 break;
35 case 'docean':
36 $this->service = new DOcean;
37 break;
38 case 's3compatible':
39 $this->service = new S3Compatible;
40 break;
41 default:$this->service = false;
42 }
43 }
44
45 /**
46 * Verify Service Credentials
47 * @since 1.0.0
48 */
49 public function verifyCredentials($data) {
50 $result = [
51 'success' => false,
52 'message' => esc_html__('Something went wrong', 'media-cloud-sync')
53 ];
54
55 $service = isset($data['service'])? $data['service'] : false;
56
57 if($service == false) {
58 $result = [
59 'success' => false,
60 'message' => esc_html__('No service selected', 'media-cloud-sync')
61 ];
62 return $result;
63 }
64
65 switch($service){
66 case 's3':
67 $config = isset($data['config']) ? $data['config'] : [];
68 $access_key = isset($config['access_key']) ? $config['access_key'] : false;
69 $secret_key = isset($config['secret_key']) ? $config['secret_key'] : false;
70 $region = isset($config['region']) ? $config['region'] : false;
71
72 if($access_key==false || $secret_key==false || $region==false) {
73 $result = [
74 'success' => false,
75 'message' => esc_html__('Insufficient Data. Please try again', 'media-cloud-sync')
76 ];
77 return $result;
78 }
79
80 $currentService = new S3;
81 return $currentService->verifyCredentials($access_key, $secret_key, $region);
82 break;
83 case 'gcloud':
84 $config = isset($data['config']) ? $data['config'] : [];
85 $config_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 $region = isset($bucketConfig['region']) ? $bucketConfig['region'] : false;
324
325 if( $access_key==false || $secret_key==false || $region==false || $bucket==false ) return $result;
326
327 $currentService = new DOcean;
328 return $currentService->createBucket($access_key, $secret_key, $region, $bucket);
329 break;
330 case 's3compatible':
331 $result = [
332 'success' => false,
333 'message' => esc_html__('Insufficient Data. Please try again', 'media-cloud-sync')
334 ];
335
336 $config = isset($data['config']) ? $data['config'] : [];
337 $access_key = isset($config['access_key']) ? $config['access_key'] : false;
338 $secret_key = isset($config['secret_key']) ? $config['secret_key'] : false;
339 $region = isset($config['region']) ? $config['region'] : false;
340 $endpoint = isset($config['endpoint']) ? $config['endpoint'] : false;
341
342 $bucketData = isset($data['bucketData']) ? $data['bucketData'] : [];
343 $bucketConfig = isset($bucketData['addNewConfig']) ? $bucketData['addNewConfig'] : [];
344 $bucket = isset($bucketConfig['bucket_name']) ? $bucketConfig['bucket_name'] : false;
345 $region = isset($bucketConfig['region']) ? $bucketConfig['region'] : false;
346
347 if( $access_key==false || $secret_key==false || $bucket==false || $endpoint==false ) return $result;
348
349 $currentService = new S3Compatible;
350 return $currentService->createBucket($endpoint, $access_key, $secret_key, $region, $bucket, $endpoint);
351 break;
352 default:
353 $result = [
354 'success' => false,
355 'message' => esc_html__('Invalid service', 'media-cloud-sync')
356 ];
357 }
358
359 return $result;
360 }
361
362 /**
363 * Verify Object write permission
364 * @since 1.0.0
365 */
366 public function verifyObjectWritePermission($data) {
367 $result = [
368 'success' => false,
369 'message' => esc_html__('Something went wrong', 'media-cloud-sync')
370 ];
371
372 $service = isset($data['service'])? $data['service'] : false;
373
374 if($service == false) {
375 $result = [
376 'success' => false,
377 'message' => esc_html__('No service selected', 'media-cloud-sync')
378 ];
379 return $result;
380 }
381
382 switch($service){
383 case 's3':
384 $result = [
385 'success' => false,
386 'message' => esc_html__('Insufficient Data. Please try again', 'media-cloud-sync')
387 ];
388 $config = isset($data['config']) ? $data['config'] : [];
389 $bucketData = isset($data['bucketData']) ? $data['bucketData'] : [];
390 if(isset($bucketData['addNew']) && $bucketData['addNew']) {
391 $bucketConfig = isset($bucketData['addNewConfig']) ? $bucketData['addNewConfig'] : [];
392 } else {
393 $bucketConfig = isset($bucketData['config']) ? $bucketData['config'] : [];
394 }
395 $bucket = isset($bucketConfig['bucket_name']) ? $bucketConfig['bucket_name'] : false;
396 $transfer_acceleration = isset($bucketConfig['transfer_acceleration']) ? $bucketConfig['transfer_acceleration'] : false;
397 $access_key = isset($config['access_key']) ? $config['access_key'] : false;
398 $secret_key = isset($config['secret_key']) ? $config['secret_key'] : false;
399 $region = isset($config['region']) ? $config['region'] : false;
400
401 if( $access_key==false || $secret_key==false || $region==false || $bucket==false ) return $result;
402
403 $currentService = new S3;
404 return $currentService->verifyObjectWritePermission($access_key, $secret_key, $region, $bucket, $transfer_acceleration);
405
406 case 'gcloud':
407 $result = [
408 'success' => false,
409 'message' => esc_html__('Insufficient Data. Please try again', 'media-cloud-sync')
410 ];
411 $config = isset($data['config']) ? $data['config'] : [];
412 $config_file = isset($config['config_json']) ? $config['config_json'] : [ 'name' => '', 'path' => '' ];
413
414 $bucketData = isset($data['bucketData']) ? $data['bucketData'] : [];
415 if(isset($bucketData['addNew']) && $bucketData['addNew']) {
416 $bucketConfig = isset($bucketData['addNewConfig']) ? $bucketData['addNewConfig'] : [];
417 } else {
418 $bucketConfig = isset($bucketData['config']) ? $bucketData['config'] : [];
419 }
420 $bucket = isset($bucketConfig['bucket_name']) ? $bucketConfig['bucket_name'] : false;
421
422 if( !(isset($config_file['path']) && !empty($config_file['path']) && file_exists($config_file['path']) && $bucket!=false ) ) return $result;
423
424 $currentService = new GCloud;
425 return $currentService->verifyObjectWritePermission($config_file, $bucket);
426 break;
427 case 'docean':
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 $bucketData = isset($data['bucketData']) ? $data['bucketData'] : [];
434 if(isset($bucketData['addNew']) && $bucketData['addNew']) {
435 $bucketConfig = isset($bucketData['addNewConfig']) ? $bucketData['addNewConfig'] : [];
436 } else {
437 $bucketConfig = isset($bucketData['config']) ? $bucketData['config'] : [];
438 }
439 $bucket = isset($bucketConfig['bucket_name']) ? $bucketConfig['bucket_name'] : false;
440 $transfer_acceleration = isset($bucketConfig['transfer_acceleration']) ? $bucketConfig['transfer_acceleration'] : false;
441 $access_key = isset($config['access_key']) ? $config['access_key'] : false;
442 $secret_key = isset($config['secret_key']) ? $config['secret_key'] : false;
443 $region = isset($config['region']) ? $config['region'] : false;
444
445 if( $access_key==false || $secret_key==false || $region==false || $bucket==false ) return $result;
446
447 $currentService = new DOcean;
448 return $currentService->verifyObjectWritePermission($access_key, $secret_key, $region, $bucket);
449 break;
450 case 's3compatible':
451 $result = [
452 'success' => false,
453 'message' => esc_html__('Insufficient Data. Please try again', 'media-cloud-sync')
454 ];
455 $config = isset($data['config']) ? $data['config'] : [];
456 $bucketData = isset($data['bucketData']) ? $data['bucketData'] : [];
457 if(isset($bucketData['addNew']) && $bucketData['addNew']) {
458 $bucketConfig = isset($bucketData['addNewConfig']) ? $bucketData['addNewConfig'] : [];
459 } else {
460 $bucketConfig = isset($bucketData['config']) ? $bucketData['config'] : [];
461 }
462 $bucket = isset($bucketConfig['bucket_name']) ? $bucketConfig['bucket_name'] : false;
463 $transfer_acceleration = isset($bucketConfig['transfer_acceleration']) ? $bucketConfig['transfer_acceleration'] : false;
464 $access_key = isset($config['access_key']) ? $config['access_key'] : false;
465 $secret_key = isset($config['secret_key']) ? $config['secret_key'] : false;
466 $region = isset($config['region']) ? $config['region'] : false;
467 $endpoint = isset($config['endpoint']) ? $config['endpoint'] : false;
468
469 if( $access_key==false || $secret_key==false || $endpoint==false || $bucket==false ) return $result;
470 $currentService = new S3Compatible;
471 return $currentService->verifyObjectWritePermission($endpoint, $access_key, $secret_key, $region, $bucket);
472 break;
473 default:
474 break;
475 }
476
477 return false;
478 }
479
480
481 /**
482 * Verify Object delete permission
483 * @since 1.0.0
484 */
485 public function verifyObjectDeletePermission($data) {
486 $result = [
487 'success' => false,
488 'message' => esc_html__('Something went wrong', 'media-cloud-sync')
489 ];
490
491 $service = isset($data['service'])? $data['service'] : false;
492
493 if($service == false) {
494 $result = [
495 'success' => false,
496 'message' => esc_html__('No service selected', 'media-cloud-sync')
497 ];
498 return $result;
499 }
500
501 switch($service){
502 case 's3':
503 $result = [
504 'success' => false,
505 'message' => esc_html__('Insufficient Data. Please try again', 'media-cloud-sync')
506 ];
507 $config = isset($data['config']) ? $data['config'] : [];
508 $bucketData = isset($data['bucketData']) ? $data['bucketData'] : [];
509 if(isset($bucketData['addNew']) && $bucketData['addNew']) {
510 $bucketConfig = isset($bucketData['addNewConfig']) ? $bucketData['addNewConfig'] : [];
511 } else {
512 $bucketConfig = isset($bucketData['config']) ? $bucketData['config'] : [];
513 }
514 $bucket = isset($bucketConfig['bucket_name']) ? $bucketConfig['bucket_name'] : false;
515 $transfer_acceleration = isset($bucketConfig['transfer_acceleration']) ? $bucketConfig['transfer_acceleration'] : false;
516 $access_key = isset($config['access_key']) ? $config['access_key'] : false;
517 $secret_key = isset($config['secret_key']) ? $config['secret_key'] : false;
518 $region = isset($config['region']) ? $config['region'] : false;
519
520 if( $access_key==false || $secret_key==false || $region==false || $bucket==false ) return $result;
521
522 $currentService = new S3;
523 return $currentService->verifyObjectDeletePermission($access_key, $secret_key, $region, $bucket, $transfer_acceleration);
524
525 case 'gcloud':
526 $result = [
527 'success' => false,
528 'message' => esc_html__('Insufficient Data. Please try again', 'media-cloud-sync')
529 ];
530 $config = isset($data['config']) ? $data['config'] : [];
531 $bucketData = isset($data['bucketData']) ? $data['bucketData'] : [];
532 if(isset($bucketData['addNew']) && $bucketData['addNew']) {
533 $bucketConfig = isset($bucketData['addNewConfig']) ? $bucketData['addNewConfig'] : [];
534 } else {
535 $bucketConfig = isset($bucketData['config']) ? $bucketData['config'] : [];
536 }
537 $bucket = isset($bucketConfig['bucket_name']) ? $bucketConfig['bucket_name'] : false;
538 $access_key = isset($config['access_key']) ? $config['access_key'] : false;
539 $secret_key = isset($config['secret_key']) ? $config['secret_key'] : false;
540 $region = isset($config['region']) ? $config['region'] : false;
541
542 if( $access_key==false || $secret_key==false || $region==false || $bucket==false ) return $result;
543
544 $currentService = new GCloud;
545 return $currentService->verifyObjectDeletePermission($access_key, $secret_key, $region, $bucket);
546 break;
547 case 'docean':
548 $result = [
549 'success' => false,
550 'message' => esc_html__('Insufficient Data. Please try again', 'media-cloud-sync')
551 ];
552 $config = isset($data['config']) ? $data['config'] : [];
553 $bucketData = isset($data['bucketData']) ? $data['bucketData'] : [];
554 if(isset($bucketData['addNew']) && $bucketData['addNew']) {
555 $bucketConfig = isset($bucketData['addNewConfig']) ? $bucketData['addNewConfig'] : [];
556 } else {
557 $bucketConfig = isset($bucketData['config']) ? $bucketData['config'] : [];
558 }
559 $bucket = isset($bucketConfig['bucket_name']) ? $bucketConfig['bucket_name'] : false;
560 $access_key = isset($config['access_key']) ? $config['access_key'] : false;
561 $secret_key = isset($config['secret_key']) ? $config['secret_key'] : false;
562 $region = isset($config['region']) ? $config['region'] : false;
563
564 if( $access_key==false || $secret_key==false || $region==false || $bucket==false ) return $result;
565
566 $currentService = new Docean;
567 return $currentService->verifyObjectDeletePermission($access_key, $secret_key, $region, $bucket);
568 break;
569 case 's3compatible':
570 $result = [
571 'success' => false,
572 'message' => esc_html__('Insufficient Data. Please try again', 'media-cloud-sync')
573 ];
574 $config = isset($data['config']) ? $data['config'] : [];
575 $bucketData = isset($data['bucketData']) ? $data['bucketData'] : [];
576 if(isset($bucketData['addNew']) && $bucketData['addNew']) {
577 $bucketConfig = isset($bucketData['addNewConfig']) ? $bucketData['addNewConfig'] : [];
578 } else {
579 $bucketConfig = isset($bucketData['config']) ? $bucketData['config'] : [];
580 }
581 $bucket = isset($bucketConfig['bucket_name']) ? $bucketConfig['bucket_name'] : false;
582 $access_key = isset($config['access_key']) ? $config['access_key'] : false;
583 $secret_key = isset($config['secret_key']) ? $config['secret_key'] : false;
584 $region = isset($config['region']) ? $config['region'] : false;
585 $endpoint = isset($config['endpoint']) ? $config['endpoint'] : false;
586
587 if( $access_key==false || $secret_key==false || $endpoint==false || $bucket==false ) return $result;
588
589 $currentService = new S3Compatible;
590 return $currentService->verifyObjectDeletePermission($endpoint, $access_key, $secret_key, $region, $bucket);
591 break;
592 default:
593 break;
594 }
595
596 return false;
597 }
598
599
600 /**
601 * Get Bucket Security Settings
602 */
603 public function getBucketSecuritySettings($data) {
604 $result = [
605 'success' => false,
606 'message' => esc_html__('Something went wrong', 'media-cloud-sync')
607 ];
608
609 $service = isset($data['service'])? $data['service'] : false;
610
611 if($service == false) {
612 $result = [
613 'success' => false,
614 'message' => esc_html__('No service selected', 'media-cloud-sync')
615 ];
616 return $result;
617 }
618
619 switch($service){
620 case 's3':
621 $result = [
622 'success' => false,
623 'message' => esc_html__('Insufficient Data. Please try again', 'media-cloud-sync')
624 ];
625 $config = isset($data['config']) ? $data['config'] : [];
626 $bucketData = isset($data['bucketData']) ? $data['bucketData'] : [];
627 if(isset($bucketData['addNew']) && $bucketData['addNew']) {
628 $bucketConfig = isset($bucketData['addNewConfig']) ? $bucketData['addNewConfig'] : [];
629 } else {
630 $bucketConfig = isset($bucketData['config']) ? $bucketData['config'] : [];
631 }
632 $bucket = isset($bucketConfig['bucket_name']) ? $bucketConfig['bucket_name'] : false;
633 $transfer_acceleration = isset($bucketConfig['transfer_acceleration']) ? $bucketConfig['transfer_acceleration'] : false;
634 $access_key = isset($config['access_key']) ? $config['access_key'] : false;
635 $secret_key = isset($config['secret_key']) ? $config['secret_key'] : false;
636 $region = isset($config['region']) ? $config['region'] : false;
637
638 if( $access_key==false || $secret_key==false || $region==false || $bucket==false ) return $result;
639
640 $currentService = new S3;
641 return $currentService->getBucketSecuritySettings($access_key, $secret_key, $region, $bucket, $transfer_acceleration);
642 break;
643 default:
644 break;
645 }
646
647 return $result;
648 }
649
650
651 /**
652 * Change bucket ownership
653 */
654 public function changeObjectOwnership($data) {
655 $result = [
656 'success' => false,
657 'message' => esc_html__('Something went wrong', 'media-cloud-sync')
658 ];
659
660 $service = isset($data['service'])? $data['service'] : false;
661
662 if($service == false) {
663 $result = [
664 'success' => false,
665 'message' => esc_html__('No service selected', 'media-cloud-sync')
666 ];
667 return $result;
668 }
669
670 switch($service){
671 case 's3':
672 $result = [
673 'success' => false,
674 'message' => esc_html__('Insufficient Data. Please try again', 'media-cloud-sync')
675 ];
676 $config = isset($data['config']) ? $data['config'] : [];
677 $bucketData = isset($data['bucketData']) ? $data['bucketData'] : [];
678 if(isset($bucketData['addNew']) && $bucketData['addNew']) {
679 $bucketConfig = isset($bucketData['addNewConfig']) ? $bucketData['addNewConfig'] : [];
680 } else {
681 $bucketConfig = isset($bucketData['config']) ? $bucketData['config'] : [];
682 }
683 $bucket = isset($bucketConfig['bucket_name']) ? $bucketConfig['bucket_name'] : false;
684 $transfer_acceleration = isset($bucketConfig['transfer_acceleration']) ? $bucketConfig['transfer_acceleration'] : false;
685 $access_key = isset($config['access_key']) ? $config['access_key'] : false;
686 $secret_key = isset($config['secret_key']) ? $config['secret_key'] : false;
687 $region = isset($config['region']) ? $config['region'] : false;
688 $value = isset($data['value']) ? $data['value'] : false;
689
690 if( $access_key==false || $secret_key==false || $region==false || $bucket==false ) return $result;
691
692 $currentService = new S3;
693 return $currentService->changeObjectOwnership($value, $access_key, $secret_key, $region, $bucket, $transfer_acceleration);
694 break;
695
696 default:
697 break;
698 }
699
700 return $result;
701 }
702
703
704
705 /**
706 * Upload media item
707 */
708 public function uploadMedia($attachment_meta, $attachment_id, $source_path) {
709 global $wpmcsItem;
710 $upload_dir = wp_get_upload_dir();
711 $type = get_post_mime_type($attachment_id);
712 $is_image = (0 === strpos($type, 'image/'));
713 $existing = $wpmcsItem->get($attachment_id);
714 $existing_extras = $wpmcsItem->get_extras($attachment_id);
715 $has_existing = !Utils::is_empty($existing);
716 $sizes = [];
717 $uploaded = [];
718 $extras = [];
719 $prefix = '';
720 $file_path = '';
721 $file_dir = '';
722 $original_file_path = '';
723 $original_file_source_path = '';
724
725
726 // Add prefix if object versioning is ON
727 if (isset($this->settings['object_versioning']) && $this->settings['object_versioning']) {
728 $prefix = ($existing_extras && isset($existing_extras['prefix']) && !empty($existing_extras['prefix']))
729 ? $existing_extras['prefix']
730 : Utils::generate_object_versioning_prefix();
731 $extras['prefix'] = $prefix;
732 }
733
734 // Get width and height from image meta
735 if (isset($attachment_meta) && !empty($attachment_meta)) {
736 $extras['width'] = (isset($attachment_meta['width']) && !empty($attachment_meta['width'])) ? $attachment_meta['width'] : 0;
737 $extras['height'] = (isset($attachment_meta['height']) && !empty($attachment_meta['height'])) ? $attachment_meta['height'] : 0;
738 }
739
740
741 // Get Original File Name/Path from Image meta
742 $original_file = isset($attachment_meta) && !empty($attachment_meta) &&
743 isset($attachment_meta['original_image']) && !empty($attachment_meta['original_image'])
744 ? $attachment_meta['original_image'] : '';
745
746 $file_path = trailingslashit($upload_dir['basedir']) . $source_path;
747 $file_dir = isset(pathinfo($file_path)['dirname']) ? pathinfo($file_path)['dirname'] : '';
748
749 // Check whether the extension is enabled for uploading
750 if (!Utils::is_extension_available($file_path)) {
751 return $attachment_meta;
752 }
753
754 // Upload the Full Size file
755 if( $has_existing && ( $existing['source_path'] == $source_path ) ) {
756 $uploaded = [
757 'success' => true,
758 'file_url' => $existing['url'],
759 'key' => $existing['key']
760 ];
761 } else if(file_exists($file_path)){
762 $uploaded = $this->service->uploadSingle($file_path, $source_path, $prefix);
763 }
764
765 if(
766 isset($uploaded) && !empty($uploaded) &&
767 isset($uploaded['success']) && $uploaded['success']
768 ) {
769 if(isset($original_file) && !empty($original_file)){
770 // Find original image relative and absolute path
771 $original_file_path = trailingslashit($file_dir) . $original_file;
772 $original_file_source_path = Utils::get_attachment_source_path($original_file_path);
773 $uploaded_original = [];
774
775 // Upload Original File
776 if(
777 !Utils::is_empty($existing_extras) &&
778 isset($existing_extras['original']) && !Utils::is_empty($existing_extras['original']) &&
779 $existing_extras['original']['source_path'] == $original_file_source_path
780 ) {
781 $uploaded_original = [
782 'success' => true,
783 'file_url' => $existing_extras['original']['url'],
784 'key' => $existing_extras['original']['key']
785 ];
786 } else if(file_exists($original_file_path)) {
787 $uploaded_original = $this->service->uploadSingle($original_file_path, $original_file_source_path, $prefix);
788 }
789
790 if(
791 isset($uploaded_original) && !empty($uploaded_original) &&
792 isset($uploaded_original['success']) && $uploaded_original['success']
793 ) {
794 $extras['original'] = array(
795 'source_path' => $original_file_source_path,
796 'url' => $uploaded_original['file_url'],
797 'key' => $uploaded_original['key']
798 );
799 }
800 }
801
802 if (
803 $is_image &&
804 isset($attachment_meta['sizes']) && !empty($attachment_meta['sizes']) &&
805 isset($file_dir) && !empty($file_dir)
806 ) {
807 foreach ($attachment_meta['sizes'] as $size => $sub_image) {
808 $sub_size = [];
809 $sub_file = isset($sub_image['file']) ? $sub_image['file'] : false;
810 $sub_file_path = '';
811 $sub_file_source_path = '';
812 $uploaded_sub_image = [];
813
814 if ($sub_file) {
815 $sub_file_path = $file_dir . '/' . $sub_file;
816 $sub_file_source_path = Utils::get_attachment_source_path($sub_file_path);
817 }
818
819 // Upload Image size
820 if(
821 !Utils::is_empty($existing_extras) &&
822 isset($existing_extras['sizes']) && !Utils::is_empty($existing_extras['sizes']) &&
823 isset($existing_extras['sizes'][$size]) && !Utils::is_empty($existing_extras['sizes'][$size]) &&
824 $existing_extras['sizes'][$size]['source_path'] == $sub_file_source_path
825 ) {
826 $uploaded_sub_image = [
827 'success' => true,
828 'file_url' => $existing_extras['sizes'][$size]['url'],
829 'key' => $existing_extras['sizes'][$size]['key']
830 ];
831 } else if(file_exists($sub_file_path)) {
832 $uploaded_sub_image = $this->service->uploadSingle($sub_file_path, $sub_file_source_path, $prefix);
833 }
834
835 if (
836 isset($uploaded_sub_image) && !empty($uploaded_sub_image) &&
837 isset($uploaded_sub_image['success']) && $uploaded_sub_image['success']
838 ) {
839 $sub_size['source_path'] = $sub_file_source_path;
840 $sub_size['url'] = $uploaded_sub_image['file_url'];
841 $sub_size['key'] = $uploaded_sub_image['key'];
842 $sub_size['width'] = isset($sub_image['width']) ? $sub_image['width']: 0;
843 $sub_size['height'] = isset($sub_image['height']) ? $sub_image['height']: 0;
844 }
845
846 $sizes[$size] = $sub_size;
847 }
848 }
849
850
851 if (isset($sizes) && !empty($sizes)) {
852 $extras['sizes'] = $sizes;
853 }
854
855 return [
856 'file' => [
857 'source_path' => $source_path,
858 'url' => $uploaded['file_url'],
859 'key' => $uploaded['key'],
860 ],
861 'extra' => $extras
862 ];
863
864 }
865
866 return false;
867 }
868
869 /**
870 * Delete media item
871 */
872 public function delete_attachments_by_item($item, $delete_backup = true) {
873 global $wpmcsItem;
874 $upload_dir = wp_get_upload_dir();
875
876 if (isset($item['extra']) && !empty($item['extra'])) {
877 $extras = unserialize($item['extra']);
878 if (
879 isset($extras) && !empty($extras) &&
880 isset($extras['sizes']) && !empty($extras['sizes'])
881 ) {
882 foreach ($extras['sizes'] as $sub_image) {
883 if (isset($sub_image['key']) && !empty($sub_image['key'])) {
884 $this->service->deleteSingle($sub_image['key']);
885 }
886 }
887 }
888
889 if (
890 isset($extras) && !empty($extras) &&
891 isset($extras['original']) && !empty($extras['original'])
892 ) {
893 $this->service->deleteSingle($extras['original']['key']);
894 }
895
896 if (
897 isset($extras) && !empty($extras) &&
898 isset($extras['backup']) && !empty($extras['backup']) &&
899 $delete_backup
900 ) {
901 $backup = unserialize($extras['backup']);
902 if (isset($backup) && !empty($backup)) {
903 $this->delete_attachments_by_item($backup, false);
904 }
905 }
906 }
907 if (isset($item['key']) && !empty($item['key'])) {
908 $this->service->deleteSingle($item['key']);
909 }
910 }
911
912
913 /**
914 * Get presigned URL
915 * @since 1.0.0
916 */
917 public function get_presigned_url($path) {
918 $url_result = $this->service->get_presigned_url($path);
919 if(isset($url_result['success']) && $url_result['success']) {
920 return isset($url_result['file_url']) ? $url_result['file_url'] : false;
921 }
922 return false;
923 }
924
925 /**
926 * Remove query strings from service.
927 *
928 * @param string $content
929 * @param string $base_url Optional base URL that must exist within URL for Amazon query strings to be removed.
930 *
931 * @return string
932 */
933 public function remove_query_strings( $content, $base_url = '' ) {
934 $pattern = '\?[^\s"<\?]*(?:X-Amz-Algorithm|AWSAccessKeyId|Key-Pair-Id|GoogleAccessId)=[^\s"<\?]+';
935 $group = 0;
936
937 if ( ! is_string( $content ) ) {
938 return $content;
939 }
940
941 if ( ! empty( $base_url ) ) {
942 $pattern = preg_quote( $base_url, '/' ) . '[^\s"<\?]+(' . $pattern . ')';
943 $group = 1;
944 }
945 if ( ! preg_match_all( '/' . $pattern . '/', $content, $matches ) || ! isset( $matches[ $group ] ) ) {
946 // No query strings found, return
947 return $content;
948 }
949
950 $matches = array_unique( $matches[ $group ] );
951
952 foreach ( $matches as $match ) {
953 $content = str_replace( $match, '', $content );
954 }
955 return $content;
956 }
957
958
959 /**
960 * Move object to server from cloud
961 */
962 public function object_to_server($key, $save_path) {
963 $path_parts = pathinfo($save_path);
964 if (!file_exists($path_parts['dirname'])) {
965 mkdir($path_parts['dirname'], 0755, true);
966 }
967 return $this->service->object_to_server($key, $save_path);
968 }
969
970 /**
971 * Get the service domain
972 *
973 */
974 public function get_domain() {
975 return $this->service->get_domain();
976 }
977
978 /**
979 * Ensures only one instance of Class is loaded or can be loaded.
980 *
981 * @return Service Class instance
982 * @since 1.0.0
983 * @static
984 */
985 public static function instance(){
986 if (is_null(self::$instance)) {
987 self::$instance = new self();
988 }
989 return self::$instance;
990 }
991 }