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