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

530 lines 20.8 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 default:$this->service = false;
39 }
40 }
41
42 /**
43 * Verify Service Credentials
44 * @since 1.0.0
45 */
46 public function verifyCredentials($data) {
47 $result = [
48 'success' => false,
49 'message' => esc_html__('Something went wrong', 'media-cloud-sync')
50 ];
51
52 $service = isset($data['service'])? $data['service'] : false;
53
54 if($service == false) {
55 $result = [
56 'success' => false,
57 'message' => esc_html__('No service selected', 'media-cloud-sync')
58 ];
59 return $result;
60 }
61
62 switch($service){
63 case 's3':
64 $config = isset($data['config']) ? $data['config'] : [];
65 $access_key = isset($config['access_key']) ? $config['access_key'] : false;
66 $secret_key = isset($config['secret_key']) ? $config['secret_key'] : false;
67 $region = isset($config['region']) ? $config['region'] : false;
68
69 if($access_key==false || $secret_key==false || $region==false) {
70 $result = [
71 'success' => false,
72 'message' => esc_html__('Insufficient Data. Please try again', 'media-cloud-sync')
73 ];
74 return $result;
75 }
76
77 $currentService = new S3;
78 return $currentService->verifyCredentials($access_key, $secret_key, $region);
79 break;
80 case 'gcloud':
81 $config = isset($data['config']) ? $data['config'] : [];
82 $config_file = isset($config['config_json']) ? $config['config_json'] : [ 'name' => '', 'path' => '' ];
83
84 if(!(isset($config_file['path']) && !empty($config_file['path']) && file_exists($config_file['path']))) {
85 $result = [
86 'success' => false,
87 'message' => esc_html__('Insufficient Data. Please try again', 'media-cloud-sync')
88 ];
89 return $result;
90 }
91
92 $currentService = new GCloud;
93 return $currentService->verifyCredentials($config_file);
94 break;
95 case 'docean':
96 $currentService = new DOcean;
97 break;
98 default:
99 $result = [
100 'success' => false,
101 'message' => esc_html__('Invalid service', 'media-cloud-sync')
102 ];
103 }
104
105 return $result;
106 }
107
108 /**
109 * Verify Bucket Credentials
110 * @since 1.0.0
111 */
112 public function verifyBucket($data) {
113 $result = [
114 'success' => false,
115 'message' => esc_html__('Something went wrong', 'media-cloud-sync')
116 ];
117
118 $service = isset($data['service'])? $data['service'] : false;
119
120 if($service == false) {
121 $result = [
122 'success' => false,
123 'message' => esc_html__('No service selected', 'media-cloud-sync')
124 ];
125 return $result;
126 }
127
128 switch($service){
129 case 's3':
130 $result = [
131 'success' => false,
132 'message' => esc_html__('Insufficient Data. Please try again', 'media-cloud-sync')
133 ];
134 $config = isset($data['config']) ? $data['config'] : [];
135 $bucketConfig = isset($data['bucketConfig']) ? $data['bucketConfig'] : [];
136 $bucket = isset($bucketConfig['bucket_name']) ? $bucketConfig['bucket_name'] : false;
137 $transfer_acceleration = isset($bucketConfig['transfer_acceleration']) ? $bucketConfig['transfer_acceleration'] : false;
138 $access_key = isset($config['access_key']) ? $config['access_key'] : false;
139 $secret_key = isset($config['secret_key']) ? $config['secret_key'] : false;
140 $region = isset($config['region']) ? $config['region'] : false;
141
142 if( $access_key==false || $secret_key==false || $region==false || $bucket==false ) return $result;
143
144 $currentService = new S3;
145
146
147 return $currentService->verifyBucket($access_key, $secret_key, $region, $bucket, $transfer_acceleration);
148
149 case 'gcloud':
150 $result = [
151 'success' => false,
152 'message' => esc_html__('Insufficient Data. Please try again', 'media-cloud-sync')
153 ];
154 $config = isset($data['config']) ? $data['config'] : [];
155 $config_file = isset($config['config_json']) ? $config['config_json'] : [ 'name' => '', 'path' => '' ];
156 $bucketConfig = isset($data['bucketConfig']) ? $data['bucketConfig'] : [];
157 $bucket = isset($bucketConfig['bucket_name']) ? $bucketConfig['bucket_name'] : false;
158
159 if( !(isset($config_file['path']) && !empty($config_file['path']) && file_exists($config_file['path']) && $bucket!=false ) ) return $result;
160
161 $currentService = new GCloud;
162 return $currentService->verifyBucket($config_file, $bucket);
163 break;
164 case 'docean':
165 $currentService = new DOcean;
166 break;
167 default:
168 $result = [
169 'success' => false,
170 'message' => esc_html__('Invalid service', 'media-cloud-sync')
171 ];
172 }
173
174 return $result;
175 }
176
177 /**
178 * Verify Bucket Credentials
179 * @since 1.0.0
180 */
181 public function createBucket($data) {
182 $result = [
183 'success' => false,
184 'message' => esc_html__('Something went wrong', 'media-cloud-sync')
185 ];
186
187 $service = isset($data['service'])? $data['service'] : false;
188
189 if($service == false) {
190 $result = [
191 'success' => false,
192 'message' => esc_html__('No service selected', 'media-cloud-sync')
193 ];
194 return $result;
195 }
196
197 switch($service){
198 case 's3':
199 $result = [
200 'success' => false,
201 'message' => esc_html__('Insufficient Data. Please try again', 'media-cloud-sync')
202 ];
203 $config = isset($data['config']) ? $data['config'] : [];
204 $bucketConfig = isset($data['bucketConfig']) ? $data['bucketConfig'] : [];
205 $bucket = isset($bucketConfig['bucket_name']) ? $bucketConfig['bucket_name'] : false;
206 $transfer_acceleration = isset($bucketConfig['transfer_acceleration']) ? $bucketConfig['transfer_acceleration'] : false;
207 $access_key = isset($config['access_key']) ? $config['access_key'] : false;
208 $secret_key = isset($config['secret_key']) ? $config['secret_key'] : false;
209 $region = isset($config['region']) ? $config['region'] : false;
210
211 if( $access_key==false || $secret_key==false || $region==false || $bucket==false ) return $result;
212
213 $currentService = new S3;
214 return $currentService->createBucket($access_key, $secret_key, $region, $bucket, $transfer_acceleration);
215 case 'gcloud':
216 $result = [
217 'success' => false,
218 'message' => esc_html__('Insufficient Data. Please try again', 'media-cloud-sync')
219 ];
220 $config = isset($data['config']) ? $data['config'] : [];
221 $config_file = isset($config['config_json']) ? $config['config_json'] : [ 'name' => '', 'path' => '' ];
222 $bucketConfig = isset($data['bucketConfig']) ? $data['bucketConfig'] : [];
223 $bucket = isset($bucketConfig['bucket_name']) ? $bucketConfig['bucket_name'] : false;
224 $region = isset($bucketConfig['region']) ? $bucketConfig['region'] : false;
225
226 if( !(isset($config_file['path']) && !empty($config_file['path']) && file_exists($config_file['path']) && $region!=false && $bucket!=false ) ) return $result;
227
228 $currentService = new GCloud;
229 return $currentService->createBucket($config_file, $region, $bucket);
230 break;
231 case 'docean':
232 $currentService = new DOcean;
233 break;
234 default:
235 $result = [
236 'success' => false,
237 'message' => esc_html__('Invalid service', 'media-cloud-sync')
238 ];
239 }
240
241 return $result;
242 }
243
244 /**
245 * Upload media item
246 */
247 public function uploadMedia($attachment_meta, $attachment_id, $source_path) {
248 global $wpmcsItem;
249 $upload_dir = wp_get_upload_dir();
250 $type = get_post_mime_type($attachment_id);
251 $is_image = (0 === strpos($type, 'image/'));
252 $existing = $wpmcsItem->get($attachment_id);
253 $existing_extras = $wpmcsItem->get_extras($attachment_id);
254 $has_existing = !Utils::is_empty($existing);
255 $sizes = [];
256 $uploaded = [];
257 $extras = [];
258 $prefix = '';
259 $file_path = '';
260 $file_dir = '';
261 $original_file_path = '';
262 $original_file_source_path = '';
263
264
265 // Add prefix if object versioning is ON
266 if (isset($this->settings['object_versioning']) && $this->settings['object_versioning']) {
267 $prefix = ($existing_extras && isset($existing_extras['prefix']) && !empty($existing_extras['prefix']))
268 ? $existing_extras['prefix']
269 : Utils::generate_object_versioning_prefix();
270 $extras['prefix'] = $prefix;
271 }
272
273 // Get width and height from image meta
274 if (isset($attachment_meta) && !empty($attachment_meta)) {
275 $extras['width'] = (isset($attachment_meta['width']) && !empty($attachment_meta['width'])) ? $attachment_meta['width'] : 0;
276 $extras['height'] = (isset($attachment_meta['height']) && !empty($attachment_meta['height'])) ? $attachment_meta['height'] : 0;
277 }
278
279
280 // Get Original File Name/Path from Image meta
281 $original_file = isset($attachment_meta) && !empty($attachment_meta) &&
282 isset($attachment_meta['original_image']) && !empty($attachment_meta['original_image'])
283 ? $attachment_meta['original_image'] : '';
284
285 $file_path = trailingslashit($upload_dir['basedir']) . $source_path;
286 $file_dir = isset(pathinfo($file_path)['dirname']) ? pathinfo($file_path)['dirname'] : '';
287
288 // Check whether the extension is enabled for uploading
289 if (!Utils::is_extension_available($file_path)) {
290 return $attachment_meta;
291 }
292
293 // Upload the Full Size file
294 if( $has_existing && ( $existing['source_path'] == $source_path ) ) {
295 $uploaded = [
296 'success' => true,
297 'file_url' => $existing['url'],
298 'key' => $existing['key']
299 ];
300 } else if(file_exists($file_path)){
301 $uploaded = $this->service->uploadSingle($file_path, $source_path, $prefix);
302 }
303
304 if(
305 isset($uploaded) && !empty($uploaded) &&
306 isset($uploaded['success']) && $uploaded['success']
307 ) {
308 if(isset($original_file) && !empty($original_file)){
309 // Find original image relative and absolute path
310 $original_file_path = trailingslashit($file_dir) . $original_file;
311 $original_file_source_path = Utils::get_attachment_source_path($original_file_path);
312 $uploaded_original = [];
313
314 // Upload Original File
315 if(
316 !Utils::is_empty($existing_extras) &&
317 isset($existing_extras['original']) && !Utils::is_empty($existing_extras['original']) &&
318 $existing_extras['original']['source_path'] == $original_file_source_path
319 ) {
320 $uploaded_original = [
321 'success' => true,
322 'file_url' => $existing_extras['original']['url'],
323 'key' => $existing_extras['original']['key']
324 ];
325 } else if(file_exists($original_file_path)) {
326 $uploaded_original = $this->service->uploadSingle($original_file_path, $original_file_source_path, $prefix);
327 }
328
329 if(
330 isset($uploaded_original) && !empty($uploaded_original) &&
331 isset($uploaded_original['success']) && $uploaded_original['success']
332 ) {
333 $extras['original'] = array(
334 'source_path' => $original_file_source_path,
335 'url' => $uploaded_original['file_url'],
336 'key' => $uploaded_original['key']
337 );
338 }
339 }
340
341 if (
342 $is_image &&
343 isset($attachment_meta['sizes']) && !empty($attachment_meta['sizes']) &&
344 isset($file_dir) && !empty($file_dir)
345 ) {
346 foreach ($attachment_meta['sizes'] as $size => $sub_image) {
347 $sub_size = [];
348 $sub_file = isset($sub_image['file']) ? $sub_image['file'] : false;
349 $sub_file_path = '';
350 $sub_file_source_path = '';
351 $uploaded_sub_image = [];
352
353 if ($sub_file) {
354 $sub_file_path = $file_dir . '/' . $sub_file;
355 $sub_file_source_path = Utils::get_attachment_source_path($sub_file_path);
356 }
357
358 // Upload Image size
359 if(
360 !Utils::is_empty($existing_extras) &&
361 isset($existing_extras['sizes']) && !Utils::is_empty($existing_extras['sizes']) &&
362 isset($existing_extras['sizes'][$size]) && !Utils::is_empty($existing_extras['sizes'][$size]) &&
363 $existing_extras['sizes'][$size]['source_path'] == $sub_file_source_path
364 ) {
365 $uploaded_sub_image = [
366 'success' => true,
367 'file_url' => $existing_extras['sizes'][$size]['url'],
368 'key' => $existing_extras['sizes'][$size]['key']
369 ];
370 } else if(file_exists($sub_file_path)) {
371 $uploaded_sub_image = $this->service->uploadSingle($sub_file_path, $sub_file_source_path, $prefix);
372 }
373
374 if (
375 isset($uploaded_sub_image) && !empty($uploaded_sub_image) &&
376 isset($uploaded_sub_image['success']) && $uploaded_sub_image['success']
377 ) {
378 $sub_size['source_path'] = $sub_file_source_path;
379 $sub_size['url'] = $uploaded_sub_image['file_url'];
380 $sub_size['key'] = $uploaded_sub_image['key'];
381 $sub_size['width'] = isset($sub_image['width']) ? $sub_image['width']: 0;
382 $sub_size['height'] = isset($sub_image['height']) ? $sub_image['height']: 0;
383 }
384
385 $sizes[$size] = $sub_size;
386 }
387 }
388
389
390 if (isset($sizes) && !empty($sizes)) {
391 $extras['sizes'] = $sizes;
392 }
393
394 return [
395 'file' => [
396 'source_path' => $source_path,
397 'url' => $uploaded['file_url'],
398 'key' => $uploaded['key'],
399 ],
400 'extra' => $extras
401 ];
402
403 }
404
405 return false;
406 }
407
408 /**
409 * Delete media item
410 */
411 public function delete_attachments_by_item($item, $delete_backup = true) {
412 global $wpmcsItem;
413 $upload_dir = wp_get_upload_dir();
414
415 if (isset($item['extra']) && !empty($item['extra'])) {
416 $extras = unserialize($item['extra']);
417 if (
418 isset($extras) && !empty($extras) &&
419 isset($extras['sizes']) && !empty($extras['sizes'])
420 ) {
421 foreach ($extras['sizes'] as $sub_image) {
422 if (isset($sub_image['key']) && !empty($sub_image['key'])) {
423 $this->service->deleteSingle($sub_image['key']);
424 }
425 }
426 }
427
428 if (
429 isset($extras) && !empty($extras) &&
430 isset($extras['original']) && !empty($extras['original'])
431 ) {
432 $this->service->deleteSingle($extras['original']['key']);
433 }
434
435 if (
436 isset($extras) && !empty($extras) &&
437 isset($extras['backup']) && !empty($extras['backup']) &&
438 $delete_backup
439 ) {
440 $backup = unserialize($extras['backup']);
441 if (isset($backup) && !empty($backup)) {
442 $this->delete_attachments_by_item($backup, false);
443 }
444 }
445 }
446 if (isset($item['key']) && !empty($item['key'])) {
447 $this->service->deleteSingle($item['key']);
448 }
449 }
450
451
452 /**
453 * Get presigned URL
454 * @since 1.0.0
455 */
456 public function get_presigned_url($path) {
457 $url_result = $this->service->get_presigned_url($path);
458 if(isset($url_result['success']) && $url_result['success']) {
459 return isset($url_result['file_url']) ? $url_result['file_url'] : false;
460 }
461 return false;
462 }
463
464 /**
465 * Remove query strings from service.
466 *
467 * @param string $content
468 * @param string $base_url Optional base URL that must exist within URL for Amazon query strings to be removed.
469 *
470 * @return string
471 */
472 public function remove_query_strings( $content, $base_url = '' ) {
473 $pattern = '\?[^\s"<\?]*(?:X-Amz-Algorithm|AWSAccessKeyId|Key-Pair-Id|GoogleAccessId)=[^\s"<\?]+';
474 $group = 0;
475
476 if ( ! is_string( $content ) ) {
477 return $content;
478 }
479
480 if ( ! empty( $base_url ) ) {
481 $pattern = preg_quote( $base_url, '/' ) . '[^\s"<\?]+(' . $pattern . ')';
482 $group = 1;
483 }
484 if ( ! preg_match_all( '/' . $pattern . '/', $content, $matches ) || ! isset( $matches[ $group ] ) ) {
485 // No query strings found, return
486 return $content;
487 }
488
489 $matches = array_unique( $matches[ $group ] );
490
491 foreach ( $matches as $match ) {
492 $content = str_replace( $match, '', $content );
493 }
494 return $content;
495 }
496
497
498 /**
499 * Move object to server from cloud
500 */
501 public function object_to_server($key, $save_path) {
502 $path_parts = pathinfo($save_path);
503 if (!file_exists($path_parts['dirname'])) {
504 mkdir($path_parts['dirname'], 0755, true);
505 }
506 return $this->service->object_to_server($key, $save_path);
507 }
508
509 /**
510 * Get the service domain
511 *
512 */
513 public function get_domain() {
514 return $this->service->get_domain();
515 }
516
517 /**
518 * Ensures only one instance of Class is loaded or can be loaded.
519 *
520 * @return Service Class instance
521 * @since 1.0.0
522 * @static
523 */
524 public static function instance(){
525 if (is_null(self::$instance)) {
526 self::$instance = new self();
527 }
528 return self::$instance;
529 }
530 }