| 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 |
private $providers = [ |
| 13 |
's3' => ['class' => 'S3', 'sdk' => 's3'], |
| 14 |
'gcloud' => ['class' => 'GCloud', 'sdk' => 'google'], |
| 15 |
'docean' => ['class' => 'DOcean', 'sdk' => 's3'], |
| 16 |
'cloudflareR2' => ['class' => 'CloudflareR2', 'sdk' => 's3'], |
| 17 |
's3compatible' => ['class' => 'S3Compatible', 'sdk' => 's3'], |
| 18 |
]; |
| 19 |
|
| 20 |
protected $settings; |
| 21 |
|
| 22 |
|
| 23 |
/** |
| 24 |
* Service constructor. |
| 25 |
* @since 1.0.0 |
| 26 |
*/ |
| 27 |
public function __construct() { |
| 28 |
$this->assets_url = WPMCS_ASSETS_URL; |
| 29 |
$this->version = WPMCS_VERSION; |
| 30 |
$this->token = WPMCS_TOKEN; |
| 31 |
|
| 32 |
$this->settings = Utils::get_settings(); |
| 33 |
|
| 34 |
$current_service = Utils::get_service(); |
| 35 |
|
| 36 |
if($current_service) { |
| 37 |
$this->service = $this->get_handler_class($current_service); |
| 38 |
} |
| 39 |
} |
| 40 |
|
| 41 |
/** |
| 42 |
* Verify Service Credentials |
| 43 |
* @since 1.0.0 |
| 44 |
*/ |
| 45 |
public function verifyCredentials($data) { |
| 46 |
$result = [ |
| 47 |
'success' => false, |
| 48 |
'message' => esc_html__('Something went wrong', 'media-cloud-sync') |
| 49 |
]; |
| 50 |
|
| 51 |
$service = isset($data['service'])? $data['service'] : false; |
| 52 |
$handler_class = $this->get_handler_class($service); |
| 53 |
|
| 54 |
if($service == false || $handler_class == false) { |
| 55 |
$result = [ |
| 56 |
'success' => false, |
| 57 |
'message' => esc_html__('No service selected', 'media-cloud-sync') |
| 58 |
]; |
| 59 |
return $result; |
| 60 |
} |
| 61 |
$config = isset($data['config']) ? $data['config'] : []; |
| 62 |
|
| 63 |
return $handler_class->verifyCredentials($config); |
| 64 |
} |
| 65 |
|
| 66 |
/** |
| 67 |
* Verify Bucket Exist |
| 68 |
* @since 1.0.0 |
| 69 |
*/ |
| 70 |
public function verifyBucketExist($data) { |
| 71 |
$result = [ |
| 72 |
'success' => false, |
| 73 |
'message' => esc_html__('Something went wrong', 'media-cloud-sync') |
| 74 |
]; |
| 75 |
|
| 76 |
$service = isset($data['service'])? $data['service'] : false; |
| 77 |
$handler_class = $this->get_handler_class($service); |
| 78 |
|
| 79 |
if($service == false || $handler_class == false) { |
| 80 |
$result = [ |
| 81 |
'success' => false, |
| 82 |
'message' => esc_html__('No service selected', 'media-cloud-sync') |
| 83 |
]; |
| 84 |
return $result; |
| 85 |
} |
| 86 |
|
| 87 |
$config = isset($data['config']) ? $data['config'] : []; |
| 88 |
$bucketData = isset($data['bucketData']) ? $data['bucketData'] : []; |
| 89 |
$bucketConfig = isset($bucketData['config']) ? $bucketData['config'] : []; |
| 90 |
|
| 91 |
return $handler_class->verifyBucketExist($config, $bucketConfig); |
| 92 |
} |
| 93 |
|
| 94 |
/** |
| 95 |
* Verify Bucket Credentials |
| 96 |
* @since 1.0.0 |
| 97 |
*/ |
| 98 |
public function createBucket($data) { |
| 99 |
$result = [ |
| 100 |
'success' => false, |
| 101 |
'message' => esc_html__('Something went wrong', 'media-cloud-sync') |
| 102 |
]; |
| 103 |
|
| 104 |
$service = isset($data['service'])? $data['service'] : false; |
| 105 |
$handler_class = $this->get_handler_class($service); |
| 106 |
|
| 107 |
if($service == false || $handler_class == false) { |
| 108 |
$result = [ |
| 109 |
'success' => false, |
| 110 |
'message' => esc_html__('No service selected', 'media-cloud-sync') |
| 111 |
]; |
| 112 |
return $result; |
| 113 |
} |
| 114 |
|
| 115 |
$config = isset($data['config']) ? $data['config'] : []; |
| 116 |
$bucketData = isset($data['bucketData']) ? $data['bucketData'] : []; |
| 117 |
$bucketAddNewConfig = isset($bucketData['addNewConfig']) ? $bucketData['addNewConfig'] : []; |
| 118 |
|
| 119 |
return $handler_class->createBucket( $config, $bucketAddNewConfig ); |
| 120 |
} |
| 121 |
|
| 122 |
/** |
| 123 |
* Verify Object write permission |
| 124 |
* @since 1.0.0 |
| 125 |
*/ |
| 126 |
public function verifyObjectWritePermission($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 |
$handler_class = $this->get_handler_class($service); |
| 134 |
|
| 135 |
if($service == false || $handler_class == false) { |
| 136 |
$result = [ |
| 137 |
'success' => false, |
| 138 |
'message' => esc_html__('No service selected', 'media-cloud-sync') |
| 139 |
]; |
| 140 |
return $result; |
| 141 |
} |
| 142 |
|
| 143 |
$config = isset($data['config']) ? $data['config'] : []; |
| 144 |
$bucketData = isset($data['bucketData']) ? $data['bucketData'] : []; |
| 145 |
if(isset($bucketData['addNew']) && $bucketData['addNew']) { |
| 146 |
$bucketConfig = isset($bucketData['addNewConfig']) ? $bucketData['addNewConfig'] : []; |
| 147 |
} else { |
| 148 |
$bucketConfig = isset($bucketData['config']) ? $bucketData['config'] : []; |
| 149 |
} |
| 150 |
|
| 151 |
return $handler_class->verifyObjectWritePermission($config, $bucketConfig); |
| 152 |
} |
| 153 |
|
| 154 |
|
| 155 |
/** |
| 156 |
* Verify Object delete permission |
| 157 |
* @since 1.0.0 |
| 158 |
*/ |
| 159 |
public function verifyObjectDeletePermission($data) { |
| 160 |
$result = [ |
| 161 |
'success' => false, |
| 162 |
'message' => esc_html__('Something went wrong', 'media-cloud-sync') |
| 163 |
]; |
| 164 |
|
| 165 |
$service = isset($data['service'])? $data['service'] : false; |
| 166 |
$handler_class = $this->get_handler_class($service); |
| 167 |
|
| 168 |
if($service == false || $handler_class == false) { |
| 169 |
$result = [ |
| 170 |
'success' => false, |
| 171 |
'message' => esc_html__('No service selected', 'media-cloud-sync') |
| 172 |
]; |
| 173 |
return $result; |
| 174 |
} |
| 175 |
|
| 176 |
$config = isset($data['config']) ? $data['config'] : []; |
| 177 |
$bucketData = isset($data['bucketData']) ? $data['bucketData'] : []; |
| 178 |
if(isset($bucketData['addNew']) && $bucketData['addNew']) { |
| 179 |
$bucketConfig = isset($bucketData['addNewConfig']) ? $bucketData['addNewConfig'] : []; |
| 180 |
} else { |
| 181 |
$bucketConfig = isset($bucketData['config']) ? $bucketData['config'] : []; |
| 182 |
} |
| 183 |
|
| 184 |
return $handler_class->verifyObjectDeletePermission( $config, $bucketConfig ); |
| 185 |
} |
| 186 |
|
| 187 |
|
| 188 |
/** |
| 189 |
* Verify Object read permission |
| 190 |
* @since 1.0.0 |
| 191 |
*/ |
| 192 |
public function verifyObjectReadPermission() { |
| 193 |
return $this->service->verifyObjectReadPermission(); |
| 194 |
} |
| 195 |
|
| 196 |
/** |
| 197 |
* Get Bucket Security Settings |
| 198 |
*/ |
| 199 |
public function getBucketSecuritySettings($data) { |
| 200 |
$result = [ |
| 201 |
'success' => false, |
| 202 |
'message' => esc_html__('Something went wrong', 'media-cloud-sync') |
| 203 |
]; |
| 204 |
|
| 205 |
$service = isset($data['service'])? $data['service'] : false; |
| 206 |
$handler_class = $this->get_handler_class($service); |
| 207 |
|
| 208 |
if($service == false || $handler_class == false) { |
| 209 |
$result = [ |
| 210 |
'success' => false, |
| 211 |
'message' => esc_html__('No service selected', 'media-cloud-sync') |
| 212 |
]; |
| 213 |
return $result; |
| 214 |
} |
| 215 |
|
| 216 |
if(!method_exists($handler_class, 'getBucketSecuritySettings')) { |
| 217 |
$result = [ |
| 218 |
'success' => false, |
| 219 |
'message' => esc_html__('Service does not have getBucketSecuritySettings method', 'media-cloud-sync') |
| 220 |
]; |
| 221 |
return $result; |
| 222 |
} |
| 223 |
|
| 224 |
$config = isset($data['config']) ? $data['config'] : []; |
| 225 |
$bucketData = isset($data['bucketData']) ? $data['bucketData'] : []; |
| 226 |
if(isset($bucketData['addNew']) && $bucketData['addNew']) { |
| 227 |
$bucketConfig = isset($bucketData['addNewConfig']) ? $bucketData['addNewConfig'] : []; |
| 228 |
} else { |
| 229 |
$bucketConfig = isset($bucketData['config']) ? $bucketData['config'] : []; |
| 230 |
} |
| 231 |
|
| 232 |
return $handler_class->getBucketSecuritySettings( $config, $bucketConfig ); |
| 233 |
} |
| 234 |
|
| 235 |
|
| 236 |
/** |
| 237 |
* Change Bucket Public Access |
| 238 |
* @since 1.0.0 |
| 239 |
* @param array $data |
| 240 |
*/ |
| 241 |
public function changePublicAccess($data) { |
| 242 |
$result = [ |
| 243 |
'success' => false, |
| 244 |
'message' => esc_html__('Something went wrong', 'media-cloud-sync') |
| 245 |
]; |
| 246 |
|
| 247 |
$service = isset($data['service'])? $data['service'] : false; |
| 248 |
$handler_class = $this->get_handler_class($service); |
| 249 |
|
| 250 |
if($service == false || $handler_class == false) { |
| 251 |
$result = [ |
| 252 |
'success' => false, |
| 253 |
'message' => esc_html__('No service selected', 'media-cloud-sync') |
| 254 |
]; |
| 255 |
return $result; |
| 256 |
} |
| 257 |
|
| 258 |
if(method_exists($handler_class, 'changePublicAccess') == false) { |
| 259 |
$result = [ |
| 260 |
'success' => false, |
| 261 |
'message' => esc_html__('Method not supported for this service', 'media-cloud-sync') |
| 262 |
]; |
| 263 |
return $result; |
| 264 |
} |
| 265 |
|
| 266 |
$config = isset($data['config']) ? $data['config'] : []; |
| 267 |
$bucketData = isset($data['bucketData']) ? $data['bucketData'] : []; |
| 268 |
if(isset($bucketData['addNew']) && $bucketData['addNew']) { |
| 269 |
$bucketConfig = isset($bucketData['addNewConfig']) ? $bucketData['addNewConfig'] : []; |
| 270 |
} else { |
| 271 |
$bucketConfig = isset($bucketData['config']) ? $bucketData['config'] : []; |
| 272 |
} |
| 273 |
$value = isset($data['value']) ? $data['value'] : false; |
| 274 |
|
| 275 |
return $handler_class->changePublicAccess( $config, $bucketConfig, $value ); |
| 276 |
} |
| 277 |
|
| 278 |
/** |
| 279 |
* Change bucket ownership |
| 280 |
*/ |
| 281 |
public function changeObjectOwnership($data) { |
| 282 |
$result = [ |
| 283 |
'success' => false, |
| 284 |
'message' => esc_html__('Something went wrong', 'media-cloud-sync') |
| 285 |
]; |
| 286 |
|
| 287 |
$service = isset($data['service'])? $data['service'] : false; |
| 288 |
$handler_class = $this->get_handler_class($service); |
| 289 |
|
| 290 |
if($service == false || $handler_class == false) { |
| 291 |
$result = [ |
| 292 |
'success' => false, |
| 293 |
'message' => esc_html__('No service selected', 'media-cloud-sync') |
| 294 |
]; |
| 295 |
return $result; |
| 296 |
} |
| 297 |
|
| 298 |
if(method_exists($handler_class, 'changeObjectOwnership') == false) { |
| 299 |
$result = [ |
| 300 |
'success' => false, |
| 301 |
'message' => esc_html__('Method not supported for this service', 'media-cloud-sync') |
| 302 |
]; |
| 303 |
return $result; |
| 304 |
} |
| 305 |
|
| 306 |
$config = isset($data['config']) ? $data['config'] : []; |
| 307 |
$bucketData = isset($data['bucketData']) ? $data['bucketData'] : []; |
| 308 |
if(isset($bucketData['addNew']) && $bucketData['addNew']) { |
| 309 |
$bucketConfig = isset($bucketData['addNewConfig']) ? $bucketData['addNewConfig'] : []; |
| 310 |
} else { |
| 311 |
$bucketConfig = isset($bucketData['config']) ? $bucketData['config'] : []; |
| 312 |
} |
| 313 |
$value = isset($data['value']) ? $data['value'] : false; |
| 314 |
|
| 315 |
return $handler_class->changeObjectOwnership( $config, $bucketConfig, $value ); |
| 316 |
} |
| 317 |
|
| 318 |
|
| 319 |
/** |
| 320 |
* Generates a URL for a given key in the cloud storage. |
| 321 |
* |
| 322 |
* @param string $key The key of the object in the cloud storage. |
| 323 |
* |
| 324 |
* @return string The URL of the object. |
| 325 |
*/ |
| 326 |
public function get_url($key) { |
| 327 |
return $this->service->generate_file_url($key); |
| 328 |
} |
| 329 |
|
| 330 |
|
| 331 |
/** |
| 332 |
* Checks if a given URL is from a provider. |
| 333 |
* |
| 334 |
* @param string $url The URL to be checked. |
| 335 |
* |
| 336 |
* @return bool True if the URL is from a provider, false otherwise. |
| 337 |
*/ |
| 338 |
public function is_provider_url($url) { |
| 339 |
return $this->service->is_provider_url($url); |
| 340 |
} |
| 341 |
|
| 342 |
|
| 343 |
/** |
| 344 |
* Get private URL |
| 345 |
* @since 1.0.0 |
| 346 |
*/ |
| 347 |
public function get_private_url($path) { |
| 348 |
$url_result = $this->service->get_private_url($path); |
| 349 |
if(isset($url_result['success']) && $url_result['success']) { |
| 350 |
return isset($url_result['file_url']) ? $url_result['file_url'] : false; |
| 351 |
} |
| 352 |
return false; |
| 353 |
} |
| 354 |
|
| 355 |
|
| 356 |
/** |
| 357 |
* Upload a single file to the cloud storage. |
| 358 |
* |
| 359 |
* @param string $file_path The absolute path to the file on the local server. |
| 360 |
* @param string $relative_source_path The relative path to the file on the local server. |
| 361 |
* @param string $prefix An optional prefix to add to the cloud storage path. |
| 362 |
* @return array The result of the upload operation, including success status and any relevant messages. |
| 363 |
*/ |
| 364 |
public function uploadSingle($file_path, $relative_source_path, $prefix = '') { |
| 365 |
return $this->service->uploadSingle($file_path, $relative_source_path, $prefix); |
| 366 |
} |
| 367 |
|
| 368 |
|
| 369 |
public function deleteSingle($key) { |
| 370 |
return $this->service->deleteSingle($key); |
| 371 |
} |
| 372 |
|
| 373 |
|
| 374 |
/** |
| 375 |
* Move object to server from cloud |
| 376 |
*/ |
| 377 |
public function object_to_server($key, $save_path) { |
| 378 |
$path_parts = pathinfo($save_path); |
| 379 |
if (!file_exists($path_parts['dirname'])) { |
| 380 |
mkdir($path_parts['dirname'], 0755, true); |
| 381 |
} |
| 382 |
return $this->service->object_to_server($key, $save_path); |
| 383 |
} |
| 384 |
|
| 385 |
/** |
| 386 |
* Copy an object to a new path in the cloud storage |
| 387 |
* |
| 388 |
* @param string $key The key of the object to be copied |
| 389 |
* @param string $new_path The new path to move the object to |
| 390 |
* @return array The result of the copy operation |
| 391 |
*/ |
| 392 |
public function copy_to_new_path($key, $new_path) { |
| 393 |
return $this->service->copy_to_new_path($key, $new_path); |
| 394 |
} |
| 395 |
|
| 396 |
/** |
| 397 |
* Get Service Handler |
| 398 |
*/ |
| 399 |
public function get_handler_class($service) { |
| 400 |
if(isset($this->providers[$service])) { |
| 401 |
$provider = $this->providers[$service]; |
| 402 |
if(!empty($provider['sdk'])) { |
| 403 |
self::load_sdk($provider['sdk']); |
| 404 |
} |
| 405 |
$class = __NAMESPACE__ . '\\' . $provider['class']; |
| 406 |
if(class_exists($class)) { |
| 407 |
return new $class(); |
| 408 |
} |
| 409 |
} |
| 410 |
return false; |
| 411 |
} |
| 412 |
|
| 413 |
/** |
| 414 |
* Lazy load the bundled SDK autoloader for the given service. |
| 415 |
* Each SDK is required at most once per request. |
| 416 |
* |
| 417 |
* @since 1.3.10 |
| 418 |
*/ |
| 419 |
private static function load_sdk($sdk) { |
| 420 |
static $loaded = []; |
| 421 |
if (isset($loaded[$sdk])) { |
| 422 |
return; |
| 423 |
} |
| 424 |
if ($sdk === 's3') { |
| 425 |
require_once WPMCS_SDK_PATH . 's3/aws-autoloader.php'; |
| 426 |
} elseif ($sdk === 'google') { |
| 427 |
require_once WPMCS_SDK_PATH . 'google/autoload.php'; |
| 428 |
} else { |
| 429 |
return; |
| 430 |
} |
| 431 |
$loaded[$sdk] = true; |
| 432 |
} |
| 433 |
|
| 434 |
/** |
| 435 |
* Get the service domain |
| 436 |
* |
| 437 |
*/ |
| 438 |
public function get_domain() { |
| 439 |
return $this->service->get_domain(); |
| 440 |
} |
| 441 |
|
| 442 |
/** |
| 443 |
* Ensures only one instance of Class is loaded or can be loaded. |
| 444 |
* |
| 445 |
* @return Service Class instance |
| 446 |
* @since 1.0.0 |
| 447 |
* @static |
| 448 |
*/ |
| 449 |
public static function instance(){ |
| 450 |
if (is_null(self::$instance)) { |
| 451 |
self::$instance = new self(); |
| 452 |
} |
| 453 |
return self::$instance; |
| 454 |
} |
| 455 |
} |